-
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.
- Loading branch information
Showing
25 changed files
with
286 additions
and
165 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
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
57 changes: 57 additions & 0 deletions
57
examples/class_is_composed_from_cherrypicked_methods_with_rename.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 | ||
# class variables and instance variables. Composed methods will have | ||
# access to all these variables. | ||
@extendable | ||
class ExampleClass(object): | ||
PUBLIC = 24 | ||
_HIDDEN = 25 | ||
__PRIVATE = 26 | ||
|
||
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 class variables and instance variables, thus it won't work as a | ||
# stand-alone object. | ||
class ExampleTrait(object): | ||
@staticmethod | ||
def static_method(): | ||
return 1, 2, 3 | ||
|
||
@classmethod | ||
def class_method(cls): | ||
return cls.PUBLIC, cls._HIDDEN, cls.__PRIVATE | ||
|
||
def instance_method(self): | ||
return self.public, self._hidden, self.__private | ||
|
||
|
||
# Then, here we do the actual composition, where we cherry-pick each method from | ||
# ExampleTrait and compose them into ExampleClass. While we do that, we also | ||
# specify function names as keyword parameters to change the name of the | ||
# functions. | ||
ExampleClass.add_traits(ExampleTrait.instance_method, | ||
ExampleTrait.class_method, | ||
ExampleTrait.static_method, | ||
instance_method="renamed_instance_method", | ||
class_method="renamed_class_method", | ||
static_method="renamed_static_method") | ||
|
||
# Here are the proofs that composed methods work as part of new class. | ||
assert ExampleClass.renamed_static_method() == (1, 2, 3),\ | ||
"Class composition fails with classmethod in class!" | ||
assert ExampleClass.renamed_class_method() == (24, 25, 26),\ | ||
"Class composition fails with class method in class!" | ||
assert ExampleClass().renamed_class_method() == (24, 25, 26), \ | ||
"Class composition fails with class method in instance!" | ||
assert ExampleClass().renamed_instance_method() == (42, 43, 44),\ | ||
"Class composition fails with instance method!" |
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
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
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
57 changes: 57 additions & 0 deletions
57
examples/instance_is_composed_from_cherrypicked_methods_with_rename.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 | ||
# class variables and instance variables. Composed methods will have | ||
# access to all these variables. | ||
@extendable | ||
class ExampleClass(object): | ||
PUBLIC = 24 | ||
_HIDDEN = 25 | ||
__PRIVATE = 26 | ||
|
||
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 class variables and instance variables, thus it won't work as a | ||
# stand-alone object. | ||
class ExampleTrait(object): | ||
@staticmethod | ||
def static_method(): | ||
return 1, 2, 3 | ||
|
||
@classmethod | ||
def class_method(cls): | ||
return cls.PUBLIC, cls._HIDDEN, cls.__PRIVATE | ||
|
||
def instance_method(self): | ||
return self.public, self._hidden, self.__private | ||
|
||
|
||
# Then, before composition, we create instance out of the class. After that | ||
# we do the actual composition, where we cherry-pick each method from | ||
# ExampleTrait and compose them into ExampleClass. While we do that, we also | ||
# specify function names as keyword parameters to change the name of the | ||
# functions. | ||
example_instance = ExampleClass() | ||
example_instance.add_traits(ExampleTrait.instance_method, | ||
ExampleTrait.class_method, | ||
ExampleTrait.static_method, | ||
instance_method="renamed_instance_method", | ||
class_method="renamed_class_method", | ||
static_method="renamed_static_method") | ||
|
||
# Here are the proofs that composed methods work as part of new instance. | ||
assert example_instance.renamed_static_method() == (1, 2, 3),\ | ||
"Class composition fails with classmethod in class!" | ||
assert example_instance.renamed_class_method() == (24, 25, 26),\ | ||
"Class composition fails with class method in class!" | ||
assert example_instance.renamed_instance_method() == (42, 43, 44),\ | ||
"Class composition fails with instance method!" |
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
Oops, something went wrong.