将Python代码转换为PHP

问题描述 投票:12回答:7

那里有一个软件转换器可以自动将此python代码转换为PHP吗?

#!/usr/bin/python
import math

def calcNumEntropyBits(s):
        if len(s) <= 0: return 0.0
        symCount = {}
        for c in s:
                if c not in symCount: symCount[c] = 1
                else: symCount[c] += 1
        entropy = 0.0
        for c,n in symCount.iteritems():
                prob = n / float(len(s))
                entropy += prob * (math.log(prob)/math.log(2))
        if entropy >= 0.0: return 0.0
        else: return -(entropy*len(s))

def testEntropy(s):
        print "Bits of entropy in '%s' is %.2f" % (s, calcNumEntropyBits(s))

testEntropy('hello world')
testEntropy('bubba dubba')
testEntropy('aaaaaaaaaaa')
testEntropy('aaaaabaaaaa')
testEntropy('abcdefghijk')
php python code-translation transpiler
7个回答
14
投票

我不知道在野外有任何Python到PHP的转换器,但是移植它应该是一件微不足道的工作,相似之处很容易发现:

function calcNumEntropyBits($s) {
        if (strlen($s) <= 0) return 0.0;
        $symCount = array();
        foreach (str_split($s) as $c) {
                if (!in_array($c,$symCount)) $symCount[$c] = 1;
                else $symCount[$c] ++;
        }
        $entropy = 0.0;
        foreach ($symCount as $c=>$n) {
                $prob = $n / (float)strlen($s);
                $entropy += $prob * log($prob)/log(2);
        }
        if ($entropy >= 0.0) return 0.0;
        else return -($entropy*strlen($s));
}

function testEntropy($s):
        printf("Bits of entropy in '%s' is %.2f",$s,calcNumEntropyBits($s));

testEntropy('hello world');
testEntropy('bubba dubba');
testEntropy('aaaaaaaaaaa');
testEntropy('aaaaabaaaaa');
testEntropy('abcdefghijk');

第一个函数的最后几行也可以被编写为标准的PHP三元表达式:

return ($entropy >= 0.0)? 0.0: -($entropy*strlen($s));

7
投票

我创建了一个名为py2php的python-to-php转换器。它可以自动转换基本逻辑,然后您需要调整库调用等。仍处于实验阶段。

这里是由OP提供的python自动生成的PHP。

<?php
require_once('py2phplib.php');
require_once( 'math.php');
function calcNumEntropyBits($s) {
    if ((count($s) <= 0)) {
        return 0.0;
    }
    $symCount = array();
    foreach( $s as $c ) {
        if (!$symCount.__contains__($c)) {
            $symCount[$c] = 1;
        }
        else {
            $symCount[$c] += 1;
        }
    }
    $entropy = 0.0;
    foreach( $symCount->iteritems() as $temp_c ) {
        $prob = ($n / float(count($s)));
        $entropy += ($prob * (math::log($prob) / math::log(2)));
    }
    if (($entropy >= 0.0)) {
        return 0.0;
    }
    else {
        return -($entropy * count($s));
    }
}

function testEntropy($s) {
    pyjslib_printFunc(sprintf('Bits of entropy in \'%s\' is %.2f', new pyjslib_Tuple([$s, calcNumEntropyBits($s)])));
}

testEntropy('hello world');
testEntropy('bubba dubba');
testEntropy('aaaaaaaaaaa');
testEntropy('aaaaabaaaaa');
testEntropy('abcdefghijk');

由于数学导入和__contains__,它无法正确运行,但是通过手工修复这些问题很容易。


6
投票

我用Python制作PHP解释器大约完成了1/2步,我可以告诉你,实际上有数十种主要的边缘案例发挥着成千上万种可能性,这几乎使得将Python移植到PHP几乎不可能。 Python的语法比PHP强得多,而在该语言上却有更进一步的发展,与同类语言相比,Python的stdlib可能是最先进的语言之一。

我的建议是使您的问题再上一步,为什么您需要在PHP中使用一组基于Python的逻辑。尝试移植/转换代码的替代方法包括从PHP到Python的子处理,使用Gearman让Python在后端处理,而PHP处理视图逻辑,或者更复杂的解决方案是在两者之间实现服务总线或消息队列。 PHP应用程序和Python服务。

PS。对任何可读性问题深表歉意,请立即完成为期2天的冲刺。


2
投票

不存在这样的工具,您必须自己移植代码


1
投票

我想知道自己同样的问题,并且我在GitHubGist站点中找到了这个PyToPhp.py文件。它很简单,并且似乎是开始的起点。

我将看看它!


0
投票

我从https://github.com/dan-da/py2php更改了库py2php,并将其分叉到https://github.com/bunkahle/py2php的新存储库中现在,您还可以使用已翻译为PHP代码的python数学库。仍然需要对代码进行修改才能使其正常工作。


0
投票
def compute_cost(X, y, theta):
    """ Vectorized implementation of MSE cost function.

    Arguments:
        - X: Inputs, a 2d array of shape (m, n + 1)
        - y: Target values, a 2d array of shape (m, 1)
        - theta: Parameters, a 2d array of shape (n + 1, 1)

    Outputs:
        - the mean squared error value
    """
    raise Implemented('You should implement compute_cost function')
© www.soinside.com 2019 - 2024. All rights reserved.