-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for propertty setter and deleter
- Loading branch information
Showing
6 changed files
with
167 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
examples/instance_is_composed_from_cherrypicked_property_in_class.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/python -tt | ||
# -*- coding: utf-8 -*- | ||
from pytraits import extendable | ||
|
||
|
||
# Let's start by creating a simple class with some values. It contains | ||
# only instance variables. Composed property will have access to all | ||
# these variables. | ||
@extendable | ||
class ExampleClass(object): | ||
def __init__(self): | ||
self.public = 42 | ||
self._hidden = 43 | ||
self.__private = 44 | ||
|
||
|
||
# Then we create a class which contains different types of methods that will be | ||
# transferred as a part of the class above. Note that ExampleTrait requires target | ||
# object to contain instance variables, thus it won't work as a stand-alone object. | ||
class ExampleTrait(object): | ||
@property | ||
def trait_property(self): | ||
return self.public, self._hidden, self.__private | ||
|
||
@trait_property.setter | ||
def trait_property(self, new_value): | ||
self.public, self._hidden, self.__private = new_value | ||
|
||
@trait_property.deleter | ||
def trait_property(self): | ||
self.public, self._hidden, self.__private = (42, 43, 44) | ||
|
||
|
||
# Then add the property as a part of our new class simply by referring it. | ||
example_instance = ExampleClass() | ||
example_instance.add_traits(ExampleTrait.trait_property) | ||
|
||
|
||
# Here are the proofs that composed property works as part of new class. | ||
assert example_instance.trait_property == (42, 43, 44),\ | ||
"Cherry-picked property not working in new class!" | ||
|
||
# We also demonstrate that we can alter the values through the property | ||
example_instance.trait_property = (142, 143, 144) | ||
assert example_instance.trait_property == (142, 143, 144),\ | ||
"Cherry-picked property's setter not working in new class!" | ||
|
||
# Finally, we can delete property's content | ||
del example_instance.trait_property | ||
assert example_instance.trait_property == (42, 43, 44),\ | ||
"Cherry-picked property's deleter not working in new class!" |
57 changes: 57 additions & 0 deletions
57
examples/instance_is_composed_from_cherrypicked_property_in_instance.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/python -tt | ||
# -*- coding: utf-8 -*- | ||
from pytraits import extendable | ||
|
||
|
||
# Let's start by creating a simple class with some values. It contains | ||
# only instance variables. Composed property will have access to all | ||
# these variables. | ||
@extendable | ||
class ExampleClass(object): | ||
def __init__(self): | ||
self.public = 42 | ||
self._hidden = 43 | ||
self.__private = 44 | ||
|
||
|
||
# Then we create a class which contains different types of methods that will be | ||
# transferred as a part of the class above. Note that ExampleTrait requires target | ||
# object to contain instance variables, thus it won't work as a stand-alone object. | ||
class ExampleTrait(object): | ||
@property | ||
def trait_property(self): | ||
return self.public, self._hidden, self.__private | ||
|
||
@trait_property.setter | ||
def trait_property(self, new_value): | ||
self.public, self._hidden, self.__private = new_value | ||
|
||
@trait_property.deleter | ||
def trait_property(self): | ||
self.public, self._hidden, self.__private = (42, 43, 44) | ||
|
||
|
||
# Create instance out of the trait class. Now we need to notice that we need | ||
# to refer to instance's class to get the property and transfer it to new | ||
# location. Using directly my_trait_instance.trait_property would naturally | ||
# invoke retrieval of the values (which in this case would not even exist and | ||
# and would raise an error). | ||
example_instance = ExampleClass() | ||
my_trait_instance = ExampleTrait() | ||
example_instance.add_traits(my_trait_instance.__class__.trait_property) | ||
|
||
|
||
# Here are the proofs that composed property works as part of new class. | ||
# Also we show that there is no inheritance done for ExampleClass instance. | ||
assert example_instance.trait_property == (42, 43, 44),\ | ||
"Cherry-picked property not working in new class!" | ||
|
||
# We also demonstrate that we can alter the values through the property | ||
example_instance.trait_property = (142, 143, 144) | ||
assert example_instance.trait_property == (142, 143, 144),\ | ||
"Cherry-picked property's setter not working in new class!" | ||
|
||
# Finally, we can delete property's content | ||
del example_instance.trait_property | ||
assert example_instance.trait_property == (42, 43, 44),\ | ||
"Cherry-picked property's deleter not working in new class!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters