示例#1
0
 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('_')]
示例#2
0
    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)))
示例#3
0
    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)))
示例#4
0
    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)))
示例#5
0
    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)))
示例#6
0
def test_camel_snake_camel():
    assert snake_to_camel(camel_to_snake("countryCode")), "countryCode"
示例#7
0
def test_snake_camel_snake():
    assert camel_to_snake(snake_to_camel("country_code")) == "country_code"
示例#8
0
def test_camel_to_snake(value, expected):
    assert camel_to_snake(value) == expected
示例#9
0
 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('_')]
示例#10
0
def test_camel_snake_camel():
    assert snake_to_camel(camel_to_snake("countryCode")), "countryCode"
示例#11
0
def test_snake_camel_snake():
    assert camel_to_snake(snake_to_camel("country_code")) == "country_code"
示例#12
0
def test_camel_to_snake(value, expected):
    assert camel_to_snake(value) == expected