Ejemplo n.º 1
0
 def test_json2str(self):
     # json to str 测试,要求保持 list 顺序,中文兼容,双引号括起字符串(字符串里面的双引号前面有斜杠转移)
     value = {
         'a': [1, 2, '呵xx呵', u'哈"哈',
               str_util.to_str('智汇云', encode='GBK')]
     }
     json_value = '{"a": [1, 2, "\\u5475xx\\u5475", "\\u54c8\\"\\u54c8", "\\u667a\\u6c47\\u4e91"]}'
     assert str_util.json2str(value) == json_value
     assert str_util.to_json(json_value) == str_util.deep_str(value)
     # 加入时间测试
     value2 = {
         'a': [
             1, 2, '呵xx呵', u'哈"哈',
             str_util.to_str('智汇云', encode='GBK'),
             time.strptime('2014/03/25 19:05:33', '%Y/%m/%d %H:%M:%S')
         ],
         u'eff_time':
         datetime.datetime(2015, 6, 28, 14, 19, 41),
     }
     value3 = {
         'a': [
             1, 2, '呵xx呵', u'哈"哈',
             str_util.to_str('智汇云', encode='GBK'), "2014-03-25 19:05:33"
         ],
         u'eff_time':
         "2015-06-28 14:19:41"
     }
     json_value2 = '{"a": [1, 2, "\\u5475xx\\u5475", "\\u54c8\\"\\u54c8", "\\u667a\\u6c47\\u4e91", "2014-03-25 19:05:33"], "eff_time": "2015-06-28 14:19:41"}'
     assert str_util.json2str(value2) == json_value2
     assert str_util.to_json(json_value2) == str_util.deep_str(value3)
     # eval 能将非标准json的内容转换过来
     json_value = u"{'a': [1, 2, '\\u5475xx\\u5475', '\\xE5\\x8C\\x85']}"
     assert str_util.to_json(json_value) == eval(json_value)
Ejemplo n.º 2
0
    def _test_deep_object(self):
        # 类的转换
        class B(object):
            a = arg1

        class A(B):
            obj = arg1
            b = B

            def __init__(self, arg):
                self.arg = arg

            # 类里的函数测试
            def test2(self, arg):
                pass

            @staticmethod  # 申明此方法是一个静态方法,外部可以直接调用
            def tt(a):
                pass

            @classmethod  # 申明此方法是一个类方法
            def class_method(class_name, arg1):
                pass

        c1 = A(arg1)
        assert c1.obj == arg1
        assert vars(c1) == {'arg': arg1}  # 当前类不改变。 obj 是静态属性,不包含在 vars 内

        c2 = str_util.deep_str(c1, str_unicode=str_util.to_str)
        assert vars(c2) == {'a': arg3, 'b': B, 'obj': arg3, 'arg': arg3}
        c3 = str_util.deep_str(c1, all2str=True)
        assert vars(c3) == {'a': arg4, 'b': B, 'obj': arg4, 'arg': arg4}
        c4 = str_util.deep_str(c1, all2str='time')
        assert vars(c4) == {'a': arg5, 'b': B, 'obj': arg5, 'arg': arg5}
Ejemplo n.º 3
0
 def test_urlencode(self):
     # 字典转请求参数
     param1 = {'name': '测试用户', 'password': 123456}
     assert http_util.urlencode(
         param1
     ) == 'password=123456&name=%E6%B5%8B%E8%AF%95%E7%94%A8%E6%88%B7'
     assert http_util.urlencode(
         param1,
         encode='gbk') == 'password=123456&name=%B2%E2%CA%D4%D3%C3%BB%A7'
     param2 = {'name': '测试用户', 'password': {u'哈': [1, 2, '3', u'测试']}}
     assert http_util.urlencode(
         param2
     ) == 'password=%7B%22%5Cu54c8%22%3A+%5B1%2C+2%2C+%223%22%2C+%22%5Cu6d4b%5Cu8bd5%22%5D%7D&name=%E6%B5%8B%E8%AF%95%E7%94%A8%E6%88%B7'
     # 请求参数转字典
     assert str_util.deep_str(
         http_util.getRequestParams(
             http_util.urlencode(param1))) == str_util.deep_str(
                 param1, all2str=True)
     assert str_util.deep_str(
         http_util.getRequestParams(
             'http://xx.xx.com:8080/te?password=123456&name=%E6%B5%8B%E8%AF%95%E7%94%A8%E6%88%B7'
         )) == str_util.deep_str(param1, all2str=True)
     assert str_util.deep_str(
         http_util.getRequestParams(http_util.urlencode(param2))) == {
             u'password': u'{"\\u54c8": [1, 2, "3", "\\u6d4b\\u8bd5"]}',
             u'name': u'测试用户'
         }
