From 6be49d9b156a22460476c7d5c729fd4ff697a778 Mon Sep 17 00:00:00 2001 From: Joel Bender Date: Tue, 26 Sep 2017 00:20:17 -0400 Subject: [PATCH] adding and removing a property --- py25/bacpypes/constructeddata.py | 5 +++ py27/bacpypes/constructeddata.py | 5 +++ py34/bacpypes/constructeddata.py | 5 +++ sandbox/add_remove_property.py | 59 ++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 sandbox/add_remove_property.py diff --git a/py25/bacpypes/constructeddata.py b/py25/bacpypes/constructeddata.py index 2e589b8..1d1cade 100755 --- a/py25/bacpypes/constructeddata.py +++ b/py25/bacpypes/constructeddata.py @@ -600,6 +600,11 @@ def ArrayOf(klass): # not found raise ValueError("%r not in array" % (value,)) + def remove(self, item): + # find the index of the item and delete it + indx = self.index(item) + self.__delitem__(indx) + def encode(self, taglist): if _debug: ArrayOf._debug("(%r)encode %r", self.__class__.__name__, taglist) diff --git a/py27/bacpypes/constructeddata.py b/py27/bacpypes/constructeddata.py index 75818ad..38e491c 100755 --- a/py27/bacpypes/constructeddata.py +++ b/py27/bacpypes/constructeddata.py @@ -598,6 +598,11 @@ def ArrayOf(klass): # not found raise ValueError("%r not in array" % (value,)) + def remove(self, item): + # find the index of the item and delete it + indx = self.index(item) + self.__delitem__(indx) + def encode(self, taglist): if _debug: ArrayOf._debug("(%r)encode %r", self.__class__.__name__, taglist) diff --git a/py34/bacpypes/constructeddata.py b/py34/bacpypes/constructeddata.py index 45dc228..54b9f5a 100755 --- a/py34/bacpypes/constructeddata.py +++ b/py34/bacpypes/constructeddata.py @@ -598,6 +598,11 @@ def ArrayOf(klass): # not found raise ValueError("%r not in array" % (value,)) + def remove(self, item): + # find the index of the item and delete it + indx = self.index(item) + self.__delitem__(indx) + def encode(self, taglist): if _debug: ArrayOf._debug("(%r)encode %r", self.__class__.__name__, taglist) diff --git a/sandbox/add_remove_property.py b/sandbox/add_remove_property.py new file mode 100644 index 0000000..e5a90be --- /dev/null +++ b/sandbox/add_remove_property.py @@ -0,0 +1,59 @@ + +from bacpypes.basetypes import PropertyIdentifier +from bacpypes.constructeddata import ArrayOf +from bacpypes.object import AnalogValueObject + +# create an array of property identifiers datatype +ArrayOfPropertyIdentifier = ArrayOf(PropertyIdentifier) + +aopi = ArrayOfPropertyIdentifier() +aopi.append('objectName') +aopi.append('objectType') +aopi.append('description') +aopi.debug_contents() + +aopi.remove('objectType') +aopi.debug_contents() + +print("Create an Analog Value Object") +av = AnalogValueObject( + objectName='av-sample', + objectIdentifier=('analogValue', 1), + description="sample", + ) +av.debug_contents() +print("") + +print("Change the description") +av.description = "something else" +av.debug_contents() +print("") + + +# get the description property by the attribute name +description_property = av._attr_to_property('description') +print("description_property = %r" % (description_property,)) +print("") + +print("Delete the property") +av.delete_property(description_property) +print("...property deleted") + +try: + av.description = "this raises an exception" +except Exception as err: + print(repr(err)) +av.debug_contents() +print("") + +print("===== Add the property") +av.add_property(description_property) +print("...property added") + +try: + av.description = "this works" +except Exception as err: + print(repr(err)) +av.debug_contents() +print("") +