class UDict[KT, VT, CDV]
class UDict(dictionary: AnyDict[KT, VT]) # (1)!
class UDict(dictionary: AnyDict[KT, VT], *, default: CDV)
class UDict(**kwargs: VT)
class UDict(*, default: CDV, **kwargs: VT)
-
AnyDict[KT, VT] = LikeDict[KT, VT] | dict[KT, VT]
. In UDictNote
LikeDict[KT, VT]
is type which has__getitem__
,__setitem__
,__delitem__
andget()
methods.UDict
isLikeDict
.
UDict as generic
You can use UDict
as Generic
, because of it, there are 3 TypeVar
s: KT, VT, CDV.
KT and VT are key type and value type. In inbuilt dict
there are KT and VT type vars.
CDV is class default value.
In this documentation KT, VT and CDV will be used in methods.
Create UDict object. If default
, when not existent keys is given in
getting item, method will return default
.
(property, settable) dictionary: dict[KT, VT]
UDict's dictionary.
(property, settable) keys: list[KT]
UDict's keys
(property, settable) values: list[VT]
UDict's values
(property, settable) items: list[tuple[KT, VT]]
UDict's items.
Tip
You can use tuples to set items or you can use tuples or lists with lists
(property, settable) default: CDV
UDict's default value
reverse() -> UDict[KT, VT, CDV]
Reverses UDict and returns it. (1)
-
How UDict is being reversing?
Just is being reversing items
u{'hello': 1, 'hi': 2}
->u{'hi': 2, 'hello': 1}
(reversed)
Warning
reverse()
edits UDict. If you don't want to reverse UDict use reversed()
method instead.
reversed() -> UDict[KT, VT, CDV]
Returns reversed UDict
Get reversed UDict with inbuilt reversed()
and ~
operator
You can get reversed UDict with inbuilt reversed()
and with invert operator (~
).
sort() -> UDict[KT, VT, CDV]
Sorts UDict and returns it. (1)
-
How UDict is being sorting?
Just are being sorting items by keys.
u{'b': 1, 'a': 2}
->u{'a': 2, 'b': 1}
(sorted)
Warning
sort()
edits UDict. If you don't want to sort UDict use sorted()
method instead.
sorted() -> UDict[KT, VT, CDV]
Returns sorted UDict
get()
def get(*, key: KT) -> VT | CDV
def get(*, key: KT, default: DV) -> VT | DV
def get(*, index: int) -> VT | CDV
def get(*, index: int, default: DV) -> VT | DV
def get(*, value: VT) -> KT | CDV
def get(*, value: VT, default: DV) -> KT | DV
Using more than 1 argument
If you use 0 or 2 or 3 of this arguments (key
, index
, value
), method will raise ValueError
Arguments:
key: KT
UDict value's key to find.
index: int
UDict value's index to find
Warning
Indexes are starting from 1. Index of first element of UDict is 1.
index
argument more than UDict length
If you use index
argument make sure that index
are less than UDict length. Otherwise get()
will raise IndexError
value: VT
UDict key's value to find
default: DV
Default value, if the result was not found. If not provided, then default
is UDict.default
property.
Example
is_empty() -> bool
Returns True
if the UDict is empty (len(UDict) == 0
), otherwise returns False
.
Convert UDict to bool
You can convert UDict to bool
or use UDict in if
statement. If UDict.is_empty() == True
then bool(UDict)
is False and by contrast. Same in if
because of if x
is the equivalent
of if bool(x)
Magic methods
Currently, UDict supports all these magic methods:
__call__(func: Callable[[KT, VT], VT]) -> UDict[KT, VT, CDV]
Returns new UDict, but all values generated with func
function. First argument: key, second: value.
Arguments:
func: (KT, VT) -> VT
First argument of function is key, second is value. Returns new value
__neg__() -> UDict[KT, int | float, CDV]
Negates all values (if they support the -
operator) to their opposite numbers.
__invert__() -> UDict[KT, VT, CDV]
The equivalent of reversed()
__reversed__() -> UDict[KT, VT, CDV]
The equivalent of reversed()
__getitem__(key: KT | int | slice) -> UDict[KT, VT, DV] | VT
Returns the value for a single key, or a UDict for multiple keys provided with a slice.
Indexes starting at 1.
Arguments:
key: KT | int | slice
Value's key or index to get or values's indexes slice to get.
Using indexes
You can also use indexes in __getitem__()
.
Failure
Keep in mind that indexes are using after the keys with the given value were not found.
Example
If you have 1
key, d[1]
syntax will use 1
how key, not index. If you want to use index in
all ways, use get()
instead.
__setitem__(key: KT | int | slice, value: VT | list[VT] | tuple[VT])
Sets the value or values for the given key or keys.
key: KT | int | slice
Value's key or keys to set. This argument is the same with key
argument in __getitem__()
method
value: VT | list[VT] | tuple[VT]
Value or values to set.
Example
__delitem__(key: KT | int | slice)
Deletes items with the given key or keys.
key: KT | int | slice
Item's key or keys to delete. This argument is the same with key
argument in __getitem__()
method.
__len__() -> int
Returns the length of the UDict.
Example
d = UDict(hello=1, hi=2) print(len(d)) # 2
__iter__() -> Iterator[tuple[KT, VT]]
Iterate over the UDict. The equivalent of items.__iter__()
.
__bool__() -> bool
Returns whether the UDict is not empty. The equivalent of not is_empty()
__contains__(item: tuple[KT, VT] | list[KT | VT] | KT) -> bool
Checks whether the item or key is in the UDict.
Arguments:
item: tuple[KT, VT] | list[KT | VT] | KT
Item or item's key.
__repr__() -> str
Returns the string representation of the UDict. This allows the UDict to be used in print()
and repr()
.
__hash__() -> int
Returns the hash for repr(UDict)
. The equivalent of repr(UDict).__hash__()
.
__cmp__(other: dict[KT, VT] | UDict[KT, VT, CDV]) -> int
Used by @cmp_generator
, which generates comparison magic methods like ==
, !=
, >
, >=
, <
, <=
operators.
Comparing UDicts involves comparing their lengths, except for __eq__()
.
Example
__eq__(other: dict[KT, VT] | UDict[KT, VT, CDV]) -> bool
Checks whether UDicts are the same. (Overrides generated by @cmp_generator
magic method)
Tip
You can use dict to compare with UDict. Method will automatically generate UDict from this dict
__add__(other: dict[KT, VT] | UDict[KT, VT, CDV]) -> UDict[KT, VT, CDV]
Adds the dictionary or UDict's dictionary to the UDict's dictionary. This method also has r
and i
versions (+=
).
__sub__(other: dict[KT, VT] | UDict[KT, VT, CDV]) -> UDict[KT, VT, CDV]
Subtracts the dictionary or UDict's dictionary from the UDict's dictionary. This method also has r
and i
versions (-=
).
Example
__mul__(other: dict[KT, float | int] | UDict[KT, float | int, DV] | float | int) -> UDict[KT, SupportsMul, CDV]
Multiply all values by other
, if other
is int
or float
.
Multiply values with keys equals to other
keys by other
values, if other
is dict
or UDict.
This method also have r
and i
version (*=
)
Warning
Make sure that your UDict's values supports multiply operator (*
)
Example
__truediv__(other: dict[KT, float | int] | UDict[KT, float | int, DV] | float | int) -> UDict[KT, SupportsTrueDiv, CDV]
Same that __mul__()
,
but with divide operator (/
).
Warning
Make sure that your UDict's values supports divide operator (/
)
Example
Examples
Init
Example
Example
You can also define default
value for items when you use item's getter:
Get items
For getting items you should use the way you use in lists and dicts: use UDict[key]
syntax:
Tip
You can also use index of key or slice of indexes of keys:
Warning
In this class first index is 1
You can also use get()
method. You can get item by its key using key
kwarg, you can get item by its index using index
kwarg, and you get item key by its value using value
kwarg.
You can define default
only for this calling
Set items
For setting items you should use the way you use in lists and dicts:
use UDict[key] = value
syntax:
Delete items
For deleting items you should use the way you use in lists and dicts:
use del UDict[key]
syntax:
Get length of dict
Iterate dict: keys
, values
, items
properties
You can iterate dict using for key, value in UDict
syntax.
If you want iterate only all keys or values, use keys
or values
properties
Check that dict is empty or not empty
You can use is_empty()
method to check that UDict is empty
You also can use if UDict
or bool(UDict)
syntax
Check that key or item in dict
You can check that key in dict
You can also check that item in dict
Using repr()
and print()
Because print()
uses repr()
, you can directly print UDict
objects
Comparing dicts
You can compare UDict
s using inbuilt compare operators (==
, !=
, >
, >=
, <
, <=
)
Note
When you use equal and not equal compare operator, dicts are comparing by its items and length, but in other compare operators dicts are comparing only by its length.
For example, d > d2 -> len(d) > len(d2)
, etc.
Math operations
You can use inbuilt math operators (+
, -
, *
, /
, +=
, -=
, *=
, /=
) with UDict
s
Note
When you use sum and sub math operators (+
, -
) dicts are summing or subtracting, but
when you use other math operators dict will be multiplying or dividing by integer or dict.
When dict it works like this:
d = UDict(hi=1, hello=2)
print(d + {'world': 3}) # u{'hi': 1, 'hello': 2, 'world': 3}
print(d - {'hello': 2}) # u{'hi': 1}
print(d * 2) # u{'hi': 2, 'hello': 4}
print(d * {'hi': 2}) # u{'hi': 2, 'hello': 2}
print(d / 2) # u{'hi': 0.5, 'hello': 1}
print(d / {'hi': 2}) # u{'hi': 0.5, 'hello': 2}
Negative dict
Reverse dict
You can reverse dict using reverse()
or reversed()
method
When you use reverse()
, dict updates in contrast to reversed()
. Be careful!
You can use ~
operator and reversed()
class. They are equivalents of UDict.reversed()
Sort dict
You can use sort()
and sorted()
methods for sorting UDict
sort()
is updating dict, sorted()
- isn't. Be careful!