Skip to content

Commit

Permalink
Merge pull request #117 from jackieff/speedup_inp_reading
Browse files Browse the repository at this point in the history
Speeding up lookup of inp sections and bracketed words
  • Loading branch information
aerispaha authored May 6, 2021
2 parents 5581cf4 + 3331597 commit 2c1429a
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions swmmio/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,25 @@ def get_inp_sections_details(inp_path, include_brackets=False):
found_sects = OrderedDict()

with open(inp_path) as f:
for line in f:
sect_not_found = True
for sect_id, data in INP_OBJECTS.items():
# find the start of an INP section
search_tag = format_inp_section_header(sect_id)
if search_tag.lower() in line.lower():
if include_brackets:
sect_id = '[{}]'.format(sect_id.upper())
found_sects[sect_id.upper()] = data
sect_not_found = False
break
if sect_not_found:
if '[' and ']' in line:
h = line.strip()
if not include_brackets:
h = h.replace('[', '').replace(']', '')
found_sects[h] = OrderedDict(columns=['blob'])
txt = f.read()
section_dict = {
key: txt.find("[{}]".format(key)) for key in INP_OBJECTS.keys()
if txt.find("[{}]".format(key)) >= 0
}
section_dict = sorted(section_dict, key=section_dict.get)
bracketed_words = re.findall(r"\[([A-Za-z0-9_]+)\]", txt)

for sect in bracketed_words:
if sect not in section_dict:
if not include_brackets:
h = sect.replace('[', '').replace(']', '')
found_sects[h] = OrderedDict(columns=['blob'])
else:
if include_brackets:
sect_id = '[{}]'.format(sect.upper())
else:
sect_id = sect.upper()
found_sects[sect_id] = INP_OBJECTS[sect]

# make necessary adjustments to columns that change based on options
ops_cols = INP_OBJECTS['OPTIONS']['columns']
Expand Down

0 comments on commit 2c1429a

Please sign in to comment.