Index: spongenet/trunk/spongenet/parse.py =================================================================== --- spongenet/trunk/spongenet/parse.py (revision 369) +++ spongenet/trunk/spongenet/parse.py (revision 370) @@ -186,5 +186,5 @@ -class Point(object): +class Point(dict): """ A data point for a sponge sensor sample." @@ -200,32 +200,35 @@ >>> _data = Data(_xmldoc) >>> point = _data.devices[0].sensors[0].points[0] - >>> point.id != None - True - >>> point.descr != None - True - >>> point.type != None - True - >>> point.format != None - True - >>> point.unit != None - True - >>> point.rangemin != None - True - >>> point.rangemax != None - True - >>> point.value != None + >>> len(point.keys()) == 8 + True + >>> point["id"] != None + True + >>> point["descr"] != None + True + >>> point["type"] != None + True + >>> point["format"] != None + True + >>> point["unit"] != None + True + >>> point["rangemin"] != None + True + >>> point["rangemax"] != None + True + >>> point["value"] != None True """ def __init__(self, point): + super(Point, self).__init__({}) for key, value in point.attrib.items(): key = key.lower() - super(Point, self).__setattr__(key, value) + self[key] = value for elem in point.getchildren(): tag = XMLNS_PATTERN.search(elem.tag).groups()[1].lower() - super(Point, self).__setattr__(tag, elem.text) - - -class Sensor(object): + self[tag] = elem.text + + +class Sensor(dict): """ A collection of data points for a sponge sensor sample. @@ -243,28 +246,31 @@ >>> len(sensor.points) 3 - >>> sensor.id != None - True - >>> sensor.serialno != None - True - >>> sensor.prodno != None - True - >>> sensor.prodname != None - True - >>> sensor.descr != None - True - >>> sensor.adr != None - True - >>> sensor.protocolver != None - True - >>> sensor.verticalposition != None - True - >>> sensor.status != None + >>> len(sensor.keys()) == 9 + True + >>> sensor["id"] != None + True + >>> sensor["serialno"] != None + True + >>> sensor["prodno"] != None + True + >>> sensor["prodname"] != None + True + >>> sensor["descr"] != None + True + >>> sensor["adr"] != None + True + >>> sensor["protocolver"] != None + True + >>> sensor["verticalposition"] != None + True + >>> sensor["status"] != None True """ def __init__(self, sensor, xmlns): + super(Sensor, self).__init__({}) for key, value in sensor.attrib.items(): key = key.lower() - super(Sensor, self).__setattr__(key, value) + self[key] = value for elem in sensor.getchildren(): tag = XMLNS_PATTERN.search(elem.tag).groups()[1].lower() @@ -273,8 +279,8 @@ in elem.findall(xmlns + "Point")] else: - super(Sensor, self).__setattr__(tag, elem.text) - - -class Device(object): + self[tag] = elem.text + + +class Device(dict): """ Data from a collection of sponge sensors for a single time sample. @@ -292,42 +298,45 @@ >>> len(device.sensors) 15 - >>> device.id != None - True - >>> device.sessionid != None - True - >>> device.descr != None - True - >>> device.serialno != None - True - >>> device.prodno != None - True - >>> device.prodname != None - True - >>> device.devicetype != None - True - >>> device.protocolver != None - True - >>> device.time != None - True - >>> device.status != None - True - >>> device.location != None - True - >>> device.verticalposition != None - True - >>> device.owner != None - True - >>> device.recordnumber != None - True - >>> device.data_time != None - True - >>> device.data_sessionid != None + >>> len(device.keys()) + 16 + >>> device["id"] != None + True + >>> device["sessionid"] != None + True + >>> device["descr"] != None + True + >>> device["serialno"] != None + True + >>> device["prodno"] != None + True + >>> device["prodname"] != None + True + >>> device["devicetype"] != None + True + >>> device["protocolver"] != None + True + >>> device["time"] != None + True + >>> device["status"] != None + True + >>> device["location"] != None + True + >>> device["verticalposition"] != None + True + >>> device["owner"] != None + True + >>> device["recordnumber"] != None + True + >>> device["data_time"] != None + True + >>> device["data_sessionid"] != None True """ def __init__(self, device, xmlns): + super(Device, self).__init__({}) for key, value in device.attrib.items(): key = key.lower() - super(Device, self).__setattr__(key, value) + self[key] = value for elem in device.getchildren(): tag = XMLNS_PATTERN.search(elem.tag).groups()[1].lower() @@ -335,5 +344,5 @@ for subelem in elem.getchildren(): tag = XMLNS_PATTERN.search(subelem.tag).groups()[1].lower() - super(Device, self).__setattr__(tag, subelem.text) + self[tag] = subelem.text elif tag == "data": for key, value in elem.attrib.items(): @@ -343,9 +352,9 @@ elif key == "sessionid": key = "data_sessionid" - super(Device, self).__setattr__(key, value) + self[key] = value self.sensors = [Sensor(sensor, xmlns) for sensor in elem.findall(xmlns + "SensorData")] else: - super(Device, self).__setattr__(tag, elem.text) + self[tag] = elem.text