pyTooling.Common

Common types, helper functions and classes.

Hint

See high-level help for explanations and usage examples.

Variables

Functions

  • isnestedclass(): Returns true, if the given class cls is a member on an outer class scope.

  • getsizeof(): Recursively calculate the “true” size of an object including complex members like __dict__.

  • firstElement(): Returns the first element from an indexable.

  • lastElement(): Returns the last element from an indexable.

  • firstItem(): Returns the first item from an iterable.

  • lastItem(): Returns the last item from an iterable.

  • firstKey(): Retrieves the first key from a dictionary’s keys.

  • firstValue(): Retrieves the first value from a dictionary’s values.

  • firstPair(): Retrieves the first key-value-pair from a dictionary.

  • mergedicts(): Merge multiple dictionaries into a single new dictionary.

  • zipdicts(): Iterate multiple dictionaries simultaneously.


Variables

pyTooling.Common.CurrentPlatform

Gathered information for the current platform.

Platforms.Linux

Functions

pyTooling.Common.isnestedclass(cls, scope)[source]

Returns true, if the given class cls is a member on an outer class scope.

Parameters:
  • cls (Type) – Class to check, if it’s a nested class.

  • scope (Type) – Outer class which is the outer scope of cls.

Return type:

bool

Returns:

True, if cls is a nested class within scope.

pyTooling.Common.getsizeof(obj)[source]

Recursively calculate the “true” size of an object including complex members like __dict__.

Parameters:

obj (Any) – Object to calculate the size of.

Return type:

int

Returns:

True size of an object in bytes.

Background Information

The function sys.getsizeof() only returns the raw size of a Python object and doesn’t account for the overhead of e.g. _dict__ to store dynamically allocated object members.

See also

The code is based on code snippets and ideas from:

pyTooling.Common.firstElement(indexable)[source]

Returns the first element from an indexable.

Parameters:

indexable (Union[List[TypeVar(Element)], Tuple[TypeVar(Element), ...]]) – Indexable to get the first element from.

Return type:

TypeVar(Element)

Returns:

First element.

pyTooling.Common.lastElement(indexable)[source]

Returns the last element from an indexable.

Parameters:

indexable (Union[List[TypeVar(Element)], Tuple[TypeVar(Element), ...]]) – Indexable to get the last element from.

Return type:

TypeVar(Element)

Returns:

Last element.

pyTooling.Common.firstItem(iterable)[source]

Returns the first item from an iterable.

Parameters:

iterable (Iterable[TypeVar(Element)]) – Iterable to get the first item from.

Return type:

TypeVar(Element)

Returns:

First item.

pyTooling.Common.lastItem(iterable)[source]

Returns the last item from an iterable.

Parameters:

iterable (Iterable[TypeVar(Element)]) – Iterable to get the last item from.

Return type:

TypeVar(Element)

Returns:

Last item.

pyTooling.Common.firstKey(d)[source]

Retrieves the first key from a dictionary’s keys.

Parameters:

d (Dict[TypeVar(_DictKey1), TypeVar(_DictValue1)]) – Dictionary to get the first key from.

Return type:

TypeVar(_DictKey1)

Returns:

The first key.

pyTooling.Common.firstValue(d)[source]

Retrieves the first value from a dictionary’s values.

Parameters:

d (Dict[TypeVar(_DictKey1), TypeVar(_DictValue1)]) – Dictionary to get the first value from.

Return type:

TypeVar(_DictValue1)

Returns:

The first value.

pyTooling.Common.firstPair(d)[source]

Retrieves the first key-value-pair from a dictionary.

Parameters:

d (Dict[TypeVar(_DictKey1), TypeVar(_DictValue1)]) – Dictionary to get the first key-value-pair from.

Return type:

Tuple[TypeVar(_DictKey1), TypeVar(_DictValue1)]

Returns:

The first key-value-pair as tuple.

pyTooling.Common.mergedicts(*dicts, filter=None)[source]

Merge multiple dictionaries into a single new dictionary.

If parameter filter isn’t None, then this function is applied to every element during the merge operation. If it returns true, the dictionary element will be present in the resulting dictionary.

Parameters:
  • dicts (Tuple[Dict, ...]) – Tuple of dictionaries to merge as positional parameters.

  • filter (Optional[Callable[[Hashable, Any], bool]]) – Optional filter function to apply to each dictionary element when merging.

Return type:

Dict

Returns:

A new dictionary containing the merge result.

pyTooling.Common.zipdicts(*dicts)[source]

Iterate multiple dictionaries simultaneously.

Parameters:

dicts (Tuple[Dict, ...]) – Tuple of dictionaries to iterate as positional parameters.

Return type:

Generator[Tuple, None, None]

Returns:

A generator returning a tuple containing the key and values of each dictionary in the order of given dictionaries.

See also

The code is based on code snippets and ideas from: