From 12f8a689963517e863d9ab4e743fa90ee723f43f Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Thu, 14 Dec 2017 12:52:33 +0100 Subject: Add dict attribute access --- mastodon/Mastodon.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 9c97b79..722c4d5 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -1392,6 +1392,20 @@ class Mastodon: return (date_time_utc - epoch_utc).total_seconds() + @staticmethod + def __json_allow_dict_attrs(json_object): + """ + Makes it possible to use attribute notation to access a dicts + elements, while still allowing the dict to act as a dict. + """ + class AttribAccessDict(dict): + def __getattr__(self, attr): + return self[attr] + + if isinstance(json_object, dict): + return AttribAccessDict(json_object) + return json_object + @staticmethod def __json_date_parse(json_object): """ @@ -1428,6 +1442,7 @@ class Mastodon: def __json_hooks(json_object): json_object = Mastodon.__json_date_parse(json_object) json_object = Mastodon.__json_id_to_bignum(json_object) + json_object = Mastodon.__json_allow_dict_attrs(json_object) return json_object def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): -- cgit v1.2.3