Python Forum
Dynamically create functions from Json
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamically create functions from Json
#4
Hi nilamo and Larz60+,
Thank you for your help !

So nilmao, you're totally right. I don't know why I haven't done that before, I'm stupid. So i worked on it.
Larz60+, I saw your code, that's not exactly what I was looking for, but nilmao found what I was looking for. Anyway, I keep it in mind, it could be useful to another project.

Anyway, following your idea nilmao, here is what I ended up with :
#NAME = test.py
from printer import *

ML390 = Printer(1, "commands.json")

ML390.command("setUtility")
#NAME = commands.py
{
  "lineFeed":{"command":"0A", "text":"Line Feed"},
  "carriageReturn":{"command":"0D", "text":"Carriage Return"},
  "formFeed":{"command":"0C", "text":"formFeed"},
  "setLetterQuality":{"command":"1B 78 32", "text":"Set Letter Quality"},
  "setUtility":{"command":"1B 78 31", "text":"Set Utility"}
}
#NAME = printer.py
import platform #Lib to check os
import json

class Printer(): #Main class
    def __init__(self, port, file): #"port" is the parallel port where the printer is plugged in, "file" is the json file containing 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 or port <= 0: #Checks if the port is an int
            raise Exception(f"Invalid port : {port}") #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(f"Invalid OS : {self.os}") #No supported os error

    def printText(self, text):
        self.port.write(str(text))
        self.port.flush()

    def command(self, key): #custom command generator
        if key in self.data: #If the key is in the json
            commands = self.data[key]["command"] #Extract the command string from the json
            print(commands) #TEMPORARY

            commands = commands.split(" ") #Split each hex number individually
            for n in range(len(commands)): #For each hex number
                commands[n] = f"\x{commands[n]}" #add "\x" before
            commands = "".join(commands) #Put everything back into a single string

            self.printText(commands) #Send the command to the printer

            print(self.data[key]["text"]) #TEMPORARY
            print(commands) #TEMPORARY
        else: #If the key isn't in the json
            raise Exception(f"Invalid key: {key}") #Wrong key error
The new problem now is that in line 31, (you already know what will happen Wink), python tells me that I have to put the hex value after. And if I replace the string by "\\x" or r"\x", when running the command, I the printer litearally prints "\x0A" or "\x0B", or ... (not the quotes).
Should I create an other thread or can you help me now ?

Thanks again (I never say it enough Big Grin),
Clément.
Reply


Messages In This Thread
RE: Dynamically create functions from Json - by Clement_2000 - Feb-21-2019, 05:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create variable and list dynamically quest_ 12 4,790 Jan-26-2021, 07:14 PM
Last Post: quest_
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,792 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  JSON Decode error when using API to create dataframe Rubstiano7 4 3,116 Jan-11-2021, 07:52 PM
Last Post: buran
  How to create a basic grid with functions trousers1 2 1,940 Nov-22-2019, 04:16 PM
Last Post: ThomasL
  Create a new list dynamically floatingshed 6 14,518 Nov-20-2017, 01:25 PM
Last Post: floatingshed
  dynamically create a list wfsteadman 0 2,423 Aug-30-2017, 05:34 PM
Last Post: wfsteadman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020