Skip to content

Commit

Permalink
UDict: many methods added
Browse files Browse the repository at this point in the history
  • Loading branch information
bleudev committed Jul 2, 2024
1 parent 37656f4 commit 68e2286
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions docs/useful_classes/udict.md
Original file line number Diff line number Diff line change
Expand Up @@ -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__&#40;self, other: dict[KT, VT] | UDict[KT, VT, CDV]&#41; -> UDict[KT, VT, CDV]:)

[//]: # ( new_dict = self.__dict.copy&#40;&#41;)

[//]: # ( )
[//]: # ( if isinstance&#40;other, UDict&#41;:)

[//]: # ( other: dict[KT, VT] = other.dictionary)

[//]: # ( )
[//]: # ( for k, v in other.items&#40;&#41;:)

[//]: # ( if new_dict.get&#40;k&#41; == v:)

[//]: # ( del new_dict[k])

[//]: # ( return UDict&#40;new_dict&#41;)

[//]: # ( )
[//]: # ( def __mul__&#40;)

[//]: # ( self, other: dict[KT, float | int] | UDict[KT, float | int, DV] | float | int)

[//]: # ( &#41; -> UDict[KT, VT, CDV]:)

[//]: # ( new_dict = self.__dict.copy&#40;&#41;)

[//]: # ( )
[//]: # ( if isinstance&#40;other, UDict&#41;:)

[//]: # ( other: dict[KT, VT] = other.dictionary)

[//]: # ( if isinstance&#40;other, &#40;int, float&#41;&#41;:)

[//]: # ( other = dict&#40;[&#40;k, other&#41; for k in new_dict.keys&#40;&#41;]&#41;)

[//]: # ( )
[//]: # ( for k, v in other.items&#40;&#41;:)

[//]: # ( new_dict[k] *= v)

[//]: # ( )
[//]: # ( return UDict&#40;new_dict&#41;)

[//]: # ()
[//]: # ( def __truediv__&#40;)

[//]: # ( self, other: dict[KT, float | int] | UDict[KT, float | int, DV] | float | int)

[//]: # ( &#41; -> UDict[KT, VT, CDV]:)

[//]: # ( new_dict = self.__dict.copy&#40;&#41;)

[//]: # ( )
[//]: # ( if isinstance&#40;other, UDict&#41;:)

[//]: # ( other: dict[KT, VT] = other.dictionary)

[//]: # ( if isinstance&#40;other, &#40;int, float&#41;&#41;:)

[//]: # ( other = dict&#40;[&#40;k, other&#41; for k in new_dict.keys&#40;&#41;]&#41;)

[//]: # ( )
[//]: # ( for k, v in other.items&#40;&#41;:)

[//]: # ( new_dict[k] /= v)

[//]: # ( )
[//]: # ( return UDict&#40;new_dict&#41;)

0 comments on commit 68e2286

Please sign in to comment.