Python numpy.compat.asbytes_nested() Examples

The following are 30 code examples of numpy.compat.asbytes_nested(). 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_defchararray.py    From Computable with MIT License 6 votes vote down vote up
def test_rstrip(self):
        assert_(issubclass(self.A.rstrip().dtype.type, np.string_))
        assert_array_equal(self.A.rstrip(), asbytes_nested([
                [' abc', ''],
                ['12345', 'MixedCase'],
                ['123 \t 345', 'UPPER']]))
        assert_array_equal(self.A.rstrip(asbytes_nested(['5', 'ER'])),
                           asbytes_nested([
                [' abc ', ''],
                ['1234', 'MixedCase'],
                ['123 \t 345 \x00', 'UPP']]))
        assert_(issubclass(self.B.rstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.rstrip(), [
                [sixu(' \u03a3'), ''],
                ['12345', 'MixedCase'],
                ['123 \t 345', 'UPPER']]) 
Example #2
Source File: test_defchararray.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def test_lstrip(self):
        tgt = asbytes_nested([['abc ', ''],
                              ['12345', 'MixedCase'],
                              ['123 \t 345 \0 ', 'UPPER']])
        assert_(issubclass(self.A.lstrip().dtype.type, np.string_))
        assert_array_equal(self.A.lstrip(), tgt)

        tgt = asbytes_nested([[' abc', ''],
                              ['2345', 'ixedCase'],
                              ['23 \t 345 \x00', 'UPPER']])
        assert_array_equal(self.A.lstrip(asbytes_nested(['1', 'M'])), tgt)

        tgt = [[sixu('\u03a3 '), ''],
               ['12345', 'MixedCase'],
               ['123 \t 345 \0 ', 'UPPER']]
        assert_(issubclass(self.B.lstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.lstrip(), tgt) 
Example #3
Source File: test_core.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def test_fillvalue_conversion(self):
        # Tests the behavior of fill_value during conversion
        # We had a tailored comment to make sure special attributes are
        # properly dealt with
        a = array(asbytes_nested(['3', '4', '5']))
        a._optinfo.update({'comment':"updated!"})

        b = array(a, dtype=int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))

        b = array(a, dtype=float)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0.))

        b = a.astype(int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))
        assert_equal(b._optinfo['comment'], "updated!")

        b = a.astype([('a', '|S3')])
        assert_equal(b['a']._data, a._data)
        assert_equal(b['a'].fill_value, a.fill_value) 
Example #4
Source File: test_defchararray.py    From Computable with MIT License 6 votes vote down vote up
def test_replace(self):
        R = self.A.replace(asbytes_nested(['3', 'a']),
                           asbytes_nested(['##########', '@']))
        assert_(issubclass(R.dtype.type, np.string_))
        assert_array_equal(R, asbytes_nested([
                [' abc ', ''],
                ['12##########45', 'MixedC@se'],
                ['12########## \t ##########45 \x00', 'UPPER']]))

        if sys.version_info[0] < 3:
            # NOTE: b'abc'.replace(b'a', 'b') is not allowed on Py3
            R = self.A.replace(asbytes('a'), sixu('\u03a3'))
            assert_(issubclass(R.dtype.type, np.unicode_))
            assert_array_equal(R, [
                    [sixu(' \u03a3bc '), ''],
                    ['12345', sixu('MixedC\u03a3se')],
                    ['123 \t 345 \x00', 'UPPER']]) 
Example #5
Source File: test_defchararray.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def test_replace(self):
        R = self.A.replace(asbytes_nested(['3', 'a']),
                           asbytes_nested(['##########', '@']))
        tgt = asbytes_nested([[' abc ', ''],
                              ['12##########45', 'MixedC@se'],
                              ['12########## \t ##########45 \x00', 'UPPER']])
        assert_(issubclass(R.dtype.type, np.string_))
        assert_array_equal(R, tgt)

        if sys.version_info[0] < 3:
            # NOTE: b'abc'.replace(b'a', 'b') is not allowed on Py3
            R = self.A.replace(asbytes('a'), sixu('\u03a3'))
            tgt = [[sixu(' \u03a3bc '), ''],
                   ['12345', sixu('MixedC\u03a3se')],
                   ['123 \t 345 \x00', 'UPPER']]
            assert_(issubclass(R.dtype.type, np.unicode_))
            assert_array_equal(R, tgt) 
Example #6
Source File: test_defchararray.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def test_rjust(self):
        assert_(issubclass(self.A.rjust(10).dtype.type, np.string_))

        C = self.A.rjust([10, 20])
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])

        C = self.A.rjust(20, asbytes('#'))
        assert_(np.all(C.startswith(asbytes('#'))))
        assert_array_equal(C.endswith(asbytes('#')),
                           [[False, True], [False, False], [False, False]])

        C = np.char.rjust(asbytes('FOO'), [[10, 20], [15, 8]])
        tgt = asbytes_nested([['       FOO', '                 FOO'],
                              ['            FOO', '     FOO']])
        assert_(issubclass(C.dtype.type, np.string_))
        assert_array_equal(C, tgt) 
Example #7
Source File: test_defchararray.py    From Computable with MIT License 6 votes vote down vote up
def test_strip(self):
        assert_(issubclass(self.A.strip().dtype.type, np.string_))
        assert_array_equal(self.A.strip(), asbytes_nested([
                ['abc', ''],
                ['12345', 'MixedCase'],
                ['123 \t 345', 'UPPER']]))
        assert_array_equal(self.A.strip(asbytes_nested(['15', 'EReM'])),
                           asbytes_nested([
                [' abc ', ''],
                ['234', 'ixedCas'],
                ['23 \t 345 \x00', 'UPP']]))
        assert_(issubclass(self.B.strip().dtype.type, np.unicode_))
        assert_array_equal(self.B.strip(), [
                [sixu('\u03a3'), ''],
                ['12345', 'MixedCase'],
                ['123 \t 345', 'UPPER']]) 
Example #8
Source File: test_defchararray.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def test_rstrip(self):
        assert_(issubclass(self.A.rstrip().dtype.type, np.string_))

        tgt = asbytes_nested([[' abc', ''],
                              ['12345', 'MixedCase'],
                              ['123 \t 345', 'UPPER']])
        assert_array_equal(self.A.rstrip(), tgt)

        tgt = asbytes_nested([[' abc ', ''],
                              ['1234', 'MixedCase'],
                              ['123 \t 345 \x00', 'UPP']
                              ])
        assert_array_equal(self.A.rstrip(asbytes_nested(['5', 'ER'])), tgt)

        tgt = [[sixu(' \u03a3'), ''],
               ['12345', 'MixedCase'],
               ['123 \t 345', 'UPPER']]
        assert_(issubclass(self.B.rstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.rstrip(), tgt) 
Example #9
Source File: test_defchararray.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def test_strip(self):
        tgt = asbytes_nested([['abc', ''],
                              ['12345', 'MixedCase'],
                              ['123 \t 345', 'UPPER']])
        assert_(issubclass(self.A.strip().dtype.type, np.string_))
        assert_array_equal(self.A.strip(), tgt)

        tgt = asbytes_nested([[' abc ', ''],
                              ['234', 'ixedCas'],
                              ['23 \t 345 \x00', 'UPP']])
        assert_array_equal(self.A.strip(asbytes_nested(['15', 'EReM'])), tgt)

        tgt = [[sixu('\u03a3'), ''],
               ['12345', 'MixedCase'],
               ['123 \t 345', 'UPPER']]
        assert_(issubclass(self.B.strip().dtype.type, np.unicode_))
        assert_array_equal(self.B.strip(), tgt) 
Example #10
Source File: test_defchararray.py    From Computable with MIT License 6 votes vote down vote up
def test_lstrip(self):
        assert_(issubclass(self.A.lstrip().dtype.type, np.string_))
        assert_array_equal(self.A.lstrip(), asbytes_nested([
                ['abc ', ''],
                ['12345', 'MixedCase'],
                ['123 \t 345 \0 ', 'UPPER']]))
        assert_array_equal(self.A.lstrip(asbytes_nested(['1', 'M'])),
                           asbytes_nested([
                [' abc', ''],
                ['2345', 'ixedCase'],
                ['23 \t 345 \x00', 'UPPER']]))
        assert_(issubclass(self.B.lstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.lstrip(), [
                [sixu('\u03a3 '), ''],
                ['12345', 'MixedCase'],
                ['123 \t 345 \0 ', 'UPPER']]) 
Example #11
Source File: test_defchararray.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def test_from_string_array(self):
        A = np.array(asbytes_nested([['abc', 'foo'],
                                     ['long   ', '0123456789']]))
        assert_equal(A.dtype.type, np.string_)
        B = np.char.array(A)
        assert_array_equal(B, A)
        assert_equal(B.dtype, A.dtype)
        assert_equal(B.shape, A.shape)
        B[0, 0] = 'changed'
        assert_(B[0, 0] != A[0, 0])
        C = np.char.asarray(A)
        assert_array_equal(C, A)
        assert_equal(C.dtype, A.dtype)
        C[0, 0] = 'changed again'
        assert_(C[0, 0] != B[0, 0])
        assert_(C[0, 0] == A[0, 0]) 
Example #12
Source File: test_core.py    From lambda-packs with MIT License 6 votes vote down vote up
def test_fillvalue_conversion(self):
        # Tests the behavior of fill_value during conversion
        # We had a tailored comment to make sure special attributes are
        # properly dealt with
        a = array(asbytes_nested(['3', '4', '5']))
        a._optinfo.update({'comment':"updated!"})

        b = array(a, dtype=int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))

        b = array(a, dtype=float)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0.))

        b = a.astype(int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))
        assert_equal(b._optinfo['comment'], "updated!")

        b = a.astype([('a', '|S3')])
        assert_equal(b['a']._data, a._data)
        assert_equal(b['a'].fill_value, a.fill_value) 
Example #13
Source File: test_defchararray.py    From Computable with MIT License 6 votes vote down vote up
def test_from_string_array(self):
        A = np.array(asbytes_nested([['abc', 'foo'],
                                     ['long   ', '0123456789']]))
        assert_equal(A.dtype.type, np.string_)
        B = np.char.array(A)
        assert_array_equal(B, A)
        assert_equal(B.dtype, A.dtype)
        assert_equal(B.shape, A.shape)
        B[0, 0] = 'changed'
        assert_(B[0, 0] != A[0, 0])
        C = np.char.asarray(A)
        assert_array_equal(C, A)
        assert_equal(C.dtype, A.dtype)
        C[0, 0] = 'changed again'
        assert_(C[0, 0] != B[0, 0])
        assert_(C[0, 0] == A[0, 0]) 
Example #14
Source File: test_core.py    From ImageFusion with MIT License 6 votes vote down vote up
def test_fillvalue_conversion(self):
        # Tests the behavior of fill_value during conversion
        # We had a tailored comment to make sure special attributes are
        # properly dealt with
        a = array(asbytes_nested(['3', '4', '5']))
        a._optinfo.update({'comment':"updated!"})
        #
        b = array(a, dtype=int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))
        #
        b = array(a, dtype=float)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0.))
        #
        b = a.astype(int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))
        assert_equal(b._optinfo['comment'], "updated!")
        #
        b = a.astype([('a', '|S3')])
        assert_equal(b['a']._data, a._data)
        assert_equal(b['a'].fill_value, a.fill_value) 
Example #15
Source File: test_core.py    From Computable with MIT License 6 votes vote down vote up
def test_fillvalue_conversion(self):
        "Tests the behavior of fill_value during conversion"
        # We had a tailored comment to make sure special attributes are properly
        # dealt with
        a = array(asbytes_nested(['3', '4', '5']))
        a._optinfo.update({'comment':"updated!"})
        #
        b = array(a, dtype=int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))
        #
        b = array(a, dtype=float)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0.))
        #
        b = a.astype(int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))
        assert_equal(b._optinfo['comment'], "updated!")
        #
        b = a.astype([('a', '|S3')])
        assert_equal(b['a']._data, a._data)
        assert_equal(b['a'].fill_value, a.fill_value) 
Example #16
Source File: test_defchararray.py    From ImageFusion with MIT License 6 votes vote down vote up
def test_from_string_array(self):
        A = np.array(asbytes_nested([['abc', 'foo'],
                                     ['long   ', '0123456789']]))
        assert_equal(A.dtype.type, np.string_)
        B = np.char.array(A)
        assert_array_equal(B, A)
        assert_equal(B.dtype, A.dtype)
        assert_equal(B.shape, A.shape)
        B[0, 0] = 'changed'
        assert_(B[0, 0] != A[0, 0])
        C = np.char.asarray(A)
        assert_array_equal(C, A)
        assert_equal(C.dtype, A.dtype)
        C[0, 0] = 'changed again'
        assert_(C[0, 0] != B[0, 0])
        assert_(C[0, 0] == A[0, 0]) 
Example #17
Source File: test_core.py    From ImageFusion with MIT License 5 votes vote down vote up
def test_set_record_slice(self):
        base = self.data['base']
        (base_a, base_b, base_c) = (base['a'], base['b'], base['c'])
        base[:3] = (pi, pi, 'pi')

        assert_equal(base_a.dtype, int)
        assert_equal(base_a._data, [3, 3, 3, 4, 5])

        assert_equal(base_b.dtype, float)
        assert_equal(base_b._data, [pi, pi, pi, 4.4, 5.5])

        assert_equal(base_c.dtype, '|S8')
        assert_equal(base_c._data,
                     asbytes_nested(['pi', 'pi', 'pi', 'four', 'five'])) 
Example #18
Source File: test_defchararray.py    From Computable with MIT License 5 votes vote down vote up
def test_rjust(self):
        assert_(issubclass(self.A.rjust(10).dtype.type, np.string_))
        widths = np.array([[10, 20]])
        C = self.A.rjust([10, 20])
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
        C = self.A.rjust(20, asbytes('#'))
        assert_(np.all(C.startswith(asbytes('#'))))
        assert_array_equal(C.endswith(asbytes('#')),
                           [[False, True], [False, False], [False, False]])
        C = np.char.rjust(asbytes('FOO'), [[10, 20], [15, 8]])
        assert_(issubclass(C.dtype.type, np.string_))
        assert_array_equal(C, asbytes_nested([
                ['       FOO', '                 FOO'],
                ['            FOO', '     FOO']])) 
Example #19
Source File: test_mrecords.py    From ImageFusion with MIT License 5 votes vote down vote up
def setup(self):
        # Generic setup
        ilist = [1, 2, 3, 4, 5]
        flist = [1.1, 2.2, 3.3, 4.4, 5.5]
        slist = asbytes_nested(['one', 'two', 'three', 'four', 'five'])
        ddtype = [('a', int), ('b', float), ('c', '|S8')]
        mask = [0, 1, 0, 0, 1]
        self.base = ma.array(list(zip(ilist, flist, slist)),
                             mask=mask, dtype=ddtype) 
Example #20
Source File: test_mrecords.py    From ImageFusion with MIT License 5 votes vote down vote up
def test_setslices_hardmask(self):
        # Tests setting slices w/ hardmask.
        base = self.base.copy()
        mbase = base.view(mrecarray)
        mbase.harden_mask()
        try:
            mbase[-2:] = (5, 5, 5)
            assert_equal(mbase.a._data, [1, 2, 3, 5, 5])
            assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 5, 5.5])
            assert_equal(mbase.c._data,
                         asbytes_nested(['one', 'two', 'three', '5', 'five']))
            assert_equal(mbase.a._mask, [0, 1, 0, 0, 1])
            assert_equal(mbase.b._mask, mbase.a._mask)
            assert_equal(mbase.b._mask, mbase.c._mask)
        except NotImplementedError:
            # OK, not implemented yet...
            pass
        except AssertionError:
            raise
        else:
            raise Exception("Flexible hard masks should be supported !")
        # Not using a tuple should crash
        try:
            mbase[-2:] = 3
        except (NotImplementedError, TypeError):
            pass
        else:
            raise TypeError("Should have expected a readable buffer object!") 
