Skip to content

Commit

Permalink
Merge pull request #67 from SpiNNakerManchester/neuron_implementation…
Browse files Browse the repository at this point in the history
…_work

Neuron implementation work
  • Loading branch information
rowleya committed Sep 25, 2018
2 parents 0a2935a + 56a200d commit e0e4c13
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions spinn_utilities/classproperty.py
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)
30 changes: 30 additions & 0 deletions unittests/test_class_property.py
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

0 comments on commit e0e4c13

Please sign in to comment.