Python numpy.compat.asbytes() Examples
The following are 30
code examples of numpy.compat.asbytes().
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_core.py From lambda-packs with MIT License | 6 votes |
def test_fillvalue(self): # Yet more fun with the fill_value data = masked_array([1, 2, 3], fill_value=-999) series = data[[0, 2, 1]] assert_equal(series._fill_value, data._fill_value) mtype = [('f', float), ('s', '|S3')] x = array([(1, 'a'), (2, 'b'), (pi, 'pi')], dtype=mtype) x.fill_value = 999 assert_equal(x.fill_value.item(), [999., asbytes('999')]) assert_equal(x['f'].fill_value, 999) assert_equal(x['s'].fill_value, asbytes('999')) x.fill_value = (9, '???') assert_equal(x.fill_value.item(), (9, asbytes('???'))) assert_equal(x['f'].fill_value, 9) assert_equal(x['s'].fill_value, asbytes('???')) x = array([1, 2, 3.1]) x.fill_value = 999 assert_equal(np.asarray(x.fill_value).dtype, float) assert_equal(x.fill_value, 999.) assert_equal(x._fill_value, np.array(999.))
Example #2
Source File: _iotools.py From lambda-packs with MIT License | 6 votes |
def __init__(self, delimiter=None, comments=asbytes('#'), autostrip=True): self.comments = comments # Delimiter is a character if isinstance(delimiter, unicode): delimiter = delimiter.encode('ascii') if (delimiter is None) or _is_bytes_like(delimiter): delimiter = delimiter or None _handyman = self._delimited_splitter # Delimiter is a list of field widths elif hasattr(delimiter, '__iter__'): _handyman = self._variablewidth_splitter idx = np.cumsum([0] + list(delimiter)) delimiter = [slice(i, j) for (i, j) in zip(idx[:-1], idx[1:])] # Delimiter is a single integer elif int(delimiter): (_handyman, delimiter) = ( self._fixedwidth_splitter, int(delimiter)) else: (_handyman, delimiter) = (self._delimited_splitter, None) self.delimiter = delimiter if autostrip: self._handyman = self.autostrip(_handyman) else: self._handyman = _handyman #
Example #3
Source File: test_format.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_bad_header(): # header of length less than 2 should fail s = BytesIO() assert_raises(ValueError, format.read_array_header_1_0, s) s = BytesIO(asbytes('1')) assert_raises(ValueError, format.read_array_header_1_0, s) # header shorter than indicated size should fail s = BytesIO(asbytes('\x01\x00')) assert_raises(ValueError, format.read_array_header_1_0, s) # headers without the exact keys required should fail d = {"shape": (1, 2), "descr": "x"} s = BytesIO() format.write_array_header_1_0(s, d) assert_raises(ValueError, format.read_array_header_1_0, s) d = {"shape": (1, 2), "fortran_order": False, "descr": "x", "extrakey": -1} s = BytesIO() format.write_array_header_1_0(s, d) assert_raises(ValueError, format.read_array_header_1_0, s)
Example #4
Source File: _iotools.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def __init__(self, delimiter=None, comments=asbytes('#'), autostrip=True): self.comments = comments # Delimiter is a character if isinstance(delimiter, unicode): delimiter = delimiter.encode('ascii') if (delimiter is None) or _is_bytes_like(delimiter): delimiter = delimiter or None _handyman = self._delimited_splitter # Delimiter is a list of field widths elif hasattr(delimiter, '__iter__'): _handyman = self._variablewidth_splitter idx = np.cumsum([0] + list(delimiter)) delimiter = [slice(i, j) for (i, j) in zip(idx[:-1], idx[1:])] # Delimiter is a single integer elif int(delimiter): (_handyman, delimiter) = ( self._fixedwidth_splitter, int(delimiter)) else: (_handyman, delimiter) = (self._delimited_splitter, None) self.delimiter = delimiter if autostrip: self._handyman = self.autostrip(_handyman) else: self._handyman = _handyman #
Example #5
Source File: test_format.py From lambda-packs with MIT License | 6 votes |
def test_bad_header(): # header of length less than 2 should fail s = BytesIO() assert_raises(ValueError, format.read_array_header_1_0, s) s = BytesIO(asbytes('1')) assert_raises(ValueError, format.read_array_header_1_0, s) # header shorter than indicated size should fail s = BytesIO(asbytes('\x01\x00')) assert_raises(ValueError, format.read_array_header_1_0, s) # headers without the exact keys required should fail d = {"shape": (1, 2), "descr": "x"} s = BytesIO() format.write_array_header_1_0(s, d) assert_raises(ValueError, format.read_array_header_1_0, s) d = {"shape": (1, 2), "fortran_order": False, "descr": "x", "extrakey": -1} s = BytesIO() format.write_array_header_1_0(s, d) assert_raises(ValueError, format.read_array_header_1_0, s)
Example #6
Source File: test_mrecords.py From lambda-packs with MIT License | 6 votes |
def test_exotic_formats(self): # Test that 'exotic' formats are processed properly easy = mrecarray(1, dtype=[('i', int), ('s', '|S8'), ('f', float)]) easy[0] = masked assert_equal(easy.filled(1).item(), (1, asbytes('1'), 1.)) solo = mrecarray(1, dtype=[('f0', '<f8', (2, 2))]) solo[0] = masked assert_equal(solo.filled(1).item(), np.array((1,), dtype=solo.dtype).item()) mult = mrecarray(2, dtype="i4, (2,3)float, float") mult[0] = masked mult[1] = (1, 1, 1) mult.filled(0) assert_equal_records(mult.filled(0), np.array([(0, 0, 0), (1, 1, 1)], dtype=mult.dtype))
Example #7
Source File: test_return_character.py From lambda-packs with MIT License | 6 votes |
def check_function(self, t): tname = t.__doc__.split()[0] if tname in ['t0', 't1', 's0', 's1']: assert_(t(23) == asbytes('2')) r = t('ab') assert_(r == asbytes('a'), repr(r)) r = t(array('ab')) assert_(r == asbytes('a'), repr(r)) r = t(array(77, 'u1')) assert_(r == asbytes('M'), repr(r)) #assert_(_raises(ValueError, t, array([77,87]))) #assert_(_raises(ValueError, t, array(77))) elif tname in ['ts', 'ss']: assert_(t(23) == asbytes('23 '), repr(t(23))) assert_(t('123456789abcdef') == asbytes('123456789a')) elif tname in ['t5', 's5']: assert_(t(23) == asbytes('23 '), repr(t(23))) assert_(t('ab') == asbytes('ab '), repr(t('ab'))) assert_(t('123456789abcdef') == asbytes('12345')) else: raise NotImplementedError
Example #8
Source File: test_mrecords.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_exotic_formats(self): # Test that 'exotic' formats are processed properly easy = mrecarray(1, dtype=[('i', int), ('s', '|S8'), ('f', float)]) easy[0] = masked assert_equal(easy.filled(1).item(), (1, asbytes('1'), 1.)) solo = mrecarray(1, dtype=[('f0', '<f8', (2, 2))]) solo[0] = masked assert_equal(solo.filled(1).item(), np.array((1,), dtype=solo.dtype).item()) mult = mrecarray(2, dtype="i4, (2,3)float, float") mult[0] = masked mult[1] = (1, 1, 1) mult.filled(0) assert_equal_records(mult.filled(0), np.array([(0, 0, 0), (1, 1, 1)], dtype=mult.dtype))
Example #9
Source File: test_mrecords.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def setup(self): # Generic setup _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(list(map(asbytes, ['one', 'two', 'three'])), mask=[0, 0, 1], dtype='|S8') ddtype = [('a', int), ('b', float), ('c', '|S8')] mrec = fromarrays([_a, _b, _c], dtype=ddtype, fill_value=(asbytes('99999'), asbytes('99999.'), asbytes('N/A'))) nrec = recfromarrays((_a._data, _b._data, _c._data), dtype=ddtype) self.data = (mrec, nrec, ddtype)
Example #10
Source File: test_mrecords.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_tolist(self): # Test tolist. _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(['one', 'two', 'three'], mask=[1, 0, 0], dtype='|S8') ddtype = [('a', int), ('b', float), ('c', '|S8')] mrec = fromarrays([_a, _b, _c], dtype=ddtype, fill_value=(99999, 99999., 'N/A')) assert_equal(mrec.tolist(), [(1, 1.1, None), (2, 2.2, asbytes('two')), (None, None, asbytes('three'))])
Example #11
Source File: test__iotools.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_int64_dtype(self): "Check that int64 integer types can be specified" converter = StringConverter(np.int64, default=0) val = asbytes("-9223372036854775807") assert_(converter(val) == -9223372036854775807) val = asbytes("9223372036854775807") assert_(converter(val) == 9223372036854775807)
Example #12
Source File: _iotools.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def str2bool(value): """ Tries to transform a string supposed to represent a boolean to a boolean. Parameters ---------- value : str The string that is transformed to a boolean. Returns ------- boolval : bool The boolean representation of `value`. Raises ------ ValueError If the string is not 'True' or 'False' (case independent) Examples -------- >>> np.lib._iotools.str2bool('TRUE') True >>> np.lib._iotools.str2bool('false') False """ value = value.upper() if value == asbytes('TRUE'): return True elif value == asbytes('FALSE'): return False else: raise ValueError("Invalid boolean")
Example #13
Source File: test__iotools.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_keep_missing_values(self): "Check that we're not losing missing values" converter = StringConverter(int, default=0, missing_values=asbytes("N/A")) assert_equal( converter.missing_values, set(asbytes_nested(['', 'N/A'])))
Example #14
Source File: test__iotools.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_missing(self): "Tests the use of missing values." converter = StringConverter(missing_values=(asbytes('missing'), asbytes('missed'))) converter.upgrade(asbytes('0')) assert_equal(converter(asbytes('0')), 0) assert_equal(converter(asbytes('')), converter.default) assert_equal(converter(asbytes('missing')), converter.default) assert_equal(converter(asbytes('missed')), converter.default) try: converter('miss') except ValueError: pass
Example #15
Source File: test__iotools.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_keep_default_zero(self): "Check that we don't lose a default of 0" converter = StringConverter(int, default=0, missing_values=asbytes("N/A")) assert_equal(converter.default, 0)
Example #16
Source File: test__iotools.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_upgrademapper(self): "Tests updatemapper" dateparser = _bytes_to_date StringConverter.upgrade_mapper(dateparser, date(2000, 1, 1)) convert = StringConverter(dateparser, date(2000, 1, 1)) test = convert(asbytes('2001-01-01')) assert_equal(test, date(2001, 1, 1)) test = convert(asbytes('2009-01-01')) assert_equal(test, date(2009, 1, 1)) test = convert(asbytes('')) assert_equal(test, date(2000, 1, 1))
Example #17
Source File: test__iotools.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_uint64_dtype(self): "Check that uint64 integer types can be specified" converter = StringConverter(np.uint64, default=0) val = asbytes("9223372043271415339") assert_(converter(val) == 9223372043271415339)
Example #18
Source File: test_io.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def writelines(self, lines): BytesIO.writelines(self, [asbytes(s) for s in lines])
Example #19
Source File: test_mrecords.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_get(self): # Tests fields retrieval base = self.base.copy() mbase = base.view(mrecarray) # As fields.......... for field in ('a', 'b', 'c'): assert_equal(getattr(mbase, field), mbase[field]) assert_equal(base[field], mbase[field]) # as elements ....... mbase_first = mbase[0] assert_(isinstance(mbase_first, mrecarray)) assert_equal(mbase_first.dtype, mbase.dtype) assert_equal(mbase_first.tolist(), (1, 1.1, asbytes('one'))) # Used to be mask, now it's recordmask assert_equal(mbase_first.recordmask, nomask) assert_equal(mbase_first._mask.item(), (False, False, False)) assert_equal(mbase_first['a'], mbase['a'][0]) mbase_last = mbase[-1] assert_(isinstance(mbase_last, mrecarray)) assert_equal(mbase_last.dtype, mbase.dtype) assert_equal(mbase_last.tolist(), (None, None, None)) # Used to be mask, now it's recordmask assert_equal(mbase_last.recordmask, True) assert_equal(mbase_last._mask.item(), (True, True, True)) assert_equal(mbase_last['a'], mbase['a'][-1]) assert_((mbase_last['a'] is masked)) # as slice .......... mbase_sl = mbase[:2] assert_(isinstance(mbase_sl, mrecarray)) assert_equal(mbase_sl.dtype, mbase.dtype) # Used to be mask, now it's recordmask assert_equal(mbase_sl.recordmask, [0, 1]) assert_equal_records(mbase_sl.mask, np.array([(False, False, False), (True, True, True)], dtype=mbase._mask.dtype)) assert_equal_records(mbase_sl, base[:2].view(mrecarray)) for field in ('a', 'b', 'c'): assert_equal(getattr(mbase_sl, field), base[:2][field])
Example #20
Source File: test_io.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def write(self, s): BytesIO.write(self, asbytes(s))
Example #21
Source File: test_io.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def __init__(self, s=""): BytesIO.__init__(self, asbytes(s))
Example #22
Source File: test__iotools.py From lambda-packs with MIT License | 5 votes |
def test_keep_missing_values(self): "Check that we're not losing missing values" converter = StringConverter(int, default=0, missing_values=asbytes("N/A")) assert_equal( converter.missing_values, set(asbytes_nested(['', 'N/A'])))
Example #23
Source File: _iotools.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def _fixedwidth_splitter(self, line): if self.comments is not None: line = line.split(self.comments)[0] line = line.strip(asbytes("\r\n")) if not line: return [] fixed = self.delimiter slices = [slice(i, i + fixed) for i in range(0, len(line), fixed)] return [line[s] for s in slices] #
Example #24
Source File: _iotools.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def _delimited_splitter(self, line): if self.comments is not None: line = line.split(self.comments)[0] line = line.strip(asbytes(" \r\n")) if not line: return [] return line.split(self.delimiter) #
Example #25
Source File: _iotools.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def _is_bytes_like(obj): """ Check whether obj behaves like a bytes object. """ try: obj + asbytes('') except (TypeError, ValueError): return False return True
Example #26
Source File: test_core.py From lambda-packs with MIT License | 5 votes |
def test_check_on_scalar(self): # Test _check_fill_value set to valid and invalid values _check_fill_value = np.ma.core._check_fill_value fval = _check_fill_value(0, int) assert_equal(fval, 0) fval = _check_fill_value(None, int) assert_equal(fval, default_fill_value(0)) fval = _check_fill_value(0, "|S3") assert_equal(fval, asbytes("0")) fval = _check_fill_value(None, "|S3") assert_equal(fval, default_fill_value(b"camelot!")) self.assertRaises(TypeError, _check_fill_value, 1e+20, int) self.assertRaises(TypeError, _check_fill_value, 'stuff', int)
Example #27
Source File: test_mrecords.py From lambda-packs with MIT License | 5 votes |
def setup(self): # Generic setup _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(list(map(asbytes, ['one', 'two', 'three'])), mask=[0, 0, 1], dtype='|S8') ddtype = [('a', int), ('b', float), ('c', '|S8')] mrec = fromarrays([_a, _b, _c], dtype=ddtype, fill_value=(asbytes('99999'), asbytes('99999.'), asbytes('N/A'))) nrec = recfromarrays((_a._data, _b._data, _c._data), dtype=ddtype) self.data = (mrec, nrec, ddtype)
Example #28
Source File: test_mrecords.py From lambda-packs with MIT License | 5 votes |
def test_get(self): # Tests fields retrieval base = self.base.copy() mbase = base.view(mrecarray) # As fields.......... for field in ('a', 'b', 'c'): assert_equal(getattr(mbase, field), mbase[field]) assert_equal(base[field], mbase[field]) # as elements ....... mbase_first = mbase[0] assert_(isinstance(mbase_first, mrecarray)) assert_equal(mbase_first.dtype, mbase.dtype) assert_equal(mbase_first.tolist(), (1, 1.1, asbytes('one'))) # Used to be mask, now it's recordmask assert_equal(mbase_first.recordmask, nomask) assert_equal(mbase_first._mask.item(), (False, False, False)) assert_equal(mbase_first['a'], mbase['a'][0]) mbase_last = mbase[-1] assert_(isinstance(mbase_last, mrecarray)) assert_equal(mbase_last.dtype, mbase.dtype) assert_equal(mbase_last.tolist(), (None, None, None)) # Used to be mask, now it's recordmask assert_equal(mbase_last.recordmask, True) assert_equal(mbase_last._mask.item(), (True, True, True)) assert_equal(mbase_last['a'], mbase['a'][-1]) assert_((mbase_last['a'] is masked)) # as slice .......... mbase_sl = mbase[:2] assert_(isinstance(mbase_sl, mrecarray)) assert_equal(mbase_sl.dtype, mbase.dtype) # Used to be mask, now it's recordmask assert_equal(mbase_sl.recordmask, [0, 1]) assert_equal_records(mbase_sl.mask, np.array([(False, False, False), (True, True, True)], dtype=mbase._mask.dtype)) assert_equal_records(mbase_sl, base[:2].view(mrecarray)) for field in ('a', 'b', 'c'): assert_equal(getattr(mbase_sl, field), base[:2][field])
Example #29
Source File: test_io.py From recruit with Apache License 2.0 | 5 votes |
def __init__(self, s=""): BytesIO.__init__(self, asbytes(s))
Example #30
Source File: test__iotools.py From lambda-packs with MIT License | 5 votes |
def test_uint64_dtype(self): "Check that uint64 integer types can be specified" converter = StringConverter(np.uint64, default=0) val = asbytes("9223372043271415339") assert_(converter(val) == 9223372043271415339)