strutils¶
System-level utilities and helper functions.
-
oslo_utils.strutils.
bool_from_string
(subject, strict=False, default=False)¶ Interpret a string as a boolean.
A case-insensitive match is performed such that strings matching ‘t’, ‘true’, ‘on’, ‘y’, ‘yes’, or ‘1’ are considered True and, when strict=False, anything else returns the value specified by ‘default’.
Useful for JSON-decoded stuff and config file parsing.
If strict=True, unrecognized values, including None, will raise a ValueError which is useful when parsing values passed in from an API call. Strings yielding False are ‘f’, ‘false’, ‘off’, ‘n’, ‘no’, or ‘0’.
-
oslo_utils.strutils.
int_from_bool_as_string
(subject)¶ Interpret a string as a boolean and return either 1 or 0.
Any string value in:
(‘True’, ‘true’, ‘On’, ‘on’, ‘1’)is interpreted as a boolean True.
Useful for JSON-decoded stuff and config file parsing
-
oslo_utils.strutils.
is_int_like
(val)¶ Check if a value looks like an integer with base 10.
Parameters: val (string) – Value to verify Returns: bool
-
oslo_utils.strutils.
mask_password
(message, secret='***')¶ Replace password with ‘secret’ in message.
Parameters: - message – The string which includes security information.
- secret – value with which to replace passwords.
Returns: The unicode value of message with the password fields masked.
For example:
>>> mask_password("'adminPass' : 'aaaaa'") "'adminPass' : '***'" >>> mask_password("'admin_pass' : 'aaaaa'") "'admin_pass' : '***'" >>> mask_password('"password" : "aaaaa"') '"password" : "***"' >>> mask_password("'original_password' : 'aaaaa'") "'original_password' : '***'" >>> mask_password("u'original_password' : u'aaaaa'") "u'original_password' : u'***'"
-
oslo_utils.strutils.
string_to_bytes
(text, unit_system='IEC', return_int=False)¶ Converts a string into an float representation of bytes.
The units supported for IEC
Kb(it), Kib(it), Mb(it), Mib(it), Gb(it), Gib(it), Tb(it), Tib(it) KB, KiB, MB, MiB, GB, GiB, TB, TiB
The units supported for SI
kb(it), Mb(it), Gb(it), Tb(it) kB, MB, GB, TB
Note that the SI unit system does not support capital letter ‘K’
Parameters: - text – String input for bytes size conversion.
- unit_system – Unit system for byte size conversion.
- return_int – If True, returns integer representation of text in bytes. (default: decimal)
Returns: Numerical representation of text in bytes.
Raises ValueError: If text has an invalid value.
-
oslo_utils.strutils.
to_slug
(value, incoming=None, errors='strict')¶ Normalize string.
Convert to lowercase, remove non-word characters, and convert spaces to hyphens.
Inspired by Django’s slugify filter.
Parameters: - value – Text to slugify
- incoming – Text’s current encoding
- errors – Errors handling policy. See here for valid values http://docs.python.org/2/library/codecs.html
Returns: slugified unicode representation of value
Raises TypeError: If text is not an instance of str