Parse [DEMANDS]

This commit is contained in:
WQY\qiong
2022-11-12 15:59:22 +08:00
parent 2403a1a75d
commit 22545f5626

View File

@@ -84,3 +84,49 @@ def set_demand_cache(name: str, cs: ChangeSet) -> BatchSqlChangeSet:
def set_demand(name: str, cs: ChangeSet) -> ChangeSet:
css = set_demand_cache(name, cs)
return execute_batch(name, css.redo_sql, css.undo_sql, css.redo_cs, css.undo_cs)
class InpDemand:
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.junction = str(tokens[0])
self.demand = float(tokens[1])
self.pattern = str(tokens[2]) if num_without_desc >= 3 else None
self.category = str(tokens[3]) if num_without_desc >= 4 else None
def inp_in_demand(section: list[str]) -> ChangeSet:
objs: dict[str, list[InpDemand]] = {}
for s in section:
# skip comment
if s.startswith(';'):
continue
obj = InpDemand(s)
if obj.junction not in objs:
objs[obj.junction] = []
objs[obj.junction].append(obj)
cs = ChangeSet()
for junction, demands in objs.items():
obj_cs = {'operation': API_ADD, 'type': 'demand', 'junction' : junction, 'demands' : []}
for obj in demands:
obj_cs['demands'].append({'demand': obj.demand, 'pattern' : obj.pattern, 'category': obj.category})
cs.append(obj_cs)
return cs
def inp_out_demand(name: str) -> list[str]:
lines = []
objs = read_all(name, f"select * from demands order by _order")
for obj in objs:
junction = obj['junction']
demand = obj['demand']
patten = obj['patten'] if obj['patten'] != None else ''
category = f";{obj['category']}" if obj['category'] != None else ';'
lines.append(f'{junction} {demand} {patten} {category}')
return lines