Esempio n. 1
0
class PyTypedArray:
    """
    PyTypedArray is the base class that wraps the JavaScript TypedArray objects.
    The derived objects provides an interface to the JavaScript array objects:
        PyUint8ClampedArray     [Uint8ClampedArray]
        PyUint8Array            [Uint8Array]
        PyUint16Array           [Uint16Array]
        PyUint32Array           [Uint32Array]
        PyInt8Array             [Int8Array]
        PyInt16Array            [Int16Array]
        PyInt32Array            [Int32Array]
        PyFloat32Array          [Float32Array]
        PyFloat64Array          [Float64Array]
     The PyTypedArray interface include index syntax, iteration, and math operations.
     The module contains an Ndarray class to instantiate N-dimensional arrays, and PyImageData and PyImageMatrix classes that provide an interface to canvas ImageData.
    """
    def __init__(self, data=None, offset=None, length=None, typedarray=None):
        """
        The PyTypedArray is instantiated with either the array size, an array of the TypedArray or Python type, or an existing ArrayBuffer to view, which creates a new TypedArray of size and included data as the specified type. Optional arguments include offset index at which ArrayBuffer data is inserted and length of an ArrayBuffer.
        """
        if data:
            if isinstance(data, int):
                if pyjs_mode.optimized:
                    self.__data = JS("""new @{{typedarray}}(@{{data}})""")
                else:
                    self.__data = JS(
                        """new @{{typedarray}}(@{{data}}['valueOf']())""")
            elif isinstance(data, (list, tuple)):
                if pyjs_mode.optimized:
                    self.__data = JS(
                        """new @{{typedarray}}(@{{data}}['getArray']())""")
                else:
                    data = [dat.valueOf() for dat in data]
                    self.__data = JS(
                        """new @{{typedarray}}(@{{data}}['getArray']())""")
            elif isinstance(data, PyTypedArray):
                self.__data = JS(
                    """new @{{typedarray}}(@{{data}}['__data'])""")
            else:  #TypedArray or ArrayBuffer
                if offset is None and length is None:
                    self.__data = JS("""new @{{typedarray}}(@{{data}})""")
                else:
                    if offset is None:
                        offset = 0
                    if length is None:
                        self.__data = JS(
                            """new @{{typedarray}}(@{{data}}, @{{offset}})""")
                    else:
                        self.__data = JS(
                            """new @{{typedarray}}(@{{data}}, @{{offset}}, @{{length}})"""
                        )
        else:
            self.__data = None

    def __str__(self):
        """
        Return string representation of PyTypedArray object.
        """
        return self.__data.toString()

    def __iter__(self):
        """
        Iterate over PyTypedArray object.
        """
        index = 0
        while index < self.__data.length:
            yield self[index]
            index += 1

    def __getitem__(self, index):
        """
        Get TypedArray element by index.
        """
        return JS("""@{{int}}(@{{self}}['__data'][@{{index}}]);""")

    def __setitem__(self, index, value):
        """
        Set TypedArray element by index.
        """
        if pyjs_mode.optimized:
            JS("""@{{self}}['__data'][@{{index}}]=@{{value}};""")
        else:
            value = value.valueOf()
            JS("""@{{self}}['__data'][@{{index}}]=@{{value}};""")
        return None

    def __len__(self):
        """
        Get TypedArray array length.
        """
        return self.__data.length

    def set(self, data, offset=0):
        """
        Set data to the array. Arguments: data is a list of either the TypedArray or Python type, offset is the start index where data will be set (defaults to 0).
        """
        if isinstance(data, (list, tuple)):
            if pyjs_mode.optimized:
                self.__data.set(data.getArray(), offset)
            else:
                data = [dat.valueOf() for dat in data]
                self.__data.set(data.getArray(), offset)
        elif isinstance(data, PyTypedArray):
            self.__data.set(data.__data, offset)

    def subarray(self, begin, end=None):
        """
        Retrieve a subarray of the array. The subarray is a TypedArray and is a view of the derived array. Arguments begin and optional end (defaults to array end) are the index spanning the subarray.
        """
        if end is None:
            end = self.__data.length
        array = self.__data.subarray(begin, end)
        pytypedarray = self.__class__()
        pytypedarray.__data = array
        return pytypedarray

    def getLength(self):
        """
        Return array.length attribute.
        """
        return self.__data.length

    def getByteLength(self):
        """
        Return array.byteLength attribute.
        """
        return self.__data.byteLength

    def getBuffer(self):
        """
        Return array.buffer attribute.
        """
        return self.__data.buffer

    def getByteOffset(self):
        """
        Return array.byteOffset attribute.
        """
        return self.__data.byteOffset

    def getBytesPerElement(self):
        """
        Return array.BYTES_PER_ELEMENT attribute.
        """
        return self.__data.BYTES_PER_ELEMENT

    def getArray(self):
        """
        Return JavaScript TypedArray.
        """
        return self.__data

    def setArray(self, array):
        """
        Set JavaScript TypedArray.
        """
        self.__data = array
        return None
class PyTypedArray:

    """
    PyTypedArray is the base class that wraps the JavaScript TypedArray objects.
    The derived objects provides an interface to the JavaScript array objects:
        PyUint8ClampedArray     [Uint8ClampedArray]
        PyUint8Array            [Uint8Array]
        PyUint16Array           [Uint16Array]
        PyUint32Array           [Uint32Array]
        PyInt8Array             [Int8Array]
        PyInt16Array            [Int16Array]
        PyInt32Array            [Int32Array]
        PyFloat32Array          [Float32Array]
        PyFloat64Array          [Float64Array]
     The PyTypedArray interface include index syntax, iteration, and math operations.
     The module contains an Ndarray class to instantiate N-dimensional arrays, and PyImageData and PyImageMatrix classes that provide an interface to canvas ImageData.
    """

    def __init__(self, data=None, offset=None, length=None, typedarray=None):
        """
        The PyTypedArray is instantiated with either the array size, an array of the TypedArray or Python type, or an existing ArrayBuffer to view, which creates a new TypedArray of size and included data as the specified type. Optional arguments include offset index at which ArrayBuffer data is inserted and length of an ArrayBuffer.
        """
        if data:
            if isinstance(data, int):
                if pyjs_mode.optimized:
                    self.__data = JS("""new @{{typedarray}}(@{{data}})""")
                else:
                    self.__data = JS("""new @{{typedarray}}(@{{data}}['valueOf']())""")
            elif isinstance(data, (list,tuple)):
                if pyjs_mode.optimized:
                    self.__data = JS("""new @{{typedarray}}(@{{data}}['getArray']())""")
                else:
                    data = [dat.valueOf() for dat in data]
                    self.__data = JS("""new @{{typedarray}}(@{{data}}['getArray']())""")
            elif isinstance(data, PyTypedArray):
                self.__data = JS("""new @{{typedarray}}(@{{data}}['__data'])""")
            else:   #TypedArray or ArrayBuffer
                if offset is None and length is None:
                    self.__data = JS("""new @{{typedarray}}(@{{data}})""")
                else:
                    if offset is None:
                        offset = 0
                    if length is None:
                        self.__data = JS("""new @{{typedarray}}(@{{data}}, @{{offset}})""")
                    else:
                        self.__data = JS("""new @{{typedarray}}(@{{data}}, @{{offset}}, @{{length}})""")
        else:
            self.__data = None

    def __str__(self):
        """
        Return string representation of PyTypedArray object.
        """
        return self.__data.toString()

    def __iter__(self):
        """
        Iterate over PyTypedArray object.
        """
        index = 0
        while index < self.__data.length:
            yield self[index]
            index += 1

    def __getitem__(self, index):
        """
        Get TypedArray element by index.
        """
        return JS("""@{{int}}(@{{self}}['__data'][@{{index}}]);""")

    def __setitem__(self, index, value):
        """
        Set TypedArray element by index.
        """
        if pyjs_mode.optimized:
            JS("""@{{self}}['__data'][@{{index}}]=@{{value}};""")
        else:
            value = value.valueOf()
            JS("""@{{self}}['__data'][@{{index}}]=@{{value}};""")
        return None

    def __len__(self):
        """
        Get TypedArray array length.
        """
        return self.__data.length

    def set(self, data, offset=0):
        """
        Set data to the array. Arguments: data is a list of either the TypedArray or Python type, offset is the start index where data will be set (defaults to 0).
        """
        if isinstance(data, (list,tuple)):
            if pyjs_mode.optimized:
                self.__data.set(data.getArray(), offset)
            else:
                data = [dat.valueOf() for dat in data]
                self.__data.set(data.getArray(), offset)
        elif isinstance(data, PyTypedArray):
            self.__data.set(data.__data, offset)

    def subarray(self, begin, end=None):
        """
        Retrieve a subarray of the array. The subarray is a TypedArray and is a view of the derived array. Arguments begin and optional end (defaults to array end) are the index spanning the subarray.
        """
        if end is None:
            end = self.__data.length
        array = self.__data.subarray(begin, end)
        pytypedarray = self.__class__()
        pytypedarray.__data = array
        return pytypedarray

    def getLength(self):
        """
        Return array.length attribute.
        """
        return self.__data.length

    def getByteLength(self):
        """
        Return array.byteLength attribute.
        """
        return self.__data.byteLength

    def getBuffer(self):
        """
        Return array.buffer attribute.
        """
        return self.__data.buffer

    def getByteOffset(self):
        """
        Return array.byteOffset attribute.
        """
        return self.__data.byteOffset

    def getBytesPerElement(self):
        """
        Return array.BYTES_PER_ELEMENT attribute.
        """
        return self.__data.BYTES_PER_ELEMENT

    def getArray(self):
        """
        Return JavaScript TypedArray.
        """
        return self.__data

    def setArray(self, array):
        """
        Set JavaScript TypedArray.
        """
        self.__data = array
        return None