From f0f269666a2c8f3f783accd6dac7ddf0f9755f82 Mon Sep 17 00:00:00 2001 From: OscarCharlieST Date: Mon, 31 Oct 2022 13:12:32 +0000 Subject: [PATCH 1/6] Changed testing times, added test file --- .../test_times.cpython-37-pytest-6.1.2.pyc | Bin 0 -> 1203 bytes __pycache__/times.cpython-37.pyc | Bin 0 -> 1394 bytes test_times.py | 13 +++++++++++++ times.py | 2 +- 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 __pycache__/test_times.cpython-37-pytest-6.1.2.pyc create mode 100644 __pycache__/times.cpython-37.pyc create mode 100644 test_times.py diff --git a/__pycache__/test_times.cpython-37-pytest-6.1.2.pyc b/__pycache__/test_times.cpython-37-pytest-6.1.2.pyc new file mode 100644 index 0000000000000000000000000000000000000000..19bc5d3b4d120a85f3476f433857fb323a32c8f5 GIT binary patch literal 1203 zcma)5Jx?1k7`AxcETz5CEJ05E=%du zxg!G^`wu#H?O*WPiNC_4_`4-cR4{&d!#1fBL;PLFgB{^?;3bV2L*% zD55w*9c*?ITLfVg+Z_k%6A~5TVy9?y3v{>R8XZTaPKnyo`HVVcir*oxa0#){zK7Lv zFO9R2WPW!u6~NwYY%qu1B8TXD4`a$&+M_b99jUY2o{z& zM>#$tV?te8qGdWWu*X(Ta%)J&c5aLBl3ZA{lG}YuXEoj@6Ps40ec?>dupkcMypWR# z<{Y%5))GRibnb@n#V{;jd^HTvfH6ns&&eY+cIX0K%$*Q}UXU&@N_k;|&+sRh-q@xH z=CHJXy40*U>Z|p}YNJ_e)LV7^-UXXe@KRsI02V^E^PRhtdTaf@U%j=l39gsh;Ng+~ za#-I2hX=SX?iF<%{BLff)zCTbC3`6DKe(o74_}dX+ry`e&z@%|bx+jV?V47bo=^qO z#3+)=Wgj!vlZ<*qS-{ewd0GvGU|bsZ2CxC#@XFkRDR4bPRo=-?BoopPxLWXgK@|C% zWn6!V0cWZL$Ns@6jAWQVt|$eDP>+3an2sW9sQw^I4}!=~f|#k2FM>YP5mfb+Ngbxi zYtB=ymi&Io<3RdQXcQ!YgcGl*oG9P}rW}aKCD&gycXeSiI`g#p#?+X)uirK{zahQ? z?5nN4R`)%0D!Olv6UMugiJ?rh?lEJ-)y=N{f5fWpQ*=$Y23X5Z)VwhV;So#xFoB;> zS0I)_AY8>&QpOeRA{%``6;wt1Dd66Wa{r8kR{>Bf{&a|I(|Vz?HO`KCC}AKskSbnd uyrO*^Q-Zk!a~;a1fn*vqNlaU}#1rk~ZpSGdMeL=%KT!tZqD55EC6eFf`#VIoV;t{^41v>Ka4l9I>cMrDWKs5Q4Tu}om*X%932&2P=Oayxwwt59*LJZm6sOTjIMGd_Kr$8yX=Hz#}LhinB zynpm{%>H19J~(FywKXM1WxgF+JT8PBZVGjv%lQx#585w=D5u(5 zD6JOvwfS%F;z2%DdQ{Hl>zK5{>_c|?wKFQSVj|WCc{u`y>u|Xfy$1`chn3t$-EV+6 zu)Z3yidM|fD*))48n!s^fuR3W1i&UzT8`0i;z=}Wna*JE$w$B}NCR(8Ao#QhmL$H! zMsn(NRJToC>MT!hp|*j`BMEK%0GN$L!5wsVxj35b37MAT2Xti}Eyi~;0CpX$rOuFx z1VTi-oSekLk{FteA8^B%6h0EV1iw|f#2Qq70sII-#)VEW(U-8*7LW<4*vY0Ln`CzY z7yH9}&s!$W-85pgqrm68w2)78(Wu|7LF}sF=JACq*9LBFFIK>)9ti7-FCOP+U@i^`2X;+ zw|Klk=q6~7tp2(2p-89y>|?!7|M?40y%~T;!ratJob;sTn#R@RKAIwfNlI;yXL2HJ mp!Q3tZ7`QGbnj*By3tiT8q7+5l#6w==xQB#aU9hnm+HT|{!8`% literal 0 HcmV?d00001 diff --git a/test_times.py b/test_times.py new file mode 100644 index 0000000..be6b9fe --- /dev/null +++ b/test_times.py @@ -0,0 +1,13 @@ +# Test Times File + +from times import compute_overlap_time +from times import time_range +import datetime + +def test_given_input(): + large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00",2,3600/2) + 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'), ('2010-01-12 11:15:00', '2010-01-12 10:37:00'), ('2010-01-12 11:15:00', '2010-01-12 10:45:00')] + assert result == expected diff --git a/times.py b/times.py index d57f401..e3b2f5e 100644 --- a/times.py +++ b/times.py @@ -21,6 +21,6 @@ def compute_overlap_time(range1, range2): return overlap_time if __name__ == "__main__": - large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00") + large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00",2,3600/2) short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) print(compute_overlap_time(large, short)) \ No newline at end of file From 267f742bed906a505f8fc83b203380caa575d746 Mon Sep 17 00:00:00 2001 From: OscarCharlieST Date: Thu, 3 Nov 2022 10:38:17 +0000 Subject: [PATCH 2/6] Added tests, edited code to cover error cases --- .../test_times.cpython-37-pytest-6.1.2.pyc | Bin 1203 -> 2632 bytes __pycache__/times.cpython-37.pyc | Bin 1394 -> 1969 bytes test_times.py | 32 +++++++++++++++--- times.py | 25 ++++++++------ 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/__pycache__/test_times.cpython-37-pytest-6.1.2.pyc b/__pycache__/test_times.cpython-37-pytest-6.1.2.pyc index 19bc5d3b4d120a85f3476f433857fb323a32c8f5..c7023c48ed97703d4f84951647e423524e43b076 100644 GIT binary patch literal 2632 zcmd^A%Wfk@6s@X$y8VDW2s6Bt4HCUbWP1`JMFa%~w!;Fku`HU7tL$l|-ECEsCnU02 zOjxks2as4KOC*+j04qMCwyfqCX2rc_C$T3PqS-N`6W_Xh>vZ2L*YP=@*Xz;wFTLJh zE+K!Dg^NLc03Y=&1Wq{3Nr&2z4IM@(8M+;h;tb3EVbBR|?m!-PLYvdP(y4Hldq0v+ z#C;yXSmhzFz*yrEufka8^lK8=ovbT$-Fk8n(6S`0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times.py b/test_times.py index be6b9fe..645ed68 100644 --- a/test_times.py +++ b/test_times.py @@ -4,10 +4,34 @@ from times import time_range import datetime -def test_given_input(): - large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00",2,3600/2) - short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) +def test_subset_overlap(): + large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00", 2) + short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2) + result = compute_overlap_time(large, short) + expected = 15.0*60.0 + assert result == expected + +def test_partial_overlap(): + large = time_range("2010-01-12 10:00:00", "2010-01-12 10:40:00", 2) + short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2) + + result = compute_overlap_time(large, short) + expected = 10.0*60.0 + assert result == expected + +def test_vanishing_overlap(): + large = time_range("2010-01-12 10:45:00", "2010-01-12 12:00:00", 2) + short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2) 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'), ('2010-01-12 11:15:00', '2010-01-12 10:37:00'), ('2010-01-12 11:15:00', '2010-01-12 10:45:00')] + expected = 0.0 assert result == expected + +def test_zero_overlap(): + large = time_range("2010-01-12 12:00:00", "2010-01-12 12:00:00", 2) + short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2,) + + result = compute_overlap_time(large, short) + expected = 0.0 + assert result == expected + diff --git a/times.py b/times.py index e3b2f5e..361f1fa 100644 --- a/times.py +++ b/times.py @@ -1,4 +1,5 @@ import datetime +import time def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0): @@ -12,15 +13,19 @@ def time_range(start_time, end_time, number_of_intervals=1, gap_between_interval 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)) - return overlap_time + latest_start = max(min([start for start, end in range1]), + min([start for start, end in range2])) + earliest_end = min(max([end for start, end in range1]), + max([end for start, end in range2])) + ls_sec = time.mktime(time.strptime(latest_start, "%Y-%m-%d %H:%M:%S")) + ee_sec = time.mktime(time.strptime(earliest_end, "%Y-%m-%d %H:%M:%S")) + if ls_sec < ee_sec: + return ee_sec - ls_sec + else: + return 0 if __name__ == "__main__": - large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00",2,3600/2) - short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) - print(compute_overlap_time(large, short)) \ No newline at end of file + large = time_range("2010-01-12 10:00:00", "2010-01-12 10:31:00", 2) + short = time_range("2010-01-12 10:30:00", "2010-01-12 10:50:00", 2) + print() + print(compute_overlap_time(large, short), 'seconds of overlap') \ No newline at end of file From 11efac0765c07d2f834e309c4ec2c7882fd3da0a Mon Sep 17 00:00:00 2001 From: OscarCharlieST Date: Thu, 3 Nov 2022 10:44:48 +0000 Subject: [PATCH 3/6] Answers UCL-COMP0233-22-23/RSE-Classwork#19 --- test_times.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test_times.py b/test_times.py index 645ed68..35a30fc 100644 --- a/test_times.py +++ b/test_times.py @@ -35,3 +35,4 @@ def test_zero_overlap(): expected = 0.0 assert result == expected +# Forgot to mention Answers UCL-COMP0233-22-23/RSE-Classwork#19 From 94ace23e0030dfd5ab497e06dd8cf9a4b5338751 Mon Sep 17 00:00:00 2001 From: OscarCharlieST Date: Thu, 3 Nov 2022 11:07:23 +0000 Subject: [PATCH 4/6] Added input test which raises a value error Answers UCL-COMP0233-22-23/RSE-Classwork#20 --- test_times.py | 8 +++++++- times.py | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/test_times.py b/test_times.py index 35a30fc..25dc96d 100644 --- a/test_times.py +++ b/test_times.py @@ -2,7 +2,7 @@ from times import compute_overlap_time from times import time_range -import datetime +import pytest def test_subset_overlap(): large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00", 2) @@ -34,5 +34,11 @@ def test_zero_overlap(): result = compute_overlap_time(large, short) expected = 0.0 assert result == expected + +def test_input_values(): + with pytest.raises(ValueError): + large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00", 2) + short = time_range("2010-01-12 10:50:00", "2010-01-12 10:45:00", 2) + result = compute_overlap_time(large, short) # Forgot to mention Answers UCL-COMP0233-22-23/RSE-Classwork#19 diff --git a/times.py b/times.py index 361f1fa..f946903 100644 --- a/times.py +++ b/times.py @@ -1,8 +1,13 @@ import datetime import time +from pytest import raises -def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0): +def correctly_ordered_inputs(range0): + if range0[0][0] > range0[-1][1]: + raise ValueError("\n The input contains a time range which goes backwards in time as index is increased. \n Please ensure inputs are time-ordered.") + +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") d = (end_time_s - start_time_s).total_seconds() / number_of_intervals + gap_between_intervals_s * (1 / number_of_intervals - 1) @@ -12,7 +17,13 @@ def time_range(start_time, end_time, number_of_intervals=1, gap_between_interval 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): + + # Checking inputs + correctly_ordered_inputs(range1) + correctly_ordered_inputs(range2) + latest_start = max(min([start for start, end in range1]), min([start for start, end in range2])) earliest_end = min(max([end for start, end in range1]), @@ -23,9 +34,10 @@ def compute_overlap_time(range1, range2): return ee_sec - ls_sec else: return 0 + if __name__ == "__main__": - large = time_range("2010-01-12 10:00:00", "2010-01-12 10:31:00", 2) + large = time_range("2010-01-12 10:35:00", "2010-01-12 10:31:00", 2) short = time_range("2010-01-12 10:30:00", "2010-01-12 10:50:00", 2) print() print(compute_overlap_time(large, short), 'seconds of overlap') \ No newline at end of file From 1caee322d77e61ba81a2502d824f2483ebd6ad83 Mon Sep 17 00:00:00 2001 From: OscarCharlieST Date: Thu, 3 Nov 2022 11:14:04 +0000 Subject: [PATCH 5/6] Added github actions testing Answers UCL-COMP0233-22-23/RSE-Classwork#21 --- .github/workflows/python-tests.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workflows/python-tests.yml diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml new file mode 100644 index 0000000..efb5e99 --- /dev/null +++ b/.github/workflows/python-tests.yml @@ -0,0 +1,25 @@ +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 + - name: Test with pytest + run: | + pytest \ No newline at end of file From 3057136744d974ebccc0429467b40b82b8fdc6d0 Mon Sep 17 00:00:00 2001 From: Charlie <115104080+OscarCharlieST@users.noreply.github.com> Date: Thu, 3 Nov 2022 11:23:51 +0000 Subject: [PATCH 6/6] Added pytest UCL-COMP0233-22-23/RSE-Classwork#21 --- .github/workflows/python-tests.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index efb5e99..c7d9102 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -20,6 +20,7 @@ jobs: # This case we are only updating pip, but you could add other dependencies needed. run: | python -m pip install --upgrade pip + python3 -m pip install pytest - name: Test with pytest run: | - pytest \ No newline at end of file + pytest