mirror of
https://github.com/JoelBender/bacpypes
synced 2025-09-28 22:15:23 +08:00

When the tutorial is over, the reader will continue with HandsOnLab which contains Samples1,2,3,4 Samples 5 and 14 were removed from the official table of content as they are not completed yet. All code is tested, files were moved from samples to their folder. Signed-off-by: Christian Tremblay <christian.tremblay@servisys.com>
120 lines
2.7 KiB
Python
120 lines
2.7 KiB
Python
#!/usr/bin/env python
|
|
|
|
"""
|
|
The capabilty module is used to mix together classes that provide
|
|
both separate and overlapping functionality. The original design
|
|
was motivated by a component architecture where collections of
|
|
components that needed to be mixed together were specified outside
|
|
the application in a database.
|
|
|
|
THIS FILE IS A DUPLICATE OF A UNIT TEST USED, PUT HERE FOR CONVENIENCE
|
|
|
|
"""
|
|
|
|
import unittest
|
|
|
|
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
|
|
from bacpypes.capability import Capability, Collector
|
|
|
|
# some debugging
|
|
_debug = 0
|
|
_log = ModuleLogger(globals())
|
|
|
|
|
|
@bacpypes_debugging
|
|
class BaseCollector(Collector):
|
|
|
|
def __init__(self):
|
|
if _debug: BaseCollector._debug("__init__")
|
|
Collector.__init__(self)
|
|
|
|
def transform(self, value):
|
|
if _debug: BaseCollector._debug("transform %r", value)
|
|
|
|
for fn in self.capability_functions('transform'):
|
|
print(" - fn: {}".format(fn))
|
|
value = fn(self, value)
|
|
|
|
return value
|
|
|
|
@bacpypes_debugging
|
|
class PlusOne(Capability):
|
|
|
|
def __init__(self):
|
|
if _debug: PlusOne._debug("__init__")
|
|
|
|
def transform(self, value):
|
|
if _debug: PlusOne._debug("transform %r", value)
|
|
return value + 1
|
|
|
|
|
|
@bacpypes_debugging
|
|
class TimesTen(Capability):
|
|
|
|
def __init__(self):
|
|
if _debug: TimesTen._debug("__init__")
|
|
|
|
def transform(self, value):
|
|
if _debug: TimesTen._debug("transform %r", value)
|
|
return value * 10
|
|
|
|
|
|
@bacpypes_debugging
|
|
class MakeList(Capability):
|
|
|
|
def __init__(self):
|
|
if _debug: MakeList._debug("__init__")
|
|
|
|
def transform(self, value):
|
|
if _debug: MakeList._debug("transform %r", value)
|
|
return [value]
|
|
|
|
|
|
#
|
|
# Example classes
|
|
#
|
|
|
|
class Example1(BaseCollector):
|
|
pass
|
|
|
|
class Example2(BaseCollector, PlusOne):
|
|
pass
|
|
|
|
class Example3(BaseCollector, TimesTen, PlusOne):
|
|
pass
|
|
|
|
class Example4(BaseCollector, MakeList, TimesTen):
|
|
pass
|
|
|
|
|
|
@bacpypes_debugging
|
|
class TestExamples(unittest.TestCase):
|
|
|
|
def test_example_1(self):
|
|
if _debug: TestExamples._debug("test_example_1")
|
|
|
|
assert Example1().transform(1) == 1
|
|
|
|
def test_example_2(self):
|
|
if _debug: TestExamples._debug("test_example_2")
|
|
|
|
assert Example2().transform(2) == 3
|
|
|
|
def test_example_3(self):
|
|
if _debug: TestExamples._debug("test_example_3")
|
|
|
|
assert Example3().transform(3) == 31
|
|
|
|
def test_example_4(self):
|
|
if _debug: TestExamples._debug("test_example_4")
|
|
|
|
assert Example4().transform(4) == [4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
|
|
|
|
def test_example_5(self):
|
|
if _debug: TestExamples._debug("test_example_5")
|
|
|
|
obj = Example2()
|
|
obj.add_capability(MakeList)
|
|
|
|
assert obj.transform(5) == [6]
|