RestOC.StrHelper module¶
StrHelper Module
Several useful helper methods for use with strings
-
RestOC.StrHelper.
bytes_human
(num, suffix='B')¶ Bytes Human
Returns the size of bytes in the closest binary prefix so that they are clearly understood by humans
- Parameters
num (uint) – The bytes to convert to human readable
- Returns
str
-
RestOC.StrHelper.
decrypt
(key, val)¶ Decrypt
Decrypts a string using the key and returns it. Key must be in multiples of 16 bytes
- Parameters
key (str) – A key that was used to encrypt the original value
val (str) – The value to decrypt
- Returns
str
-
RestOC.StrHelper.
digits
(val)¶ Digits
Returns only the digits in the string
- Parameters
val (str) – The string to strip all non-digit characters from
- Returns
str
-
RestOC.StrHelper.
encrypt
(key, val)¶ Encrypt
Encrypts a string using the passed key and returns it. Key must be in multiples of 16 bytes
- Parameters
key (str) – The key used to encrypt the value
val (str) – The value to encrypt and return
- Returns
str
-
RestOC.StrHelper.
normalize
(val)¶ Normalize
Replaces all special alpha characters with their ascii equivalent
- Parameters
val (str) – The text to normalize
- Returns
str
-
RestOC.StrHelper.
random
(length=8, sets='_aZ', duplicates=True)¶ Random
Generates a random string. By default this function will generate an 8 character string using lowercase letters with possible repeating characters
- Parameters
length (int) – Requested length of the password
sets (str|str[]) – A list of names from the standard sets, a string starting with an underscore representing one named set, or any other string to be used as an array of characters to chose from. If you want certain characters to have a greater chance of appearing, use them more times, e.g. twice the ‘A’s, “AABC”, or three times the ‘B’s, “ABBBC”. Make sure not to turn off duplicates for this to be effective
duplicates (bool) – Defaults to True, allowing characters to be used more than once
- Sets:
0x: 0123456789abcdef 0: 01234567 10: 0123456789 az: abcdefghijklmnopqrstuvwxyz az*: abcdefghijkmnopqrstuvwxyz AZ: ABCDEFGHIJKLMNOPQRSTUVWXYZ AZ*: ABCDEFGHJKLMNPQRSTUVWXYZ aZ: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ aZ*: abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ !: !@#$%^&*-_+.? !*: !@$%^*-_.
Examples
> random(8, ‘_0x’) “baadbeef”
- Returns
str
-
RestOC.StrHelper.
strtr
(text, table)¶ String Translate
Port of PHP strtr (string translate)
- Parameters
text (str) – The string to translate
table (dict) – The translation table
- Returns
str