Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Testing times with yaml fixture #106

Open
wants to merge 13 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
11 changes: 11 additions & 0 deletions week005-testing/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

language: python
python:
- "3.7"
- "3.8"
# command to install dependencies
#install:
- pip install requests pyyaml pytest pytest-cov
# command to run tests
script:
- pytest --cov ./
24 changes: 24 additions & 0 deletions week005-testing/fixture.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

- generic:
time_range_1: ["2010-01-12 10:00:00", "2010-01-12 12:00:00"]
time_range_2: ["2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60]
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"]

- no_overlap:
time_range_1: ["2010-01-12 10:00:00", "2010-01-12 11:00:00"]
time_range_2: ["2010-01-12 12:30:00", "2010-01-12 12:45:00", 2, 60]
expected: []

- multiple_overlaps:
time_range_1: ["2010-01-12 10:00:00", "2010-01-12 13:00:00", 3, 900]
time_range_2: ["2010-01-12 10:40:00", "2010-01-12 11:20:00", 2, 120]
expected:
- ["2010-01-12 10:40:00","2010-01-12 10:50:00"]
- ["2010-01-12 11:05:00", "2010-01-12 11:20:00"]

- touching:
time_range_1: ["2010-01-12 10:00:00", "2010-01-12 11:00:00"]
time_range_2: ["2010-01-12 11:00:00", "2010-01-12 12:45:00"]
expected: []
19 changes: 19 additions & 0 deletions week005-testing/test_times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from times import time_range
from times import compute_overlap_time
from pytest import raises
import pytest
import yaml

with open('fixture.yaml', 'r') as yaml_file:
fixture = yaml.safe_load(yaml_file)
print(fixture)

@pytest.mark.parametrize("test_name", fixture)
def test_general_overlap(test_name):
properties = list(test_name.values())[0]
first_range = time_range(*properties['time_range_1'])
second_range = time_range(*properties['time_range_2'])
expected_overlap = [(start, stop) for start, stop in properties['expected']]
print(properties)
assert compute_overlap_time(first_range, second_range) == expected_overlap

35 changes: 35 additions & 0 deletions week005-testing/times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 end_time_s < start_time_s:
# raise ValueError("end_time must be *later* than start_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)]
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:
if (end1 <= start2) or (end2 <= start1):
pass
#elif (end1 == start2):
# overlap_time.append(end1)
#elif (end2 == start1):
# overlap_time.append(end2)
else:
low = max(start1, start2)
high = min(end1, end2)
overlap_time.append((low, high))
return overlap_time

if __name__ == "__main__":
#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)
large = time_range("2010-01-12 10:00:00", "2010-01-10 12:00:00")
# short = time_range("2010-01-12 10:30:00", "2010-01-12 10:40:00")
# print(compute_overlap_time(large, short))
48 changes: 48 additions & 0 deletions week04/quakes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""A script to find the biggest earthquake in an online dataset."""

# At the top of the file, import any libraries you will use.
import requests
import json

# If you want, you can define some functions to help organise your code.
# def helper_function(argument_1, argument_2):
# ...

# When you run the file, it should print out the location and magnitude
# of the biggest earthquake.
# You can run the file with `python quakes.py` from this directory.
if __name__ == "__main__":

quakes = requests.get("http://earthquake.usgs.gov/fdsnws/event/1/query.geojson",
params={
'starttime': "2000-01-01",
"maxlatitude": "58.723",
"minlatitude": "50.008",
"maxlongitude": "1.67",
"minlongitude": "-9.756",
"minmagnitude": "1",
"endtime": "2018-10-11",
"orderby": "time-asc"}
).text

with open('quakes_data.json' ,'w') as quakes_data_file:
quakes_data_file.write(quakes)

with open('quakes_data.json','r') as quakes_source:
quakes_content = quakes_source.read()

quakes_data = json.loads(quakes_content)

magnitudes = [i['properties']['mag'] for i in quakes_data['features']]

max_magnitude = max(magnitudes)

coords_finder = (i['geometry']['coordinates'] for i in quakes_data['features'] \
if i['properties']['mag'] == max_magnitude)

coords = [xyz for xyz in coords_finder]

# The lines below assume that the results are stored in variables
# named max_magnitude and coords, but you can change that.
print(f"The maximum magnitude is {max_magnitude} "
f"and it occured at coordinates {coords}.")
Loading