Parse [RESERVOIRS]

This commit is contained in:
WQY\qiong
2022-11-12 14:51:03 +08:00
parent 749361fd47
commit fb9bef103a
3 changed files with 46 additions and 12 deletions

View File

@@ -33,11 +33,11 @@ class InpTitle:
def inp_in_title(section: list[str]) -> ChangeSet:
title = InpTitle(section)
cs = ChangeSet({'operation' : API_ADD, 'type': 'title', 'value' : title.value})
obj = InpTitle(section)
cs = ChangeSet({'operation' : API_ADD, 'type': 'title', 'value' : obj.value})
return cs
def inp_out_title(name: str) -> list[str]:
title = str(get_title(name)['value'])
return title.split('\n')
obj = str(get_title(name)['value'])
return obj.split('\n')

View File

@@ -138,19 +138,19 @@ class InpJunction:
def inp_in_junction(section: list[str]) -> ChangeSet:
cs = ChangeSet()
for s in section:
junction = InpJunction(s)
cs.append({'operation': API_ADD, 'type': 'junction', 'id': junction.id, 'elevation': junction.elevation, 'demand': junction.demand, 'pattern': junction.pattern})
obj = InpJunction(s)
cs.append({'operation': API_ADD, 'type': 'junction', 'id': obj.id, 'elevation': obj.elevation, 'demand': obj.demand, 'pattern': obj.pattern})
return cs
def inp_out_junction(name: str) -> list[str]:
lines = []
junctions = read_all(name, 'select * from junctions')
for j in junctions:
id = j['id']
elev = j['elevation']
demand = j['demand'] if j['demand'] != None else ''
pattern = j['pattern'] if j['pattern'] != None else ''
objs = read_all(name, 'select * from junctions')
for obj in objs:
id = obj['id']
elev = obj['elevation']
demand = obj['demand'] if obj['demand'] != None else ''
pattern = obj['pattern'] if obj['pattern'] != None else ''
desc = ';'
lines.append(f'{id} {elev} {demand} {pattern} {desc}')
return lines

View File

@@ -114,3 +114,37 @@ def delete_reservoir_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
def delete_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, delete_reservoir_cache(name, cs))
class InpReservoir:
def __init__(self, line: str) -> None:
tokens = line.split()
num = len(tokens)
has_desc = tokens[-1].startswith(';')
num_without_desc = (num - 1) if has_desc else num
self.id = str(tokens[0])
self.head = float(tokens[1])
self.pattern = float(tokens[2]) if num_without_desc >= 3 else None
self.desc = str(tokens[-1]) if has_desc else None
def inp_in_reservoir(section: list[str]) -> ChangeSet:
cs = ChangeSet()
for s in section:
obj = InpReservoir(s)
cs.append({'operation': API_ADD, 'type': 'reservoir', 'id': obj.id, 'head': obj.head, 'pattern': obj.pattern})
return cs
def inp_out_junction(name: str) -> list[str]:
lines = []
objs = read_all(name, 'select * from junctions')
for obj in objs:
id = obj['id']
head = obj['head']
pattern = obj['pattern'] if obj['pattern'] != None else ''
desc = ';'
lines.append(f'{id} {head} {pattern} {desc}')
return lines