Skip to content

Commit

Permalink
Version 0.0.2, added Datapoint class, basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
siznax committed Oct 17, 2019
1 parent 99ec098 commit cd7856d
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 47 deletions.
95 changes: 54 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
khnum
=====
khnum 𓎹𓃝
==========

<a href="https://en.wikipedia.org/wiki/Khnum"><img
alt="Khnum image" align="right"
src="https://upload.wikimedia.org/wikipedia/commons/5/5e/Khnum.svg"></a>

the god of human readable numbers


Install
-------

```
pip install khnum
𓎹𓃝
```

Usage
Expand All @@ -21,51 +23,62 @@ Usage
import khnum
```

Examples
--------

```
>>> khnum.cnum(1234567890)
'1,234,567,890'
```

```
>>> khnum.hnum(1234567890)
'1.2B'
```

```
>>> khnum.hnum(1234567890, 'bytes')
'1.2GB'
```

```
>>> khnum.hnum(1234567890, 'si')
'1.1GiB'
```

```
>>> num = khnum.num(1234567890, 'b')
>>> print(num)
{
"_num": 1234567890,
"cnum": "1,234,567,890",
"hnum": "1.2GB",
"type": "<class 'int'>",
"units": "b"
}
```


Help
----

```
Help on module khnum.khnum in khnum:
NAME
khnum.khnum
FUNCTIONS
cnum(num)
returns str with thousands separated by commas
>>> khnum.cnum(123456789)
'123,456,789'
hnum(num, units='decimal')
returns rounded human readable str with units suffix
>>> khnum.hnum(123456789) # decimal
'123.5M' # million
>>> khnum.hnum(123456789, 'b') # bytes
'123.5MB' # megabytes
>>> khnum.hnum(123456789, 's') # SI
'117.7MiB' # mebibytes
>>> khnum.hnum(123456789e24, 'si')
'102121062.3YiB' # yobibytes
raises ValueError for un-supported units
Power Decimal Bytes SI (binary)
---------------------------------------------
10^3 Kilo (K) Kilo (KB) 1024^1 Kibi (KiB)
10^6 Mill (M) Mega (MB) 1024^2 Mebi (MiB)
10^9 Bill (B) Giga (GB) 1024^3 Gibi (GiB)
10^12 Tril (T) Tera (TB) 1024^4 Tebi (TiB)
10^15 Quad (Q) Peta (PB) 1024^5 Pebi (PiB)
10^18 Quin (Qn) Exa- (EB) 1024^6 Exbi (EiB)
10^21 Sext (S) Zeta (ZB) 1024^7 Zebi (ZiB)
10^24 Sept (Sp) Yota (YB) 1024^8 Yobi (YiB)
$ pydoc khnum
```

```
>>> help(khnum)
```


Test
----

```
$ pytest
```


Contributors
------------

Expand Down
7 changes: 3 additions & 4 deletions khnum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
human readable numbers
'''

name='khnum'

__author__ = '[email protected]'
__version__ = '0.0.1'
__version__ = '0.0.2'


from .khnum import cnum, hnum
from .datapoint import Datapoint as num
from .khnum import cnum, hnum, pretty
36 changes: 36 additions & 0 deletions khnum/datapoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'''
Datapoint class
~~~~~~~~~~~~~~~
'''

from . import khnum


class Datapoint():

'''
represents a human readable number
'''

def __init__(self, num, units='d'):
'''
returns Datapoint instance
'''
self._num = num
self.cnum = khnum.cnum(num)
self.hnum = khnum.hnum(num, units)
self.type = str(type(num))
self.units = units

def __str__(self):
return khnum.pretty(dict(self.__dict__))

def update(self, num, units='d'):
'''
update datapoint value with number and (optional) khnum units
'''
self.type = str(type(num))
self._num = num
self.cnum = khnum.cnum(num)
self.hnum = khnum.hnum(num, units)
self.units = units
23 changes: 22 additions & 1 deletion khnum/khnum.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
'''
Khnum functions
~~~~~~~~~~~~~~~
'''

import json

UNITS = ['decimal', 'bytes', 'si']


def cnum(num):
'''
returns str with thousands separated by commas
Expand All @@ -7,6 +17,7 @@ def cnum(num):
'''
return "{:,}".format(num)


def hnum(num, units='decimal'):
'''
returns rounded human readable str with units suffix
Expand All @@ -16,7 +27,7 @@ def hnum(num, units='decimal'):
>>> khnum.hnum(123456789, 'b') # bytes
'123.5MB' # megabytes
>>> khnum.hnum(123456789, 's') # SI
'117.7MiB' # mebibytes
Expand Down Expand Up @@ -61,3 +72,13 @@ def hnum(num, units='decimal'):
num /= boundary

return "%.1f%s%s" % (num, last_unit, suffix)


def pretty(data):
'''
returns prettified JSON from data
'''
return json.dumps(data,
indent=4,
sort_keys=True,
separators=(',', ': '))
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
long_description_content_type="text/markdown",
name='khnum',
packages=find_packages(),
tests_require=['pytest'],
url='https://github.com/siznax/khnum/',
version='0.0.1'
version='0.0.2'
)
36 changes: 36 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'''
Khnum basic tests
'''

from khnum import khnum # export PYTHONPATH=..


def test_datapoint():
num = khnum.num(1234567890)
assert num.cnum == '1,234,567,890'
assert num.hnum == '1.2B'

def test_datapoint_bytes():
num = khnum.num(1234567890, 'byte')
assert num.cnum == '1,234,567,890'
assert num.hnum == '1.2GB'

def test_datapoint_print():
num = khnum.num(1234567890)
print(num)

def test_datapoint_si():
num = khnum.num(1234567890, 'si')
assert num.cnum == '1,234,567,890'
assert num.hnum == '1.1GiB'

def test_datapoint_update():
num = khnum.num(1234567890)

assert num.cnum == '1,234,567,890'
assert num.hnum == '1.2B'

num.update(987654321, 'b')

assert num.cnum == '987,654,321'
assert num.hnum == '987.7MB'

0 comments on commit cd7856d

Please sign in to comment.