Example #21
Source File: test_mrecords.py    From ImageFusion with MIT License 5 votes vote down vote up
def test_set_elements(self):
        base = self.base.copy()
        # Set an element to mask .....................
        mbase = base.view(mrecarray).copy()
        mbase[-2] = masked
        assert_equal(
            mbase._mask.tolist(),
            np.array([(0, 0, 0), (1, 1, 1), (0, 0, 0), (1, 1, 1), (1, 1, 1)],
                     dtype=bool))
        # Used to be mask, now it's recordmask!
        assert_equal(mbase.recordmask, [0, 1, 0, 1, 1])
        # Set slices .................................
        mbase = base.view(mrecarray).copy()
        mbase[:2] = (5, 5, 5)
        assert_equal(mbase.a._data, [5, 5, 3, 4, 5])
        assert_equal(mbase.a._mask, [0, 0, 0, 0, 1])
        assert_equal(mbase.b._data, [5., 5., 3.3, 4.4, 5.5])
        assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])
        assert_equal(mbase.c._data,
                     asbytes_nested(['5', '5', 'three', 'four', 'five']))
        assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])
        #
        mbase = base.view(mrecarray).copy()
        mbase[:2] = masked
        assert_equal(mbase.a._data, [1, 2, 3, 4, 5])
        assert_equal(mbase.a._mask, [1, 1, 0, 0, 1])
        assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 4.4, 5.5])
        assert_equal(mbase.b._mask, [1, 1, 0, 0, 1])
        assert_equal(mbase.c._data,
                     asbytes_nested(['one', 'two', 'three', 'four', 'five']))
        assert_equal(mbase.b._mask, [1, 1, 0, 0, 1]) 
Example #22
Source File: test_mrecords.py    From Computable with MIT License 5 votes vote down vote up
def test_set_elements(self):
        base = self.base.copy()
        # Set an element to mask .....................
        mbase = base.view(mrecarray).copy()
        mbase[-2] = masked
        assert_equal(mbase._mask.tolist(),
                     np.array([(0, 0, 0), (1, 1, 1), (0, 0, 0), (1, 1, 1), (1, 1, 1)],
                              dtype=bool))
        # Used to be mask, now it's recordmask!
        assert_equal(mbase.recordmask, [0, 1, 0, 1, 1])
        # Set slices .................................
        mbase = base.view(mrecarray).copy()
        mbase[:2] = (5, 5, 5)
        assert_equal(mbase.a._data, [5, 5, 3, 4, 5])
        assert_equal(mbase.a._mask, [0, 0, 0, 0, 1])
        assert_equal(mbase.b._data, [5., 5., 3.3, 4.4, 5.5])
        assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])
        assert_equal(mbase.c._data,
                     asbytes_nested(['5', '5', 'three', 'four', 'five']))
        assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])
        #
        mbase = base.view(mrecarray).copy()
        mbase[:2] = masked
        assert_equal(mbase.a._data, [1, 2, 3, 4, 5])
        assert_equal(mbase.a._mask, [1, 1, 0, 0, 1])
        assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 4.4, 5.5])
        assert_equal(mbase.b._mask, [1, 1, 0, 0, 1])
        assert_equal(mbase.c._data,
                     asbytes_nested(['one', 'two', 'three', 'four', 'five']))
        assert_equal(mbase.b._mask, [1, 1, 0, 0, 1])
    # 
Example #23
Source File: test_defchararray.py    From Computable with MIT License 5 votes vote down vote up
def test_partition(self):
        P = self.A.partition(asbytes_nested(['3', 'M']))
        assert_(issubclass(P.dtype.type, np.string_))
        assert_array_equal(P, asbytes_nested([
                [(' abc ', '', ''), ('', '', '')],
                [('12', '3', '45'), ('', 'M', 'ixedCase')],
                [('12', '3', ' \t 345 \0 '), ('UPPER', '', '')]])) 
Example #24
Source File: test_defchararray.py    From Computable with MIT License 5 votes vote down vote up
def test_lower(self):
        assert_(issubclass(self.A.lower().dtype.type, np.string_))
        assert_array_equal(self.A.lower(), asbytes_nested([
                [' abc ', ''],
                ['12345', 'mixedcase'],
                ['123 \t 345 \0 ', 'upper']]))
        assert_(issubclass(self.B.lower().dtype.type, np.unicode_))
        assert_array_equal(self.B.lower(), [
                [sixu(' \u03c3 '), sixu('')],
                [sixu('12345'), sixu('mixedcase')],
                [sixu('123 \t 345 \0 '), sixu('upper')]]) 
