words

Functions for working with (English) words.

New in version 0.4.5.

Constants

Data:

CR

The carriage return character (\r) for use in f-strings.

LF

The newline character (\n) for use in f-strings.

TAB

A literal TAB (\t) character for use in f-strings.

ascii_digits

ASCII numbers.

greek_lowercase

Lowercase Greek letters.

greek_uppercase

Uppercase Greek letters.

CR = '\r'

Type:    str

The carriage return character (\r) for use in f-strings.

New in version 1.3.0.

LF = '\n'

Type:    str

The newline character (\n) for use in f-strings.

New in version 1.3.0.

TAB = '\t'

Type:    str

A literal TAB (\t) character for use in f-strings.

New in version 1.3.0.

ascii_digits

Type:    str

ASCII numbers.

New in version 0.7.0.

greek_uppercase

Type:    str

Uppercase Greek letters.

New in version 0.7.0.

greek_lowercase

Type:    str

Lowercase Greek letters.

New in version 0.7.0.

Fonts

Data:

DOUBLESTRUCK_LETTERS

Doublestruck Font.

FRAKTUR_LETTERS

Fraktur Font.

MONOSPACE_LETTERS

Monospace Font.

SANS_SERIF_BOLD_ITALIC_LETTERS

Bold and Italic Sans-Serif Font.

SANS_SERIF_BOLD_LETTERS

Bold Sans-Serif Font.

SANS_SERIF_ITALIC_LETTERS

Italic Sans-Serif Font.

SANS_SERIF_LETTERS

Normal Sans-Serif Font.

SCRIPT_LETTERS

Script Font.

SERIF_BOLD_ITALIC_LETTERS

Bold and Italic Serif Font.

SERIF_BOLD_LETTERS

Bold Serif Font.

SERIF_ITALIC_LETTERS

Italic Serif Font.

Classes:

Font

Represents a Unicode pseudo-font.

Functions:

make_font(uppers, lowers[, digits, …])

Returns a dictionary mapping ASCII alphabetical characters and digits to the Unicode equivalents in a different pseudo-font.

make_font(uppers, lowers, digits=None, greek_uppers=None, greek_lowers=None)[source]

Returns a dictionary mapping ASCII alphabetical characters and digits to the Unicode equivalents in a different pseudo-font.

New in version 0.7.0.

Parameters
  • uppers (Iterable[str]) – Iterable of uppercase letters (A-Z, 26 characters).

  • lowers (Iterable[str]) – Iterable of lowercase letters (a-z, 26 characters).

  • digits (Optional[Iterable[str]]) – Optional iterable of digits (0-9). Default None.

  • greek_uppers (Optional[Iterable[str]]) – Optional iterable of uppercase Greek letters (A-Ω, 25 characters). Default None.

  • greek_lowers (Optional[Iterable[str]]) – Optional iterable of lowercase Greek letters (α-ϖ, 32 characters). Default None.

Return type

Font

class Font[source]

Bases: Dict[str, str]

Represents a Unicode pseudo-font.

Mapping of ASCII letters to their equivalents in the pseudo-font.

Individual characters can be converted using the Font.get method or the getitem syntax. Entire strings can be converted by calling the Font object and passing the string as the first argument.

Methods:

__call__(text)

Returns the given text in this font.

__getitem__(char)

Returns the given character in this font.

get(char[, default])

Returns the given character in this font.

__call__(text)[source]

Returns the given text in this font.

Parameters

text (str)

Return type

str

__getitem__(char)[source]

Returns the given character in this font.

If the character is not found in this font the character is returned unchanged.

Parameters

char (str) – The character to convert.

Return type

str

get(char, default=None)[source]

Returns the given character in this font.

If the character is not found in this font the character is returned unchanged or, if a value for default is provided, that is returned instead.

Parameters
  • char (str) – The character to convert.

  • default (Optional[str]) – Optional default value. Default None.

Return type

str

SERIF_BOLD_LETTERS

Bold Serif Font.

This font includes numbers and Greek letters.

New in version 0.7.0.

SERIF_ITALIC_LETTERS

Italic Serif Font.

This font includes Greek letters.

New in version 0.7.0.

SERIF_BOLD_ITALIC_LETTERS

Bold and Italic Serif Font.

This font includes Greek letters.

New in version 0.7.0.

SANS_SERIF_LETTERS

Normal Sans-Serif Font.

This font includes numbers.

New in version 0.7.0.

SANS_SERIF_BOLD_LETTERS

Bold Sans-Serif Font.

This font includes numbers.

New in version 0.7.0.

SANS_SERIF_ITALIC_LETTERS

Italic Sans-Serif Font.

New in version 0.7.0.

SANS_SERIF_BOLD_ITALIC_LETTERS

Bold and Italic Sans-Serif Font.

This font includes Greek letters.

New in version 0.7.0.

SCRIPT_LETTERS

Script Font.

New in version 0.7.0.

FRAKTUR_LETTERS

Fraktur Font.

New in version 0.7.0.

MONOSPACE_LETTERS

Monospace Font.

This font includes numbers.

New in version 0.7.0.

