0

I'm working on a command-prompt like program in python, where the user types a command in, a dictionary finds the correlating method, and then accesses another file with all the methods in there, and then runs the one the user asked.

However, when I try to run any of them, it first print out anything besides the 'return' statement, from all the methods and then executes the method asked.

This is the main file which I run. (rcmd.py)

from commands import *

def get_input():

    user = input("Guppy\RACHEAL> ")
    return user

def command_line():

    # LOOP DETERMINATION VARIABLE
    exit = False

    while exit == False:

        user_input = get_input()

        if user_input == "exit":
            exit = True
            break
        elif user_input == "":
            get_input()
        else:
            e = commands.execute(user_input)
            print("FINISHED --> " + str(e))

# PROGRAM START LOOP
#
#
command_line()

This is the file where the dictionary is used to execute a method (commands.py)

from modules import *

class commands(object):

    def execute(user_input):

        # DICTIONARY OF COMMANDS
        COMMANDS = {

            # || GENERAL || #
            "help"            : modules.help(),

            # || FIND || #
            "find- time"      : modules.find_time(user),
            "find- date"      : modules.find_date(),

            # || INPUT || #
            "input- log: new" : modules.input_new_log()

        }

        try:

            execution = COMMANDS[user_input]
            return execution

        except KeyError:

            return "ERROR --> No such command"

This is the file with all the methods import datetime

class modules(object):

# -- || GENERAL MODULES || --- #
    def help():
        return "Action terms:\n'find'\n'input'" + "\n__________\n"

# --- || 'FIND' MODULES || --- #
    def find_time():

        print("Nothing")
        return datetime.datetime.now().time()

    def find_date():
        return datetime.datetime.now().date()

# --- || 'INPUT' MODULES || --- #
    def input_new_log():
        new_user_log = input("> ")
        path = 'C:\\Users\\Guppy\\Projects\\RACHEAL\\RACHEALs Databases\\user_log.txt'
        with open(path, 'w') as log:
            log.write(new_user_log)
            input_new_log_exit_code = 1
        return "EXIT CODE = " + str(input_new_log_exit_code)

This is the output I get

Guppy\RACHEAL> find- date
Guppy\RACHEAL> Nothing
>
FINISHED --> 2017-31-12

Where I should just be getting

Guppy\RACHEAL> find- date
FINISHED --> 2017-31-12

If anyone has any idea why this isn't working correctly, or what I'm doing wrong, it would be really appreciated.

guppythegod
  • 63
  • 1
  • 6

2 Answers2

1

The problem is where you define COMMANDS in commands.py:

# DICTIONARY OF COMMANDS
    COMMANDS = {

        # || GENERAL || #
        "help"            : modules.help(),

        # || FIND || #
        "find- time"      : modules.find_time(user),
        "find- date"      : modules.find_date(),

        # || INPUT || #
        "input- log: new" : modules.input_new_log()

    }

Here you are setting each value to the output of the functions and not the functions themselves. It should look more like this:

# DICTIONARY OF COMMANDS
    COMMANDS = {

        # || GENERAL || #
        "help"            : modules.help,

        # || FIND || #
        "find- time"      : modules.find_time,
        "find- date"      : modules.find_date,

        # || INPUT || #
        "input- log: new" : modules.input_new_log

    }

You will also have to change the line execution = COMMANDS[user_input] to execution = COMMANDS[user_input](). This will give you the output that you’re looking for.

wdt12
  • 404
  • 2
  • 9
1

The problem is in the way you construct the dictionary of functions to call.

    COMMANDS = {

        # || GENERAL || #
        "help"            : modules.help(),

        # || FIND || #
        "find- time"      : modules.find_time(user),
        "find- date"      : modules.find_date(),

        # || INPUT || #
        "input- log: new" : modules.input_new_log()

    }

For each entry in the dictionary, you call the function, and assign the result of calling the function to the entry. What you need to do is assign a reference to the function. Note the absence of brackets.

    COMMANDS = {

        # || GENERAL || #
        "help"            : modules.help,

        # || FIND || #
        "find- time"      : modules.find_time,
        "find- date"      : modules.find_date,

        # || INPUT || #
        "input- log: new" : modules.input_new_log

    }

You then need to call the functionm after you have looked it up in the dictionary:

        function= COMMANDS[user_input]
        return function()

Note the extra brackets which cause the function to be called here.

Also, you have an inconsistency with find_time(). In the dictionary constructor, you're passing a patameter, but the function does not appear to expect this.

Simon Callan
  • 3,020
  • 1
  • 23
  • 34