-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
3735a6b
commit 0084087
Showing
4 changed files
with
32 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from . import operations | ||
from .attrdict import AttrDict | ||
from .magic import MagicFilter | ||
|
||
__all__ = ("operations", "MagicFilter", "F") | ||
__all__ = ("operations", "MagicFilter", "F", "AttrDict") | ||
|
||
F = MagicFilter() |
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,14 @@ | ||
from typing import Any, Dict, TypeVar | ||
|
||
KT = TypeVar("KT") | ||
VT = TypeVar("VT") | ||
|
||
|
||
class AttrDict(Dict[KT, VT]): | ||
""" | ||
A wrapper over dict which where element can be accessed as regular attributes | ||
""" | ||
|
||
def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
super(AttrDict, self).__init__(*args, **kwargs) | ||
self.__dict__ = self # type: ignore |
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,15 @@ | ||
from magic_filter import AttrDict | ||
|
||
|
||
class TestAttrDict: | ||
def test_attrdict(self): | ||
attr = AttrDict({"a": 1, "b": 2, "c": "d"}) | ||
|
||
assert attr["a"] == 1 | ||
assert attr.a == 1 | ||
|
||
assert attr["b"] == 2 | ||
assert attr.b == 2 | ||
|
||
assert attr["c"] == "d" | ||
assert attr.c == "d" |