Skip to main content

python humanize utilities

Project description


.. image::

This modest package contains various common humanization utilities, like turning
a number into a fuzzy human readable duration ('3 minutes ago') or into a human
readable size or throughput. It works with python 2.7 and 3.3 and is localized
to Russian, French, Korean and Slovak.


Integer humanization::

>>> import humanize
>>> humanize.intcomma(12345)
>>> humanize.intword(123455913)
'123.5 million'
>>> humanize.intword(12345591313)
'12.3 billion'
>>> humanize.apnumber(4)
>>> humanize.apnumber(41)

Date & time humanization::

>>> import datetime
>>> humanize.naturalday(
>>> humanize.naturaldelta(datetime.timedelta(seconds=1001))
'16 minutes'
>>> humanize.naturalday( - datetime.timedelta(days=1))
>>> humanize.naturalday(, 6, 5))
'Jun 05'
>>> humanize.naturaldate(, 6, 5))
'Jun 05 2007'
>>> humanize.naturaltime( - datetime.timedelta(seconds=1))
'a second ago'
>>> humanize.naturaltime( - datetime.timedelta(seconds=3600))
'an hour ago'

Filesize humanization::

>>> humanize.naturalsize(1000000)
'1.0 MB'
>>> humanize.naturalsize(1000000, binary=True)
'976.6 KiB'
>>> humanize.naturalsize(1000000, gnu=True)

Human readable floating point numbers::

>>> humanize.fractional(1/3)
>>> humanize.fractional(1.5)
'1 1/2'
>>> humanize.fractional(0.3)
>>> humanize.fractional(0.333)
>>> humanize.fractional(1)


How to change locale in runtime ::

>>> print humanize.naturaltime(datetime.timedelta(seconds=3))
3 seconds ago
>>> _t = humanize.i18n.activate('ru_RU')
>>> print humanize.naturaltime(datetime.timedelta(seconds=3))
3 секунды назад
>>> humanize.i18n.deactivate()
>>> print humanize.naturaltime(datetime.timedelta(seconds=3))
3 seconds ago

You can pass additional parameter *path* to :func:`activate` to specify a path to
search locales in. ::

>>> humanize.i18n.activate('pt_BR')
IOError: [Errno 2] No translation file found for domain: 'humanize'
>>> humanize.i18n.activate('pt_BR', path='path/to/my/portuguese/translation/')
<gettext.GNUTranslations instance ...>

How to add new phrases to existing locale files ::

$ xgettext -o humanize.pot -k'_' -k'N_' -k'P_:1c,2' -l python humanize/*.py # extract new phrases
$ msgmerge -U humanize/locale/ru_RU/LC_MESSAGES/humanize.po humanize.pot # add them to locale files
$ msgfmt --check -o humanize/locale/ru_RU/LC_MESSAGES/humanize{.mo,.po} # compile to binary .mo

How to add new locale ::

$ msginit -i humanize.pot -o humanize/locale/<locale name>/LC_MESSAGES/humanize.po --locale <locale name>

Where <locale name> is locale abbreviation, eg 'en_GB', 'pt_BR' or just 'ru', 'fr' etc.

Project details

Download files

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

Files for humanize-k, version 0.5.2
Filename, size File type Python version Upload date Hashes
Filename, size humanize_k-0.5.2-py2-none-any.whl (33.0 kB) File type Wheel Python version py2 Upload date Hashes View
Filename, size humanize-k-0.5.2.tar.gz (18.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page