Home > Software design >  Load item description from json file
Load item description from json file

Time:05-14

Recently i saw a post about someone making a program that could control a computer it was launched on. (it was this one) Add commands to user input I was really interested in it and I wanted to replicate it and improve my python skills on the way.

After watching some tutorials I had the ability to send and recieve emails and started working on some commands. First I added the ability to take screenshots as its the most important one. I then added functions and commands to do other stuff. Then I wanted to add a help command to display all commands if there is no args and the description of a specific command if there is an args. I first added the one without args and this is the code for it:

import json

user_input = "$say hello\n$help"
def help(*args):
    if args == ():
        for func_name, aliases in info_json.items():
            print(func_name)
    else:
        pass
        #print the description for the command

def command1():
    print("I am command 1.")
def command2():
    print("I am command 2.")
def command3():
    print("I am command 3.")
def say(*args):
    print(f"You said i should say \"{' '.join(args)}\"! Very cool :D")
def pause(sec):
    print(f"I waited for {sec} seconds!")

commands = {
    "$help":help,
    "$pause":pause,
    "$say":say,
    "$command1":command1,
    "$command2":command2,
    "$command3":command3,
}
with open("commands.json") as json_file:
    help_json = json.load(json_file)


def call_command(BEFEHL):
    function, *args = BEFEHL.split(' ')
    commands[function](*args)


for line in user_input.split("\n"):
    try:
        call_command(line)
    except KeyError:
        print("This command does not exist.")

I replaced the actual functions with print statements like the original author did :D

This code worked very well and I started to work on the description on specific functions. I created the commands.json example:

{
  "command1": ["This command is command 1. It prints out 'I am command 1' "],
  "command2": ["This command is command 2. It prints out 'I am command 2' "],
  "command3": ["This command is command 3. It prints out 'I am command 3' "]
}

Is there any way you can print out the stuff in the json which stands behind the command? An example use would be:

>>> $help command1
print("This is command 1. It prints out 'I am command 1' ")

I would really appreciate to know if this is possible! :D

CodePudding user response:

When you load a json, it basically acts like a Python dictionary, so you can retrieve the description of the command from its key, which you are passing as parameter.

Your help() function should look something like this:

def help(*args):
    if args == ():
        for func_name, aliases in help_json.items():
            print(func_name)
    else:
        print(help_json.get(args[0], "Command does not exist"))

The second argument "Command does not exist" is the default value to print when the get() cannot find the key in the dictionary.

  • Related