Ejemplo n.º 4
0
    def test_deep_str(self):
        # 测试键值是否同时转
        assert str_util.deep_str({'a': [1, 1, 2, [u'哈哈', '33', ]], '测试值': (3, 4, u'ff',)}) == {
            u'a': [1, 1, 2, [u'哈哈', u'33']], u'测试值': (3, 4, u'ff')}
        assert str_util.deep_str({'a': [1, 1, 2, ['哈哈', '33', ]], u'测试值': (3, 4, u'ff',)},
                                 str_unicode=str_util.to_str) == {'a': [1, 1, 2, ['哈哈', '33']], '测试值': (3, 4, 'ff')}

        # 不能改变原参数值
        arg1 = {'aa': 4.55, 'b1': {'ll': 66.55, u'测试': 554, '测试2': u'测试2值', 'c': [1, u'哈啊', '啊哈'], u'元组': (
        1, '22', 3.55, set('abcd'), decimal.Decimal('55.6722'), datetime.datetime(2015, 6, 28, 14, 19, 41),
        uuid.UUID('81ab20bf-ecd9-4cc7-beb1-498da7e0b75d'))}}
        arg2 = copy.deepcopy(arg1)
        arg3 = str_util.deep_str(arg1, str_unicode=str_util.to_str)
        assert arg1 == arg2
        assert arg1 != arg3
        assert arg3 == {'aa': 4.55, 'b1': {'ll': 66.55, '测试': 554, '测试2': '测试2值', 'c': [1, '哈啊', '啊哈'], '元组': (
        1, '22', 3.55, set('abcd'), 55.6722, '2015-06-28 14:19:41', '81ab20bfecd94cc7beb1498da7e0b75d')}}
        arg4 = str_util.deep_str(arg1, all2str=True)
        assert arg4 == {u'aa': u'4.55',
                        u'b1': {u'll': u'66.55', u'测试': u'554', u'测试2': u'测试2值', u'c': [u'1', u'哈啊', u'啊哈'], u'元组': (
                        u'1', u'22', u'3.55', set('abcd'), u'55.6722', u'2015-06-28 14:19:41',
                        u'81ab20bfecd94cc7beb1498da7e0b75d')}}
        arg5 = str_util.deep_str(arg1, all2str='time')
        assert arg5 == {u'aa': 4.55, u'b1': {u'll': 66.55, u'测试': 554, u'测试2': u'测试2值', u'c': [1, u'哈啊', u'啊哈'],
                                             u'元组': (1, u'22', 3.55, set('abcd'), 55.6722, u'2015-06-28 14:19:41',
                                                     '81ab20bfecd94cc7beb1498da7e0b75d')}}

        # int, float 类型的转换
        a = {1: 5.444, ('', 44.55): [12, 55.50]}
        assert str_util.deep_str(a) == a
        assert str_util.deep_str(a, all2str=True) == {u'1': u'5.444', (u'', u'44.55'): [u'12', u'55.5']}

        # 截取最大长度
        d = [u"eval 处理是为了让“时”,“包”等字符转为人可以阅读的文字", {123456789012345: '123456789012345'}]
        assert str_util.deep_str(d, max='13') == [u"eval 处理是为了让“时...", {123456789012345: u'1234567890123...'}]
        assert str_util.deep_str(d, max=13, str_unicode=str_util.to_str) == ["eval 处理是为了让“时...",
                                                                             {123456789012345: '1234567890123...'}]
Ejemplo n.º 5
0
    def test_deep_str(self):
        # 测试键值是否同时转
        assert str_util.deep_str({
            'a': [1, 1, 2, [
                u'哈哈',
                '33',
            ]],
            '测试值': (
                3,
                4,
                u'ff',
            )
        }) == {
            u'a': [1, 1, 2, [u'哈哈', u'33']],
            u'测试值': (3, 4, u'ff')
        }
        assert str_util.deep_str(
            {
                'a': [1, 1, 2, [
                    '哈哈',
                    '33',
                ]],
                u'测试值': (
                    3,
                    4,
                    u'ff',
                )
            },
            str_unicode=str_util.to_str) == {
                'a': [1, 1, 2, ['哈哈', '33']],
                '测试值': (3, 4, 'ff')
            }

        # 不能改变原参数值
        arg1 = {
            'aa': 4.55,
            'b1': {
                'll':
                66.55,
                u'测试':
                554,
                '测试2':
                u'测试2值',
                'c': [1, u'哈啊', '啊哈'],
                u'元组': (1, '22', 3.55, set('abcd'), decimal.Decimal('55.6722'),
                        datetime.datetime(2015, 6, 28, 14, 19, 41))
            }
        }
        arg2 = copy.deepcopy(arg1)
        arg3 = str_util.deep_str(arg1, str_unicode=str_util.to_str)
        assert arg1 == arg2
        assert arg1 != arg3
        assert arg3 == {
            'aa': 4.55,
            'b1': {
                'll': 66.55,
                '测试': 554,
                '测试2': '测试2值',
                'c': [1, '哈啊', '啊哈'],
                '元组':
                (1, '22', 3.55, set('abcd'), 55.6722, '2015-06-28 14:19:41')
            }
        }
        arg4 = str_util.deep_str(arg1, all2str=True)
        assert arg4 == {
            u'aa': u'4.55',
            u'b1': {
                u'll':
                u'66.55',
                u'测试':
                u'554',
                u'测试2':
                u'测试2值',
                u'c': [u'1', u'哈啊', u'啊哈'],
                u'元组': (u'1', u'22', u'3.55', set('abcd'), u'55.6722',
                        u'2015-06-28 14:19:41')
            }
        }
        arg5 = str_util.deep_str(arg1, all2str='time')
        assert arg5 == {
            u'aa': 4.55,
            u'b1': {
                u'll':
                66.55,
                u'测试':
                554,
                u'测试2':
                u'测试2值',
                u'c': [1, u'哈啊', u'啊哈'],
                u'元组':
                (1, u'22', 3.55, set('abcd'), 55.6722, u'2015-06-28 14:19:41')
            }
        }

        # int, float 类型的转换
        a = {1: 5.444, ('', 44.55): [12, 55.50]}
        assert str_util.deep_str(a) == a
        assert str_util.deep_str(a, all2str=True) == {
            u'1': u'5.444',
            (u'', u'44.55'): [u'12', u'55.5']
        }

        # 截取最大长度
        d = [
            u"eval 处理是为了让“时”,“包”等字符转为人可以阅读的文字", {
                123456789012345: '123456789012345'
            }
        ]
        assert str_util.deep_str(d, max='13') == [
            u"eval 处理是为了让“时...", {
                123456789012345: u'1234567890123...'
            }
        ]
        assert str_util.deep_str(d, max=13, str_unicode=str_util.to_str) == [
            "eval 处理是为了让“时...", {
                123456789012345: '1234567890123...'
            }
        ]

        # 类的转换
        class B(object):
            a = arg1

        class A(B):
            obj = arg1
            b = B

            def __init__(self, arg):
                self.arg = arg

            # 类里的函数测试
            def test2(self, arg):
                pass

            @staticmethod  # 申明此方法是一个静态方法,外部可以直接调用
            def tt(a):
                pass

            @classmethod  # 申明此方法是一个类方法
            def class_method(class_name, arg1):
                pass

        c1 = A(arg1)
        assert c1.obj == arg1
        assert vars(c1) == {'arg': arg1}  # 当前类不改变。 obj 是静态属性,不包含在 vars 内

        c2 = str_util.deep_str(c1, str_unicode=str_util.to_str)
        assert vars(c2) == {'a': arg3, 'b': B, 'obj': arg3, 'arg': arg3}
        c3 = str_util.deep_str(c1, all2str=True)
        assert vars(c3) == {'a': arg4, 'b': B, 'obj': arg4, 'arg': arg4}
        c4 = str_util.deep_str(c1, all2str='time')
        assert vars(c4) == {'a': arg5, 'b': B, 'obj': arg5, 'arg': arg5}