Example #25
Source File: test_defchararray.py    From Computable with MIT License 5 votes vote down vote up
def test_center(self):
        assert_(issubclass(self.A.center(10).dtype.type, np.string_))
        widths = np.array([[10, 20]])
        C = self.A.center([10, 20])
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
        C = self.A.center(20, asbytes('#'))
        assert_(np.all(C.startswith(asbytes('#'))))
        assert_(np.all(C.endswith(asbytes('#'))))
        C = np.char.center(asbytes('FOO'), [[10, 20], [15, 8]])
        assert_(issubclass(C.dtype.type, np.string_))
        assert_array_equal(C, asbytes_nested([
                ['   FOO    ', '        FOO         '],
                ['      FOO      ', '  FOO   ']])) 
Example #26
Source File: test_defchararray.py    From Computable with MIT License 5 votes vote down vote up
def test_capitalize(self):
        assert_(issubclass(self.A.capitalize().dtype.type, np.string_))
        assert_array_equal(self.A.capitalize(), asbytes_nested([
                [' abc ', ''],
                ['12345', 'Mixedcase'],
                ['123 \t 345 \0 ', 'Upper']]))
        assert_(issubclass(self.B.capitalize().dtype.type, np.unicode_))
        assert_array_equal(self.B.capitalize(), [
                [sixu(' \u03c3 '), ''],
                ['12345', 'Mixedcase'],
                ['123 \t 345 \0 ', 'Upper']]) 
Example #27
Source File: test_defchararray.py    From Computable with MIT License 5 votes vote down vote up
def test_from_object_array(self):
        A = np.array([['abc', 2],
                      ['long   ', '0123456789']], dtype='O')
        B = np.char.array(A)
        assert_equal(B.dtype.itemsize, 10)
        assert_array_equal(B, asbytes_nested([['abc', '2'],
                                              ['long', '0123456789']])) 
Example #28
Source File: test_core.py    From Computable with MIT License 5 votes vote down vote up
def test_set_record_slice(self):
        base = self.data['base']
        (base_a, base_b, base_c) = (base['a'], base['b'], base['c'])
        base[:3] = (pi, pi, 'pi')

        assert_equal(base_a.dtype, int)
        assert_equal(base_a._data, [3, 3, 3, 4, 5])

        assert_equal(base_b.dtype, float)
        assert_equal(base_b._data, [pi, pi, pi, 4.4, 5.5])

        assert_equal(base_c.dtype, '|S8')
        assert_equal(base_c._data,
                     asbytes_nested(['pi', 'pi', 'pi', 'four', 'five'])) 
Example #29
Source File: test_mrecords.py    From Computable with MIT License 5 votes vote down vote up
def test_setslices_hardmask(self):
        "Tests setting slices w/ hardmask."
        base = self.base.copy()
        mbase = base.view(mrecarray)
        mbase.harden_mask()
        try:
            mbase[-2:] = (5, 5, 5)
            assert_equal(mbase.a._data, [1, 2, 3, 5, 5])
            assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 5, 5.5])
            assert_equal(mbase.c._data,
                         asbytes_nested(['one', 'two', 'three', '5', 'five']))
            assert_equal(mbase.a._mask, [0, 1, 0, 0, 1])
            assert_equal(mbase.b._mask, mbase.a._mask)
            assert_equal(mbase.b._mask, mbase.c._mask)
        except NotImplementedError:
            # OK, not implemented yet...
            pass
        except AssertionError:
            raise
        else:
            raise Exception("Flexible hard masks should be supported !")
        # Not using a tuple should crash
        try:
            mbase[-2:] = 3
        except (NotImplementedError, TypeError):
            pass
        else:
            raise TypeError("Should have expected a readable buffer object!") 
Example #30
Source File: test__iotools.py    From Computable with MIT License 5 votes vote down vote up
def test_tab_delimiter(self):
        "Test tab delimiter"
        strg = asbytes(" 1\t 2\t 3\t 4\t 5  6")
        test = LineSplitter(asbytes('\t'))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5  6']))
        strg = asbytes(" 1  2\t 3  4\t 5  6")
        test = LineSplitter(asbytes('\t'))(strg)
        assert_equal(test, asbytes_nested(['1  2', '3  4', '5  6']))