【问题标题】:Run python function from terminal从终端运行python函数
【发布时间】:2015-05-22 22:11:41
【问题描述】:

我有一个不错的 python 类,我需要实例化该类,然后我需要在该类中运行一个特定的函数。基本上,我们使用像 PHP 这样的语言来运行 shell 命令。这是我的python类(lighting.py):

#!/usr/bin/python

from phue import Bridge
from pprint import pprint
import time
import logging;logging.basicConfig()

class OfficeLights(object):

    #Basically Python's constructor
    def __init__(self):
        self.ip = 'xx.xx.xx.xx'
        self.username = 'xxxxx'
        self.lightInterface = Bridge(self.ip, self.username) 
        self.lightInterface.connect()
        self.lightInterface.get_api()
        self.lightInterface.create_group('Office', [1,2,3,4])
        self.cycles = 15 
        self.period = 1 
        self.evvDev = 'http://dev.google.com'
        self.evvStage = 'http://staging.google.com'

    #List all the lights available to play with
    def listLights(self):
        lights = self.lightInterface.lights

        for l in lights:
            print(l.name)

    #Generic strobe function
    def strobe(self, hue, cycles):
        for x in range(0, cycles):
            self.lightInterface.set_group(1, 'on', True)
            self.lightInterface.set_group(1, 'hue', hue)
            self.lightInterface.set_group(1, 'bri', 254)
            time.sleep(self.period)
            self.lightInterface.set_group(1, 'on', False)
            time.sleep(self.period)

    #Flashing funtions, to be executed on actions
    def flashRed(self):
        self.strobe(0, self.cycles)

    def flashGreen(self):
        self.strobe(25500, self.cycles)

    def flashPurple(self):
        self.strobe(46920, self.cycles)

    def flashPink(self):
        self.strobe(56100, self.cycles)

    #Check if a website is up/down based on https status headers
    def is_website_online(self, host):
        import httplib2
        h = httplib2.Http()
        resp = h.request(host, 'HEAD')
        return int(resp[0]['status']) < 400

    #Check EVV sites for up/down
    def check_evv_sites(self):
        if(self.is_website_online(self.evvDev) is not True):
            self.flashRed()
        if(self.is_website_online(self.evvStage) is not True):
            self.flashRed()
        else:
            self.flashGreen()

我正在尝试从终端运行命令,但我只收到“未定义 OfficeLights”的错误?不知道我还需要做什么?

python -c 'import lighting; lights = OfficeLights(); lights.flashPurple();'

【问题讨论】:

    标签: python macos bash terminal


    【解决方案1】:

    测试样本:

    └> cat hello.py
    class Hello:
        def hello(self):
            print "hello"
    
    └> python -c 'from hello import Hello; h= Hello(); h.hello()'
    hello
    
    └> python -c 'import hello; h= hello.Hello(); h.hello()'
    hello
    

    您可以选择import mypackage.mymodulefrom mypackage.mymodule import myclass

    python -c 'from lighting import OfficeLights; lights = OfficeLights(); lights.flashPurple();'
    

    【讨论】:

    • 谢谢!我根本没有考虑导入类本身,现在工作得很好!
    【解决方案2】:

    确保您引用了导入的模块或使用上一个答案中列出的 from 方法。

    python -c 'import lighting; lights = lighting.OfficeLights(); lights.flashPurple();'
    

    此外,模块需要在您的项目路径中,或者需要从包含模块的同一目录发出命令。

    【讨论】:

      猜你喜欢
      • 2015-05-16
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-23
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多