diff --git a/python/obitools3/utils.pxd b/python/obitools3/utils.pxd index 3a6db25..4d9849d 100644 --- a/python/obitools3/utils.pxd +++ b/python/obitools3/utils.pxd @@ -12,4 +12,6 @@ cdef str tostr(object string) cdef obitype_t get_obitype_single_value(object value) cdef obitype_t update_obitype(obitype_t obitype, object new_value) cdef obitype_t get_obitype_iterable_value(object value) -cdef obitype_t get_obitype(object value) \ No newline at end of file +cdef obitype_t get_obitype(object value) + +cdef object __etag__(str x) diff --git a/python/obitools3/utils.pyx b/python/obitools3/utils.pyx index acc884f..5d0bb83 100644 --- a/python/obitools3/utils.pyx +++ b/python/obitools3/utils.pyx @@ -15,6 +15,8 @@ from obitools3.dms.capi.obierrno cimport OBI_LINE_IDX_ERROR, \ OBI_ELT_IDX_ERROR #obi_errno +import re + #from obitools3.dms.obiseq cimport Nuc_Seq, Nuc_Seq_Stored @@ -156,3 +158,50 @@ cdef obitype_t get_obitype(object value) : else : return get_obitype_single_value(value) + + +__re_int__ = re.compile("^[+-]?[0-9]+$") +__re_float__ = re.compile("^[+-]?[0-9]+(\.[0-9]*)?([eE][+-]?[0-9]+)?$") +__re_str__ = re.compile("""^"[^"]*"|'[^']*'$""") +__re_dict__ = re.compile("""^\{\ * + ( + ("[^"]*"|'[^']*') + \ *:\ * + ([^,}]+| + "[^"]*"| + '[^']*' + ) + )? + (\ *,\ * + ("[^"]*"|'[^']*') + \ *:\ * + ([^,}]+| + "[^"]*"| + '[^']*' + ) + )*\ *\}$""", re.VERBOSE) + +__re_val__ = re.compile("""(("[^"]*"|'[^']*') *: *([^,}]+|"[^"]*"|'[^']*') *[,}] *)""") + +cdef object __etag__(str x): + cdef list elements + cdef tuple i + + if __re_int__.match(x): + v=int(x) + elif __re_float__.match(x): + v=float(x) + elif __re_str__.match(x): + v=x[1:-1] + elif x=='None': + v=None + elif x=='False': + v=False + elif x=='True': + v=True + elif __re_dict__.match(x): + elements=__re_val__.findall(x) + v=dict([(i[1][1:-1],__etag__(i[2])) for i in elements]) + else: + v=x + return v