# general

Python API: `modelconverter.utils.general`

General-purpose helpers shared across modelconverter.

Holds the small utilities that fit nowhere more specific: sanitizing model names into something the conversion tools accept, and
formatting or parsing the byte sizes used to report on and bound the disk cache.

## Functions

| Name | Description |
| --- | --- |
| [`dir_stats`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/general.md#general-Functions-dir_stats) | Return the total size (bytes) and number of files under `path`. |
| [`human_size`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/general.md#general-Functions-human_size) | Format a number of bytes for display. |
| [`parse_size`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/general.md#general-Functions-parse_size) | Parse a human-written byte size such as `"50GiB"` into bytes. |
| [`sanitize_net_name`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/general.md#general-Functions-sanitize_net_name) | Sanitize net name or path. |

### function `dir_stats`

```python
def dir_stats(path: Path) -> tuple[int, int]
```

Return the total size (bytes) and number of files under `path`.

Entries that cannot be stat'ed are skipped: a container run killed before its entrypoint could chown the mounts back leaves
root-owned files behind, and neither reporting on the cache nor keeping it within its budget must be what breaks.

### function `human_size`

```python
def human_size(num: float) -> str
```

Format a number of bytes for display.

> **Example**
> ```pycon
>>> human_size(1536)
'1.5 KiB'
>>> human_size(5 * 1024**3)
'5.0 GiB'
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `num` | `float` | Size in bytes. | required |

Returns

| Type | Description |
| --- | --- |
| `str` | The size with one decimal place and a binary unit, such as `"1.5 GiB"`. |

### function `parse_size`

```python
def parse_size(value: str | int) -> int
```

Parse a human-written byte size such as `"50GiB"` into bytes.

The unit is optional and its prefixes are binary, matching [human_size](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/general.md#general-Functions-human_size) on the way out and Docker's own byte values on the way in: `50G`, `50GB` and `50GiB` all mean the same 50 * 1024^3 bytes, and a bare number is a count of bytes.

> **Example**
> ```pycon
>>> parse_size("50GiB")
53687091200
>>> parse_size("50GB")
53687091200
>>> parse_size("512m")
536870912
>>> parse_size(1024)
1024
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `value` | `str | int` | Size to parse. An integer is returned unchanged. | required |

Returns

| Type | Description |
| --- | --- |
| `int` | The size in bytes. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If `value` is not a size. |

### function `sanitize_net_name`

```python
def sanitize_net_name(name: str, *, with_suffix: bool = False) -> str
```

Sanitize net name or path.

If input is a path, only sanitize the basename. If input is a name, sanitize the whole string. Collapse multiple underscores.

> **Example**
> ```pycon
>>> sanitize_net_name("my model!.onnx")
'my_model_onnx'
>>> sanitize_net_name("my model!.onnx", with_suffix=True)
'my_model_.onnx'
>>> sanitize_net_name("a/b/my model!.onnx")
'a/b/my_model_onnx'
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `name` | `str` | The name or path to sanitize. | required |
| `with_suffix` | `bool` | If `True`, the suffix (file extension) is preserved and not sanitized. | `False` |

Returns

| Type | Description |
| --- | --- |
| `str` | The sanitized name or path. |
