Overview
Human Friendly Numbers.
Numbers created by bin_(), hex() and bytesize_() and
simply stay in their representation even through calculations if they are the left operand.
Any string conversion results in a pretty formatted number.
Binary:
>>> from humannum import bin_
>>> bin_(42)
Bin('0b101010')
>>> str(bin_(42))
'0b101010'
>>> str(bin_(42) + 24)
'0b1000010'
>>> str(bin_(42, width=16))
'0b0000000000101010'
Hexadecimal
>>> from humannum import hex_
>>> hex_(42)
Hex('0x2A')
>>> str(hex_(42))
'0x2A'
>>> str(hex_(42) + 24)
'0x42'
>>> str(hex_(42, width=16))
'0x002A'
Bytes:
>>> from humannum import bytesize
>>> bytesize_(42)
Bytesize('42 bytes')
>>> str(bytesize_(42))
'42 bytes'
>>> str(bytesize_(42) + 24)
'66 bytes'
>>> str(bytesize_(42*1000))
'42000 bytes'
>>> str(bytesize_(42*1024))
'42 KB'
Frequency
>>> from humannum import freq
>>> freq(42)
Freq('42Hz')
>>> str(freq(42))
'42Hz'
>>> str(freq(42) + 24)
'66Hz'
>>> str(freq(42*1000))
'42kHz'
Modules:
| Name | Description |
|---|---|
baseint |
Base Integer. |
binary |
Binary Class. |
bytesize |
Bytes. |
converter |
Converter. |
freq |
Bytes. |
hex |
Hexadecimal Class. |
Classes:
| Name | Description |
|---|---|
Bin |
Integer with binary representation. |
Bytesize |
Integer with byte size representation. |
Hex |
Integer with hexadecimal representation. |
Functions:
| Name | Description |
|---|---|
int_ |
Convert Integer. |
bin_ |
Integer with binary representation. |
hex_ |
Integer with hexadecial representation. |
bytesize_ |
Integer with hexadecial representation. |
Bin
Bases: BaseInt
Integer with binary representation.
Usage
>>> Bin(50)
Bin('0b110010')
>>> Bin('0b110010')
Bin('0b110010')
>>> str(Bin(50))
'0b110010'
>>> str(Bin(500))
'0b111110100'
This binary value behaves like a normal integer, but the first value dominates.
>>> 8 + Bin(8)
16
>>> str(Bin(8) + 8)
'0b10000'
>>> str(Bin(8) - 2)
'0b110'
>>> str(Bin(8) * 3)
'0b11000'
>>> str(Bin(8) / 3)
'0b10'
>>> str(Bin(8) // 3)
'0b10'
>>> str(Bin(8) % 5)
'0b11'
>>> str(Bin(8) << 1)
'0b10000'
>>> str(Bin(8) >> 1)
'0b100'
>>> str(Bin(8) ** 2)
'0b1000000'
>>> str(Bin(9) & 3)
'0b1'
>>> str(Bin(8) | 3)
'0b1011'
>>> str(Bin(9) ^ 3)
'0b1010'
>>> divmod(Bin(9), 3)
(Bin('0b11'), Bin('0b0'))
>>> str(~Bin(9))
'-0b1010'
>>> str(-Bin(9))
'-0b1001'
>>> str(abs(Bin(-9)))
'0b1001'
>>> str(+Bin(9))
'0b1001'
>>> Bin(8) | 'A'
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for |: 'Bin' and 'str'
>>> divmod(Bin(9), 'A')
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for divmod(): 'Bin' and 'str'
Corner Cases:
>>> str(Bin(0))
'0b0'
>>> str(Bin(-5))
'-0b101'
Width:
>>> a = Bin(3)
>>> str(a)
'0b11'
>>> a.width=6
>>> str(a)
'0b000011'
>>> a = Bin(-3)
>>> str(a)
'-0b11'
>>> a.width=6
>>> str(a)
'-0b000011'
Bytesize
Bases: BaseInt, int
Integer with byte size representation.
Usage
>>> Bytesize(1)
Bytesize('1 byte')
>>> Bytesize('1 byte')
Bytesize('1 byte')
>>> str(Bytesize(1))
'1 byte'
>>> str(Bytesize(50))
'50 bytes'
>>> str(Bytesize(50 * 1024))
'50 KB'
>>> str(Bytesize(1023))
'1023 bytes'
>>> str(Bytesize(1024))
'1 KB'
>>> str(Bytesize(1025))
'1025 bytes'
This value behaves like a normal integer.
>>> str(Bytesize(50) + 50)
'100 bytes'
>>> 50 + Bytesize(50)
100
>>> str(Bytesize(8) - 2)
'6 bytes'
>>> str(Bytesize(8) * 3)
'24 bytes'
>>> str(Bytesize(8) / 3)
'2 bytes'
>>> str(Bytesize(8) // 3)
'2 bytes'
>>> str(Bytesize(8) % 5)
'3 bytes'
>>> str(Bytesize(8) << 1)
'16 bytes'
>>> str(Bytesize(8) >> 1)
'4 bytes'
>>> str(Bytesize(8) ** 2)
'64 bytes'
>>> str(Bytesize(9) & 3)
'1 byte'
>>> str(Bytesize(8) | 3)
'11 bytes'
>>> str(Bytesize(9) ^ 3)
'10 bytes'
>>> str(divmod(Bytesize(9), 3))
"(Bytesize('3 bytes'), Bytesize('0 bytes'))"
>>> str(~Bytesize(9))
'-10 bytes'
>>> str(-Bytesize(9))
'-9 bytes'
>>> str(abs(Bytesize(-9)))
'9 bytes'
>>> str(+Bytesize(9))
'9 bytes'
>>> Bytesize(8) | 'A'
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for |: 'Bytesize' and 'str'
>>> divmod(Bytesize(9), 'A')
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for divmod(): 'Bytesize' and 'str'
An integer can retrieved by
>>> Bytesize(50) + 50
Bytesize('100 bytes')
>>> int(Bytesize(50) + 50)
100
Corner Cases:
>>> Bytesize(0)
Bytesize('0 bytes')
>>> Bytesize(-5)
Bytesize('-5 bytes')
Hex
Bases: BaseInt
Integer with hexadecimal representation.
Usage
>>> Hex(50)
Hex('0x32')
>>> Hex('0x32')
Hex('0x32')
>>> str(Hex(50))
'0x32'
>>> str(Hex(500))
'0x1F4'
This hexadecimal value behaves like a normal integer.
>>> 8 + Hex(8)
16
>>> str(Hex(8) + 8)
'0x10'
>>> str(Hex(8) - 2)
'0x6'
>>> str(Hex(8) * 3)
'0x18'
>>> str(Hex(8) / 3)
'0x2'
>>> str(Hex(8) // 3)
'0x2'
>>> str(Hex(8) % 5)
'0x3'
>>> str(Hex(8) << 1)
'0x10'
>>> str(Hex(8) >> 1)
'0x4'
>>> str(Hex(8) ** 2)
'0x40'
>>> str(Hex(9) & 3)
'0x1'
>>> str(Hex(8) | 3)
'0xB'
>>> str(Hex(9) ^ 3)
'0xA'
>>> divmod(Hex(9), 3)
(Hex('0x3'), Hex('0x0'))
>>> str(~Hex(9))
'-0xA'
>>> str(-Hex(9))
'-0x9'
>>> str(abs(Hex(-9)))
'0x9'
>>> str(+Hex(9))
'0x9'
>>> Hex(8) | 'A'
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for |: 'Hex' and 'str'
>>> divmod(Hex(9), 'A')
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for divmod(): 'Hex' and 'str'
Corner Cases:
>>> str(Hex(0))
'0x0'
>>> str(Hex(-5))
'-0x5'
Width:
>>> a = Hex(3)
>>> str(a)
'0x3'
>>> a.width=6
>>> str(a)
'0x03'
>>> a = Hex(-3)
>>> str(a)
'-0x3'
>>> a.width=6
>>> str(a)
'-0x03'
int_
cached
Convert Integer.
Returns:
| Type | Description |
|---|---|
tuple[int, int | None]
|
Value, Width |
Usage
>>> int_('0h10')
(16, 8)
>>> int_('0h010')
(16, 12)
>>> int_('0d10')
(10, 7)
>>> int_('0x10')
(16, 8)
>>> int_('0o10')
(8, 6)
>>> int_('0b10')
(2, 2)
>>> int_("8'h10")
(16, 8)
>>> int_("8'd10")
(10, 8)
>>> int_("8'o10")
(8, 8)
>>> int_("8'b10")
(2, 8)
bin_
Integer with binary representation.
The binary format is kept through calculations!!!
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
width |
int | None
|
Width in bits. |
Usage
Basics:
>>> bin_(32)
Bin('0b100000')
>>> str(bin_(32) + 3)
'0b100011'
>>> str(bin_(-32))
'-0b100000'
>>> str(bin_("0x50"))
'0b01010000'
>>> str(bin_("-0b1010000"))
'-0b1010000'
>>> str(bin_("0o50"))
'0b101000'
>>> bin_("5Z")
Traceback (most recent call last):
...
ValueError: invalid literal for int() with base 10: '5Z'
The width in bits is optional:
>>> bin_(32, width=16)
Bin('0b0000000000100000')
The width can be also taken from the value:
>>> bin_("16'd50")
Bin('0b0000000000110010')
Smaller widths are not truncated:
>>> bin_("16'd50", width=4)
Bin('0b110010')
hex_
Integer with hexadecial representation.
The hexadecial format is kept through calculations
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
width |
int | None
|
Width in bits. |
Usage
Basics:
>>> hex_(32)
Hex('0x20')
>>> str(hex_(32) + 3)
'0x23'
>>> str(hex_(-32))
'-0x20'
>>> str(hex_("0x50"))
'0x50'
>>> str(hex_("-0b1010000"))
'-0x50'
>>> str(hex_("0o50"))
'0x28'
>>> hex_("5Z")
Traceback (most recent call last):
...
ValueError: invalid literal for int() with base 10: '5Z'
A width in bits is optional:
>>> hex_(32, width=16)
Hex('0x0020')
If given, the default width is taken from the value:
>>> hex_("16'd50")
Hex('0x0032')
Smaller widths are not truncated:
>>> hex_("16'd50", width=4)
Hex('0x32')
bytesize_
Integer with hexadecial representation.
The hexadecial format is kept through calculations
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value |
required |
Usage
bytesize_(3210241024) Bytesize('32 MB') str(bytesize_(3210241024)) '32 MB' str(bytesize_("45000.2 KB")) '46080204 bytes' str(bytesize_(Bytesize(40*1024))) '40 KB' str(bytesize_("0x1000")) '4 KB' str(int(bytesize_("0x1000"))) '4096' str(bytesize_("-0x1000")) '-4096 bytes' bytesize_("5FOO") Traceback (most recent call last): ... ValueError: Invalid number of bytes: '5FOO'