cornflakes.common package

Module contents

cornflakes common module.

default_ca_path()

Find default ca-certificate of the current system.

type_to_str(f)

Function to convert python object to string -> fix scientific notation for float / Decimal.

extract_var_names(obj)

Extract variables from class or function.

patch_module(module)

Method to overwrite module with all submodules in a generic way.

check_type(type_hint, value[, key, skip])

Check if the provided value matches the specified type hint.

get_actual_type(type_hint)

Returns the actual type of the type hint.

recursive_update(d, u[, merge_lists])

Method to recursively update a dictionary.

datetime_ms(year[, month, day, hour, ...])

Create Instance of cornflakes.common.DatetimeMS().

unquoted_string(x)

Create Instance of cornflakes.common.UnquotedString().

get_method_definition(func)

This function returns the string representation of a given function.

get_method_type_hint(method)

This function returns the type hint of a given function as a string in the format Callable[[args], return].

has_return_statement(func)

Checks if a given function has a return statement that returns a value other than None.

cornflakes.common.check_type(type_hint, value, key='', skip=False)

Check if the provided value matches the specified type hint.

Parameter:
  • type_hint (Any) – The type hint or annotation to check against.

  • key – The attribute or variable name. (only required for error messages)

  • value – The value to be checked.

  • skip – If True, skip the type check and return None. Defaults to False.

Rückgabe:

The value if it matches the type hint.

Verursacht:
  • TypeError – If the value does not match the expected type.

  • ValueError – If an error occurs during type conversion or when the value does not match the expected type.

This function handles various scenarios, including handling of special types, class types, dataclasses, unions, lists, tuples, built-in types, and custom types.

Example: ` # Checking if a value matches the type hint result = check_type(int, 'my_var', 10) # `result` will be 10, as it matches the `int` type hint. `

cornflakes.common.datetime_ms(year, month=None, day=None, hour=0, minute=0, second=0, millisecond=0, tzinfo=None)

Create Instance of cornflakes.common.DatetimeMS().

Parameter:
  • month (int | None) –

  • day (int | None) –

  • hour (int | None) –

  • minute (int | None) –

  • second (int | None) –

  • millisecond (int | None) –

  • tzinfo (tzinfo | None) –

Rückgabetyp:

DatetimeMS

cornflakes.common.default_ca_path()

Find default ca-certificate of the current system.

Rückgabe:

system default ca-certificate

Rückgabetyp:

str

cornflakes.common.extract_var_names(obj)

Extract variables from class or function.

Parameter:

obj (Callable[[...], Any]) –

Rückgabetyp:

dict

cornflakes.common.get_actual_type(type_hint)

Returns the actual type of the type hint.

cornflakes.common.get_method_definition(func)

This function returns the string representation of a given function. It supports both lambda functions and functions defined using def. It also supports functools.partial objects.

Parameter:

func (callable) – A function or a functools.partial object.

Rückgabe:

A string representation of the function or functools.partial object.

Rückgabetyp:

str

Examples: >>> def original_function(x, y, z=3): … return x + y + z … >>> p = partial(original_function, 1, z=5) >>> get_method_definition(p) # doctest: +NORMALIZE_WHITESPACE ‚partial(original_function, 1, z=5)‘

# >>> l = lambda x, y, z: print(x, y, z) # >>> get_method_definition(l) # ‚lambda x, y, z: print(x, y, z)‘ # # >>> method = partial(lambda x, y, z: print(x, y, z), x=1) # >>> get_method_definition(method) # ‚partial(lambda x, y, z: print(x, y, z), x=1)‘

cornflakes.common.get_method_type_hint(method)

This function returns the type hint of a given function as a string in the format Callable[[args], return]. It supports functions with positional arguments, keyword arguments, variable positional arguments (*args), and variable keyword arguments (**kwargs). The type hints for *args and **kwargs are represented as .

Parameter:

method (callable) – A function.

Rückgabe:

The type hint of the function as a string.

Rückgabetyp:

str

Examples: >>> def func1(x: int, y: str) -> bool: … return y in str(x) … >>> get_method_type_hint(func1) ‚Callable[[int, str], bool]‘

>>> def func2(x, y, *args, **kwargs):
...     return str(x) + y + str(args) + str(kwargs)
...
>>> get_method_type_hint(func2)
'Callable[..., Any]'
>>> def func3(x, y):
...     return x + y
...
>>> get_method_type_hint(func3)
'Callable[[Any, Any], Any]'
cornflakes.common.has_return_statement(func)

Checks if a given function has a return statement that returns a value other than None.

Parameter:

func (Callable) – The function to be checked

Rückgabe:

True if the function has a return statement that returns a value other than None, False otherwise.

Rückgabetyp:

bool

Xdoctest:

>>> def no_return(): pass
>>> has_return_statement(no_return)
False
>>> def return_none():
...     return None
>>> has_return_statement(return_none)
False
>>> def return_something():
...     return 42
>>> has_return_statement(return_something)
True
>>> def return_conditionally(x):
...     if x > 0:
...         return x
>>> has_return_statement(return_conditionally)
True
>>> def multiple_return(x):
...     if x > 0:
...         return None
...     return x
>>> has_return_statement(multiple_return)
True
>>> def multiple_none_return(x):
...     if x > 0:
...         return None
...     return None
>>> has_return_statement(multiple_none_return)
False
cornflakes.common.patch_module(module)

Method to overwrite module with all submodules in a generic way.

  1. Overwrite names from submodules declared in __all__ to parent module.

  2. Overwrite doc_string and adds auto summary with objects defined in __all__.

Parameter:

module (str) –

cornflakes.common.recursive_update(d, u, merge_lists=False)

Method to recursively update a dictionary.

cornflakes.common.type_to_str(f)

Function to convert python object to string -> fix scientific notation for float / Decimal.

cornflakes.common.unquoted_string(x)

Create Instance of cornflakes.common.UnquotedString().

Parameter:

x (str) –