EcStart PHP 技術討論論壇's Archiver

FIEND 發表於 2008-5-21 02:09

PiP - Python in PHP

<a href="http://www.csh.rit.edu/%7Ejon/projects/pip/" target="_blank">http://www.csh.rit.edu/~jon/projects/pip/<br><br></a><h1>PiP - Python in PHP</h1>
<p>
Both <a href="http://www.python.org/">Python</a> and <a href="http://www.php.net/">PHP</a>
are what has become known as interpretted scripting languages. Each has
achieved substantial popularity due primarily to their ability to
facilitate rapid prototyping. As both a Python and PHP enthusiast, I
wanted to bring these two systems together to promote even greater and
more interesting development opportunities.
</p>
<p>To that end, I've written a Python extension for PHP. In short, this
extensions allows the Python interpretter to be embedded inside of PHP
(think of PHP as the parent language with Python as its child). This
allows native Python objects to be instantiated and manipulated from
within PHP. There is also initial support for accessing PHP functions
and data from within the embedded Python environment.
</p>
<p>
What exists right now is primarily a "proof of concept" implementation.  It will serve as a solid starting point development.
</p>
<p>
The Python extension is available from its <a href="http://pecl.php.net/package/python">PECL package page</a>.  The source code currently lives <a href="http://cvs.php.net/cvs.php/pecl/python/">in the PECL CVS repository</a>.  Some packaged <a href="http://www.csh.rit.edu/%7Ejon/projects/pip/#dist">distributions</a> are also available.
</p>
<p>
<a href="http://www.csh.rit.edu/%7Ejon/pres/">Slides</a> from my presentations at the <a href="http://www.phpconference.de/2002/index_en.php">2002 International PHP Conference</a> are available.  I discussed <a href="http://www.csh.rit.edu/%7Ejon/pres/php-2002/pip-internals.html">internals</a> and <a href="http://www.csh.rit.edu/%7Ejon/pres/php-2002/pip-applications.html">applications</a>.
</p>

<a name="examples"><h2>Examples</h2></a>
<p>
Here's an example of what it can do at the moment:
</p>
<span class="caption">1. Evaluating Python Code from PHP</span>
<pre>&lt;?php<br>$a = "test";<br>$b = true;<br>$c = 50;<br>$d = 60.4;<br><br>$code = &lt;&lt;&lt;EOD<br>import php<br><br>a = php.var('a')<br>b = php.var('b')<br>c = php.var('c')<br>d = php.var('d')<br><br>print a, b, c, d<br>print a, d / c + b, a<br>EOD;<br><br>py_eval($code);<br>?&gt;<br></pre>

<span class="caption">Result</span>
<pre>test 1 50 60.4<br>test 2.208 test<br></pre>

<br>

<span class="caption">2. Python Object Instantiation</span>
<pre># module.py<br>class TestClass:<br>    def __init__(self, foo):<br>        self.foo = foo<br><br>    def returnInt(self):<br>        return 1113<br><br>    def test(self, a, b = 'str'):<br>        return "a = %d, b = %s" % (a, b)<br><br>    def returnMe(self):<br>        return self<br><br>    def returnTuple(self):<br>        return (1, "two", 3.0)<br><br>    def returnList(self):<br>        return [1, "two", 3.0]<br><br>    def returnDict(self):<br>        d = {}<br>        d['one'] = 1<br>        d['two'] = 2<br>        d['three'] = 3<br>        return d<br><br>    def p(self, var):<br>        print var<br></pre>

<pre>&lt;?php<br>$p = new Python('module', 'TestClass', array(435));<br>print $p-&gt;returnInt() . "\n";<br>print $p-&gt;test(1, 'bar') . "\n";<br><br>print $p-&gt;foo . "\n";<br>$p-&gt;foo = 987;<br>print $p-&gt;foo . "\n";<br><br># $copy points to the same object<br>$copy = $p-&gt;returnMe();<br>print $copy-&gt;foo . "\n";<br>$p-&gt;foo = 987;<br>print $copy-&gt;foo . "\n";<br>?&gt;<br></pre>

<span class="caption">Result</span>
<pre>1113<br>a = 1, b = bar<br>435<br>987<br>987<br>987<br></pre>

