xotl.tools.string
- Common string operations¶
Some additions for string standard module.
In Python 3 str is always unicode but unicode and basestring types doesn’t exists. bytes type can be used as an array of one byte each item.
-
xotl.tools.string.
cut_prefix
(self: str, prefix: str) → str[source]¶ Removes the leading prefix if exists, else return value unchanged.
In Python 3.9+ this is the same as
str.removeprefix()
.
-
xotl.tools.string.
cut_any_prefix
(value: str, *prefixes) → str[source]¶ Apply
cut_prefix()
for the first matching prefix.
-
xotl.tools.string.
cut_prefixes
(value: str, *prefixes) → str[source]¶ Apply
cut_prefix()
for all provided prefixes in order.
-
xotl.tools.string.
cut_suffix
(self: str, suffix: str) → str[source]¶ Removes the tailing suffix if exists, else return value unchanged.
In Python 3.9+ this is the same as
str.removesuffix()
.
-
xotl.tools.string.
cut_any_suffix
(value: str, *suffixes) → str[source]¶ Apply
cut_suffix()
for the first matching suffix.
-
xotl.tools.string.
cut_suffixes
(value: str, *suffixes) → str[source]¶ Apply
cut_suffix()
for all provided suffixes in order.
-
xotl.tools.string.
make_a10z
(string: str) → str[source]¶ Utility to find out that “internationalization” is “i18n”.
Examples:
>>> print(make_a10z('parametrization')) p13n
-
xotl.tools.string.
slugify
(value, replacement='-', invalid_chars='', valid_chars='', encoding=None)[source]¶ Return the normal-form of a given string value that is valid for slugs.
Convert all non-ascii to valid characters, whenever possible, using unicode ‘NFKC’ normalization and lower-case the result. Replace unwanted characters by the value of replacement (remove extra when repeated).
Default valid characters are
[_a-z0-9]
. Extra arguments invalid_chars and valid_chars can modify this standard behaviour, see next:Parameters: - value – The source value to slugify.
- replacement –
A character to be used as replacement for unwanted characters. Could be both, the first extra positional argument, or as a keyword argument. Default value is a hyphen (‘-‘).
There will be a contradiction if this argument contains any invalid character (see invalid_chars).
None
, orFalse
, will be converted converted to an empty string for backward compatibility with old versions of this function, but not use this, will be deprecated. - invalid_chars –
Characters to be considered invalid. There is a default set of valid characters which are kept in the resulting slug. Characters given in this parameter are removed from the resulting valid character set (see valid_chars).
Extra argument values can be used for compatibility with invalid_underscore argument in deprecated normalize_slug function:
True
is a synonymous of underscore"_"
.False
orNone
: An empty set.
Could be given as a name argument or in the second extra positional argument. Default value is an empty set.
- valid_chars – A collection of extra valid characters. Could be
either a valid string, any iterator of strings, or
None
to use only default valid characters. Non-ASCII characters are ignored. - encoding – If value is not a text (unicode), it is decoded before
ASCII normalization
.
Examples:
>>> slugify(' Á.e i Ó u ') == 'a-e-i-o-u' True >>> slugify(' Á.e i Ó u ', '.', invalid_chars='AU') == 'e.i.o' True >>> slugify(' Á.e i Ó u ', valid_chars='.') == 'a.e-i-o-u' True >>> slugify('_x', '_') == '_x' True >>> slugify('-x', '_') == 'x' True >>> slugify(None) == 'none' True >>> slugify(1 == 1) == 'true' True >>> slugify(1.0) == '1-0' True >>> slugify(135) == '135' True >>> slugify(123456, '', invalid_chars='52') == '1346' True >>> slugify('_x', '_') == '_x' True
Changed in version 1.5.5: Added the invalid_underscore parameter.
Changed in version 1.6.6: Replaced the invalid_underscore paremeter by invalids. Added the valids parameter.
Changed in version 1.7.2: Clarified the role of invalids with regards to replacement.
Changed in version 1.8.0: Deprecate the invalids paremeter name in favor of invalid_chars, also deprecate the valids paremeter name in favor of valid_chars.
Changed in version 1.8.7: Add parameter ‘encoding’.
Changed in version 2.1.0: Remove deprecated parameters invalids and valids.