numtxt 1.2

black_moshannon

Yet another update on the numtxt module. Technically, it’s now 1.2.1 but what the hey.

(previous post)
(the module)
(GitHub)

So what’s new? Well last time I wondered about adding an SI prefix function – so that’s one of the new things:

>>> numtxt.si(299792458/475e-09, 'Hz')
'631.142 THz'

That’s just the basic use of it. It can take several different keywords. Want to use the full prefix name? Change the formatting of number? You can do that:

>>> numtxt.si(299792458/475e-09, 'hertz', name=True, fmt='{:3.0f}')
'631 terahertz'

Now, it defaults to just using the power of thousand prefixes (kilo, milli, mega, mirco, etc…), but if you want to use the power of ten prefixes (hector, centi, deca, deci), just change the base to 10:

>>> numtxt.si(130)
'130.000'
>>> numtxt.si(130, base=10)
'1.300 h'

A “hidden” use of setting the base here allows one to show how much memory in bytes something is, when displayed on the Windows operating system:

>>> import shutil
>>> stats = shutil.disk_usage('C:/ ')
>>> numtxt.si(stats[0], 'B') # total drive size, Linux
'1.000 TB'
>>> numtxt.si(stats[0], 'B', base=1024) # Windows
'931.414 GB'

Doing it that way, by the way, is an entire subject matter on its own.

Now the way this function is coded, should si prefixes ever be extended, the function itself is left alone and only the dictionary table containing the prefixes is updated. And that might happen “soon” – there’s a proposal to add more. Since it’s not official yet, those prefixes are not used – but if you want to try them out, they’re already hidden in the code behind a comment character. So once the module’s updated, open the file and scroll to where _si_prefixes and _si_pfx_names are defined and uncomment them. If you’re reading this after the proposal has been approved (or other si prefix changes have happened), then I should have it updated by then and you can disregard this paragraph!

Additionally, the number formatting that the si function has, has been added to the approx function:

>>> numtxt.approx(5471619276639877320977)
'5.472 sextillion'
>>> numtxt.approx(5471619276639877320977, fmt='{:3.0f}')
' 5 sextillion'

And finally, handling of really small numbers has been restored to the approx function. It was there for the first version, but removed soon after as it just wasn’t working right. But after posting a question online where a guy’s response didn’t understand at all what I was asking about (“You can’t use at most 17 digits of a float! You must account for all 50+ digits of a float and you need a method to pick which of those digits are the right ones!”), then asking a semantics question to my sister (“You’d want to say in this manner”), I’m restoring the feature. It shows ups below a tenth:

>>> numtxt.approx(0.1)
'0'
>>> numtxt.approx(0.09)
'90.000 thousandths'

I’m not sure if it should even be, but since the approx function is to give you a sense of size in a written form, perhaps it should.
You can get it with pip install numtxt or upgrade via pip install --upgrade numtxt

P.S. oh yeah, there’s also three other functions as well:

>>> numtxt.gen10_dict(10)
{0: '', 3: 'thousand', 6: 'million', 9: 'billion', 12: 'trillion', 15: 'quadrillion', 18: 'quintillion', 21: 'sextillion', 24: 'septillion', 27: 'octillion', 30: 'nonillion'}
>>> numtxt.gen1k_dict(10)
{0: '', 1: 'thousand', 2: 'million', 3: 'billion', 4: 'trillion', 5: 'quadrillion', 6: 'quintillion', 7: 'sextillion', 8: 'septillion', 9: 'octillion', 10: 'nonillion'}
>>> numtxt.table(10)
 n  | cardinal | ordinal | precedence |   tuple   
----+----------+---------+------------+-----------
 0  |   zero   | zeroth  |  nullary   |   empty   
 1  |   one    |  first  |  primary   |  single   
 2  |   two    | second  | secondary  |  double   
 3  |  three   |  third  |  tertiary  |  triple   
 4  |   four   | fourth  | quaternary | quadruple 
 5  |   five   |  fifth  |  quinary   | quintuple 
 6  |   six    |  sixth  |   senary   | sextuple  
 7  |  seven   | seventh | septenary  | septuple  
 8  |  eight   | eighth  |  octonary  |  octuple  
 9  |   nine   |  ninth  |   nonary   |  nonuple  
 10 |   ten    |  tenth  |   denary   |  decuple  
Tagged with: , , ,
Posted in Python

Leave a comment

In Archive
Design a site like this with WordPress.com
Get started