Skip to content

Commit 9d9de69

Browse files
authored
Merge pull request #168 from carlpatt/tests
2 parents d30c2b6 + 8819ece commit 9d9de69

4 files changed

Lines changed: 113 additions & 0 deletions

File tree

src/humanize/filesize.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ def naturalsize(value, binary=False, gnu=False, format="%.1f"):
1616
1717
Non-GNU modes are compatible with jinja2's `filesizeformat` filter.
1818
19+
Examples:
20+
```pycon
21+
>>> naturalsize(3000000)
22+
'3.0 MB'
23+
>>> naturalsize(300, False, True)
24+
'300B'
25+
>>> naturalsize(3000, False, True)
26+
'2.9K'
27+
>>> naturalsize(3000, False, True, "%.3f")
28+
'2.930K'
29+
>>> naturalsize(3000, True)
30+
'2.9 KiB'
31+
32+
```
1933
Args:
2034
value (int, float, str): Integer to convert.
2135
binary (bool): If `True`, uses binary suffixes (KiB, MiB) with base

src/humanize/number.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ def ordinal(value):
1717
anything `int()` will turn into an integer. Anything other value will have nothing
1818
done to it.
1919
20+
Examples:
21+
```pycon
22+
>>> ordinal(1)
23+
'1st'
24+
>>> ordinal(1002)
25+
'1002nd'
26+
>>> ordinal(103)
27+
'103rd'
28+
>>> ordinal(4)
29+
'4th'
30+
>>> ordinal(12)
31+
'12th'
32+
>>> ordinal(101)
33+
'101st'
34+
>>> ordinal(111)
35+
'111th'
36+
>>> ordinal("something else")
37+
'something else'
38+
>>> ordinal(None) is None
39+
True
40+
41+
```
2042
Args:
2143
value (int, str, float): Integer to convert.
2244
@@ -50,6 +72,24 @@ def intcomma(value, ndigits=None):
5072
For example, 3000 becomes "3,000" and 45000 becomes "45,000". To maintain some
5173
compatibility with Django's `intcomma`, this function also accepts floats.
5274
75+
Examples:
76+
``pycon
77+
>>> intcomma(100)
78+
'100'
79+
>>> intcomma("1000")
80+
'1,000'
81+
>>> intcomma(1_000_000)
82+
'1,000,000'
83+
>>> intcomma(1_234_567.25)
84+
'1,234,567.25'
85+
>>> intcomma(1234.5454545, 2)
86+
'1,234.55'
87+
>>> intcomma(14308.40, 1)
88+
'14,308.4'
89+
>>> intcomma(None) is None
90+
True
91+
92+
```
5393
Args:
5494
value (int, float, str): Integer or float to convert.
5595
ndigits (int, None): Digits of precision for rounding after the decimal point.
@@ -100,6 +140,22 @@ def intword(value, format="%.1f"):
100140
1200000 becomes "1.2 million" and "1_200_000_000" becomes "1.2 billion". Supports up
101141
to decillion (33 digits) and googol (100 digits).
102142
143+
Examples:
144+
```pycon
145+
>>> intword("100")
146+
'100'
147+
>>> intword("1000000")
148+
'1.0 million'
149+
>>> intword(1_200_000_000)
150+
'1.2 billion'
151+
>>> intword(8100000000000000000000000000000000)
152+
'8.1 decillion'
153+
>>> intword(None) is None
154+
True
155+
>>> intword("1234000", "%0.3f")
156+
'1.234 million'
157+
158+
```
103159
Args:
104160
value (int, float, str): Integer to convert.
105161
format (str): To change the number of decimal or general format of the number
@@ -130,6 +186,22 @@ def intword(value, format="%.1f"):
130186
def apnumber(value):
131187
"""Converts an integer to Associated Press style.
132188
189+
Examples:
190+
```pycon
191+
>>> apnumber(0)
192+
'zero'
193+
>>> apnumber(5)
194+
'five'
195+
>>> apnumber(10)
196+
'10'
197+
>>> apnumber("7")
198+
'seven'
199+
>>> apnumber("foo")
200+
'foo'
201+
>>> apnumber(None) is None
202+
True
203+
204+
```
133205
Args:
134206
value (int, float, str): Integer to convert.
135207
@@ -182,6 +254,11 @@ def fractional(value):
182254
'1/3'
183255
>>> fractional(1)
184256
'1'
257+
>>> fractional("ten")
258+
'ten'
259+
>>> fractional(None) is None
260+
True
261+
185262
```
186263
Args:
187264
value (int, float, str): Integer to convert.
@@ -216,6 +293,19 @@ def scientific(value, precision=2):
216293
'3.00 x 10⁻¹'
217294
>>> scientific(int(500))
218295
'5.00 x 10²'
296+
>>> scientific(-1000)
297+
'1.00 x 10⁻³'
298+
>>> scientific(1000, 1)
299+
'1.0 x 10³'
300+
>>> scientific(1000, 3)
301+
'1.000 x 10³'
302+
>>> scientific("99")
303+
'9.90 x 10¹'
304+
>>> scientific("foo")
305+
'foo'
306+
>>> scientific(None) is None
307+
True
308+
219309
```
220310
221311
Args:

src/humanize/time.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def naturalday(value, format="%b %d"):
229229
For date values that are tomorrow, today or yesterday compared to
230230
present day return representing string. Otherwise, return a string
231231
formatted according to `format`.
232+
232233
"""
233234
try:
234235
value = dt.date(value.year, value.month, value.day)
@@ -384,6 +385,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
384385
>>> delta = dt.timedelta(seconds=3633, days=2, microseconds=123000)
385386
>>> precisedelta(delta)
386387
'2 days, 1 hour and 33.12 seconds'
388+
387389
```
388390
389391
A custom `format` can be specified to control how the fractional part
@@ -392,6 +394,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
392394
```pycon
393395
>>> precisedelta(delta, format="%0.4f")
394396
'2 days, 1 hour and 33.1230 seconds'
397+
395398
```
396399
397400
Instead, the `minimum_unit` can be changed to have a better resolution;
@@ -403,6 +406,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
403406
```pycon
404407
>>> precisedelta(delta, minimum_unit="microseconds")
405408
'2 days, 1 hour, 33 seconds and 123 milliseconds'
409+
406410
```
407411
408412
If desired, some units can be suppressed: you will not see them represented and the
@@ -411,6 +415,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
411415
```pycon
412416
>>> precisedelta(delta, suppress=['days'])
413417
'49 hours and 33.12 seconds'
418+
414419
```
415420
416421
Note that microseconds precision is lost if the seconds and all
@@ -420,6 +425,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
420425
>>> delta = dt.timedelta(seconds=90, microseconds=100)
421426
>>> precisedelta(delta, suppress=['seconds', 'milliseconds', 'microseconds'])
422427
'1.50 minutes'
428+
423429
```
424430
"""
425431
date, delta = date_and_delta(value)

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ deps = pre-commit
1717
commands = pre-commit run --all-files --show-diff-on-failure
1818
skip_install = true
1919
passenv = PRE_COMMIT_COLOR
20+
21+
[pytest]
22+
addopts = --doctest-modules

0 commit comments

Comments
 (0)