Skip to content

Commit

Permalink
Allow subscript access on BaseData objects.
Browse files Browse the repository at this point in the history
This is a compatibility hack to allow us to switch from dictionary responses to
BaseData derived objects.

Refs ask#61.
  • Loading branch information
JNRowe committed Aug 30, 2011
1 parent 0b76ff2 commit e6ffc94
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions github2/core.py
@@ -1,5 +1,7 @@
import sys

from warnings import warn

from datetime import datetime
from dateutil import (parser, tz)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e6ffc94

Please sign in to comment.