Пример #1
0
def _slistShape(slist, itemsize=None, shape=None):
    """_slistShape(slist, itemsize=None, shape=None)  computes the "natural"
    shape and itemsize of slist, and combines this with the specified
    itemsize and shape,  checking for consistency.

    Specifying an itemsize overrides the slist's natural itemsize.

    >>> _slistShape(["this","that"], itemsize=10)
    ((2,), 10)
    >>> _slistShape(["this","that"], itemsize=3)
    ((2,), 3)

    Specifying a shape checks for consistency against the slist's shape.

    >>> _slistShape(["this","that"], shape=(2,1,1))
    ((2, 1, 1), 4)
    >>> _slistShape(["this","that"], shape=(3,2))
    Traceback (most recent call last):
    ...    
    ValueError: Inconsistent list and shape
    
    """
    shape_items = _slistShape0(slist)
    if shape is None:
        shape = shape_items[0]
    else:
        if _gen.product(shape) != _gen.product(shape_items[0]):
            raise ValueError("Inconsistent list and shape")
    if itemsize is None:
        itemsize = shape_items[-1]
    else:
        pass  # specified itemsize => padded extension or silent truncation.
    return (shape, itemsize)
Пример #2
0
    def __init__(self, objects=None, shape=None, rank=None):
        """
        >>> ObjectArray(shape=(1,))
        ObjectArray([None])
        >>> ObjectArray(objects=['t','u'])
        ObjectArray(['t', 'u'])
        >>> ObjectArray(objects=['t','u'], shape=(3,))
        Traceback (most recent call last):
        ...
        ValueError: object list is not compatible with shape
        """
        if rank is None:
            rank = _getRank(objects)

        if objects is not None:
            oshape = _shapeFromNestedSequence(objects, rank)
            if shape is None:
                shape = oshape
            else:
                if _gen.product(shape) != _gen.product(oshape):
                    raise ValueError("object list is not compatible with shape")
            objects = _flatten(objects, rank)
        else:
            if shape is None:
                shape = (1,)

        if isinstance(objects, _gen.NDArray) and shape == ():
            objects = [objects[()]]

        if not isinstance(shape, (list, tuple)):
            shape = (shape,)

        _objarr.__init__(self, shape, objects)
Пример #3
0
    def __init__(self, objects=None, shape=None, rank=None):
        """
        >>> ObjectArray(shape=(1,))
        ObjectArray([None])
        >>> ObjectArray(objects=['t','u'])
        ObjectArray(['t', 'u'])
        >>> ObjectArray(objects=['t','u'], shape=(3,))
        Traceback (most recent call last):
        ...
        ValueError: object list is not compatible with shape
        """
        if rank is None:
            rank = _getRank(objects)

        if objects is not None:
            oshape = _shapeFromNestedSequence(objects, rank)
            if shape is None:
                shape = oshape
            else:
                if _gen.product(shape) != _gen.product(oshape):
                    raise ValueError(
                        "object list is not compatible with shape")
            objects = _flatten(objects, rank)
        else:
            if shape is None:
                shape = (1, )

        if isinstance(objects, _gen.NDArray) and shape == ():
            objects = [objects[()]]

        if not isinstance(shape, (list, tuple)):
            shape = (shape, )

        _objarr.__init__(self, shape, objects)
Пример #4
0
def _slistShape(slist, itemsize=None, shape=None):
    """_slistShape(slist, itemsize=None, shape=None)  computes the "natural"
    shape and itemsize of slist, and combines this with the specified
    itemsize and shape,  checking for consistency.

    Specifying an itemsize overrides the slist's natural itemsize.

    >>> _slistShape(["this","that"], itemsize=10)
    ((2,), 10)
    >>> _slistShape(["this","that"], itemsize=3)
    ((2,), 3)

    Specifying a shape checks for consistency against the slist's shape.

    >>> _slistShape(["this","that"], shape=(2,1,1))
    ((2, 1, 1), 4)
    >>> _slistShape(["this","that"], shape=(3,2))
    Traceback (most recent call last):
    ...    
    ValueError: Inconsistent list and shape
    
    """
    shape_items = _slistShape0(slist)
    if shape is None:
        shape = shape_items[0]
    else:
        if _gen.product(shape) != _gen.product(shape_items[0]):
            raise ValueError("Inconsistent list and shape")
    if itemsize is None:
        itemsize = shape_items[-1]
    else:
        pass  # specified itemsize => padded extension or silent truncation.
    return (shape, itemsize)
Пример #5
0
def fromfile(file, formats, shape=-1, names=None, byteorder=sys.byteorder, aligned=0):
    """Create an array from binary file data

    If file is a string then that file is opened, else it is assumed
    to be a file object. No options at the moment, all file positioning
    must be done prior to this function call with a file object

    >>> import testdata, sys
    >>> fd=open(testdata.filename)
    >>> fd.seek(2880*2)
    >>> r=fromfile(fd, formats='f8,i4,a5', shape=3, byteorder='big')
    >>> print r[0]
    (5.1000000000000005, 61, 'abcde')
    >>> r._shape
    (3,)
    """

    if isinstance(shape, types.IntType) or isinstance(shape, types.LongType):
        shape = (shape,)
    name = 0
    if isinstance(file, types.StringType):
        name = 1
        file = open(file, 'rb')
    try:
        size = os.fstat(file.fileno())[stat.ST_SIZE] - file.tell()
    except:
        size = os.path.getsize(file.name) - file.tell()

    dummy = array(None, formats=formats, shape=0, aligned=aligned)
    itemsize = dummy._itemsize

    if shape and itemsize:
        shapesize = _gen.product(shape)*itemsize
        if shapesize < 0:
            shape = list(shape)
            shape[ shape.index(-1) ] = size / -shapesize
            shape = tuple(shape)

    nbytes = _gen.product(shape)*itemsize

    if nbytes > size:
        raise ValueError(
                "Not enough bytes left in file for specified shape and type")

    # create the array
    _array = RecArray(None, formats=formats, shape=shape, names=names,
                byteorder=byteorder, aligned=aligned)
    nbytesread = file.readinto(_array._data)
    if nbytesread != nbytes:
        raise IOError("Didn't read as many bytes as expected")
    if name:
        file.close()
    return _array
Пример #6
0
def fromstring(datastring, formats, shape=0, names=None, byteorder=sys.byteorder, aligned=0):
    """ create a Record Array from binary data contained in a string"""
    _array = RecArray(chararray._stringToBuffer(datastring), formats, shape, names,
                byteorder=byteorder, aligned=aligned)
    if _gen.product(_array._shape)*_array._itemsize > len(datastring):
        raise ValueError("Insufficient input data.")
    else: 
        return _array
Пример #7
0
 def __init__(self, shape, objects=None):
     if objects is None:
         objects = [None] * _gen.product(shape)
     _gen.NDArray.__init__(self,
                           shape=shape,
                           buffer=_dummyBuffer,
                           itemsize=1)
     self._objects = objects[:]
Пример #8
0
 def _clone(self, shape):
     """returns an empty array identical to 'self' but with 'shape'
     """
     c = self.view()
     c._shape = shape
     c._strides = _stridesFromShape(shape)
     c._byteoffset = 0
     c._objects = [None] * _gen.product(shape)
     return c
Пример #9
0
 def _clone(self, shape):
     """returns an empty array identical to 'self' but with 'shape'
     """
     c = self.view()
     c._shape = shape
     c._strides = _stridesFromShape(shape)
     c._byteoffset = 0
     c._objects = [None] * _gen.product(shape)
     return c
Пример #10
0
def _stridesFromShape(shape):
    """_stridesFromShape() computes the strides which correspond to
    the specified shape."""

    if len(shape) > 1:
        head = [_gen.product(shape[1:])]
        tail = _stridesFromShape(shape[1:])
        head.extend(tail)
        return head
    else:
        return [1]
Пример #11
0
def _stridesFromShape(shape):

    """_stridesFromShape() computes the strides which correspond to
    the specified shape."""

    if len(shape) > 1:
        head = [_gen.product(shape[1:])]
        tail = _stridesFromShape(shape[1:])
        head.extend(tail)
        return head
    else:
        return [1]
Пример #12
0
def fromstring(datastring,
               formats,
               shape=0,
               names=None,
               byteorder=sys.byteorder,
               aligned=0):
    """ create a Record Array from binary data contained in a string"""
    _array = RecArray(chararray._stringToBuffer(datastring),
                      formats,
                      shape,
                      names,
                      byteorder=byteorder,
                      aligned=aligned)
    if _gen.product(_array._shape) * _array._itemsize > len(datastring):
        raise ValueError("Insufficient input data.")
    else:
        return _array
Пример #13
0
 def __init__(self, shape, objects=None):
     if objects is None:
         objects = [None] * _gen.product(shape)
     _gen.NDArray.__init__(self, shape=shape, buffer=_dummyBuffer, itemsize=1)
     self._objects = objects[:]
Пример #14
0
def fromfile(file,
             formats,
             shape=-1,
             names=None,
             byteorder=sys.byteorder,
             aligned=0):
    """Create an array from binary file data

    If file is a string then that file is opened, else it is assumed
    to be a file object. No options at the moment, all file positioning
    must be done prior to this function call with a file object

    >>> import testdata, sys
    >>> fd=open(testdata.filename)
    >>> fd.seek(2880*2)
    >>> r=fromfile(fd, formats='f8,i4,a5', shape=3, byteorder='big')
    >>> print r[0]
    (5.1000000000000005, 61, 'abcde')
    >>> r._shape
    (3,)
    """

    if isinstance(shape, types.IntType) or isinstance(shape, types.LongType):
        shape = (shape, )
    name = 0
    if isinstance(file, types.StringType):
        name = 1
        file = open(file, 'rb')
    try:
        size = os.fstat(file.fileno())[stat.ST_SIZE] - file.tell()
    except:
        size = os.path.getsize(file.name) - file.tell()

    dummy = array(None, formats=formats, shape=0, aligned=aligned)
    itemsize = dummy._itemsize

    if shape and itemsize:
        shapesize = _gen.product(shape) * itemsize
        if shapesize < 0:
            shape = list(shape)
            shape[shape.index(-1)] = size / -shapesize
            shape = tuple(shape)

    nbytes = _gen.product(shape) * itemsize

    if nbytes > size:
        raise ValueError(
            "Not enough bytes left in file for specified shape and type")

    # create the array
    _array = RecArray(None,
                      formats=formats,
                      shape=shape,
                      names=names,
                      byteorder=byteorder,
                      aligned=aligned)
    nbytesread = file.readinto(_array._data)
    if nbytesread != nbytes:
        raise IOError("Didn't read as many bytes as expected")
    if name:
        file.close()
    return _array