Skip to content

Commit

Permalink
alias AttMapEcho as EchoAttMap; close pepkit#38
Browse files Browse the repository at this point in the history
  • Loading branch information
vreuter committed May 16, 2019
1 parent 0c3984a commit 177bd4b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
8 changes: 4 additions & 4 deletions attmap/__init__.py
Expand Up @@ -2,7 +2,7 @@

from ._att_map_like import AttMapLike
from .attmap import AttMap
from .attmap_echo import AttMapEcho
from .attmap_echo import *
from .helpers import *
from .ordattmap import OrdAttMap
from .pathex_attmap import PathExAttMap
Expand All @@ -11,6 +11,6 @@
AttributeDict = AttMap
AttributeDictEcho = AttMapEcho

__all__ = ["AttMapLike", "AttMap", "AttMapEcho",
"AttributeDict", "AttributeDictEcho",
"OrdAttMap", "PathExAttMap", "get_data_lines"]
__all__ = ["AttMapLike", "AttMap", "AttMapEcho", "AttributeDict",
"AttributeDictEcho", "EchoAttMap", "OrdAttMap", "PathExAttMap",
"get_data_lines"]
15 changes: 13 additions & 2 deletions attmap/attmap_echo.py
Expand Up @@ -2,15 +2,23 @@

from .pathex_attmap import PathExAttMap

__author__ = "Vince Reuter"
__email__ = "vreuter@virginia.edu"

__all__ = ["AttMapEcho", "EchoAttMap"]


class AttMapEcho(PathExAttMap):
""" An AttMap that returns key/attr if it has no set value. """

def __getattr__(self, item, default=None):
def __getattr__(self, item, default=None, expand=True):
"""
Fetch the value associated with the provided identifier.
:param int | str item: identifier for value to fetch
:param object default: default return value
:param bool expand: whether to attempt variable expansion of string
value, in case it's a path
:return object: whatever value corresponds to the requested key/item
:raises AttributeError: if the requested item has not been set,
no default value is provided, and this instance is not configured
Expand All @@ -21,7 +29,7 @@ def __getattr__(self, item, default=None):
to be indicative of the intent of protection.
"""
try:
return super(self.__class__, self).__getattr__(item, default)
return super(self.__class__, self).__getattr__(item, default, expand)
except (AttributeError, TypeError):
# If not, triage and cope accordingly.
if self._is_od_member(item) or \
Expand All @@ -35,3 +43,6 @@ def __getattr__(self, item, default=None):
def _lower_type_bound(self):
""" Most specific type to which an inserted value may be converted """
return AttMapEcho


EchoAttMap = AttMapEcho
2 changes: 2 additions & 0 deletions docs/changelog.md
@@ -1,6 +1,8 @@
# Changelog

## [0.12.1] - 2019-05-16
### Added
- `EchoAttMap` as alias for `AttMapEcho`; see [Issue 38](https://github.com/pepkit/attmap/issues/38)
### Fixed
- In any `OrdAttMap, for membership (`__contains__`) consider items added via attribute syntax.
- Prevent duplicate key/attr iteration in any `OrdAttMap`.
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Expand Up @@ -7,8 +7,8 @@
__email__ = "vreuter@virginia.edu"


ALL_ATTMAPS = [AttributeDict, AttributeDictEcho, AttMap, OrdAttMap, AttMapEcho,
PathExAttMap]
ALL_ATTMAPS = [AttributeDict, AttributeDictEcho, AttMap, AttMapEcho,
EchoAttMap, OrdAttMap, PathExAttMap]


@pytest.fixture(scope="function", params=ALL_ATTMAPS)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_packaging.py
Expand Up @@ -23,12 +23,16 @@ def get_base_check(*bases):
return lambda obj: obj.__bases__ == bases


ECHO_TEST_FUNS = [isclass, get_base_check(PathExAttMap)]


@pytest.mark.parametrize(["obj_name", "typecheck"], itertools.chain(*[
[("AttMapLike", f) for f in [isclass, lambda obj: obj.__metaclass__ == ABCMeta]],
[("AttMap", f) for f in [isclass, get_base_check(AttMapLike)]],
[("OrdAttMap", f) for f in [isclass, get_base_check(OrderedDict, AttMap)]],
[("PathExAttMap", f) for f in [isclass, get_base_check(OrdAttMap)]],
[("AttMapEcho", f) for f in [isclass, get_base_check(PathExAttMap)]],
[("AttMapEcho", f) for f in ECHO_TEST_FUNS],
[("EchoAttMap", f) for f in ECHO_TEST_FUNS],
[("get_data_lines", isfunction)]
]))
def test_top_level_exports(obj_name, typecheck):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_special_mutability.py
Expand Up @@ -8,8 +8,8 @@
__email__ = "vreuter@virginia.edu"


ALL_ATTMAPS = [AttributeDict, AttributeDictEcho]
NULLABLE_ATTMAPS = ALL_ATTMAPS
OLD_ATTMAPS = [AttributeDict, AttributeDictEcho]
NULLABLE_ATTMAPS = OLD_ATTMAPS


@pytest.fixture(scope="function", params=["arbitrary", "random"])
Expand All @@ -22,7 +22,7 @@ class UniversalMutabilityTests:
""" Tests of attmap behavior with respect to mutability """

@staticmethod
@pytest.fixture(scope="function", params=ALL_ATTMAPS)
@pytest.fixture(scope="function", params=OLD_ATTMAPS)
def m(request):
""" Provide a test case with a fresh empty data object. """
return get_att_map(request.param)
Expand Down

0 comments on commit 177bd4b

Please sign in to comment.