开发者

Moving from PHP to Python

开发者 https://www.devze.com 2022-12-17 20:26 出处:网络
Currently I do all of my web-based programming in PHP, and each day I get more and more anxious to try Python.

Currently I do all of my web-based programming in PHP, and each day I get more and more anxious to try Python.

Not that I haven't played with it in an interpreter, but I mean really, write a web-based project in Python, and possibly move pretty much exclusively over to Python.

But, I know that Python isn't开发者_如何转开发 strictly a web-based programming language (which is awesome) which makes me wonder if there are any hoops to jump through to use it on the web.

A simple example would be, that I'm sure there's no $_POST like variables in Python by default. How does functionality like that find its way into Python?

How do I move from PHP to Python smoothly? How do I use Python on the web?


Here are some other web frameworks for your reference.


Python is unlike PHP executed by a CGI like interface. This interface has an API to such variables.

Frameworks like Django incorporate that API. It's recommended to use such a framework which makes a lot of things easier.


Have a look at django web framework.


Many web frameworks exist for Python, such as Django, werkzeug, etc. If you want to get "closer to the metal" then look into WSGI.


There is nothing wrong with PHP, except that it's not Python. So it's not Pythonic!

If you often have to go between the Python world and the PHP world (WordPress, Drupal, or other PHP framework), you can feel my pains for not being able to use tons and tons of native Python libraries with ease, such as SQLAlchemy, Machine Learning, Deep Learning such as TensorFlow, etc.

PHP frameworks such as WordPress do offer xml-rpc, wp-api, wp-cli, and other APIs to interface with Python and other languages. However, preparing Python programs for such APIs and having PHP to interface with the API on the other end is error prone, not robust, costly to API client and Web server in terms of CPU and Memory just for the sake of packing, transmitting, and unpacking for the API, hard to troubleshoot on both ends of the API, and not scalable. Hence, enterprises, web, or big-data applications cannot rely on those APIs for high-speed and high-volume Web applications and large scale data sets.

So comes pyx.php to PHP-to-Python programmers' rescue! (I wrote this.)

Why convert from PHP to Python?

If you ask this question, you probably shouldn't use pyx.php.

pyx.php is a Cython compiled module that you can use to convert or translate 99% of most common PHP source codes to pure Python. In another words, it is a PHP-to-Python syntax emulation library in Cython.

The converted Python codes only require a Python 3.x interpreter, the modules in the pyx repository, and some standard Python libraries. PHP interpreter is not needed at all.

If we have already translated most of the WordPress core and other scripts from PHP to Python using pyx.php, you can convert almost any PHP code into Python.

With the speed of compiled Cython, running Python code translated from PHP using pyx.php might be even faster than running the original PHP code in the same computer.

Installation

Download from your browser, or from Linux shell:

$ wget https://wordpy.com/pyx/pyx.tgz
$ tar xvfpz pyx.tgz

Alternatively, you can:

$ git clone https://github.com/wordpy/pyx/

Currently, pyx.php is only available for Python 3.x running 64-bit Linux. Python 2.x, Mac, or other platforms can be compiled when there are many requests.

Quick Start

$ python # or ipython

>>> import pyx.php as Php; array = Php.array
>>> arr1 = array( (0,'1-0'),('a','1-a'),('b','1-b'),)
>>> arr2 = array( (0,'2-0'),(  1,'2-1'),('b','2-b'),('c','2-c'),)
>>> arr1 + arr2   # same as: Php.array_plus(arr1, arr2), see below
>>> Php.array_merge(arr1, arr2)

For more, please see https://wordpy.com/pyx/php/

0

精彩评论

暂无评论...
验证码 换一张
取 消