Post Reply 
Python: Multiple Files
05-09-2021, 09:38 PM
Post: #1
Python: Multiple Files
Is there a way for Python not to automatically run all the python scripts when [Num] is pressed?
Visit this user's website Find all posts by this user
Quote this message in a reply
05-09-2021, 10:39 PM
Post: #2
RE: Python: Multiple Files
Probably not very convient but you can use import to run a program. Thats the only thing i found
Find all posts by this user
Quote this message in a reply
05-09-2021, 10:53 PM
Post: #3
RE: Python: Multiple Files
Scratch that import only works for functions in a program but looks like if you import a program it appears to run both programs ?
Find all posts by this user
Quote this message in a reply
05-10-2021, 02:13 AM
Post: #4
RE: Python: Multiple Files
(05-09-2021 10:53 PM)Dougggg Wrote:  Scratch that import only works for functions in a program but looks like if you import a program it appears to run both programs ?

The entire program can be a function.

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
05-10-2021, 09:59 AM (This post was last modified: 05-10-2021 10:00 AM by Dirk.nl.)
Post: #5
RE: Python: Multiple Files
Quote:Is there a way for Python not to automatically run all the python scripts when [Num] is pressed?

https://www.hpmuseum.org/forum/thread-16724.html

— Dirk Hartland
Find all posts by this user
Quote this message in a reply
05-22-2021, 07:33 AM
Post: #6
RE: Python: Multiple Files
I've gotten around this by not including `while True`, `if __main__:` and similar in my Prime Python scripts. I think of them not as programs that you run, but as functions that you load into memory.

For example, if I have a script called names.py:

Code:

# names.py

def get_name():
    """
    Get a name from the user.
    """"
    name = input("What is your name?")
    print("Your name is: ", name)

I intentionally don't call the `get_name()` function inside the script.
Instead, the function will be imported from my script when I switch to the Numeric view (`> import names`), and I'll call the function from the Numeric REPL using the script's namespace: `names.get_name()`.

This is similar to the Numworks sample scripts. They do let you choose which script to import instead of importing all of them, but you still enter the main function in the script ("simulation(), mandelbrot(), etc.") into the REPL to make it do something.

However, having to enter the complete namespace and function name using the calculator keypad is cumbersome. Two approaches to this could be:
  1. Tabbing autocompletion, as in the Numworks editor or your favorite desktop IDE. So, I could type na<TAB>get<TAB> and have it expanded to `names.get_name()`.
  2. Softkeys similar to the Toolbox...User functions in the Home view that give you shortcuts to imported user functions. If the namespace for a user script got too crowded, we could use the Python convention of private functions that start with an underscore (`def _internal_function()`) and only show the public functions for the imported scripts in the soft key menu.

-DrBarnack
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: