Feb-20-2019, 09:59 PM
Bonjour experts !
It is still for my printer controller project.
I'm now trying to make a lib to get a flexible, and easy tom modify command list.
So the goal for now is that I want to create a custom user command list, with functions declared in a class.
So here is what I've done :
As I would say in my country : "Je suis chiffoné !"
Help me please.
Thank you in advance,
Clément.
It is still for my printer controller project.
I'm now trying to make a lib to get a flexible, and easy tom modify command list.
So the goal for now is that I want to create a custom user command list, with functions declared in a class.
So here is what I've done :
#File name = test.py from printer import * ML390 = Printer(1, "commands.json") ML390.formFeed()
#File name = commands.json [ {"name":"lineFeed", "command":"x0A"}, {"name":"carriageReturn", "command":"x0D"}, {"name":"formFeed", "command":"x0C"}, {"name":"setLetterQuality", "command":"x1B x78 x32"}, {"name":"setUtility", "command":"x1B x78 x31"} ]
#File name = printer.py import platform #Lib to check os import json class Printer(): #Main class def __init__(self, port, file): #"port" is the parralel port where the printer is pluged in, "file" is the json file containg the custom functions and command to send to the printer self.os = platform.system() #Returning os in a variable self.data = json.load(open(file)) #Opening user selected file if type(port) != int: #Checks if the port is an int raise Exception("PORT ERROR : Port must be an intreger") #Returns error if port <= 0: #Checks if the port is the right one raise Exception("PORT ERROR : Port must be 1 or higher") #returns error if self.os == "Linux" : #If on Linux self.port = open("/dev/lp"+str(port-1), "w") #Open as /dev/lpx elif self.os == "Windows" : #If on Winows self.port = open("LPT"+str(port), "w") #Open as LPTx else: #If on oter system raise Exception("OS ERROR : Unsupported OS") #No supported os error for n in range(len(self.data)): #Cor all the commands defined in the json exec(f"""def {self.data[n]["name"]}(self): self.printText(data[n]["command"])""") #Create a custom function from the json, function name is name in the json def printText(self, text): text = str(text) self.port.write(text) self.port.flush()When running test.py, I get this error :
Error:Traceback (most recent call last):
File "test.py", line 6, in <module>
ML390.formFeed()
AttributeError: 'Printer' object has no attribute 'formFeed'
I don't know how to declare the functions in the main class, but keep the dynamic function creation when calling the class (__init__). . As I would say in my country : "Je suis chiffoné !"
Help me please.
Thank you in advance,
Clément.