【问题标题】:PHP - how to call a specific function from a python program (Python 3.4, PHP 7, CodeIgniter)PHP - 如何从 Python 程序调用特定函数(Python 3.4、PHP 7、CodeIgniter)
【发布时间】:2017-05-14 01:33:23
【问题描述】:

我有一个由一个函数组成的 python 文件,machineLearning(issue)

在我的 PHP 代码中,要运行 python 脚本,我有以下内容:

'category' => exec("python C:/xampp/htdocs/ci/assets/machineLearning.py $temp 2>&1"),

它按预期运行机器学习功能。但是,我需要包含另一个名为updateTrain 的函数,它接受参数issue, category。这是当前的python文件:

from textblob import TextBlob;
from textblob.classifiers import NaiveBayesClassifier;
import sys;

train = [
    ('I cannot enter or exit the building','idCard'),
    ('Enter or exit', 'idCard'),
    ('Card needs replacing','idCard'),
    ('ID card not working','idCard'),
    ('Printing', 'printer'),
    ("My ID Card doesn't sign me into the printers", "printer"),
    ('Mono', "printer"),
    ('Colour Printing',"printer"),
    ('I cannot connect my phone to the wifi', 'byod'),
    ('I do not get emails on my phone, tablet, or laptop', 'byod'),
    ('Cannot send emails','email'),
    ('Do not recieve emails','email'),
    ('Cannot sign in to moodle', 'email'),
    ('Cannot sign in to computer','desktop_pc'),
    ("Software isn't working",'desktop_pc'),
    ('Scratch disks are full', 'desktop_pc'),
    ('I have forgotten my password for the computers', 'desktop_pc'),
    ('forgotten password','forgotten_password'),
    ('My password does not work','forgotten_password'),
    ('Applications fail to launch and I get an error', 'desktop_pc'),
    ('My ID card does not allow me to print. I go to scan my card on the printer, and it comes up with a message saying that I need to associate my card every day I want to print. Other peoples cards work fine?','idCard'),
    ("I am having trouble connecting my android phone to my emails. I open the gmail app and select exchange account. I then enter my email and password and click okay on the message that asks if its alright to use the autoconfigure.xml. It then gives me all the server details, I click okay and then it tells me that I cannot connect to the server.", 'byod'),
    ('I cannot find my work', 'lost_work'),
    ('My work has been corrupted', 'lost_work'),
    ('My work has been lost', 'lost_work'),
    ('Coursework gone missing', 'lost_work'),
    ('my id card isnt working','idCard'),
    ('I cannot print in colour', 'printer')
]

cl = NaiveBayesClassifier(train);

def updateTrain(issue, category):
    new_data = [(issue,category)];
    cl.update(new_data);


def machineLearning(issue):
    test = [
        ("My id card does not allow me to go through the gates, the light flashes orange and then goes red. The gate doesn't open", "idCard"),
        ('My id card has snapped', 'idCard'),
        ('When printing, swiping my ID card does not automatically sign me in, I have to manually register my card', 'printer'),
        ('I am getting errors when trying to sign into my email account on the website', 'email'),
        ('Microsoft applications were not working','desktop_pc'),
        ('Software is missing','desktop_pc'),
        ('software keeps crashing','desktop_pc'),
        ('my phone refuses to connect to the internet','byod'),
        ('I cannot sign in to the computers, moodle or emails because I have forgotten my password','desktop_pc'),
        ('I cannot get my emails on my device I bought from home','byod'),
        ('My ID card does not allow me to print. I go to scan my card on the printer, and it comes up with a message saying that I need to associate my card every day I want to print. Other peoples cards work fine?','idCard'),
        ('My password does not allow me to sign in to the computers, laptops, moodle or emails','forgotten_password'),
        ('I have forgotten my password','forgotten_poassword'),
        ('My work has been corrupted', 'lost_work'),
        ('My work has been lost', 'lost_work'),
        ('Coursework gone missing', 'lost_work'),
        ('I cannot print in colour', 'printer'),
    ];
    print(cl.classify(issue));

if __name__ == "__main__": 
    machineLearning(sys.argv[1]);

由于updateTrain(issue, category) 需要2 个参数,我该如何更改上述exec 命令以使其忽略更新列车?

同样,我如何调用updateTrain,而忽略machineLearning 函数?

【问题讨论】:

    标签: php python codeigniter python-3.x php-7


    【解决方案1】:

    考虑到您从 PHP 代码调用 Python 脚本的方式,我认为最好的解决方案(或者至少从当前情况来看是最简单的)是向该脚本传递参数。

    在脚本的主体 (if __name__ == "__main__") 中,您需要测试传递给脚本的参数。例如,如果你想调用updateTrain 方法,忽略machineLearning 方法:

    if sys.argv[1] == "updateTrain":
        updateTrain()
    elif sys.argv[1] == "machineLearning":
        machineLearning()
    

    然后,当您从 PHP 调用此脚本时,您需要将 updateTrainmachineLearning 作为参数传递,这很容易,因为您正在运行 Bash 命令:

    'category' => exec("python C:/xampp/htdocs/ci/assets/machineLearning.py updateTrain 2>&1")
    

    在更复杂的情况下,当您需要将参数传递给您调用的函数时,您也需要将它们传递给命令。例如,由于updateTrain 有两个参数,比如两个整数:

    if sys.argv[1] == "updateTrain":
        issue = int(sys.argv[2])
        category = int(sys.argv[3])
        updateTrain(issue, category)
    

    您的 PHP 调用将变为:

    'category' => exec("python C:/xampp/htdocs/ci/assets/machineLearning.py updateTrain 12 42 2>&1")
    

    最后,您的脚本将如下所示:

    if __name__ == "__main__":
        if sys.argv[1] == "updateTrain":
            if len(sys.argv) >= 4:
                issue = sys.argv[2]      # convert it to the right type
                category = sys.argv[3]   # convert it too
                updateTrain(issue, category)
    
        elif sys.argv[1] == "machineLearning":
            if len(sys.argv) >= 3:
                issue = sys.argv[2]   # convert it here as well
                machineLearning(issue)
    
        else:
            print("Unknown command")
    

    脚本通常会被以下命令调用,这些命令需要传递给 PHP 中的exec

    python machineLearning.py updateTrain 12 42
    python machineLearning.py machineLearning 25
    

    作为旁注,请记住这是一个快速的解决方案,而为脚本提供实际且完整的类似 Bash 命令的行为会更加复杂。此外,还有几个库,例如 argparse,可以更轻松地编写完整的命令。

    【讨论】:

    • 你能解释一下为什么需要找到 sys.argv 的长度吗?
    • @RobbieWhite 这是一个非常、非常、非常基本的输入验证。你想访问sys.argv[2],所以你想知道你是否可以。虽然“尝试然后原谅”被认为比“先请求许可”更符合 Python 风格,但我认为在这种特殊情况下,最好在访问其元素之前测试 sys.argv 的长度,而不是尝试访问其元素然后处理IndexError 异常。
    • @RobbieWhite 但无论如何,它不是必需的,特别是如果唯一的呼叫者是你。我只是想给自己一些守卫。
    • 啊,我明白了,谢谢!我对 PHP 还很陌生,虽然我可以在 python 中做相当复杂的事情,但我对 sys.argv 并不熟悉。
    • @RobbieWhite 好吧,这只是一个list,所以应该这样处理。特别是关于索引,还有元素的类型,因为每个元素都是一个字符串(因为它来自命令行)。问题是,您可能期望类似命令的脚本与“经典”脚本的行为不同,例如异常处理。
    猜你喜欢
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    • 2013-02-25
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多