encommon.types package#
Subpackages#
- encommon.types.test package
Submodules#
encommon.types.classes module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- pydantic model encommon.types.classes.BaseModel[source]#
Bases:
BaseModel
Pydantic base model but with added methods and routines.
- Parameters:
data – Keyword arguments passed to Pydantic model.
Show JSON schema
{ "title": "BaseModel", "description": "Pydantic base model but with added methods and routines.\n\n:param data: Keyword arguments passed to Pydantic model.", "type": "object", "properties": {}, "additionalProperties": false }
- Config:
extra: str = forbid
encommon.types.dicts module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- encommon.types.dicts.merge_dicts(dict1: dict[Any, Any], dict2: dict[Any, Any], force: bool | None = False, *, merge_list: bool = True, merge_dict: bool = True, paranoid: bool = False) dict[Any, Any] [source]#
Recursively merge the contents of provided dictionaries.
Warning
This function will update the
dict1
reference.Example#
>>> dict1 = {'a': 'b', 'c': [1]} >>> dict2 = {'a': 'B', 'c': [2]} >>> merge_dicts(dict1, dict2) {'a': 'b', 'c': [1, 2]} >>> dict1 {'a': 'b', 'c': [1, 2]}
- param dict1:
Primary dictionary which is merged into.
- param dict2:
Secondary dictionary for primary updates.
- param force:
Force overwriting concrete values in the primary dictionary with those from secondary. When
None
only overwrites if destination isNone
.- param merge_list:
Determines if merged or overwritten.
- param merge_dict:
Determines if merged or overwritten.
- param paranoid:
Perform an initial deepcopy on both of the provided dictionaries before performing merges.
- returns:
Provided dictionary with the other merged in.
- encommon.types.dicts.sort_dict(value: dict[Any, Any], reverse: bool = False) dict[Any, Any] [source]#
Sort the keys within the dictionary and return new one.
Example#
>>> foo = {'b': 'be', 'a': 'ey'} >>> sort_dict(foo) {'a': 'ey', 'b': 'be'} >>> sort_dict(foo, True) {'b': 'be', 'a': 'ey'}
- param value:
Dictionary whose keys are sorted into new.
- param reverse:
Optionally reverse the sort direction.
- returns:
New dictionary with keys sorted alphabetical.
encommon.types.empty module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
encommon.types.funcs module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
encommon.types.lists module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- encommon.types.lists.dedup_list(value: list[Any], update: bool = True) list[Any] [source]#
Return the provided list values with duplicates removed.
Warning
This function will update the
value
reference.Example#
>>> value = [1, 2, 2, 3] >>> dedup_list(value) [1, 2, 3] >>> value [1, 2, 3]
- param value:
List of values processed for duplication.
- param update:
Indicate if to update provided reference.
- returns:
Provided list values with duplicates removed.
- encommon.types.lists.fuzzy_list(values: str | list[str], patterns: str | list[str]) list[str] [source]#
Return the provided values that match provided patterns.
Example#
>>> values = ['foo', 'bar', 'baz', 'bop'] >>> patterns = ['*o', 'ba*'] >>> fuzzy_list(values, patterns) ['foo', 'bar', 'baz']
- param values:
Value or values to enumerate for matching.
- param patterns:
Patterns which values can match any one.
- returns:
Provided values that match provided patterns.
- encommon.types.lists.inlist(needle: Any, haystack: Sequence[Any]) bool [source]#
Return the boolean indicating whether needle in haystack.
Example#
>>> haystack = [1, 2, 3] >>> inlist(2, haystack) True
- param needle:
Provided item that may be within haystack.
- param haystack:
List of items which may contain needle.
- returns:
Boolean indicating whether needle in haystack.
encommon.types.notate module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- encommon.types.notate.delate(source: dict[str, Any] | list[Any], path: str, delim: str = '/') None [source]#
Delete the value within the dictionary using notation.
Example#
>>> source = {'foo': {'bar': 'baz'}} >>> delate(source, 'foo/bar') >>> source {'foo': {}}
- param source:
Dictionary object processed in notation.
- param path:
Path to the value within the source object.
- param delim:
Override default delimiter between parts.
- encommon.types.notate.expate(source: dict[str, Any], delim: str = '/') dict[str, Any] [source]#
Explode the dictionary from a single depth of notation.
Example#
>>> expate({'foo/bar': 'baz'}) {'foo': {'bar': 'baz'}}
- param source:
Dictionary object processed in notation.
- param delim:
Override default delimiter between parts.
- returns:
New dictionary that was recursively exploded.
- encommon.types.notate.getate(source: tuple[Any, ...] | dict[str, Any] | list[Any], path: str, default: Any | None = None, delim: str = '/') Any [source]#
Collect the value within the dictionary using notation.
Example#
>>> source = {'foo': {'bar': 'baz'}} >>> getate(source, 'foo/bar') 'baz'
Example#
>>> source = {'foo': ['bar', 'baz']} >>> getate(source, 'foo/1') 'baz'
- param source:
Dictionary object processed in notation.
- param path:
Path to the value within the source object.
- param default:
Value to use if none is found in source.
- param delim:
Override default delimiter between parts.
- returns:
Value that was located within provided source.
- encommon.types.notate.impate(source: dict[str, Any] | list[dict[str, Any]], delim: str = '/', parent: str | None = None, *, implode_list: bool = True, recurse_list: bool = True) dict[str, Any] | list[dict[str, Any]] [source]#
Implode the dictionary into a single depth of notation.
Example#
>>> impate({'foo': {'bar': 'baz'}}) {'foo/bar': 'baz'}
- param source:
Dictionary object processed in notation.
- param delim:
Override default delimiter between parts.
- param parent:
Parent key prefix for downstream update.
- param implode_list:
Determine whether list is imploded.
- param recurse_list:
Determine whether flatten in list.
- returns:
New dictionary that was recursively imploded. It is also possible that a list of dictionary will be returned when provided and implode_list is False.
- encommon.types.notate.setate(source: dict[str, Any] | list[Any], path: str, value: Any, delim: str = '/') None [source]#
Define the value within the dictionary using notation.
Example#
>>> source = {'foo': {'bar': 'baz'}} >>> source['foo']['bar'] 'baz' >>> setate(source, 'foo/bar', 'bop') >>> source['foo']['bar'] 'bop'
- param source:
Dictionary object processed in notation.
- param path:
Path to the value within the source object.
- param value:
Value which will be defined at noted point.
- param delim:
Override default delimiter between parts.
encommon.types.strings module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- encommon.types.strings.hasstr(haystack: str, needle: str) bool [source]#
Return the boolean indicating if needle is in haystack.
Example#
>>> haystack = 'barfoobaz' >>> needle = 'foo' >>> hasstr(haystack, needle) True
- param haystack:
Full string which may contain needle.
- param needle:
Substring which may be within haystack.
- returns:
Boolean indicating if needle is in haystack.
- encommon.types.strings.inrepr(needle: str, haystack: Any) bool [source]#
Return the boolean indicating if needle is in haystack.
Example#
>>> class MyClass: pass >>> haystack = MyClass() >>> needle = 'MyClass' >>> inrepr(needle, haystack) True
- param needle:
Substring which may be within haystack.
- param haystack:
Object which is inspected for needle.
- returns:
Boolean indicating if needle is in haystack.
- encommon.types.strings.instr(needle: str, haystack: Any) bool [source]#
Return the boolean indicating if needle is in haystack.
Example#
>>> class MyClass: pass >>> haystack = MyClass() >>> needle = 'MyClass' >>> instr(needle, haystack) True
- param needle:
Substring which may be within haystack.
- param haystack:
Object which is inspected for needle.
- returns:
Boolean indicating if needle is in haystack.
- encommon.types.strings.rplstr(source: str, match: str, value: Any) str [source]#
Return the source string with the match value replaced.
Example#
>>> rplstr('foo', 'foo', 'bar') 'bar'
- param source:
String that to be processed and returned.
- param match:
What will be replaced within the string.
- param value:
Replace value for the string is matched.
- returns:
Source string with the match value replaced.
encommon.types.types module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
Module contents#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- pydantic model encommon.types.BaseModel[source]#
Bases:
BaseModel
Pydantic base model but with added methods and routines.
- Parameters:
data – Keyword arguments passed to Pydantic model.
Show JSON schema
{ "title": "BaseModel", "description": "Pydantic base model but with added methods and routines.\n\n:param data: Keyword arguments passed to Pydantic model.", "type": "object", "properties": {}, "additionalProperties": false }
- Config:
extra: str = forbid
- encommon.types.clsname(cls: object) str [source]#
Return the actual definition name for the Python class.
- Parameters:
cls – Provided Python class related to operation.
- Returns:
Actual definition name for the Python class.
- encommon.types.dedup_list(value: list[Any], update: bool = True) list[Any] [source]#
Return the provided list values with duplicates removed.
Warning
This function will update the
value
reference.Example#
>>> value = [1, 2, 2, 3] >>> dedup_list(value) [1, 2, 3] >>> value [1, 2, 3]
- param value:
List of values processed for duplication.
- param update:
Indicate if to update provided reference.
- returns:
Provided list values with duplicates removed.
- encommon.types.delate(source: dict[str, Any] | list[Any], path: str, delim: str = '/') None [source]#
Delete the value within the dictionary using notation.
Example#
>>> source = {'foo': {'bar': 'baz'}} >>> delate(source, 'foo/bar') >>> source {'foo': {}}
- param source:
Dictionary object processed in notation.
- param path:
Path to the value within the source object.
- param delim:
Override default delimiter between parts.
- encommon.types.expate(source: dict[str, Any], delim: str = '/') dict[str, Any] [source]#
Explode the dictionary from a single depth of notation.
Example#
>>> expate({'foo/bar': 'baz'}) {'foo': {'bar': 'baz'}}
- param source:
Dictionary object processed in notation.
- param delim:
Override default delimiter between parts.
- returns:
New dictionary that was recursively exploded.
- encommon.types.funcname() str [source]#
Return the current function name where code is running.
- Returns:
Current function name where code is running.
- encommon.types.fuzzy_list(values: str | list[str], patterns: str | list[str]) list[str] [source]#
Return the provided values that match provided patterns.
Example#
>>> values = ['foo', 'bar', 'baz', 'bop'] >>> patterns = ['*o', 'ba*'] >>> fuzzy_list(values, patterns) ['foo', 'bar', 'baz']
- param values:
Value or values to enumerate for matching.
- param patterns:
Patterns which values can match any one.
- returns:
Provided values that match provided patterns.
- encommon.types.getate(source: tuple[Any, ...] | dict[str, Any] | list[Any], path: str, default: Any | None = None, delim: str = '/') Any [source]#
Collect the value within the dictionary using notation.
Example#
>>> source = {'foo': {'bar': 'baz'}} >>> getate(source, 'foo/bar') 'baz'
Example#
>>> source = {'foo': ['bar', 'baz']} >>> getate(source, 'foo/1') 'baz'
- param source:
Dictionary object processed in notation.
- param path:
Path to the value within the source object.
- param default:
Value to use if none is found in source.
- param delim:
Override default delimiter between parts.
- returns:
Value that was located within provided source.
- encommon.types.hasstr(haystack: str, needle: str) bool [source]#
Return the boolean indicating if needle is in haystack.
Example#
>>> haystack = 'barfoobaz' >>> needle = 'foo' >>> hasstr(haystack, needle) True
- param haystack:
Full string which may contain needle.
- param needle:
Substring which may be within haystack.
- returns:
Boolean indicating if needle is in haystack.
- encommon.types.impate(source: dict[str, Any] | list[dict[str, Any]], delim: str = '/', parent: str | None = None, *, implode_list: bool = True, recurse_list: bool = True) dict[str, Any] | list[dict[str, Any]] [source]#
Implode the dictionary into a single depth of notation.
Example#
>>> impate({'foo': {'bar': 'baz'}}) {'foo/bar': 'baz'}
- param source:
Dictionary object processed in notation.
- param delim:
Override default delimiter between parts.
- param parent:
Parent key prefix for downstream update.
- param implode_list:
Determine whether list is imploded.
- param recurse_list:
Determine whether flatten in list.
- returns:
New dictionary that was recursively imploded. It is also possible that a list of dictionary will be returned when provided and implode_list is False.
- encommon.types.inlist(needle: Any, haystack: Sequence[Any]) bool [source]#
Return the boolean indicating whether needle in haystack.
Example#
>>> haystack = [1, 2, 3] >>> inlist(2, haystack) True
- param needle:
Provided item that may be within haystack.
- param haystack:
List of items which may contain needle.
- returns:
Boolean indicating whether needle in haystack.
- encommon.types.inrepr(needle: str, haystack: Any) bool [source]#
Return the boolean indicating if needle is in haystack.
Example#
>>> class MyClass: pass >>> haystack = MyClass() >>> needle = 'MyClass' >>> inrepr(needle, haystack) True
- param needle:
Substring which may be within haystack.
- param haystack:
Object which is inspected for needle.
- returns:
Boolean indicating if needle is in haystack.
- encommon.types.instr(needle: str, haystack: Any) bool [source]#
Return the boolean indicating if needle is in haystack.
Example#
>>> class MyClass: pass >>> haystack = MyClass() >>> needle = 'MyClass' >>> instr(needle, haystack) True
- param needle:
Substring which may be within haystack.
- param haystack:
Object which is inspected for needle.
- returns:
Boolean indicating if needle is in haystack.
- encommon.types.lattrs(cls: object) list[str] [source]#
Return the list of attributes which are found in class.
- Parameters:
cls – Provided Python class related to operation.
- Returns:
List of attributes which are found in class.
- encommon.types.merge_dicts(dict1: dict[Any, Any], dict2: dict[Any, Any], force: bool | None = False, *, merge_list: bool = True, merge_dict: bool = True, paranoid: bool = False) dict[Any, Any] [source]#
Recursively merge the contents of provided dictionaries.
Warning
This function will update the
dict1
reference.Example#
>>> dict1 = {'a': 'b', 'c': [1]} >>> dict2 = {'a': 'B', 'c': [2]} >>> merge_dicts(dict1, dict2) {'a': 'b', 'c': [1, 2]} >>> dict1 {'a': 'b', 'c': [1, 2]}
- param dict1:
Primary dictionary which is merged into.
- param dict2:
Secondary dictionary for primary updates.
- param force:
Force overwriting concrete values in the primary dictionary with those from secondary. When
None
only overwrites if destination isNone
.- param merge_list:
Determines if merged or overwritten.
- param merge_dict:
Determines if merged or overwritten.
- param paranoid:
Perform an initial deepcopy on both of the provided dictionaries before performing merges.
- returns:
Provided dictionary with the other merged in.
- encommon.types.rplstr(source: str, match: str, value: Any) str [source]#
Return the source string with the match value replaced.
Example#
>>> rplstr('foo', 'foo', 'bar') 'bar'
- param source:
String that to be processed and returned.
- param match:
What will be replaced within the string.
- param value:
Replace value for the string is matched.
- returns:
Source string with the match value replaced.
- encommon.types.setate(source: dict[str, Any] | list[Any], path: str, value: Any, delim: str = '/') None [source]#
Define the value within the dictionary using notation.
Example#
>>> source = {'foo': {'bar': 'baz'}} >>> source['foo']['bar'] 'baz' >>> setate(source, 'foo/bar', 'bop') >>> source['foo']['bar'] 'bop'
- param source:
Dictionary object processed in notation.
- param path:
Path to the value within the source object.
- param value:
Value which will be defined at noted point.
- param delim:
Override default delimiter between parts.
- encommon.types.sort_dict(value: dict[Any, Any], reverse: bool = False) dict[Any, Any] [source]#
Sort the keys within the dictionary and return new one.
Example#
>>> foo = {'b': 'be', 'a': 'ey'} >>> sort_dict(foo) {'a': 'ey', 'b': 'be'} >>> sort_dict(foo, True) {'b': 'be', 'a': 'ey'}
- param value:
Dictionary whose keys are sorted into new.
- param reverse:
Optionally reverse the sort direction.
- returns:
New dictionary with keys sorted alphabetical.