Skip to main content

Human-readable number formatting and hex string utilities

Project description

k3num

Build Status Python package Documentation Status Package

Convert number to human readable format in a string.

k3num is a component of pykit3 project: a python3 toolkit set.

k3num convert numbers(or numbers in dict or list) to human readable format in string::

>>> readable(103425)
'101.0K'
>>> readable({ 'total': 10240, 'progress': [1, 1024*2.1, 1024*3.2], })
{'total': '10K', 'progress': ['1', '2.10K', '3.20K']}
>>> parsenum('5.2K')
5324.8
>>> parsenum('10%')
0.1

Attributes: value_to_unit(dict): map of int to unit, e.g.: 1024 -> "K", 1024² -> "M".

    Usage::

        >>> value_to_unit[1024**2]
        'M'

        >>> unit_to_value['K']
        1024

unit_to_value(dict): reverse map of `value_to_unit`.

k3num.Hex provides hex integer with arithmatic support.

Different ways to create a 4-byte crc32 hex str::

>>> Hex(0x0102, 4)
'00000102'
>>> Hex(0x0102, 'crc32')
'00000102'
>>> Hex.crc32(0x0102)
'00000102'
>>> Hex('00000102', 'crc32')
'00000102'
>>> Hex.crc32('00000102')
'00000102'

Create with a tuple of prefix and a filling byte::

>>> Hex(('12', 1), 'crc32')
'12010101'

Arithmetic operations::

>>> Hex(0x0102, 'crc32') + 1
'00000103'
>>> Hex(0x0102, 'crc32') * 2
'00000204'
>>> Hex(0x0102, 'crc32') - 1000000
'00000000'
>>> Hex(0x0102, 'crc32') * 1000000000
'ffffffff'

Iterate over sha1 space with a specific step::

>>> Hex.sha1(0) + Hex.sha1(('10', 0))
'1000000000000000000000000000000000000000'
>>> Hex.sha1(0) + Hex.sha1(('10', 0)) * 2
'2000000000000000000000000000000000000000'

Install

pip install k3num

Synopsis

>>> readable(103425)
'101.0K'
>>> readable({ 'total': 10240, 'progress': [1, 1024*2.1, 1024*3.2], })
{'total': '10K', 'progress': ['1', '2.10K', '3.20K']}
>>> parsenum('5.2K')
5324.8
>>> parsenum('10%')
0.1
>>> value_to_unit[1024**2]
'M'
>>> unit_to_value['K']
1024
>>> Hex(0x0102, 4)
'00000102'
>>> Hex(0x0102, 'crc32')
'00000102'
>>> Hex.crc32(0x0102)
'00000102'
>>> Hex('00000102', 'crc32')
'00000102'
>>> Hex.crc32('00000102')
'00000102'
>>> Hex(('12', 1), 'crc32')
'12010101'
>>> Hex(0x0102, 'crc32') + 1
'00000103'
>>> Hex(0x0102, 'crc32') * 2
'00000204'
>>> Hex(0x0102, 'crc32') - 1000000
'00000000'
>>> Hex(0x0102, 'crc32') * 1000000000
'ffffffff'
>>> Hex.sha1(0) + Hex.sha1(('10', 0))
'1000000000000000000000000000000000000000'
>>> Hex.sha1(0) + Hex.sha1(('10', 0)) * 2
'2000000000000000000000000000000000000000'

Author

Zhang Yanpo (张炎泼) drdr.xp@gmail.com

Copyright and License

The MIT License (MIT)

Copyright (c) 2015 Zhang Yanpo (张炎泼) drdr.xp@gmail.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

k3num-0.1.9.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

k3num-0.1.9-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file k3num-0.1.9.tar.gz.

File metadata

  • Download URL: k3num-0.1.9.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for k3num-0.1.9.tar.gz
Algorithm Hash digest
SHA256 a77a9a6ae69c96977d31858872f240834363df6145f04b577e4cf2c3aef48498
MD5 46cfdc938b8c30ffee314d857f0ed88e
BLAKE2b-256 8940cbfc7b86a035f1f9a2f61ac75b7b0710e1e2e2164559aa0140b9a00f5254

See more details on using hashes here.

File details

Details for the file k3num-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: k3num-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for k3num-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 a8b0a7cbaa3e641a3359e37cd5bd687de98645d475a27f96441d1e6d05b07886
MD5 a03713f450a72153b9354cf69bc7d47d
BLAKE2b-256 b5705cd1b443735ee3b695713901a3e2bab039e4b57acd39da07ed1e2afc66a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page