Static Properties¶
Static properties allow users to define attributes to a class ad class creation.
Unlike python’s native property descriptor. a vproperty’s getter setter and deleter are already defined.
class Foo(object):
x = vproperty()
Normal python descriptor symantics are already defined:
class Foo(object):
@vproperty
def x(self):
'Getter for x'
@x.setter
def x(self):
'Setter for x'
Validation¶
Type and instance arguments may be given to perform arbitrary valitation before the setter is called.
class Foo(object):
x = vproperty(type=int)
class Bar(object):
x = vproperty(isntance=int)
- The type argument is only required to be a function that acccepts a single input and returns a value::
- class Foo(object):
- x = vproperty(type=lambda item: item.lower())
or
class Foo(object):
x = vproperty()
@x.type
def check_x_type(self, value):
pass
The instance argument performs a strict isinstance check.
class Foo(object):
x = vproperty(type=int)
class Bar(object):
x = vproperty(isntance=int)
foo = Foo()
foo.x = '1' # x is set to one
bar = Bar()
bar.x = '1' # error x is not an int.
Delegation¶
Delegation may be one of two forms, strict: and can only be done when the instance argument is used or loose, where no error checking is done:
class Foo(object):
x = vproperty(type=int)
class Bar(object):
foo = vproperty(isntance=Foo)
# Strong Delegation of bar.x to bar.foo.x
x = foo.x
# Weak delegation of bar.y to bar.foo.x
y = delegate('foo', 'x')
# Also can do
y = delegate(foo, Foo.x)
vproperty objects¶
-
class
traity.statics.
vproperty
(fget=None, fset=None, fdel=None, type=None, ftype=None, instance=None, fdefault=None)¶ Parameters: - fget – method to get attribute
- fset – method to set attribute
- fdel – method to delete attribute
- ftype – method to convert type of attribute on assignment
- instance – if given, all assignments to this property must pass an isinstance check.
- fdefault – method to get attribute if undefined
- type – shorthand for ftype, type may be a basic python type not a method. int, float etc.
Define a static attribute to a class. Behaves almost exactly the same as Python properties:
class Parrot(object): def __init__(self): self._voltage = 100000 @vproperty def voltage(self): """Get the current voltage.""" return self._voltage
vproperty defines default fget, fset and fdel:
class Parrot(object): def __init__(self): self.voltage = 100000 voltage = vproperty()
vproperty adds ftype, instance and fdefault:
class Parrot(object): voltage = vproperty(type=int) def __init__(self): self.voltage = 100000 #OK self.voltage = '100000' #ValueError 'abc' is not convertable to int. self.voltage = 'abc'
or:
class Parrot(object): voltage = vproperty() @voltage.type def dynamic_range(self, value): return clamp(value, 0, self.max_volts)
defaults:
class Parrot(object): voltage = vproperty() @voltage.default def voltage_default(self): return 10000
-
delegates_to
(attr)¶ Delegates to an inner attribute. instance= argument must be set in constructor.
Used by __getattr__ for convinience. eg x.delegates_to(‘y’) can be written as x.y
-
getter
(value)¶
-
type
(method)¶ Set the type validation as a method of the containing class:
@x.type def check(self, value): ...
.