Optimize read performance

This commit is contained in:
WQY\qiong
2023-03-10 00:13:17 +08:00
parent e41abe362f
commit 1fda14c3cf
4 changed files with 210 additions and 117 deletions

View File

@@ -3,6 +3,7 @@ from .project import is_project_open, get_project_open_count, open_project, clos
from .project import copy_project
from .inp_in import read_inp, import_inp
from .inp_in_new import read_inp_new
from .inp_out import dump_inp, export_inp
from .database import API_ADD, API_UPDATE, API_DELETE

View File

@@ -1,132 +1,224 @@
from .project import *
from .database import ChangeSet, get_current_operation, set_restore_operation
from .sections import section_name
from .database import ChangeSet
from .sections import *
from .s1_title import inp_in_title_new
from .s2_junctions import inp_in_junction_new
from .s3_reservoirs import inp_in_reservoir_new
from .s4_tanks import inp_in_tank_new
from .s11_patterns import inp_in_pattern_new
from .s12_curves import inp_in_curve_new
from .s13_controls import inp_in_control_new
from .s14_rules import inp_in_rule_new
from .s21_times import inp_in_time_new
from .s22_report import inp_in_report_new
from .s23_options import inp_in_option_new
from .s27_backdrop import inp_in_backdrop_new
def parse_inp(project: str, inp: str) -> None:
section = ''
_UNKNOWN = 'UNKNOWN'
def scan1(project: str, inp: str) -> list[str]:
handlers = {
TITLE: inp_in_title_new, #1
PATTERNS: inp_in_pattern_new, #11
CURVES: inp_in_curve_new, #12
CONTROLS: inp_in_control_new, #13
RULES: inp_in_rule_new, #14
TIMES: inp_in_time_new, #21
REPORT: inp_in_report_new, #22
OPTIONS: inp_in_option_new, #23
BACKDROP: inp_in_backdrop_new,#27
}
curr_section = _UNKNOWN
prev_section = _UNKNOWN
inp_sections: list[str] = []
sections : dict[str, list[str]]= {}
for c in handlers.keys():
sections[c] = []
for line in open(inp):
line = line.strip()
if line == '':
if section == 'CONTROLS' or section == 'RULES':
pass
else:
section = ''
continue
if line.startswith('['):
is_section = False
the_section = _UNKNOWN
for s in section_name:
if line.startswith(f'[{s}'):
section = s
is_section = True
the_section = s
inp_sections.append(s)
break
if is_section:
prev_section = curr_section
if prev_section in handlers.keys():
handlers[prev_section](project, sections[prev_section])
sections[prev_section].clear()
curr_section = the_section
continue
elif line == '':
continue
if curr_section in handlers.keys():
sections[curr_section].append(line)
return inp_sections
def scan2(project: str, inp: str) -> None:
handlers = {
JUNCTIONS: inp_in_junction_new, #2
RESERVOIRS: inp_in_reservoir_new, #3
TANKS: inp_in_tank_new, #4
}
curr_section = _UNKNOWN
sections : dict[str, list[str]]= {}
for c in handlers.keys():
sections[c] = []
for line in open(inp):
line = line.strip()
if line.startswith('['):
is_candidate = False
for s in handlers.keys():
if line.startswith(f'[{s}'):
curr_section = s
is_candidate = True
break
if is_candidate:
continue
else:
curr_section = _UNKNOWN
continue
if section == 'TITLE':
pass
elif line == '':
continue
elif section == 'JUNCTIONS': # + coords
pass
elif section == 'RESERVOIRS': # + coords
pass
elif section == 'TANKS': # + coords
pass
elif section == 'PIPES':
pass
elif section == 'PUMPS':
pass
elif section == 'VALVES':
pass
elif section == 'TAGS':
pass
elif section == 'DEMANDS':
pass
elif section == 'STATUS':
pass
elif section == 'PATTERNS':
pass
elif section == 'CURVES':
pass
elif section == 'CONTROLS':
pass
elif section == 'RULES':
pass
elif section == 'ENERGY':
pass
elif section == 'EMITTERS':
pass
elif section == 'QUALITY':
pass
elif section == 'SOURCES':
pass
elif section == 'REACTIONS':
pass
elif section == 'MIXING':
pass
elif section == 'TIMES':
pass
elif section == 'REPORT':
pass
elif section == 'OPTIONS':
pass
elif section == 'COORDINATES':
pass
elif section == 'VERTICES':
pass
elif section == 'LABELS':
pass
elif section == 'BACKDROP':
pass
elif section == 'END':
pass # :)
if curr_section in handlers.keys():
handlers[curr_section](project, line)
def read_inp(project: str, inp: str) -> bool:
if is_project_open(project):
close_project(project)
if have_project(project):
delete_project(project)
create_project(project)
open_project(project)
close_project(project)
return True
def import_inp(project: str, cs: ChangeSet) -> bool:
def scan3(project: str, inp: str) -> None:
handlers = {
#PIPES: , #5
#PUMPS: , #6
#VALVES: , #7
#DEMANDS: , #9
#EMITTERS: , #16
#QUALITY: , #17
#SOURCES: , #18
#MIXING: , #20
#COORDINATES: #24
#LABELS: #26
}
curr_section = _UNKNOWN
sections : dict[str, list[str]]= {}
for c in handlers.keys():
sections[c] = []
for line in open(inp):
line = line.strip()
if line.startswith('['):
is_candidate = False
for s in handlers.keys():
if line.startswith(f'[{s}'):
curr_section = s
is_candidate = True
break
if is_candidate:
continue
else:
curr_section = _UNKNOWN
continue
elif line == '':
continue
if curr_section in handlers.keys():
handlers[curr_section](project, line)
def scan4(project: str, inp: str) -> None:
handlers = {
#TAGS: inp_in_junction_new, #5
#STATUS: inp_in_reservoir_new, #6
#ENERGY: inp_in_tank_new, #7
#REACTIONS: , #9
#VERTICES: , #16
}
curr_section = _UNKNOWN
sections : dict[str, list[str]]= {}
for c in handlers.keys():
sections[c] = []
for line in open(inp):
line = line.strip()
if line.startswith('['):
is_candidate = False
for s in handlers.keys():
if line.startswith(f'[{s}'):
curr_section = s
is_candidate = True
break
if is_candidate:
continue
else:
curr_section = _UNKNOWN
continue
elif line == '':
continue
if curr_section in handlers.keys():
handlers[curr_section](project, line)
def parse_inp(project: str, inp: str) -> None:
scan1(project, inp)
scan2(project, inp)
scan3(project, inp)
scan4(project, inp)
def read_inp_new(project: str, inp: str) -> bool:
if is_project_open(project):
close_project(project)
if have_project(project):
delete_project(project)
create_project(project)
open_project(project)
parse_inp(project, inp)
close_project(project)
return True
def import_inp_new(project: str, cs: ChangeSet) -> bool:
if is_project_open(project):
close_project(project)

View File

@@ -6,7 +6,7 @@ files = [
#'fengxian',
#'jbh',
#'nanjing',
#'net3',
'net3',
#'zj',
#'suzhouhe',
]
@@ -20,7 +20,7 @@ def db2inp():
dump_inp(file, f'./db_inp/{file}.db.inp')
if __name__ == '__main__':
#inp2db()
inp2db()
#db2inp()
#print(run_inp('net3'))
pass

View File

@@ -185,8 +185,8 @@ def close_project(name: str) -> None:
def copy_project(source: str, new: str) -> None:
return api.copy_project(source, new)
def read_inp(name: str, inp: str) -> None:
return api.read_inp(name, inp)
def read_inp(name: str, inp: str) -> bool:
return api.read_inp_new(name, inp)
def dump_inp(name: str, inp: str) -> None:
return api.dump_inp(name, inp)