1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
.. _COMMON:
.. _COMMON/HelperFunctions:
Common Helper Functions
#######################
The :mod:`pyTooling.Common` package contains several useful helper functions, which are explained in the following
sections.
.. #contents:: Table of Contents
:local:
:depth: 1
.. _COMMON/Helper/getsizeof:
getsizeof
*********
The :func:`~pyTooling.Common.getsizeof` functions returns the "true" size of a Python object including auxiliary data
structures.
.. rubric:: Example:
.. code-block:: Python
class A:
_data : int
def __init__(self) -> None:
_data = 5
from pyTooling.Common import getsizeof
a = A()
print(getsizeof(a))
.. rubric:: Details
In addition to :func:`sys.getsizeof`, the used algorithm accounts also for:
* ``__dict__``
* ``__slots__``
* iterable members made of:
* :class:`tuple`
* :class:`list`
* :class:`typing.Set`
* :class:`collection.deque`
.. admonition:: Background Information
The function :func:`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.
.. _COMMON/Helper/isnestedclass:
isnestedclass
*************
The :func:`~pyTooling.Common.isnestedclass` functions returns true, if a class is a nested class inside another
class.
.. rubric:: Example:
.. code-block:: Python
class A:
class N:
_data : int
def __init__(self) -> None:
_data = 5
N = A.N
print(isnestedclass(N, A))
.. _COMMON/Helper/firstElement:
firstElement
************
:func:`~pyTooling.Common.firstElement` returns the first element from an iterable.
.. code-block:: Python
lst = [1, 2, 3]
f = firstElement(lst)
# 1
.. _COMMON/Helper/lastElement:
lastElement
***********
:func:`~pyTooling.Common.lastElement` returns the last element from an iterable.
.. code-block:: Python
lst = [1, 2, 3]
l = lastElement(lst)
# 3
.. _COMMON/Helper/firstItem:
firstItem
*********
:func:`~pyTooling.Common.firstItem` returns the first item from an iterable.
.. code-block:: Python
lst = [1, 2, 3]
f = firstItem(lst)
# 1
.. _COMMON/Helper/lastItem:
lastItem
********
:func:`~pyTooling.Common.lastItem` returns the last item from an iterable.
.. code-block:: Python
lst = [1, 2, 3]
l = lastItem(lst)
# 3
.. _COMMON/Helper/firstKey:
firstKey
********
:func:`~pyTooling.Common.firstKey` returns the first key from a dictionary.
.. code-block:: Python
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.
.. _COMMON/Helper/firstValue:
firstValue
**********
:func:`~pyTooling.Common.firstValue` returns the first value from a dictionary.
.. code-block:: Python
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.
.. _COMMON/Helper/firstPair:
firstPair
*********
:func:`~pyTooling.Common.firstPair` returns the first pair (key-value-pair tuple) from a dictionary.
.. code-block:: Python
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.
.. _COMMON/Helper/mergedicts:
mergedicts
**********
:func:`~pyTooling.Common.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.
.. rubric:: Example:
.. code-block:: Python
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"}
.. _COMMON/Helper/zipdicts:
zipdicts
********
:func:`~pyTooling.Common.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.
.. rubric:: Example:
.. code-block:: Python
dictA = {11: "11", 12: "12", 13: "13"}
dictB = {11: "21", 12: "22", 13: "23"}
for key, valueA, valueB in zipdicts(dictA, dictB):
pass
|