I'm writing as a final project for an independent study and I am currently working on some backbone code for it. I created a class called Chip and I am trying to automate instantiating objects as I have some 50 objects to instantiate. My code is as follows:
ChipLookup.py
class Chip:
def __init__(self, name, function, type, datasheet, pinout):
self.name = name.lstrip('x')
self.function = function.strip()
self.type = type.strip()
self.datasheet = datasheet.strip()
self.pinout = pinout.strip()
#used to provide a string to eval later
def string_output(a, b, c):
x = a[b][c]
return x
def main():
RawChipList = []
for line in open('ChipDB.txt'):
RawChipList.append(line)
#parses the data from ChipDB.txt
def data_split(a):
N = []
a = a.strip("\n")
N = a.split(",")
return N
M = range(len(RawChipList))
List = []
Chips = []
for i in M:
List.append(data_split(RawChipList[i]))
eval('{0} = Chip(List[i][0], List[i][1], List[i][2], List[i][3], List[i][4])'.format(string_output(List, i, 0)))
#error above, I am trying to create a Chip Object under the name List[i][0], so for example the first would be x74sn00
Chips.append(generator(List, i, 0))
return (M, Chips, List)
A few lines from ChipDB.txt. The last two fields on each line are going to be files, so they have not been filled yet. (note: There is a space after the last comma on each line)
ChipDB.txt
x74sn00, Quad 2 input NAND, NAND, ,
x74sn02, Quad 2-input NOR Gate, NOT, ,
x74sn04, Hex Inverter, Invert, ,
x74sn08, Quad 2-input AND Gate, AND, ,
x74sn10, Triple 3-input NAND Gate, NAND, ,
The error I'm getting is: (note: this is when I import it interactively)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ChipLookup.py", line 31, in main
eval('{0} = Chip(List[i][0], List[i][1], List[i][2], List[i][3], List[i][4])'.format(str(generator(List, i, 0))))
File "<string>", line 1
x74sn00 = Chip(List[i][0], List[i][1], List[i][2], List[i][3], List[i][4])
^
SyntaxError: invalid syntax
However, when I define Chip interactively and type the code
x74sn00 = Chip('x74sn00, 'quad 2 input NAND', 'NAND', ' ', ' ')
It works fine. What am I doing wrong here?