Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speeding up lookup of inp sections and bracketed words #117

Merged
merged 6 commits into from
May 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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