本文整理匯總了Python中array.array方法的典型用法代碼示例。如果您正苦於以下問題:Python array.array方法的具體用法?Python array.array怎麽用?Python array.array使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類array
的用法示例。
在下文中一共展示了array.array方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __xor__
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def __xor__(self, other):
'''
Take a bitwise 'XOR' of the bit vector on which the method is invoked with
the argument bit vector. Return the result as a new bit vector. If the two
bit vectors are not of the same size, pad the shorter one with zeros from the
left.
'''
if self.size < other.size:
bv1 = self._resize_pad_from_left(other.size - self.size)
bv2 = other
elif self.size > other.size:
bv1 = self
bv2 = other._resize_pad_from_left(self.size - other.size)
else:
bv1 = self
bv2 = other
res = BitVector( size = bv1.size )
lpb = map(operator.__xor__, bv1.vector, bv2.vector)
res.vector = array.array( 'H', lpb )
return res
示例2: __and__
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def __and__(self, other):
'''
Take a bitwise 'AND' of the bit vector on which the method is invoked with
the argument bit vector. Return the result as a new bit vector. If the two
bit vectors are not of the same size, pad the shorter one with zeros from the
left.
'''
if self.size < other.size:
bv1 = self._resize_pad_from_left(other.size - self.size)
bv2 = other
elif self.size > other.size:
bv1 = self
bv2 = other._resize_pad_from_left(self.size - other.size)
else:
bv1 = self
bv2 = other
res = BitVector( size = bv1.size )
lpb = map(operator.__and__, bv1.vector, bv2.vector)
res.vector = array.array( 'H', lpb )
return res
示例3: __or__
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def __or__(self, other):
'''
Take a bitwise 'OR' of the bit vector on which the method is invoked with the
argument bit vector. Return the result as a new bit vector. If the two bit
vectors are not of the same size, pad the shorter one with zero's from the
left.
'''
if self.size < other.size:
bv1 = self._resize_pad_from_left(other.size - self.size)
bv2 = other
elif self.size > other.size:
bv1 = self
bv2 = other._resize_pad_from_left(self.size - other.size)
else:
bv1 = self
bv2 = other
res = BitVector( size = bv1.size )
lpb = map(operator.__or__, bv1.vector, bv2.vector)
res.vector = array.array( 'H', lpb )
return res
示例4: set_value
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def set_value(self, *args, **kwargs):
'''
You can call set_value() to change the bit pattern associated with
a previously constructed bitvector object:
bv = BitVector(intVal = 7, size =16)
print(bv) # 0000000000000111
bv.set_value(intVal = 45)
print(bv) # 101101
You can think of this method as carrying out an in-place resetting
of the bit array in a bitvector. The method does not return
anything. The allowable modes for changing the internally stored
bit array for a bitvector are the same as for the constructor.
'''
self.__init__( *args, **kwargs )
# For backward compatibility:
示例5: _new_alloc_handle
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def _new_alloc_handle(shape, ctx, delay_alloc, dtype=mx_real_t):
"""Return a new handle with specified shape and context.
Empty handle is only used to hold results.
Returns
-------
handle
A new empty `NDArray` handle.
"""
hdl = NDArrayHandle()
check_call(_LIB.MXNDArrayCreateEx(
c_array_buf(mx_uint, native_array('I', shape)),
mx_uint(len(shape)),
ctypes.c_int(ctx.device_typeid),
ctypes.c_int(ctx.device_id),
ctypes.c_int(int(delay_alloc)),
ctypes.c_int(int(_DTYPE_NP_TO_MX[np.dtype(dtype).type])),
ctypes.byref(hdl)))
return hdl
示例6: __rsub__
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def __rsub__(self, other):
"""x.__rsub__(y) <=> y-x
Only `NDArray` is supported for now.
Example
-------
>>> x = mx.nd.ones((2,3))*3
>>> y = mx.nd.ones((2,3))
>>> x.__rsub__(y).asnumpy()
array([[-2., -2., -2.],
[-2., -2., -2.]], dtype=float32)
"""
if isinstance(other, Number):
return _internal._RMinusScalar(self, scalar=other)
else:
raise TypeError('type %s not supported' % str(type(other)))
示例7: __rmod__
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def __rmod__(self, other):
"""x.__rmod__(y) <=> y%x
Only `NDArray` is supported for now.
Example
-------
>>> x = mx.nd.ones((2,3))*3
>>> y = mx.nd.ones((2,3))
>>> x.__rmod__(y).asnumpy()
array([[ 1., 1., 1.,
[ 1., 1., 1., dtype=float32)
"""
if isinstance(other, Number):
return _internal._RModScalar(self, scalar=other)
else:
raise TypeError('type %s not supported' % str(type(other)))
示例8: __neg__
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def __neg__(self):
"""x.__neg__() <=> -x
Numerical negative, element-wise.
Example
-------
>>> a = mx.sym.Variable('a')
>>> a
<Symbol a>
>>> -a
<Symbol _mulscalar0>
>>> a_neg = a.__neg__()
>>> c = a_neg*b
>>> ex = c.eval(ctx=mx.cpu(), a=mx.nd.ones([2,3]), b=mx.nd.ones([2,3]))
>>> ex[0].asnumpy()
array([[-1., -1., -1.],
[-1., -1., -1.]], dtype=float32)
"""
return self.__mul__(-1.0)
示例9: pad_from_right
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def pad_from_right( self, n ):
'''
You can pad a bitvector at its right end with a designated number of
zeros with this method. This method returns the bitvector object on
which it is invoked. So you can think of this method as carrying
out an in-place extension of a bitvector (although, under the hood,
the extension is carried out by giving a new longer _vector
attribute to the bitvector object).
'''
new_str = str( self ) + '0'*n
bitlist = list(map( int, list(new_str) ))
self.size = len( bitlist )
two_byte_ints_needed = (len(bitlist) + 15) // 16
self.vector = array.array( 'H', [0]*two_byte_ints_needed )
list(map( self._setbit, enumerate(bitlist), bitlist))
示例10: handleResize
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def handleResize(self, signum, frame):
h,w=array('h', ioctl(sys.stderr,termios.TIOCGWINSZ,'\0'*8))[:2]
self.term_width = w
示例11: download_and_parse_mnist_file
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def download_and_parse_mnist_file(file_name, datadir=None, force=False):
file_name = maybe_download_mnist_file(file_name, datadir=datadir,
force=force)
# Open the file and unzip it if necessary
if os.path.splitext(file_name)[1] == '.gz':
open_fn = gzip.open
else:
open_fn = open
# Parse the file
with open_fn(file_name, 'rb') as file_descriptor:
header = file_descriptor.read(4)
assert len(header) == 4
zeros, data_type, n_dims = struct.unpack('>HBB', header)
assert zeros == 0
hex_to_data_type = {
0x08: 'B',
0x09: 'b',
0x0b: 'h',
0x0c: 'i',
0x0d: 'f',
0x0e: 'd'}
data_type = hex_to_data_type[data_type]
dim_sizes = struct.unpack(
'>' + 'I' * n_dims,
file_descriptor.read(4 * n_dims))
data = array.array(data_type, file_descriptor.read())
data.byteswap()
desired_items = functools.reduce(operator.mul, dim_sizes)
assert len(data) == desired_items
return np.array(data).reshape(dim_sizes)
示例12: _handle_resize
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def _handle_resize(self, signum=None, frame=None):
"""Tries to catch resize signals sent from the terminal."""
h, w = array('h', ioctl(self.fd, termios.TIOCGWINSZ, '\0' * 8))[:2]
self.term_width = w
示例13: __invert__
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def __invert__(self):
'''
Invert the bits in the bit vector on which the method is invoked
and return the result as a new bit vector.
'''
res = BitVector( size = self.size )
lpb = list(map( operator.__inv__, self.vector ))
res.vector = array.array( 'H' )
for i in range(len(lpb)):
res.vector.append( lpb[i] & 0x0000FFFF )
return res
示例14: shift_right
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def shift_right( self, n ):
'''
Call this method if you want to shift in-place a bitvector to the right
non-circularly. As a bitvector is shifted non-circularly to the
right, the exposed bit positions at the left end are filled with
zeros. This method returns the bitvector object on which it is
invoked. This is to allow for chained invocations of the method.
'''
for i in range(n):
self.shift_right_by_one()
return self
# Allow array like subscripting for getting and setting:
示例15: _new_alloc_handle
# 需要導入模塊: import array [as 別名]
# 或者: from array import array [as 別名]
def _new_alloc_handle(stype, shape, ctx, delay_alloc, dtype, aux_types, aux_shapes=None):
"""Return a new handle with specified storage type, shape, dtype and context.
Empty handle is only used to hold results
Returns
-------
handle
A new empty ndarray handle
"""
hdl = NDArrayHandle()
for aux_t in aux_types:
if np.dtype(aux_t) != np.dtype("int64"):
raise NotImplementedError("only int64 is supported for aux types")
aux_type_ids = [int(_DTYPE_NP_TO_MX[np.dtype(aux_t).type]) for aux_t in aux_types]
aux_shapes = [(0,) for aux_t in aux_types] if aux_shapes is None else aux_shapes
aux_shape_lens = [len(aux_shape) for aux_shape in aux_shapes]
aux_shapes = py_sum(aux_shapes, ())
num_aux = mx_uint(len(aux_types))
check_call(_LIB.MXNDArrayCreateSparseEx(
ctypes.c_int(int(_STORAGE_TYPE_STR_TO_ID[stype])),
c_array_buf(mx_uint, native_array('I', shape)),
mx_uint(len(shape)),
ctypes.c_int(ctx.device_typeid),
ctypes.c_int(ctx.device_id),
ctypes.c_int(int(delay_alloc)),
ctypes.c_int(int(_DTYPE_NP_TO_MX[np.dtype(dtype).type])),
num_aux,
c_array_buf(ctypes.c_int, native_array('i', aux_type_ids)),
c_array_buf(mx_uint, native_array('I', aux_shape_lens)),
c_array_buf(mx_uint, native_array('I', aux_shapes)),
ctypes.byref(hdl)))
return hdl