Ejemplo n.º 1
0
    def set_from_dict(self, values):
        for d in self.definition.definitions:
            if isinstance(d, Struct):
                self.values[d].set_from_dict(values[d])

            elif isinstance(d, Array) and isinstance(d.definition, Struct):
                for indices in NdArray.iterate(d.shape()):
                    value = NdArray.get(values[d], indices)
                    cache = NdArray.get(self.values[d], indices)
                    cache.set_from_dict(value)

            else:
                self.values[d] = values[d]
Ejemplo n.º 2
0
    def set_dirty(self, dirty, include_children=True):
        self.dirty = dirty

        if include_children:
            for d in self.definition.definitions:
                value = self.values[d]

                if isinstance(d, Struct):
                    value.set_dirty(dirty, include_children)

                if Array.is_array_of_structs(d):
                    for indices in NdArray.iterate(d.shape()):
                        NdArray.get(value,
                                    indices).set_dirty(dirty, include_children)
Ejemplo n.º 3
0
    def get_as_dict(self):
        data = {}

        for d in self.definition.definitions:
            value = self.values[d]

            if isinstance(d, Struct):
                value = self.values[d].get_as_dict()

            if Array.is_array_of_structs(d):
                value = np.zeros(d.shape()).tolist()

                for indices in NdArray.iterate(d.shape()):
                    cache = NdArray.get(self.values[d], indices)
                    NdArray.assign(value, indices, cache.get_as_dict())

            data[d] = value

        return data
Ejemplo n.º 4
0
    def __init__(self, definition):
        if type(definition) != Struct:
            raise BytesError(
                "ByteCaches can only be initialized with struct definitions")
        self.definition = definition
        self.values = {}
        self.dirty = False

        for i, d in enumerate(self.definition.definitions):
            value = None

            if isinstance(d, Struct):
                value = ByteCache(d)

            if Array.is_array_of_structs(d):
                value = np.zeros(d.shape()).tolist()

                for indices in NdArray.iterate(d.shape()):
                    NdArray.assign(value, indices, ByteCache(d.definition))

            self.values[d] = value
Ejemplo n.º 5
0
    def is_dirty(self, include_children=True):
        if not include_children:
            return self.dirty

        dirty = self.dirty

        for d in self.definition.definitions:
            if dirty:
                return True

            value = self.values[d]

            if isinstance(d, Struct):
                dirty = dirty or value.is_dirty(include_children)

            if Array.is_array_of_structs(d):
                for indices in NdArray.iterate(d.shape()):
                    dirty = dirty or NdArray.get(
                        value, indices).is_dirty(include_children)

        return dirty
Ejemplo n.º 6
0
    def to_bytes(self, values, path=()):
        if isinstance(self.definition, Scalar):
            return self.to_bytes_for_scalars(values, path)

        elif isinstance(self.definition, Vector):
            return self.to_bytes_for_vectors(values, path)

        elif isinstance(self.definition, Matrix):
            return self.to_bytes_for_matrices(values, path)

        else:
            bytez = bytearray()

            for indices in NdArray.iterate(self.shape()):
                bytez += self.definition.to_bytes(
                    NdArray.get(values, indices),
                    list(path) +
                    ["array" + "".join(["[{}]".format(i) for i in indices])])
                padding = (self.a - len(bytez) % self.a) % self.a
                bytez += bytearray(padding)

            return bytez
Ejemplo n.º 7
0
    def from_bytes(self, bytez):
        if isinstance(self.definition, Scalar):
            return self.from_bytes_for_scalars(bytez)

        elif isinstance(self.definition, Vector):
            return self.from_bytes_for_vectors(bytez)

        elif isinstance(self.definition, Matrix):
            return self.from_bytes_for_matrices(bytez)

        else:
            values = np.zeros(self.shape()).tolist()
            offset = 0
            size = self.definition.size()

            for indices in NdArray.iterate(self.shape()):
                NdArray.assign(
                    values, indices,
                    self.definition.from_bytes(bytez[offset:offset + size]))
                offset += size
                offset += (self.a - offset % self.a) % self.a  # bytes

            return values