1 Star 0 Fork 9

陈嘉浩 / python-in-stata

forked from 连享会 / python-in-stata 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Python in Stata

This package includes a Stata C plugin and helper files for embedding the Python programming language within Stata. In short, the plugin gives Stata users the ability to use Python to interact with Stata data values, matrices, macros, and numeric scalars. The plugin can be used interactively inside the Stata GUI, or can be used to execute Python files. Python files can be used separately or in combination with Stata .ado or .do files.

Documentation

The main source of documentation is python_plugin.tex.

Use with caution

The code in this package is experimental. Save your data before using the plugin. There is currently one known limitation/bug which can crash Stata. There may be other, unknown bugs that can crash Stata, too.

Known limitations

  1. Dropping a Stata program that uses the Python plugin and then re-running it can crash Stata, depending on what Python modules are used in the program, and whether it's the only Stata program that uses the plugin. For many Python modules this is not a problem. Nor does it seem to be a problem to drop and re-run python.ado, even if it's the only program using the plugin.

    Remedy: It's not clear what is causing this problem, but there seems to be a simple solution. If wanting to drop a program that uses the plugin, make sure that another program also uses it--for example, use python.ado at least once--or declare the plugin in Stata directly, with program python_plugin, plugin.

  2. The interactive Python interpreter within Stata is limited to single-line inputs. Unfortunately there is no remedy for this at the moment. With some creativity, though, quite a bit of Python code can be packed into a single line, or combinations of single-line inputs. If more than one line of input is needed in a single statement, you can write the code in a Python .py file, and run the file using the file option of python.ado or import it in an interactive session.

  3. The Stata GUI's Break button does not interrupt the plugin. There is not recourse for infinite loops in the plugin besides closing Stata.

  4. The plugin does not have continuous access to user input. Python code requiring continuous control over stdin, such as the input() function, will not work.

  5. Calling sys.exit() in a Python file will close Stata. In the interactive interpreter, sys.exit() may be safely used to exit the plugin, but in a Python file sys.exit() will close Stata.

Installation

The file INSTALL includes installation instructions for Windows and Mac OS X.

Usage

If the plugin is installed correctly, typing python should open an interactive session of Python.

The functionality of the plugin comes mainly in the form of st_ functions that are meant to be analogs of Mata's st_ functions. For example,

. sysuse auto
(1978 Automobile Data)

. list make-head in 1

     +----------------------------------------------+
     | make          price   mpg   rep78   headroom |
     |----------------------------------------------|
  1. | AMC Concord   4,099    22       3        2.5 |
     +----------------------------------------------+

. scalar s = 1234.5

. python
--------------------------- python (type exit() to exit) ---
. st_numscalar("s")
1234.5

. st_isstrvar(0)
True

. _st_sdata(0, 0)
'AMC Concord'

. st_isnumvar(1)
True

. _st_data(0, 1)
4099.0

. exit()
------------------------------------------------------------

For more examples and a complete description of the package's functionality, see python_plugin.tex.

License

Copyright (c) 2013, James Fiedler (MIT License)

Copyright (c) 2013 James Fiedler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Use Python within Stata 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/chenjiahao_elevate/python-in-stata.git
git@gitee.com:chenjiahao_elevate/python-in-stata.git
chenjiahao_elevate
python-in-stata
python-in-stata
master

搜索帮助