-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
86 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Any, Iterator, Mapping, TypeVar, final | ||
|
||
from attrs import frozen | ||
from typing_extensions import Self | ||
from wraps import wrap_option | ||
|
||
__all__ = ("MappingView", "mapping_view") | ||
|
||
K = TypeVar("K") | ||
V = TypeVar("V") | ||
|
||
|
||
@final | ||
@frozen() | ||
class MappingView(Mapping[K, V]): | ||
"""Represents view over mappings.""" | ||
|
||
mapping: Mapping[K, V] | ||
"""The mapping to view.""" | ||
|
||
def __iter__(self) -> Iterator[K]: | ||
yield from self.mapping | ||
|
||
def __getitem__(self, key: K) -> V: | ||
return self.mapping[key] | ||
|
||
def __contains__(self, key: Any) -> bool: | ||
return key in self.mapping | ||
|
||
def __len__(self) -> int: | ||
return len(self.mapping) | ||
|
||
@wrap_option | ||
def get_option(self, key: K) -> V: | ||
return self[key] | ||
|
||
def copy(self) -> Self: | ||
return type(self)(self) | ||
|
||
|
||
mapping_view = MappingView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters