|
58 | 58 | # this package
|
59 | 59 | from domdf_python_tools.doctools import prettify_docstrings
|
60 | 60 |
|
61 |
| -__all__ = ["Dictable", "NamedList", "namedlist", "UserList", "UserFloat"] |
| 61 | +__all__ = [ |
| 62 | + "Dictable", |
| 63 | + "NamedList", |
| 64 | + "namedlist", |
| 65 | + "UserList", |
| 66 | + "UserFloat", |
| 67 | + "Lineup", |
| 68 | + "_V", |
| 69 | + "_LU", |
| 70 | + "_T", |
| 71 | + "_S", |
| 72 | + "_F", |
| 73 | + ] |
62 | 74 |
|
| 75 | +_F = TypeVar("_F", bound="UserFloat") |
| 76 | +_LU = TypeVar("_LU", bound="Lineup") |
| 77 | +_S = TypeVar("_S", bound="UserList") |
| 78 | +_T = TypeVar("_T") |
63 | 79 | _V = TypeVar("_V")
|
64 | 80 |
|
65 | 81 |
|
@@ -110,10 +126,6 @@ def __eq__(self, other) -> bool:
|
110 | 126 | return NotImplemented
|
111 | 127 |
|
112 | 128 |
|
113 |
| -_T = TypeVar("_T") |
114 |
| -_S = TypeVar("_S", bound="UserList") |
115 |
| - |
116 |
| - |
117 | 129 | @prettify_docstrings
|
118 | 130 | class UserList(MutableSequence[_T]):
|
119 | 131 | """
|
@@ -363,9 +375,6 @@ def __index__(self) -> int:
|
363 | 375 | ...
|
364 | 376 |
|
365 | 377 |
|
366 |
| -_F = TypeVar("_F", bound="UserFloat") |
367 |
| - |
368 |
| - |
369 | 378 | @prettify_docstrings
|
370 | 379 | class UserFloat(Real):
|
371 | 380 | """
|
@@ -462,6 +471,10 @@ def __getnewargs__(self) -> Tuple[float]:
|
462 | 471 | return self._value
|
463 | 472 |
|
464 | 473 | def __trunc__(self) -> int:
|
| 474 | + """ |
| 475 | + Truncates the float to an integer. |
| 476 | + """ |
| 477 | + |
465 | 478 | return float(self).__trunc__()
|
466 | 479 |
|
467 | 480 | def __round__(self, ndigits: Optional[int] = None) -> Union[int, float]: # type: ignore
|
@@ -533,6 +546,22 @@ def __ceil__(self):
|
533 | 546 | def __floor__(self):
|
534 | 547 | raise NotImplementedError
|
535 | 548 |
|
| 549 | + def __bool__(self) -> bool: |
| 550 | + """ |
| 551 | + Return ``self != 0``. |
| 552 | + """ |
| 553 | + |
| 554 | + return super().__bool__() |
| 555 | + |
| 556 | + def __complex__(self) -> complex: |
| 557 | + """ |
| 558 | + Returrn :func:`complex(self) <complex>``. |
| 559 | +
|
| 560 | + ``complex(self) == complex(float(self), 0)`` |
| 561 | + """ |
| 562 | + |
| 563 | + return super().__complex__() |
| 564 | + |
536 | 565 |
|
537 | 566 | @prettify_docstrings
|
538 | 567 | class NamedList(UserList[_T]):
|
@@ -566,3 +595,79 @@ class cls(NamedList):
|
566 | 595 | cls.__name__ = name
|
567 | 596 |
|
568 | 597 | return cls
|
| 598 | + |
| 599 | + |
| 600 | +class Lineup(UserList[_T]): |
| 601 | + """ |
| 602 | + List-like type with fluent methods and some star players. |
| 603 | + """ |
| 604 | + |
| 605 | + def replace(self: _LU, what: _T, with_: _T) -> _LU: |
| 606 | + r""" |
| 607 | + Replace the first instance of ``what`` with ``with_``. |
| 608 | +
|
| 609 | + :param what: The object to find and replace. |
| 610 | + :param with\_: The new value for the position in the list. |
| 611 | + """ |
| 612 | + |
| 613 | + self[self.index(what)] = with_ |
| 614 | + |
| 615 | + return self |
| 616 | + |
| 617 | + def sort( |
| 618 | + self: _LU, |
| 619 | + *, |
| 620 | + key=None, |
| 621 | + reverse: bool = False, |
| 622 | + ) -> _LU: # type: ignore |
| 623 | + """ |
| 624 | + Sort the list in ascending order and return the self. |
| 625 | +
|
| 626 | + The sort is in-place (i.e. the list itself is modified) and stable (i.e. the |
| 627 | + order of two equal elements is maintained). |
| 628 | +
|
| 629 | + If a key function is given, apply it once to each list item and sort them, |
| 630 | + ascending or descending, according to their function values. |
| 631 | +
|
| 632 | + The reverse flag can be set to sort in descending order. |
| 633 | + """ |
| 634 | + |
| 635 | + super().sort(key=key, reverse=reverse) |
| 636 | + return self |
| 637 | + |
| 638 | + def reverse(self: _LU, ) -> _LU: # type: ignore |
| 639 | + super().reverse() |
| 640 | + return self |
| 641 | + |
| 642 | + def append( |
| 643 | + self: _LU, |
| 644 | + item: _T, |
| 645 | + ) -> _LU: # type: ignore |
| 646 | + super().append(item) |
| 647 | + return self |
| 648 | + |
| 649 | + def extend( |
| 650 | + self: _LU, |
| 651 | + other: Iterable[_T], |
| 652 | + ) -> _LU: # type: ignore |
| 653 | + super().extend(other) |
| 654 | + return self |
| 655 | + |
| 656 | + def insert( |
| 657 | + self: _LU, |
| 658 | + i: int, |
| 659 | + item: _T, |
| 660 | + ) -> _LU: # type: ignore |
| 661 | + super().insert(i, item) |
| 662 | + return self |
| 663 | + |
| 664 | + def remove( |
| 665 | + self: _LU, |
| 666 | + item: _T, |
| 667 | + ) -> _LU: # type: ignore |
| 668 | + super().remove(item) |
| 669 | + return self |
| 670 | + |
| 671 | + def clear(self: _LU, ) -> _LU: # type: ignore |
| 672 | + super().clear() |
| 673 | + return self |
0 commit comments