Skip to content

Commit

Permalink
Merge pull request #20 from honey-team/future-annotations
Browse files Browse the repository at this point in the history
Add import of `future.annotations` for simplifying of code with string annotations
  • Loading branch information
bleudev authored Jun 3, 2024
2 parents d425d67 + 65a0750 commit c3f304b
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions ufpy/udict.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Generic, Iterator, overload, TypeVar, Callable

from .cmp import cmp_generator
Expand All @@ -14,7 +16,8 @@
CDV = TypeVar('CDV')
DV = TypeVar('DV')

class _ClassDefault: ...
class _ClassDefault:
...

@cmp_generator
@i_generator
Expand Down Expand Up @@ -83,37 +86,37 @@ def default(self, value: CDV):
self.__default = value

# call
def __call__(self, func: Callable[[KT, VT], VT]) -> 'UDict[KT, VT, CDV]':
def __call__(self, func: Callable[[KT, VT], VT]) -> UDict[KT, VT, CDV]:
new_dict = self.__dict
for k, v in self:
new_dict[k] = func(k, v)
return UDict(new_dict, default=self.__default)

# reverse integers
def __neg__(self) -> 'UDict[KT, VT, CDV]':
def __neg__(self) -> UDict[KT, VT, CDV]:
return self(lambda k, v: -v)

# reverse
def reverse(self) -> 'UDict[KT, VT, CDV]':
def reverse(self) -> UDict[KT, VT, CDV]:
self.__dict = self.reversed().__dict
return self

def reversed(self) -> 'UDict[KT, VT, CDV]':
def reversed(self) -> UDict[KT, VT, CDV]:
keys, values = list(self.__dict.keys())[::-1], list(self.__dict.values())[::-1]
return UDict(dict(list(zip(keys, values))))

def __invert__(self) -> 'UDict[KT, VT, CDV]':
def __invert__(self) -> UDict[KT, VT, CDV]:
return self.reversed()

def __reversed__(self) -> 'UDict[KT, VT, CDV]':
def __reversed__(self) -> UDict[KT, VT, CDV]:
return self.reversed()

# sort
def sort(self) -> 'UDict[KT, VT, CDV]':
def sort(self) -> UDict[KT, VT, CDV]:
self.__dict = self.sorted().__dict
return self

def sorted(self) -> 'UDict[KT, VT, CDV]':
def sorted(self) -> UDict[KT, VT, CDV]:
keys = sorted(list(self.__dict.keys()))
values = get_items_for_several_keys(self.__dict, keys)
return UDict(dict(list(zip(keys, values))))
Expand All @@ -134,7 +137,7 @@ def __get_keys_from_slice_or_int(self, key: KT | int | slice) -> list[KT]:
return [list(self.__dict.keys())[i - 1] for i in indexes]
return [key]

def __getitem__(self, key: KT | int | slice) -> 'UDict[KT, VT, DV] | VT':
def __getitem__(self, key: KT | int | slice) -> UDict[KT, VT, DV] | VT:
keys = self.__get_keys_from_slice_or_int(key)

l = get_items_for_several_keys(self.__dict, keys, self.__default)
Expand Down Expand Up @@ -248,16 +251,16 @@ def __hash__(self) -> int:
return hash(self.__repr__())

# Comparing
def __cmp__(self, other: 'dict[KT, VT] | UDict[KT, VT, CDV]') -> int:
def __cmp__(self, other: dict[KT, VT] | UDict[KT, VT, CDV]) -> int:
return len(self) - len(other)

def __eq__(self, other: 'dict[KT, VT] | UDict[KT, VT, CDV]') -> bool:
def __eq__(self, other: dict[KT, VT] | UDict[KT, VT, CDV]) -> bool:
if isinstance(other, UDict):
other = other.dictionary
return self.__dict == other

# Math operations
def __add__(self, other: 'dict[KT, VT] | UDict[KT, VT, CDV]') -> 'UDict[KT, VT, CDV]':
def __add__(self, other: dict[KT, VT] | UDict[KT, VT, CDV]) -> UDict[KT, VT, CDV]:
new_dict = self.__dict.copy()

if isinstance(other, UDict):
Expand All @@ -267,7 +270,7 @@ def __add__(self, other: 'dict[KT, VT] | UDict[KT, VT, CDV]') -> 'UDict[KT, VT,
new_dict[k] = v
return UDict(new_dict)

def __sub__(self, other: 'dict[KT, VT] | UDict[KT, VT, CDV]') -> 'UDict[KT, VT, CDV]':
def __sub__(self, other: dict[KT, VT] | UDict[KT, VT, CDV]) -> UDict[KT, VT, CDV]:
new_dict = self.__dict.copy()

if isinstance(other, UDict):
Expand All @@ -279,8 +282,8 @@ def __sub__(self, other: 'dict[KT, VT] | UDict[KT, VT, CDV]') -> 'UDict[KT, VT,
return UDict(new_dict)

def __mul__(
self, other: 'dict[KT, float | int] | UDict[KT, float | int, DV] | float | int'
) -> 'UDict[KT, VT, CDV]':
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):
Expand All @@ -294,8 +297,8 @@ def __mul__(
return UDict(new_dict)

def __truediv__(
self, other: 'dict[KT, float | int] | UDict[KT, float | int, DV] | float | int'
) -> 'UDict[KT, VT, CDV]':
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):
Expand Down

0 comments on commit c3f304b

Please sign in to comment.