embdgen.core.utils.SizeType

class embdgen.core.utils.SizeType.SizeType(bytes_val: int | None = None)[source]

Bases: object

A type to specify sizes in bytes and sectors

This class can automatically parse string input as decimal or hexadecimal with a unit. Supported units are bytes (B) and Sectors (S) of 512 bytes. For bytes it also supports the SI prefixes for kilo (k), mega (M) and giga (G) with 1 kB == 1024 B.

>>> SizeType(123).bytes
123
>>> SizeType(0).bytes
0
>>> SizeType().bytes is None
True

All comparison functions (<,<=,>,>=,==,!=) are implemented, with None treaded as bigger than any non-None value.

>>> SizeType(1) > SizeType(0)
True
>>> SizeType(None) > SizeType(0)
True
>>> SizeType(None) > SizeType(None)
False
>>> SizeType(None) >= SizeType(None)
True

Some arithmetic functions are implemented as well. For these types None is treated as 0

>>> SizeType(1) + SizeType(2) == SizeType(3)
True
>>> SizeType(1) + SizeType(None) == SizeType(1)
True
>>> SizeType(3) - SizeType(2) == SizeType(1)
True
>>> SizeType(3) - SizeType(None) == SizeType(3)
True
classmethod parse(text: str) SizeType[source]

Parses a size from the a string representation

Examples:

>>> SizeType.parse("0").bytes
0
>>> SizeType.parse("1 S").bytes
512
>>> SizeType.parse("0x10 S").bytes
8192
>>> SizeType.parse("5 MB").bytes
5242880
>>> SizeType.parse("").bytes
Traceback (most recent call last):
...
Exception: Invalid string: 
property is_sector_aligned: bool

Return true, if this is aligned to a sector number

property sectors: int

The sector number of this

Raises:

Exception if not sector aligned

property bytes: int

The number of bytes

property hex_bytes: str

The number of bytes as hex string with at least eight digits

>>> SizeType(0x100).hex_bytes
'0x00000100'
>>> SizeType().hex_bytes
'?'
property is_undefined: bool

Returns true, if no value is set

>>> SizeType().is_undefined
True
>>> SizeType(0).is_undefined
False