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

Add 'humanize' command-line interface #185

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sakurai-youhei
Copy link

This PR adds 'humanize' command-line interface, which enables users to run humanize directly on the terminal.

Example of use:

$ humanize naturalsize 8589934592
8.6 GB
$ humanize naturalsize --binary 8589934592
8.0 GiB
$ echo 8589934592 | python -m humanize naturalsize --format "%.3f"
8.590 GB
$
$ humanize --version
humanize 0.1.dev845
$
$ humanize --help
usage: humanize [-h] [-v] FUNCTION ...

Python humanize utilities

positional arguments:
  FUNCTION
    apnumber     Converts an integer to Associated Press style.
    clamp        Returns number with the specified format, clamped between floor and ceil.
    fractional   Convert to fractional number.
    intcomma     Converts an integer to a string containing commas every three digits.
    intword      Converts a large integer to a friendly text representation.
    metric       Return a value with a metric SI unit-prefix appended.
    naturalsize  Format a number of bytes like a human readable filesize (e.g. 10 kB).
    ordinal      Converts an integer to its ordinal as a string.
    scientific   Return number in string scientific notation z.wq x 10ⁿ.

options:
  -h, --help     show this help message and exit
  -v, --version  show program's version number and exit
$
$ humanize naturalsize --help
usage: humanize naturalsize [-h] [--locale LOCALE] [--binary] [--gnu] [--format FORMAT] [values ...]

positional arguments:
  values           Values to humanize. If not found, humanizes inputs from stdin.

options:
  -h, --help       show this help message and exit
  --locale LOCALE  Language name, e.g. `en_GB`.
  --binary         If `True`, uses binary suffixes (KiB, MiB) with base 2**10 instead of 10**3. (default: `False`)
  --gnu            If `True`, the binary argument is ignored and GNU-style (`ls -sh` style) prefixes are used (K, M) with the 2**10 definition. (default: `False`)
  --format FORMAT  Custom formatter. (default: `%.1f`)

Closes #184

Changes proposed in this pull request:

  • CLI for humanize.filesize and humanize.number.
  • humanize.cli package, which relies on argparse to keep the package dependencies free.
  • humanize.__main__ module for python -m humanize.
  • Tests.

Note: humanize.time is excluded from the scope because it's too hard to process arbitrary date & time inputs.

@hugovk
Copy link
Member

hugovk commented May 13, 2024

Thank you for the PR! Just a quick note to say it's not forgotten, and I'll have a look soon but it might be after PyCon US.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggestion: Add an entrypoint and/or scripts for command-line use
2 participants