Parse [PATTERNS]

This commit is contained in:
WQY\qiong
2022-11-12 16:42:30 +08:00
parent 375001acd6
commit 9d7420df8f

View File

@@ -91,3 +91,39 @@ def delete_pattern_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
def delete_pattern(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, delete_pattern_cache(name, cs))
def inp_in_demand(section: list[str]) -> ChangeSet:
descs = {}
patterns: dict[str, list[float]] = {}
count = len(section)
for i in range(0, count):
if section[i].startswith(';'):
# this is description
next = i + 1
if next < count and section[next].startswith(';') == False:
next_tokens = section[next].split()
descs[next_tokens[0]] = section[i].removeprefix(';')
continue
tokens = section[i].split()
if tokens[0] not in patterns:
patterns[tokens[0]] = []
for token in tokens[1:]:
patterns[tokens[0]].append(float(token))
cs = ChangeSet()
for id, factors in patterns.items():
cs.append({'operation': API_ADD, 'type': 'pattern', 'id' : id, 'factors' : factors})
return cs
def inp_out_demand(name: str) -> list[str]:
lines = []
objs = read_all(name, f"select * from patterns order by _order")
for obj in objs:
id = obj['id']
factor = obj['factor']
lines.append(f'{id} {factor}')
return lines