module documentation

Various utility classes and functions.

Unknown Field: copyright
  1. 2013-2021 by the Babel Team.
Unknown Field: license
BSD, see LICENSE for more details.
Variable missing Undocumented
Variable ​PYTHON_​FUTURE_​IMPORT_re Undocumented
Variable ​PYTHON_​MAGIC_​COMMENT_re Undocumented
Class ​Fixed​Offset​Timezone Fixed offset in minutes east from UTC.
Class ​Text​Wrapper Undocumented
Function distinct Yield all items in an iterable collection that are distinct.
Function parse​_encoding Deduce the encoding of a source file from magic comment.
Function parse​_future​_flags Parse the compiler flags by __future__ from the given Python code.
Function pathmatch Extended pathname pattern matching.
Function wraptext Simple wrapper around the textwrap.wrap function in the standard library. This version does not wrap lines on hyphens in words.
missing =

Undocumented

PYTHON_FUTURE_IMPORT_re =

Undocumented

PYTHON_MAGIC_COMMENT_re =

Undocumented

def distinct(iterable):

Yield all items in an iterable collection that are distinct.

Unlike when using sets for a similar effect, the original ordering of the items in the collection is preserved by this function.

>>> print(list(distinct([1, 2, 1, 3, 4, 4])))
[1, 2, 3, 4]
>>> print(list(distinct('foobar')))
['f', 'o', 'b', 'a', 'r']
Parameters
iterablethe iterable collection providing the data
def parse_encoding(fp):

Deduce the encoding of a source file from magic comment.

It does this in the same way as the Python interpreter

The fp argument should be a seekable file object.

(From Jeff Dairiki)

def parse_future_flags(fp, encoding='latin-1'):
Parse the compiler flags by __future__ from the given Python code.
def pathmatch(pattern, filename):

Extended pathname pattern matching.

This function is similar to what is provided by the fnmatch module in the Python standard library, but:

  • can match complete (relative or absolute) path names, and not just file names, and
  • also supports a convenience pattern ("**") to match files at any directory level.

Examples:

>>> pathmatch('**.py', 'bar.py')
True
>>> pathmatch('**.py', 'foo/bar/baz.py')
True
>>> pathmatch('**.py', 'templates/index.html')
False
>>> pathmatch('./foo/**.py', 'foo/bar/baz.py')
True
>>> pathmatch('./foo/**.py', 'bar/baz.py')
False
>>> pathmatch('^foo/**.py', 'foo/bar/baz.py')
True
>>> pathmatch('^foo/**.py', 'bar/baz.py')
False
>>> pathmatch('**/templates/*.html', 'templates/index.html')
True
>>> pathmatch('**/templates/*.html', 'templates/foo/bar.html')
False
Parameters
patternthe glob pattern
filenamethe path name of the file to match against
def wraptext(text, width=70, initial_indent='', subsequent_indent=''):
Simple wrapper around the textwrap.wrap function in the standard library. This version does not wrap lines on hyphens in words.
Parameters
textthe text to wrap
widththe maximum line width
initial​_indentstring that will be prepended to the first line of wrapped output
subsequent​_indentstring that will be prepended to all lines save the first of wrapped output