Skip to content

Commit

Permalink
Refactor seq_file_parser.py for performance and readability (#92)
Browse files Browse the repository at this point in the history
* Remove else statement from SeqFileParser.parse

* f-string formatting

* black formatting

* Replace dict() with {} for performance

* Replace list() with [] for performance

* Switch to comprehension list for performance

* Remove useless elif to reduce nested statements

* peer-review: Iterate result of map call
  • Loading branch information
ThibFrgsGmz authored Nov 17, 2022
1 parent 662c4bc commit 30fa76d
Showing 1 changed file with 27 additions and 50 deletions.
77 changes: 27 additions & 50 deletions src/fprime_gds/common/parsers/seq_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ def replaceSpacesAndCommas(matchobj):
s = s.replace(",", " ")
# get the split indices of the modified string:
indices = [(m.start(), m.end()) for m in re.finditer(r"\S+", s)]
toReturn = []
for start, end in indices:
toReturn.append(string[start:end])
return toReturn
return [string[start:end] for start, end in indices]

def parseArgs(args):
"""
Expand All @@ -85,25 +82,24 @@ def parseArg(arg):
):
return arg[1:-1]
# If the string contains a "." assume that it is a float:
elif "." in arg:
if "." in arg:
return float(arg)
elif arg in ["True", "true", "TRUE"]:
if arg in ["True", "true", "TRUE"]:
return True
elif arg in ["False", "false", "FALSE"]:
if arg in ["False", "false", "FALSE"]:
return False
else:
try:
# See if it translates to an integer:
return int(arg, 0)
except ValueError:
try:
# See if it translates to an integer:
return int(arg, 0)
# See if it translates to a float:
return float(arg)
except ValueError:
try:
# See if it translates to a float:
return float(arg)
except ValueError:
# Otherwise it is an enum type:
return str(arg)
# Otherwise it is an enum type:
return str(arg)

return list(map(parseArg, args))
return [item for item in map(parseArg, args)]

def parseTime(lineNumber, time):
"""
Expand Down Expand Up @@ -176,13 +172,7 @@ def parseAbsolute(timeStr):
delta = (dt - epoch).total_seconds()
else:
raise gseExceptions.GseControllerParsingException(
"Line %d: %s"
% (
lineNumber + 1,
"Invalid time descriptor '"
+ d
+ "' found. Descriptor should either be 'A' for absolute times or 'R' for relative times",
)
f"Line {lineNumber + 1}: Invalid time descriptor {d} found. Descriptor should either be 'A' for absolute times or 'R' for relative times"
)
seconds = int(delta)
useconds = int((delta - seconds) * 1000000)
Expand All @@ -201,37 +191,24 @@ def parseAbsolute(timeStr):
length = len(line)
if length < 2:
raise gseExceptions.GseControllerParsingException(
"Line %d: %s"
% (
i + 1,
"Each line must contain a minimum of two fields, time and command mnemonic\n",
)
f"Line {i + 1}: Each line must contain a minimum of two fields, time and command mnemonic\n"
)
try:
descriptor, seconds, useconds = parseTime(i, line[0])
except Exception:
raise gseExceptions.GseControllerParsingException(
f"Line {i + 1}: Encountered syntax error parsing timestamp"
)
else:
mnemonic = line[1]
args = []
if length > 2:
args = line[2:]
try:
descriptor, seconds, useconds = parseTime(i, line[0])
args = parseArgs(args)
except Exception:
raise gseExceptions.GseControllerParsingException(
"Line %d: %s"
% (
i + 1,
"Encountered syntax error parsing timestamp",
)
f"Line {i + 1}: Encountered syntax error parsing arguments"
)
mnemonic = line[1]
args = []
if length > 2:
args = line[2:]
try:
args = parseArgs(args)
except Exception:
raise gseExceptions.GseControllerParsingException(
"Line %d: %s"
% (
i + 1,
"Encountered syntax error parsing arguments",
)
)
yield i, descriptor, seconds, useconds, mnemonic, args
except gseExceptions.GseControllerParsingException as exc:
if not cont:
Expand Down

0 comments on commit 30fa76d

Please sign in to comment.