From b26c89652869e903333e83271ab17ebda06bf02f Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Thu, 9 Jan 2020 22:37:41 +0000 Subject: [PATCH] Added notes on type conversion --- .../common/flask_labthings/fields.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/openflexure_microscope/common/flask_labthings/fields.py b/openflexure_microscope/common/flask_labthings/fields.py index 46e04e86..e7ae7bbd 100644 --- a/openflexure_microscope/common/flask_labthings/fields.py +++ b/openflexure_microscope/common/flask_labthings/fields.py @@ -11,7 +11,13 @@ 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 +""" +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, @@ -48,10 +54,11 @@ def to_string(o): # Map of Python type conversions -DEFAULT_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