Skip to content

Commit

Permalink
split parse into parse and output function
Browse files Browse the repository at this point in the history
  • Loading branch information
gschnabel committed Mar 25, 2023
1 parent f153d3d commit 96e1c50
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions exfor_parserpy/exfor_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,23 +285,24 @@ def output_entry(datadic, ofs=0, auxinfo=None):
return lines, ofs


def parse(lines=None, datadic=None, inverse=False, ofs=0):
if not inverse:
datadic = {}
while ofs < len(lines):
if read_str_field(lines[ofs], 0) == "ENTRY":
entryid = read_str_field(lines[ofs], 1).strip()
entry, ofs = parse_entry(lines, ofs)
datadic[entryid] = entry
else:
ofs += 1
return datadic, ofs
else:
lines = []
for curentryid, curdic in datadic.items():
curlines, ofs = output_entry(curdic, ofs)
lines.extend(curlines)
return lines, ofs
def parse(lines, ofs=0):
datadic = {}
while ofs < len(lines):
if read_str_field(lines[ofs], 0) == "ENTRY":
entryid = read_str_field(lines[ofs], 1).strip()
entry, ofs = parse_entry(lines, ofs)
datadic[entryid] = entry
else:
ofs += 1
return datadic, ofs


def output(datadic, ofs=0):
lines = []
for curentryid, curdic in datadic.items():
curlines, ofs = output_entry(curdic, ofs)
lines.extend(curlines)
return lines, ofs


# the user interface
Expand All @@ -320,7 +321,7 @@ def from_exfor(cont):


def to_exfor(exfor_dic):
lines, _ = parse(datadic=exfor_dic, inverse=True)
lines, _ = output(datadic=exfor_dic)
return lines


Expand Down

0 comments on commit 96e1c50

Please sign in to comment.