从Python快速导入自定义SPSS命令 [英] Quickly import custom SPSS commands from Python

查看:241
本文介绍了从Python快速导入自定义SPSS命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简洁的Python模块,其中包含一些自定义SPSS函数供我的同事使用.但是,要使用该功能,他们首先需要输入BEGIN PROGRAM.在实际调用函数之前,先导入模块等.因为它们大多数都不精通技术,所以我希望使事情变得更容易.

I have written a neat Python module that contains some custom SPSS functions for my coworkers to use. However, to use the functionality, they first need to type BEGIN PROGRAM. Import module etc... before actually calling the function. Because most of them aren't tech savvy, I'm looking to make things easier.

现在他们必须输入如下内容:

Now they have to type something like this:

BEGIN PROGRAM.
import sys
sys.path.append("C:/Python scripts")
import mymodule
mymodule.custom_function('a','c')
END PROGRAM.

有没有一种方法可以编写一些SPSS-macro来单行调用我的Python模块中的函数?像这样:

Is there a way I can write some SPSS-macro that will call the functions from my Python module in a single line? Like this:

!mymodule.custom_function varlist=a,b

我试图定义一个普通的SPSS宏,并在宏主体中编写BEGIN PROGRAM等,但这会产生错误.看来,宏主体只能包含有效的SPSS语法,而不能包含python代码.

I've tried defining a normal SPSS-macro and writing the BEGIN PROGRAM etc. in the macro body, but this yields an error. It seems that a macro-body can only contain valid SPSS syntax and no python code.

还有其他解决方法吗?

推荐答案

有几种可能的解决方案.如果要自动加载一组标准的Python函数,则可以创建一个启动脚本.它将被命名为StartClient_.py,并放置在Statistics安装目录下的scripts目录中.每次启动统计信息时将执行该操作. (这可以使用STATS OPEN PROJECT扩展命令来概括,该扩展命令在被调用时可以做很多事情,包括编写启动脚本.)

There are several possible solutions. If you want to have a standard set of Python functions loaded automatically, you can create a startup script. It would be named StartClient_.py and placed in the scripts directory under the Statistics installation. That will be executed each time Statistics is launched. (This can be generalized using the STATS OPEN PROJECT extension command that can do a lot of things when it is invoked, including writing a startup script.)

如果仅希望用户无需面对任何Python代码或BEGIN/END程序即可运行Python函数的另一种解决方案是使用SPSSINC PROGRAM扩展命令(实用工具">运行Python程序").需要一个模块和一个函数来运行,以及要指定的任何参数.

Another solution if you just want users to be able to run Python functions without facing any Python code or BEGIN/END program is to use the SPSSINC PROGRAM extension command (Utilities > Run Python Program). It take a module and function to run and whatever parameters you want to specify.

这是一个例子: SPSSINC计划testpgm.mypgn x =年龄y =收入z = .05.

Here's an example: SPSSINC PROGRAM testpgm.mypgn x=age y = income z=.05.

作为开发人员,要进行此项工作,请参见此命令的语法帮助.该命令将使用sys.argv加载模块并将参数传递给您的代码.

As the developer, to make this work see the syntax help for this command. The command loads the module and passes the parameters to your code using sys.argv.

如果您主要是指点和点击用户,则可以创建一些自定义对话框来保存Python代码并让用户安装.

If you have mostly point and click users, you could just create a few custom dialog boxes that hold the Python code and have users install them.

这篇关于从Python快速导入自定义SPSS命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