executing functions from windows console - python - windows

I have implemented some logic through python functions and am looking for a way to calling those functions more efficiently. Is there a way to calling these functions from windows command prompt or python cmd like below?
python function:
def logic1(args)
...
want to call the function like this:
logic1(args)

Import the file in the python console like this:
from my_file.py import *
Or
from my_file.py import name1, name2 ...
Now you can use these in the python console. Out is just the same as importing into files.
For calling the functions in the Windows prompt you need to separate each function into its own file and use py2exe to convert them into executables. This may be an overhead of work though, depending on what you want to achieve.

Related

Checking for a running python process using python

I have a python script called speech.pyw. I don't want it showing up on the screen when run so I used that extension.
How can I check using another python script whether or not this script is running? If it isn't running, this script should launch it.
Off the top of my head, there are at least two ways to do this:
You could make the script create an empty file in a specific location, and the other script could check for that. Note that you might have to manually remove the file if the script exits uncleanly.
You could list all running processes, and check if the first one is among those processes. This is somewhat more brittle and platform-dependant.
An alternative hybrid strategy would be for the script to create the specific file and write it's PID (process id) to it. The runner script could read that file, and if the specified PID either wasn't running or was not the script, it could delete the file. This is also somewhat platform-dependant.
!/usr/bin/env python2
import psutil
import sys
processName="wastetime.py"
def check_if_script_is_running(script_name):
script_name_lower = script_name.lower()
for proc in psutil.process_iter():
try:
for element in proc.cmdline():
if element.lower() == script_name_lower:
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False;
print(check_if_script_is_running(processName))
sys.stdin.readline()

Running an existing tkinter script from another tkinter script

I'm new to python and tkinter.
I have a working tkinter script (Which I would like to avoid editing)
Now I'm writing a script which will be top level GUI.
The button from this script should launch my existing script with some command line arguments (like running it from a shell i.e. python3.4.1 script.py args).
I have tried the following:
Using os.system
btn2 = Button(frame2, text="Configure>>", command="os.system('python script.py args')")
Using subprocess
def runSubProcess(self):
p=subprocess.Popen(["python3.4.1","script.py args"],stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output=p.communicate()[0]
Both methods are not working.
Any suggestions are welcome and thanks.
Edit: Also I don't need to communicate with the new window, as it'll be writing to a file which will be used later. Just the control return from child to parent window should suffice (upon clicking OK)
-Vinay
Your first method should work. However, you passed the wrong thing to the command argument for the Button. You should pass a function to the command instead of a string. You can use lambda:
btn2 = Button(frame2, text="Configure>>", command=lambda: os.system('python script.py args'))

Why won't an external executable run without manual input from the terminal command line?

I am currently writing a Python script that will pipe some RNA sequences (strings) into a UNIX executable, which, after processing them, will then send the output back into my Python script for further processing. I am doing this with the subprocess module.
However, in order for the executable to run, it must also have some additional arguments provided to it. Using the subprocess.call method, I have been trying to run:
import subprocess
seq= "acgtgagtag"
output= subprocess.Popen(["./DNAanalyzer", seq])
Despite having my environmental variables set properly, the executables running without problem from the command line of the terminal, and the subprocess module functioning normally (e.g. subprocess.Popen(["ls"]) works just fine), the Unix executable prints out the same output:
Failed to open input file acgtgagtag.in
Requesting input manually.
There are a few other Unix executables in this package, and all of them behave the same way. I even tried to create a simple text file containing the sequence and specify it as the input in both the Python script as well as within the command line, but the executables only want manual input.
I have looked through the package's manual, but it does not mention why the executables can ostensibly be only run through the command line. Because I have limited experience with this module (and Python in general), can anybody indicate what the best approach to this problem would be?
The Popen() is actually a constructor for an object – that object being a "sub-shell" that directly runs the executable. But because I didn't set a standard input or output (stdin and stdout), they default to None, meaning that the process's I/O are both closed.
What I should have done is pass subprocess.PIPE to signify to the Popen object that I want to pipe input and output between my program and the process.
Additionally, the environment variables of the script (in the main shell) were not the same as the environment variables of the subshell, and these specific executables needed certain environment variables in order to function (in this case, it was the path to the parameter files in its package). This was done in the following fashion:
import subprocess as sb
seq= "acgtgagtag"
my_env= {BIOPACKAGEPATH: "/Users/Bobmcbobson/Documents/Biopackage/"}
p= sb.Popen(['biopackage/bin/DNAanalyzer'], stdin=sb.PIPE, stdout=sb.PIPE, env=my_env)
strb = (seq + '\n').encode('utf-8')
data = p.communicate(input=strb)
After creating the Popen object, we send it a formatted input string using communicate(). The output can now be read, and further processed in whatever way in the script.

LLDB: How to define a function with arguments in .lldbinit?

I would like write a helper function which to be available in my LLDB session. (I am not talking about python here)
This function will invoke methods of current program variables and then pass them to a python script.
I guess i understand how to write a python script, but i am still not sure how to write an lldb-script which interacts with my program.
For a general intro on how to use the lldb Python module to interact with your program, see:
https://lldb.llvm.org/use/python-reference.html
That will show you some different ways you can use Python in lldb, and particularly how to make Python based commands and load them into the lldb command interpreter.
There are a variety of example scripts that you can look at here:
https://github.com/llvm/llvm-project/tree/main/lldb/examples/python
There's an on-line version of the Python API help here:
https://lldb.llvm.org/python_api.html
and you can access the same information from within lldb by doing:
(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> help(lldb)
Help on package lldb:
NAME
lldb
FILE
/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python/lldb/__init__.py
DESCRIPTION
...

Is there the redirect operator >> in Python 3?

I did not know there was a redirect operator in Python 2, for example here somebody is using it to redirect something to a file. I just knew that there is one in Bash. Is still there such a thing in Python 3?
print function in Python 3.x optionally accept file parameter, you can specify file object.
import sys
print(n, file=sys.stderr)
Using the shell redirection this way
python foo_bar.py > file
is still possible with python 3.x as this is not a feature of python itself but this is actually a feature of the shell interpreter in which the python command is run.

Resources