Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Simple UI to run script2 + args
-
import sys import runpy try: # https://docs.python.org/3/whatsnew/3.0.html#builtins raw_input # Python 2 except NameError: raw_input = input # Python 3 script2 = sys.argv[1] if sys.argv[1:] else 'script2.py' first_name = raw_input("What is your first name? ").strip() or 'Donald' middle_name = raw_input("What is your middle name? ").strip() or 'J.' last_name = raw_input("What is your last name? ").strip() or 'Trump' fmt = "%s: Your full name is %s %s %s." print(fmt % (sys.argv[0].split('/')[-1], first_name, middle_name, last_name)) sys.argv = ['script2.py', first_name, middle_name, last_name] with open(sys.argv[0]) as in_file: exec(in_file.read()) # exec() is a security hole a mile wide and 10 miles deep!! """script2.py import sys print("{}: Your full name is {} {} {}.".format(*sys.argv)) """
-
Python 3 fix also merged into upstream... https://github.com/mwarkentin/Learn-Python-The-Hard-Way/pull/2/files
-
Uh, I didn't know that python had a short hand syntax for conditional assignments evaluating for
True
. That is nice 👍🏻. -
rich,
Are you aware that you long-press the play button for a script, it lets you define arguments? Just checking, not everyone realizes this.another option would be to use a
dialogs
popup rather than raw_inputimport dialogs fields=[{'title':'First Name','type':'text'}, {'title':'Last Name','type':'text'}, {'title':'Age','type':'number'}] response=dialogs.form_dialog('What is your info?',fields)
-
@JonB Thanks. I really like this dialogue box! If I can get the responses to act as args to control my second script, then I have it made!
-
This post is deleted! -
Put that text at the end into a file called
script2.py
;-). Then run the rest of the code in a separate file that I calledscript1.py
. -
@ccc Thanks. I still get the traceback error "AttributeError: 'tuple' object has no attribute 'remove'"
-
Try changing this line:
sys.argv = ['script2.py', first_name, middle_name, last_name]
To have square brackets [] instead of parentheses ().
-
This post is deleted! -
@ccc Thank you! This works as expected!
The first two args are necessary, the third arg is supposed to be optional. In its current form, (with Trump removed of course,) leaving the third arg blank causes script2 error (citing an invalid arg).
Do you know how to leave an arg as optional? -
The part about 'Trump' being removed made my day! Thanks for that.
# in script2.py, select one of the following and rename it to main() def default_arg_main(script_name, first_name, middle_name, last_name=''): last_name = last_name or 'Bonehead' fmt = "{}: Your full name is {} {} {}." print(fmt.format(script_name, first_name, middle_name, last_name)) def if_main(*args): if len(args) == 4: fmt = "{}: Your full name is {} {} {}." else: fmt = "{}: Your full name is {} {}." print(fmt.format(*args)) def join_main(*args): print("{}: Your full name is {}.".format(args[0], ' '.join(args[1:]))) def list_main(*args): fmt = ('Zero sys.argv is not possible!', 'Usage: {} first_name, [middle_name, [last_name]]', "{}: Your full name is {}.", "{}: Your full name is {} {}.", "{}: Your full name is {} {} {}.")[len(args)] print(fmt.format(*args)) # and then... print('=' * 44) main(*sys.argv)
-
Clearly, you are a Python master!
-
@ccc Here is the project: Savethemblobs_app
What improvements would you make? Please feel free to fork.