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:
TypeVar
Invariant
TypeVar
bound todomdf_python_tools.bases.UserFloat
.
-
_LU
= TypeVar(_LU, bound=Lineup)¶ Type:
TypeVar
Invariant
TypeVar
bound todomdf_python_tools.bases.Lineup
.
-
_S
= TypeVar(_S, bound=UserList)¶ Type:
TypeVar
Invariant
TypeVar
bound 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.
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)
.
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
data
attribute ofUserList
instances. 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 theUserList
with. Default[]
.
Subclassing requirements
Subclasses of
UserList
are 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
item
to 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
item
in theUserList
.extend
(other)Extend the
NamedList
by appending elements fromother
.index
(item, *args)Returns the index of the fist element matching
item
.insert
(i, item)Insert
item
at positioni
in theUserList
.pop
([i])Removes and returns the item at index
i
.remove
(item)Removes the first occurrence of
item
from 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
.-
__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
item
from 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:
NamedList
now subclassesUserList
rather thancollections.UserList
.
UserFloat¶
-
class
UserFloat
(value=0.0)[source]¶ Bases:
Real
Class which simulates a float.
New in version 1.6.0.
- Parameters
value (
Union
[SupportsFloat
,SupportsIndex
,str
,bytes
,bytearray
]) – The values to initialise theUserFloat
with. 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
UserFloat
tondigits
decimal 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
UserFloat
tondigits
decimal places, defaulting to0
.If
ndigits
is 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
item
to the end of theUserList
.clear
()Remove all items from the
UserList
.extend
(other)Extend the
NamedList
by appending elements fromother
.insert
(i, item)Insert
item
at positioni
in theUserList
.remove
(item)Removes the first occurrence of
item
from the list.replace
(what, with_)Replace the first instance of
what
withwith_
.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
item
from 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
-