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.
UICamelCase
will be converted toui_camel_case
and 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
-1
index.
- Raises:
ValueError – If
count
is less than0
.IndexError – If index is out or range.
- Returns:
Index value.
- Return type:
int
Note
-1
is the last index in the sequence. Unlessallow_greater
isTrue
then-1
last index+ 1
. Only the-1
is treated differently whenallow_greater
isTrue
.-2
is the second to last index in the sequence.10
items andidx=-2
then index8
is returned.-3
is the third to last index in the sequence.10
items andidx=-3
then index7
is 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
arg
is iterable.- Parameters:
arg (object) – object to test
excluded_types (Iterable[type], optional) – Iterable of type to exclude. If
arg
matches any type inexcluded_types
thenFalse
will be returned. Default(str,)
- Returns:
True
ifarg
is an iterable object and not of a type inexcluded_types
; Otherwise,False
.- Return type:
bool
Note
if
arg
is 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_word
orpascalCaseWord
- 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_word
orCamelCaseWord
- 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
True
then 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 = <object object>
Null Object uses with None is not an option
- ooodev.utils.gen_util.TNullObj = TypeVar(TNullObj, bound=object)
Type:
TypeVar
Invariant
TypeVar
bound toobject
.