Skip to main content

Python humanize utilities

Project description

humanize

PyPI version Supported Python versions PyPI downloads Travis CI Status GitHub Actions status codecov MIT License

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.5+ and is localized to:

  • Dutch
  • Finnish
  • French
  • German
  • Indonesian
  • Italian
  • Japanese
  • Korean
  • Persian
  • Portuguese Brazilian
  • Russian
  • Simplified Chinese
  • Slovak
  • Turkish
  • Vietnamese

Usage

Integer humanization:

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

Date & time humanization:

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

File size humanization:

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

Human readable floating point numbers:

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

Localization

How to change locale at 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 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 src/humanize/*.py  # extract new phrases
$ msgmerge -U src/humanize/locale/ru_RU/LC_MESSAGES/humanize.po humanize.pot # add them to locale files
$ msgfmt --check -o src/humanize/locale/ru_RU/LC_MESSAGES/humanize{.mo,.po} # compile to binary .mo

How to add a new locale:

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

Where <locale name> is a 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.

Source Distribution

humanize-1.0.0.tar.gz (34.4 kB view details)

Uploaded Source

Built Distribution

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

humanize-1.0.0-py2.py3-none-any.whl (51.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file humanize-1.0.0.tar.gz.

File metadata

  • Download URL: humanize-1.0.0.tar.gz
  • Upload date:
  • Size: 34.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for humanize-1.0.0.tar.gz
Algorithm Hash digest
SHA256 38ace9b66bcaeb7f8186b9dbf0b3448e00148e5b4fbaf726f96c789e52c3e741
MD5 9f1eecc0ef17f4eecc60a29e3889d72a
BLAKE2b-256 30426431a1b451ec7101c2b597c2d099da241dd30bc8390ecc48bf737bbb6444

See more details on using hashes here.

File details

Details for the file humanize-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: humanize-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 51.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for humanize-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3478104dcb9e111991ad141b15c9bf9522aa00ccfc5144561d639b3372e1d064
MD5 3f4eacd52482db6d4c107da3ed904b20
BLAKE2b-256 4a736c3e7fae17203610523953f35ce9fefebb61d72b5af1c5ecd1e3fd23ada7

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