Skip to content
Snippets Groups Projects
Commit edf7d590 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

MAINT: refactored the convert to value function

parent 27605bd7
No related branches found
No related tags found
2 merge requests!57RELEASE 0.7.3,!52F refactor high level api
Pipeline #19493 canceled
...@@ -31,7 +31,10 @@ This is refactored from apiutils. ...@@ -31,7 +31,10 @@ This is refactored from apiutils.
""" """
from caosdb.common.datatype import (BOOLEAN, DATETIME, DOUBLE, FILE, INTEGER, from caosdb.common.datatype import (BOOLEAN, DATETIME, DOUBLE, FILE, INTEGER,
REFERENCE, TEXT, is_list_datatype, get_list_datatype) REFERENCE, TEXT,
is_list_datatype,
get_list_datatype,
is_reference)
import caosdb as db import caosdb as db
from .apiutils import get_type_of_entity_with from .apiutils import get_type_of_entity_with
...@@ -39,7 +42,7 @@ from .apiutils import get_type_of_entity_with ...@@ -39,7 +42,7 @@ from .apiutils import get_type_of_entity_with
from typing import Any, Optional, List, Union, Dict from typing import Any, Optional, List, Union, Dict
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime
@dataclass @dataclass
class CaosDBPropertyMetaData: class CaosDBPropertyMetaData:
...@@ -230,50 +233,67 @@ class CaosDBPythonEntity(object): ...@@ -230,50 +233,67 @@ class CaosDBPythonEntity(object):
""" """
self._id = idx self._id = idx
def _type_converted_list(self, val, pr): def _type_converted_list(self,
"""Convert a list to a python list of the correct type.""" val: List,
prrealpre = pr.replace("&lt;", "<").replace("&gt;", ">") pr: str):
prreal = prrealpre[prrealpre.index("<") + 1:prrealpre.rindex(">")] """
Convert a list to a python list of the correct type.
val: List
The value of a property containing the list.
pr: str
The datatype according to the database entry.
"""
if not is_list_datatype(pr):
raise RuntimeError("Not a list.")
prreal = get_list_datatype(pr)
lst = [self._type_converted_value(i, prreal) for i in val] lst = [self._type_converted_value(i, prreal) for i in val]
return ([i[0] for i in lst], lst[0][1]) return ([i[0] for i in lst], lst[0][1])
def _type_converted_value(self, val, pr): def _type_converted_value(self,
"""Convert val to the correct type which is indicated by the database val: Any,
pr: str):
"""
Convert val to the correct type which is indicated by the database
type string in pr. type string in pr.
Returns a tuple with two entries: References with ids will be turned into CaosDBPythonUnresolvedReference.
- the converted value
- True if the value has to be interpreted as an id acting as a reference
If datatype is None return the tuple:
(value, False)
""" """
if val is None: if val is None:
return (None, False) return None
elif pr is None: elif pr is None:
return (val, False) return val
elif pr == DOUBLE: elif pr == DOUBLE:
return (float(val), False) return float(val)
elif pr == BOOLEAN: elif pr == BOOLEAN:
return (bool(val), False) return bool(val)
elif pr == INTEGER: elif pr == INTEGER:
return (int(val), False) return int(val)
elif pr == TEXT: elif pr == TEXT:
return (val, False) return str(val)
elif pr == FILE: elif pr == FILE:
return (int(val), False) return int(val)
elif pr == REFERENCE: elif pr == REFERENCE:
return (int(val), True) return CaosDBPythonUnresolvedReference(val)
elif pr == DATETIME: elif pr == DATETIME:
return (val, False) return val
elif pr[0:4] == "LIST": elif is_list_datatype(pr):
return self._type_converted_list(val, pr) return self._type_converted_list(val, pr)
elif isinstance(val, db.Entity): elif isinstance(val, db.Entity):
return (convert_to_python_object(val), False) return convert_to_python_object(val)
else: else:
return (int(val), True) # Generic references to entities:
return CaosDBPythonUnresolvedReference(val)
def _parse_datetime(self, val):
"""
Convert val into a datetime object.
"""
# TODO: try different representations
return datetime.strptime("%Y-%m-%d %H:%M:%S", val)
def attribute_as_list(self, name): def attribute_as_list(self, name):
"""This is a workaround for the problem that lists containing only one """This is a workaround for the problem that lists containing only one
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment