Skip to content

Commit

Permalink
Upgrading YAPF to 0.17.0 (#151)
Browse files Browse the repository at this point in the history
YAPF once again returns a non-zero exit code when diffs exist, so we can
simplify the YAPF command in the build script.
  • Loading branch information
mtlynch committed Aug 31, 2017
1 parent 7a96c18 commit aa1aed4
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 97 deletions.
9 changes: 2 additions & 7 deletions build
Expand Up @@ -15,18 +15,13 @@ coverage run \
pyflakes greenpithumb/*.py tests/*.py

# Check that source has correct formatting.
DIFF=$(yapf \
yapf \
--diff \
--recursive \
--style google \
./ \
--exclude "./greenpithumb/dht11/*" \
--exclude "./third_party/*")
if [ -n "${DIFF}" ]; then
echo "FAIL: Formatting does not match style:"
echo "${DIFF}"
exit 1
fi
--exclude "./third_party/*"

# Check that docstrings are formatted correctly.
PYTHONPATH=$PYTHONPATH:$(pwd)/third_party/docstringchecker \
Expand Down
2 changes: 1 addition & 1 deletion dev_requirements.txt
Expand Up @@ -2,4 +2,4 @@ coverage
mock
pyflakes==1.2.3
pylint==1.5.5
yapf==0.14.0
yapf==0.17.0
4 changes: 2 additions & 2 deletions greenpithumb/clock.py
Expand Up @@ -15,8 +15,8 @@ def wait(self, wait_time_seconds):
wait_time_seconds: Number of seconds to wait.
"""
if wait_time_seconds < 0.0:
raise ValueError('Wait time cannot be negative: %f' %
wait_time_seconds)
raise ValueError(
'Wait time cannot be negative: %f' % wait_time_seconds)
time.sleep(wait_time_seconds)

def now(self):
Expand Down
9 changes: 5 additions & 4 deletions greenpithumb/db_store.py
Expand Up @@ -127,8 +127,8 @@ def _do_insert(self, sql, timestamp, value):
value: Value to insert for the record.
"""
timestamp_utc = _timestamp_to_utc(timestamp)
self._cursor.execute(sql,
(timestamp_utc.strftime(_TIMESTAMP_FORMAT), value))
self._cursor.execute(sql, (timestamp_utc.strftime(_TIMESTAMP_FORMAT),
value))
self._connection.commit()

def _do_get(self, sql, record_type):
Expand All @@ -144,8 +144,9 @@ def _do_get(self, sql, record_type):
self._cursor.execute(sql)
data = []
for row in self._cursor.fetchall():
timestamp = datetime.datetime.strptime(
row[0], _TIMESTAMP_FORMAT).replace(tzinfo=pytz.utc)
timestamp = datetime.datetime.strptime(row[0],
_TIMESTAMP_FORMAT).replace(
tzinfo=pytz.utc)
data.append((timestamp, row[1]))
typed_data = map(record_type._make, data)
return typed_data
Expand Down
8 changes: 4 additions & 4 deletions greenpithumb/greenpithumb.py
Expand Up @@ -222,16 +222,16 @@ def main(args):
record_queue = Queue.Queue()
raspberry_pi_io = pi_io.IO(GPIO)
adc = make_adc(wiring_config)
local_soil_moisture_sensor = make_soil_moisture_sensor(adc, raspberry_pi_io,
wiring_config)
local_soil_moisture_sensor = make_soil_moisture_sensor(
adc, raspberry_pi_io, wiring_config)
local_temperature_sensor, local_humidity_sensor = make_dht11_sensors(
wiring_config)
local_light_sensor = make_light_sensor(adc, wiring_config)
camera_manager = make_camera_manager(args.camera_rotation, args.image_path,
local_light_sensor)

with contextlib.closing(db_store.open_or_create_db(
args.db_file)) as db_connection:
with contextlib.closing(
db_store.open_or_create_db(args.db_file)) as db_connection:
record_processor = create_record_processor(db_connection, record_queue)
pump_manager = make_pump_manager(
args.moisture_threshold,
Expand Down
5 changes: 2 additions & 3 deletions greenpithumb/light_sensor.py
Expand Up @@ -43,8 +43,7 @@ def light(self):
('Light sensor reading of %i is less than the minimum '
'expected value of %i.') % (light, _LIGHT_SENSOR_MIN_VALUE))

light_as_pct = 100 * (
float(light - _LIGHT_SENSOR_MIN_VALUE) /
(_LIGHT_SENSOR_MAX_VALUE - _LIGHT_SENSOR_MIN_VALUE))
light_as_pct = 100 * (float(light - _LIGHT_SENSOR_MIN_VALUE) / (
_LIGHT_SENSOR_MAX_VALUE - _LIGHT_SENSOR_MIN_VALUE))

return light_as_pct
4 changes: 2 additions & 2 deletions greenpithumb/poller.py
Expand Up @@ -106,8 +106,8 @@ def _next_poll_time_unix(self):
next_poll_time_unix = _round_up_to_multiple(
self._unix_now(), int(self._poll_interval.total_seconds()))
if self._last_poll_time and (
next_poll_time_unix ==
_datetime_to_unix_time(self._last_poll_time)):
next_poll_time_unix == _datetime_to_unix_time(
self._last_poll_time)):
next_poll_time_unix += int(self._poll_interval.total_seconds())

return next_poll_time_unix
Expand Down
4 changes: 2 additions & 2 deletions greenpithumb/record_processor.py
Expand Up @@ -56,6 +56,6 @@ def try_process_next_record(self):
elif isinstance(record, db_store.WateringEventRecord):
self._watering_event_store.insert(record)
else:
raise UnsupportedRecordError('Unrecognized record type: %s' %
str(record))
raise UnsupportedRecordError(
'Unrecognized record type: %s' % str(record))
return True
82 changes: 36 additions & 46 deletions tests/test_db_store.py
Expand Up @@ -40,8 +40,8 @@ def test_does_not_initialize_existing_db_file(self, mock_connect):
def test_creates_file_and_tables_when_db_does_not_already_exist(self):
# Create a path for a file that does not already exist.
db_path = os.path.join(self._temp_dir, 'test.db')
with contextlib.closing(db_store.open_or_create_db(
db_path)) as connection:
with contextlib.closing(
db_store.open_or_create_db(db_path)) as connection:
cursor = connection.cursor()
# Insertions into all tables should work after initialization.
cursor.execute('INSERT INTO temperature VALUES (?, ?)',
Expand Down Expand Up @@ -98,15 +98,13 @@ def test_get_soil_moisture(self):
soil_moisture_data.sort(
key=lambda SoilMoistureRecord: SoilMoistureRecord.timestamp)

self.assertEqual(
soil_moisture_data[0].timestamp,
datetime.datetime(
2016, 7, 23, 10, 51, 0, tzinfo=pytz.utc))
self.assertEqual(soil_moisture_data[0].timestamp,
datetime.datetime(
2016, 7, 23, 10, 51, 0, tzinfo=pytz.utc))
self.assertEqual(soil_moisture_data[0].soil_moisture, 300)
self.assertEqual(
soil_moisture_data[1].timestamp,
datetime.datetime(
2016, 7, 23, 10, 52, 0, tzinfo=pytz.utc))
self.assertEqual(soil_moisture_data[1].timestamp,
datetime.datetime(
2016, 7, 23, 10, 52, 0, tzinfo=pytz.utc))
self.assertEqual(soil_moisture_data[1].soil_moisture, 400)

def test_get_soil_moisture_empty_database(self):
Expand Down Expand Up @@ -146,15 +144,13 @@ def test_get_light(self):
light_data = store.get()
light_data.sort(key=lambda LightRecord: LightRecord.timestamp)

self.assertEqual(
light_data[0].timestamp,
datetime.datetime(
2016, 7, 23, 10, 51, 0, tzinfo=pytz.utc))
self.assertEqual(light_data[0].timestamp,
datetime.datetime(
2016, 7, 23, 10, 51, 0, tzinfo=pytz.utc))
self.assertEqual(light_data[0].light, 300)
self.assertEqual(
light_data[1].timestamp,
datetime.datetime(
2016, 7, 23, 10, 52, 0, tzinfo=pytz.utc))
self.assertEqual(light_data[1].timestamp,
datetime.datetime(
2016, 7, 23, 10, 52, 0, tzinfo=pytz.utc))
self.assertEqual(light_data[1].light, 400)

def test_get_light_empty_database(self):
Expand Down Expand Up @@ -194,15 +190,13 @@ def test_get_humidity(self):
humidity_data = store.get()
humidity_data.sort(key=lambda HumidityRecord: HumidityRecord.timestamp)

self.assertEqual(
humidity_data[0].timestamp,
datetime.datetime(
2016, 7, 23, 10, 51, 0, tzinfo=pytz.utc))
self.assertEqual(humidity_data[0].timestamp,
datetime.datetime(
2016, 7, 23, 10, 51, 0, tzinfo=pytz.utc))
self.assertEqual(humidity_data[0].humidity, 50)
self.assertEqual(
humidity_data[1].timestamp,
datetime.datetime(
2016, 7, 23, 10, 52, 0, tzinfo=pytz.utc))
self.assertEqual(humidity_data[1].timestamp,
datetime.datetime(
2016, 7, 23, 10, 52, 0, tzinfo=pytz.utc))
self.assertEqual(humidity_data[1].humidity, 51)

def test_get_humidity_empty_database(self):
Expand All @@ -220,8 +214,8 @@ def test_insert_temperature(self):
store = db_store.TemperatureStore(self.mock_connection)
store.insert(temperature_record)
self.mock_cursor.execute.assert_called_once_with(
'INSERT INTO temperature VALUES (?, ?)',
('2016-07-23T10:51Z', 21.1))
'INSERT INTO temperature VALUES (?, ?)', ('2016-07-23T10:51Z',
21.1))
self.mock_connection.commit.assert_called_once()

def test_insert_temperature_with_non_utc_time(self):
Expand All @@ -233,8 +227,8 @@ def test_insert_temperature_with_non_utc_time(self):
store = db_store.TemperatureStore(self.mock_connection)
store.insert(temperature_record)
self.mock_cursor.execute.assert_called_once_with(
'INSERT INTO temperature VALUES (?, ?)',
('2016-07-23T15:51Z', 21.1))
'INSERT INTO temperature VALUES (?, ?)', ('2016-07-23T15:51Z',
21.1))
self.mock_connection.commit.assert_called_once()

def test_get_temperature(self):
Expand All @@ -245,15 +239,13 @@ def test_get_temperature(self):
temperature_data.sort(
key=lambda TemperatureRecord: TemperatureRecord.timestamp)

self.assertEqual(
temperature_data[0].timestamp,
datetime.datetime(
2016, 7, 23, 10, 51, 0, tzinfo=pytz.utc))
self.assertEqual(temperature_data[0].timestamp,
datetime.datetime(
2016, 7, 23, 10, 51, 0, tzinfo=pytz.utc))
self.assertEqual(temperature_data[0].temperature, 21.0)
self.assertEqual(
temperature_data[1].timestamp,
datetime.datetime(
2016, 7, 23, 10, 52, 0, tzinfo=pytz.utc))
self.assertEqual(temperature_data[1].timestamp,
datetime.datetime(
2016, 7, 23, 10, 52, 0, tzinfo=pytz.utc))
self.assertEqual(temperature_data[1].temperature, 21.5)

def test_get_temperature_empty_database(self):
Expand Down Expand Up @@ -296,15 +288,13 @@ def test_get_water_pumped(self):
watering_event_data.sort(
key=lambda WaterintEventRecord: WaterintEventRecord.timestamp)

self.assertEqual(
watering_event_data[0].timestamp,
datetime.datetime(
2016, 7, 23, 10, 51, 0, tzinfo=pytz.utc))
self.assertEqual(watering_event_data[0].timestamp,
datetime.datetime(
2016, 7, 23, 10, 51, 0, tzinfo=pytz.utc))
self.assertEqual(watering_event_data[0].water_pumped, 300)
self.assertEqual(
watering_event_data[1].timestamp,
datetime.datetime(
2016, 7, 23, 10, 52, 0, tzinfo=pytz.utc))
self.assertEqual(watering_event_data[1].timestamp,
datetime.datetime(
2016, 7, 23, 10, 52, 0, tzinfo=pytz.utc))
self.assertEqual(watering_event_data[1].water_pumped, 301)

def test_get_water_pumped_empty_database(self):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_pi_io.py
Expand Up @@ -36,8 +36,9 @@ def test_setup_only_called_once_per_pin(self):
io.turn_pin_off(pin_two)
io.turn_pin_on(pin_two)

self.assertEqual(mock_GPIO.setup.call_args_list, [(
(pin_one, mock_GPIO.OUT),), ((pin_two, mock_GPIO.OUT),)])
self.assertEqual(mock_GPIO.setup.call_args_list,
[((pin_one, mock_GPIO.OUT),), ((pin_two,
mock_GPIO.OUT),)])

def test_close(self):
mock_GPIO = mock.Mock()
Expand Down
15 changes: 5 additions & 10 deletions tests/test_poller.py
Expand Up @@ -100,16 +100,14 @@ def test_last_poll_time_updates_when_wait_completes(self):
# time.
self.assertTrue(scheduler.wait_until_poll_time(timeout=120))
self.assertEqual(
datetime.datetime(
2017, 4, 9, 11, 45, 0, tzinfo=pytz.utc),
datetime.datetime(2017, 4, 9, 11, 45, 0, tzinfo=pytz.utc),
scheduler.last_poll_time())

self.mock_clock.now.return_value = datetime.datetime(
2017, 4, 9, 11, 49, 29, tzinfo=pytz.utc)
self.assertTrue(scheduler.wait_until_poll_time(timeout=120))
self.assertEqual(
datetime.datetime(
2017, 4, 9, 11, 50, 0, tzinfo=pytz.utc),
datetime.datetime(2017, 4, 9, 11, 50, 0, tzinfo=pytz.utc),
scheduler.last_poll_time())


Expand Down Expand Up @@ -162,8 +160,7 @@ def test_temperature_poller(self):
self.block_until_poll_completes()

self.assertEqual(
db_store.TemperatureRecord(
timestamp=TIMESTAMP_A, temperature=21.0),
db_store.TemperatureRecord(timestamp=TIMESTAMP_A, temperature=21.0),
self.record_queue.get(block=True, timeout=TEST_TIMEOUT_SECONDS))
# Should be no more items in the queue.
self.assertTrue(self.record_queue.empty())
Expand All @@ -180,8 +177,7 @@ def test_humidity_poller(self):
self.block_until_poll_completes()

self.assertEqual(
db_store.HumidityRecord(
timestamp=TIMESTAMP_A, humidity=50.0),
db_store.HumidityRecord(timestamp=TIMESTAMP_A, humidity=50.0),
self.record_queue.get(block=True, timeout=TEST_TIMEOUT_SECONDS))
# Should be no more items in the queue.
self.assertTrue(self.record_queue.empty())
Expand All @@ -198,8 +194,7 @@ def test_light_poller(self):
self.block_until_poll_completes()

self.assertEqual(
db_store.LightRecord(
timestamp=TIMESTAMP_A, light=50.0),
db_store.LightRecord(timestamp=TIMESTAMP_A, light=50.0),
self.record_queue.get(block=True, timeout=TEST_TIMEOUT_SECONDS))
# Should be no more items in the queue.
self.assertTrue(self.record_queue.empty())
Expand Down
8 changes: 4 additions & 4 deletions tests/test_pump.py
Expand Up @@ -186,8 +186,8 @@ def test_current_hour_and_minute_equal_to_sleep_time(self):
"""Running pump should not be allowed."""
now = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000, tzinfo=pytz.utc)
self.mock_local_clock.now.return_value = now
sleep_windows = [(datetime.time(now.hour, now.minute),
datetime.time(11, 0))]
sleep_windows = [(datetime.time(now.hour, now.minute), datetime.time(
11, 0))]
pump_scheduler = pump.PumpScheduler(self.mock_local_clock,
sleep_windows)
self.assertFalse(pump_scheduler.is_running_pump_allowed())
Expand All @@ -196,8 +196,8 @@ def test_current_hour_and_minute_equal_to_wake_time(self):
"""Running pump should be allowed."""
now = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000, tzinfo=pytz.utc)
self.mock_local_clock.now.return_value = now
sleep_windows = [(datetime.time(10, 0), datetime.time(now.hour,
now.minute))]
sleep_windows = [(datetime.time(10, 0), datetime.time(
now.hour, now.minute))]
pump_scheduler = pump.PumpScheduler(self.mock_local_clock,
sleep_windows)
self.assertTrue(pump_scheduler.is_running_pump_allowed())
6 changes: 2 additions & 4 deletions tests/test_pump_history.py
Expand Up @@ -23,8 +23,7 @@ def test_last_pump_time_returns_timestamp_when_only_one_event_in_db(self):
2017, 1, 1, 12, 45, 9, 123456, tzinfo=pytz.utc))
]
self.assertEqual(
datetime.datetime(
2017, 1, 1, 12, 45, 9, 123456, tzinfo=pytz.utc),
datetime.datetime(2017, 1, 1, 12, 45, 9, 123456, tzinfo=pytz.utc),
pump_history.last_pump_time(self.mock_watering_event_store))

def test_last_pump_time_returns_timestamp_when_db_has_many_events(self):
Expand All @@ -37,6 +36,5 @@ def test_last_pump_time_returns_timestamp_when_db_has_many_events(self):
2015, 12, 5, 12, 45, 9, 555555, tzinfo=pytz.utc)),
]
self.assertEqual(
datetime.datetime(
2017, 3, 2, 0, 15, 59, 987654, tzinfo=pytz.utc),
datetime.datetime(2017, 3, 2, 0, 15, 59, 987654, tzinfo=pytz.utc),
pump_history.last_pump_time(self.mock_watering_event_store))
12 changes: 6 additions & 6 deletions tests/test_sleep_windows.py
Expand Up @@ -8,12 +8,12 @@ class TestSleepWindowParser(unittest.TestCase):

def test_parser_valid_inputs(self):
self.assertEqual(
sleep_windows.parse(['01:00-03:15']),
[(datetime.time(1, 0), datetime.time(3, 15))])
sleep_windows.parse(['01:00-03:15']), [(datetime.time(1, 0),
datetime.time(3, 15))])
self.assertEqual(
sleep_windows.parse(['01:00-03:15', '13:45-16:00']),
[(datetime.time(1, 0), datetime.time(3, 15)),
(datetime.time(13, 45), datetime.time(16, 0))])
[(datetime.time(1, 0), datetime.time(3, 15)), (datetime.time(
13, 45), datetime.time(16, 0))])
# Overlapping ranges OK.
self.assertEqual(
sleep_windows.parse(['01:00-03:15', '02:45-04:00']),
Expand All @@ -26,8 +26,8 @@ def test_parser_valid_inputs(self):
(datetime.time(2, 45), datetime.time(3, 0))])
# Hypersomnia OK.
self.assertEqual(
sleep_windows.parse(['01:42-01:42']),
[(datetime.time(1, 42), datetime.time(1, 42))])
sleep_windows.parse(['01:42-01:42']), [(datetime.time(1, 42),
datetime.time(1, 42))])

def test_parser_invalid_inputs(self):
invalid_inputs = [
Expand Down

0 comments on commit aa1aed4

Please sign in to comment.