Skip to content

Commit

Permalink
implemented non overridable default access method
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisantonellis committed May 4, 2018
1 parent 21bf457 commit 4baeebc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
26 changes: 18 additions & 8 deletions delimited/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def _unwrap(self, data):
def unwrap(self):
return self._unwrap(self)

def _access_handler(self, haystack, segment, path, i, kwargs={}):
# def _access_handler(self, haystack, segment, path, i, default=None, _update_function=None, _update_kwargs=None):
def _access_handler(self, haystack, segment, path, i, default=None):
return haystack[segment], None

def _access(self, path=None, create=False, _update_function=None, _update_kwargs={}):
Expand Down Expand Up @@ -139,13 +140,22 @@ def _access(self, path=None, create=False, _update_function=None, _update_kwargs

else:
function = self._access_handler
kwargs.update({
"kwargs": {
"create": create,
"_update_function": _update_function,
"_update_kwargs": _update_kwargs
}
})

# create closure for default method that access handler
# should call if no special logic applied
def default(**kwargs):
return haystack[segment], None

# return self._access_handler_no_override(**{
# "haystack": haystack,
# "segment": s,
# "path": path,
# "i": i,
# "_update_function": _update_function,
# "_update_kwargs": _update_kwargs
# })

kwargs.update({"default": default})

haystack, status = function(**kwargs)

Expand Down
20 changes: 16 additions & 4 deletions test/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,33 @@ def test_ref__subclassing(self):

class Foo(NestedDict):

def _access_handler(self, haystack, segment, path, i, kwargs):
def _access_handler(self, haystack, segment, path, i, default):

if isinstance(haystack[segment], Foo):
if isinstance(haystack[segment], Foo):
model = haystack[segment]
haystack = model.get(path.after(i))
status = {"child": True}
raise StopPathIteration(haystack, status)

return super()._access_handler(haystack, segment, path, i)
return default()

child = Foo({"k": "v"})
parent = Foo({"child": child})

value = parent.get("child")


def test_ref__subclassing2(self):

class Foo(NestedDict):

def _access_handler(self, haystack, segment, path, i, default):
return default()

child = Foo({"k": "v"})
parent = Foo({"child": child})

parent.get("child")

# get

def test_get__string_arg__returns_value(self):
Expand Down

0 comments on commit 4baeebc

Please sign in to comment.