Skip to content

Commit

Permalink
Big update
Browse files Browse the repository at this point in the history
  • Loading branch information
bleudev committed May 31, 2024
1 parent 6185757 commit 86f36c5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
16 changes: 13 additions & 3 deletions examples/udict.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ use `UDict[key]` syntax:
d['id'] # 2
```

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 index of key or slice of indexes of keys:

> [!CAUTION]
> In this class first index is 1
```python
d[1] # 2
d[:] # u{'id': 2, 'content': 'hello, world'} (UDict object)
Expand All @@ -56,4 +59,11 @@ For setting items you should use the way you use in lists and dicts:
use `UDict[key] = value` syntax:
```python
d['id'] = 3
```
```

Also you can use indexes and slices when you are setting items:
```python
d[1] = 2
d[2:6:2] = 8
d[:3] = 1, 2, 3
```
2 changes: 2 additions & 0 deletions ufpy/typ/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .protocols import *
from .type_alias import *
12 changes: 11 additions & 1 deletion ufpy/protocols.py → ufpy/typ/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'SupportsGet',
'SupportsSetItem',
'SupportsDelItem',
'LikeDict',
)

from typing import Protocol, Generic, TypeVar
Expand All @@ -11,7 +12,6 @@
VT = TypeVar('VT')
DV = TypeVar('DV')

T = TypeVar('T')

class SupportsGetItem(Protocol, Generic[KT, VT]):
def __getitem__(self, key: KT) -> VT: ...
Expand All @@ -27,3 +27,13 @@ def __setitem__(self, key: KT, value: VT) -> None: ...

class SupportsDelItem(Protocol, Generic[KT, VT]):
def __delitem__(self, key: KT) -> None: ...


class LikeDict(
SupportsGet[KT, VT],
SupportsGetItem[KT, VT],
SupportsSetItem[KT, VT],
SupportsDelItem[KT, VT],
Generic[KT, VT]
):
...
9 changes: 9 additions & 0 deletions ufpy/typ/type_alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .protocols import LikeDict

__all__ = (
'AnyCollection',
'AnyDict'
)

type AnyCollection[T] = tuple[T, ...] | list[T]
type AnyDict[KT, VT] = dict[KT, VT] | LikeDict[KT, VT]
30 changes: 18 additions & 12 deletions ufpy/udict.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Generic, Iterator, overload, TypeVar, Iterable, Callable
from typing import Generic, Iterator, overload, TypeVar, Callable

from .cmp import cmp_generator
from .math_op import i_generator, r_generator
from .typ import AnyDict, AnyCollection
from .utils import set_items_for_several_keys, get_items_for_several_keys, del_items_for_several_keys

__all__ = (
Expand All @@ -17,15 +18,18 @@
@r_generator
class UDict(Generic[KT, VT, DV]):
@overload
def __init__(self, dictionary: dict[KT, VT]): ...
def __init__(self, dictionary: AnyDict[KT, VT]): ...
@overload
def __init__(self, dictionary: dict[KT, VT], *, default: DV): ...
def __init__(self, dictionary: AnyDict[KT, VT], *, default: DV): ...
@overload
def __init__(self, **kwargs: VT): ...
@overload
def __init__(self, *, default: DV, **kwargs: VT): ...
def __init__(self, dictionary = None, *, default = None, **kwargs):
self.__dict = dictionary if dictionary is not None else kwargs

def __init__(self, dictionary: AnyDict[KT, VT] = None, *, default: DV = None, **kwargs: VT):
if isinstance(dictionary, UDict):
dictionary = dictionary.dictionary
self.__dict = dictionary if dictionary else kwargs
self.__default = default

# dictionary
Expand All @@ -34,7 +38,7 @@ def dictionary(self) -> dict[KT, VT]:
return self.__dict

@dictionary.setter
def dictionary(self, value: 'dict[KT, VT] | UDict[KT, VT]'):
def dictionary(self, value: AnyDict[KT, VT]):
if isinstance(value, UDict):
value = value.dictionary
self.__dict = value
Expand All @@ -45,24 +49,26 @@ def keys(self) -> list[KT]:
return list(self.__dict.keys())

@keys.setter
def keys(self, value: Iterable[KT]):
values = list(self.__dict.values())
self.__dict = dict(list(zip(value, values)))
def keys(self, value: AnyCollection[KT]):
self.__dict = dict(list(zip(value, self.values)))

# values
@property
def values(self) -> list[VT]:
return list(self.__dict.values())

@values.setter
def values(self, value: Iterable[VT]):
keys = list(self.__dict.keys())
self.__dict = dict(list(zip(keys, value)))
def values(self, value: AnyCollection[VT]):
self.__dict = dict(list(zip(self.keys, value)))

# items
@property
def items(self) -> list[tuple[KT, VT]]:
return list(zip(self.keys, self.values))

@items.setter
def items(self, value: AnyCollection[tuple[KT, VT] | list[KT | VT]]):
self.__dict = dict(value)

# default
@property
Expand Down
5 changes: 2 additions & 3 deletions ufpy/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
__all__ = (
'get_items_for_several_keys',
'set_items_for_several_keys',
'del_items_for_several_keys',
)

from typing import TypeVar

from .protocols import SupportsGet, SupportsSetItem, SupportsDelItem

type AnyCollection[T] = tuple[T, ...] | list[T]
from .typ import SupportsGet, SupportsSetItem, SupportsDelItem, AnyCollection

KT = TypeVar('KT')
VT = TypeVar('VT')
Expand Down

0 comments on commit 86f36c5

Please sign in to comment.