Hi! I’ve written this simple piece of Python code to check that no mistakes were during the execution of a program. It goes through the log file checking there are no repeated file names (I shouldn’t be overwriting anything). Do you have any suggestions on how to improve it? For example, I know there must be a better way of combining condition
and parse_line
to not “untimestamp
” the line twice, I just have no clue on how to do it.
Anyways, here’s the code:
import sys untimestamp = lambda x: " ".join(x.split(':')[3:]).strip(' ') def condition(line: str) -> bool: notimestamp = untimestamp(line) return notimestamp.startswith('Generated') and parse_line(line).endswith('0000.nii.gz') def parse_line(line: str) -> str: notimestamp = untimestamp(line) return notimestamp.split()[1] def main(log_file: str) -> None: seen = set() with open(log_file, 'r') as file: mylines = [parse_line(line) for line in file.readlines() if condition(line)] for line in mylines: assert line.endswith('0000.nii.gz'), line assert line not in seen, line seen.add(line) print('Success')
Thanks!!
submitted by /u/vincetheDCfan
[link] [comments]
r/learnpython Hi! I’ve written this simple piece of Python code to check that no mistakes were during the execution of a program. It goes through the log file checking there are no repeated file names (I shouldn’t be overwriting anything). Do you have any suggestions on how to improve it? For example, I know there must be a better way of combining condition and parse_line to not “untimestamp” the line twice, I just have no clue on how to do it. Anyways, here’s the code: import sys untimestamp = lambda x: ” “.join(x.split(‘:’)[3:]).strip(‘ ‘) def condition(line: str) -> bool: notimestamp = untimestamp(line) return notimestamp.startswith(‘Generated’) and parse_line(line).endswith(‘0000.nii.gz’) def parse_line(line: str) -> str: notimestamp = untimestamp(line) return notimestamp.split()[1] def main(log_file: str) -> None: seen = set() with open(log_file, ‘r’) as file: mylines = [parse_line(line) for line in file.readlines() if condition(line)] for line in mylines: assert line.endswith(‘0000.nii.gz’), line assert line not in seen, line seen.add(line) print(‘Success’) Thanks!! submitted by /u/vincetheDCfan [link] [comments]
Hi! I’ve written this simple piece of Python code to check that no mistakes were during the execution of a program. It goes through the log file checking there are no repeated file names (I shouldn’t be overwriting anything). Do you have any suggestions on how to improve it? For example, I know there must be a better way of combining condition
and parse_line
to not “untimestamp
” the line twice, I just have no clue on how to do it.
Anyways, here’s the code:
import sys untimestamp = lambda x: " ".join(x.split(':')[3:]).strip(' ') def condition(line: str) -> bool: notimestamp = untimestamp(line) return notimestamp.startswith('Generated') and parse_line(line).endswith('0000.nii.gz') def parse_line(line: str) -> str: notimestamp = untimestamp(line) return notimestamp.split()[1] def main(log_file: str) -> None: seen = set() with open(log_file, 'r') as file: mylines = [parse_line(line) for line in file.readlines() if condition(line)] for line in mylines: assert line.endswith('0000.nii.gz'), line assert line not in seen, line seen.add(line) print('Success')
Thanks!!
submitted by /u/vincetheDCfan
[link] [comments]