Python numpy.compat.long() Examples
The following are 30
code examples of numpy.compat.long().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy.compat
, or try the search function
.
Example #1
Source File: test_regression.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_object_array_self_reference(self): # Object arrays with references to themselves can cause problems a = np.array(0, dtype=object) a[()] = a assert_raises(TypeError, int, a) assert_raises(TypeError, long, a) assert_raises(TypeError, float, a) assert_raises(TypeError, oct, a) assert_raises(TypeError, hex, a) # Test the same for a circular reference. b = np.array(a, dtype=object) a[()] = b assert_raises(TypeError, int, a) # Numpy has no tp_traverse currently, so circular references # cannot be detected. So resolve it: a[()] = 0 # This was causing a to become like the above a = np.array(0, dtype=object) a[...] += 1 assert_equal(a, 1)
Example #2
Source File: test_classes.py From recruit with Apache License 2.0 | 6 votes |
def test_truediv(Poly): # true division is valid only if the denominator is a Number and # not a python bool. p1 = Poly([1,2,3]) p2 = p1 * 5 for stype in np.ScalarType: if not issubclass(stype, Number) or issubclass(stype, bool): continue s = stype(5) assert_poly_almost_equal(op.truediv(p2, s), p1) assert_raises(TypeError, op.truediv, s, p2) for stype in (int, long, float): s = stype(5) assert_poly_almost_equal(op.truediv(p2, s), p1) assert_raises(TypeError, op.truediv, s, p2) for stype in [complex]: s = stype(5, 0) assert_poly_almost_equal(op.truediv(p2, s), p1) assert_raises(TypeError, op.truediv, s, p2) for s in [tuple(), list(), dict(), bool(), np.array([1])]: assert_raises(TypeError, op.truediv, p2, s) assert_raises(TypeError, op.truediv, s, p2) for ptype in classes: assert_raises(TypeError, op.truediv, p2, ptype(1))
Example #3
Source File: functions.py From Computable with MIT License | 6 votes |
def take(array, indices, axis=0, outarr=None, clipmode=RAISE): array = np.asarray(array) if isinstance(axis, (int, long, np.integer)): res = array.take(indices, axis, outarr, clipmode) if outarr is None: return res return else: def_axes = list(range(array.ndim)) for x in axis: def_axes.remove(x) axis = list(axis) + def_axes work = array.transpose(axis) res = work[indices] if outarr is None: return res outarr[...] = res return
Example #4
Source File: test_classes.py From vnpy_crypto with MIT License | 6 votes |
def check_truediv(Poly): # true division is valid only if the denominator is a Number and # not a python bool. p1 = Poly([1,2,3]) p2 = p1 * 5 for stype in np.ScalarType: if not issubclass(stype, Number) or issubclass(stype, bool): continue s = stype(5) assert_poly_almost_equal(op.truediv(p2, s), p1) assert_raises(TypeError, op.truediv, s, p2) for stype in (int, long, float): s = stype(5) assert_poly_almost_equal(op.truediv(p2, s), p1) assert_raises(TypeError, op.truediv, s, p2) for stype in [complex]: s = stype(5, 0) assert_poly_almost_equal(op.truediv(p2, s), p1) assert_raises(TypeError, op.truediv, s, p2) for s in [tuple(), list(), dict(), bool(), np.array([1])]: assert_raises(TypeError, op.truediv, p2, s) assert_raises(TypeError, op.truediv, s, p2) for ptype in classes: assert_raises(TypeError, op.truediv, p2, ptype(1))
Example #5
Source File: functions.py From Computable with MIT License | 6 votes |
def put(array, indices, values, axis=0, clipmode=RAISE): if not isinstance(array, np.ndarray): raise TypeError("put only works on subclass of ndarray") work = asarray(array) if axis == 0: if array.ndim == 1: work.put(indices, values, clipmode) else: work[indices] = values elif isinstance(axis, (int, long, np.integer)): work = work.swapaxes(0, axis) work[indices] = values work = work.swapaxes(0, axis) else: def_axes = list(range(work.ndim)) for x in axis: def_axes.remove(x) axis = list(axis)+def_axes work = work.transpose(axis) work[indices] = values work = work.transpose(axis)
Example #6
Source File: test_regression.py From vnpy_crypto with MIT License | 5 votes |
def test_signed_integer_division_overflow(self): # Ticket #1317. def test_type(t): min = np.array([np.iinfo(t).min]) min //= -1 with np.errstate(divide="ignore"): for t in (np.int8, np.int16, np.int32, np.int64, int, np.long): test_type(t)
Example #7
Source File: defchararray.py From vnpy_crypto with MIT License | 5 votes |
def __new__(subtype, shape, itemsize=1, unicode=False, buffer=None, offset=0, strides=None, order='C'): global _globalvar if unicode: dtype = unicode_ else: dtype = string_ # force itemsize to be a Python long, since using NumPy integer # types results in itemsize.itemsize being used as the size of # strings in the new array. itemsize = long(itemsize) if sys.version_info[0] >= 3 and isinstance(buffer, _unicode): # On Py3, unicode objects do not have the buffer interface filler = buffer buffer = None else: filler = None _globalvar = 1 if buffer is None: self = ndarray.__new__(subtype, shape, (dtype, itemsize), order=order) else: self = ndarray.__new__(subtype, shape, (dtype, itemsize), buffer=buffer, offset=offset, strides=strides, order=order) if filler is not None: self[...] = filler _globalvar = 0 return self
Example #8
Source File: defchararray.py From vnpy_crypto with MIT License | 5 votes |
def zfill(a, width): """ Return the numeric string left-filled with zeros Calls `str.zfill` element-wise. Parameters ---------- a : array_like, {str, unicode} Input array. width : int Width of string to left-fill elements in `a`. Returns ------- out : ndarray, {str, unicode} Output array of str or unicode, depending on input type See also -------- str.zfill """ a_arr = numpy.asarray(a) width_arr = numpy.asarray(width) size = long(numpy.max(width_arr.flat)) return _vec_string( a_arr, (a_arr.dtype.type, size), 'zfill', (width_arr,))
Example #9
Source File: defchararray.py From vnpy_crypto with MIT License | 5 votes |
def center(a, width, fillchar=' '): """ Return a copy of `a` with its elements centered in a string of length `width`. Calls `str.center` element-wise. Parameters ---------- a : array_like of str or unicode width : int The length of the resulting strings fillchar : str or unicode, optional The padding character to use (default is space). Returns ------- out : ndarray Output array of str or unicode, depending on input types See also -------- str.center """ a_arr = numpy.asarray(a) width_arr = numpy.asarray(width) size = long(numpy.max(width_arr.flat)) if numpy.issubdtype(a_arr.dtype, numpy.string_): fillchar = asbytes(fillchar) return _vec_string( a_arr, (a_arr.dtype.type, size), 'center', (width_arr, fillchar))
Example #10
Source File: test_regression.py From vnpy_crypto with MIT License | 5 votes |
def test_object_array_nested(self): # but is fine with a reference to a different array a = np.array(0, dtype=object) b = np.array(0, dtype=object) a[()] = b assert_equal(int(a), int(0)) assert_equal(long(a), long(0)) assert_equal(float(a), float(0)) if sys.version_info.major == 2: # in python 3, this falls back on operator.index, which fails on # on dtype=object assert_equal(oct(a), oct(0)) assert_equal(hex(a), hex(0))
Example #11
Source File: test_regression.py From vnpy_crypto with MIT License | 5 votes |
def test_object_array_self_reference(self): # Object arrays with references to themselves can cause problems a = np.array(0, dtype=object) a[()] = a assert_raises(RecursionError, int, a) assert_raises(RecursionError, long, a) assert_raises(RecursionError, float, a) if sys.version_info.major == 2: # in python 3, this falls back on operator.index, which fails on # on dtype=object assert_raises(RecursionError, oct, a) assert_raises(RecursionError, hex, a) a[()] = None
Example #12
Source File: test_regression.py From vnpy_crypto with MIT License | 5 votes |
def test_array_side_effect(self): # The second use of itemsize was throwing an exception because in # ctors.c, discover_itemsize was calling PyObject_Length without # checking the return code. This failed to get the length of the # number 2, and the exception hung around until something checked # PyErr_Occurred() and returned an error. assert_equal(np.dtype('S10').itemsize, 10) np.array([['abc', 2], ['long ', '0123456789']], dtype=np.string_) assert_equal(np.dtype('S10').itemsize, 10)
Example #13
Source File: defchararray.py From vnpy_crypto with MIT License | 5 votes |
def ljust(a, width, fillchar=' '): """ Return an array with the elements of `a` left-justified in a string of length `width`. Calls `str.ljust` element-wise. Parameters ---------- a : array_like of str or unicode width : int The length of the resulting strings fillchar : str or unicode, optional The character to use for padding Returns ------- out : ndarray Output array of str or unicode, depending on input type See also -------- str.ljust """ a_arr = numpy.asarray(a) width_arr = numpy.asarray(width) size = long(numpy.max(width_arr.flat)) if numpy.issubdtype(a_arr.dtype, numpy.string_): fillchar = asbytes(fillchar) return _vec_string( a_arr, (a_arr.dtype.type, size), 'ljust', (width_arr, fillchar))
Example #14
Source File: defchararray.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def __new__(subtype, shape, itemsize=1, unicode=False, buffer=None, offset=0, strides=None, order='C'): global _globalvar if unicode: dtype = unicode_ else: dtype = string_ # force itemsize to be a Python long, since using Numpy integer # types results in itemsize.itemsize being used as the size of # strings in the new array. itemsize = long(itemsize) if sys.version_info[0] >= 3 and isinstance(buffer, _unicode): # On Py3, unicode objects do not have the buffer interface filler = buffer buffer = None else: filler = None _globalvar = 1 if buffer is None: self = ndarray.__new__(subtype, shape, (dtype, itemsize), order=order) else: self = ndarray.__new__(subtype, shape, (dtype, itemsize), buffer=buffer, offset=offset, strides=strides, order=order) if filler is not None: self[...] = filler _globalvar = 0 return self
Example #15
Source File: defchararray.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def zfill(a, width): """ Return the numeric string left-filled with zeros Calls `str.zfill` element-wise. Parameters ---------- a : array_like, {str, unicode} Input array. width : int Width of string to left-fill elements in `a`. Returns ------- out : ndarray, {str, unicode} Output array of str or unicode, depending on input type See also -------- str.zfill """ a_arr = numpy.asarray(a) width_arr = numpy.asarray(width) size = long(numpy.max(width_arr.flat)) return _vec_string( a_arr, (a_arr.dtype.type, size), 'zfill', (width_arr,))
Example #16
Source File: defchararray.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def rjust(a, width, fillchar=' '): """ Return an array with the elements of `a` right-justified in a string of length `width`. Calls `str.rjust` element-wise. Parameters ---------- a : array_like of str or unicode width : int The length of the resulting strings fillchar : str or unicode, optional The character to use for padding Returns ------- out : ndarray Output array of str or unicode, depending on input type See also -------- str.rjust """ a_arr = numpy.asarray(a) width_arr = numpy.asarray(width) size = long(numpy.max(width_arr.flat)) if numpy.issubdtype(a_arr.dtype, numpy.string_): fillchar = asbytes(fillchar) return _vec_string( a_arr, (a_arr.dtype.type, size), 'rjust', (width_arr, fillchar))
Example #17
Source File: defchararray.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def ljust(a, width, fillchar=' '): """ Return an array with the elements of `a` left-justified in a string of length `width`. Calls `str.ljust` element-wise. Parameters ---------- a : array_like of str or unicode width : int The length of the resulting strings fillchar : str or unicode, optional The character to use for padding Returns ------- out : ndarray Output array of str or unicode, depending on input type See also -------- str.ljust """ a_arr = numpy.asarray(a) width_arr = numpy.asarray(width) size = long(numpy.max(width_arr.flat)) if numpy.issubdtype(a_arr.dtype, numpy.string_): fillchar = asbytes(fillchar) return _vec_string( a_arr, (a_arr.dtype.type, size), 'ljust', (width_arr, fillchar))
Example #18
Source File: defchararray.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def multiply(a, i): """ Return (a * i), that is string multiple concatenation, element-wise. Values in `i` of less than 0 are treated as 0 (which yields an empty string). Parameters ---------- a : array_like of str or unicode i : array_like of ints Returns ------- out : ndarray Output array of str or unicode, depending on input types """ a_arr = numpy.asarray(a) i_arr = numpy.asarray(i) if not issubclass(i_arr.dtype.type, integer): raise ValueError("Can only multiply by integers") out_size = _get_num_chars(a_arr) * max(long(i_arr.max()), 0) return _vec_string( a_arr, (a_arr.dtype.type, out_size), '__mul__', (i_arr,))
Example #19
Source File: test_function_base.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def _check_inverse_of_slicing(self, indices): a_del = delete(self.a, indices) nd_a_del = delete(self.nd_a, indices, axis=1) msg = 'Delete failed for obj: %r' % indices # NOTE: The cast should be removed after warning phase for bools if not isinstance(indices, (slice, int, long, np.integer)): indices = np.asarray(indices, dtype=np.intp) indices = indices[(indices >= 0) & (indices < 5)] assert_array_equal(setxor1d(a_del, self.a[indices, ]), self.a, err_msg=msg) xor = setxor1d(nd_a_del[0,:, 0], self.nd_a[0, indices, 0]) assert_array_equal(xor, self.nd_a[0,:, 0], err_msg=msg)
Example #20
Source File: test_regression.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_array_side_effect(self): # The second use of itemsize was throwing an exception because in # ctors.c, discover_itemsize was calling PyObject_Length without # checking the return code. This failed to get the length of the # number 2, and the exception hung around until something checked # PyErr_Occurred() and returned an error. assert_equal(np.dtype('S10').itemsize, 10) np.array([['abc', 2], ['long ', '0123456789']], dtype=np.string_) assert_equal(np.dtype('S10').itemsize, 10)
Example #21
Source File: test_regression.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_signed_integer_division_overflow(self): # Ticket #1317. def test_type(t): min = np.array([np.iinfo(t).min]) min //= -1 with np.errstate(divide="ignore"): for t in (np.int8, np.int16, np.int32, np.int64, np.int, np.long): test_type(t)
Example #22
Source File: test_shape_base.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_r1array(self): """ Test to make sure equivalent Travis O's r1array function """ assert_(atleast_1d(3).shape == (1,)) assert_(atleast_1d(3j).shape == (1,)) assert_(atleast_1d(long(3)).shape == (1,)) assert_(atleast_1d(3.0).shape == (1,)) assert_(atleast_1d([[2, 3], [4, 5]]).shape == (2, 2))
Example #23
Source File: test_regression.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_permutation_longs(self): np.random.seed(1234) a = np.random.permutation(12) np.random.seed(1234) b = np.random.permutation(long(12)) assert_array_equal(a, b)
Example #24
Source File: test_return_logical.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def check_function(self, t): assert_(t(True) == 1, repr(t(True))) assert_(t(False) == 0, repr(t(False))) assert_(t(0) == 0) assert_(t(None) == 0) assert_(t(0.0) == 0) assert_(t(0j) == 0) assert_(t(1j) == 1) assert_(t(234) == 1) assert_(t(234.6) == 1) assert_(t(long(234)) == 1) assert_(t(234.6 + 3j) == 1) assert_(t('234') == 1) assert_(t('aaa') == 1) assert_(t('') == 0) assert_(t([]) == 0) assert_(t(()) == 0) assert_(t({}) == 0) assert_(t(t) == 1) assert_(t(-234) == 1) assert_(t(10 ** 100) == 1) assert_(t([234]) == 1) assert_(t((234,)) == 1) assert_(t(array(234)) == 1) assert_(t(array([234])) == 1) assert_(t(array([[234]])) == 1) assert_(t(array([234], 'b')) == 1) assert_(t(array([234], 'h')) == 1) assert_(t(array([234], 'i')) == 1) assert_(t(array([234], 'l')) == 1) assert_(t(array([234], 'f')) == 1) assert_(t(array([234], 'd')) == 1) assert_(t(array([234 + 3j], 'F')) == 1) assert_(t(array([234], 'D')) == 1) assert_(t(array(0)) == 0) assert_(t(array([0])) == 0) assert_(t(array([[0]])) == 0) assert_(t(array([0j])) == 0) assert_(t(array([1])) == 1) assert_raises(ValueError, t, array([0, 0]))
Example #25
Source File: test_return_integer.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def check_function(self, t): assert_(t(123) == 123, repr(t(123))) assert_(t(123.6) == 123) assert_(t(long(123)) == 123) assert_(t('123') == 123) assert_(t(-123) == -123) assert_(t([123]) == 123) assert_(t((123,)) == 123) assert_(t(array(123)) == 123) assert_(t(array([123])) == 123) assert_(t(array([[123]])) == 123) assert_(t(array([123], 'b')) == 123) assert_(t(array([123], 'h')) == 123) assert_(t(array([123], 'i')) == 123) assert_(t(array([123], 'l')) == 123) assert_(t(array([123], 'B')) == 123) assert_(t(array([123], 'f')) == 123) assert_(t(array([123], 'd')) == 123) #assert_raises(ValueError, t, array([123],'S3')) assert_raises(ValueError, t, 'abc') assert_raises(IndexError, t, []) assert_raises(IndexError, t, ()) assert_raises(Exception, t, t) assert_raises(Exception, t, {}) if t.__doc__.split()[0] in ['t8', 's8']: assert_raises(OverflowError, t, 100000000000000000000000) assert_raises(OverflowError, t, 10000000011111111111111.23)
Example #26
Source File: test_return_real.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def check_function(self, t): if t.__doc__.split()[0] in ['t0', 't4', 's0', 's4']: err = 1e-5 else: err = 0.0 assert_(abs(t(234) - 234.0) <= err) assert_(abs(t(234.6) - 234.6) <= err) assert_(abs(t(long(234)) - 234.0) <= err) assert_(abs(t('234') - 234) <= err) assert_(abs(t('234.6') - 234.6) <= err) assert_(abs(t(-234) + 234) <= err) assert_(abs(t([234]) - 234) <= err) assert_(abs(t((234,)) - 234.) <= err) assert_(abs(t(array(234)) - 234.) <= err) assert_(abs(t(array([234])) - 234.) <= err) assert_(abs(t(array([[234]])) - 234.) <= err) assert_(abs(t(array([234], 'b')) + 22) <= err) assert_(abs(t(array([234], 'h')) - 234.) <= err) assert_(abs(t(array([234], 'i')) - 234.) <= err) assert_(abs(t(array([234], 'l')) - 234.) <= err) assert_(abs(t(array([234], 'B')) - 234.) <= err) assert_(abs(t(array([234], 'f')) - 234.) <= err) assert_(abs(t(array([234], 'd')) - 234.) <= err) if t.__doc__.split()[0] in ['t0', 't4', 's0', 's4']: assert_(t(1e200) == t(1e300)) # inf #assert_raises(ValueError, t, array([234], 'S1')) assert_raises(ValueError, t, 'abc') assert_raises(IndexError, t, []) assert_raises(IndexError, t, ()) assert_raises(Exception, t, t) assert_raises(Exception, t, {}) try: r = t(10 ** 400) assert_(repr(r) in ['inf', 'Infinity'], repr(r)) except OverflowError: pass
Example #27
Source File: test_type_check.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_basic(self): assert_(np.isscalar(3)) assert_(not np.isscalar([3])) assert_(not np.isscalar((3,))) assert_(np.isscalar(3j)) assert_(np.isscalar(long(10))) assert_(np.isscalar(4.0))
Example #28
Source File: user_array.py From recruit with Apache License 2.0 | 5 votes |
def __long__(self): return self._scalarfunc(long)
Example #29
Source File: arrayterator.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def __getitem__(self, index): """ Return a new arrayterator. """ # Fix index, handling ellipsis and incomplete slices. if not isinstance(index, tuple): index = (index,) fixed = [] length, dims = len(index), len(self.shape) for slice_ in index: if slice_ is Ellipsis: fixed.extend([slice(None)] * (dims-length+1)) length = len(fixed) elif isinstance(slice_, (int, long)): fixed.append(slice(slice_, slice_+1, 1)) else: fixed.append(slice_) index = tuple(fixed) if len(index) < dims: index += (slice(None),) * (dims-len(index)) # Return a new arrayterator object. out = self.__class__(self.var, self.buf_size) for i, (start, stop, step, slice_) in enumerate( zip(self.start, self.stop, self.step, index)): out.start[i] = start + (slice_.start or 0) out.step[i] = step * (slice_.step or 1) out.stop[i] = start + (slice_.stop or stop-start) out.stop[i] = min(stop, out.stop[i]) return out
Example #30
Source File: arrayterator.py From vnpy_crypto with MIT License | 5 votes |
def __getitem__(self, index): """ Return a new arrayterator. """ # Fix index, handling ellipsis and incomplete slices. if not isinstance(index, tuple): index = (index,) fixed = [] length, dims = len(index), self.ndim for slice_ in index: if slice_ is Ellipsis: fixed.extend([slice(None)] * (dims-length+1)) length = len(fixed) elif isinstance(slice_, (int, long)): fixed.append(slice(slice_, slice_+1, 1)) else: fixed.append(slice_) index = tuple(fixed) if len(index) < dims: index += (slice(None),) * (dims-len(index)) # Return a new arrayterator object. out = self.__class__(self.var, self.buf_size) for i, (start, stop, step, slice_) in enumerate( zip(self.start, self.stop, self.step, index)): out.start[i] = start + (slice_.start or 0) out.step[i] = step * (slice_.step or 1) out.stop[i] = start + (slice_.stop or stop-start) out.stop[i] = min(stop, out.stop[i]) return out