Module gen_util
General Utilities
- class ooodev.utils.gen_util.Util[source]
- static camel_to_snake(name)[source]
Converts CamelCase to snake_case
- Parameters:
name (str) – CamelCase string
- Returns:
snake_case string
- Return type:
str
Note
This method is preferred over the to_snake_case method when converting CamelCase strings. It does a better job of handling leading caps.
UICamelCasewill be converted toui_camel_caseand notu_i_camel_case.
- static generate_random_alpha_numeric(length=8)[source]
Generates a random alpha numeric string.
- Parameters:
length (int, optional) – Length of string to generate. Default
8- Returns:
Random string
- Return type:
str
- static generate_random_hex_string(length=8)[source]
Generates a random string.
- Parameters:
length (int, optional) – Length of string to generate. Default
8- Returns:
Random string
- Return type:
str
- static generate_random_string(length=8)[source]
Generates a random string.
- Parameters:
length (int, optional) – Length of string to generate. Default
8- Returns:
Random string
- Return type:
str
- static get_index(idx, count, allow_greater=False)[source]
Gets the index.
- Parameters:
idx (int) – Index of element. Can be a negative value to index from the end of the list.
count (int) – Number of elements.
allow_greater (bool, optional) – If True and index is greater then the count then the index becomes the next index if element is appended. Defaults to False. Only affect the
-1index.
- Raises:
ValueError – If
countis less than0.IndexError – If index is out or range.
- Returns:
Index value.
- Return type:
int
Note
-1is the last index in the sequence. Unlessallow_greaterisTruethen-1last index+ 1. Only the-1is treated differently whenallow_greaterisTrue.-2is the second to last index in the sequence.10items andidx=-2then index8is returned.-3is the third to last index in the sequence.10items andidx=-3then index7is returned.New in version 0.20.2.
- static get_obj_full_name(obj)[source]
Gets the full name of an object. The full name is the module and class name.
- Parameters:
obj (Any) – Object to get full name of.
- Returns:
Full name of object on success; Otherwise, empty string.
- Return type:
str
- classmethod is_iterable(arg, excluded_types=None)[source]
Gets if
argis iterable.- Parameters:
arg (object) – object to test
excluded_types (Iterable[type], optional) – Iterable of type to exclude. If
argmatches any type inexcluded_typesthenFalsewill be returned. Default(str,)
- Returns:
Trueifargis an iterable object and not of a type inexcluded_types; Otherwise,False.- Return type:
bool
Note
if
argis of type str then return result isFalse.Example
# non-string iterables assert is_iterable(arg=("f", "f")) # tuple assert is_iterable(arg=["f", "f"]) # list assert is_iterable(arg=iter("ff")) # iterator assert is_iterable(arg=range(44)) # generator assert is_iterable(arg=b"ff") # bytes (Python 2 calls this a string) # strings or non-iterables assert not is_iterable(arg=u"ff") # string assert not is_iterable(arg=44) # integer assert not is_iterable(arg=is_iterable) # function # excluded_types, optionally exclude types from enum import Enum, auto class Color(Enum): RED = auto() GREEN = auto() BLUE = auto() assert is_iterable(arg=Color) # Enum assert not is_iterable(arg=Color, excluded_types=(Enum, str)) # Enum
- static natural_key_sorter(text)[source]
Sort Key Sorts in human order.
# Example:
a_list.sort(key=Util.natural_key_sorter)
- Return type:
list- Parameters:
text (str) –
- static to_camel_case(s)[source]
Converts string to
CamelCase- Parameters:
s (str) – string to convert such as
snake_case_wordorpascalCaseWord- Returns:
string converted to
CamelCaseWord- Return type:
str
- classmethod to_pascal_case(s)[source]
Converts string to
pascalCase- Parameters:
s (str) – string to convert such as
snake_case_wordorCamelCaseWord- Returns:
string converted to
pascalCaseWord- Return type:
str
- static to_single_space(s, strip=True)[source]
Gets a string with multiple spaces converted to single spaces
- Parameters:
s (str) – String
strip (bool, optional) – If
Truethen whitespace is stripped from start and end or string. DefaultTrue.
- Returns:
String with extra spaces removed.
- Return type:
str
Example
>>> s = ' The quick brown fox' >>> print(Util.to_single_space(s)) 'The quick brown fox'
- ooodev.utils.gen_util.NULL_OBJ = <ooodev.utils.gen_util._null_obj object>
Null Object uses when None is not an option. Truthy value is
FalseChanged in version 0.52.5: NULL_OBJ now returns
Falsein boolean context.if NULL_OBJ: print("This will never be printed") if not NULL_OBJ: print("This will always be printed")
- ooodev.utils.gen_util.TNullObj = TypeVar(TNullObj, bound=_null_obj)
Type:
TypeVarInvariant
TypeVarbound toooodev.utils.gen_util._null_obj.