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

Added get(key, default=..) method to AttributeProxy, to allow dict like checking of existing variables #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/jsonapi_client/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ def __getattr__(self, item):
except KeyError:
raise AttributeError

def get(self, item, default=object()):
# Can't use default=None as .get('foo', None) is common, so use
# a unique empty object with a reference check
if default is self.get.__defaults__[0]:
self.__getattr__(item)

item_json = jsonify_attribute_name(item)
return self[item_json] if hasattr(self, item_json) else default

def __setattr__(self, key, value):
if key == '_target_object':
return super().__setattr__(key, value)
Expand Down