def __dir__(self): attrs = set( list(self.__dict__.keys()) + [camel_to_snake(key) for key in self._attrs.keys()]) if self._obj is not None: attrs.update( camel_to_snake(t.tag) for t in self._obj.iterchildren()) return [attr for attr in attrs if not attr.startswith('_')]
def __getattr__(self, attr): """ Magic for returning an attribute. Will try the attributes of the wrapper class first, then attributes in self._attrs, then the attributes of the wrapped objectified element. Will try a camelCased version of the snake_cased input if the attribute contains an underscore. This means foo.company_name will return the same as foo.companyName. """ if "_" in attr: attr = snake_to_camel(attr) if attr in self.__dict__: # Check ourselves first to avoid infinite loops return getattr(self, attr) try: return self._attrs[attr] except KeyError: if self._obj is not None: try: return self._obj[attr] except (AttributeError, KeyError): pass raise AttributeError("Model has no attribute '%s' (tried %r)" % (camel_to_snake(attr), dir(self)))
def getter(self): try: return self._attrs[attr] except KeyError: if self.get_elem() is not None: try: return self.get_elem()[attr].text except (AttributeError, KeyError): pass raise AttributeError("Model has no attribute '%s' (tried %r)" % (camel_to_snake(attr), dir(self)))
def test_camel_snake_camel(): assert snake_to_camel(camel_to_snake("countryCode")), "countryCode"
def test_snake_camel_snake(): assert camel_to_snake(snake_to_camel("country_code")) == "country_code"
def test_camel_to_snake(value, expected): assert camel_to_snake(value) == expected
def __dir__(self): attrs = set(list(self.__dict__.keys()) + [camel_to_snake(key) for key in self._attrs.keys()]) if self._obj is not None: attrs.update(camel_to_snake(t.tag) for t in self._obj.iterchildren()) return [attr for attr in attrs if not attr.startswith('_')]