-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from SpiNNakerManchester/neuron_implementation…
…_work Neuron implementation work
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class ClassPropertyDescriptor(object): | ||
""" A class to handle the management of class properties | ||
""" | ||
|
||
def __init__(self, fget): | ||
self.fget = fget | ||
|
||
def __get__(self, obj, klass=None): | ||
if klass is None: | ||
klass = type(obj) | ||
return self.fget.__get__(obj, klass)() | ||
|
||
|
||
def classproperty(func): | ||
""" Defines a property at the class-level | ||
""" | ||
if not isinstance(func, (classmethod, staticmethod)): | ||
func = classmethod(func) | ||
|
||
return ClassPropertyDescriptor(func) |
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,30 @@ | ||
from spinn_utilities.classproperty import classproperty | ||
|
||
|
||
class ClassWithClassProperty(object): | ||
|
||
_my_property = "Test" | ||
getter_called = False | ||
|
||
@classproperty | ||
def my_property(cls): | ||
cls.getter_called = True | ||
return cls._my_property | ||
|
||
@classmethod | ||
def set_my_property(cls, my_property): | ||
cls._my_property = my_property | ||
|
||
|
||
def test_class_property(): | ||
|
||
assert not ClassWithClassProperty.getter_called | ||
assert ClassWithClassProperty.my_property == \ | ||
ClassWithClassProperty._my_property | ||
assert ClassWithClassProperty.getter_called | ||
|
||
instance_1 = ClassWithClassProperty() | ||
instance_2 = ClassWithClassProperty() | ||
ClassWithClassProperty.set_my_property("NewValue") | ||
assert instance_1.my_property == instance_2.my_property | ||
assert ClassWithClassProperty.my_property == instance_1.my_property |