This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
'''recorder.py | |
Provides WAV recording functionality via two approaches: | |
Blocking mode (record for a set duration): | |
>>> rec = Recorder(channels=2) | |
>>> with rec.open('blocking.wav', 'wb') as recfile: | |
... recfile.record(duration=5.0) | |
Non-blocking mode (start and stop recording): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The slow way | |
class Person: | |
def __init__(self, name, occupation): | |
self.name = name | |
self.occupation = occupation | |
self.relatives = self._get_all_relatives() | |
def _get_all_relatives(): | |
... | |
# This is an expensive operation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Better | |
class Person: | |
def __init__(self, name, occupation): | |
self.name = name | |
self.occupation = occupation | |
self._relatives = None | |
@property | |
def relatives(self): | |
if self._relatives is None: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Even better | |
def lazy_property(fn): | |
'''Decorator that makes a property lazy-evaluated. | |
''' | |
attr_name = '_lazy_' + fn.__name__ | |
@property | |
def _lazy_property(self): | |
if not hasattr(self, attr_name): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Boiler: | |
def safety_check(self): | |
# Convert fixed-point floating point | |
temperature = self.modbus.read_holding() | |
pressure_psi = self.abb_f100.register / F100_FACTOR | |
if (psi_to_pascal(pressure_psi) > MAX_PRESSURE or | |
temperature > MAX_TEMPERATURE): | |
# Shutdown! | |
self.pnoz.relay[15] &= MASK_POWER_COIL | |
self.pnoz.port.write('$PL,15\0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Better | |
class Boiler: | |
def safety_check(self): | |
if any([self.temperature > MAX_TEMPERATURE, | |
self.pressure > MAX_PRESSURE]): | |
if not self.shutdown(): | |
self.alarm() | |
def alarm(self): | |
with open(BUZZER_MP3_FILE) as f: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# At initiation `point` is not well-formed | |
point = Point() | |
point.x = 12 | |
point.y = 5 | |
# Better | |
point = Point(x=12, y=5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point: | |
def __init__(self, x, y): | |
self.x, self.y = x, y | |
@classmethod | |
def polar(cls, r, theta): | |
return cls(r * cos(theta), | |
r * sin(theta)) | |
point = Point.polar(r=13, theta=22.6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def send_task(self, task, job, obligation): | |
... | |
processed = ... | |
... | |
copied = ... | |
... | |
executed = ... | |
100 more lines |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TaskSender: | |
def __init__(self, task, job obligation): | |
self.task = task | |
self.job = job | |
self.obligation = obligation | |
self.processed = [] | |
self.copied = [] | |
self.executed = [] | |
def __call__(self): |
OlderNewer