From: Alfredo Deza Date: Tue, 17 Mar 2015 15:02:59 +0000 (-0400) Subject: create an obj util that can convert objects to_dict X-Git-Tag: v1.2.2~6^2~7 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=1b26b9799b15366ecce18dc66fd3fec6933d6e00;p=radosgw-agent.git create an obj util that can convert objects to_dict Signed-off-by: Alfredo Deza --- diff --git a/radosgw_agent/util/obj.py b/radosgw_agent/util/obj.py new file mode 100644 index 0000000..30d63c0 --- /dev/null +++ b/radosgw_agent/util/obj.py @@ -0,0 +1,22 @@ + + +def to_dict(_object, **extra_keys): + """ + A utility to convert an object with attributes to a dictionary with the + optional feature of slapping extra_keys. Because extra_keys can be + optionally set, it is assumed that any keys that clash will get + overwritten. + + Private methods (anything that starts with `_`) are ignored. + """ + dictified_obj = {} + for k, v in _object.__dict__.items(): + if not k.startswith('_'): + # get key + value = extra_keys.pop(k, v) + dictified_obj[k] = value + if extra_keys: + for k, v in extra_keys.items(): + dictified_obj[k] = v + + return dictified_obj