将Python代码转换为PHP

2024-04-29 19:36:00 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一个软件转换器可以自动将这个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')

Tags: inlogforlenreturnif软件def
3条回答

我不知道有任何Python-to-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));

我创建了一个名为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');

由于数学导入和包含,它将无法正确运行,但这些将很容易手工修复。

我用Python编写一个PHP解释器已经差不多完成了1/2的工作,我可以直接告诉您,实际上有几十个主要的边缘案例,它们提供了成千上万的可能性,这使得将Python移植到PHP几乎不可能。Python的语法比PHP强得多,而在该语言中,Python的stdlib可能是其类中与任何其他语言相比最先进的语言之一。

我的建议是将您的问题再往后退一步,看看为什么您需要一组PHP中基于Python的逻辑。尝试移植/翻译代码的替代方案可能包括从PHP到Python的子流程、使用Gearman让Python在后台工作而PHP处理视图逻辑,或者更复杂的解决方案是在PHP应用程序和Python服务之间实现服务总线或消息队列。

另外,对任何可读性问题表示歉意,刚刚完成了为期两天的sprint。

相关问题 更多 >