The following API documentation was automatically generated from the source code of humanfriendly 4.18:
The humanfriendly package started out as a single humanfriendly module. Eventually this module grew to a size that necessitated splitting up the code into multiple modules (see e.g. tables, terminal, text and usage). Most of the functionality that remains in the humanfriendly module will eventually be moved to submodules as well (as time permits and a logical subdivision of functionality presents itself to me).
While moving functionality around like this my goal is to always preserve backwards compatibility. For example if a function is moved to a submodule an import of that function is added in the main module so that backwards compatibility with previously written import statements is preserved.
If backwards compatibility of documented functionality has to be broken then the major version number will be bumped. So if you’re using the humanfriendly package in your project, make sure to at least pin the major version number in order to avoid unexpected surprises.
The main module of the humanfriendly package.
SizeUnit(divider, symbol, name)
Return self as a plain tuple. Used by copy and pickle.
Create new instance of SizeUnit(divider, symbol, name)
Return a nicely formatted representation string
Return a new OrderedDict which maps field names to their values.
Make a new SizeUnit object from a sequence or iterable
Return a new SizeUnit object replacing specified fields with new values
Alias for field number 0
Alias for field number 2
Alias for field number 1
CombinedUnit(decimal, binary)
Return self as a plain tuple. Used by copy and pickle.
Create new instance of CombinedUnit(decimal, binary)
Return a nicely formatted representation string
Return a new OrderedDict which maps field names to their values.
Make a new CombinedUnit object from a sequence or iterable
Return a new CombinedUnit object replacing specified fields with new values
Alias for field number 1
Alias for field number 0
Coerce any value to a boolean.
Parameters: | value – Any Python value. If the value is a string:
Other Python values are coerced using bool(). |
---|---|
Returns: | A proper boolean value. |
Raises: | exceptions.ValueError when the value is a string but cannot be coerced with certainty. |
Coerce strings to compiled regular expressions.
Parameters: |
|
---|---|
Returns: | A compiled regular expression. |
Raises: | ValueError when value isn’t a string and also isn’t a compiled regular expression. |
Coerce a value to the number of seconds.
Parameters: | value – An int, float or datetime.timedelta object. |
---|---|
Returns: | An int or float value. |
When value is a datetime.timedelta object the total_seconds() method is called. On Python 2.6 this method is not available so it is emulated.
Format a byte count as a human readable file size.
Parameters: | |
---|---|
Returns: | The corresponding human readable file size (a string). |
This function knows how to format sizes in bytes, kilobytes, megabytes, gigabytes, terabytes and petabytes. Some examples:
>>> from humanfriendly import format_size
>>> format_size(0)
'0 bytes'
>>> format_size(1)
'1 byte'
>>> format_size(5)
'5 bytes'
> format_size(1000)
'1 KB'
> format_size(1024, binary=True)
'1 KiB'
>>> format_size(1000 ** 3 * 4)
'4 GB'
Parse a human readable data size and return the number of bytes.
Parameters: | |
---|---|
Returns: | The corresponding size in bytes (an integer). |
Raises: | InvalidSize when the input can’t be parsed. |
This function knows how to parse sizes in bytes, kilobytes, megabytes, gigabytes, terabytes and petabytes. Some examples:
>>> from humanfriendly import parse_size
>>> parse_size('42')
42
>>> parse_size('13b')
13
>>> parse_size('5 bytes')
5
>>> parse_size('1 KB')
1000
>>> parse_size('1 kilobyte')
1000
>>> parse_size('1 KiB')
1024
>>> parse_size('1 KB', binary=True)
1024
>>> parse_size('1.5 GB')
1500000000
>>> parse_size('1.5 GB', binary=True)
1610612736
Format a metre count as a human readable length.
Parameters: | |
---|---|
Returns: | The corresponding human readable length (a string). |
This function supports ranges from nanometres to kilometres.
Some examples:
>>> from humanfriendly import format_length
>>> format_length(0)
'0 metres'
>>> format_length(1)
'1 metre'
>>> format_length(5)
'5 metres'
>>> format_length(1000)
'1 km'
>>> format_length(0.004)
'4 mm'
Parse a human readable length and return the number of metres.
Parameters: | length – The human readable length to parse (a string). |
---|---|
Returns: | The corresponding length in metres (a float). |
Raises: | InvalidLength when the input can’t be parsed. |
Some examples:
>>> from humanfriendly import parse_length
>>> parse_length('42')
42
>>> parse_length('1 km')
1000
>>> parse_length('5mm')
0.005
>>> parse_length('15.3cm')
0.153
Format a number as a string including thousands separators.
Parameters: | |
---|---|
Returns: | The formatted number (a string). |
This function is intended to make it easier to recognize the order of size of the number being formatted.
Here’s an example:
>>> from humanfriendly import format_number
>>> print(format_number(6000000))
6,000,000
> print(format_number(6000000000.42))
6,000,000,000.42
> print(format_number(6000000000.42, num_decimals=0))
6,000,000,000
Round a floating point number to two decimal places in a human friendly format.
Parameters: | |
---|---|
Returns: | The formatted number as a string. If no decimal places are required to represent the number, they will be omitted. |
The main purpose of this function is to be used by functions like format_length(), format_size() and format_timespan().
Here are some examples:
>>> from humanfriendly import round_number
>>> round_number(1)
'1'
>>> round_number(math.pi)
'3.14'
>>> round_number(5.001)
'5'
Format a timespan in seconds as a human readable string.
Parameters: |
|
---|---|
Returns: | The formatted timespan as a string. |
Raise: | See coerce_seconds(). |
Some examples:
>>> from humanfriendly import format_timespan
>>> format_timespan(0)
'0 seconds'
>>> format_timespan(1)
'1 second'
>>> import math
>>> format_timespan(math.pi)
'3.14 seconds'
>>> hour = 60 * 60
>>> day = hour * 24
>>> week = day * 7
>>> format_timespan(week * 52 + day * 2 + hour * 3)
'1 year, 2 days and 3 hours'
Parse a “human friendly” timespan into the number of seconds.
Parameters: | value – A string like 5h (5 hours), 10m (10 minutes) or 42s (42 seconds). |
---|---|
Returns: | The number of seconds as a floating point number. |
Raises: | InvalidTimespan when the input can’t be parsed. |
Note that the parse_timespan() function is not meant to be the “mirror image” of the format_timespan() function. Instead it’s meant to allow humans to easily and succinctly specify a timespan with a minimal amount of typing. It’s very useful to accept easy to write time spans as e.g. command line arguments to programs.
The time units (and abbreviations) supported by this function are:
Some examples:
>>> from humanfriendly import parse_timespan
>>> parse_timespan('42')
42.0
>>> parse_timespan('42s')
42.0
>>> parse_timespan('1m')
60.0
>>> parse_timespan('1h')
3600.0
>>> parse_timespan('1d')
86400.0
Parse a date/time string into a tuple of integers.
Parameters: | datestring – The date/time string to parse. |
---|---|
Returns: | A tuple with the numbers (year, month, day, hour, minute, second) (all numbers are integers). |
Raises: | InvalidDate when the date cannot be parsed. |
Supported date/time formats:
Note
If you want to parse date/time strings with a fixed, known format and parse_date() isn’t useful to you, consider time.strptime() or datetime.datetime.strptime(), both of which are included in the Python standard library. Alternatively for more complex tasks consider using the date/time parsing module in the dateutil package.
Examples:
>>> from humanfriendly import parse_date
>>> parse_date('2013-06-17')
(2013, 6, 17, 0, 0, 0)
>>> parse_date('2013-06-17 02:47:42')
(2013, 6, 17, 2, 47, 42)
Here’s how you convert the result to a number (Unix time):
>>> from humanfriendly import parse_date
>>> from time import mktime
>>> mktime(parse_date('2013-06-17 02:47:42') + (-1, -1, -1))
1371430062.0
And here’s how you convert it to a datetime.datetime object:
>>> from humanfriendly import parse_date
>>> from datetime import datetime
>>> datetime(*parse_date('2013-06-17 02:47:42'))
datetime.datetime(2013, 6, 17, 2, 47, 42)
Here’s an example that combines format_timespan() and parse_date() to calculate a human friendly timespan since a given date:
>>> from humanfriendly import format_timespan, parse_date
>>> from time import mktime, time
>>> unix_time = mktime(parse_date('2013-06-17 02:47:42') + (-1, -1, -1))
>>> seconds_since_then = time() - unix_time
>>> print(format_timespan(seconds_since_then))
1 year, 43 weeks and 1 day
Shorten a pathname to make it more human friendly.
Parameters: | pathname – An absolute pathname (a string). |
---|---|
Returns: | The pathname with the user’s home directory abbreviated. |
Given an absolute pathname, this function abbreviates the user’s home directory to ~/ in order to shorten the pathname without losing information. It is not an error if the pathname is not relative to the current user’s home directory.
Here’s an example of its usage:
>>> from os import environ
>>> from os.path import join
>>> vimrc = join(environ['HOME'], '.vimrc')
>>> vimrc
'/home/peter/.vimrc'
>>> from humanfriendly import format_path
>>> format_path(vimrc)
'~/.vimrc'
Convert a human friendly pathname to an absolute pathname.
Expands leading tildes using os.path.expanduser() and environment variables using os.path.expandvars() and makes the resulting pathname absolute using os.path.abspath().
Parameters: | pathname – A human friendly pathname (a string). |
---|---|
Returns: | An absolute pathname (a string). |
Easy to use timer to keep track of long during operations.
Remember the time when the Timer was created.
Parameters: |
|
---|
When start_time is given Timer uses time.time() as a clock source, otherwise it uses humanfriendly.compat.monotonic().
Start or resume counting elapsed time.
Returns: | The Timer object. |
---|---|
Raises: | ValueError when the timer isn’t resumable. |
Stop counting elapsed time.
Raises: | ValueError when the timer isn’t resumable. |
---|
Easy to use rate limiting of repeating actions.
Parameters: | seconds – The number of seconds to sleep (an integer or floating point number). |
---|
This method sleeps for the given number of seconds minus the elapsed_time. If the resulting duration is negative time.sleep() will still be called, but the argument given to it will be the number 0 (negative numbers cause time.sleep() to raise an exception).
The use case for this is to initialize a Timer inside the body of a for or while loop and call Timer.sleep() at the end of the loop body to rate limit whatever it is that is being done inside the loop body.
For posterity: Although the implementation of sleep() only requires a single line of code I’ve added it to humanfriendly anyway because now that I’ve thought about how to tackle this once I never want to have to think about it again :-P (unless I find ways to improve this).
Get the number of seconds counted so far.
Human readable timespan rounded to seconds (a string).
Show a spinner on the terminal as a simple means of feedback to the user.
The Spinner class shows a “spinner” on the terminal to let the user know that something is happening during long running operations that would otherwise be silent (leaving the user to wonder what they’re waiting for). Below are some visual examples that should illustrate the point.
Simple spinners:
Here’s a screen capture that shows the simplest form of spinner:
![]()
The following code was used to create the spinner above:
import itertools import time from humanfriendly import Spinner with Spinner(label="Downloading") as spinner: for i in itertools.count(): # Do something useful here. time.sleep(0.1) # Advance the spinner. spinner.step()
Spinners that show elapsed time:
Here’s a spinner that shows the elapsed time since it started:
![]()
The following code was used to create the spinner above:
import itertools import time from humanfriendly import Spinner, Timer with Spinner(label="Downloading", timer=Timer()) as spinner: for i in itertools.count(): # Do something useful here. time.sleep(0.1) # Advance the spinner. spinner.step()
Spinners that show progress:
Here’s a spinner that shows a progress percentage:
![]()
The following code was used to create the spinner above:
import itertools import random import time from humanfriendly import Spinner, Timer with Spinner(label="Downloading", total=100) as spinner: progress = 0 while progress < 100: # Do something useful here. time.sleep(0.1) # Advance the spinner. spinner.step(progress) # Determine the new progress value. progress += random.random() * 5
If you want to provide user feedback during a long running operation but it’s not practical to periodically call the step() method consider using AutomaticSpinner instead.
As you may already have noticed in the examples above, Spinner objects can be used as context managers to automatically call clear() when the spinner ends. This helps to make sure that if the text cursor is hidden its visibility is restored before the spinner ends (even if an exception interrupts the spinner).
Initialize a spinner.
Parameters: |
|
---|
Advance the spinner by one step and redraw it.
Parameters: |
|
---|
This method advances the spinner by one step without starting a new line, causing an animated effect which is very simple but much nicer than waiting for a prompt which is completely silent for a long time.
Sleep for a short period before redrawing the spinner.
This method is useful when you know you’re dealing with code that will call step() at a high frequency. It will sleep for the interval with which the spinner is redrawn (less than a second). This avoids creating the equivalent of a busy loop that’s rate limiting the spinner 99% of the time.
This method doesn’t redraw the spinner, you still have to call step() in order to do that.
Clear the spinner.
The next line which is shown on the standard output or error stream after calling this method will overwrite the line that used to show the spinner. Also the visibility of the text cursor is restored.
Clear the spinner when leaving the context.
Show a spinner on the terminal that automatically starts animating.
This class shows a spinner on the terminal (just like Spinner does) that automatically starts animating. This class should be used as a context manager using the with statement. The animation continues for as long as the context is active.
AutomaticSpinner provides an alternative to Spinner for situations where it is not practical for the caller to periodically call step() to advance the animation, e.g. because you’re performing a blocking call and don’t fancy implementing threading or subprocess handling just to provide some user feedback.
This works using the multiprocessing module by spawning a subprocess to render the spinner while the main process is busy doing something more useful. By using the with statement you’re guaranteed that the subprocess is properly terminated at the appropriate time.
Initialize an automatic spinner.
Parameters: |
|
---|
Enable the use of automatic spinners as context managers.
Enable the use of automatic spinners as context managers.
Raised when a string cannot be parsed into a date.
For example:
>>> from humanfriendly import parse_date
>>> parse_date('2013-06-XY')
Traceback (most recent call last):
File "humanfriendly.py", line 206, in parse_date
raise InvalidDate(format(msg, datestring))
humanfriendly.InvalidDate: Invalid date! (expected 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS' but got: '2013-06-XY')
Raised when a string cannot be parsed into a file size.
For example:
>>> from humanfriendly import parse_size
>>> parse_size('5 Z')
Traceback (most recent call last):
File "humanfriendly/__init__.py", line 267, in parse_size
raise InvalidSize(format(msg, size, tokens))
humanfriendly.InvalidSize: Failed to parse size! (input '5 Z' was tokenized as [5, 'Z'])
Raised when a string cannot be parsed into a length.
For example:
>>> from humanfriendly import parse_length
>>> parse_length('5 Z')
Traceback (most recent call last):
File "humanfriendly/__init__.py", line 267, in parse_length
raise InvalidLength(format(msg, length, tokens))
humanfriendly.InvalidLength: Failed to parse length! (input '5 Z' was tokenized as [5, 'Z'])
Raised when a string cannot be parsed into a timespan.
For example:
>>> from humanfriendly import parse_timespan
>>> parse_timespan('1 age')
Traceback (most recent call last):
File "humanfriendly/__init__.py", line 419, in parse_timespan
raise InvalidTimespan(format(msg, timespan, tokens))
humanfriendly.InvalidTimespan: Failed to parse timespan! (input '1 age' was tokenized as [1, 'age'])
Usage: humanfriendly [OPTIONS]
Human friendly input/output (text formatting) on the command line based on the Python package with the same name.
Supported options:
Option | Description |
---|---|
-c, --run-command | Execute an external command (given as the positional arguments) and render a spinner and timer while the command is running. The exit status of the command is propagated. |
--format-table | Read tabular data from standard input (each line is a row and each whitespace separated field is a column), format the data as a table and print the resulting table to standard output. See also the --delimiter option. |
-d, --delimiter=VALUE | Change the delimiter used by --format-table to VALUE (a string). By default all whitespace is treated as a delimiter. |
-l, --format-length=LENGTH | Convert a length count (given as the integer or float LENGTH) into a human readable string and print that string to standard output. |
-n, --format-number=VALUE | Format a number (given as the integer or floating point number VALUE) with thousands separators and two decimal places (if needed) and print the formatted number to standard output. |
-s, --format-size=BYTES | Convert a byte count (given as the integer BYTES) into a human readable string and print that string to standard output. |
-b, --binary | Change the output of -s, --format-size to use binary multiples of bytes (base-2) instead of the default decimal multiples of bytes (base-10). |
-t, --format-timespan=SECONDS | Convert a number of seconds (given as the floating point number SECONDS) into a human readable timespan and print that string to standard output. |
--parse-length=VALUE | Parse a human readable length (given as the string VALUE) and print the number of metres to standard output. |
--parse-size=VALUE | Parse a human readable data size (given as the string VALUE) and print the number of bytes to standard output. |
--demo | Demonstrate changing the style and color of the terminal font using ANSI escape sequences. |
-h, --help | Show this message and exit. |
Command line interface for the humanfriendly program.
Run an external command and show a spinner while the command is running.
Print a human readable length.
Print large numbers in a human readable format.
Print a human readable size.
Read tabular data from standard input and print a table.
Print a human readable timespan.
Parse a human readable length and print the number of metres.
Parse a human readable data size and print the number of bytes.
Demonstrate the use of ANSI escape sequences.
Demonstrate 256 color mode support.
Compatibility with Python 2 and 3.
This module exposes aliases and functions that make it easier to write Python code that is compatible with Python 2 and Python 3.
Alias for basestring() (in Python 2) or str (in Python 3). See also is_string().
Alias for HTMLParser.HTMLParser (in Python 2) or html.parser.HTMLParser (in Python 3).
Alias for raw_input() (in Python 2) or input() (in Python 3).
Alias for StringIO.StringIO (in Python 2) or io.StringIO (in Python 3).
Alias for unicode() (in Python 2) or str (in Python 3). See also coerce_string().
Alias for time.monotonic() (in Python 3.3 and higher) or monotonic.monotonic() (a conditional dependency on older Python versions).
alias of str
Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
alias of str
Find tags and other markup and call handler functions.
Start tags are handled by calling self.handle_starttag() or self.handle_startendtag(); end tags by self.handle_endtag(). The data between tags is passed from the parser to the derived class by calling self.handle_data() with the data as argument (the data may be split up in arbitrary chunks). If convert_charrefs is True the character references are converted automatically to the corresponding Unicode character (and self.handle_data() is no longer split in chunks), otherwise they are passed by calling self.handle_entityref() or self.handle_charref() with the string containing respectively the named or numeric reference as the argument.
Initialize and reset this instance.
If convert_charrefs is True (the default), all character references are automatically converted to the corresponding Unicode characters.
Handle any buffered data.
Feed data to the parser.
Call this as often as you want, with as little or as much text as you want (may include ‘n’).
Return full source of start tag: ‘<...>’.
Reset this instance. Loses all unprocessed data.
Text I/O implementation using an in-memory buffer.
The initial_value argument sets the value of object. The newline argument is like the one of TextIOWrapper’s constructor.
Close the IO object.
Attempting any further operation after the object is closed will raise a ValueError.
This method has no effect if the file is already closed.
Retrieve the entire contents of the object.
Read at most size characters, returned as a string.
If the argument is negative or omitted, read until EOF is reached. Return an empty string at EOF.
Returns True if the IO object can be read.
Read until newline or EOF.
Returns an empty string if EOF is hit immediately.
Change stream position.
Returns the new absolute position.
Returns True if the IO object can be seeked.
Tell the current file position.
Truncate size to pos.
The pos argument defaults to the current file position, as returned by tell(). The current file position is unchanged. Returns the new absolute position.
Returns True if the IO object can be written.
Write string to file.
Returns the number of characters written, which is always equal to the length of the string.
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. On *nix systems, readline is used if available.
Monotonic clock, cannot go backward.
Coerce any value to a Unicode string (unicode() in Python 2 and str in Python 3).
Parameters: | value – The value to coerce. |
---|---|
Returns: | The value coerced to a Unicode string. |
Check if a value is a basestring() (in Python 2) or str (in Python 3) object.
Parameters: | value – The value to check. |
---|---|
Returns: | True if the value is a string, False otherwise. |
Simple function decorators to make Python programming easier.
The name of the property used to cache the return values of functions (a string).
Rudimentary caching decorator for functions.
Parameters: | function – The function whose return value should be cached. |
---|---|
Returns: | The decorated function. |
The given function will only be called once, the first time the wrapper function is called. The return value is cached by the wrapper function as an attribute of the given function and returned on each subsequent call.
Note
Currently no function arguments are supported because only a single return value can be cached. Accepting any function arguments at all would imply that the cache is parametrized on function arguments, which is not currently the case.
Interactive terminal prompts.
The prompts module enables interaction with the user (operator) by asking for confirmation (prompt_for_confirmation()) and asking to choose from a list of options (prompt_for_choice()). It works by rendering interactive prompts on the terminal.
The number of times an interactive prompt is shown on invalid input (an integer).
Prompt the user for confirmation.
Parameters: |
|
---|---|
Returns: |
|
Raises: |
|
When default is False and the user doesn’t enter any text an error message is printed and the prompt is repeated:
>>> prompt_for_confirmation("Are you sure?")
Are you sure? [y/n]
Error: Please enter 'yes' or 'no' (there's no default choice).
Are you sure? [y/n]
The same thing happens when the user enters text that isn’t recognized:
>>> prompt_for_confirmation("Are you sure?")
Are you sure? [y/n] about what?
Error: Please enter 'yes' or 'no' (the text 'about what?' is not recognized).
Are you sure? [y/n]
Prompt the user to select a choice from a group of options.
Parameters: |
|
---|---|
Returns: | The string corresponding to the user’s choice. |
Raises: |
|
When no options are given an exception is raised:
>>> prompt_for_choice([])
Traceback (most recent call last):
File "humanfriendly/prompts.py", line 148, in prompt_for_choice
raise ValueError("Can't prompt for choice without any options!")
ValueError: Can't prompt for choice without any options!
If a single option is given the user isn’t prompted:
>>> prompt_for_choice(['only one choice'])
'only one choice'
Here’s what the actual prompt looks like by default:
>>> prompt_for_choice(['first option', 'second option'])
1. first option
2. second option
Enter your choice as a number or unique substring (Control-C aborts): second
'second option'
If you don’t like the whitespace (empty lines and indentation):
>>> prompt_for_choice(['first option', 'second option'], padding=False)
1. first option
2. second option
Enter your choice as a number or unique substring (Control-C aborts): first
'first option'
Prompt the user for input (free form text).
Parameters: |
|
---|---|
Returns: | The text entered by the user (a string) or the value of the default argument. |
Raises: |
|
Wrap a text to be rendered as an interactive prompt in ANSI escape sequences.
Parameters: |
|
---|---|
Returns: | The resulting prompt text (a string). |
ANSI escape sequences are only used when the standard output stream is connected to a terminal. When the standard input stream is connected to a terminal any escape sequences are wrapped in “readline hints”.
Make interactive prompts more user friendly.
The prompts presented by raw_input() (in Python 2) and input() (in Python 3) are not very user friendly by default, for example the cursor keys (←, ↑, → and ↓) and the Home and End keys enter characters instead of performing the action you would expect them to. By simply importing the readline module these prompts become much friendlier (as mentioned in the Python standard library documentation).
This function is called by the other functions in this module to enable user friendly prompts.
Allow the user to provide valid input up to limit times.
Parameters: | limit – The maximum number of attempts (a number, defaults to MAX_ATTEMPTS). |
---|---|
Returns: | A generator of numbers starting from one. |
Raises: | TooManyInvalidReplies when an interactive prompt receives repeated invalid input (MAX_ATTEMPTS). |
This function returns a generator for interactive prompts that want to repeat on invalid input without getting stuck in infinite loops.
Raised by interactive prompts when they’ve received too many invalid inputs.
Customizations for and integration with the Sphinx documentation generator.
The humanfriendly.sphinx module uses the Sphinx extension API to customize the process of generating Sphinx based Python documentation. The most relevant functions to take a look at are setup(), enable_special_methods() and enable_usage_formatting().
Enable all of the provided Sphinx customizations.
Parameters: | app – The Sphinx application object. |
---|
The setup() function makes it easy to enable all of the Sphinx customizations provided by the humanfriendly.sphinx module with the least amount of code. All you need to do is to add the module name to the extensions variable in your conf.py file:
# Sphinx extension module names.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.doctest',
'sphinx.ext.intersphinx',
'humanfriendly.sphinx',
]
When Sphinx sees the humanfriendly.sphinx name it will import the module and call its setup() function.
At the time of writing this just calls enable_special_methods() and enable_usage_formatting(), but of course more functionality may be added at a later stage. If you don’t like that idea you may be better of calling the individual functions from your own setup() function.
Enable documenting “special methods” using the autodoc extension.
Parameters: | app – The Sphinx application object. |
---|
This function connects the special_methods_callback() function to autodoc-skip-member events.
Enable documenting “special methods” using the autodoc extension.
Refer to enable_special_methods() to enable the use of this function (you probably don’t want to call special_methods_callback() directly).
This function implements a callback for autodoc-skip-member events to include documented “special methods” (method names with two leading and two trailing underscores) in your documentation. The result is similar to the use of the special-members flag with one big difference: Special methods are included but other types of members are ignored. This means that attributes like __weakref__ will always be ignored (this was my main annoyance with the special-members flag).
The parameters expected by this function are those defined for Sphinx event callback functions (i.e. I’m not going to document them here :-).
Reformat human friendly usage messages to reStructuredText.
Parameters: | app – The Sphinx application object (as given to setup()). |
---|
This function connects the usage_message_callback() function to autodoc-process-docstring events.
Reformat human friendly usage messages to reStructuredText.
Refer to enable_usage_formatting() to enable the use of this function (you probably don’t want to call usage_message_callback() directly).
This function implements a callback for autodoc-process-docstring that reformats module docstrings using render_usage() so that Sphinx doesn’t mangle usage messages that were written to be human readable instead of machine readable. Only module docstrings whose first line starts with USAGE_MARKER are reformatted.
The parameters expected by this function are those defined for Sphinx event callback functions (i.e. I’m not going to document them here :-).
Functions that render ASCII tables.
Some generic notes about the table formatting functions in this module:
Render tabular data using the most appropriate representation.
Parameters: | |
---|---|
Returns: | The rendered table (a string). |
If you want an easy way to render tabular data on a terminal in a human friendly format then this function is for you! It works as follows:
Render a table using characters like dashes and vertical bars to emulate borders.
Parameters: |
|
---|---|
Returns: | The rendered table (a string). |
Here’s an example:
>>> from humanfriendly.tables import format_pretty_table
>>> column_names = ['Version', 'Uploaded on', 'Downloads']
>>> humanfriendly_releases = [
... ['1.23', '2015-05-25', '218'],
... ['1.23.1', '2015-05-26', '1354'],
... ['1.24', '2015-05-26', '223'],
... ['1.25', '2015-05-26', '4319'],
... ['1.25.1', '2015-06-02', '197'],
... ]
>>> print(format_pretty_table(humanfriendly_releases, column_names))
-------------------------------------
| Version | Uploaded on | Downloads |
-------------------------------------
| 1.23 | 2015-05-25 | 218 |
| 1.23.1 | 2015-05-26 | 1354 |
| 1.24 | 2015-05-26 | 223 |
| 1.25 | 2015-05-26 | 4319 |
| 1.25.1 | 2015-06-02 | 197 |
-------------------------------------
Notes about the resulting table:
If a column contains numeric data (integer and/or floating point numbers) in all rows (ignoring column names of course) then the content of that column is right-aligned, as can be seen in the example above. The idea here is to make it easier to compare the numbers in different columns to each other.
The column names are highlighted in color so they stand out a bit more (see also HIGHLIGHT_COLOR). The following screen shot shows what that looks like (my terminals are always set to white text on a black background):
Render tabular data with one column per line (allowing columns with line breaks).
Parameters: | |
---|---|
Returns: | The rendered table (a string). |
Here’s an example:
>>> from humanfriendly.tables import format_robust_table
>>> column_names = ['Version', 'Uploaded on', 'Downloads']
>>> humanfriendly_releases = [
... ['1.23', '2015-05-25', '218'],
... ['1.23.1', '2015-05-26', '1354'],
... ['1.24', '2015-05-26', '223'],
... ['1.25', '2015-05-26', '4319'],
... ['1.25.1', '2015-06-02', '197'],
... ]
>>> print(format_robust_table(humanfriendly_releases, column_names))
-----------------------
Version: 1.23
Uploaded on: 2015-05-25
Downloads: 218
-----------------------
Version: 1.23.1
Uploaded on: 2015-05-26
Downloads: 1354
-----------------------
Version: 1.24
Uploaded on: 2015-05-26
Downloads: 223
-----------------------
Version: 1.25
Uploaded on: 2015-05-26
Downloads: 4319
-----------------------
Version: 1.25.1
Uploaded on: 2015-06-02
Downloads: 197
-----------------------
The column names are highlighted in bold font and color so they stand out a bit more (see HIGHLIGHT_COLOR).
Interaction with UNIX terminals.
The terminal module makes it easy to interact with UNIX terminals and format text for rendering on UNIX terminals. If the terms used in the documentation of this module don’t make sense to you then please refer to the Wikipedia article on ANSI escape sequences for details about how ANSI escape sequences work.
The ANSI “Control Sequence Introducer” (a string).
The ANSI “Select Graphic Rendition” sequence (a string).
The ANSI escape sequence to erase the current line (a string).
The ANSI escape sequence to reset styling (a string).
A dictionary with (name, number) pairs of portable color codes. Used by ansi_style() to generate ANSI escape sequences that change font color.
A dictionary with (name, number) pairs of text styles (effects). Used by ansi_style() to generate ANSI escape sequences that change text styles. Only widely supported text styles are included here.
A compiled regular expression used to separate significant characters from other text.
This pattern is used by clean_terminal_output() to split terminal output into regular text versus backspace, carriage return and line feed characters and ANSI ‘erase line’ escape sequences.
The default number of lines in a terminal (an integer).
The default number of columns in a terminal (an integer).
The output encoding for Unicode strings.
The color used to highlight important tokens in formatted text (e.g. the usage message of the humanfriendly program). If the environment variable $HUMANFRIENDLY_HIGHLIGHT_COLOR is set it determines the value of HIGHLIGHT_COLOR.
Print a formatted message to the standard output stream.
For details about argument handling please refer to format().
Renders the message using format() and writes the resulting string (followed by a newline) to sys.stdout using auto_encode().
Print a formatted message to the standard error stream.
For details about argument handling please refer to format().
Renders the message using format() and writes the resulting string (followed by a newline) to sys.stderr using auto_encode().
Show a warning message on the terminal.
For details about argument handling please refer to format().
Renders the message using format() and writes the resulting string (followed by a newline) to sys.stderr using auto_encode().
If sys.stderr is connected to a terminal that supports colors, ansi_wrap() is used to color the message in a red font (to make the warning stand out from surrounding text).
Reliably write Unicode strings to the terminal.
Parameters: |
|
---|
Renders the text using format() and writes it to the given stream. If an UnicodeEncodeError is encountered in doing so, the text is encoded using DEFAULT_ENCODING and the write is retried. The reasoning behind this rather blunt approach is that it’s preferable to get output on the command line in the wrong encoding then to have the Python program blow up with a UnicodeEncodeError exception.
Strip ANSI escape sequences from the given string.
Parameters: |
|
---|---|
Returns: | The text without ANSI escape sequences (a string). |
Generate ANSI escape sequences for the given color and/or style(s).
Parameters: |
|
---|---|
Returns: | The ANSI escape sequences to enable the requested text styles or an empty string if no styles were requested. |
Raises: | ValueError when an invalid color name is given. |
Even though only eight named colors are supported, the use of bright=True and faint=True increases the number of available colors to around 24 (it may be slightly lower, for example because faint black is just black).
Support for 8-bit colors
In release 4.7 support for 256 color mode was added. While this significantly increases the available colors it’s not very human friendly in usage because you need to look up color codes in the 256 color mode palette.
You can use the humanfriendly --demo command to get a demonstration of the available colors, see also the screen shot below. Note that the small font size in the screen shot was so that the demonstration of 256 color mode support would fit into a single screen shot without scrolling :-) (I wasn’t feeling very creative).
Support for 24-bit colors
In release 4.14 support for 24-bit colors was added by accepting a tuple or list with three integers representing the RGB (red, green, blue) value of a color. This is not included in the demo because rendering millions of colors was deemed unpractical ;-).
Calculate the effective width of the given text (ignoring ANSI escape sequences).
Parameters: | text – The text whose width should be calculated (a string). |
---|---|
Returns: | The width of the text without ANSI escape sequences (an integer). |
This function uses ansi_strip() to strip ANSI escape sequences from the given string and returns the length of the resulting string.
Wrap text in ANSI escape sequences for the given color and/or style(s).
Parameters: |
|
---|---|
Returns: | The result of this function depends on the keyword arguments:
|
Wrap an ANSI escape sequence in readline hints.
Parameters: | text – The text with the escape sequence to wrap (a string). |
---|---|
Returns: | The wrapped text. |
Remove readline hints from a string.
Parameters: | text – The text to strip (a string). |
---|---|
Returns: | The stripped text. |
Clean up the terminal output of a command.
Parameters: | text – The raw text with special characters (a Unicode string). |
---|---|
Returns: | A list of Unicode strings (one for each line). |
This function emulates the effect of backspace (0x08), carriage return (0x0D) and line feed (0x0A) characters and the ANSI ‘erase line’ escape sequence on interactive terminals. It’s intended to clean up command output that was originally meant to be rendered on an interactive terminal and that has been captured using e.g. the script program [1] or the pty module [2].
[1] | My coloredlogs package supports the coloredlogs --to-html command which uses script to fool a subprocess into thinking that it’s connected to an interactive terminal (in order to get it to emit ANSI escape sequences). |
[2] | My capturer package uses the pty module to fool the current process and subprocesses into thinking they are connected to an interactive terminal (in order to get them to emit ANSI escape sequences). |
Some caveats about the use of this function:
Check if a stream is connected to a terminal.
Parameters: | stream – The stream to check (a file-like object, defaults to sys.stdout). |
---|---|
Returns: | True if the stream is connected to a terminal, False otherwise. |
See also terminal_supports_colors().
Convert HTML with simple text formatting to text with ANSI escape sequences.
Parameters: |
|
---|---|
Returns: | Text with ANSI escape sequences (a string). |
Please refer to the documentation of the HTMLConverter class for details about the conversion process (like which tags are supported) and an example with a screenshot.
Check if a stream is connected to a terminal that supports ANSI escape sequences.
Parameters: | stream – The stream to check (a file-like object, defaults to sys.stdout). |
---|---|
Returns: | True if the terminal supports ANSI escape sequences, False otherwise. |
This function is inspired by the implementation of django.core.management.color.supports_color().
Determine the number of lines and columns visible in the terminal.
Returns: | A tuple of two integers with the line and column count. |
---|
The result of this function is based on the first of the following three methods that works:
Note
The find_terminal_size() function performs the steps above every time it is called, the result is not cached. This is because the size of a virtual terminal can change at any time and the result of find_terminal_size() should be correct.
Pre-emptive snarky comment: It’s possible to cache the result of this function and use signal.SIGWINCH to refresh the cached values!
Response: As a library I don’t consider it the role of the humanfriendly.terminal module to install a process wide signal handler ...
Find the terminal size using fcntl.ioctl().
Parameters: | stream – A stream connected to the terminal (a file object with a fileno attribute). |
---|---|
Returns: | A tuple of two integers with the line and column count. |
Raises: | This function can raise exceptions but I’m not going to document them here, you should be using find_terminal_size(). |
Based on an implementation found on StackOverflow.
Find the terminal size using the external command stty size.
Parameters: | stream – A stream connected to the terminal (a file object). |
---|---|
Returns: | A tuple of two integers with the line and column count. |
Raises: | This function can raise exceptions but I’m not going to document them here, you should be using find_terminal_size(). |
Print a human friendly usage message to the terminal.
Parameters: | text – The usage message to print (a string). |
---|
This function does two things:
Print a large text to the terminal using a pager.
Parameters: |
|
---|
When connected_to_terminal() returns True a pager is used to show the text on the terminal, otherwise the text is printed directly without invoking a pager.
The use of a pager helps to avoid the wall of text effect where the user has to scroll up to see where the output began (not very user friendly).
Refer to get_pager_command() for details about the command line that’s used to invoke the pager.
Get the command to show a text on the terminal using a pager.
Parameters: | text – The text to print to the terminal (a string). |
---|---|
Returns: | A list of strings with the pager command and arguments. |
The use of a pager helps to avoid the wall of text effect where the user has to scroll up to see where the output began (not very user friendly).
If the given text contains ANSI escape sequences the command less --RAW-CONTROL-CHARS is used, otherwise the environment variable $PAGER is used (if $PAGER isn’t set less is used).
When the selected pager is less, the following options are used to make the experience more user friendly:
Convert HTML with simple text formatting to text with ANSI escape sequences.
The following text styles are supported:
Colors can be specified as follows:
Here’s a small demonstration:
from humanfriendly.text import dedent
from humanfriendly.terminal import html_to_ansi
print(html_to_ansi(dedent('''
<b>Hello world!</b>
<i>Is this thing on?</i>
I guess I can <u>underline</u> or <s>strike-through</s> text?
And what about <span style="color: red">color</span>?
''')))
rainbow_colors = [
'#FF0000', '#E2571E', '#FF7F00', '#FFFF00', '#00FF00',
'#96BF33', '#0000FF', '#4B0082', '#8B00FF', '#FFFFFF',
]
html_rainbow = "".join('<span style="color: %s">o</span>' % c for c in rainbow_colors)
print(html_to_ansi("Let's try a rainbow: %s" % html_rainbow))
Here’s what the results look like:
Some more details:
Implementation notes:
New in version 4.15: humanfriendly.terminal.HTMLConverter was added to the humanfriendly package during the initial development of my new chat-archive project, whose command line interface makes for a great demonstration of the flexibility that this feature provides (hint: check out how the search keyword highlighting combines with the regular highlighting).
The names of tags that are padded with vertical whitespace.
Initialize an HTMLConverter object.
Parameters: |
|
---|
Reset the parser, convert some HTML and get the text with ANSI escape sequences.
Parameters: | data – The HTML to convert to text (a string). |
---|---|
Returns: | The converted text (only in case output is a StringIO object). |
Get the current style from the top of the stack (a dictionary).
Close previously opened ANSI escape sequences.
This method overrides the same method in the superclass to ensure that an ANSI_RESET code is emitted when parsing reaches the end of the input but a style is still active. This is intended to prevent malformed HTML from messing up terminal output.
Emit an ANSI escape sequence for the given or current style to the output stream.
Parameters: | style – A dictionary with arguments for ansi_style() or None, in which case the style at the top of the stack is emitted. |
---|
Process a decimal or hexadecimal numeric character reference.
Parameters: | value – The decimal or hexadecimal value (a string). |
---|
Process textual data.
Parameters: | data – The decoded text (a string). |
---|
Process the end of an HTML tag.
Parameters: | tag – The name of the tag (a string). |
---|
Process a named character reference.
Parameters: | name – The name of the character reference (a string). |
---|
Process the start of an HTML tag.
Parameters: |
|
---|
Normalize a URL to enable string equality comparison.
Parameters: | url – The URL to normalize (a string). |
---|---|
Returns: | The normalized URL (a string). |
Convert a CSS color to something that ansi_style() understands.
Parameters: | value – A string like rgb(1,2,3), #AABBCC or yellow. |
---|---|
Returns: | A color value supported by ansi_style() or None. |
Push new style information onto the stack.
Parameters: | changes – Any keyword arguments are passed on to ansi_style(). |
---|
This method is a helper for handle_starttag() that does the following:
Prepare a URL for rendering on the terminal.
Parameters: | url – The URL to simplify (a string). |
---|---|
Returns: | The simplified URL (a string). |
This method pre-processes a URL before rendering on the terminal. The following modifications are made:
Reset the state of the HTML parser and ANSI converter.
When output is a StringIO object a new instance will be created (and the old one garbage collected).
Compare two URLs for equality using normalize_url().
Parameters: |
|
---|---|
Returns: |
This method is used by handle_endtag() to omit the URL of a hyperlink (<a href="...">) when the link text is that same URL.
Utility classes and functions that make it easy to write unittest compatible test suites.
Over the years I’ve developed the habit of writing test suites for Python projects using the unittest module. During those years I’ve come to know pytest and in fact I use pytest to run my test suites (due to its much better error reporting) but I’ve yet to publish a test suite that requires pytest. I have several reasons for doing so:
Automatically configure logging to the terminal.
Parameters: | log_level – The log verbosity (a number, defaults to logging.DEBUG). |
---|
When coloredlogs is installed coloredlogs.install() will be used to configure logging to the terminal. When this fails with an ImportError then logging.basicConfig() is used as a fall back.
Create missing directories.
Parameters: | pathname – The pathname of a directory (a string). |
---|
Retry a function until assertions no longer fail.
Parameters: |
|
---|---|
Returns: | The value returned by func. |
Raises: | Once the timeout has expired retry() will raise the previously retried assertion error. When func keeps returning False until timeout expires CallableTimedOut will be raised. |
This function sleeps between retries to avoid claiming CPU cycles we don’t need. It starts by sleeping for 0.1 second but adjusts this to one second as the number of retries grows.
Test a command line entry point.
Parameters: |
|
---|---|
Returns: | A tuple with two values:
|
The equivalent of the UNIX touch program in Python.
Parameters: | filename – The pathname of the file to touch (a string). |
---|
Note that missing directories are automatically created using make_dirs().
Base class to enable composition of context managers.
Enable use as context managers.
Enable use as context managers.
Context manager that temporary replaces an object attribute using setattr().
Initialize a PatchedAttribute object.
Parameters: |
|
---|
Replace (patch) the attribute.
Returns: | The object whose attribute was patched. |
---|
Restore the attribute to its original value.
Context manager that temporary replaces an object item using __setitem__().
Initialize a PatchedItem object.
Parameters: |
|
---|
Replace (patch) the item.
Returns: | The object whose item was patched. |
---|
Restore the item to its original value.
Easy temporary directory creation & cleanup using the with statement.
Here’s an example of how to use this:
with TemporaryDirectory() as directory:
# Do something useful here.
assert os.path.isdir(directory)
Initialize a TemporaryDirectory object.
Parameters: | options – Any keyword arguments are passed on to tempfile.mkdtemp(). |
---|
Create the temporary directory using tempfile.mkdtemp().
Returns: | The pathname of the directory (a string). |
---|
Cleanup the temporary directory using shutil.rmtree().
Context manager to temporarily customize $PATH (the executable search path).
This class is a composition of the PatchedItem and TemporaryDirectory context managers.
Initialize a CustomSearchPath object.
Parameters: | isolated – True to clear the original search path, False to add the temporary directory to the start of the search path. |
---|
Activate the custom $PATH.
Returns: | The pathname of the directory that has been added to $PATH (a string). |
---|
Deactivate the custom $PATH.
The value of $PATH or os.defpath (a string).
Context manager to mock the existence of a program (executable).
This class extends the functionality of CustomSearchPath.
Initialize a MockedProgram object.
Parameters: |
|
---|
Create the mock program.
Returns: | The pathname of the directory that has been added to $PATH (a string). |
---|
Ensure that the mock program was run.
Raises: | AssertionError when the mock program hasn’t been run. |
---|
Context manager that captures what’s written to sys.stdout and sys.stderr.
Initialize a CaptureOutput object.
Parameters: |
---|
Start capturing what’s written to sys.stdout and sys.stderr.
Stop capturing what’s written to sys.stdout and sys.stderr.
Get the text written to sys.stdout.
Subclass of unittest.TestCase with automatic logging and other miscellaneous features.
A list of exception types that are translated into skipped tests.
Wrap test methods using skipTestWrapper().
Replacement for unittest.TestCase.assertRaises() that returns the exception.
Refer to the unittest.TestCase.assertRaises() documentation for details on argument handling. The return value is the caught exception.
Warning
This method does not support use as a context manager.
Automatically configure logging to the terminal.
Parameters: | log_level – Refer to configure_logging(). |
---|
The setUp() method is automatically called by unittest.TestCase before each test method starts. It does two things:
Decide whether a test that raised an exception should be skipped.
Parameters: | exception – The exception that was raised by the test. |
---|---|
Returns: | True to translate the exception into a skipped test, False to propagate the exception as usual. |
The shouldSkipTest() method skips exceptions listed in the exceptionsToSkip attribute. This enables subclasses of TestCase to customize the default behavior with a one liner.
Enable skipping of tests.
This method was added in humanfriendly 3.3 as a fall back for the unittest.TestCase.skipTest() method that was added in Python 2.7 and 3.1 (because humanfriendly also supports Python 2.6).
Since then humanfriendly has gained a conditional dependency on unittest2 which enables actual skipping of tests (instead of just mocking it) on Python 2.6.
This method now remains for backwards compatibility (and just because it’s a nice shortcut).
Wrap test methods to translate exceptions into skipped tests.
Parameters: |
|
---|---|
Returns: | The return value of the test method. |
When a TestCase object is initialized, __init__() wraps all of the test_* methods with skipTestWrapper().
When a test method raises an exception, skipTestWrapper() will catch the exception and call shouldSkipTest() to decide whether to translate the exception into a skipped test.
When shouldSkipTest() returns True the exception is swallowed and unittest.SkipTest is raised instead of the original exception.
Simple text manipulation functions.
The text module contains simple functions to manipulate text:
Compact whitespace in a string.
Trims leading and trailing whitespace, replaces runs of whitespace characters with a single space and interpolates any arguments using format().
Parameters: | |
---|---|
Returns: | The compacted text (a string). |
Here’s an example of how I like to use the compact() function, this is an example from a random unrelated project I’m working on at the moment:
raise PortDiscoveryError(compact("""
Failed to discover port(s) that Apache is listening on!
Maybe I'm parsing the wrong configuration file? ({filename})
""", filename=self.ports_config))
The combination of compact() and Python’s multi line strings allows me to write long text fragments with interpolated variables that are easy to write, easy to read and work well with Python’s whitespace sensitivity.
Replace repeating empty lines with a single empty line (similar to cat -s).
Parameters: | text – The text in which to compact empty lines (a string). |
---|---|
Returns: | The text with empty lines compacted (a string). |
Concatenate a list of items in a human friendly way.
Parameters: | items – A sequence of strings. |
---|---|
Returns: | A single string. |
>>> from humanfriendly.text import concatenate
>>> concatenate(["eggs", "milk", "bread"])
'eggs, milk and bread'
Dedent a string (remove common leading whitespace from all lines).
Removes common leading whitespace from all lines in the string using textwrap.dedent(), removes leading and trailing empty lines using trim_empty_lines() and interpolates any arguments using format().
Parameters: | |
---|---|
Returns: | The dedented text (a string). |
The compact() function’s documentation contains an example of how I like to use the compact() and dedent() functions. The main difference is that I use compact() for text that will be presented to the user (where whitespace is not so significant) and dedent() for data file and code generation tasks (where newlines and indentation are very significant).
Format a string using the string formatting operator and/or str.format().
Parameters: |
|
---|---|
Returns: | The text with any positional and/or keyword arguments interpolated (a string). |
The implementation of this function is so trivial that it seems silly to even bother writing and documenting it. Justifying this requires some context :-).
Why format() instead of the string formatting operator?
For really simple string interpolation Python’s string formatting operator is ideal, but it does have some strange quirks:
When you switch from interpolating a single value to interpolating multiple values you have to wrap them in tuple syntax. Because format() takes a variable number of arguments it always receives a tuple (which saves me a context switch :-). Here’s an example:
>>> from humanfriendly.text import format
>>> # The string formatting operator.
>>> print('the magic number is %s' % 42)
the magic number is 42
>>> print('the magic numbers are %s and %s' % (12, 42))
the magic numbers are 12 and 42
>>> # The format() function.
>>> print(format('the magic number is %s', 42))
the magic number is 42
>>> print(format('the magic numbers are %s and %s', 12, 42))
the magic numbers are 12 and 42
When you interpolate a single value and someone accidentally passes in a tuple your code raises a TypeError. Because format() takes a variable number of arguments it always receives a tuple so this can never happen. Here’s an example:
>>> # How expecting to interpolate a single value can fail.
>>> value = (12, 42)
>>> print('the magic value is %s' % value)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>> # The following line works as intended, no surprises here!
>>> print(format('the magic value is %s', value))
the magic value is (12, 42)
Why format() instead of the str.format() method?
When you’re doing complex string interpolation the str.format() function results in more readable code, however I frequently find myself adding parentheses to force evaluation order. The format() function avoids this because of the relative priority between the comma and dot operators. Here’s an example:
>>> "{adjective} example" + " " + "(can't think of anything less {adjective})".format(adjective='silly')
"{adjective} example (can't think of anything less silly)"
>>> ("{adjective} example" + " " + "(can't think of anything less {adjective})").format(adjective='silly')
"silly example (can't think of anything less silly)"
>>> format("{adjective} example" + " " + "(can't think of anything less {adjective})", adjective='silly')
"silly example (can't think of anything less silly)"
The compact() and dedent() functions are wrappers that combine format() with whitespace manipulation to make it easy to write nice to read Python code.
Check if a text is empty or contains only whitespace.
Parameters: | text – The text to check for “emptiness” (a string). |
---|---|
Returns: | True if the text is empty or contains only whitespace, False otherwise. |
Remove “hard wrapping” from the paragraphs in a string.
Parameters: | text – The text to reformat (a string). |
---|---|
Returns: | The text without hard wrapping (a string). |
This function works by removing line breaks when the last character before a line break and the first character after the line break are both non-whitespace characters. This means that common leading indentation will break join_lines() (in that case you can use dedent() before calling join_lines()).
Combine a count with the singular or plural form of a word.
If the plural form of the word is not provided it is obtained by concatenating the singular form of the word with the letter “s”. Of course this will not always be correct, which is why you have the option to specify both forms.
Parameters: |
|
---|---|
Returns: | The count and singular/plural word concatenated (a string). |
Generate a random string.
Parameters: |
|
---|---|
Returns: | A random string. |
The random_string() function is very useful in test suites; by the time I included it in humanfriendly.text I had already included variants of this function in seven different test suites :-).
Split a comma-separated list of strings.
Parameters: |
|
---|---|
Returns: | A list of zero or more nonempty strings. |
Here’s the default behavior of Python’s built in str.split() function:
>>> 'foo,bar, baz,'.split(',')
['foo', 'bar', ' baz', '']
In contrast here’s the default behavior of the split() function:
>>> from humanfriendly.text import split
>>> split('foo,bar, baz,')
['foo', 'bar', 'baz']
Here is an example that parses a nested data structure (a mapping of logging level names to one or more styles per level) that’s encoded in a string so it can be set as an environment variable:
>>> from pprint import pprint
>>> encoded_data = 'debug=green;warning=yellow;error=red;critical=red,bold'
>>> parsed_data = dict((k, split(v, ',')) for k, v in (split(kv, '=') for kv in split(encoded_data, ';')))
>>> pprint(parsed_data)
{'debug': ['green'],
'warning': ['yellow'],
'error': ['red'],
'critical': ['red', 'bold']}
Split a string into paragraphs (one or more lines delimited by an empty line).
Parameters: | text – The text to split into paragraphs (a string). |
---|---|
Returns: | A list of strings. |
Tokenize a text into numbers and strings.
Parameters: | text – The text to tokenize (a string). |
---|---|
Returns: | A list of strings and/or numbers. |
This function is used to implement robust tokenization of user input in functions like parse_size() and parse_timespan(). It automatically coerces integer and floating point numbers, ignores whitespace and knows how to separate numbers from strings even without whitespace. Some examples to make this more concrete:
>>> from humanfriendly.text import tokenize
>>> tokenize('42')
[42]
>>> tokenize('42MB')
[42, 'MB']
>>> tokenize('42.5MB')
[42.5, 'MB']
>>> tokenize('42.5 MB')
[42.5, 'MB']
Trim leading and trailing empty lines from the given text.
Parameters: | text – The text to trim (a string). |
---|---|
Returns: | The trimmed text (a string). |
Parsing and reformatting of usage messages.
The usage module parses and reformats usage messages:
Usage messages in general are free format of course, however the functions in this module assume a certain structure from usage messages in order to successfully parse and reformat them, refer to parse_usage() for details.
The string that starts the first line of a usage message.
Highlight special items in a usage message.
Parameters: | usage_text – The usage message to process (a string). |
---|---|
Returns: | The usage message with special items highlighted. |
This function highlights the following special items:
All items are highlighted in the color defined by HIGHLIGHT_COLOR.
Find the meta variables in the given usage message.
Parameters: | usage_text – The usage message to parse (a string). |
---|---|
Returns: | A list of strings with any meta variables found in the usage message. |
When a command line option requires an argument, the convention is to format such options as --option=ARG. The text ARG in this example is the meta variable.
Parse a usage message by inferring its structure (and making some assumptions :-).
Parameters: | text – The usage message to parse (a string). |
---|---|
Returns: | A tuple of two lists:
|
Usage messages in general are free format of course, however parse_usage() assume a certain structure from usage messages in order to successfully parse them:
The usage message starts with a line Usage: ... that shows a symbolic representation of the way the program is to be invoked.
After some free form text a line Supported options: (surrounded by empty lines) precedes the documentation of the supported command line options.
The command line options are documented as follows:
-v, --verbose
Make more noise.
So all of the variants of the command line option are shown together on a separate line, followed by one or more paragraphs describing the option.
There are several other minor assumptions, but to be honest I’m not sure if anyone other than me is ever going to use this functionality, so for now I won’t list every intricate detail :-).
If you’re curious anyway, refer to the usage message of the humanfriendly package (defined in the humanfriendly.cli module) and compare it with the usage message you see when you run humanfriendly --help and the generated usage message embedded in the readme.
Feel free to request more detailed documentation if you’re interested in using the humanfriendly.usage module outside of the little ecosystem of Python packages that I have been building over the past years.
Reformat a command line program’s usage message to reStructuredText.
Parameters: | text – The plain text usage message (a string). |
---|---|
Returns: | The usage message rendered to reStructuredText (a string). |
Use cog to inject a usage message into a reStructuredText file.
Parameters: | module_name – The name of the module whose __doc__ attribute is the source of the usage message (a string). |
---|
This simple wrapper around render_usage() makes it very easy to inject a reformatted usage message into your documentation using cog. To use it you add a fragment like the following to your *.rst file:
.. [[[cog
.. from humanfriendly.usage import inject_usage
.. inject_usage('humanfriendly.cli')
.. ]]]
.. [[[end]]]
The lines in the fragment above are single line reStructuredText comments that are not copied to the output. Their purpose is to instruct cog where to inject the reformatted usage message. Once you’ve added these lines to your *.rst file, updating the rendered usage message becomes really simple thanks to cog:
$ cog.py -r README.rst
This will inject or replace the rendered usage message in your README.rst file with an up to date copy.
Import a module.
The ‘package’ argument is required when performing a relative import. It specifies the package to use as the anchor point from which to resolve the relative import to an absolute import.