bases
Useful base classes.
Classes:
|
The basic structure of a class that can be converted into a dictionary. |
|
List-like type with fluent methods and some star players. |
|
A list with a name. |
|
Class which simulates a float. |
|
Typed version of |
Data:
Invariant |
|
Invariant |
|
Invariant |
Functions:
|
A factory function to return a custom list subclass with a name. |
Type Variables
-
_F= TypeVar(_F, bound=UserFloat) Type:
TypeVarInvariant
TypeVarbound todomdf_python_tools.bases.UserFloat.
-
_LU= TypeVar(_LU, bound=Lineup) Type:
TypeVarInvariant
TypeVarbound todomdf_python_tools.bases.Lineup.
-
_S= TypeVar(_S, bound=UserList) Type:
TypeVarInvariant
TypeVarbound todomdf_python_tools.bases.UserList.
Dictable
-
class
Dictable(*args, **kwargs)[source] Bases:
Iterable[Tuple[str,~_V]]The basic structure of a class that can be converted into a dictionary.
Attributes:
Methods:
__eq__(other)Return
self == other.__iter__()Iterate over the attributes of the class.
__repr__()Return a string representation of the
Dictable.__str__()Return
str(self).-
__class_getitem__= <bound method GenericAlias of <class 'domdf_python_tools.bases.Dictable'>> Type:
MethodType
-
UserList
-
class
UserList(initlist=None)[source] Bases:
MutableSequence[~_T]Typed version of
collections.UserList.Class that simulates a list. The instance’s contents are kept in a regular list, which is accessible via the
dataattribute ofUserListinstances. The instance’s contents are initially set to a copy of list, defaulting to the empty list[].New in version 0.10.0.
- Parameters
initlist (
Optional[Iterable[~_T]]) – The initial values to populate theUserListwith. Default[].
Subclassing requirements
Subclasses of
UserListare expected to offer a constructor which can be called with either no arguments or one argument. List operations which return a new sequence attempt to create an instance of the actual implementation class. To do so, it assumes that the constructor can be called with a single parameter, which is a sequence object used as a data source.If a derived class does not wish to comply with this requirement, all of the special methods supported by this class will need to be overridden; please consult the sources for information about the methods which need to be provided in that case.
Methods:
__add__(other)Return
self + value.__contains__(item)Return
key in self.__delitem__(i)Delete
self[key].__eq__(other)Return
self == other.__ge__(other)Return
self >= other.__getitem__(i)Return
self[key].__gt__(other)Return
self > other.__le__(other)Return
self <= other.__lt__(other)Return
self < other.__mul__(n)Return
self * value.__radd__(other)Return
value + self.__repr__()Return a string representation of the
UserList.__rmul__(n)Return
self * value.__setitem__(i, item)Set
self[key]tovalue.append(item)Append
itemto the end of theUserList.clear()Remove all items from the
UserList.copy()Returns a copy of the
UserList.count(item)Returns the number of occurrences of
itemin theUserList.extend(other)Extend the
NamedListby appending elements fromother.index(item, *args)Returns the index of the fist element matching
item.insert(i, item)Insert
itemat positioniin theUserList.pop([i])Removes and returns the item at index
i.remove(item)Removes the first occurrence of
itemfrom the list.reverse()Reverse the list in place.
sort(*[, key, reverse])Sort the list in ascending order and return
None.Attributes:
A real list object used to store the contents of the
UserList.-
__class_getitem__= <bound method GenericAlias of <class 'domdf_python_tools.bases.UserList'>> Type:
MethodType
-
__getitem__(i)[source] Return
self[key].- Return type
- Overloads
__getitem__(i:int) ->~_T__getitem__(i:slice) ->MutableSequence[~_T]
-
__setitem__(i, item)[source] Set
self[key]tovalue.- Overloads
__setitem__(i:int, o:~_T)__setitem__(i:slice, o:Iterable[~_T] )
-
index(item, *args)[source] Returns the index of the fist element matching
item.- Parameters
item (
~_T)args
- Raises
ValueError – if the item is not present.
- Return type
-
pop(i=- 1)[source] Removes and returns the item at index
i.- Raises
IndexError – if list is empty or index is out of range.
- Return type
-
remove(item)[source] Removes the first occurrence of
itemfrom the list.- Parameters
item (
~_T)- Raises
ValueError – if the item is not present.
-
sort(*, key=None, reverse=False)[source] Sort the list in ascending order and return
None.The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).
If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.
The reverse flag can be set to sort in descending order.
NamedList
Both NamedList and namedlist() can be used to create a named list.
namedlist() can be used as follows:
>>> ShoppingList = namedlist("ShoppingList")
>>> shopping_list = ShoppingList(["egg and bacon", "egg sausage and bacon", "egg and spam", "egg bacon and spam"])
>>>
If you wish to create a subclass with additional features it is recommended to subclass
from NamedList rather than from namedlist(). For example, do this:
>>> class ShoppingList(NamedList):
... pass
>>>
and not this:
>>> class ShoppingList(namedlist())
... pass
>>>
This avoids any potential issues with mypy.
-
class
NamedList(initlist=None)[source] -
A list with a name.
The name of the list is taken from the name of the subclass.
Changed in version 0.10.0:
NamedListnow subclassesUserListrather thancollections.UserList.
UserFloat
-
class
UserFloat(value=0.0)[source] Bases:
RealClass which simulates a float.
New in version 1.6.0.
- Parameters
value (
Union[SupportsFloat,SupportsIndex,str,bytes,bytearray]) – The values to initialise theUserFloatwith. Default0.0.
Methods:
__abs__()Return
abs(self).__add__(other)Return
self + value.__bool__()Return
self != 0.Return
complex(self).__divmod__(other)Return
divmod(self, value).__eq__(other)Return
self == other.Return
float(self).__floordiv__(other)Return
self // value.__ge__(other)Return
self >= other.__gt__(other)Return
self > other.__int__()Return
int(self).__le__(other)Return
self <= other.__lt__(other)Return
self < other.__mod__(other)Return
self % value.__mul__(other)Return
self * value.__ne__(other)Return
self != other.__neg__()Return
- self.__pos__()Return
+ self.__pow__(other[, mod])Return
pow(self, value, mod).__radd__(other)Return
value + self.__rdivmod__(other)Return
divmod(value, self).__repr__()Return a string representation of the
UserFloat.__rfloordiv__(other)Return
value // self.__rmod__(other)Return
value % self.__rmul__(other)Return
value * self.__round__([ndigits])Round the
UserFloattondigitsdecimal places, defaulting to0.__rpow__(other[, mod])Return
pow(value, self, mod).__rsub__(other)Return
value - self.__rtruediv__(other)Return
value / self.__str__()Return
str(self).__sub__(other)Return
value - self.__truediv__(other)Return
self / value.Truncates the float to an integer.
Returns the float as a fraction.
fromhex(string)Create a floating-point number from a hexadecimal string.
hex()Returns the hexadecimal (base 16) representation of the float.
Returns whether the float is an integer.
-
__complex__()[source] Return
complex(self).complex(self) == complex(float(self), 0)
- Return type
-
__float__()[source] Return
float(self).- Return type
-
__pow__(other, mod=None)[source] Return
pow(self, value, mod).- Return type
-
__round__(ndigits=None)[source] Round the
UserFloattondigitsdecimal places, defaulting to0.If
ndigitsis omitted orNone, returns anint, otherwise returns afloat. Rounds half toward even.
-
__rpow__(other, mod=None)[source] Return
pow(value, self, mod).- Return type
Lineup
-
class
Lineup(initlist=None)[source] -
List-like type with fluent methods and some star players.
Methods:
append(item)Append
itemto the end of theUserList.clear()Remove all items from the
UserList.extend(other)Extend the
NamedListby appending elements fromother.insert(i, item)Insert
itemat positioniin theUserList.remove(item)Removes the first occurrence of
itemfrom the list.replace(what, with_)Replace the first instance of
whatwithwith_.reverse()Reverse the list in place.
sort(*[, key, reverse])Sort the list in ascending order and return the self.
-
remove(item)[source] Removes the first occurrence of
itemfrom the list.- Parameters
item (
~_T)- Return type
- Raises
ValueError – if the item is not present.
-
sort(*, key=None, reverse=False)[source] Sort the list in ascending order and return the self.
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).
If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.
The reverse flag can be set to sort in descending order.
- Return type
-