Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
py-training-2017
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
Le runner partagé sera indisponible toute la journée du mercredi 9 juillet.
Show more breadcrumbs
python-uga
py-training-2017
Commits
e50344a0
Commit
e50344a0
authored
1 year ago
by
Franck Thollard
Browse files
Options
Downloads
Patches
Plain Diff
weather station: bug fixes plus API changes
parent
bc4731a3
Branches
Branches containing commit
No related tags found
1 merge request
!5
Merging Testing
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pyfiles/weather_station.py
+27
-51
27 additions, 51 deletions
pyfiles/weather_station.py
with
27 additions
and
51 deletions
pyfiles/weather_station.py
+
27
−
51
View file @
e50344a0
import
unittest
#!/usr/bin/env python3
class
WeatherStation
(
object
):
"""
A weather station that holds wind and temperature
...
...
@@ -7,62 +7,38 @@ class WeatherStation(object):
wind and temperature must have the same length.
"""
def
__init__
(
self
,
wind
,
temperature
):
self
.
wind
=
list
(
wind
)
def
__init__
(
self
,
wind
:
list
[
int
]
,
temperature
:
list
[
float
]
):
self
.
wind
=
[
abs
(
x
)
for
x
in
wind
]
self
.
temp
=
list
(
temperature
)
if
len
(
self
.
wind
)
!=
len
(
self
.
temp
):
raise
ValueError
(
"
wind and temperature should have the same size
"
)
def
max_temp
(
self
)
:
def
max_temp
(
self
,
percieved
=
True
)
->
float
:
"""
returns the maximum temperature recorded in the station
"""
if
percieved
:
return
max
(
self
.
all_percieved_temp
())
else
:
return
max
(
self
.
temp
)
def
arg_max_temp
(
self
)
:
def
arg_max_temp
(
self
,
percieved
=
True
)
->
int
:
"""
returns the index of (one of the) maximum temperature recorded in the station
"""
return
self
.
temp
.
index
(
self
.
max_temp
())
paris
=
WeatherStation
([
10
,
0
,
20
,
30
,
20
,
0
],
[
1
,
5
,
1
,
-
1
,
-
1
,
3
])
# OR paris = WeatherStation(wind=[10, 0, 20, 30, 20, 0], temperature=[1, 5, 1, -1, -1, 3])
idx_max_temp
=
paris
.
arg_max_temp
()
print
(
f
"
max temp is
{
paris
.
max_temp
()
}
°C at index
{
paris
.
arg_max_temp
()
}
"
)
print
(
f
"
wind speed at max temp =
{
paris
.
wind
[
idx_max_temp
]
}
km/h
"
)
# testing
class
TestWeatherStation
(
unittest
.
TestCase
):
"""
Test the weather station
"""
def
setUp
(
self
):
"""
Generates a well structured station (paris)
"""
self
.
paris
=
WeatherStation
(
[
10
,
0
,
20
,
30
,
20
,
0
],
[
1
,
5
,
1
,
-
1
,
-
1
,
3
],
)
def
test_building_with_good_input_arrays
(
self
):
"""
test that things goes smoothly if the input are correct
"""
self
.
assertEqual
(
0
,
self
.
paris
.
wind
[
1
])
self
.
assertEqual
(
5
,
self
.
paris
.
temp
[
1
])
def
test_building_with_input_iterables
(
self
):
"""
test that things goes smoothly if the input are correct
"""
r_station
=
WeatherStation
(
range
(
10
),
range
(
10
))
self
.
assertEqual
(
4
,
r_station
.
wind
[
4
])
self
.
assertEqual
(
5
,
r_station
.
temp
[
5
])
def
test_building_with_bad_arrays
(
self
):
"""
test that an exception is raised with incorrect inputs
"""
with
self
.
assertRaises
(
ValueError
):
bad_station
=
WeatherStation
([
10
,
0
,
20
,
30
,
20
,
0
],
[
1
,
5
,
1
])
def
test_max_temp
(
self
):
"""
test test_max_temp function
"""
self
.
assertEqual
(
5
,
self
.
paris
.
max_temp
())
def
test_arg_max_temp
(
self
):
"""
test arg_max_temp function
"""
self
.
assertEqual
(
1
,
self
.
paris
.
arg_max_temp
())
if
percieved
:
temp
=
self
.
all_percieved_temp
()
return
temp
.
index
(
self
.
max_temp
(
temp
))
else
:
return
self
.
temp
.
index
(
self
.
max_temp
(
percieved
=
False
))
def
percieve_temp
(
self
,
temp
:
float
,
wind
:
int
)
->
float
:
"""
returns the percieved temp given a temp and a wind speed
"""
if
wind
==
0
:
return
temp
else
:
return
temp
-
wind
/
10
def
percieved_temp_at
(
self
,
index
)
->
float
:
return
self
.
percieve_temp
(
self
.
temp
[
index
],
self
.
wind
[
index
])
def
all_percieved_temp
(
self
)
->
list
[
float
]:
return
[
self
.
percieved_temp_at
(
index
)
for
index
in
range
(
len
(
self
.
wind
))]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment