0

I have a nice python class and I need to instantiate the class, and then I need to run a specific function in that class. Basically, we are using a language like PHP to run shell commands. Here is my python class (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()

I am trying to run the command from terminal but I only ever get the error that 'OfficeLights is not defined'? Not sure what else I need to do?

python -c 'import lighting; lights = OfficeLights(); lights.flashPurple();'
asked May 22, 2015 at 22:11

2 Answers 2

2

test sample:

└> 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

you can choose either import mypackage.mymodule or from mypackage.mymodule import myclass

python -c 'from lighting import OfficeLights; lights = OfficeLights(); lights.flashPurple();'
answered May 22, 2015 at 22:16
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I did not consider at all to import the class itself, works very well now!
0

Make sure you reference the module imported or use the from method listed in the previous answer.

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

Additionally, the module needs to be in your project path, or the command needs to be issued from the same directory containing the module.

answered May 22, 2015 at 22:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.