From e6ffc94a01ce437bf3abd3294f9ab9df1f22a878 Mon Sep 17 00:00:00 2001 From: James Rowe Date: Tue, 30 Aug 2011 12:20:16 +0100 Subject: [PATCH] Allow subscript access on BaseData objects. This is a compatibility hack to allow us to switch from dictionary responses to BaseData derived objects. Refs #61. --- github2/core.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/github2/core.py b/github2/core.py index 87cc117..4a1b619 100644 --- a/github2/core.py +++ b/github2/core.py @@ -1,5 +1,7 @@ import sys +from warnings import warn + from datetime import datetime from dateutil import (parser, tz) @@ -261,6 +263,29 @@ def iterate(self): class BaseData(object): __metaclass__ = BaseDataType + def __getitem__(self, key): + """Access objects's attribute using subscript notation + + This is here purely to maintain compatibility when switching ``dict`` + responses to ``BaseData`` derived objects. + """ + warn("Subscript access on %r is deprecated, use object attributes" + % self.__class__.__name__, DeprecationWarning) + if not key in self._meta.keys(): + raise KeyError(key) + return getattr(self, key) + + def __setitem__(self, key, value): + """Update object's attribute using subscript notation + + :see: ``BaseData.__getitem__`` + """ + warn("Subscript access on %r is deprecated, use object attributes" + % self.__class__.__name__, DeprecationWarning) + if not key in self._meta.keys(): + raise KeyError(key) + setattr(self, key, value) + def repr_string(string): """Shorten string for use in repr() output