Skip to content

Commit ddb3bac

Browse files
author
Carl
committed
testing/docs: add a section to "tox.ini" that includes doctests in testing
add doctests to some modules
1 parent 3993a24 commit ddb3bac

4 files changed

Lines changed: 113 additions & 0 deletions

File tree

src/humanize/filesize.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@ def naturalsize(value, binary=False, gnu=False, format="%.1f"):
1515
By default, decimal suffixes (kB, MB) are used.
1616
1717
Non-GNU modes are compatible with jinja2's `filesizeformat` filter.
18+
Examples:
19+
```pycon
20+
>>> naturalsize(3000000)
21+
'3.0 MB'
22+
>>> naturalsize(300, False, True)
23+
'300B'
24+
>>> naturalsize(3000, False, True)
25+
'2.9K'
26+
>>> naturalsize(3000, False, True, "%.3f")
27+
'2.930K'
28+
>>> naturalsize(3000, True)
29+
'2.9 KiB'
1830
31+
```
1932
Args:
2033
value (int, float, str): Integer to convert.
2134
binary (bool): If `True`, uses binary suffixes (KiB, MiB) with base

src/humanize/number.py

Lines changed: 88 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
@@ -49,7 +71,24 @@ def intcomma(value, ndigits=None):
4971
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.
74+
Examples:
75+
``pycon
76+
>>> intcomma(100)
77+
'100'
78+
>>> intcomma("1000")
79+
'1,000'
80+
>>> intcomma(1_000_000)
81+
'1,000,000'
82+
>>> intcomma(1_234_567.25)
83+
'1,234,567.25'
84+
>>> intcomma(1234.5454545, 2)
85+
'1,234.55'
86+
>>> intcomma(14308.40, 1)
87+
'14,308.4'
88+
>>> intcomma(None) is None
89+
True
5290
91+
```
5392
Args:
5493
value (int, float, str): Integer or float to convert.
5594
ndigits (int, None): Digits of precision for rounding after the decimal point.
@@ -99,7 +138,22 @@ def intword(value, format="%.1f"):
99138
Works best for numbers over 1 million. For example, 1_000_000 becomes "1.0 million",
100139
1200000 becomes "1.2 million" and "1_200_000_000" becomes "1.2 billion". Supports up
101140
to decillion (33 digits) and googol (100 digits).
141+
Examples:
142+
```pycon
143+
>>> intword("100")
144+
'100'
145+
>>> intword("1000000")
146+
'1.0 million'
147+
>>> intword(1_200_000_000)
148+
'1.2 billion'
149+
>>> intword(8100000000000000000000000000000000)
150+
'8.1 decillion'
151+
>>> intword(None) is None
152+
True
153+
>>> intword("1234000", "%0.3f")
154+
'1.234 million'
102155
156+
```
103157
Args:
104158
value (int, float, str): Integer to convert.
105159
format (str): To change the number of decimal or general format of the number
@@ -132,7 +186,22 @@ def apnumber(value):
132186
133187
Args:
134188
value (int, float, str): Integer to convert.
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
135203
204+
```
136205
Returns:
137206
str: For numbers 0-9, the number spelled out. Otherwise, the number. This always
138207
returns a string unless the value was not `int`-able, unlike the Django filter.
@@ -174,6 +243,7 @@ def fractional(value):
174243
175244
Examples:
176245
```pycon
246+
177247
>>> fractional(0.3)
178248
'3/10'
179249
>>> fractional(1.3)
@@ -182,6 +252,11 @@ def fractional(value):
182252
'1/3'
183253
>>> fractional(1)
184254
'1'
255+
>>> fractional("ten")
256+
'ten'
257+
>>> fractional(None) is None
258+
True
259+
185260
```
186261
Args:
187262
value (int, float, str): Integer to convert.
@@ -216,6 +291,19 @@ def scientific(value, precision=2):
216291
'3.00 x 10⁻¹'
217292
>>> scientific(int(500))
218293
'5.00 x 10²'
294+
>>> scientific(-1000)
295+
'1.00 x 10⁻³'
296+
>>> scientific(1000, 1)
297+
'1.0 x 10³'
298+
>>> scientific(1000, 3)
299+
'1.000 x 10³'
300+
>>> scientific("99")
301+
'9.90 x 10¹'
302+
>>> scientific("foo")
303+
'foo'
304+
>>> scientific(None) is None
305+
True
306+
219307
```
220308
221309
Args:

src/humanize/time.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ def naturalday(value, format="%b %d"):
213213
For date values that are tomorrow, today or yesterday compared to
214214
present day return representing string. Otherwise, return a string
215215
formatted according to `format`.
216+
217+
Examples:
218+
219+
216220
"""
217221
try:
218222
value = dt.date(value.year, value.month, value.day)
@@ -368,6 +372,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
368372
>>> delta = dt.timedelta(seconds=3633, days=2, microseconds=123000)
369373
>>> precisedelta(delta)
370374
'2 days, 1 hour and 33.12 seconds'
375+
371376
```
372377
373378
A custom `format` can be specified to control how the fractional part
@@ -376,6 +381,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
376381
```pycon
377382
>>> precisedelta(delta, format="%0.4f")
378383
'2 days, 1 hour and 33.1230 seconds'
384+
379385
```
380386
381387
Instead, the `minimum_unit` can be changed to have a better resolution;
@@ -387,6 +393,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
387393
```pycon
388394
>>> precisedelta(delta, minimum_unit="microseconds")
389395
'2 days, 1 hour, 33 seconds and 123 milliseconds'
396+
390397
```
391398
392399
If desired, some units can be suppressed: you will not see them represented and the
@@ -395,6 +402,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
395402
```pycon
396403
>>> precisedelta(delta, suppress=['days'])
397404
'49 hours and 33.12 seconds'
405+
398406
```
399407
400408
Note that microseconds precision is lost if the seconds and all
@@ -404,6 +412,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
404412
>>> delta = dt.timedelta(seconds=90, microseconds=100)
405413
>>> precisedelta(delta, suppress=['seconds', 'milliseconds', 'microseconds'])
406414
'1.50 minutes'
415+
407416
```
408417
"""
409418
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)