仅在PyMOL运行时调用函数 [英] Only call function if PyMOL running
问题描述
我有一个脚本,可以对蛋白质进行一些计算.完成后,方法将导入pymol
模块,并使用pymol.cmd
API在PyMOL会话中显示结果.该过程类似于以下内容:
I have a script that performs some calculations on a protein. When it's finished, a method imports the pymol
module, and uses the pymol.cmd
API to display results in a PyMOL session. The process is something akin to the following:
def display_results(results, protein_fn):
import pymol
pymol.cmd.load(protein_fn)
pymol.cmd.alter(...)
...
protein_fn = "1abc.ent"
results = analyze_protein(protein_fn)
display_results(results, protein_fn)
但是,我的脚本不一定要 在PyMOL中显示结果,我希望仅在已安装并运行PyMOL的情况下执行此操作.
However, my script doesn't necessarily need to display the results in PyMOL, and I'd like this to only be done if PyMOL is installed and running.
检查是否已安装PyMOL很容易(我只能按try: import pymol
),但是有没有办法检查是否有活动的PyMOL会话显示结果?
It's easy to check if PyMOL is installed (I can just try: import pymol
), but is there a way to check if there's an active PyMOL session to display results in?
推荐答案
我不是PyMOL的专家(从来没有编写过脚本),但我看到了两种可能的方法:
I'm not an expert on PyMOL (haven't ever scripted it) but I see 2 possible ways:
- 做一些琐碎的事情,需要打开PyMOL会话并捕获 例外
- 查看进程名称(类似于
os.system("ps ux | grep -i pymol")
)
- Do something trivial that requires an open PyMOL session and catch exceptions
- Look at process names (something like
os.system("ps ux | grep -i pymol")
)
第一种方法更好,第二种是肮脏的骇客.
First way is better, second is a dirty hack.
这篇关于仅在PyMOL运行时调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!