Skip to content
Snippets Groups Projects
Commit c5662b8e authored by jkerdreu's avatar jkerdreu
Browse files

Added dict-like API for attributes. you can now use something like this:

dev.attributes["temperature"] = 20

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/trunk@1868 b32b6428-25c9-4566-ad07-03861ab6144f
parent a5b8329c
No related branches found
No related tags found
No related merge requests found
......@@ -55,7 +55,26 @@ class Attribute(object):
return "attr: %s value: %s" % (self.name, self.value)
class Attributes(list):
def __getitem__(self,value):
if isinstance(value,int):
return list.__getitem__(self,value)
for k in self:
if (value == k.name):
return k.value
raise KeyError(value)
def __setitem__(self,name,value):
if isinstance(name,int):
return list.__setitem__(self,name,value)
for k in self:
if (name == k.name):
k.value = value
return
raise KeyError(name)
class Device(object):
def __init__(self,devtype,addr=None,engine=None):
......@@ -77,7 +96,7 @@ class Device(object):
self.alive_period = config.alive_timer # time in sec between two alive
self.next_alive = 0
self.__attributes = []
self.__attributes = Attributes()
self.new_attribute('busAddr',config.address)
self.new_attribute('busPort',config.port)
......@@ -148,7 +167,10 @@ class Device(object):
@attributes.setter
def attributes(self,values):
self.__attributes = values
if isinstance(values,Attributes):
self.__attributes = values
else:
raise DeviceError("Invalid attributes list, use class Attributes)")
def add_method(self,name,func):
self.methods.update({name:func})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment