Commit 2fcd1b9a authored by Sean Fitzgibbon's avatar Sean Fitzgibbon
Browse files

minor refactor

parent b2e7cb71
......@@ -59,72 +59,7 @@ UNWANTED_SECTION_NAMES = [
]
UNWANTED_SECTIONS = {name: True for name in UNWANTED_SECTION_NAMES}
def is_number(s):
'''Test if string is a number'''
try:
float(s)
return True
except ValueError:
return False
def is_point(p):
'''Test if section looks like a point section'''
result = True
if len(p) != 5: result = False
if not all([is_number(n) for n in p[:-1]]): result = False
return result
def parse_point(p):
'''Parse a point section'''
return [float(p0) for p0 in p[:-1]] + [p[-1]]
def parse_contour(c0):
'''Convert contour section into a dictionary'''
c0_dict = {
'name': c0[0].replace('"',''),
}
points = []
for p in c0[1:]:
if p[0] == 'Closed':
c0_dict['closed'] = True
elif p[0] == 'Color':
c0_dict['color'] = p[1]
elif p[0] == 'GUID':
c0_dict['guid'] = p[1].replace('"','')
elif p[0] == 'Resolution':
c0_dict['resolution'] = float(p[1])
elif is_point(p):
points.append(parse_point(p)[:4])
else:
raise ValueError(f'Unknown property: ({p})')
c0_dict['points'] = np.array(points, dtype=np.float64)
return c0_dict
def parse_cell(cell):
'''Convert cell (asterisk) section into a dictionary'''
cell_dict = {}
for prop in cell[1:]:
if prop[0] == 'Color':
cell_dict['color'] = prop[1]
elif is_point(prop):
cell_dict['point'] = np.array(parse_point(prop)[:-1])
else:
raise ValueError(f'Unknown property: ({prop})')
return cell_dict
# --- section parsing ---
def _match_section(section, match):
'''checks whether the `type` of section is in the `match` dictionary
......@@ -251,6 +186,73 @@ def _flatten_subsection(subsection, _type, offset, parent):
offset += 1
yield _row
# --- data parsing ---
def is_number(s):
'''Test if string is a number'''
try:
float(s)
return True
except ValueError:
return False
def is_point(p):
'''Test if section looks like a point section'''
result = True
if len(p) != 5: result = False
if not all([is_number(n) for n in p[:-1]]): result = False
return result
def parse_point(p):
'''Parse a point section'''
return [float(p0) for p0 in p[:-1]] + [p[-1]]
def parse_contour(c0):
'''Convert contour section into a dictionary'''
c0_dict = {
'name': c0[0].replace('"',''),
}
points = []
for p in c0[1:]:
if p[0] == 'Closed':
c0_dict['closed'] = True
elif p[0] == 'Color':
c0_dict['color'] = p[1]
elif p[0] == 'GUID':
c0_dict['guid'] = p[1].replace('"','')
elif p[0] == 'Resolution':
c0_dict['resolution'] = float(p[1])
elif is_point(p):
points.append(parse_point(p)[:4])
else:
raise ValueError(f'Unknown property: ({p})')
c0_dict['points'] = np.array(points, dtype=np.float64)
return c0_dict
def parse_cell(cell):
'''Convert cell (asterisk) section into a dictionary'''
cell_dict = {}
for prop in cell[1:]:
if prop[0] == 'Color':
cell_dict['color'] = prop[1]
elif is_point(prop):
cell_dict['point'] = np.array(parse_point(prop)[:-1])
else:
raise ValueError(f'Unknown property: ({prop})')
return cell_dict
def to_data(sections):
# convert sections to dicts
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment