Python Forum
running produced python code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
running produced python code
#1
is there a way to get the Python engine to exec some command or script via a pipe and read the python code from that pipe as the code it will interpret? i have seen programs that read something from a file accept a '|' as the first character of a file name and treat the remaining characters as a command string and run that command to produce the contents. i have a couple cases where i would sometimes like to invoke the python command that way.

i've been thinking of adding this capability to my zopen() function. but that is not something the python engine uses.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
On Linux, this script will remove blank lines from a text file when used like this:
cat text.txt | ./remove
Remember tochmod +x remove
#!/usr/bin/python3

from sys import stdin 

for line in stdin :
	if line != '\n' :
		print (line, end = '')
I don't know if it will work on Windows or not Blush
Reply
#3
that's not what i am looking for. i an looking for a way to support "|" at the start of a file name string and have it work as a command to execute. the command to execute is after the "|". the output of the command is the contents to be used by the program as if from a file. if the python engine supported this and foo.py outputted the python code to be run then you could do 'python "|python foo.py"' to run that code. but it does not support that. i am looking for a way to accomplish this.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Why not simply write your own script named ppython such that the command
Output:
ppython foo.py
would run python foo.py, read its output as Python code and execute this Python code?

Alternatively suppose we have the following script
# paillasse/pf/spamm.py

print("""\
for i in range(5):
    print(i, i**2)

print("That's all folks!")
""")
Then here is what I can do in the bash shell by using python -c and command substitution
Output:
λ python -c "$(python paillasse/pf/spamm.py)" 0 0 1 1 2 4 3 9 4 16 That's all folks! λ
Reply
#5
i'm not following that. maybe i need to read it a few more times.

the command line that outputs the python code to be run cannot have that code hardcoded in its own code. i need to be able to build any command line including those that get code somewhere on the internet at an unexpected place.

i am thinking of a wrapper that runs commands and saves output as temporary files then runs the real python with those temporary file names replacing the pipe prefix command string arguments. then i could adapt this for other commands.

i think that would work so i am thinking of a scheme to have just one such script that can figure out what is to really be run based on what command it replaces, such as /usr/local/bin/pythonxy will run /usr/bin/pythonxy after it saves the temporary files. that way i don't need to code a new script for each case. i would just symlink /usr/local/bin/pythonxy -> /usr/local/bin/usr.bin.wrapper and would figure out the path to the real executable.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
In my example, spamm.py could as well fetch the code somewhere on the internet. It does not need to be hard coded. May I suggest not to execute blindly code found on the internet Cry
Reply
#7
"on the internet" can include securely verified and trusted sources. then it's not "blindly". or the command could construct code based on info/data from various sources. it's up to whoever determines the command to do it right.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  compiling but not running source code Skaperen 1 1,347 Jun-05-2022, 10:47 PM
Last Post: Gribouillis
  code running after tracebacks Skaperen 0 1,263 Oct-03-2021, 12:09 AM
Last Post: Skaperen
  Running three-year old python code ErnestTBass 4 2,529 Jun-19-2020, 02:46 PM
Last Post: snippsat
  Recovering lost source code when the code is running in memory micseydel 13 9,691 Apr-14-2017, 02:35 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020