Module gen_util

General Utilities

class ooodev.utils.gen_util.ArgsHelper[source]

Args Helper

namedtuple NameValue(name, value)[source]

Name Value pair

Fields:
  1.  name (str) – Name component

  2.  value (Any) – Value component

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 to ui_camel_case and not u_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 than 0.

  • IndexError – If index is out or range.

Returns:

Index value.

Return type:

int

Note

-1 is the last index in the sequence. Unless allow_greater is True then -1 last index + 1. Only the -1 is treated differently when allow_greater is True.

-2 is the second to last index in the sequence. 10 items and idx=-2 then index 8 is returned. -3 is the third to last index in the sequence. 10 items and idx=-3 then index 7 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 in excluded_types then False will be returned. Default (str,)

Returns:

True if arg is an iterable object and not of a type in excluded_types; Otherwise, False.

Return type:

bool

Note

if arg is of type str then return result is False.

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 or pascalCaseWord

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 or CamelCaseWord

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. Default True.

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'
static to_snake_case(s)[source]

Convert string to snake_case

Parameters:

s (str) – string to convert such as pascalCaseWord or CamelCaseWord

Returns:

string converted to snake_case_word

Return type:

str

classmethod to_snake_case_upper(s)[source]

Convert string to SNAKE_CASE

Parameters:

s (str) – string to convert such as snake_case_word or pascalCaseWord or CamelCaseWord

Returns:

string converted to SNAKE_CASE_WORD

Return type:

str

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 to object.