Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test time first steps #26

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Python package

on: [push]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
# This case we are only updating pip, but you could add other dependencies needed.
run: |
python -m pip install --upgrade pip
python -m pip install pytest
- name: Test with pytest
run: |
pytest
Empty file added __init__.py
Empty file.
Binary file added __pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file added __pycache__/times.cpython-39.pyc
Binary file not shown.
15 changes: 15 additions & 0 deletions test_times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from times import time_range, compute_overlap_time

def test_given_input():
large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00")
short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60)
result = compute_overlap_time(large, short)
expected = [('2010-01-12 10:30:00', '2010-01-12 10:37:00'), ('2010-01-12 10:38:00', '2010-01-12 10:45:00')]
assert result == expected

def test_not_overlap():
large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00")
short = time_range("2010-01-12 13:00:00", "2010-01-12 14:00:00", 2, 60)
result = compute_overlap_time(large, short)
expected = []
assert result == expected
33 changes: 26 additions & 7 deletions times.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
import datetime


def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0):
start_time_s = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
end_time_s = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
if start_time_s > end_time_s:
raise ValueError("Time input {} is invalid".format(start_time+" "+end_time))
d = (end_time_s - start_time_s).total_seconds() / number_of_intervals + gap_between_intervals_s * (1 / number_of_intervals - 1)
sec_range = [(start_time_s + datetime.timedelta(seconds=i * d + i * gap_between_intervals_s),
start_time_s + datetime.timedelta(seconds=(i + 1) * d + i * gap_between_intervals_s))
for i in range(number_of_intervals)]
start_time_s + datetime.timedelta(seconds=(i + 1) * d + i * gap_between_intervals_s))
for i in range(number_of_intervals)]
return [(ta.strftime("%Y-%m-%d %H:%M:%S"), tb.strftime("%Y-%m-%d %H:%M:%S")) for ta, tb in sec_range]


def compute_overlap_time(range1, range2):

overlap_time = []
for start1, end1 in range1:
for start2, end2 in range2:
low = max(start1, start2)
high = min(end1, end2)
overlap_time.append((low, high))
if low < high:
overlap_time.append((low, high))
else:
pass
return overlap_time

def test_given_input():
large = time_range("2010-01-12 12:00:00", "2010-01-12 10:00:00")
short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60)
result = compute_overlap_time(large, short)
print(result)

if __name__ == "__main__":
def test_not_overlap():
large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00")
short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60)
print(compute_overlap_time(large, short))
short = time_range("2010-01-12 13:00:00", "2010-01-12 14:00:00", 2, 60)
result = compute_overlap_time(large, short)
print(result)


if __name__ == "__main__":
test_given_input()
test_not_overlap()