Dynamically create objects and setting their attributes using suds lib in python -
i'm using raspberry send sensor data on soap webservice. rpi gets data serial port. format 'date, value1, value2, value3, value4\r\n'. webservice request looks this
<sensors> <sensor> <sensorid>int</sensorid> <nodeid>int</nodeid> <sensortypeid>int</sensortypeid> <value>double</value> <status>string</status> <date>datetime</date> <deleted>boolean</deleted> <updated>boolean</updated> <remoteid>int</remoteid> <dateoflastupdate>datetime</dateoflastupdate> <userid>int</userid> <errormessage>string</errormessage> </sensor> <sensor> <sensorid>int</sensorid> <nodeid>int</nodeid> <sensortypeid>int</sensortypeid> <value>double</value> <status>string</status> <date>datetime</date> <deleted>boolean</deleted> <updated>boolean</updated> <remoteid>int</remoteid> <dateoflastupdate>datetime</dateoflastupdate> <userid>int</userid> <errormessage>string</errormessage> </sensor> </sensors> <username>string</username> <password>string</password> <uniqueclientid>string</uniqueclientid> <project>string</project>
each line serial port has 4 sensor values , datetime these values logged. need create 4 objects suds library method client.factory.create() each line parse serial, add values each attribute , append() objects array of sensor objects webservice accepts first parameter. problem can't find way dynamically create objects, enter attributes' values, , append them big array. i'm going parse 600lines serial port, 4x600=2400 need created. hard-coding object names this
while true: serial_str = port.readline() if serial_str: string_list = serial_str.split(',') date = 't'.join( [ string_list[i] in [0, 1] ] ) temperature = string_list[2] humidity = string_list[3] rain = string_list[4] wind = string_list[5].replace("\r\n","") sensor_obj1 = client.factory.create('sensor') sensor_obj1.sensorid = -1 sensor_obj1.nodeid = 1 sensor_obj1.sensortypeid = 2 sensor_obj1.value = temperature sensor_obj1.status = '' sensor_obj1.date = date sensor_obj1.deleted = 0 sensor_obj1.updated = 0 sensor_obj1.remoteid = 0 sensor_obj1.dateoflastupdate = datetime.now().strftime('%y-%m-%dt%h:%m:%s')datetime.now() sensor_obj1.userid = 0 sensor_obj1.errormessage = '' sensor_list_obj.sensor.append(sensor_obj1) sensor_obj2 = client.factory.create('sensor') ...
would work if had 1 line send, bad programming style. got started python 2.x, appreciate if point me right direction here. in advance
i not familiar problem @ hand, guess can add more parameters call client.factory.create('sensor') call. accept, **kwargs, can call:
client.factory.create('sensor', sensorid=-1, nodeid=1, ...)
if not, can abstract initialization by:
- creating function asignments , hides away 'gory' details. elegant enough, , shouldn't try more advanced if have started programming in python.
- create class sensor, inherits 1 of suds. unfortunately, don't know enough class of objects 'client.factory.create'.
also, think create list of sensors (or dictionary, if makes sense in code), append sensors structure, otherwise program impossible use if have 3 or 5 sensors instead of 4.
Comments
Post a Comment