Python humanize utilities
Project description
humanize
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 is localized to:
- Arabic
- Basque
- Bengali
- Brazilian Portuguese
- Catalan
- Danish
- Dutch
- Esperanto
- European Portuguese
- Finnish
- French
- German
- Greek
- Hebrew
- Indonesian
- Italian
- Japanese
- Klingon
- Korean
- Norwegian
- Persian
- Polish
- Russian
- Simplified Chinese
- Slovak
- Slovenian
- Spanish
- Swedish
- Turkish
- Ukrainian
- Uzbek
- Vietnamese
API reference
https://humanize.readthedocs.io
Installation
From PyPI
python3 -m pip install --upgrade humanize
From source
git clone https://github.com/python-humanize/humanize
cd humanize
python3 -m pip install -e .
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 humanize
>>> import datetime as dt
>>> humanize.naturalday(dt.datetime.now())
'today'
>>> humanize.naturaldelta(dt.timedelta(seconds=1001))
'16 minutes'
>>> humanize.naturalday(dt.datetime.now() - dt.timedelta(days=1))
'yesterday'
>>> humanize.naturalday(dt.date(2007, 6, 5))
'Jun 05'
>>> humanize.naturaldate(dt.date(2007, 6, 5))
'Jun 05 2007'
>>> humanize.naturaltime(dt.datetime.now() - dt.timedelta(seconds=1))
'a second ago'
>>> humanize.naturaltime(dt.datetime.now() - dt.timedelta(seconds=3600))
'an hour ago'
Precise time delta
>>> import humanize
>>> import datetime as dt
>>> delta = dt.timedelta(seconds=3633, days=2, microseconds=123000)
>>> humanize.precisedelta(delta)
'2 days, 1 hour and 33.12 seconds'
>>> humanize.precisedelta(delta, minimum_unit="microseconds")
'2 days, 1 hour, 33 seconds and 123 milliseconds'
>>> humanize.precisedelta(delta, suppress=["days"], format="%0.4f")
'49 hours and 33.1230 seconds'
Smaller units
If seconds are too large, set minimum_unit to milliseconds or microseconds:
>>> import humanize
>>> import datetime as dt
>>> humanize.naturaldelta(dt.timedelta(seconds=2))
'2 seconds'
>>> delta = dt.timedelta(milliseconds=4)
>>> humanize.naturaldelta(delta)
'a moment'
>>> humanize.naturaldelta(delta, minimum_unit="milliseconds")
'4 milliseconds'
>>> humanize.naturaldelta(delta, minimum_unit="microseconds")
'4 milliseconds'
>>> humanize.naturaltime(delta)
'now'
>>> humanize.naturaltime(delta, minimum_unit="milliseconds")
'4 milliseconds ago'
>>> humanize.naturaltime(delta, minimum_unit="microseconds")
'4 milliseconds ago'
File size humanization
>>> import humanize
>>> humanize.naturalsize(1_000_000)
'1.0 MB'
>>> humanize.naturalsize(1_000_000, binary=True)
'976.6 KiB'
>>> humanize.naturalsize(1_000_000, gnu=True)
'976.6K'
Human-readable floating point numbers
>>> import humanize
>>> humanize.fractional(1/3)
'1/3'
>>> humanize.fractional(1.5)
'1 1/2'
>>> humanize.fractional(0.3)
'3/10'
>>> humanize.fractional(0.333)
'333/1000'
>>> humanize.fractional(1)
'1'
Scientific notation
>>> import humanize
>>> humanize.scientific(0.3)
'3.00 x 10⁻¹'
>>> humanize.scientific(500)
'5.00 x 10²'
>>> humanize.scientific("20000")
'2.00 x 10⁴'
>>> humanize.scientific(1**10)
'1.00 x 10⁰'
>>> humanize.scientific(1**10, precision=1)
'1.0 x 10⁰'
>>> humanize.scientific(1**10, precision=0)
'1 x 10⁰'
Localization
How to change locale at runtime:
>>> import humanize
>>> import datetime as dt
>>> humanize.naturaltime(dt.timedelta(seconds=3))
'3 seconds ago'
>>> _t = humanize.i18n.activate("ru_RU")
>>> humanize.naturaltime(dt.timedelta(seconds=3))
'3 секунды назад'
>>> humanize.i18n.deactivate()
>>> humanize.naturaltime(dt.timedelta(seconds=3))
'3 seconds ago'
You can pass additional parameter path to activate to specify a path to search
locales in.
>>> import humanize
>>> humanize.i18n.activate("xx_XX")
<...>
FileNotFoundError: [Errno 2] No translation file found for domain: 'humanize'
>>> humanize.i18n.activate("pt_BR", path="path/to/my/own/translation/")
<gettext.GNUTranslations instance ...>
How to add new phrases to existing locale files:
xgettext --from-code=UTF-8 -o humanize.pot -k'_' -k'N_' -k'P_:1c,2' -k'NS_:1,2' -k'_ngettext:1,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
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.
List the language at the top of this README.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file humanize-4.14.0.tar.gz.
File metadata
- Download URL: humanize-4.14.0.tar.gz
- Upload date:
- Size: 82.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2fa092705ea640d605c435b1ca82b2866a1b601cdf96f076d70b79a855eba90d
|
|
| MD5 |
acac6b2378dd0d9b1d9d1ffefb08e00b
|
|
| BLAKE2b-256 |
b64350033d25ad96a7f3845f40999b4778f753c3901a11808a584fed7c00d9f5
|
Provenance
The following attestation bundles were made for humanize-4.14.0.tar.gz:
Publisher:
release.yml on python-humanize/humanize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
humanize-4.14.0.tar.gz -
Subject digest:
2fa092705ea640d605c435b1ca82b2866a1b601cdf96f076d70b79a855eba90d - Sigstore transparency entry: 607796931
- Sigstore integration time:
-
Permalink:
python-humanize/humanize@fedf432b5bd7751b3417447b647e43aa2ac7507d -
Branch / Tag:
refs/tags/4.14.0 - Owner: https://github.com/python-humanize
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@fedf432b5bd7751b3417447b647e43aa2ac7507d -
Trigger Event:
release
-
Statement type:
File details
Details for the file humanize-4.14.0-py3-none-any.whl.
File metadata
- Download URL: humanize-4.14.0-py3-none-any.whl
- Upload date:
- Size: 132.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d57701248d040ad456092820e6fde56c930f17749956ac47f4f655c0c547bfff
|
|
| MD5 |
8fee0653cdf0a32f081cfe72efba085c
|
|
| BLAKE2b-256 |
c35b9512c5fb6c8218332b530f13500c6ff5f3ce3342f35e0dd7be9ac3856fd3
|
Provenance
The following attestation bundles were made for humanize-4.14.0-py3-none-any.whl:
Publisher:
release.yml on python-humanize/humanize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
humanize-4.14.0-py3-none-any.whl -
Subject digest:
d57701248d040ad456092820e6fde56c930f17749956ac47f4f655c0c547bfff - Sigstore transparency entry: 607796932
- Sigstore integration time:
-
Permalink:
python-humanize/humanize@fedf432b5bd7751b3417447b647e43aa2ac7507d -
Branch / Tag:
refs/tags/4.14.0 - Owner: https://github.com/python-humanize
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@fedf432b5bd7751b3417447b647e43aa2ac7507d -
Trigger Event:
release
-
Statement type: