mpu.string¶
String manipulation, verification and formatting.
For more complex checks, you might want to use the [validators](http://validators.readthedocs.io) package.
-
mpu.string.
human_readable_bytes
(nb_bytes: Union[int, float], suffix: str = 'B') → str[source]¶ Convert a byte number into a human readable format.
- Parameters
nb_bytes (Union[int, float]) –
suffix (str, optional (default: "B")) –
- Returns
size_str
- Return type
str
Examples
>>> human_readable_bytes(123) '123.0 B'
>>> human_readable_bytes(1025) '1.0 KiB'
>>> human_readable_bytes(9671406556917033397649423) '8.0 YiB'
-
mpu.string.
is_email
(potential_email_address: str) → bool[source]¶ Check if potential_email_address is a valid e-mail address.
Please note that this function has no false-negatives but many false-positives. So if it returns that the input is not a valid e-mail adress, it certainly isn’t. If it returns True, it might still be invalid. For example, the domain could not be registered.
- Parameters
potential_email_address (str) –
- Returns
is_email
- Return type
bool
Examples
>>> is_email('') False >>> is_email('info@martin-thoma.de') True >>> is_email('info@math.martin-thoma.de') True >>> is_email('Martin Thoma <info@martin-thoma.de>') False >>> is_email('info@martin-thoma') False >>> is_email('Martin <>') False
-
mpu.string.
is_float
(potential_float: str) → bool[source]¶ Check if potential_float is a valid float.
- Returns
is_float
- Return type
bool
Examples
>>> is_float('123') True >>> is_float('1234567890123456789') True >>> is_float('0') True >>> is_float('-123') True >>> is_float('123.45') True >>> is_float('a') False >>> is_float('0x8') False
-
mpu.string.
is_iban
(potential_iban: str) → bool[source]¶ Check if a string is a valid IBAN number.
IBAN is described in ISO 13616-1:2007 Part 1.
Spaces are ignored.
# CODE 0 = always zero b = BIC or National Bank code c = Account number i = holder’s kennitala (national identification number) k = IBAN check digits n = Branch number t = Account type x = National check digit or character
Examples
>>> is_iban('DE89 3704 0044 0532 0130 00') True >>> is_iban('DE89 3704 0044 0532 0130 01') False
-
mpu.string.
is_int
(potential_int: str) → bool[source]¶ Check if potential_int is a valid integer.
- Parameters
potential_int (str) –
- Returns
is_int
- Return type
bool
Examples
>>> is_int('123') True >>> is_int('1234567890123456789') True >>> is_int('0') True >>> is_int('-123') True >>> is_int('123.45') False >>> is_int('a') False >>> is_int('0x8') False
-
mpu.string.
is_ipv4
(potential_ipv4: str, allow_leading_zeros: bool = False, allow_shortened_addresses=False) → bool[source]¶ Check if a string is a valid IPv4 address.
- Parameters
potential_ipv4 (str) –
- Returns
is_valid
- Return type
bool
Examples
>>> is_ipv4("192.168.0.4") True >>> is_ipv4("192.168..4") False >>> is_ipv4("192.168.01.4", allow_leading_zeros=True) True >>> is_ipv4("192.168.01.4", allow_leading_zeros=False) False >>> is_ipv4("256.168.01.4") False >>> is_ipv4("4", allow_shortened_addresses=True) True >>> is_ipv4("4", allow_shortened_addresses=False) False
-
mpu.string.
is_none
(string_: str, default='raise') → bool[source]¶ Check if a string is equivalent to None.
- Parameters
string_ (str) –
default ({'raise', False}) – Default behaviour if none of the “None” strings is detected.
- Returns
is_none
- Return type
bool
Examples
>>> is_none('2', default=False) False >>> is_none('undefined', default=False) True
-
mpu.string.
str2bool
(string_: str, default='raise') → bool[source]¶ Convert a string to a bool.
- Parameters
string_ (str) –
default ({'raise', False}) – Default behaviour if none of the “true” strings is detected.
- Returns
boolean
- Return type
bool
Examples
>>> str2bool('True') True >>> str2bool('1') True >>> str2bool('0') False
-
mpu.string.
str2bool_or_none
(string_: str, default='raise') → Optional[bool][source]¶ Convert a string to a bool or to None.
- Parameters
string_ (str) –
default ({'raise', False}) – Default behaviour if none of the “true” or “none” strings is detected.
- Returns
bool_or_none
- Return type
bool or None
Examples
>>> str2bool_or_none('True') True >>> str2bool_or_none('1') True >>> str2bool_or_none('0') False >>> str2bool_or_none('undefined')
-
mpu.string.
str2float_or_none
(string_: str) → Optional[float][source]¶ Convert a string to a float or to None.
- Parameters
string_ (str) –
- Returns
float_or_none
- Return type
float or None
Examples
>>> str2float_or_none('1') 1.0 >>> str2float_or_none('1.2') 1.2 >>> str2float_or_none('undefined')
-
mpu.string.
str2int_or_none
(string_: str) → Optional[int][source]¶ Convert a string to a int or to None.
- Parameters
string_ (str) –
- Returns
int_or_none
- Return type
int or None
Examples
>>> str2int_or_none('2') 2 >>> str2int_or_none('undefined')
-
mpu.string.
str2str_or_none
(string_: str) → Optional[str][source]¶ Convert a string to a str or to None.
- Parameters
string_ (str) –
- Returns
str_or_none
- Return type
bool or None
Examples
>>> str2str_or_none('True') 'True' >>> str2str_or_none('1') '1' >>> str2str_or_none('0') '0' >>> str2str_or_none('undefined')