Пример #1
0
 def _validate_values(self, values):
     """
         Method ensures that the passed value(s) can be cast to the same dtype, i.e. that
         associated with this property or the inferred dtype of the first entry of the values list.
     """
     for v in values:
         try:
             dtypes.get(v, self.dtype)
         except Exception as ex:
             return False
     return True
Пример #2
0
 def _validate_values(self, values):
     """
         Method ensures that the passed value(s) can be cast to the
         same dtype, i.e. that associated with this property or the
         inferred dtype of the first entry of the values list.
     """
     for v in values:
         try:
             dtypes.get(v, self.dtype)
         except Exception:
             return False
     return True
Пример #3
0
    def __init__(self, data=None, uncertainty=None, unit=None, dtype=None, definition=None, reference=None,
                 filename=None, encoder=None, checksum=None, comment=None, value=None):
        if data is None and value is None:
            raise TypeError("either data or value has to be set")
        if data is not None and value is not None:
            raise TypeError("only one of data or value can be set")

        self._property = None
        self._unit = unit
        self._uncertainty = uncertainty
        self._dtype = dtype
        self._definition = definition
        self._reference = reference
        self._filename = filename
        self._comment = comment
        self._encoder = encoder

        if value is not None:
            # assign value directly (through property would raise a change-event)
            self._value = dtypes.get(value, self._dtype, self._encoder)
        elif data is not None:
            if dtype is None:
                self._dtype = dtypes.infer_dtype(data)
            self._value = data

        self._checksum_type = None
        if checksum is not None:
            self.checksum = checksum
Пример #4
0
 def pseudo_values(self, new_string):
     """
         First, try to check if the new value fits in the parent property's
         dtype. If it does, then update the value.
     """
     prop_dtype = self.parent.dtype
     new_value = dtypes.get(new_string, prop_dtype)
     self.parent._value[self._index] = new_value
Пример #5
0
 def dtype(self, new_type):
     # check if this is a valid type
     if not dtypes.valid_type(new_type):
         raise AttributeError("'%s' is not a valid type." % new_type)
     # we convert the value if possible
     old_type = self._dtype
     old_value = dtypes.set(self._value, self._dtype)
     try:
         new_value = dtypes.get(old_value, new_type)
     except:
         # cannot convert, try the other way around
         try:
             old_value = dtypes.set(self._value, new_type)
             new_value = dtypes.get(old_value, new_type)
         except:
             #doesn't work either, therefore refuse
             raise ValueError("cannot convert '%s' from '%s' to '%s'" % (self.value, old_type, new_type))
     self._value = new_value
     self._dtype = new_type
Пример #6
0
 def dtype(self, new_type):
     # check if this is a valid type
     if not dtypes.valid_type(new_type):
         raise AttributeError("'%s' is not a valid type." % new_type)
     # we convert the value if possible
     old_type = self._dtype
     old_value = dtypes.set(self._value, self._dtype, self._encoder)
     try:
         new_value = dtypes.get(old_value, new_type, self._encoder)
     except:
         # cannot convert, try the other way around
         try:
             old_value = dtypes.set(self._value, new_type, self._encoder)
             new_value = dtypes.get(old_value, new_type, self._encoder)
         except:
             #doesn't work either, therefore refuse
             raise ValueError("cannot convert '%s' from '%s' to '%s'" % (self.value, old_type, new_type))
     self._value = new_value
     self._dtype = new_type
Пример #7
0
 def value(self, new_value):
     if new_value is None:
         return
     if isinstance(new_value, str):
         if new_value[0] == "[" and new_value[-1] == "]":
             new_value = new_value[1:-1].split(",")
     if not isinstance(new_value, list):
         new_value = [new_value]
     if len(new_value) == 0:
         return
     if self._dtype is None:
         self._dtype = dtypes.infer_dtype(new_value[0])
     if not self._validate_values(new_value):
         raise ValueError("odml.Property.value: passed values are not of consistent type!")
     self._value = [dtypes.get(v, self.dtype) for v in new_value]
Пример #8
0
 def value(self, new_value):
     if new_value is None:
         return
     if isinstance(new_value, str):
         if new_value[0] == "[" and new_value[-1] == "]":
             new_value = new_value[1:-1].split(",")
     if not isinstance(new_value, list):
         new_value = [new_value]
     if len(new_value) == 0:
         return
     if self._dtype is None:
         self._dtype = dtypes.infer_dtype(new_value[0])
     if not self._validate_values(new_value):
         raise ValueError("odml.Property.value: passed values are not of "
                          "consistent type!")
     self._value = [dtypes.get(v, self.dtype) for v in new_value]
Пример #9
0
    def __init__(self, data=None, uncertainty=None, unit=None, dtype=None,
                 definition=None, reference=None, comment=None, value=None):
        if data is None and value is None:
            raise TypeError("either data or value has to be set")
        if data is not None and value is not None:
            raise TypeError("only one of data or value can be set")

        self._property = None
        self._unit = unit
        self._uncertainty = uncertainty
        self._dtype = dtype
        self._definition = definition
        self._reference = reference
        self._comment = comment

        if value is not None:
            # assign value directly (through property would raise a change-event)
            self._value = dtypes.get(value, self._dtype)
        elif data is not None:
            if dtype is None:
                self._dtype = dtypes.infer_dtype(data)
            self._value = data
Пример #10
0
 def value(self, new_string):
     self._value = dtypes.get(new_string, self._dtype)
Пример #11
0
 def test_dtype_none(self):
     self.assertEqual(typ.get({'name': 'Marie'}), "{'name': 'Marie'}")
Пример #12
0
 def value(self, new_string):
     self._value = dtypes.get(new_string, self._dtype, self._encoder)
Пример #13
0
 def date(self, new_value):
     self._date = dtypes.get(new_value, "date")
 def test_dtype_none(self):
     self.assertEqual(typ.get({'name': 'Marie'}), "{'name': 'Marie'}")