Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing/docs: Include doctests in testing #168

Merged
merged 2 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/humanize/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ def naturalsize(value, binary=False, gnu=False, format="%.1f"):

Non-GNU modes are compatible with jinja2's `filesizeformat` filter.

Examples:
carlpatt marked this conversation as resolved.
Show resolved Hide resolved
```pycon
>>> naturalsize(3000000)
'3.0 MB'
>>> naturalsize(300, False, True)
'300B'
>>> naturalsize(3000, False, True)
'2.9K'
>>> naturalsize(3000, False, True, "%.3f")
'2.930K'
>>> naturalsize(3000, True)
'2.9 KiB'

carlpatt marked this conversation as resolved.
Show resolved Hide resolved
```
Args:
value (int, float, str): Integer to convert.
binary (bool): If `True`, uses binary suffixes (KiB, MiB) with base
Expand Down
90 changes: 90 additions & 0 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ def ordinal(value):
anything `int()` will turn into an integer. Anything other value will have nothing
done to it.

Examples:
```pycon
>>> ordinal(1)
'1st'
>>> ordinal(1002)
'1002nd'
>>> ordinal(103)
'103rd'
>>> ordinal(4)
'4th'
>>> ordinal(12)
'12th'
>>> ordinal(101)
'101st'
>>> ordinal(111)
'111th'
>>> ordinal("something else")
'something else'
>>> ordinal(None) is None
True

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is this newline needed, and those for subsequent ones?

Copy link
Author

@carlpatt carlpatt Oct 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes if there isn't a space between the three backticks and the test result doctests report it as a failure for some reason.

for example:

>>> precisedelta(delta, suppress=['seconds', 'milliseconds', 'microseconds'])
    '1.50 minutes'
    ```

Doctests will return:

precisedelta(delta, suppress=['seconds', 'milliseconds', 'microseconds'])
Expected:
'1.50 minutes'
```
Got:
'1.50 minutes'

When there's a blank line between them it passes with no failure.

I kept the...

```pycon 
 *documentation* 
\```

...format because I noticed it in other tests that were accepted before and I'm assuming it's there for highlighting or has something to do with the readthedocs page

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

Yes, pycon is for syntax highlighting on RTD.

Thanks, I think this is ready for merge!

```
Args:
value (int, str, float): Integer to convert.

Expand Down Expand Up @@ -50,6 +72,24 @@ def intcomma(value, ndigits=None):
For example, 3000 becomes "3,000" and 45000 becomes "45,000". To maintain some
compatibility with Django's `intcomma`, this function also accepts floats.

Examples:
``pycon
>>> intcomma(100)
'100'
>>> intcomma("1000")
'1,000'
>>> intcomma(1_000_000)
'1,000,000'
>>> intcomma(1_234_567.25)
'1,234,567.25'
>>> intcomma(1234.5454545, 2)
'1,234.55'
>>> intcomma(14308.40, 1)
'14,308.4'
>>> intcomma(None) is None
True

```
Args:
value (int, float, str): Integer or float to convert.
ndigits (int, None): Digits of precision for rounding after the decimal point.
Expand Down Expand Up @@ -100,6 +140,22 @@ def intword(value, format="%.1f"):
1200000 becomes "1.2 million" and "1_200_000_000" becomes "1.2 billion". Supports up
to decillion (33 digits) and googol (100 digits).

Examples:
```pycon
>>> intword("100")
'100'
>>> intword("1000000")
'1.0 million'
>>> intword(1_200_000_000)
'1.2 billion'
>>> intword(8100000000000000000000000000000000)
'8.1 decillion'
>>> intword(None) is None
True
>>> intword("1234000", "%0.3f")
'1.234 million'

```
Args:
value (int, float, str): Integer to convert.
format (str): To change the number of decimal or general format of the number
Expand Down Expand Up @@ -130,6 +186,22 @@ def intword(value, format="%.1f"):
def apnumber(value):
"""Converts an integer to Associated Press style.

Examples:
```pycon
>>> apnumber(0)
'zero'
>>> apnumber(5)
'five'
>>> apnumber(10)
'10'
>>> apnumber("7")
'seven'
>>> apnumber("foo")
'foo'
>>> apnumber(None) is None
True

```
Args:
value (int, float, str): Integer to convert.

Expand Down Expand Up @@ -182,6 +254,11 @@ def fractional(value):
'1/3'
>>> fractional(1)
'1'
>>> fractional("ten")
'ten'
>>> fractional(None) is None
True

```
Args:
value (int, float, str): Integer to convert.
Expand Down Expand Up @@ -216,6 +293,19 @@ def scientific(value, precision=2):
'3.00 x 10⁻¹'
>>> scientific(int(500))
'5.00 x 10²'
>>> scientific(-1000)
'1.00 x 10⁻³'
>>> scientific(1000, 1)
'1.0 x 10³'
>>> scientific(1000, 3)
'1.000 x 10³'
>>> scientific("99")
'9.90 x 10¹'
>>> scientific("foo")
'foo'
>>> scientific(None) is None
True

```

Args:
Expand Down
6 changes: 6 additions & 0 deletions src/humanize/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def naturalday(value, format="%b %d"):
For date values that are tomorrow, today or yesterday compared to
present day return representing string. Otherwise, return a string
formatted according to `format`.

"""
try:
value = dt.date(value.year, value.month, value.day)
Expand Down Expand Up @@ -368,6 +369,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
>>> delta = dt.timedelta(seconds=3633, days=2, microseconds=123000)
>>> precisedelta(delta)
'2 days, 1 hour and 33.12 seconds'

```

A custom `format` can be specified to control how the fractional part
Expand All @@ -376,6 +378,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
```pycon
>>> precisedelta(delta, format="%0.4f")
'2 days, 1 hour and 33.1230 seconds'

```

Instead, the `minimum_unit` can be changed to have a better resolution;
Expand All @@ -387,6 +390,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
```pycon
>>> precisedelta(delta, minimum_unit="microseconds")
'2 days, 1 hour, 33 seconds and 123 milliseconds'

```

If desired, some units can be suppressed: you will not see them represented and the
Expand All @@ -395,6 +399,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
```pycon
>>> precisedelta(delta, suppress=['days'])
'49 hours and 33.12 seconds'

```

Note that microseconds precision is lost if the seconds and all
Expand All @@ -404,6 +409,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f"):
>>> delta = dt.timedelta(seconds=90, microseconds=100)
>>> precisedelta(delta, suppress=['seconds', 'milliseconds', 'microseconds'])
'1.50 minutes'

```
"""
date, delta = date_and_delta(value)
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ deps = pre-commit
commands = pre-commit run --all-files --show-diff-on-failure
skip_install = true
passenv = PRE_COMMIT_COLOR

[pytest]
addopts = --doctest-modules