Coverage for beyonic.resources: 79%

# Coverage for beyonic.resources : 79%

## 29 statements 23 run 6 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | ''' ' API related resources ''' import sys, json ''' class GenericObject(dict): def __init__(self, id=None, api_key=None, **params): super(GenericObject, self).__init__() self._params = params object.__setattr__(self, 'api_key', api_key) if id: self['id'] = id """ Generic object class """ @classmethod def from_json(cls, json_string): """ Parses the given json_string """ obj = json.loads(json_string, object_hook=cls) return obj def __setattr__(self, prop, val): if prop[0] == '_' or prop in self.__dict__: return super(GenericObject, self).__setattr__(prop, val) else: self[prop] = val def __getattr__(self, prop): print self print type(self) print self.keys() if prop in self: return self[prop] else: raise AttributeError def __delitem__(self, prop): # on delete, setting value to None if prop in self: self[prop] = None def __repr__(self): ident_parts = [type(self).__name__] if isinstance(self.get('id'), dict): sid = self.get('id').get('id') ident_parts.append('id=%s' % (sid,)) elif isinstance(self.get('id'), basestring): ident_parts.append('id=%s' % (self.get('id'),)) unicode_repr = '<%s at %s> : %s' % ( ' '.join(ident_parts), hex(id(self)), str(self)) print unicode_repr if sys.version_info[0] < 3: return unicode_repr.encode('utf-8') else: return unicode_repr def __str__(self): if isinstance(self.get('id'), dict): self = self.get('id') return json.dumps(self, sort_keys=True, indent=2) ''' class GenericObject(dict): """ A dict subclass that provides access to its members as if they were attributes. Note that an attribute that has the same name as one of dict's existing method (``keys``, ``items``, etc.) will not be accessible as an attribute. """ @classmethod def from_json(cls, json_string): """ Parses the given json_string, returning GenericObject instances instead of dicts. """ return json.loads(json_string, object_hook=cls) def __setattr__(self, prop, val): if prop[0] == '_' or prop in self.__dict__: return super(GenericObject, self).__setattr__(prop, val) else: self[prop] = val def __getattr__(self, prop): """ Provides access to the members of the dict as attributes. """ if prop in self: return self[prop] else: raise AttributeError def set_client(self, client): self.__setattr__('_client', client) def save(self): data = self.copy() if '_client' in data: data['_client'] = None self._client.patch(**(data)) def __delitem__(self, prop): # on delete, setting value to None if prop in self: self[prop] = None ''' def __repr__(self): ident_parts = [type(self).__name__] if isinstance(self, dict): ident_parts.append('id=%s' % (self.get('id'),)) elif isinstance(self.get('id'), dict): sid = self.get('id').get('id') ident_parts.append('id=%s' % (sid,)) elif isinstance(self.get('id'), basestring): ident_parts.append('id=%s' % (self.get('id'),)) unicode_repr = '<%s at %s> : %s' % ( ' '.join(ident_parts), hex(id(self)), str(self)) if sys.version_info[0] < 3: return unicode_repr.encode('utf-8') else: return unicode_repr ''' def __str__(self): if isinstance(self.get('id'), dict): self = self.get('id') data = self.copy() if '_client' in data: data['_client'] = None return json.dumps(data, sort_keys=True, indent=2)