将 Python 代码转换为 PHP

发布于 2024-09-30 19:35:07 字数 1539 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

花之痕靓丽 2024-10-07 19:35:07

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

I'm not aware of any Python-to-PHP converter in the wild, but it should be a trivial task to port and the similarities are quite easy to spot:

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');

The last few lines in the first function could have also been written as a standard PHP ternary expression:

return ($entropy >= 0.0)? 0.0: -($entropy*strlen($s));
眉目亦如画i 2024-10-07 19:35:07

我创建了一个名为 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__ ,它无法正确运行,但这些很容易手动修复。

I created a python-to-php converter called py2php. It can auto-translate the basic logic and then you will need to tweak library calls, etc. Still experimental.

Here is auto-generated PHP from the python provided by the OP.

<?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');

It would not run correctly due to the math import and __contains__, but those would be easy enough to fix by hand.

余生共白头 2024-10-07 19:35:07

我用 Python 制作 PHP 解释器已经完成了大约 1/2,我可以坦率地告诉你,实际上有几十种主要的边缘情况,这些情况会产生数千种可能性,这使得将 Python 移植到 PHP 几乎是不可能的。 Python 具有比 PHP 更强大的语法,同时该语言更先进,与同类中的任何其他语言相比,Python 的 stdlib 可能是最先进的之一。

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

附言。对于任何可读性问题,我们深表歉意,刚刚结束了 2 天的冲刺。

I am about 1/2 way done making a PHP interpreter in Python and I can tell you flat out that there are literally dozens of major edge cases that play out to thousands of possibilities that would make it almost impossible to port Python to PHP. Python has a much more robust grammar then PHP while further foward in the language, Python's stdlib is probably one of the most advanced in comparison to any other language in it's class.

My recommendation is to take your question one step further back, to why do you need a set of Python based logic in PHP. Alternatives to attempting to port/translate your code could include subprocessing from PHP to Python, using Gearman to have Python do work in the backend while PHP handles view logic, or a much more involved solution would be to implement a service bus or message queue between a PHP application and Python services.

PS. Apologies for any readability issues, finishing a 2 day sprint just now.

那伤。 2024-10-07 19:35:07

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

No such tool exists, you'll have to port the code yourself

三五鸿雁 2024-10-07 19:35:07

我从 https://github.com/dan-da/py2php 更改了库 py2php 并将其分叉到 https://github.com/bunkahle/py2php 的新存储库中
您现在还可以使用已转换为 PHP 代码的 python 数学库。尽管如此,您仍然必须对代码进行调整才能使其正常工作。

I changed the library py2php from https://github.com/dan-da/py2php and forked it into a new repository at https://github.com/bunkahle/py2php
You now can also use the python math library which is translated to PHP code. Still you have to do adaptations to your code in order to get it to work.

凌乱心跳 2024-10-07 19:35:07

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

我要去看看!

I wondered myself that same question, and I found this PyToPhp.py file in the GitHubGist site. It is simple, and seem an start point for the begining.

I'm going to take a look to it!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文