1
0
mirror of https://github.com/JoelBender/bacpypes synced 2025-09-28 22:15:23 +08:00
bacpypes/samples/Tutorial/Capabilities.py
Christian Tremblay ebe2ba5abe Following along the documentation was hard because samples and tutorial files were lost among all samples. I created two folders : Tutorial and HandsOnLab. Tutorial is the folder with the first file someone will look at. The documentation invite the reader to start $python Tutorial/WhoIsIAm.py for example.
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>
2016-11-17 23:46:52 -05:00

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]