DOUBLESTRUCK_LETTERS

Doublestruck Font.

This font includes numbers.

New in version 0.7.0.

Functions

Functions:

alpha_sort(iterable, alphabet[, reverse])

Sorts a list of strings using a custom alphabet.

as_text(value)

Convert the given value to a string.

get_random_word([min_length, max_length])

Returns a random word, optionally only one whose length is between min_length and max_length.

get_words_list([min_length, max_length])

Returns the list of words, optionally only those whose length is between min_length and max_length.

word_join(iterable[, use_repr, oxford, …])

Join the given list of strings in a natural manner, with ‘and’ to join the last two elements.

alpha_sort(iterable, alphabet, reverse=False)[source]

Sorts a list of strings using a custom alphabet.

New in version 0.7.0.

Parameters
Return type

List[str]

as_text(value)[source]

Convert the given value to a string. None is converted to ''.

Parameters

value (Any) – The value to convert to a string.

Return type

str

Changed in version 0.8.0: Moved from domdf_python_tools.utils.

get_words_list(min_length=0, max_length=- 1)[source]

Returns the list of words, optionally only those whose length is between min_length and max_length.

New in version 0.4.5.

Parameters
  • min_length (int) – The minimum length of the words to return. Default 0.

  • max_length (int) – The maximum length of the words to return. A value of -1 indicates no upper limit.

Return type

List[str]

Returns

The list of words meeting the above specifiers.

get_random_word(min_length=0, max_length=- 1)[source]

Returns a random word, optionally only one whose length is between min_length and max_length.

New in version 0.4.5.

Parameters
  • min_length (int) – The minimum length of the words to return. Default 0.

  • max_length (int) – The maximum length of the words to return. A value of -1 indicates no upper limit.

Return type

str

Returns

A random word meeting the above specifiers.

word_join(iterable, use_repr=False, oxford=False, delimiter=',', connective='and')[source]

Join the given list of strings in a natural manner, with ‘and’ to join the last two elements.

Parameters
  • iterable (Iterable[str])

  • use_repr (bool) – Whether to join the repr of each object. Default False.

  • oxford (bool) – Whether to use an oxford comma when joining the last two elements. Default False. Always False if there are fewer than three elements.

  • delimiter (str) – A single character to use between the words. Default ','.

  • connective (str) – The connective to join the final two words with. Default 'and'.

Return type

str

Changed in version 0.11.0: Added delimiter and connective arguments.

truncate_string(string, max_length, ending=...)[source]

Truncate a string to max_length characters, and put ending on the end.

The truncated string is further truncated by the length of ending so the returned string is no more then max_length.

New in version 3.3.0.

Parameters
  • string (str)

  • max_length (int)

  • ending (str) – Default '...'.

Return type

str

Classes

Classes:

Plural(singular, plural)

Represents a word as its singular and plural.

PluralPhrase(template, words)

Represents a phrase which varies depending on a numerical count.

class Plural(singular, plural)[source]

Bases: partial

Represents a word as its singular and plural.

New in version 2.0.0.

Parameters
  • singular (str) – The singular form of the word.

  • plural (str) – The plural form of the word.

>>> cow = Plural("cow", "cows")
>>> n = 1
>>> print(f"The farmer has {n} {cow(n)}.")
The farmer has 1 cow.
>>> n = 2
>>> print(f"The farmer has {n} {cow(n)}.")
The farmer has 2 cows.
>>> n = 3
>>> print(f"The farmer has {n} {cow(n)}.")
The farmer has 3 cows.

Methods:

__call__(n)

Returns either the singular or plural form of the word depending on the value of n.

__repr__()

Return a string representation of the Plural.

__call__(n)[source]

Returns either the singular or plural form of the word depending on the value of n.

Parameters

n (int)

Return type

str

__repr__()[source]

Return a string representation of the Plural.

Return type

str

namedtuple PluralPhrase(template, words)[source]

Bases: NamedTuple

Represents a phrase which varies depending on a numerical count.

New in version 3.3.0.

Fields
  1.  template (str) – The phrase template.

  2.  words (Tuple[Plural, …]) – The words to insert into the template.

For example, consider the phase:

The proposed changes are to ...

The “phrase template” would be:

"The proposed {} {} to ..."

and the two words to insert are:

Plural("change", "changes")
Plural("is", "are")

The phrase is constructed as follows:

>>> phrase = PluralPhrase(
...     "The proposed {} {} to ...",
...     (Plural("change", "changes"), Plural("is", "are"))
... )
>>> phrase(1)
'The proposed change is to ...'
>>> phrase(2)
'The proposed changes are to ...'

The phrase template can use any valid syntax for str.format(), except for keyword arguments. The exception if the keyword n, which is replaced with the count (e.g. 2) passed in when the phrase is constructed. For example:

>>> phrase2 = PluralPhrase("The farmer has {n} {0}.", (Plural("cow", "cows"), ))
>>> phrase2(2)
'The farmer has 2 cows.'
__call__(n)[source]

Construct the phrase based on the value of n.

Parameters

n (int)

Return type

str

__repr__()

Return a string representation of the PluralPhrase.

Return type

str