Skip to content
Snippets Groups Projects

Refactor get_list_datatype

Merged Henrik tom Wörden requested to merge f-simple-schema-export into dev
2 files
+ 29
7
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -43,16 +43,27 @@ def LIST(datatype):
@@ -43,16 +43,27 @@ def LIST(datatype):
return "LIST<" + str(datatype) + ">"
return "LIST<" + str(datatype) + ">"
def get_list_datatype(datatype):
def get_list_datatype(datatype: str, strict: bool = False):
""" returns the datatype of the elements in the list """
""" returns the datatype of the elements in the list """
if not isinstance(datatype, str):
if not isinstance(datatype, str) or not datatype.lower().startswith("list"):
return None
if strict:
match = re.match("LIST(<|&lt;)(?P<datatype>.*)(>|&gt;)", datatype)
raise ValueError(f"Not a list dtype: {datatype}")
 
else:
 
return None
 
pattern = r"^[Ll][Ii][Ss][Tt]((<|&lt;)(?P<dtype1>.*)(>|&gt;)|\((?P<dtype2>.*)\))$"
 
match = re.match(pattern, datatype)
 
 
if match and "dtype1" in match.groupdict() and match.groupdict()["dtype1"] is not None:
 
return match.groupdict()["dtype1"]
 
 
elif match and "dtype2" in match.groupdict() and match.groupdict()["dtype2"] is not None:
 
return match.groupdict()["dtype2"]
if match is not None:
return match.group("datatype")
else:
else:
return None
if strict:
 
raise ValueError(f"Not a list dtype: {datatype}")
 
else:
 
return None
def is_list_datatype(datatype):
def is_list_datatype(datatype):
Loading