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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
|
# !/usr/bin/env python
#
# bases.py
"""
Useful base classes.
"""
#
# Copyright © 2020 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
#
# UserList based on CPython.
# Licensed under the Python Software Foundation License Version 2.
# Copyright © 2001-2020 Python Software Foundation. All rights reserved.
# Copyright © 2000 BeOpen.com. All rights reserved.
# Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
# Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
#
# stdlib
from abc import abstractmethod
from numbers import Real
from pprint import pformat
from typing import (
Any,
Dict,
Iterable,
Iterator,
List,
MutableSequence,
Optional,
SupportsFloat,
Tuple,
Type,
TypeVar,
Union,
overload
)
# this package
from domdf_python_tools._is_match import is_match_with
from domdf_python_tools.doctools import prettify_docstrings
from domdf_python_tools.typing import SupportsIndex
__all__ = [
"Dictable",
"NamedList",
"namedlist",
"UserList",
"UserFloat",
"Lineup",
"_V",
"_LU",
"_T",
"_S",
"_F",
]
_F = TypeVar("_F", bound="UserFloat")
_LU = TypeVar("_LU", bound="Lineup")
_S = TypeVar("_S", bound="UserList")
_T = TypeVar("_T")
_V = TypeVar("_V")
@prettify_docstrings
class Dictable(Iterable[Tuple[str, _V]]):
"""
The basic structure of a class that can be converted into a dictionary.
"""
@abstractmethod
def __init__(self, *args, **kwargs):
pass
def __repr__(self) -> str:
return super().__repr__()
def __str__(self) -> str:
return self.__repr__()
def __iter__(self) -> Iterator[Tuple[str, _V]]:
"""
Iterate over the attributes of the class.
"""
yield from self.__dict__.items()
def __getstate__(self) -> Dict[str, _V]:
return self.__dict__
def __setstate__(self, state):
self.__init__(**state) # type: ignore[misc]
def __copy__(self):
return self.__class__(**self.__dict__)
def __deepcopy__(self, memodict={}):
return self.__copy__()
@property
@abstractmethod
def __dict__(self):
return dict() # pragma: no cover (abc)
def __eq__(self, other) -> bool:
if isinstance(other, self.__class__):
return is_match_with(other.__dict__, self.__dict__)
return NotImplemented
@prettify_docstrings
class UserList(MutableSequence[_T]):
"""
Typed version of :class:`collections.UserList`.
Class that simulates a list. The instance’s contents are kept in a regular list,
which is accessible via the :attr:`~.UserList.data` attribute of :class:`~.UserList` instances.
The instance’s contents are initially set to a copy of list, defaulting to the empty list ``[]``.
.. versionadded:: 0.10.0
:param initlist: The initial values to populate the :class:`~.UserList` with.
:default initlist: ``[]``
.. latex:clearpage::
.. admonition:: Subclassing requirements
Subclasses of :class:`~.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.
"""
#: A real list object used to store the contents of the :class:`~domdf_python_tools.bases.UserList`.
data: List[_T]
def __init__(self, initlist: Optional[Iterable[_T]] = None):
self.data = []
if initlist is not None:
# XXX should this accept an arbitrary sequence?
if type(initlist) is type(self.data): # noqa: E721
self.data[:] = initlist
elif isinstance(initlist, UserList):
self.data[:] = initlist.data[:]
else:
self.data = list(initlist)
def __repr__(self) -> str:
return repr(self.data)
def __lt__(self, other: object) -> bool:
return self.data < self.__cast(other)
def __le__(self, other: object) -> bool:
return self.data <= self.__cast(other)
def __eq__(self, other: object) -> bool:
return self.data == self.__cast(other)
def __gt__(self, other: object) -> bool:
return self.data > self.__cast(other)
def __ge__(self, other: object) -> bool:
return self.data >= self.__cast(other)
@staticmethod
def __cast(other):
return other.data if isinstance(other, UserList) else other
def __contains__(self, item: object) -> bool:
return item in self.data
def __len__(self) -> int:
return len(self.data)
def __iter__(self) -> Iterator[_T]:
yield from self.data
@overload
def __getitem__(self, i: int) -> _T: ...
@overload
def __getitem__(self, i: slice) -> MutableSequence[_T]: ...
def __getitem__(self, i: Union[int, slice]) -> Union[_T, MutableSequence[_T]]:
if isinstance(i, slice):
return self.__class__(self.data[i])
else:
return self.data[i]
@overload
def __setitem__(self, i: int, o: _T) -> None: ...
@overload
def __setitem__(self, i: slice, o: Iterable[_T]) -> None: ...
def __setitem__(self, i: Union[int, slice], item: Union[_T, Iterable[_T]]) -> None:
self.data[i] = item # type: ignore[index, assignment]
def __delitem__(self, i: Union[int, slice]):
del self.data[i]
def __add__(self: _S, other: Iterable[_T]) -> _S:
if isinstance(other, UserList):
return self.__class__(self.data + other.data)
elif isinstance(other, type(self.data)):
return self.__class__(self.data + other)
return self.__class__(self.data + list(other))
def __radd__(self, other):
if isinstance(other, UserList):
return self.__class__(other.data + self.data)
elif isinstance(other, type(self.data)):
return self.__class__(other + self.data)
return self.__class__(list(other) + self.data)
def __iadd__(self: _S, other: Iterable[_T]) -> _S:
if isinstance(other, UserList):
self.data += other.data
elif isinstance(other, type(self.data)):
self.data += other
else:
self.data += list(other)
return self
def __mul__(self: _S, n: int) -> _S:
return self.__class__(self.data * n)
__rmul__ = __mul__
def __imul__(self: _S, n: int) -> _S:
self.data *= n
return self
def __copy__(self):
inst = self.__class__.__new__(self.__class__)
inst.__dict__.update(self.__dict__)
# Create a copy and avoid triggering descriptors
inst.__dict__["data"] = self.__dict__["data"][:]
return inst
def append(self, item: _T) -> None:
"""
Append ``item`` to the end of the :class:`~.domdf_python_tools.bases.UserList`.
"""
self.data.append(item)
def insert(self, i: int, item: _T) -> None:
"""
Insert ``item`` at position ``i`` in the :class:`~.domdf_python_tools.bases.UserList`.
"""
self.data.insert(i, item)
def pop(self, i: int = -1) -> _T:
"""
Removes and returns the item at index ``i``.
:raises IndexError: if list is empty or index is out of range.
"""
return self.data.pop(i)
def remove(self, item: _T) -> None:
"""
Removes the first occurrence of ``item`` from the list.
:param item:
:rtype:
:raises ValueError: if the item is not present.
.. latex:clearpage::
"""
self.data.remove(item)
def clear(self) -> None:
"""
Remove all items from the :class:`~.domdf_python_tools.bases.UserList`.
"""
self.data.clear()
def copy(self: _S) -> _S:
"""
Returns a copy of the :class:`~.domdf_python_tools.bases.UserList`.
"""
return self.__class__(self)
def count(self, item: _T) -> int:
"""
Returns the number of occurrences of ``item`` in the :class:`~.domdf_python_tools.bases.UserList`.
"""
return self.data.count(item)
def index(self, item: _T, *args: Any) -> int:
"""
Returns the index of the fist element matching ``item``.
:param item:
:param args:
:raises ValueError: if the item is not present.
"""
return self.data.index(item, *args)
def reverse(self) -> None:
"""
Reverse the list in place.
"""
self.data.reverse()
def sort(self, *, key=None, reverse: bool = False) -> None:
"""
Sort the list in ascending order and return :py:obj:`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.
"""
self.data.sort(key=key, reverse=reverse)
def extend(self, other: Iterable[_T]) -> None:
"""
Extend the :class:`~.domdf_python_tools.bases.NamedList` by appending elements from ``other``.
:param other:
"""
if isinstance(other, UserList):
self.data.extend(other.data)
else:
self.data.extend(other)
@prettify_docstrings
class UserFloat(Real):
"""
Class which simulates a float.
.. versionadded:: 1.6.0
:param value: The values to initialise the :class:`~domdf_python_tools.bases.UserFloat` with.
"""
def __init__(self, value: Union[SupportsFloat, SupportsIndex, str, bytes, bytearray] = 0.0):
self._value = (float(value), )
def as_integer_ratio(self) -> Tuple[int, int]:
"""
Returns the float as a fraction.
"""
return float(self).as_integer_ratio()
def hex(self) -> str: # noqa: A003 # pylint: disable=redefined-builtin
"""
Returns the hexadecimal (base 16) representation of the float.
"""
return float(self).hex()
def is_integer(self) -> bool:
"""
Returns whether the float is an integer.
"""
return float(self).is_integer()
@classmethod
def fromhex(cls: Type[_F], string: str) -> _F:
"""
Create a floating-point number from a hexadecimal string.
:param string:
"""
return cls(float.fromhex(string))
def __add__(self: _F, other: float) -> _F:
return self.__class__(float(self).__add__(other))
def __sub__(self: _F, other: float) -> _F:
return self.__class__(float(self).__sub__(other))
def __mul__(self: _F, other: float) -> _F:
return self.__class__(float(self).__mul__(other))
def __floordiv__(self: _F, other: float) -> _F: # type: ignore[override]
return self.__class__(float(self).__floordiv__(other))
def __truediv__(self: _F, other: float) -> _F:
return self.__class__(float(self).__truediv__(other))
def __mod__(self: _F, other: float) -> _F:
return self.__class__(float(self).__mod__(other))
def __divmod__(self: _F, other: float) -> Tuple[_F, _F]:
return tuple(self.__class__(x) for x in float(self).__divmod__(other)) # type: ignore[return-value]
def __pow__(self: _F, other: float, mod=None) -> _F:
return self.__class__(float(self).__pow__(other, mod))
def __radd__(self: _F, other: float) -> _F:
return self.__class__(float(self).__radd__(other))
def __rsub__(self: _F, other: float) -> _F:
return self.__class__(float(self).__rsub__(other))
def __rmul__(self: _F, other: float) -> _F:
return self.__class__(float(self).__rmul__(other))
def __rfloordiv__(self: _F, other: float) -> _F: # type: ignore[override]
return self.__class__(float(self).__rfloordiv__(other))
def __rtruediv__(self: _F, other: float) -> _F:
return self.__class__(float(self).__rtruediv__(other))
def __rmod__(self: _F, other: float) -> _F:
return self.__class__(float(self).__rmod__(other))
def __rdivmod__(self: _F, other: float) -> Tuple[_F, _F]:
return tuple(self.__class__(x) for x in float(self).__rdivmod__(other)) # type: ignore
def __rpow__(self: _F, other: float, mod=None) -> _F:
return self.__class__(float(self).__rpow__(other, mod))
def __getnewargs__(self) -> Tuple[float]:
return self._value
def __trunc__(self) -> int:
"""
Truncates the float to an integer.
"""
return float(self).__trunc__()
def __round__(self, ndigits: Optional[int] = None) -> Union[int, float]: # type: ignore
"""
Round the :class:`~.UserFloat` to ``ndigits`` decimal places, defaulting to ``0``.
If ``ndigits`` is omitted or :py:obj:`None`, returns an :class:`int`,
otherwise returns a :class:`float`.
Rounds half toward even.
:param ndigits:
"""
return float(self).__round__(ndigits)
def __eq__(self, other: object) -> bool:
if isinstance(other, UserFloat) and not isinstance(other, float):
return self._value == other._value
else:
return float(self).__eq__(other)
def __ne__(self, other: object) -> bool:
if isinstance(other, UserFloat) and not isinstance(other, float):
return self._value != other._value
else:
return float(self).__ne__(other)
def __lt__(self, other: Union[float, "UserFloat"]) -> bool:
if isinstance(other, UserFloat) and not isinstance(other, float):
return self._value < other._value
else:
return float(self).__lt__(other)
def __le__(self, other: Union[float, "UserFloat"]) -> bool:
if isinstance(other, UserFloat) and not isinstance(other, float):
return self._value <= other._value
else:
return float(self).__le__(other)
def __gt__(self, other: Union[float, "UserFloat"]) -> bool:
if isinstance(other, UserFloat) and not isinstance(other, float):
return self._value > other._value
else:
return float(self).__gt__(other)
def __ge__(self, other: Union[float, "UserFloat"]) -> bool:
if isinstance(other, UserFloat) and not isinstance(other, float):
return self._value >= other._value
else:
return float(self).__ge__(other)
def __neg__(self: _F) -> _F:
return self.__class__(float(self).__neg__())
def __pos__(self: _F) -> _F:
return self.__class__(float(self).__pos__())
def __str__(self) -> str:
return str(float(self))
def __int__(self) -> int:
return int(float(self))
def __float__(self) -> float:
return self._value[0]
def __abs__(self: _F) -> _F:
return self.__class__(float(self).__abs__())
def __hash__(self) -> int:
return float(self).__hash__()
def __repr__(self) -> str:
return str(self)
def __ceil__(self):
raise NotImplementedError
def __floor__(self):
raise NotImplementedError
def __bool__(self) -> bool:
"""
Return ``self != 0``.
"""
return super().__bool__()
def __complex__(self) -> complex:
"""
Return :class:`complex(self) <complex>`.
.. code-block:: python
complex(self) == complex(float(self), 0)
"""
return super().__complex__()
@prettify_docstrings
class NamedList(UserList[_T]):
"""
A list with a name.
The name of the list is taken from the name of the subclass.
.. versionchanged:: 0.10.0
:class:`~.NamedList` now subclasses :class:`.UserList` rather than :class:`collections.UserList`.
"""
def __repr__(self) -> str:
return f"{super().__repr__()}"
def __str__(self) -> str:
return f"{self.__class__.__name__}{pformat(list(self))}"
def namedlist(name: str = "NamedList") -> Type[NamedList]:
"""
A factory function to return a custom list subclass with a name.
:param name: The name of the list.
"""
class cls(NamedList):
pass
cls.__name__ = name
return cls
class Lineup(UserList[_T]):
"""
List-like type with fluent methods and some star players.
.. latex:vspace:: -10px
"""
def replace(self: _LU, what: _T, with_: _T) -> _LU:
r"""
Replace the first instance of ``what`` with ``with_``.
:param what: The object to find and replace.
:param with\_: The new value for the position in the list.
"""
self[self.index(what)] = with_
return self
def sort( # type: ignore
self: _LU,
*,
key=None,
reverse: bool = False,
) -> _LU:
"""
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.
"""
super().sort(key=key, reverse=reverse)
return self
def reverse(self: _LU) -> _LU: # type: ignore # noqa: D102
super().reverse()
return self
def append( # type: ignore # noqa: D102
self: _LU,
item: _T,
) -> _LU:
super().append(item)
return self
def extend( # type: ignore # noqa: D102
self: _LU,
other: Iterable[_T],
) -> _LU:
super().extend(other)
return self
def insert( # type: ignore # noqa: D102
self: _LU,
i: int,
item: _T,
) -> _LU:
super().insert(i, item)
return self
def remove( # type: ignore # noqa: D102
self: _LU,
item: _T,
) -> _LU:
super().remove(item)
return self
def clear(self: _LU) -> _LU: # type: ignore # noqa: D102
super().clear()
return self
|