Common Helper Functions
The pyTooling.Common
package contains several useful helper functions, which are explained in the following
sections.
getsizeof
The getsizeof()
functions returns the “true” size of a Python object including auxiliary data
structures.
Example:
class A:
_data : int
def __init__(self) -> None:
_data = 5
from pyTooling.Common import getsizeof
a = A()
print(getsizeof(a))
Details
In addition to sys.getsizeof()
, the used algorithm accounts also for:
__dict__
__slots__
iterable members made of:
collection.deque
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.
isnestedclass
The isnestedclass()
functions returns true, if a class is a nested class inside another
class.
Example:
class A:
class N:
_data : int
def __init__(self) -> None:
_data = 5
N = A.N
print(isnestedclass(N, A))
firstElement
firstElement()
returns the first element from an iterable.
lst = [1, 2, 3]
f = firstElement(lst)
# 1
lastElement
lastElement()
returns the last element from an iterable.
lst = [1, 2, 3]
l = lastElement(lst)
# 3
firstItem
firstItem()
returns the first item from an iterable.
lst = [1, 2, 3]
f = firstItem(lst)
# 1
lastItem
lastItem()
returns the last item from an iterable.
lst = [1, 2, 3]
l = lastItem(lst)
# 3
firstKey
firstKey()
returns the first key from a dictionary.
d = {}
d["a"] = 1
d["b"] = 2
k = firstKey(d)
# "a"
Hint
The dictionary should be an order preserving dictionary, otherwise the “first” item is not defined and can return any key.
firstValue
firstValue()
returns the first value from a dictionary.
d = {}
d["a"] = 1
d["b"] = 2
k = firstValue(d)
# 1
Hint
The dictionary should be an order preserving dictionary, otherwise the “first” item is not defined and can return any value.
firstPair
firstPair()
returns the first pair (key-value-pair tuple) from a dictionary.
d = {}
d["a"] = 1
d["b"] = 2
k = firstPair(d)
# ("a", 1)
Hint
The dictionary should be an order preserving dictionary, otherwise the “first” item is not defined and can return any pair.
mergedicts
mergedicts()
merges multiple dictionaries into a new single dictionary. It accepts an
arbitrary number of dictionaries to merge. Optionally, the named parameter func
accepts a function that can be
applied to every element during the merge operation.
Example:
dictA = {11: "11", 12: "12", 13: "13"}
dictB = {21: "21", 22: "22", 23: "23"}
mergedDict = mergedicts(dictA, dictB)
# {11: "11", 12: "12", 13: "13", 21: "21", 22: "22", 23: "23"}
zipdicts
zipdicts()
is a generator that iterates multiple dictionaries simultaneously. It expects
multiple dictionary objects (fulfilling the mapping protocol) as positional parameters.
An exception is raised, if not all dictionary objects have the same number of items. Also an exception is raised, if a key doesn’t exist in all dictionaries.
Example:
dictA = {11: "11", 12: "12", 13: "13"}
dictB = {11: "21", 12: "22", 13: "23"}
for key, valueA, valueB in zipdicts(dictA, dictB):
pass