patternpythonMinor
Randomized Lighting with Python
Viewed 0 times
withlightingrandomizedpython
Problem
I use this to automate the lights in my home while I am away at night (simulating presence). The scheduling of when this runs is handled by my home automation software (Indigo Domotics).
I'm open to any/all critique, but am especially interested in whether doing the randomization within the Light object is the right approach (and if the method of randomizing is good/pythonic/efficient). I'm a self taught programmer as a hobby only and am always trying to refine my thinking around code structure.
```
#!/usr/bin/python
from datetime import datetime
import random
# (min/max) seconds to delay before light turns on (randomized every time)
start_delay_range = (15, 70)
# (min/max) brightness (0-100, randomized every time)
brightness_range = (50, 85)
# (min/max) duration for light to stay on (randomized every time)
duration_range = (300, 900)
class Light():
'''
Define a light object.
Attributes:
name (str): Name of the light.
device_id (int): The Indigo ID of the device.
dimmable (bool): Set the light as dimmable or not.
'''
def __init__(self, name, device_id, dimmable):
self.name = name
self.device_id = device_id
self.dimmable = dimmable
def brightness(self):
'''Return random brightness within .'''
return random.randint(brightness_range[0], brightness_range[1])
def duration(self):
'''Return random duration within .'''
return random.randint(duration_range[0], duration_range[1])
def start_delay(self):
'''Return random delay within .'''
return random.randint(start_delay_range[0], start_delay_range[1])
def run(self):
'''Turn on the light to after for .'''
if self.dimmable:
# Turn on light
indigo.dimmer.setBrightness(self.device_id,
value = self.brightness(),
delay = self.start_delay())
else:
# Tu
I'm open to any/all critique, but am especially interested in whether doing the randomization within the Light object is the right approach (and if the method of randomizing is good/pythonic/efficient). I'm a self taught programmer as a hobby only and am always trying to refine my thinking around code structure.
```
#!/usr/bin/python
from datetime import datetime
import random
# (min/max) seconds to delay before light turns on (randomized every time)
start_delay_range = (15, 70)
# (min/max) brightness (0-100, randomized every time)
brightness_range = (50, 85)
# (min/max) duration for light to stay on (randomized every time)
duration_range = (300, 900)
class Light():
'''
Define a light object.
Attributes:
name (str): Name of the light.
device_id (int): The Indigo ID of the device.
dimmable (bool): Set the light as dimmable or not.
'''
def __init__(self, name, device_id, dimmable):
self.name = name
self.device_id = device_id
self.dimmable = dimmable
def brightness(self):
'''Return random brightness within .'''
return random.randint(brightness_range[0], brightness_range[1])
def duration(self):
'''Return random duration within .'''
return random.randint(duration_range[0], duration_range[1])
def start_delay(self):
'''Return random delay within .'''
return random.randint(start_delay_range[0], start_delay_range[1])
def run(self):
'''Turn on the light to after for .'''
if self.dimmable:
# Turn on light
indigo.dimmer.setBrightness(self.device_id,
value = self.brightness(),
delay = self.start_delay())
else:
# Tu
Solution
Looking at the random number generation, a lot of duplicated code can be easily removed.
Starting at the 4th line:
Another repetition that can be avoided concern the arguments of
And your (repetitive) properties are now much shorter!
Starting at the 4th line:
import random. The only function that is used is randint, so you could do from random import randint and then use randint everywhere, instead of random.randint.Another repetition that can be avoided concern the arguments of
randint: you can unpack the tuple using the *.def start_delay(self):
'''Return random delay within .'''
return randint(*start_delay_range)And your (repetitive) properties are now much shorter!
Code Snippets
def start_delay(self):
'''Return random delay within <start_delay_range>.'''
return randint(*start_delay_range)Context
StackExchange Code Review Q#121519, answer score: 2
Revisions (0)
No revisions yet.