Code is much more precise than English; Here’s what I’d like to do:
import sys
fileName = sys.argv[1]
className = sys.argv[2]
# open py file here and import the class
# ???
# Instantiante new object of type "className"
a = eval(className + "()") # I don't know if this is the way to do that.
# I "know" that className will have this method:
a.writeByte(0x0)
EDIT:
Per the request of the answers, here’s what I’m trying to do:
I’m writing a virtual processor adhering to the SIC/XE instruction set. It’s an educational theoretical processor used to teach the fundamentals of assembly language and systems software to computer science students. There is a notion of a “device” that I’m trying to abstract from the programming of the “processor.” Essentially, I want the user of my program to be able to write their own device plugin (limited to “read_byte” and “write_byte” functionality) and then I want them to be able to “hook up” their devices to the processor at command-line time, so that they can write something like:
python3 sicsim -d1 dev1module Dev1Class -d2 ...
They would also supply the memory image, which would know how to interact with their device. I basically want both of us to be able to write our code without it interfering with each other.
Use
importlib.import_moduleand the built in functiongetattr. No need foreval.This will require that the file lives somewhere on your python path. Most of the time, the current directory is on said path. If this is not sufficient, you’ll have to parse the directory out of it and
appendit to thesys.path. I’ll be glad to help with that. Just give me a sample input for the first commandline argument.Valid input for this version would be something like: