words
Functions for working with (English) words.
New in version 0.4.5.
Constants
Data:
The carriage return character ( |
|
The newline character ( |
|
A literal |
|
ASCII numbers. |
|
Lowercase Greek letters. |
|
Uppercase Greek letters. |
Fonts
Data:
Doublestruck |
|
Fraktur |
|
Monospace |
|
Bold and Italic Sans-Serif |
|
Bold Sans-Serif |
|
Italic Sans-Serif |
|
Normal Sans-Serif |
|
Script |
|
Bold and Italic Serif |
|
Bold Serif |
|
Italic Serif |
Classes:
Represents a Unicode pseudo-font. |
Functions:
|
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). DefaultNone
.greek_uppers (
Optional
[Iterable
[str
]]) – Optional iterable of uppercase Greek letters (A-Ω, 25 characters). DefaultNone
.greek_lowers (
Optional
[Iterable
[str
]]) – Optional iterable of lowercase Greek letters (α-ϖ, 32 characters). DefaultNone
.
- Return type
-
class
Font
[source] -
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 thegetitem
syntax. Entire strings can be converted by calling theFont
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.
-
__getitem__
(char)[source] Returns the given character in this font.
If the character is not found in this font the character is returned unchanged.
-
-
SERIF_BOLD_LETTERS
Bold Serif
Font
.This font includes numbers and 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.
Functions
Functions:
|
Sorts a list of strings using a custom alphabet. |
|
Convert the given value to a string. |
|
Returns a random word, optionally only one whose length is between |
|
Returns the list of words, optionally only those whose length is between |
|
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.
-
as_text
(value)[source] Convert the given value to a string.
None
is converted to''
.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
andmax_length
.New in version 0.4.5.
-
get_random_word
(min_length=0, max_length=- 1)[source] Returns a random word, optionally only one whose length is between
min_length
andmax_length
.New in version 0.4.5.
-
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
use_repr (
bool
) – Whether to join therepr
of each object. DefaultFalse
.oxford (
bool
) – Whether to use an oxford comma when joining the last two elements. DefaultFalse
. AlwaysFalse
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
Changed in version 0.11.0: Added
delimiter
andconnective
arguments.
Classes
Classes:
|
Represents a word as its singular and plural. |
|
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
>>> 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
.
-
namedtuple
PluralPhrase
(template, words)[source] Bases:
NamedTuple
Represents a phrase which varies depending on a numerical count.
New in version 3.3.0.
- Fields
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 keywordn
, 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.'
-
__repr__
() Return a string representation of the
PluralPhrase
.- Return type