Raspberry Pi 的 Python 脚本无法正常运行

标签 python linux raspberry-pi new-operator

我前几天写的 python 脚本有点问题。该脚本适用于我的 rasbperry pi,它有一个连接到 GPIO 引脚的数字接口(interface)。此脚本的目的是测试并查看它是否会解锁 5 秒的机电锁。虽然当我运行脚本时什么也没发生。没有错误被记录并且是 python 脚本的新手我不确定问题是什么。任何建议或帮助将不胜感激。

我知道数字接口(interface)和 raspberry pi 交互没有任何问题,因为我可以使用下面的命令手动运行 python,并且一切正常。锁定释放,然后我可以将其关闭。尽管当我运行我的脚本时什么也没发生。

python
import piface.pfio
piface.pfio.init()
piface.pfio.digital_write(0, 1)
led0 = piface.pfio.LED(0)
led0.turn_off()

我的 python 脚本

#!/usr/bin/env python3
"""Door Lock: System to control an electric lock"""

import piface.pfio as piface
from time import sleep

class AuthToken:
    def _init_(self, id, secret):
        self.id=id
        self.secret.secret

class TestDoorController:
    def send_open_pulse(self):
        print "unlock the door"

class BasicAuthenticator:
    id = "Andrew"
    secretPassword = "1234"
    def check(self,token):
        print "checking input of '" + token.id + "', + password + : " + token.secret + ", against secret password'" + self.secretPassword +"'"
        result = (token.secret == self.secretPassword) & (token.id == self.id)
        print "authentication is: " + str(result)
        return result

class TestInput:
    def getInput(self):
        print "checking for input"
        authToken + AuthToken("Andrew","1234")
        return authInput

class DoorControllerPiFace:
    def send_open_pulse(self):
        piface.digital_write(0,1)
        sleep(5)
        piface.digital_write(0,0)

def main():
    authInput = TestInput()
    authenticator = BasicAuthenticator()
    doorController = DoorControllerPiFace()

    if(authenticator.check(authInput.getInput())):
        doorController.send_open_pulse()
    if _name_ == '_main_':

        main()

现在我知道构造函数没有参数了吗?

文件“door_controllerTEST.py”,第 47 行,位于 主要()

文件“door_controllerTEST.py”,第 43 行,在 main 如果(authenticator.check(authInput.getInput())):

文件“door_controllerTEST.py”,第 30 行,在 getInput 中 返回 AuthToken("Andrew","1234")

TypeError: 这个构造函数没有参数

将额外的 _ 添加到我的 AuthToken 类中,它给我一个错误,提示我的 pfio 尚未初始化。所以我加入了

import piface.pfio as pfio
pfio.init()

之后一切正常。

最佳答案

类测试输入

函数 getInput(self): 没有返回任何东西(即变量 authInput 为空)。

您可能想要返回创建的 token ? 返回 AuthToken("Andrew","1234") 即。

BasicAuthenticator 类

此类检查 self.idself.secretPassword 但它们未设置。您正在设置 idsecretPassword

主要

你的 main 函数也没有被调用,应该是 if __name__ == '__main__': 带有双下划线。

class AuthToken:
    def __init__(self, id, secret):
        self.id=id
        self.secret=secret

class TestDoorController:
    def send_open_pulse(self):
        print "unlock the door"

class BasicAuthenticator:
    def __init__(self):
        self.id = "Andrew"
        self.secretPassword = "1234"

    def check(self,token):
        print "checking input of '" + token.id + "', + password + : " + token.secret + ", against secret password'" + self.secretPassword +"'"
        result = (token.secret == self.secretPassword) & (token.id == self.id)
        print "authentication is: " + str(result)
        return result

class TestInput:
    def getInput(self):
        print "checking for input"
        return AuthToken("Andrew","1234")

class DoorControllerPiFace:
    def send_open_pulse(self):
        piface.digital_write(0,1)
        sleep(5)
        piface.digital_write(0,0)

def main():
    authInput = TestInput()
    authenticator = BasicAuthenticator()
    doorController = DoorControllerPiFace()

    if(authenticator.check(authInput.getInput())):
        doorController.send_open_pulse()

if __name__ == '__main__':
    main()

关于Raspberry Pi 的 Python 脚本无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21219791/

相关文章:

python - 如何让 HTML 按钮激活 python 脚本?

python - 为什么我无法加载使用 pip 安装的 mkl 模块?

python - uwsgi 无法识别的选项 '--wsgi-file'

php - 如何粗略估计 LAMP 应用容量?

python - 如何使用 Popen.communicate(在 Linux 上)传递击键(ALT+TAB)?

text - 语音到文本翻译树莓派

python - PyAudio - 多 channel 管理\去混音

python - 查找由 scikit-image 的 blob_log(高斯拉普拉斯算子)方法识别的 Blob 列表的平均像素值

python - 使用 os.walk 制作 root 列表

linux - netbeans 不会从远程计算机(linux)读取编译所需的 .bashrc