Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested expansion; #74 #75

Merged
merged 6 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions attmap/pathex_attmap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
""" Canonical behavior for attmap in pepkit projects """

import sys
if sys.version_info < (3,4):
from collections import Mapping
else:
from collections.abc import Mapping
from .ordattmap import OrdAttMap
from ubiquerg import expandpath

Expand Down Expand Up @@ -39,7 +44,7 @@ def __getattr__(self, item, default=None, expand=True):
else:
return _safely_expand(v) if expand else v

def __getitem__(self, item, expand=True):
def __getitem__(self, item, expand=True, to_dict=False):
"""
Fetch the value of given key.

Expand All @@ -49,22 +54,22 @@ def __getitem__(self, item, expand=True):
:raise KeyError: if the requested key is unmapped.
"""
v = super(PathExAttMap, self).__getitem__(item)
return _safely_expand(v) if expand else v
return _safely_expand(v, to_dict) if expand else v

def get(self, k, default=None, expand=True):
try:
return self.__getitem__(k, expand)
except KeyError:
return default

def items(self, expand=False):
def items(self, expand=False, to_dict=False):
"""
Produce list of key-value pairs, optionally expanding paths.

:param bool expand: whether to expand paths
:return Iterable[object]: stored key-value pairs, optionally expanded
"""
return [(k, self.__getitem__(k, expand)) for k in self]
return [(k, self.__getitem__(k, expand, to_dict)) for k in self]

def values(self, expand=False):
"""
Expand Down Expand Up @@ -100,12 +105,16 @@ def to_dict(self, expand=False):

:return dict: builtin dict representation of this instance
"""
return self._simplify_keyvalue(self.items(expand), dict)
return self._simplify_keyvalue(self.items(expand, to_dict=True), dict)

@property
def _lower_type_bound(self):
return PathExAttMap


def _safely_expand(x):
return expandpath(x) if isinstance(x, str) else x
def _safely_expand(x, to_dict=False):
if isinstance(x, str):
return expandpath(x)
if to_dict and isinstance(x, Mapping):
return {k: _safely_expand(v, to_dict) for k, v in x.items()}
return x
9 changes: 2 additions & 7 deletions tests/test_basic_ops_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def test_del_unmapped_key(attmap_type, seed_data, delkey):
pytest.fail("Attempt to remove unmapped key hit exception: {}".format(e))


@pytest.mark.xfail(reason="attmap text representations have changed.")
@pytest.mark.parametrize("f_extra_checks_pair",
[(repr, []), (str, [lambda s, dt: s.startswith(dt.__name__)])])
def test_text(attmap_type, entries, f_extra_checks_pair):
Expand Down Expand Up @@ -153,13 +154,7 @@ def entries(self):
return dict([kv for kv, _ in self.DATA])

@staticmethod
@pytest.fixture("function", params=[k for ((k, _), _) in DATA])
def k(request):
""" Provide the requesting test case with a key into a mapping. """
return request.param

@staticmethod
@pytest.fixture("function")
@pytest.fixture(scope="function")
def m(attmap_type):
""" Build an AttMap instance of the given subtype. """
return get_att_map(attmap_type)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_basic_ops_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def entries(self):
return dict([kv for kv, _ in self.DATA])

@staticmethod
@pytest.fixture("function", params=[k for ((k, _), _) in DATA])
@pytest.fixture(scope="function", params=[k for ((k, _), _) in DATA])
def k(request):
""" Key to test """
return request.param
Expand Down