python类string_()的实例源码

field_diag.py 文件源码 项目:fbpic 作者: fbpic 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def setup_openpmd_meshes_group( self, dset ) :
        """
        Set the attributes that are specific to the mesh path

        Parameter
        ---------
        dset : an h5py.Group object that contains all the mesh quantities
        """
        # Field Solver
        dset.attrs["fieldSolver"] = np.string_("PSATD")
        # Field boundary
        dset.attrs["fieldBoundary"] = np.array([
            np.string_("reflecting"), np.string_("reflecting"),
            np.string_("reflecting"), np.string_("reflecting") ])
        # Particle boundary
        dset.attrs["particleBoundary"] = np.array([
            np.string_("absorbing"), np.string_("absorbing"),
            np.string_("absorbing"), np.string_("absorbing") ])
        # Current Smoothing
        dset.attrs["currentSmoothing"] = np.string_("Binomial")
        dset.attrs["currentSmoothingParameters"] = \
          np.string_("period=1;numPasses=1;compensator=false")
        # Charge correction
        dset.attrs["chargeCorrection"] = np.string_("spectral")
        dset.attrs["chargeCorrectionParameters"] = np.string_("period=1")
defchararray.py 文件源码 项目:lambda-numba 作者: rlhotovy 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def add(x1, x2):
    """
    Return element-wise string concatenation for two arrays of str or unicode.

    Arrays `x1` and `x2` must have the same shape.

    Parameters
    ----------
    x1 : array_like of str or unicode
        Input array.
    x2 : array_like of str or unicode
        Input array.

    Returns
    -------
    add : ndarray
        Output array of `string_` or `unicode_`, depending on input types
        of the same shape as `x1` and `x2`.

    """
    arr1 = numpy.asarray(x1)
    arr2 = numpy.asarray(x2)
    out_size = _get_num_chars(arr1) + _get_num_chars(arr2)
    dtype = _use_unicode(arr1, arr2)
    return _vec_string(arr1, (dtype, out_size), '__add__', (arr2,))
test_defchararray.py 文件源码 项目:lambda-numba 作者: rlhotovy 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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])
test_defchararray.py 文件源码 项目:lambda-numba 作者: rlhotovy 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def test_ljust(self):
        assert_(issubclass(self.A.ljust(10).dtype.type, np.string_))

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

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

        C = np.char.ljust(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)
test_defchararray.py 文件源码 项目:lambda-numba 作者: rlhotovy 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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)
test_defchararray.py 文件源码 项目:lambda-numba 作者: rlhotovy 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
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)
test_defchararray.py 文件源码 项目:lambda-numba 作者: rlhotovy 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
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)
defchararray.py 文件源码 项目:deliver 作者: orchestor 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def add(x1, x2):
    """
    Return element-wise string concatenation for two arrays of str or unicode.

    Arrays `x1` and `x2` must have the same shape.

    Parameters
    ----------
    x1 : array_like of str or unicode
        Input array.
    x2 : array_like of str or unicode
        Input array.

    Returns
    -------
    add : ndarray
        Output array of `string_` or `unicode_`, depending on input types
        of the same shape as `x1` and `x2`.

    """
    arr1 = numpy.asarray(x1)
    arr2 = numpy.asarray(x2)
    out_size = _get_num_chars(arr1) + _get_num_chars(arr2)
    dtype = _use_unicode(arr1, arr2)
    return _vec_string(arr1, (dtype, out_size), '__add__', (arr2,))
test_defchararray.py 文件源码 项目:deliver 作者: orchestor 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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])
test_defchararray.py 文件源码 项目:deliver 作者: orchestor 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def test_ljust(self):
        assert_(issubclass(self.A.ljust(10).dtype.type, np.string_))

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

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

        C = np.char.ljust(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)
test_defchararray.py 文件源码 项目:deliver 作者: orchestor 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
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)
test_defchararray.py 文件源码 项目:deliver 作者: orchestor 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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)
test_defchararray.py 文件源码 项目:deliver 作者: orchestor 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
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)
readwrite.py 文件源码 项目:scanpy 作者: theislab 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def preprocess_writing(key, value):
    if isinstance(value, dict):
        # hack for storing dicts
        value = np.array([str(value)])
    else:
        value = np.array(value)
        if value.ndim == 0:
            value = np.array([value])
    # some output about the data to write
    logg.m(key, type(value),
           value.dtype, value.dtype.kind, value.shape,
           v=6)
    # make sure string format is chosen correctly
    if value.dtype.kind == 'U':
        value = value.astype(np.string_)
    return key, value


问题


面经


文章

微信
公众号

扫码关注公众号