From 05320809ae112af382252e3927a16a12c6a0a017 Mon Sep 17 00:00:00 2001 From: IIITM-Jay Date: Sun, 21 Jul 2024 20:38:08 +0530 Subject: [PATCH] Refactor and Optimization:: constants.py --- constants.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/constants.py b/constants.py index cb3e689d..18b0613b 100644 --- a/constants.py +++ b/constants.py @@ -38,21 +38,25 @@ imported_regex = re.compile('^\s*\$import\s*(?P.*)\s*::\s*(?P.*)', re.M) -causes = [] -with open("causes.csv") as f: - csv_reader = csv.reader(f, skipinitialspace=True) - for row in csv_reader: - causes.append((int(row[0], 0), row[1])) -csrs = [] -with open("csrs.csv") as f: - csv_reader = csv.reader(f, skipinitialspace=True) - for row in csv_reader: - csrs.append((int(row[0], 0), row[1])) -csrs32 = [] -with open("csrs32.csv") as f: - csv_reader = csv.reader(f, skipinitialspace=True) - for row in csv_reader: - csrs32.append((int(row[0], 0), row[1])) +def read_csv(filename): + """ + Reads a CSV file and returns a list of tuples. + Each tuple contains an integer value (from the first column) and a string (from the second column). + + Args: + filename (str): The name of the CSV file to read. + + Returns: + list of tuple: A list of (int, str) tuples extracted from the CSV file. + """ + with open(filename) as f: + csv_reader = csv.reader(f, skipinitialspace=True) + return [(int(row[0], 0), row[1]) for row in csv_reader] + +causes = read_csv("causes.csv") +csrs = read_csv("csrs.csv") +csrs32 = read_csv("csrs32.csv") + arg_lut = {} with open("arg_lut.csv") as f: csv_reader = csv.reader(f, skipinitialspace=True)