當前位置: 首頁>>代碼示例>>Python>>正文


Python jsonutils.to_primitive方法代碼示例

本文整理匯總了Python中oslo_serialization.jsonutils.to_primitive方法的典型用法代碼示例。如果您正苦於以下問題:Python jsonutils.to_primitive方法的具體用法?Python jsonutils.to_primitive怎麽用?Python jsonutils.to_primitive使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在oslo_serialization.jsonutils的用法示例。


在下文中一共展示了jsonutils.to_primitive方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_to_primitive_recursive

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def test_to_primitive_recursive(self):
        obj = TestObject(id=1, nested=TestObject(id=2))

        # Ensure only the id attribute is returned
        primitive = obj.to_primitive()
        expected = {
            'designate_object.name': 'TestObject',
            'designate_object.data': {
                'id': 1,
                'nested': {
                    'designate_object.name': 'TestObject',
                    'designate_object.data': {
                        'id': 2,
                    },
                    'designate_object.changes': ['id'],
                    'designate_object.namespace': 'designate',
                    'designate_object.version': '1.0',
                }
            },
            'designate_object.changes': ['id', 'nested'],
            'designate_object.namespace': 'designate',
            'designate_object.version': '1.0',
        }
        self.assertEqual(expected, primitive) 
開發者ID:openstack,項目名稱:designate,代碼行數:26,代碼來源:test_base.py

示例2: test_deepcopy

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def test_deepcopy(self):
        # Create the Original object
        o_obj = TestObject()
        o_obj.id = "My ID"
        o_obj.name = "My Name"

        # Clear the "changed" flag for one of the two fields we set
        o_obj.obj_reset_changes(['name'])

        # Deepcopy the object
        c_obj = copy.deepcopy(o_obj)

        # Ensure the copy was successful
        self.assertEqual(o_obj.id, c_obj.id)
        self.assertEqual(o_obj.name, c_obj.name)
        self.assertEqual(o_obj.obj_attr_is_set('nested'),
                         c_obj.obj_attr_is_set('nested'))

        self.assertEqual(o_obj.obj_get_changes(), c_obj.obj_get_changes())
        self.assertEqual(o_obj.to_primitive(), c_obj.to_primitive()) 
開發者ID:openstack,項目名稱:designate,代碼行數:22,代碼來源:test_base.py

示例3: test_iteritems_with_cycle

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def test_iteritems_with_cycle(self):
        class IterItemsClass(object):
            def __init__(self):
                self.data = dict(a=1, b=2, c=3)
                self.index = 0

            def iteritems(self):
                return self.data.items()

        x = IterItemsClass()
        x2 = IterItemsClass()
        x.data['other'] = x2
        x2.data['other'] = x

        # If the cycle isn't caught, to_primitive() will eventually result in
        # an exception due to excessive recursion depth.
        jsonutils.to_primitive(x) 
開發者ID:openstack,項目名稱:oslo.serialization,代碼行數:19,代碼來源:test_jsonutils.py

示例4: test_mapping

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def test_mapping(self):
        # Make sure collections.Mapping is converted to a dict
        # and not a list.
        class MappingClass(collections.Mapping):
            def __init__(self):
                self.data = dict(a=1, b=2, c=3)

            def __getitem__(self, val):
                return self.data[val]

            def __iter__(self):
                return iter(self.data)

            def __len__(self):
                return len(self.data)

        x = MappingClass()
        p = jsonutils.to_primitive(x)
        self.assertEqual({'a': 1, 'b': 2, 'c': 3}, p) 
開發者ID:openstack,項目名稱:oslo.serialization,代碼行數:21,代碼來源:test_jsonutils.py

示例5: test_depth

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def test_depth(self):
        class LevelsGenerator(object):
            def __init__(self, levels):
                self._levels = levels

            def iteritems(self):
                if self._levels == 0:
                    return iter([])
                else:
                    return iter([(0, LevelsGenerator(self._levels - 1))])

        l4_obj = LevelsGenerator(4)

        json_l2 = {0: {0: None}}
        json_l3 = {0: {0: {0: None}}}
        json_l4 = {0: {0: {0: {0: None}}}}

        ret = jsonutils.to_primitive(l4_obj, max_depth=2)
        self.assertEqual(json_l2, ret)

        ret = jsonutils.to_primitive(l4_obj, max_depth=3)
        self.assertEqual(json_l3, ret)

        ret = jsonutils.to_primitive(l4_obj, max_depth=4)
        self.assertEqual(json_l4, ret) 
開發者ID:openstack,項目名稱:oslo.serialization,代碼行數:27,代碼來源:test_jsonutils.py

示例6: create_share_group

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def create_share_group(self, context, share_group_id, request_spec=None,
                           filter_properties=None):
        """Casts an rpc to the scheduler to create a share group.

        Example of 'request_spec' argument value::

            {

                'share_group_type_id': 'fake_share_group_type_id',
                'share_group_id': 'some_fake_uuid',
                'availability_zone_id': 'some_fake_az_uuid',
                'share_types': [models.ShareType],
                'resource_type': models.ShareGroup,

            }

        """
        request_spec_p = jsonutils.to_primitive(request_spec)
        call_context = self.client.prepare(version='1.8')
        return call_context.cast(context,
                                 'create_share_group',
                                 share_group_id=share_group_id,
                                 request_spec=request_spec_p,
                                 filter_properties=filter_properties) 
開發者ID:openstack,項目名稱:manila,代碼行數:26,代碼來源:rpcapi.py

示例7: migrate_share_to_host

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def migrate_share_to_host(
            self, context, share_id, host, force_host_assisted_migration,
            preserve_metadata, writable, nondisruptive, preserve_snapshots,
            new_share_network_id, new_share_type_id, request_spec=None,
            filter_properties=None):

        call_context = self.client.prepare(version='1.7')
        request_spec_p = jsonutils.to_primitive(request_spec)
        return call_context.cast(
            context, 'migrate_share_to_host',
            share_id=share_id,
            host=host,
            force_host_assisted_migration=force_host_assisted_migration,
            preserve_metadata=preserve_metadata,
            writable=writable,
            nondisruptive=nondisruptive,
            preserve_snapshots=preserve_snapshots,
            new_share_network_id=new_share_network_id,
            new_share_type_id=new_share_type_id,
            request_spec=request_spec_p,
            filter_properties=filter_properties) 
開發者ID:openstack,項目名稱:manila,代碼行數:23,代碼來源:rpcapi.py

示例8: serialize_entity

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def serialize_entity(context, entity):
        return json.to_primitive(entity, convert_instances=True) 
開發者ID:openstack,項目名稱:zun,代碼行數:4,代碼來源:rpc.py

示例9: serialize_entity

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def serialize_entity(context, entity):
        return jsonutils.to_primitive(entity, convert_instances=True) 
開發者ID:openstack,項目名稱:designate,代碼行數:4,代碼來源:rpc.py

示例10: test_to_primitive

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def test_to_primitive(self):
        obj = TestObject(id=1)

        # Ensure only the id attribute is returned
        primitive = obj.to_primitive()
        expected = {
            'designate_object.name': 'TestObject',
            'designate_object.data': {
                'id': 1,
            },
            'designate_object.changes': ['id'],
            'designate_object.namespace': 'designate',
            'designate_object.version': '1.0',
        }
        self.assertEqual(expected, primitive)

        # Set the name attribute to a None value
        obj.name = None

        # Ensure both the id and name attributes are returned
        primitive = obj.to_primitive()
        expected = {
            'designate_object.name': 'TestObject',
            'designate_object.data': {
                'id': 1,
                'name': None,
            },
            'designate_object.changes': ['id', 'name'],
            'designate_object.namespace': 'designate',
            'designate_object.version': '1.0',
        }
        self.assertEqual(expected, primitive) 
開發者ID:openstack,項目名稱:designate,代碼行數:34,代碼來源:test_base.py

示例11: test_to_primitive_nested_obj

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def test_to_primitive_nested_obj(self):
        # Create a few objects
        obj_one = TestObject()
        obj_two = TestObject()
        obj_two.id = "Two"
        obj_one.nested = obj_two

        # Create a ListObject
        obj = TestObjectList(objects=[obj_one, obj_two])

        primitive = obj.to_primitive()
        expected = {
            'designate_object.name': 'TestObjectList',
            'designate_object.changes': ['objects'],
            'designate_object.data': {
                'objects': [
                    {'designate_object.changes': ['nested'],
                     'designate_object.data': {'nested':
                         {
                             'designate_object.changes': [
                                 'id'],
                             'designate_object.data': {
                                 'id': 'Two'},
                             'designate_object.name': 'TestObject',
                             'designate_object.namespace': 'designate',
                             'designate_object.version': '1.0'}},
                     'designate_object.name': 'TestObject',
                     'designate_object.namespace': 'designate',
                     'designate_object.version': '1.0'},
                    {'designate_object.changes': ['id'],
                     'designate_object.data': {'id': 'Two'},
                     'designate_object.name': 'TestObject',
                     'designate_object.namespace': 'designate',
                     'designate_object.version': '1.0'}]},
            'designate_object.namespace': 'designate',
            'designate_object.version': '1.0'}

        self.assertEqual(expected, primitive) 
開發者ID:openstack,項目名稱:designate,代碼行數:40,代碼來源:test_base.py

示例12: __init__

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def __init__(self, default=jsonutils.to_primitive, encoding='utf-8'):
        self._default = default
        self._encoding = encoding 
開發者ID:openstack,項目名稱:oslo.serialization,代碼行數:5,代碼來源:json_serializer.py

示例13: test_dumps_default

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def test_dumps_default(self):
        args = [ReprObject()]
        convert = functools.partial(jsonutils.to_primitive, fallback=repr)
        self.assertEqual('["repr"]', jsonutils.dumps(args, default=convert)) 
開發者ID:openstack,項目名稱:oslo.serialization,代碼行數:6,代碼來源:test_jsonutils.py

示例14: test_bytes

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def test_bytes(self):
        self.assertEqual(jsonutils.to_primitive(b'abc'), 'abc') 
開發者ID:openstack,項目名稱:oslo.serialization,代碼行數:4,代碼來源:test_jsonutils.py

示例15: test_list

# 需要導入模塊: from oslo_serialization import jsonutils [as 別名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 別名]
def test_list(self):
        self.assertEqual([1, 2, 3], jsonutils.to_primitive([1, 2, 3])) 
開發者ID:openstack,項目名稱:oslo.serialization,代碼行數:4,代碼來源:test_jsonutils.py


注:本文中的oslo_serialization.jsonutils.to_primitive方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。