From 4862c7a47482d55650ca41f21962718f2e9e690d Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Fri, 10 Jan 2020 19:44:50 +0000 Subject: [PATCH] Moved conflicting code into 'types' submodule --- .../common/flask_labthings/fields.py | 62 ----------------- .../common/flask_labthings/types.py | 66 +++++++++++++++++++ 2 files changed, 66 insertions(+), 62 deletions(-) create mode 100644 openflexure_microscope/common/flask_labthings/types.py diff --git a/openflexure_microscope/common/flask_labthings/fields.py b/openflexure_microscope/common/flask_labthings/fields.py index e7ae7bbd..68149fcd 100644 --- a/openflexure_microscope/common/flask_labthings/fields.py +++ b/openflexure_microscope/common/flask_labthings/fields.py @@ -1,64 +1,2 @@ # Marshmallow fields from marshmallow.fields import * - -# Marshmallow fields to JSON schema types -# Note: We shouldn't ever need to use this directly. We should go via the apispec converter -from apispec.ext.marshmallow.field_converter import DEFAULT_FIELD_MAPPING - -# Extra standard library Python types -from datetime import date, datetime, time, timedelta -from decimal import Decimal -from typing import Dict, List, Tuple, Union -from uuid import UUID - -""" -TODO: Use this to convert arbitrary dictionary into its own schema, for W3C TD - -First: Convert Python non-builtins to builtins using DEFAULT_BUILTIN_CONVERSIONS -Then match types of each element to Field using DEFAULT_TYPE_MAPPING -Finally convert Fields to JSON using converter (preferred due to extra metadata), or DEFAULT_FIELD_MAPPING -""" -# Python types to Marshmallow fields -DEFAULT_TYPE_MAPPING = { - bool: Boolean, - date: Date, - datetime: DateTime, - Decimal: Decimal, - float: Float, - int: Integer, - str: String, - time: Time, - timedelta: TimeDelta, - UUID: UUID, - dict: Dict, - Dict: Dict, -} - -# Functions to handle conversion of common Python types into serialisable Python types - - -def ndarray_to_list(o): - return o.tolist() - - -def to_int(o): - return int(o) - - -def to_float(o): - return float(o) - - -def to_string(o): - return str(o) - - -# Map of Python type conversions -DEFAULT_BUILTIN_CONVERSIONS = { - "numpy.ndarray": ndarray_to_list, - "numpy.int": to_int, - "fractions.Fraction": to_float, -} - -# Use with [x.__module__+"."+x.__name__ for x in inspect.getmro(type(POSSIBLE_MATCHER))] -# Resulting array will contain strings with the same format as keys in DEFAULT_BUILTIN_CONVERSIONS diff --git a/openflexure_microscope/common/flask_labthings/types.py b/openflexure_microscope/common/flask_labthings/types.py new file mode 100644 index 00000000..b0304368 --- /dev/null +++ b/openflexure_microscope/common/flask_labthings/types.py @@ -0,0 +1,66 @@ +# Marshmallow fields to JSON schema types +# Note: We shouldn't ever need to use this directly. We should go via the apispec converter +from apispec.ext.marshmallow.field_converter import DEFAULT_FIELD_MAPPING + +from . import fields + +# Extra standard library Python types +from datetime import date, datetime, time, timedelta +from decimal import Decimal +from typing import Dict, List, Tuple, Union +from uuid import UUID + +""" +TODO: Use this to convert arbitrary dictionary into its own schema, for W3C TD + +First: Convert Python non-builtins to builtins using DEFAULT_BUILTIN_CONVERSIONS +Then match types of each element to Field using DEFAULT_TYPE_MAPPING +Finally convert Fields to JSON using converter (preferred due to extra metadata), or DEFAULT_FIELD_MAPPING +""" +# Python types to Marshmallow fields +DEFAULT_TYPE_MAPPING = { + bool: fields.Boolean, + date: fields.Date, + datetime: fields.DateTime, + Decimal: fields.Decimal, + float: fields.Float, + int: fields.Integer, + str: fields.String, + time: fields.Time, + timedelta: fields.TimeDelta, + UUID: fields.UUID, + dict: fields.Dict, + Dict: fields.Dict, +} + +# Functions to handle conversion of common Python types into serialisable Python types + + +def ndarray_to_list(o): + return o.tolist() + + +def to_int(o): + return int(o) + + +def to_float(o): + return float(o) + + +def to_string(o): + return str(o) + + +# Map of Python type conversions +DEFAULT_BUILTIN_CONVERSIONS = { + "numpy.ndarray": ndarray_to_list, + "numpy.int": to_int, + "fractions.Fraction": to_float, +} + +# TODO: Deserialiser with inverse defaults +# TODO: Option to switch to .npy serialisation/deserialisation (or look for a better common array format) + +# Use with [x.__module__+"."+x.__name__ for x in inspect.getmro(type(POSSIBLE_MATCHER))] +# Resulting array will contain strings with the same format as keys in DEFAULT_BUILTIN_CONVERSIONS