• Stars
    star
    1,677
  • Rank 27,846 (Top 0.6 %)
  • Language
    Python
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

python humanize functions

humanize

🚀 Development has moved to https://github.com/python-humanize/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
  • Bengali
  • Brazilian Portuguese
  • Catalan
  • Danish
  • Dutch
  • European Portuguese
  • Finnish
  • French
  • German
  • Indonesian
  • Italian
  • Japanese
  • Korean
  • Persian
  • Polish
  • Russian
  • Simplified Chinese
  • Slovak
  • Slovenian
  • Spanish
  • Swedish
  • Turkish
  • Ukrainian
  • Vietnamese

API reference

https://python-humanize.readthedocs.io

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' -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.

More Repositories

1

sqlx

general purpose extensions to golang's database/sql
Go
16,176
star
2

jsonq

simple json field access for golang
Go
586
star
3

modl

golang database modelling library
Go
479
star
4

johnny-cache

johnny cache django caching framework
Python
305
star
5

monet

golang blog
Go
196
star
6

speedparser

feedparser but faster and worse
Python
101
star
7

gowiki

single-file single-executable wiki written in golang
Go
77
star
8

jigo

an attempt at a jinja2 implementation in go
Go
71
star
9

dmc

dmc runs commands via ssh on multiple machines
Go
34
star
10

django-slow-log

django slow request log
Python
32
star
11

micromongo

tiny fast python orm-ish tools for mongodb
Python
27
star
12

mandira

language agnostic logic-light template system
Go
27
star
13

arachne

a complex but scalable web spider
Python
25
star
14

aranha

simple python gevent web spider
Python
23
star
15

python-github

simple python github api2 library
Python
16
star
16

cm

very simple config manager
Go
15
star
17

par2ools

par2 tools (par2ls, par2mv, par2unrar)
Python
12
star
18

contact-form

server that implements an email contact form for an otherwise static website
Go
11
star
19

what-the-sql

online sql test
Go
11
star
20

euler.go

project euler solutions in go
Go
9
star
21

jmoiron.net

personal blog + website written with flask
Python
8
star
22

dotfiles

dotfiles + cm
Vim Script
8
star
23

jinkies

jinkies is a simple jenkins cli
Python
6
star
24

ongaku

http music player for local files streamable to a chromecast via chrome tab streaming
Go
6
star
25

gaspar

generic eventlet+zmq worker library
Python
5
star
26

kokuen

django-app performance tracking w/ statsd + graphite
Python
5
star
27

pdf2zip

converts image-based pdfs to a zipfile full of extracted images
Python
5
star
28

gevent-memcache-bench

python memcached speed tests w/ and w/o gevent
Python
4
star
29

etod

epoch to date
Go
3
star
30

redtape

simple program to create simple html docs from simple markdown files
Python
3
star
31

uromkan

python unicode romaji to kana conversion
Python
3
star
32

iris

python command-line photo management thing
Python
2
star
33

daneel

Python
2
star
34

chapman

chapman python backup nonsense
Python
2
star
35

nvidia-gpu-temp

Python
1
star
36

terminal-schemer

portable style scheme application for mate-terminal and gnome-terminal
Go
1
star
37

cs101

simplistic explorations of basic CS data structures & algorithms
Go
1
star
38

pctilde

pctilde emulates the behavior of the zsh PS1 var '%~'
Go
1
star
39

golang-sdk

Go
1
star
40

ulv-covers-modern

ulv covers for gregtech ceu modern
Java
1
star