Skip to content

Commit

Permalink
Merge branch 'master' into 118-add-ndigits-to-intcomma
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 15, 2020
2 parents c49cf42 + 77d943a commit 7b78c0b
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 32 deletions.
6 changes: 3 additions & 3 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ Fixes #

Changes proposed in this pull request:

*
*
*
*
*
*
31 changes: 22 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.5, 3.6, 3.7, 3.8]
python-version: ["pypy3", "3.5", "3.6", "3.7", "3.8", "3.9"]
os: [ubuntu-latest, macos-latest, windows-latest]
include:
# Include new variables for Codecov
- os: ubuntu-latest
codecov-flag: GHA_Ubuntu
- os: macos-latest
codecov-flag: GHA_macOS
- os: windows-latest
codecov-flag: GHA_Windows
- { codecov-flag: GHA_Ubuntu, os: ubuntu-latest }
- { codecov-flag: GHA_macOS, os: macos-latest }
- { codecov-flag: GHA_Windows, os: windows-latest }
exclude:
- { python-version: 3.9, os: macos-latest }
- { python-version: 3.9, os: windows-latest }
- { python-version: pypy3, os: windows-latest }

steps:
- uses: actions/checkout@v2
Expand All @@ -35,7 +36,7 @@ jobs:
- name: macOS cache
uses: actions/cache@v1
if: startsWith(matrix.os, 'macOS')
if: startsWith(matrix.os, 'macos')
with:
path: ~/Library/Caches/pip
key:
Expand All @@ -55,7 +56,19 @@ jobs:
restore-keys: |
${{ matrix.os }}-${{ matrix.python-version }}-
- name: Install Python 3.9
if: matrix.python-version == '3.9'
run: |
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install -y --no-install-recommends python3.9-dev python3.9-distutils python3.9-venv
python3.9 -m pip install --upgrade pip setuptools
python3.9 -m venv $HOME/venv-python3.9
echo "::set-env name=VIRTUAL_ENV::$HOME/venv-python3.9"
echo "::add-path::$HOME/venv-python3.9/bin"
- name: Set up Python ${{ matrix.python-version }}
if: matrix.python-version != '3.9'
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
Expand All @@ -73,7 +86,7 @@ jobs:
- name: Upload coverage
if: success()
run: |
curl -s https://codecov.io/bash -o codecov.sh
curl --retry 8 -s https://codecov.io/bash -o codecov.sh
bash codecov.sh -F ${{ matrix.codecov-flag }}
env:
CODECOV_NAME: ${{ matrix.os }} Python ${{ matrix.python-version }}
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.0.1
rev: v2.1.0
hooks:
- id: pyupgrade
args: ["--py3-plus"]
Expand All @@ -18,7 +18,7 @@ repos:
additional_dependencies: [flake8-2020, flake8-implicit-str-concat]

- repo: https://github.com/asottile/seed-isort-config
rev: v1.9.4
rev: v2.1.0
hooks:
- id: seed-isort-config

Expand Down
33 changes: 21 additions & 12 deletions src/humanize/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@


def naturalsize(value, binary=False, gnu=False, format="%.1f"):
"""Format a number of byteslike a human readable filesize (eg. 10 kB). By
default, decimal suffixes (kB, MB) are used. Passing binary=true will use
binary suffixes (KiB, MiB) are used and the base will be 2**10 instead of
10**3. If ``gnu`` is True, the binary argument is ignored and GNU-style
(ls -sh style) prefixes are used (K, M) with the 2**10 definition.
Non-gnu modes are compatible with jinja2's ``filesizeformat`` filter."""
"""Format a number of bytes like a human readable filesize (eg. 10 kB).
By default, decimal suffixes (kB, MB) are used.
Non-gnu modes are compatible with jinja2's ``filesizeformat`` filter.
Args:
value (int, float, string): Integer to convert.
binary (Boolean): If `True`, uses binary suffixes (KiB, MiB) with base 2**10
instead of 10**3.
gnu (Boolean): If `True`, the binary argument is ignored and GNU-style
(`ls -sh` style) prefixes are used (K, M) with the 2**10 definition.
format (str): Custom formatter.
"""
if gnu:
suffix = suffixes["gnu"]
elif binary:
Expand All @@ -25,19 +33,20 @@ def naturalsize(value, binary=False, gnu=False, format="%.1f"):

base = 1024 if (gnu or binary) else 1000
bytes = float(value)
abs_bytes = abs(bytes)

if bytes == 1 and not gnu:
return "1 Byte"
elif bytes < base and not gnu:
if abs_bytes == 1 and not gnu:
return "%d Byte" % bytes
elif abs_bytes < base and not gnu:
return "%d Bytes" % bytes
elif bytes < base and gnu:
elif abs_bytes < base and gnu:
return "%dB" % bytes

for i, s in enumerate(suffix):
unit = base ** (i + 2)
if bytes < unit and not gnu:
if abs_bytes < unit and not gnu:
return (format + " %s") % ((base * bytes / unit), s)
elif bytes < unit and gnu:
elif abs_bytes < unit and gnu:
return (format + "%s") % ((base * bytes / unit), s)
if gnu:
return (format + "%s") % ((base * bytes / unit), s)
Expand Down
17 changes: 12 additions & 5 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,23 @@ def intword(value, format="%.1f"):


def apnumber(value):
"""For numbers 1-9, returns the number spelled out. Otherwise, returns the
number. This follows Associated Press style. This always returns a string
unless the value was not int-able, unlike the Django filter."""
"""Converts an integer to Associated Press style.
Args:
value (int, float, string): Integer to convert.
Returns:
str: For numbers 0-9, the number spelled out. Otherwise, the number. This always
returns a string unless the value was not int-able, unlike the Django filter.
"""
try:
value = int(value)
except (TypeError, ValueError):
return value
if not 0 < value < 10:
if not 0 <= value < 10:
return str(value)
return (
_("zero"),
_("one"),
_("two"),
_("three"),
Expand All @@ -138,7 +145,7 @@ def apnumber(value):
_("seven"),
_("eight"),
_("nine"),
)[value - 1]
)[value]


def fractional(value):
Expand Down
6 changes: 5 additions & 1 deletion tests/test_filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@
([10 ** 26 * 30, True, False, "%.3f"], "2481.542 YiB"),
],
)
def test_naturaltime_minimum_unit_default(test_args, expected):
def test_naturalsize(test_args, expected):
assert humanize.naturalsize(*test_args) == expected

args_with_negative = test_args
args_with_negative[0] *= -1
assert humanize.naturalsize(*args_with_negative) == "-" + expected
1 change: 1 addition & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def test_intword(test_args, expected):
@pytest.mark.parametrize(
"test_input, expected",
[
(0, "zero"),
(1, "one"),
(2, "two"),
(4, "four"),
Expand Down

0 comments on commit 7b78c0b

Please sign in to comment.