humanize


Namehumanize JSON
Version 4.11.0 PyPI version JSON
download
home_pageNone
SummaryPython humanize utilities
upload_time2024-10-05 14:28:32
maintainerHugo van Kemenade
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT
keywords humanize time size
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # humanize

[![PyPI version](https://img.shields.io/pypi/v/humanize.svg?logo=pypi&logoColor=FFE873)](https://pypi.org/project/humanize/)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/humanize.svg?logo=python&logoColor=FFE873)](https://pypi.org/project/humanize/)
[![Documentation Status](https://readthedocs.org/projects/python-humanize/badge/?version=latest)](https://humanize.readthedocs.io/en/latest/?badge=latest)
[![PyPI downloads](https://img.shields.io/pypi/dm/humanize.svg)](https://pypistats.org/packages/humanize)
[![GitHub Actions status](https://github.com/python-humanize/humanize/workflows/Test/badge.svg)](https://github.com/python-humanize/humanize/actions)
[![codecov](https://codecov.io/gh/python-humanize/humanize/branch/main/graph/badge.svg)](https://codecov.io/gh/python-humanize/humanize)
[![MIT License](https://img.shields.io/github/license/python-humanize/humanize.svg)](LICENCE)
[![Tidelift](https://tidelift.com/badges/package/pypi/humanize)](https://tidelift.com/subscription/pkg/pypi-humanize?utm_source=pypi-humanize&utm_medium=badge)

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
- Vietnamese

## API reference

[https://humanize.readthedocs.io](https://humanize.readthedocs.io/)

<!-- usage-start -->

## Installation

### From PyPI

```bash
python3 -m pip install --upgrade humanize
```

### From source

```bash
git clone https://github.com/python-humanize/humanize
cd humanize
python3 -m pip install -e .
```

## Usage

### Integer humanization

```pycon
>>> 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

```pycon
>>> 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

```pycon
>>> 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:

```pycon
>>> import humanize
>>> import datetime as dt
>>> humanize.naturaldelta(dt.timedelta(seconds=2))
'2 seconds'
```

```pycon
>>> 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'
```

```pycon
>>> 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

```pycon
>>> 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

```pycon
>>> 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

```pycon
>>> 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:

```pycon
>>> 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.

```pycon
>>> 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 ...>
```

<!-- usage-end -->

How to add new phrases to existing locale files:

```sh
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:

```sh
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.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "humanize",
    "maintainer": "Hugo van Kemenade",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "humanize time size",
    "author": null,
    "author_email": "Jason Moiron <jmoiron@jmoiron.net>",
    "download_url": "https://files.pythonhosted.org/packages/6a/40/64a912b9330786df25e58127194d4a5a7441f818b400b155e748a270f924/humanize-4.11.0.tar.gz",
    "platform": null,
    "description": "# humanize\n\n[![PyPI version](https://img.shields.io/pypi/v/humanize.svg?logo=pypi&logoColor=FFE873)](https://pypi.org/project/humanize/)\n[![Supported Python versions](https://img.shields.io/pypi/pyversions/humanize.svg?logo=python&logoColor=FFE873)](https://pypi.org/project/humanize/)\n[![Documentation Status](https://readthedocs.org/projects/python-humanize/badge/?version=latest)](https://humanize.readthedocs.io/en/latest/?badge=latest)\n[![PyPI downloads](https://img.shields.io/pypi/dm/humanize.svg)](https://pypistats.org/packages/humanize)\n[![GitHub Actions status](https://github.com/python-humanize/humanize/workflows/Test/badge.svg)](https://github.com/python-humanize/humanize/actions)\n[![codecov](https://codecov.io/gh/python-humanize/humanize/branch/main/graph/badge.svg)](https://codecov.io/gh/python-humanize/humanize)\n[![MIT License](https://img.shields.io/github/license/python-humanize/humanize.svg)](LICENCE)\n[![Tidelift](https://tidelift.com/badges/package/pypi/humanize)](https://tidelift.com/subscription/pkg/pypi-humanize?utm_source=pypi-humanize&utm_medium=badge)\n\nThis modest package contains various common humanization utilities, like turning a\nnumber into a fuzzy human-readable duration (\"3 minutes ago\") or into a human-readable\nsize or throughput. It is localized to:\n\n- Arabic\n- Basque\n- Bengali\n- Brazilian Portuguese\n- Catalan\n- Danish\n- Dutch\n- Esperanto\n- European Portuguese\n- Finnish\n- French\n- German\n- Greek\n- Hebrew\n- Indonesian\n- Italian\n- Japanese\n- Klingon\n- Korean\n- Norwegian\n- Persian\n- Polish\n- Russian\n- Simplified Chinese\n- Slovak\n- Slovenian\n- Spanish\n- Swedish\n- Turkish\n- Ukrainian\n- Vietnamese\n\n## API reference\n\n[https://humanize.readthedocs.io](https://humanize.readthedocs.io/)\n\n<!-- usage-start -->\n\n## Installation\n\n### From PyPI\n\n```bash\npython3 -m pip install --upgrade humanize\n```\n\n### From source\n\n```bash\ngit clone https://github.com/python-humanize/humanize\ncd humanize\npython3 -m pip install -e .\n```\n\n## Usage\n\n### Integer humanization\n\n```pycon\n>>> import humanize\n>>> humanize.intcomma(12345)\n'12,345'\n>>> humanize.intword(123455913)\n'123.5 million'\n>>> humanize.intword(12345591313)\n'12.3 billion'\n>>> humanize.apnumber(4)\n'four'\n>>> humanize.apnumber(41)\n'41'\n```\n\n### Date & time humanization\n\n```pycon\n>>> import humanize\n>>> import datetime as dt\n>>> humanize.naturalday(dt.datetime.now())\n'today'\n>>> humanize.naturaldelta(dt.timedelta(seconds=1001))\n'16 minutes'\n>>> humanize.naturalday(dt.datetime.now() - dt.timedelta(days=1))\n'yesterday'\n>>> humanize.naturalday(dt.date(2007, 6, 5))\n'Jun 05'\n>>> humanize.naturaldate(dt.date(2007, 6, 5))\n'Jun 05 2007'\n>>> humanize.naturaltime(dt.datetime.now() - dt.timedelta(seconds=1))\n'a second ago'\n>>> humanize.naturaltime(dt.datetime.now() - dt.timedelta(seconds=3600))\n'an hour ago'\n```\n\n### Precise time delta\n\n```pycon\n>>> import humanize\n>>> import datetime as dt\n>>> delta = dt.timedelta(seconds=3633, days=2, microseconds=123000)\n>>> humanize.precisedelta(delta)\n'2 days, 1 hour and 33.12 seconds'\n>>> humanize.precisedelta(delta, minimum_unit=\"microseconds\")\n'2 days, 1 hour, 33 seconds and 123 milliseconds'\n>>> humanize.precisedelta(delta, suppress=[\"days\"], format=\"%0.4f\")\n'49 hours and 33.1230 seconds'\n```\n\n#### Smaller units\n\nIf seconds are too large, set `minimum_unit` to milliseconds or microseconds:\n\n```pycon\n>>> import humanize\n>>> import datetime as dt\n>>> humanize.naturaldelta(dt.timedelta(seconds=2))\n'2 seconds'\n```\n\n```pycon\n>>> delta = dt.timedelta(milliseconds=4)\n>>> humanize.naturaldelta(delta)\n'a moment'\n>>> humanize.naturaldelta(delta, minimum_unit=\"milliseconds\")\n'4 milliseconds'\n>>> humanize.naturaldelta(delta, minimum_unit=\"microseconds\")\n'4 milliseconds'\n```\n\n```pycon\n>>> humanize.naturaltime(delta)\n'now'\n>>> humanize.naturaltime(delta, minimum_unit=\"milliseconds\")\n'4 milliseconds ago'\n>>> humanize.naturaltime(delta, minimum_unit=\"microseconds\")\n'4 milliseconds ago'\n```\n\n### File size humanization\n\n```pycon\n>>> import humanize\n>>> humanize.naturalsize(1_000_000)\n'1.0 MB'\n>>> humanize.naturalsize(1_000_000, binary=True)\n'976.6 KiB'\n>>> humanize.naturalsize(1_000_000, gnu=True)\n'976.6K'\n```\n\n### Human-readable floating point numbers\n\n```pycon\n>>> import humanize\n>>> humanize.fractional(1/3)\n'1/3'\n>>> humanize.fractional(1.5)\n'1 1/2'\n>>> humanize.fractional(0.3)\n'3/10'\n>>> humanize.fractional(0.333)\n'333/1000'\n>>> humanize.fractional(1)\n'1'\n```\n\n### Scientific notation\n\n```pycon\n>>> import humanize\n>>> humanize.scientific(0.3)\n'3.00 x 10\u207b\u00b9'\n>>> humanize.scientific(500)\n'5.00 x 10\u00b2'\n>>> humanize.scientific(\"20000\")\n'2.00 x 10\u2074'\n>>> humanize.scientific(1**10)\n'1.00 x 10\u2070'\n>>> humanize.scientific(1**10, precision=1)\n'1.0 x 10\u2070'\n>>> humanize.scientific(1**10, precision=0)\n'1 x 10\u2070'\n```\n\n## Localization\n\nHow to change locale at runtime:\n\n```pycon\n>>> import humanize\n>>> import datetime as dt\n>>> humanize.naturaltime(dt.timedelta(seconds=3))\n'3 seconds ago'\n>>> _t = humanize.i18n.activate(\"ru_RU\")\n>>> humanize.naturaltime(dt.timedelta(seconds=3))\n'3 \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u043d\u0430\u0437\u0430\u0434'\n>>> humanize.i18n.deactivate()\n>>> humanize.naturaltime(dt.timedelta(seconds=3))\n'3 seconds ago'\n```\n\nYou can pass additional parameter `path` to `activate` to specify a path to search\nlocales in.\n\n```pycon\n>>> import humanize\n>>> humanize.i18n.activate(\"xx_XX\")\n<...>\nFileNotFoundError: [Errno 2] No translation file found for domain: 'humanize'\n>>> humanize.i18n.activate(\"pt_BR\", path=\"path/to/my/own/translation/\")\n<gettext.GNUTranslations instance ...>\n```\n\n<!-- usage-end -->\n\nHow to add new phrases to existing locale files:\n\n```sh\nxgettext --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\nmsgmerge -U src/humanize/locale/ru_RU/LC_MESSAGES/humanize.po humanize.pot # add them to locale files\n```\n\nHow to add a new locale:\n\n```sh\nmsginit -i humanize.pot -o humanize/locale/<locale name>/LC_MESSAGES/humanize.po --locale <locale name>\n```\n\nWhere `<locale name>` is a locale abbreviation, eg. `en_GB`, `pt_BR` or just `ru`, `fr`\netc.\n\nList the language at the top of this README.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python humanize utilities",
    "version": "4.11.0",
    "project_urls": {
        "Documentation": "https://humanize.readthedocs.io/",
        "Funding": "https://tidelift.com/subscription/pkg/pypi-humanize?utm_source=pypi-humanize&utm_medium=pypi",
        "Homepage": "https://github.com/python-humanize/humanize",
        "Issue tracker": "https://github.com/python-humanize/humanize/issues",
        "Release notes": "https://github.com/python-humanize/humanize/releases",
        "Source": "https://github.com/python-humanize/humanize"
    },
    "split_keywords": [
        "humanize",
        "time",
        "size"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "92754bc3e242ad13f2e6c12e0b0401ab2c5e5c6f0d7da37ec69bc808e24e0ccb",
                "md5": "5ae00a2bdf399eb395598dfcbca1df90",
                "sha256": "b53caaec8532bcb2fff70c8826f904c35943f8cecaca29d272d9df38092736c0"
            },
            "downloads": -1,
            "filename": "humanize-4.11.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5ae00a2bdf399eb395598dfcbca1df90",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 128055,
            "upload_time": "2024-10-05T14:28:30",
            "upload_time_iso_8601": "2024-10-05T14:28:30.082908Z",
            "url": "https://files.pythonhosted.org/packages/92/75/4bc3e242ad13f2e6c12e0b0401ab2c5e5c6f0d7da37ec69bc808e24e0ccb/humanize-4.11.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6a4064a912b9330786df25e58127194d4a5a7441f818b400b155e748a270f924",
                "md5": "c37298e6bc6604b54aeb0e158c7c0512",
                "sha256": "e66f36020a2d5a974c504bd2555cf770621dbdbb6d82f94a6857c0b1ea2608be"
            },
            "downloads": -1,
            "filename": "humanize-4.11.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c37298e6bc6604b54aeb0e158c7c0512",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 80374,
            "upload_time": "2024-10-05T14:28:32",
            "upload_time_iso_8601": "2024-10-05T14:28:32.263361Z",
            "url": "https://files.pythonhosted.org/packages/6a/40/64a912b9330786df25e58127194d4a5a7441f818b400b155e748a270f924/humanize-4.11.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-05 14:28:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "python-humanize",
    "github_project": "humanize",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "humanize"
}
        
Elapsed time: 0.37804s