Ejemplo n.º 1
0
    def insert(self, index: int, element):
        """
        Insert an element at a given position.

        >>> array = Array()
        >>> array.insert(0, Element())
        """

        from refract.refraction import refract
        self.content.insert(index, refract(element))
Ejemplo n.º 2
0
    def append(self, element):
        """
        Append an element onto the array.

        >>> array = Array()
        >>> array.append('test')
        """

        from refract.refraction import refract
        self.content.append(refract(element))
Ejemplo n.º 3
0
    def __init__(self,
                 meta: Metadata = None,
                 attributes: Attributes = None,
                 content: List[Member] = None) -> None:
        super(Object, self).__init__(meta=meta, attributes=attributes)

        if isinstance(content, (dict)):
            from refract.refraction import refract
            self.content = refract(content).content
        else:
            self.content = content
Ejemplo n.º 4
0
    def index(self, element: Element) -> int:
        """
        Return the index in the array of the first item whose value is element.
        It is an error if there is no such item.

        >>> element = String('hello')
        >>> array = Array(content=[element])
        >>> array.index(element)
        0
        """

        from refract.refraction import refract
        return self.content.index(refract(element))
Ejemplo n.º 5
0
    def deserialise_dict(self, element_dict):
        if isinstance(element_dict, (int, str, float)):
            return refract(element_dict)

        if isinstance(element_dict, list):
            return Array(
                content=[self.deserialise_dict(e) for e in element_dict])

        if isinstance(element_dict, dict) and 'key' not in element_dict \
                and 'element' not in element_dict:
            return Array(
                content=[self.deserialise_dict(e) for e in element_dict])

        return super(LegacyJSONDeserialiser,
                     self).deserialise_dict(element_dict)
Ejemplo n.º 6
0
    def __getitem__(self, key) -> Element:
        """
        Returns the value for the given key

        >>> key = String(content='id')
        >>> value = String(content='Hello')
        >>> obj = Object(content=[Member(key=key, value=value)])
        >>> obj['id']
        String(content='Hello')
        """

        if self.content:
            from refract.refraction import refract
            refracted_key = refract(key)

            for member in self.content:
                if member.key == refracted_key:
                    return member.content.value

        raise KeyError(key)
Ejemplo n.º 7
0
    def __delitem__(self, key):
        """
        Deletes the member for the given key

        >>> key = String(content='id')
        >>> value = String(content='Hello')
        >>> obj = Object(content=[Member(key=key, value=value)])
        >>> del obj[key]
        """

        if self.content:
            from refract.refraction import refract
            refracted_key = refract(key)

            for member in self.content:
                if member.key == refracted_key:
                    self.content.remove(member)
                    return

        raise KeyError(key)
Ejemplo n.º 8
0
 def __setitem__(self, item, value):
     from refract.refraction import refract
     value = refract(value)
     self.attributes.__setitem__(item, value)
Ejemplo n.º 9
0
 def classes(self, new_value):
     from refract.refraction import refract
     self.meta.classes = refract(new_value)
Ejemplo n.º 10
0
 def links(self, new_value):
     from refract.refraction import refract
     self.meta.links = refract(new_value)
Ejemplo n.º 11
0
 def ref(self, new_value):
     from refract.refraction import refract
     self.meta.ref = refract(new_value)
Ejemplo n.º 12
0
 def description(self, new_value):
     from refract.refraction import refract
     self.meta.description = refract(new_value)
Ejemplo n.º 13
0
 def title(self, new_value):
     from refract.refraction import refract
     self.meta.title = refract(new_value)
Ejemplo n.º 14
0
 def id(self, new_value):
     from refract.refraction import refract
     self.meta.id = refract(new_value)
Ejemplo n.º 15
0
 def __contains__(self, element) -> bool:
     from refract.refraction import refract
     return refract(element) in self.content
Ejemplo n.º 16
0
 def value(self, value: Element):
     from refract.refraction import refract
     self.content = KeyValuePair(key=self.key, value=refract(value))
Ejemplo n.º 17
0
 def key(self, key: Element):
     from refract.refraction import refract
     self.content = KeyValuePair(key=refract(key), value=self.value)