Python Forum
Dynamically create functions from Json
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamically create functions from Json
#6
Hi nilamo,
I'm so happy today, you can't believe.
Everything is fixed !
Thank you !
When reading your message, this did remind me a previous message of Larz60+ in this thread https://python-forum.io/Thread-Control-a-dot-matrix-printer. And was showing the use of chr().

So I converted all my hex values into decimal values in the json.
And I changed a bit the lib to use chr()...
And that's all !
Here is the result
#NAME = commands.json
{
  "lineFeed":{"command":"10", "text":"Line Feed"},
  "carriageReturn":{"command":"13", "text":"Carriage Return"},
  "formFeed":{"command":"12", "text":"Form Feed"},
  "newLine":{"command":"10 13", "text":"New Line"},

  "setLetterQuality":{"command":"27 120 49", "text":"Set Letter Quality"},
  "setUtility":{"command":"27 120 48", "text":"Set Utility"},

  "fontRoman":{"command":"27 107 48"},
  "fontHelvette":{"command":"27 107 49"},
  "fontCourrier":{"command":"27 107 50"},
  "fontPrestige":{"command":"27 107 51"},
  "fontGothic":{"command":"27 107 54"},
  "fontBold":{"command":"27 107 55"},

  "styleNormal":{"command":"27 113 0"},
  "styleOutline":{"command":"27 113 1"},
  "styleShadow":{"command":"27 113 2"},
  "styleOutlineShadow":{"command":"27 113 3"},

  "10Cpi":{"command":"27 80 18"},
  "12Cpi":{"command":"27 77 18"},
  "15Cpi":{"command":"27 103 18"},
  "17Cpi":{"command":"27 80 15"},
  "20Cpi":{"command":"27 77 15"},

  "propOn":{"command":"27 112 1"},
  "propOff":{"command":"27 112 0"},
  "doubleOn":{"command":"27 119 49 27 87 49"},
  "doubleOff":{"command":"27 119 48 27 87 48"}
}
#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 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

            commands = commands.split(" ") #Split each dec number individually
            for n in range(len(commands)): #For each dec number
                self.printText(chr(int(commands[n]))) #Send the command to the printer
        else: #If the key isn't in the json
            raise Exception(f"Invalid key: {key}") #Wrong key error

    def sendAscii(decimals):
        decimals = decimals.split(" ")
        for n in range(len(decimals)):
            printText(chr(decimals[n]))
#NAME = test.py
from printer import *

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

ML390.command("cleanPaperBin")

ML390.command("setLetterQuality")
ML390.command("10Cpi")
ML390.command("doubleOn")
ML390.command("underlineOn")
ML390.command("boldOn")
ML390.command("styleOutlineShadow")
ML390.command("fontGothic")
ML390.command("alignCenter")


ML390.command("newLine")
ML390.printText("****************************************\n")
ML390.printText("Thank you nilamo and Larz60+ !\n\n")
ML390.printText("You solved all my problems...\n\n")
ML390.command("newLine")
ML390.command("newLine")
ML390.command("alignLeft")
ML390.command("20Cpi")
ML390.printText("...at least for now ;-p...\n\n")
ML390.command("10Cpi")
ML390.command("alignCenter")
ML390.printText("****************************************\n\n")
ML390.command("formFeed")

ML390.command("reset")
[Image: printer1.jpg]
[Image: printer2.jpg]

I'm so glad it works !
Now the GUI is left to do !

Thanks again,
Clément.
Reply


Messages In This Thread
RE: Dynamically create functions from Json - by Clement_2000 - Feb-22-2019, 06:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create variable and list dynamically quest_ 12 4,451 Jan-26-2021, 07:14 PM
Last Post: quest_
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,428 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  JSON Decode error when using API to create dataframe Rubstiano7 4 2,966 Jan-11-2021, 07:52 PM
Last Post: buran
  How to create a basic grid with functions trousers1 2 1,855 Nov-22-2019, 04:16 PM
Last Post: ThomasL
  Create a new list dynamically floatingshed 6 14,285 Nov-20-2017, 01:25 PM
Last Post: floatingshed
  dynamically create a list wfsteadman 0 2,366 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