From 68e22865f0be6c354866f39fd26aab8ab8c116cc Mon Sep 17 00:00:00 2001 From: bleudev Date: Tue, 2 Jul 2024 14:14:07 +0300 Subject: [PATCH] UDict: many methods added --- docs/useful_classes/udict.md | 140 +++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/docs/useful_classes/udict.md b/docs/useful_classes/udict.md index b9b8ec7..cb181d5 100644 --- a/docs/useful_classes/udict.md +++ b/docs/useful_classes/udict.md @@ -422,6 +422,146 @@ Returns that UDict is not empty. The equivalent of `#!py not is_empty()` print("D is not empty!") ``` +### _\_contains__(item: tuple[KT, VT] | list[KT | VT] | KT) -> bool + +Check that item or key in UDict. + +!!! example + ```py + if ('key', 'value') in d: ... + if ['key', 'value'] in d: ... + if 'key' in d: ... + ``` + +### _\_repr__() -> str + +Returns string presentation of UDict. Because of it, UDict can be used in `#!py print()` and `#!py repr()` + +!!! example + ```py + d = UDict(hello=1) + print(d) # u{'hello': 1} + print(repr(d)) # Same + ``` + +### _\_hash__() -> int + +Returns hash for `#!py repr(UDict)`. The equivalent of `#!py repr(UDict).__hash__()` + +### _\_cmp__(other: dict[KT, VT] | UDict[KT, VT, CDV]) -> int + +Used by `#!py @cmp_generator`, which generates compare magic methods, like `==`, `!=`, `>`, `>=`, `<`, `<=` operators. + +Comparing UDicts is comparing their lengths, except of [`#!py __eq__()`](). + +!!! example + ```py + d = UDict(hello=1) + d2 = UDict(hello=1, hi=2) + print(d != d2) # True + print(d < d2) # True + print(d > d2) # False + ``` + +### _\_eq__(other: dict[KT, VT] | UDict[KT, VT, CDV]) -> bool + +Checks that UDicts are same. (Overrides generated by `#!py @cmp_generator` magic method) + +!!! example + ```py + d = UDict(hello=1) + d2 = UDict(hello=1, hi=2) + print(d == d2) # False + print(d == d) # True + ``` + +!!! tip + You can use dict to compare with UDict. Method will automatically generate UDict from this dict + ```py + print(d == {'hello': 1}) # True + ``` + +### _\_add__(other: dict[KT, VT] | UDict[KT, VT, CDV]) -> UDict[KT, VT, CDV] + +Add dictionary or UDict's dictionary to UDict's dictionary. This method also have `r` and `i` version (`+=`) + +!!! example + ```py + print(d + {'hi': 2}) + print({'hi': 2} + d) + d += {'hi': 2} + ``` + *[KT]: Key type *[VT]: Value type *[CDV]: Class default value + +[//]: # ( def __sub__(self, other: dict[KT, VT] | UDict[KT, VT, CDV]) -> UDict[KT, VT, CDV]:) + +[//]: # ( new_dict = self.__dict.copy()) + +[//]: # ( ) +[//]: # ( if isinstance(other, UDict):) + +[//]: # ( other: dict[KT, VT] = other.dictionary) + +[//]: # ( ) +[//]: # ( for k, v in other.items():) + +[//]: # ( if new_dict.get(k) == v:) + +[//]: # ( del new_dict[k]) + +[//]: # ( return UDict(new_dict)) + +[//]: # ( ) +[//]: # ( def __mul__() + +[//]: # ( self, other: dict[KT, float | int] | UDict[KT, float | int, DV] | float | int) + +[//]: # ( ) -> UDict[KT, VT, CDV]:) + +[//]: # ( new_dict = self.__dict.copy()) + +[//]: # ( ) +[//]: # ( if isinstance(other, UDict):) + +[//]: # ( other: dict[KT, VT] = other.dictionary) + +[//]: # ( if isinstance(other, (int, float)):) + +[//]: # ( other = dict([(k, other) for k in new_dict.keys()])) + +[//]: # ( ) +[//]: # ( for k, v in other.items():) + +[//]: # ( new_dict[k] *= v) + +[//]: # ( ) +[//]: # ( return UDict(new_dict)) + +[//]: # () +[//]: # ( def __truediv__() + +[//]: # ( self, other: dict[KT, float | int] | UDict[KT, float | int, DV] | float | int) + +[//]: # ( ) -> UDict[KT, VT, CDV]:) + +[//]: # ( new_dict = self.__dict.copy()) + +[//]: # ( ) +[//]: # ( if isinstance(other, UDict):) + +[//]: # ( other: dict[KT, VT] = other.dictionary) + +[//]: # ( if isinstance(other, (int, float)):) + +[//]: # ( other = dict([(k, other) for k in new_dict.keys()])) + +[//]: # ( ) +[//]: # ( for k, v in other.items():) + +[//]: # ( new_dict[k] /= v) + +[//]: # ( ) +[//]: # ( return UDict(new_dict))