本文整理汇总了Python中oslo_versionedobjects.fields.Field方法的典型用法代码示例。如果您正苦于以下问题:Python fields.Field方法的具体用法?Python fields.Field怎么用?Python fields.Field使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类oslo_versionedobjects.fields
的用法示例。
在下文中一共展示了fields.Field方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_serialization
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import Field [as 别名]
def test_serialization(self):
@base.VersionedObjectRegistry.register
class Foo(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('Bar')}
@base.VersionedObjectRegistry.register
class Bar(base.VersionedObject):
fields = {'foo': fields.Field(fields.String())}
obj = Foo(objects=[])
for i in 'abc':
bar = Bar(foo=i)
obj.objects.append(bar)
obj2 = base.VersionedObject.obj_from_primitive(obj.obj_to_primitive())
self.assertFalse(obj is obj2)
self.assertEqual([x.foo for x in obj],
[y.foo for y in obj2])
示例2: test_comparable_objects
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import Field [as 别名]
def test_comparable_objects(self):
@objects.base.KarborObjectRegistry.register
class MyComparableObj(objects.base.KarborObject,
objects.base.KarborObjectDictCompat,
objects.base.KarborComparableObject):
fields = {'foo': fields.Field(fields.Integer())}
class NonVersionedObject(object):
pass
obj1 = MyComparableObj(foo=1)
obj2 = MyComparableObj(foo=1)
obj3 = MyComparableObj(foo=2)
obj4 = NonVersionedObject()
self.assertTrue(obj1 == obj2)
self.assertFalse(obj1 == obj3)
self.assertFalse(obj1 == obj4)
self.assertNotEqual(obj1, None)
示例3: setUp
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import Field [as 别名]
def setUp(self):
super(TestField, self).setUp()
self.field = fields.Field(FakeFieldType())
self.coerce_good_values = [('foo', '*foo*')]
self.coerce_bad_values = []
self.to_primitive_values = [('foo', '!foo!')]
self.from_primitive_values = [('!foo!', 'foo')]
示例4: test_subclassability
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import Field [as 别名]
def test_subclassability(self):
class MyRegistryOne(base.VersionedObjectRegistry):
def registration_hook(self, cls, index):
cls.reg_to = "one"
class MyRegistryTwo(base.VersionedObjectRegistry):
def registration_hook(self, cls, index):
cls.reg_to = "two"
@MyRegistryOne.register
class AVersionedObject1(base.VersionedObject):
VERSION = '1.0'
fields = {'baz': fields.Field(fields.Integer())}
@MyRegistryTwo.register
class AVersionedObject2(base.VersionedObject):
VERSION = '1.0'
fields = {'baz': fields.Field(fields.Integer())}
self.assertIn('AVersionedObject1',
MyRegistryOne.obj_classes())
self.assertIn('AVersionedObject2',
MyRegistryOne.obj_classes())
self.assertIn('AVersionedObject1',
MyRegistryTwo.obj_classes())
self.assertIn('AVersionedObject2',
MyRegistryTwo.obj_classes())
self.assertIn('AVersionedObject1',
base.VersionedObjectRegistry.obj_classes())
self.assertIn('AVersionedObject2',
base.VersionedObjectRegistry.obj_classes())
self.assertEqual(AVersionedObject1.reg_to, "one")
self.assertEqual(AVersionedObject2.reg_to, "two")
示例5: test_load_in_base
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import Field [as 别名]
def test_load_in_base(self):
@base.VersionedObjectRegistry.register
class Foo(base.VersionedObject):
fields = {'foobar': fields.Field(fields.Integer())}
obj = Foo()
with self.assertRaisesRegex(NotImplementedError, ".*foobar.*"):
obj.foobar
示例6: test_obj_fields
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import Field [as 别名]
def test_obj_fields(self):
class TestObj(base.VersionedObject):
fields = {'foo': fields.Field(fields.Integer())}
obj_extra_fields = ['bar']
@property
def bar(self):
return 'this is bar'
obj = TestObj()
self.assertEqual(['foo', 'bar'], obj.obj_fields)
示例7: setUp
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import Field [as 别名]
def setUp(self):
super(TestTimestampedObject, self).setUp()
@base.VersionedObjectRegistry.register_if(False)
class MyTimestampedObject(base.VersionedObject,
base.TimestampedObject):
fields = {
'field1': fields.Field(fields.String()),
}
self.myclass = MyTimestampedObject
self.my_object = self.myclass(field1='field1')
示例8: _render_object
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import Field [as 别名]
def _render_object(cls, object, *args, **kwargs):
# We need to findout the type of field sometimes - these are helper
# methods for that.
def _is_datetime_field(object, key):
field = object.FIELDS.get(key, {})
if isinstance(field, fields.Field):
# TODO(daidv): If we change to use DateTimeField or STL
# we should change this to exact object
return isinstance(field, fields.DateTimeField)
else:
return field.get('schema', {}).get('format', '') == 'date-time'
def _format_datetime_field(obj):
return datetime.datetime.strftime(
obj, utils.DATETIME_FORMAT)
# The dict we will return to be rendered to JSON / output format
r_obj = {}
# Loop over all fields that are supposed to be output
for key, value in cls.MODIFICATIONS['fields'].items():
# Get properties for this field
field_props = cls.MODIFICATIONS['fields'][key]
# Check if it has to be renamed
if field_props.get('rename', False):
obj = getattr(object, field_props.get('rename'))
# if rename is specified we need to change the key
obj_key = field_props.get('rename')
else:
# if not, move on
obj = getattr(object, key, None)
obj_key = key
# Check if this item is a relation (another DesignateObject that
# will need to be converted itself
field = object.FIELDS.get(obj_key, {})
if isinstance(field, dict) and field.get('relation'):
# Get a adapter for the nested object
# Get the class the object is and get its adapter, then set
# the item in the dict to the output
r_obj[key] = cls.get_object_adapter(
cls.ADAPTER_FORMAT,
object.FIELDS[obj_key].get('relation_cls')).render(
cls.ADAPTER_FORMAT, obj, *args, **kwargs)
elif hasattr(field, 'objname'):
# Add by daidv: Check if field is OVO field and have a relation
r_obj[key] = cls.get_object_adapter(
cls.ADAPTER_FORMAT,
field.objname).render(
cls.ADAPTER_FORMAT, obj, *args, **kwargs)
elif _is_datetime_field(object, obj_key) and obj is not None:
# So, we now have a datetime object to render correctly
# see bug #1579844
r_obj[key] = _format_datetime_field(obj)
else:
# Just attach the damn item if there is no weird edge cases
r_obj[key] = obj
# Send it back
return r_obj
示例9: _make_class_properties
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import Field [as 别名]
def _make_class_properties(cls):
# NOTE(danms/comstud): Inherit fields from super classes.
# mro() returns the current class first and returns 'object' last, so
# those can be skipped. Also be careful to not overwrite any fields
# that already exist. And make sure each cls has its own copy of
# fields and that it is not sharing the dict with a super class.
cls.fields = dict(cls.fields)
for supercls in cls.mro()[1:-1]:
if not hasattr(supercls, 'fields'):
continue
for name, field in supercls.fields.items():
if name not in cls.fields:
cls.fields[name] = field
for name, field in cls.fields.items():
if not isinstance(field, obj_fields.Field):
raise exception.ObjectFieldInvalid(
field=name, objname=cls.obj_name())
def getter(self, name=name):
attrname = _get_attrname(name)
if not hasattr(self, attrname):
self.obj_load_attr(name)
return getattr(self, attrname)
def setter(self, value, name=name, field=field):
attrname = _get_attrname(name)
field_value = field.coerce(self, name, value)
if field.read_only and hasattr(self, attrname):
# Note(yjiang5): _from_db_object() may iterate
# every field and write, no exception in such situation.
if getattr(self, attrname) != field_value:
raise exception.ReadOnlyFieldError(field=name)
else:
return
self._changed_fields.add(name)
try:
return setattr(self, attrname, field_value)
except Exception:
with excutils.save_and_reraise_exception():
attr = "%s.%s" % (self.obj_name(), name)
LOG.exception('Error setting %(attr)s',
{'attr': attr})
def deleter(self, name=name):
attrname = _get_attrname(name)
if not hasattr(self, attrname):
raise AttributeError("No such attribute `%s'" % name)
delattr(self, attrname)
setattr(cls, name, property(getter, setter, deleter))
示例10: test_obj_dict_field_modifications
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import Field [as 别名]
def test_obj_dict_field_modifications(self):
@base.VersionedObjectRegistry.register
class ObjWithDict(base.VersionedObject):
fields = {
'dict_field': fields.Field(fields.Dict(fields.Integer())),
}
obj = ObjWithDict()
obj.dict_field = {"1": 1, "3": 3, "4": 4}
def set_by_key(key, value):
obj.dict_field[key] = value
def add_by_key(key, value):
obj.dict_field[key] = value
def update_w_dict(key, value):
obj.dict_field.update({key: value})
def update_w_kwargs(key, value):
obj.dict_field.update(**{key: value})
def setdefault(key, value):
obj.dict_field.setdefault(key, value)
# positive tests to ensure that coercing works properly
set_by_key("1", "10")
add_by_key("2", "20")
update_w_dict("3", "30")
update_w_kwargs("4", "40")
setdefault("5", "50")
self.assertEqual({"1": 10, "2": 20, "3": 30, "4": 40, "5": 50},
obj.dict_field)
obj.obj_reset_changes()
# negative tests with non-coerceable values
self.assertRaises(ValueError, set_by_key, "key", "abc")
self.assertRaises(ValueError, add_by_key, "other", "abc")
self.assertRaises(ValueError, update_w_dict, "key", "abc")
self.assertRaises(ValueError, update_w_kwargs, "key", "abc")
self.assertRaises(ValueError, setdefault, "other", "abc")
# ensure that nothing has been changed
self.assertEqual({"1": 10, "2": 20, "3": 30, "4": 40, "5": 50},
obj.dict_field)
self.assertEqual({}, obj.obj_get_changes())