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 _additional_data(data): if data is None: return None element = E.additionalData() for key, value in data.items(): if key == 'birth_date': try: value = value.strftime('%Y-%m-%d') except AttributeError: pass element.append(E(snake_to_camel(key), value)) return element
def _extension_additional_data(data): if data is None: return None return E.extensionAdditionalData( E(snake_to_camel(key), value) for key, value in data.items())
def test_camel_snake_camel(): assert snake_to_camel(camel_to_snake("countryCode")), "countryCode"
def test_snake_to_camel(value, expected): assert snake_to_camel(value) == expected
def test_snake_camel_snake(): assert camel_to_snake(snake_to_camel("country_code")) == "country_code"
def _extension_additional_data(data): if data is None: return None return E.extensionAdditionalData(E(snake_to_camel(key), value) for key, value in data.items())
def __init__(self, obj=None, **kwargs): self._obj = obj self._attrs = dict((snake_to_camel(key), value) for (key, value) in kwargs.items())
def __init__(self, obj=None, **kwargs): self._obj = obj self._attrs = dict( (snake_to_camel(key), value) for (key, value) in kwargs.items())