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
prefixif exists, else returnvalueunchanged.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
suffixif exists, else returnvalueunchanged.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 argumentsinvalid_charsandvalid_charscan 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_underscoreargument in deprecatednormalize_slugfunction:Trueis a synonymous of underscore"_".FalseorNone: 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
Noneto use only default valid characters. Non-ASCII characters are ignored. - encoding – If
valueis not a text (unicode), it is decoded beforeASCII 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_underscoreparameter.Changed in version 1.6.6: Replaced the
invalid_underscoreparemeter byinvalids. Added thevalidsparameter.Changed in version 1.7.2: Clarified the role of
invalidswith regards toreplacement.Changed in version 1.8.0: Deprecate the
invalidsparemeter name in favor ofinvalid_chars, also deprecate thevalidsparemeter name in favor ofvalid_chars.Changed in version 1.8.7: Add parameter ‘encoding’.
Changed in version 2.1.0: Remove deprecated parameters
invalidsandvalids.