本文整理汇总了Python中builtins.property方法的典型用法代码示例。如果您正苦于以下问题:Python builtins.property方法的具体用法?Python builtins.property怎么用?Python builtins.property使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类builtins
的用法示例。
在下文中一共展示了builtins.property方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def __init__(self, name, setter=None, saver=None, type=None,
default=_NO_DEFAULT, write_once=False, load_stage=2, order=0,
save_via_ref=False, clone=True,
doc=None):
# pylint: disable=redefined-builtin
self.__name__ = name
if setter is None and type is bool:
setter = qubes.property.bool
self._setter = setter
self._saver = saver if saver is not None else (
lambda self, prop, value: str(value))
self.type = type
self._default = default
self._default_function = None
if isinstance(default, collections.abc.Callable):
self._default_function = default
self._write_once = write_once
self.order = order
self.load_stage = load_stage
self.save_via_ref = save_via_ref
self.clone = clone
self.__doc__ = doc
self._attr_name = '_qubesprop_' + name
示例2: property_is_default
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def property_is_default(self, prop):
'''Check whether property is in it's default value.
Properties when unset may return some default value, so
``hasattr(vm, prop.__name__)`` is wrong in some circumstances. This
method allows for checking if the value returned is in fact it's
default value.
:param qubes.property prop: property object of particular interest
:rtype: bool
''' # pylint: disable=protected-access
# both property_get_def() and ._attr_name may throw AttributeError,
# which we don't want to catch
attrname = self.property_get_def(prop)._attr_name
return not hasattr(self, attrname)
示例3: property_get_def
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def property_get_def(cls, prop):
'''Return property definition object.
If prop is already :py:class:`qubes.property` instance, return the same
object.
:param prop: property object or name
:type prop: qubes.property or str
:rtype: qubes.property
'''
if isinstance(prop, qubes.property):
return prop
props = cls.property_dict()
if prop in props:
return props[prop]
raise AttributeError('No property {!r} found in {!r}'.format(
prop, cls))
示例4: load_properties
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def load_properties(self, load_stage=None):
'''Load properties from immediate children of XML node.
``property-set`` events are not fired for each individual property.
:param int load_stage: Stage of loading.
'''
if self.xml is None:
return
all_names = set(
prop.__name__ for prop in self.property_list(load_stage))
for node in self.xml.xpath('./properties/property'):
name = node.get('name')
value = node.get('ref') or node.text
if not name in all_names:
continue
setattr(self, name, value)
示例5: clone_properties
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def clone_properties(self, src, proplist=None):
'''Clone properties from other object.
:param PropertyHolder src: source object
:param iterable proplist: list of properties \
(:py:obj:`None` or omit for all properties except those with \
:py:attr:`property.clone` set to :py:obj:`False`)
'''
if proplist is None:
proplist = [prop for prop in self.property_list()
if prop.clone]
else:
proplist = [prop for prop in self.property_list()
if prop.__name__ in proplist or prop in proplist]
for prop in proplist:
try:
# pylint: disable=protected-access
self._property_init(prop, getattr(src, prop._attr_name))
except AttributeError:
continue
self.fire_event('clone-properties', src=src, proplist=proplist)
示例6: violation
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def violation(self):
"""Amount that current solution violates this constraint
If a solution is available, than this property indicates how much
the current solution violates this constraint.
"""
lhs = sum(coef * var.x for (var, coef) in self.__expr.items())
rhs = -self.const
if self.sense == "=":
viol = abs(lhs - rhs)
elif self.sense == "<":
viol = max(lhs - rhs, 0.0)
elif self.sense == ">":
viol = max(rhs - lhs, 0.0)
else:
raise ValueError("Invalid sense {}".format(self.sense))
return viol
示例7: __get__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def __get__(self, instance, owner):
if instance is None:
return self
# XXX this violates duck typing, shall we keep it?
if not isinstance(instance, PropertyHolder):
raise AttributeError('qubes.property should be used on '
'qubes.PropertyHolder instances only')
try:
return getattr(instance, self._attr_name)
except AttributeError:
return self.get_default(instance)
示例8: get_default
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def get_default(self, instance):
if self._default is self._NO_DEFAULT:
raise AttributeError(
'property {!r} have no default'.format(self.__name__))
if self._default_function:
return self._default_function(instance)
return self._default
示例9: __set__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def __set__(self, instance, value):
self._enforce_write_once(instance)
if value is self.__class__.DEFAULT:
self.__delete__(instance)
return
try:
oldvalue = getattr(instance, self.__name__)
has_oldvalue = True
except AttributeError:
has_oldvalue = False
if self._setter is not None:
value = self._setter(instance, self, value)
if self.type not in (None, type(value)):
value = self.type(value)
if has_oldvalue:
instance.fire_event('property-pre-set:' + self.__name__,
pre_event=True,
name=self.__name__, newvalue=value, oldvalue=oldvalue)
else:
instance.fire_event('property-pre-set:' + self.__name__,
pre_event=True,
name=self.__name__, newvalue=value)
instance._property_init(self, value) # pylint: disable=protected-access
if has_oldvalue:
instance.fire_event('property-set:' + self.__name__,
name=self.__name__, newvalue=value, oldvalue=oldvalue)
else:
instance.fire_event('property-set:' + self.__name__,
name=self.__name__, newvalue=value)
示例10: __delete__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def __delete__(self, instance):
self._enforce_write_once(instance)
try:
oldvalue = getattr(instance, self.__name__)
has_oldvalue = True
except AttributeError:
has_oldvalue = False
if has_oldvalue:
instance.fire_event('property-pre-reset:' + self.__name__,
pre_event=True,
name=self.__name__, oldvalue=oldvalue)
# deprecated, to be removed in Qubes 5.0
instance.fire_event('property-pre-del:' + self.__name__,
pre_event=True,
name=self.__name__, oldvalue=oldvalue)
try:
delattr(instance, self._attr_name)
except AttributeError:
pass
instance.fire_event('property-reset:' + self.__name__,
name=self.__name__, oldvalue=oldvalue)
# deprecated, to be removed in Qubes 5.0
instance.fire_event('property-del:' + self.__name__,
name=self.__name__, oldvalue=oldvalue)
else:
instance.fire_event('property-pre-reset:' + self.__name__,
pre_event=True,
name=self.__name__)
# deprecated, to be removed in Qubes 5.0
instance.fire_event('property-pre-del:' + self.__name__,
pre_event=True,
name=self.__name__)
instance.fire_event('property-reset:' + self.__name__,
name=self.__name__)
# deprecated, to be removed in Qubes 5.0
instance.fire_event('property-del:' + self.__name__,
name=self.__name__)
示例11: __eq__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def __eq__(self, other):
if isinstance(other, str):
return self.__name__ == other
return isinstance(other, property) and self.__name__ == other.__name__
示例12: _enforce_write_once
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def _enforce_write_once(self, instance):
if self._write_once and not instance.property_is_default(self):
raise AttributeError(
'property {!r} is write-once and already set'.format(
self.__name__))
示例13: sanitize
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def sanitize(self, *, untrusted_newvalue):
'''Coarse sanitization of value to be set, before sending it to a
setter. Can raise QubesValueError if the value is invalid.
:param untrusted_newvalue: value to be validated
:return: sanitized value
:raises: qubes.exc.QubesValueError
'''
# do not treat type='str' as sufficient validation
if self.type is not None and self.type is not str:
# assume specific type will preform enough validation
try:
untrusted_newvalue = untrusted_newvalue.decode('ascii',
errors='strict')
except UnicodeDecodeError:
raise qubes.exc.QubesValueError
if self.type is bool:
return self.bool(None, None, untrusted_newvalue)
try:
return self.type(untrusted_newvalue)
except ValueError:
raise qubes.exc.QubesValueError
else:
# 'str' or not specified type
try:
untrusted_newvalue = untrusted_newvalue.decode('ascii',
errors='strict')
except UnicodeDecodeError:
raise qubes.exc.QubesValueError
allowed_set = string.printable
if not all(x in allowed_set for x in untrusted_newvalue):
raise qubes.exc.QubesValueError(
'Invalid characters in property value')
return untrusted_newvalue
#
# exceptions
#
示例14: dontsave
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def dontsave(self, prop, value):
'''Dummy saver that never saves anything.'''
# pylint: disable=bad-staticmethod-argument,unused-argument
raise property.DontSave()
#
# some setters provided
#
示例15: forbidden
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import property [as 别名]
def forbidden(self, prop, value):
'''Property setter that forbids loading a property.
This is used to effectively disable property in classes which inherit
unwanted property. When someone attempts to load such a property, it
:throws AttributeError: always
''' # pylint: disable=bad-staticmethod-argument,unused-argument
raise AttributeError(
'setting {} property on {} instance is forbidden'.format(
prop.__name__, self.__class__.__name__))