Python Forum

Full Version: a form of coded logic in a comand line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
i am wanting to come up with an easy way to express Python logic in a single line of tokenized text like that which comes from command arguments in sys.argv. the way it can be done, now, is by having each token be a whole code line, preceded by spaces for the indentation. my goal is to come up with another way to express Python logic as tokenized text which is simpler for a human to type in within the constraints of command line arguments. the existing means is simple to convert to use with the "python -c" command. my goal is to shift most of the burden of expression from human to computer. then the new form would be converted for use with the "python -c" command. this new form may well have no indentation at all, such as a C-like form. but it doesn't have to be C-like. this is a form intended for short logic that can fit on a command line.
Why? What is the use case for such a thing?
(Feb-26-2022, 10:22 AM)ndc85430 Wrote: [ -> ]Why? What is the use case for such a thing?
to be able to include logic within a command typed in at a CLI as part of that command's arguments. i've been doing this with a few languages but none are compact enough for quick one-time code. for example, python takes much effort to get the indentation right (in a command line).
Can you show examples of the kind of thing you mean? Why wouldn't you just write the code in a file?
writing the code in a file is an extra step making the effort more burdensome. this would be code to type in quickly as part of doing a quick command. well, at least i do commands quickly.
i have no examples, yet. if you can't imagine any, you probably don't do commands as much as i do (2000-6000 a day).
In Bash, if you type Ctrl-x Ctrl-e it starts the editor defined by $EDITOR to edit the command that you want to type. When you close and save the editor, it runs the command. I use jed as $EDITOR for this. Younger people would probably use nano.
If you're doing quick one liners, I can understand wanting to do that on the command line. I don't get why you need special syntax, though. What about using the Python REPL, then, since you want indentation support and not having to write code in a file?

Also, I find it strange that you can't provide an example of what you want to do - it sounds like you have a need to do this, so surely you're able to come up with something that shows the difficulty you have now and what you'd prefer to do. You're right, I can't think of examples - I don't have a need for this and don't fully understand the problem.
In Bash, you can also use Here Documents
Output:
$ python3 <<EOF > for i in range(10): > print(i * i) > EOF 0 1 4 9 16 25 36 49 64 81
(Mar-02-2022, 06:54 AM)Gribouillis Wrote: [ -> ]In Bash, if you type Ctrl-x Ctrl-e it starts the editor defined by $EDITOR to edit the command that you want to type. When you close and save the editor, it runs the command. I use jed as $EDITOR for this. Younger people would probably use nano.

i'm still trying to get this in a command line. it may be entered via sudo and/or ssh, for example.
Pages: 1 2