-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First implementation of schedule (#4)
* Add Schedule of filtering (#3) * Add Run class * duration in hours * complete tests * Add schedule * Remove unused stuff * Update README.md * Type hint and missing parameter.
- Loading branch information
1 parent
05962cf
commit 3e53636
Showing
11 changed files
with
196 additions
and
288 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,12 @@ | |
from setuptools import Command, find_packages, setup | ||
|
||
# Package meta-data. | ||
NAME = "pypool-pump" | ||
NAME = "pypool_pump" | ||
PYTHON_PACKAGE_FOLDER = "pypool_pump" | ||
DESCRIPTION = "Pool filtering pump duration" | ||
DESCRIPTION = "Calculate pool filtering pump duration and schedule" | ||
URL = "https://github.com/oncleben31/pypool-pump" | ||
EMAIL = "[email protected]" | ||
AUTHOR = "Oncleben31" | ||
REQUIRES_PYTHON = ">=2.7.0" | ||
VERSION = None | ||
|
||
# What packages are required for this module to be executed? | ||
|
@@ -91,7 +90,6 @@ def run(self): | |
long_description_content_type="text/markdown", | ||
author=AUTHOR, | ||
author_email=EMAIL, | ||
python_requires=REQUIRES_PYTHON, | ||
url=URL, | ||
packages=find_packages("src"), | ||
package_dir={"": "src"}, | ||
|
@@ -103,10 +101,7 @@ def run(self): | |
# Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python", | ||
"Programming Language :: Python :: 2", | ||
"Programming Language :: Python :: 3.7", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: 3.6", | ||
], | ||
# $ setup.py publish support. | ||
cmdclass={"upload": UploadCommand}, | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# coding: utf-8 | ||
"""Run class to manage time interval where the pool pump need to be active. | ||
""" | ||
|
||
from datetime import timedelta, datetime | ||
|
||
|
||
class Run: | ||
"""Represents a single run of the pool pump.""" | ||
|
||
def __init__(self, start_time_local_tz, duration_in_hours): | ||
"""Initialise run.""" | ||
self._start_time = start_time_local_tz | ||
self._duration = duration_in_hours | ||
|
||
def __repr__(self): | ||
"""Return string representation of this feed.""" | ||
return "<{}(start={}, stop={}, duration={})>".format( | ||
self.__class__.__name__, self.start_time, self.stop_time, self.duration | ||
) | ||
|
||
@property | ||
def duration(self): | ||
"""Return duration of this run.""" | ||
return self._duration | ||
|
||
@property | ||
def start_time(self): | ||
"""Return start time of this run.""" | ||
return self._start_time | ||
|
||
@property | ||
def stop_time(self): | ||
"""Return stop time of this run.""" | ||
return self.start_time + timedelta(hours=self.duration) | ||
|
||
def run_now(self, local_time): | ||
"""Check if the provided time falls within this run's timeframe.""" | ||
return self.start_time <= local_time < self.stop_time | ||
|
||
def is_next_run(self, local_time): | ||
"""Check if this is the next run after the provided time.""" | ||
return local_time <= self.stop_time | ||
|
||
def pretty_print(self): | ||
"""Provide a usable representation of start and stop time.""" | ||
if self.start_time.day != datetime.now().day: | ||
start = self.start_time.strftime("%a, %H:%M") | ||
else: | ||
start = self.start_time.strftime("%H:%M") | ||
end = self.stop_time.strftime("%H:%M") | ||
return "{} - {}".format(start, end) |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# coding: utf-8 | ||
"""tests for pypool_pump module - Run class""" | ||
|
||
import pytest | ||
from datetime import datetime, timedelta | ||
|
||
from pypool_pump import Run | ||
|
||
def test_run_propeties(): | ||
"""Test Run() properties""" | ||
date = datetime(2020,5,5,12,0) | ||
duration_in_minutes = 65 | ||
run = Run(date, duration_in_minutes/60) | ||
|
||
assert [run.start_time, run.duration, run.stop_time] == [date, duration_in_minutes/60, datetime(2020,5,5,13,5,0)] | ||
|
||
@pytest.mark.parametrize( | ||
"curent_time, state", | ||
[ | ||
(datetime(2020,5,4,12,0), False), | ||
(datetime(2020,5,5,11,59), False), | ||
(datetime(2020,5,5,12,0), True), | ||
(datetime(2020,5,5,12,10), True), | ||
(datetime(2020,5,5,13,0), True), | ||
(datetime(2020,5,5,13,4,59), True), | ||
(datetime(2020,5,5,13,5), False), | ||
(datetime(2020,5,5,13,6), False), | ||
(datetime(2020,5,6,12,50), False), | ||
], | ||
) | ||
def test_run_now(curent_time,state): | ||
"""Test Run() run_now() methods""" | ||
date = datetime(2020,5,5,12,0) | ||
duration_in_minutes = 65 | ||
run = Run(date, duration_in_minutes/60) | ||
|
||
assert run.run_now(curent_time) == state | ||
|
||
@pytest.mark.parametrize( | ||
"curent_time, state", | ||
[ | ||
(datetime(2020,5,4,12,0), True), | ||
(datetime(2020,5,5,11,59), True), | ||
(datetime(2020,5,5,12,0), True), | ||
(datetime(2020,5,5,12,10), True), | ||
(datetime(2020,5,5,13,0), True), | ||
(datetime(2020,5,5,13,4,59), True), | ||
(datetime(2020,5,5,13,5), True), | ||
(datetime(2020,5,5,13,6), False), | ||
(datetime(2020,5,6,12,50), False), | ||
], | ||
) | ||
def test_run_is_next_run(curent_time, state): | ||
""" Test Run() is_next_run() methods""" | ||
date = datetime(2020,5,5,12,0) | ||
duration_in_minutes = 65 | ||
run = Run(date, duration_in_minutes/60) | ||
|
||
assert run.is_next_run(curent_time) == state | ||
|
||
def test_run_print(): | ||
""" Test Run() prints methods""" | ||
date = datetime(2020,5,5,12,0) | ||
duration_in_minutes = 60 | ||
run = Run(date, duration_in_minutes/60) | ||
|
||
assert "{}".format(run) == "<Run(start={}, stop={}, duration=1.0)>".format(datetime(2020,5,5,12,0),datetime(2020,5,5,13,0)) | ||
|
||
def test_run_pretty_print(): | ||
""" Test Run() prints methods""" | ||
date = datetime.now() | ||
date2 = date + timedelta(days=2) | ||
duration_in_minutes = 65 | ||
run1 = Run(date, duration_in_minutes/60) | ||
run2 = Run(date2, duration_in_minutes/60) | ||
|
||
assert [run1.pretty_print(), run2.pretty_print() ] == [ | ||
"{} - {}".format(date.strftime("%H:%M"), (date + timedelta(minutes=65)).strftime("%H:%M")), | ||
"{} - {}".format(date2.strftime("%a, %H:%M"), (date2 + timedelta(minutes=65)).strftime("%H:%M")), | ||
] |