Exemplo n.º 1
0
def test_definition2dtype_errors():
    r"""Check that error raised if type not specified."""
    assert_raises(KeyError, ScalarMetaschemaProperties.definition2dtype, {})
    assert_raises(RuntimeError, ScalarMetaschemaProperties.definition2dtype,
                  {'type': 'float'})
    assert_equal(ScalarMetaschemaProperties.definition2dtype({'type': 'bytes'}),
                 np.dtype((ScalarMetaschemaProperties._valid_types['bytes'])))
Exemplo n.º 2
0
    def _generate_data(cls, typedef):
        r"""Generate mock data for the specified type.

        Args:
            typedef (dict): Type definition.

        Returns:
            object: Python object of the specified type.

        """
        dtype = ScalarMetaschemaProperties.definition2dtype(typedef)
        if typedef['type'] == '1darray':
            out = np.zeros(typedef.get('length', 2), dtype)
        elif typedef['type'] == 'ndarray':
            out = np.zeros(typedef['shape'], dtype)
        else:
            out = np.zeros(1, dtype)[0]
        out = units.add_units(out, typedef.get('units', ''))
        return out
Exemplo n.º 3
0
    def transform_type(cls, obj, typedef=None):
        r"""Transform an object based on type info.

        Args:
            obj (object): Object to transform.
            typedef (dict, optional): Type definition that should be used to
                transform the object. Defaults to None.

        Returns:
            object: Transformed object.

        """
        if typedef is None:
            return obj
        typedef0 = cls.encode_type(obj)
        typedef1 = copy.deepcopy(typedef0)
        typedef1.update(**typedef)
        dtype = ScalarMetaschemaProperties.definition2dtype(typedef1)
        arr = cls.to_array(obj).astype(dtype, casting='same_kind')
        out = cls.from_array(arr, unit_str=typedef0.get('units', None), dtype=dtype)
        out = cls.as_python_type(out, typedef)
        return units.convert_to(out, typedef1.get('units', None))
Exemplo n.º 4
0
    def decode_data(cls, obj, typedef):
        r"""Decode an object.

        Args:
            obj (string): Encoded object to decode.
            typedef (dict): Type definition that should be used to decode the
                object.

        Returns:
            object: Decoded object.

        """
        bytes = backwards.base64_decode(obj.encode('ascii'))
        dtype = ScalarMetaschemaProperties.definition2dtype(typedef)
        arr = np.frombuffer(bytes, dtype=dtype)
        # arr = np.fromstring(bytes, dtype=dtype)
        if 'shape' in typedef:
            arr = arr.reshape(typedef['shape'])
        out = cls.from_array(arr, unit_str=typedef.get('units', None), dtype=dtype)
        # Cast numpy type to native python type if they are equivalent
        out = cls.as_python_type(out, typedef)
        return out
Exemplo n.º 5
0
    def decode_data(cls, obj, typedef):
        r"""Decode an object.

        Args:
            obj (string): Encoded object to decode.
            typedef (dict): Type definition that should be used to decode the
                object.

        Returns:
            object: Decoded object.

        """
        bytes = base64.decodebytes(obj.encode('ascii'))
        dtype = ScalarMetaschemaProperties.definition2dtype(typedef)
        arr = np.frombuffer(bytes, dtype=dtype)
        # arr = np.fromstring(bytes, dtype=dtype)
        if 'shape' in typedef:
            arr = arr.reshape(typedef['shape'])
        out = cls.from_array(arr,
                             unit_str=typedef.get('units', None),
                             dtype=dtype,
                             typedef=typedef)
        return out