Index: sodar/branches/scintec-branch/sodar/scintec/maindata.py =================================================================== --- sodar/branches/scintec-branch/sodar/scintec/maindata.py (revision 268) +++ sodar/branches/scintec-branch/sodar/scintec/maindata.py (revision 269) @@ -39,24 +39,20 @@ ... 'error':'0'} True - >>> main_data[0][0].z - '10' - >>> main_data[0]['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - >>> main_data[0]['10'].speed - '99.99' + >>> main_data[0](10) == {'z':'10', 'speed':'99.99', 'dir':'999.9', + ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', + ... 'error':'0'} + True >>> main_data[0][-1] == {'z':'200', 'speed':'99.99', 'dir':'999.9', ... 'W':'-0.07', 'sigW':'99.99', 'bck':'9.99E+37', ... 'error':'0'} True - >>> main_data[0][-1].dir - '999.9' - >>> main_data[0]['200'] == {'z':'200', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.07', 'sigW':'99.99', 'bck':'9.99E+37', - ... 'error':'0'} - True - >>> main_data[0]['200'].W - '-0.07' + >>> main_data[0](200) == {'z':'200', 'speed':'99.99', 'dir':'999.9', + ... 'W':'-0.07', 'sigW':'99.99', 'bck':'9.99E+37', + ... 'error':'0'} + True + >>> main_data(datetime(2009, 11, 17, 0, 30)).stop + datetime.datetime(2009, 11, 17, 0, 30) + >>> main_data(datetime(2009, 11, 17, 0, 0),True).start + datetime.datetime(2009, 11, 17, 0, 0) Parse the last profile: @@ -70,27 +66,23 @@ ['z', 'speed', 'dir', 'W', 'sigW', 'bck', 'error'] >>> main_data[-1][0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.32', 'sigW':'99.99', 'bck':'9.99E+37', - ... 'error':'0'} - True - >>> main_data[-1][0].z - '10' - >>> main_data[-1]['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.32', 'sigW':'99.99', 'bck':'9.99E+37', - ... 'error':'0'} - True - >>> main_data[-1]['10'].speed - '99.99' + ... 'W':'-0.32', 'sigW':'99.99', 'bck':'9.99E+37', + ... 'error':'0'} + True + >>> main_data[-1](10) == {'z':'10', 'speed':'99.99', 'dir':'999.9', + ... 'W':'-0.32', 'sigW':'99.99', 'bck':'9.99E+37', + ... 'error':'0'} + True >>> main_data[-1][-1] == {'z':'200', 'speed':'15.05', 'dir':'71.8', - ... 'W':'-0.19', 'sigW':'0.53', 'bck':'9.99E+37', - ... 'error':'0'} - True - >>> main_data[-1][-1].dir - '71.8' - >>> main_data[-1]['200'] == {'z':'200', 'speed':'15.05', 'dir':'71.8', - ... 'W':'-0.19', 'sigW':'0.53', 'bck':'9.99E+37', - ... 'error':'0'} - True - >>> main_data[-1]['200'].W - '-0.19' + ... 'W':'-0.19', 'sigW':'0.53', 'bck':'9.99E+37', + ... 'error':'0'} + True + >>> main_data[-1](200) == {'z':'200', 'speed':'15.05', 'dir':'71.8', + ... 'W':'-0.19', 'sigW':'0.53', 'bck':'9.99E+37', + ... 'error':'0'} + True + >>> main_data(datetime(2009, 11, 18, 0, 0)).stop + datetime.datetime(2009, 11, 18, 0, 0) + >>> main_data(datetime(2009, 11, 17, 23, 0),True).start + datetime.datetime(2009, 11, 17, 23, 0) """ @@ -171,5 +163,5 @@ Get a profile by timestamp - maindata_instance(timestamp[,start]) -> + MainData_instance(timestamp[,start]) -> Where: @@ -196,10 +188,10 @@ Traceback (most recent call last): ... - IndexError: 'MainData' object index out of range + ValueError: Timestamp datetime.datetime(2009, 11, 16, 0, 30) + not found in 'MainData' object >>> main_data('OK') Traceback (most recent call last): ... - TypeError: 'MainData' object indices must be datetime.datetime - instances + ValueError: Timestamp 'OK' not found in 'MainData' object """ @@ -211,11 +203,9 @@ if boundary == timestamp: return profile - if isinstance(timestamp,datetime): - raise IndexError, ' '.join([repr(self.__class__.__name__), - 'object index out of range',]) - else: - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'object indices must be', - 'datetime.datetime instances',]) + raise ValueError, ' '.join(['Timestamp', + repr(timestamp), + 'not found in', + repr(self.__class__.__name__), + 'object']) class Profile(list): @@ -239,31 +229,23 @@ ['z', 'speed', 'dir', 'W', 'sigW', 'bck', 'error'] - Access observations by sequence number and variables by attribute name: + Access observations by sequence number: >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', ... 'error':'0'} True - >>> profile[0].z - '10' >>> profile[-1] == {'z':'200', 'speed':'99.99', 'dir':'999.9', ... 'W':'-0.07', 'sigW':'99.99', 'bck':'9.99E+37', ... 'error':'0'} True - >>> profile[-1].speed - '99.99' - - Access observations by elevation and variables by attribute name: - >>> profile['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - >>> profile['10'].dir - '999.9' - >>> profile['200'] == {'z':'200', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.07', 'sigW':'99.99', 'bck':'9.99E+37', - ... 'error':'0'} - True - >>> profile['200'].W - '-0.07' + + Access observations by elevation: + >>> profile(10) == {'z':'10', 'speed':'99.99', 'dir':'999.9', + ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', + ... 'error':'0'} + True + >>> profile(200) == {'z':'200', 'speed':'99.99', 'dir':'999.9', + ... 'W':'-0.07', 'sigW':'99.99', 'bck':'9.99E+37', + ... 'error':'0'} + True """ @@ -292,17 +274,17 @@ ['z', 'speed', 'dir', 'W', 'sigW', 'bck', 'error'] - Access observations by sequence number and variables by attribute name: + Access observations by sequence number: >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', ... 'error':'0'} True - >>> profile[0].z - '10' >>> profile[-1] == {'z':'200', 'speed':'99.99', 'dir':'999.9', ... 'W':'-0.07', 'sigW':'99.99', 'bck':'9.99E+37', ... 'error':'0'} True - >>> profile[-1].speed - '99.99' + >>> profile[100000] + Traceback (most recent call last): + ... + IndexError: list index out of range """ @@ -341,357 +323,51 @@ in profile_block[2:]]) - def __getitem__(self,key): - """ - Get a observation by elevation. - - profile_instance[elevation] -> + def __call__(self,elevation): + """ + Get an observation by elevation. + + Profile_instance(elevation) -> Where: - elevation is a str object object representing the elevation + elevation is a numeric or str object representing the elevation (value keyed by 'z') of the Observation object. - Access observations by elevation and variables by attribute name: + Access observations by elevation: >>> profile = Profile(good_profile) - >>> profile['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', + >>> profile(10) == {'z':'10', 'speed':'99.99', 'dir':'999.9', + ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', + ... 'error':'0'} + True + >>> profile(200) == {'z':'200', 'speed':'99.99', 'dir':'999.9', + ... 'W':'-0.07', 'sigW':'99.99', 'bck':'9.99E+37', + ... 'error':'0'} + True + >>> profile(100000) + Traceback (most recent call last): + ... + ValueError: Elevation 100000 not found in 'Profile' object + >>> profile('10') == {'z':'10', 'speed':'99.99', 'dir':'999.9', ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', ... 'error':'0'} True - >>> profile['10'].dir - '999.9' - >>> profile['200'] == {'z':'200', 'speed':'99.99', 'dir':'999.9', + >>> profile('200') == {'z':'200', 'speed':'99.99', 'dir':'999.9', ... 'W':'-0.07', 'sigW':'99.99', 'bck':'9.99E+37', ... 'error':'0'} True - >>> profile['200'].W - '-0.07' - >>> profile[100000] - Traceback (most recent call last): - ... - IndexError: list index out of range - >>> profile['100000'] - Traceback (most recent call last): - ... - TypeError: list indices must be integers - """ - - try: - return super(self.__class__, self).__getitem__(key) - except TypeError: - for observation in self: - if observation['z'] == key: - return observation - return super(self.__class__, self).__getitem__(key) - - def __setitem__(self,key,value): - """ - Protect items. - - profile_instance[object1] = object2 - - Raise an Exception if attempting to mutate a profile: - >>> profile = Profile(good_profile) - >>> profile[0] = 'OK' - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - >>> profile['10'] = 'OK' - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - >>> profile[100000] = 'OK' - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - >>> profile[100000] - Traceback (most recent call last): - ... - IndexError: list index out of range - >>> profile['100000'] = 'OK' - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - >>> profile['100000'] - Traceback (most recent call last): - ... - TypeError: list indices must be integers - - Without affecting the observation: - >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - >>> profile['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def __delitem__(self,key): - """ - Protect items. - - del profile_instance[object] - - Raise an Exception if attempting to mutate a profile: - >>> profile = Profile(good_profile) - >>> del profile[0] - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - >>> del profile['10'] - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - >>> del profile[100000] - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - >>> del profile['100000'] - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - - Without affecting the observation: - >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - >>> profile['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def __setslice__(self,i,j,seq): - """ - Protect items. - - profile_instance[i:j] = seq - - Raise an Exception if attempting to mutate a profile: - >>> profile = Profile(good_profile) - >>> profile[0:10] = 'OK' - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - - Without affecting the observations: - >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - >>> profile['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def __delslice__(self,i,j): - """ - Protect items. - - del profile_instance[i:j] - - Raise an Exception if attempting to mutate a profile: - >>> profile = Profile(good_profile) - >>> del profile[0:10] - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - - Without affecting the observations: - >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - >>> profile['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def append(self,item): - """ - Protect items. - - Raise an Exception if attempting to mutate a profile: - >>> profile = Profile(good_profile) - >>> profile.append('OK') - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - - Without affecting the observations: - >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def extend(self,seq): - """ - Protect items. - - Raise an Exception if attempting to mutate a profile: - >>> profile = Profile(good_profile) - >>> profile.extend('OK') - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - - Without affecting the observations: - >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def insert(self,i,item): - """ - Protect items. - - Raise an Exception if attempting to mutate a profile: - >>> profile = Profile(good_profile) - >>> profile.insert(0,'OK') - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - - Without affecting the observations: - >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def pop(self,*args): - """ - Protect items. - - Raise an Exception if attempting to mutate a profile: - >>> profile = Profile(good_profile) - >>> profile.pop() - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - >>> profile.pop(0) - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - - Without affecting the observations: - >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - >>> profile[-1] == {'z':'200', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.07', 'sigW':'99.99', 'bck':'9.99E+37', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def remove(self,i): - """ - Protect items. - - Raise an Exception if attempting to mutate a profile: - >>> profile = Profile(good_profile) - >>> observation = profile[0] - >>> profile.remove(observation) - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - - Without affecting the observations: - >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def reverse(self): - """ - Protect items. - - Raise an Exception if attempting to mutate a profile: - >>> profile = Profile(good_profile) - >>> profile.reverse() - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - - Without affecting the observations: - >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def sort(self,**kwargs): - """ - Protect items. - - Raise an Exception if attempting to mutate a profile: - >>> profile = Profile(good_profile) - >>> profile.sort() - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - >>> profile.sort(cmp=lambda x,y: cmp(x.lower(), y.lower())) - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - >>> profile.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()), - ... key=str.lower) - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - >>> profile.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()), - ... key=str.lower, - ... reverse=True) - Traceback (most recent call last): - ... - TypeError: 'Profile' instance's items are read-only - - Without affecting the observations: - >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) + >>> profile('100000') + Traceback (most recent call last): + ... + ValueError: Elevation '100000' not found in 'Profile' object + """ + + for observation in self: + if observation['z'] == str(int(elevation)): + return observation + raise ValueError, ' '.join(['Elevation', + repr(elevation), + 'not found in', + repr(self.__class__.__name__), + 'object']) class Observation(dict): @@ -709,20 +385,4 @@ ... 'error':'0'} True - - Access observation variables as attributes: - >>> observation.z - '10' - >>> observation.speed - '99.99' - >>> observation.dir - '999.9' - >>> observation.W - '-0.05' - >>> observation.sigW - '0.40' - >>> observation.bck - '5.46E+03' - >>> observation.error - '0' """ @@ -737,5 +397,5 @@ data is a list of str object representing variable values, - variables is a list of str objects representing variable labels. + variables is a list of str objects representing variable names. Parse a known good observation: @@ -751,5 +411,5 @@ Traceback (most recent call last): ... - AttributeError: Same number of attributes and values required in + ValueError: Same number of variable names and values required in 'Observation' object @@ -759,5 +419,5 @@ Traceback (most recent call last): ... - AttributeError: Same number of attributes and values required in + ValueError: Same number of variable names and values required in 'Observation' object """ @@ -770,355 +430,8 @@ else: # Unless there are an unequal number of variable names/values. - raise AttributeError, ' '.join(['Same number of attributes', - 'and values required in', - repr(self.__class__.__name__), - 'object',]) - - def __getattr__(self,name): - """ - Get a variable value by name. - - observation_instance.variable_name -> variable_value - - Access observation variables as attributes: - >>> observation = Observation(good_observation,good_variables) - >>> observation.z - '10' - >>> observation.speed - '99.99' - >>> observation.dir - '999.9' - >>> observation.W - '-0.05' - >>> observation.sigW - '0.40' - >>> observation.bck - '5.46E+03' - >>> observation.error - '0' - - Raise an Exception for an undefined attribute: - >>> observation.y - Traceback (most recent call last): - ... - AttributeError: 'Observation' object has no attribute 'y' - - Allow the creation and access of attributes other than variables. - >>> observation.y = 'OK' - >>> observation.y - 'OK' - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - # Attempt attribute access by dictionary key. - if name in self: - return self[name] - else: - raise AttributeError, ' '.join([repr(self.__class__.__name__), - 'object has no attribute', - repr(name),]) - - def __setattr__(self,name,value): - """ - Protect variable values from overwriting. - - observation_instance.variable_name = variable_value - - But retrieve any other bound attribute values. - - Raise an Exception if attempting to rebind a variable: - >>> observation = Observation(good_observation,good_variables) - >>> observation.z = 'OK' - Traceback (most recent call last): - ... - AttributeError: Rebinding attribute 'z' disallowed in - 'Observation' object - - However, allow attributes other than variables to be bound: - >>> observation.y = 'OK' - >>> observation.y - 'OK' - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - if name in self: - # Protect observation variables - raise AttributeError, ' '.join(['Rebinding attribute', - repr(name), - 'disallowed in', - repr(self.__class__.__name__), - 'object',]) - else: - # Otherwise, do it the normal way. - super(self.__class__, self).__setattr__(name,value) - - def __delattr__(self,name): - """ - Protect variable values from deletion. - - del observation_instance.variable_name - - But delete any other bound attribute values. - - Raise an Exception if attempting to unbind a variable: - >>> observation = Observation(good_observation,good_variables) - >>> del observation.z - Traceback (most recent call last): - ... - AttributeError: Unbinding attribute 'z' disallowed in - 'Observation' object - - Raise an Exception for attempting to unbind an undefined attribute: - >>> del observation.y - Traceback (most recent call last): - ... - AttributeError: 'Observation' object has no attribute 'y' - - However, allow attributes other than variables to be unbound: - >>> observation.y = 'OK' - >>> del observation.y - >>> del observation.y - Traceback (most recent call last): - ... - AttributeError: y - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - if name in self: - # Protect observation variables - raise AttributeError, ' '.join(['Unbinding attribute', - repr(name), - 'disallowed in', - repr(self.__class__.__name__), - 'object',]) - else: - # Otherwise, do it the normal way. - super(self.__class__, self).__delattr__(name) - - def __setitem__(self,key,value): - """ - Protect items. - - observation_instance[variable_name] = variable_value - - Raise an Exception if attempting to mutate an observation: - >>> observation = Observation(good_observation,good_variables) - >>> observation['z'] = 'OK' - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - >>> observation['y'] = 'OK' - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - >>> observation.y - Traceback (most recent call last): - ... - AttributeError: 'Observation' object has no attribute 'y' - >>> observation.y = 'OK' - >>> observation['y'] = 'Not OK' - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - >>> observation.y - 'OK' - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def __delitem__(self,key): - """ - Protect items. - - observation_instance[variable_name] = variable_value - - Raise an Exception if attempting to mutate an observation: - >>> observation = Observation(good_observation,good_variables) - >>> del observation['z'] - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - >>> observation.y = 'OK' - >>> del observation['y'] - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def clear(self): - """ - Protect items. - - Raise an Exception if attempting to mutate an observation: - >>> observation = Observation(good_observation,good_variables) - >>> observation.clear() - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def pop(self,key,*args): - """ - Protect items. - - Raise an Exception if attempting to mutate an observation: - >>> observation = Observation(good_observation,good_variables) - >>> observation.pop('z') - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - >>> observation.pop('y') - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - >>> observation.pop('y','OK') - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def popitem(self): - """ - Protect items. - - Raise an Exception if attempting to mutate an observation: - >>> observation = Observation(good_observation,good_variables) - >>> observation.popitem() - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def setdefault(self,key,*args): - """ - Protect items. - - Raise an Exception if attempting to mutate an observation: - >>> observation = Observation(good_observation,good_variables) - >>> observation.setdefault('z') - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - >>> observation.setdefault('y') - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - >>> observation.setdefault('y','OK') - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) - - def update(self,*args,**kwargs): - """ - Protect items. - - Raise an Exception if attempting to mutate an observation: - >>> observation = Observation(good_observation,good_variables) - >>> observation.update({'UP':'up','DOWN':'down',}) - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - - Raise an Exception if attempting to mutate an observation: - >>> observation.update((('UP','up'),('DOWN','down'),)) - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - - Raise an Exception if attempting to mutate an observation: - >>> observation.update(UP='up',DOWN='down') - Traceback (most recent call last): - ... - TypeError: 'Observation' instance's items are read-only - - Without affecting the variables: - >>> observation == {'z':'10', 'speed':'99.99', 'dir':'999.9', - ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', - ... 'error':'0'} - True - """ - - raise TypeError, ' '.join([repr(self.__class__.__name__), - 'instance\'s items are read-only',]) + raise ValueError, ' '.join(['Same number of variable', + 'names and values required in', + repr(self.__class__.__name__), + 'object',]) def _test():