Created
June 5, 2023 10:16
-
-
Save breinbaas/6fb8e213d68ef524a6ecbc8f478e2101 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
def line_to_id(line: str) -> str: | |
"""Extract the id from the line | |
We expect the id to be between '', the rest of the line should not contain the ' character | |
Note that we do an extra check to see if there are no spaces in the id | |
Args: | |
line (str): the line with the settings | |
Returns: | |
str: the id as str | |
""" | |
id = line.split("'")[1] | |
if " " in id: | |
print( | |
f"Warning, found id '{id}' which contains a space, this is not be allowed" | |
) | |
return id | |
def id_in_codes(id: str, codes: List[str]) -> bool: | |
"""Returns True is the id is found in the given codes | |
Args: | |
id (str): the id | |
codes (List[str]): the codes to look for | |
Returns: | |
bool: True if the id is found in the list of codes | |
""" | |
for code in codes: | |
if id.startswith(code): | |
return True | |
return False | |
def update_laterals(codes: List[str], old_file: str, new_file: str, output_file: str): | |
"""This script will search for settings in the new_file. If the id of the object | |
is also found in the old_file then the settings will be replaced by those of | |
the old file. The final settings will be written to the given output_file. | |
Args: | |
codes (List[str]): which codes to look for (for example ['LG_', 'CSO_']) | |
old_file (str): the name and path to the old file | |
new_file (str): the name and path to the new file | |
output_file (str): the name and path of the file to write to | |
""" | |
# read the old file and skip empty lines | |
lines_old = [ | |
line for line in open(old_file, "r").readlines() if len(line.strip()) > 0 | |
] | |
# extract the id from the lines and create a dictionary of all lines | |
# note that we assume that every id in the file is unique! | |
settings_old = {line_to_id(line): line for line in lines_old} | |
# read the new file and skip empty lines | |
lines_new = [ | |
line for line in open(new_file, "r").readlines() if len(line.strip()) > 0 | |
] | |
# extract the id from the lines and create a dictionary of all lines | |
# note that we assume that every id in the file is unique! | |
settings_new = {line_to_id(line): line for line in lines_new} | |
# now iterate over the new file | |
# if we find the id in the old file we write the old line | |
# else we write the new line | |
f_out = open(output_file, "w") | |
for k, v in settings_new.items(): | |
# check if we deal with an id that is part of the codes | |
if not id_in_codes(k, codes): | |
continue # nope, continue the iteration | |
# check if we old settings for this id, if so use these | |
if k in settings_old.keys(): | |
f_out.write(settings_old[k]) | |
else: # nope, only new settings, write this then... | |
f_out.write(v) | |
f_out.close() | |
if __name__ == "__main__": | |
update_laterals( | |
["LG_"], "LATERAL_BM10.txt", "LATERAL_BM14.txt", "updated_laterals.txt" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment