NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

Changeset 370

Show
Ignore:
Timestamp:
09/08/10 13:47:11
Author:
cbc
Message:

Subclass Point, Sensor, and Device from dict.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spongenet/trunk/spongenet/parse.py

    r369 r370  
    186186 
    187187 
    188 class Point(object): 
     188class Point(dict): 
    189189    """ 
    190190    A data point for a sponge sensor sample." 
     
    200200    >>> _data = Data(_xmldoc) 
    201201    >>> point = _data.devices[0].sensors[0].points[0] 
    202     >>> point.id != None 
    203     True 
    204     >>> point.descr != None 
    205     True 
    206     >>> point.type != None 
    207     True 
    208     >>> point.format != None 
    209     True 
    210     >>> point.unit != None 
    211     True 
    212     >>> point.rangemin != None 
    213     True 
    214     >>> point.rangemax != None 
    215     True 
    216     >>> point.value != None 
     202    >>> len(point.keys()) == 8 
     203    True 
     204    >>> point["id"] != None 
     205    True 
     206    >>> point["descr"] != None 
     207    True 
     208    >>> point["type"] != None 
     209    True 
     210    >>> point["format"] != None 
     211    True 
     212    >>> point["unit"] != None 
     213    True 
     214    >>> point["rangemin"] != None 
     215    True 
     216    >>> point["rangemax"] != None 
     217    True 
     218    >>> point["value"] != None 
    217219    True 
    218220    """ 
    219221 
    220222    def __init__(self, point): 
     223        super(Point, self).__init__({}) 
    221224        for key, value in point.attrib.items(): 
    222225            key = key.lower() 
    223             super(Point, self).__setattr__(key, value) 
     226            self[key] = value 
    224227        for elem in point.getchildren(): 
    225228            tag = XMLNS_PATTERN.search(elem.tag).groups()[1].lower() 
    226             super(Point, self).__setattr__(tag, elem.text) 
    227  
    228  
    229 class Sensor(object): 
     229            self[tag] = elem.text 
     230 
     231 
     232class Sensor(dict): 
    230233    """ 
    231234    A collection of data points for a sponge sensor sample. 
     
    243246    >>> len(sensor.points) 
    244247    3 
    245     >>> sensor.id != None 
    246     True 
    247     >>> sensor.serialno != None 
    248     True 
    249     >>> sensor.prodno != None 
    250     True 
    251     >>> sensor.prodname != None 
    252     True 
    253     >>> sensor.descr != None 
    254     True 
    255     >>> sensor.adr != None 
    256     True 
    257     >>> sensor.protocolver != None 
    258     True 
    259     >>> sensor.verticalposition != None 
    260     True 
    261     >>> sensor.status != None 
     248    >>> len(sensor.keys()) == 9 
     249    True 
     250    >>> sensor["id"] != None 
     251    True 
     252    >>> sensor["serialno"] != None 
     253    True 
     254    >>> sensor["prodno"] != None 
     255    True 
     256    >>> sensor["prodname"] != None 
     257    True 
     258    >>> sensor["descr"] != None 
     259    True 
     260    >>> sensor["adr"] != None 
     261    True 
     262    >>> sensor["protocolver"] != None 
     263    True 
     264    >>> sensor["verticalposition"] != None 
     265    True 
     266    >>> sensor["status"] != None 
    262267    True 
    263268    """ 
    264269 
    265270    def __init__(self, sensor, xmlns): 
     271        super(Sensor, self).__init__({}) 
    266272        for key, value in sensor.attrib.items(): 
    267273            key = key.lower() 
    268             super(Sensor, self).__setattr__(key, value) 
     274            self[key] = value 
    269275        for elem in sensor.getchildren(): 
    270276            tag = XMLNS_PATTERN.search(elem.tag).groups()[1].lower() 
     
    273279                                in elem.findall(xmlns + "Point")] 
    274280            else: 
    275                 super(Sensor, self).__setattr__(tag, elem.text) 
    276  
    277  
    278 class Device(object): 
     281                self[tag] = elem.text 
     282 
     283 
     284class Device(dict): 
    279285    """ 
    280286    Data from a collection of sponge sensors for a single time sample. 
     
    292298    >>> len(device.sensors) 
    293299    15 
    294     >>> device.id != None 
    295     True 
    296     >>> device.sessionid != None 
    297     True 
    298     >>> device.descr != None 
    299     True 
    300     >>> device.serialno != None 
    301     True 
    302     >>> device.prodno != None 
    303     True 
    304     >>> device.prodname != None 
    305     True 
    306     >>> device.devicetype != None 
    307     True 
    308     >>> device.protocolver != None 
    309     True 
    310     >>> device.time != None 
    311     True 
    312     >>> device.status != None 
    313     True 
    314     >>> device.location != None 
    315     True 
    316     >>> device.verticalposition != None 
    317     True 
    318     >>> device.owner != None 
    319     True 
    320     >>> device.recordnumber != None 
    321     True 
    322     >>> device.data_time != None 
    323     True 
    324     >>> device.data_sessionid != None 
     300    >>> len(device.keys()) 
     301    16 
     302    >>> device["id"] != None 
     303    True 
     304    >>> device["sessionid"] != None 
     305    True 
     306    >>> device["descr"] != None 
     307    True 
     308    >>> device["serialno"] != None 
     309    True 
     310    >>> device["prodno"] != None 
     311    True 
     312    >>> device["prodname"] != None 
     313    True 
     314    >>> device["devicetype"] != None 
     315    True 
     316    >>> device["protocolver"] != None 
     317    True 
     318    >>> device["time"] != None 
     319    True 
     320    >>> device["status"] != None 
     321    True 
     322    >>> device["location"] != None 
     323    True 
     324    >>> device["verticalposition"] != None 
     325    True 
     326    >>> device["owner"] != None 
     327    True 
     328    >>> device["recordnumber"] != None 
     329    True 
     330    >>> device["data_time"] != None 
     331    True 
     332    >>> device["data_sessionid"] != None 
    325333    True 
    326334    """ 
    327335 
    328336    def __init__(self, device, xmlns): 
     337        super(Device, self).__init__({}) 
    329338        for key, value in device.attrib.items(): 
    330339            key = key.lower() 
    331             super(Device, self).__setattr__(key, value) 
     340            self[key] = value 
    332341        for elem in device.getchildren(): 
    333342            tag = XMLNS_PATTERN.search(elem.tag).groups()[1].lower() 
     
    335344                for subelem in elem.getchildren(): 
    336345                    tag = XMLNS_PATTERN.search(subelem.tag).groups()[1].lower() 
    337                     super(Device, self).__setattr__(tag, subelem.text) 
     346                    self[tag] = subelem.text 
    338347            elif tag == "data": 
    339348                for key, value in elem.attrib.items(): 
     
    343352                    elif key == "sessionid": 
    344353                        key = "data_sessionid" 
    345                     super(Device, self).__setattr__(key, value) 
     354                    self[key] = value 
    346355                self.sensors = [Sensor(sensor, xmlns) for sensor 
    347356                                in elem.findall(xmlns + "SensorData")] 
    348357            else: 
    349                 super(Device, self).__setattr__(tag, elem.text) 
     358                self[tag] = elem.text 
    350359 
    351360