<br>

<span class="caption">3. Type Conversion</span>
<pre>&lt;?php<br>var_dump($p-&gt;returnTuple());<br>var_dump($p-&gt;returnList());<br>var_dump($p-&gt;returnDict());<br><br>$a = array('one' =&gt; 1, 2, 3);<br>$p-&gt;p($a);<br><br>class Test {<br>    var $member = 'test';<br>}<br><br>$t = new Test();<br>$p-&gt;p($t);<br>?&gt;<br></pre>

<span class="caption">Result</span>
<pre>array(3) {<br>  [0]=&gt;<br>  int(1)<br>  [1]=&gt;<br>  string(3) "two"<br>  [2]=&gt;<br>  float(3)<br>}<br>array(3) {<br>  [0]=&gt;<br>  int(1)<br>  [1]=&gt;<br>  string(3) "two"<br>  [2]=&gt;<br>  float(3)<br>}<br>array(3) {<br>  ["three"]=&gt;<br>  int(3)<br>  ["two"]=&gt;<br>  int(2)<br>  ["one"]=&gt;<br>  int(1)<br>}<br>{'1': 3, '0': 2, 'one': 1}<br>{'member': 'test'}<br></pre>

<p>As you can see, these current examples are fairly crude. In time,
I'll work up some more detailed examples, but hopefully the above
illustrate the extensions current cabilities.
</p>
<p>
A great deal of inspiration (in the forms design and code) for my Python in PHP extension came from the existing <a href="http://www.php.net/manual/en/ref.java.php">Java</a> PHP extension and the <a href="http://www.modpython.org/">mod_python</a> Apache module.
</p>

<a name="dist"><h2>Distributions</h2></a>
<p>The current code is still considered "alpha" quality. It may contain
bugs and definitely lacks features. There are still a large number of
performance issues that require consideration.
</p>
<p>
The distribution is packaged using the <a href="http://pear.php.net/">PEAR</a> installation tool.  Under supported platforms, the extension can be build using the following command:
</p>
<span class="caption">Building with PEAR</span>
<pre><b>$ pear build</b><br>running: phpize<br>PHP Api Version        : 20020307<br>Zend Module Api No     : 20020429<br>Zend Extension Api No  : 20021010<br>Python installation directory? [autodetect] : <br>building in /var/tmp/pear-build-jon/python-0.1<br>running: /home/jon/src/pear/PECL/python/configure --with-python<br>running: make<br>python.so copied to /home/jon/src/pear/PECL/python/python.so<br></pre>
<p>
A Microsoft Visual C++ 6.0 <a href="http://cvs.php.net/co.php/pecl/python/python.dsp">project file</a> is also included to facilitate building under Windows.
</p>
<p>
Note that you'll need <a href="http://www.python.org/">Python</a> installed in order to build the extension.  I'm currently building against <a href="http://www.python.org/2.2.2/">Python 2.2.2</a>.  I've heard that <a href="http://www.activestate.com/Products/ActivePython/">ActivePython</a> works, as well.
</p>
<p>Also, a lot of the behavior (particularly with regard to type
conversions) has not be finalized. The array to sequence conversion
will definitely evolve, for instance.
</p>
<p>
Feel free to contact me with comments or suggestions (remember: the best suggestions come in <tt>diff -u</tt> format).  I can't guarantee I'll have time to act on that immediately, but I will surely get to them soon.
</p>
<ul><li><a href="http://www.csh.rit.edu/%7Ejon/projects/pip/dist/ppython-0.6.0.tgz">Python in PHP 0.6.0</a> - PEAR package (released November 5, 2002)</li><li><a href="http://www.csh.rit.edu/%7Ejon/projects/pip/dist/pip-0.5.zip">Python in PHP 0.5</a> - Zip file (released July 7, 2002)</li></ul>

<p>
These files are also available via <a href="ftp://ftp.csh.rit.edu/pub/members/jon/pip/">anonymous FTP</a>.
</p><a href="http://www.csh.rit.edu/%7Ejon/projects/pip/" target="_blank"><br></a>

FIEND 發表於 2008-5-21 02:17

[url]http://pecl.php.net/package/python[/url]

頁: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.