Ejemplo n.º 1
0
Archivo: base.py Proyecto: Qigaoo/obspy
 def __setattr__(self, name, value):
     """
     Custom property implementation that works if the class is
     inheriting from AttribDict.
     """
     # Pass to the parent method if not a custom property.
     if name not in self._property_dict.keys():
         AttribDict.__setattr__(self, name, value)
         return
     attrib_type = self._property_dict[name]
     # If the value is None or already the correct type just set it.
     if (value is not None) and (type(value) is not attrib_type):
         # If it is a dict, and the attrib_type is no dict, than all
         # values will be assumed to be keyword arguments.
         if isinstance(value, dict):
             new_value = attrib_type(**value)
         else:
             new_value = attrib_type(value)
         if new_value is None:
             msg = 'Setting attribute "%s" failed. ' % (name)
             msg += 'Value "%s" could not be converted to type "%s"' % \
                 (str(value), str(attrib_type))
             raise ValueError(msg)
         value = new_value
     AttribDict.__setattr__(self, name, value)
     # If "name" is resource_id and value is not None, set the referred
     # object of the ResourceIdentifier to self.
     if name == "resource_id" and value is not None:
         self.resource_id.set_referred_object(self)
Ejemplo n.º 2
0
 def __setattr__(self, name, value):
     """
     Custom property implementation that works if the class is
     inheriting from AttribDict.
     """
     # Pass to the parent method if not a custom property.
     if name not in self._property_dict.keys():
         AttribDict.__setattr__(self, name, value)
         return
     attrib_type = self._property_dict[name]
     # If the value is None or already the correct type just set it.
     if (value is not None) and (type(value) is not attrib_type):
         # If it is a dict, and the attrib_type is no dict, than all
         # values will be assumed to be keyword arguments.
         if isinstance(value, dict):
             new_value = attrib_type(**value)
         else:
             new_value = attrib_type(value)
         if new_value is None:
             msg = 'Setting attribute "%s" failed. ' % (name)
             msg += 'Value "%s" could not be converted to type "%s"' % \
                 (str(value), str(attrib_type))
             raise ValueError(msg)
         value = new_value
     AttribDict.__setattr__(self, name, value)
     # If "name" is resource_id and value is not None, set the referred
     # object of the ResourceIdentifier to self.
     if name == "resource_id" and value is not None:
         self.resource_id.set_referred_object(self)
Ejemplo n.º 3
0
 def test_setattr(self):
     """
     Tests __setattr__ method of AttribDict class.
     """
     # 1
     ad = AttribDict()
     ad.test = 'NEW'
     self.assertEqual(ad['test'], 'NEW')
     self.assertEqual(ad.test, 'NEW')
     self.assertEqual(ad.get('test'), 'NEW')
     self.assertEqual(ad.__getattr__('test'), 'NEW')
     self.assertEqual(ad.__getitem__('test'), 'NEW')
     self.assertEqual(ad.__dict__['test'], 'NEW')
     self.assertEqual(ad.__dict__.get('test'), 'NEW')
     self.assertTrue('test' in ad)
     self.assertTrue('test' in ad.__dict__)
     # 2
     ad = AttribDict()
     ad.__setattr__('test', 'NEW')
     self.assertEqual(ad['test'], 'NEW')
     self.assertEqual(ad.test, 'NEW')
     self.assertEqual(ad.get('test'), 'NEW')
     self.assertEqual(ad.__getattr__('test'), 'NEW')
     self.assertEqual(ad.__getitem__('test'), 'NEW')
     self.assertEqual(ad.__dict__['test'], 'NEW')
     self.assertEqual(ad.__dict__.get('test'), 'NEW')
     self.assertTrue('test' in ad)
     self.assertTrue('test' in ad.__dict__)
Ejemplo n.º 4
0
        def __setattr__(self, name, value):
            """
            Custom property implementation that works if the class is
            inheriting from AttribDict.
            """
            # avoid type casting of 'extra' attribute, to make it possible to
            # control ordering of extra tags by using an OrderedDict for
            # 'extra'.
            if name == 'extra':
                dict.__setattr__(self, name, value)
                return
            # Pass to the parent method if not a custom property.
            if name not in self._property_dict.keys():
                AttribDict.__setattr__(self, name, value)
                return
            attrib_type = self._property_dict[name]
            # If the value is None or already the correct type just set it.
            if (value is not None) and (type(value) is not attrib_type):
                # If it is a dict, and the attrib_type is no dict, than all
                # values will be assumed to be keyword arguments.
                if isinstance(value, dict):
                    new_value = attrib_type(**value)
                else:
                    new_value = attrib_type(value)
                if new_value is None:
                    msg = 'Setting attribute "%s" failed. ' % (name)
                    msg += 'Value "%s" could not be converted to type "%s"' % \
                        (str(value), str(attrib_type))
                    raise ValueError(msg)
                value = new_value

            # Make sure all floats are finite - otherwise this is most
            # likely a user error.
            if attrib_type is float and value is not None:
                if not np.isfinite(value):
                    msg = "On %s object: Value '%s' for '%s' is " \
                          "not a finite floating point value." % (
                              type(self).__name__, str(value), name)

                    raise ValueError(msg)

            AttribDict.__setattr__(self, name, value)
            # if value is a resource id bind or unbind the resource_id
            if isinstance(value, ResourceIdentifier):
                if name == "resource_id":  # bind the resource_id to self
                    self.resource_id.set_referred_object(self, warn=False)
                else:  # else unbind to allow event scoping later
                    value._parent_key = None