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))
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))
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
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))
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)
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)
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)
def __setitem__(self, item, value): from refract.refraction import refract value = refract(value) self.attributes.__setitem__(item, value)
def classes(self, new_value): from refract.refraction import refract self.meta.classes = refract(new_value)
def links(self, new_value): from refract.refraction import refract self.meta.links = refract(new_value)
def ref(self, new_value): from refract.refraction import refract self.meta.ref = refract(new_value)
def description(self, new_value): from refract.refraction import refract self.meta.description = refract(new_value)
def title(self, new_value): from refract.refraction import refract self.meta.title = refract(new_value)
def id(self, new_value): from refract.refraction import refract self.meta.id = refract(new_value)
def __contains__(self, element) -> bool: from refract.refraction import refract return refract(element) in self.content
def value(self, value: Element): from refract.refraction import refract self.content = KeyValuePair(key=self.key, value=refract(value))
def key(self, key: Element): from refract.refraction import refract self.content = KeyValuePair(key=refract(key), value=self.value)