Commit 8fffc52e authored by paugier's avatar paugier
Browse files

clean OOP

parent 1e750f37
......@@ -57,7 +57,16 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"max temp is of 5°C at index 1 \n",
"wind speed at max temp = 0 km/h\n"
]
}
],
"source": [
"paris = [[10, 0, 20, 30, 20, 0], [1, 5, 1, -1, -1, 3]]\n",
"\n",
......@@ -100,7 +109,16 @@
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"max temp is 5°C at index 1\n",
"wind speed at max temp = 0 km/h\n"
]
}
],
"source": [
"paris = {\"wind\": [10, 0, 20, 30, 20, 0], \"temperature\": [1, 5, 1, -1, -1, 3]}\n",
"\n",
......@@ -186,13 +204,13 @@
"name": "stderr",
"output_type": "stream",
"text": [
"test_arg_max_temp (__main__.TestMyStuff0)\n",
"test_arg_max_temp (__main__.TestWeatherStationDict0)\n",
"test arg_max_temp computes correctly ... ok\n",
"test_max_temp (__main__.TestMyStuff0)\n",
"test_max_temp (__main__.TestWeatherStationDict0)\n",
"test max_temp computes correctly ... ok\n",
"\n",
"----------------------------------------------------------------------\n",
"Ran 2 tests in 0.003s\n",
"Ran 2 tests in 0.002s\n",
"\n",
"OK\n"
]
......@@ -203,7 +221,7 @@
"import unittest\n",
"\n",
"\n",
"class TestMyStuff0(unittest.TestCase):\n",
"class TestWeatherStationDict0(unittest.TestCase):\n",
" \"\"\"Test the weather station \"\"\"\n",
"\n",
" def setUp(self):\n",
......@@ -222,7 +240,7 @@
" self.assertEqual(1, arg_max_temp(self.paris))\n",
"\n",
"\n",
"suite = unittest.TestLoader().loadTestsFromTestCase(TestMyStuff0)\n",
"suite = unittest.TestLoader().loadTestsFromTestCase(TestWeatherStationDict0)\n",
"_res = unittest.TextTestRunner(verbosity=2).run(suite)"
]
},
......@@ -239,7 +257,6 @@
"- Pro:\n",
" - adding functions leads to a code that is easier to read (and therefore to debug!)\n",
" - testing functions can be done separately from the rest of the code\n",
" - the computation done on the second part depends upon the functions (*i.e* it depends on the function definitions)\n",
"- Con \n",
" - We rely on the fact that the dictionnaries have been built correctly (for example wind and temperature arrays have the same length)."
]
......@@ -309,13 +326,13 @@
"name": "stderr",
"output_type": "stream",
"text": [
"test_arg_max_temp (__main__.TestMyStuff1)\n",
"test_arg_max_temp (__main__.TestWeatherStationDict1)\n",
"test arg_max_temp computs correctly ... ok\n",
"test_build_station_with_iterable (__main__.TestMyStuff1)\n",
"test_build_station_with_iterable (__main__.TestWeatherStationDict1)\n",
"Tests that the station can be generated from iterables ... ok\n",
"test_max_temp (__main__.TestMyStuff1)\n",
"test_max_temp (__main__.TestWeatherStationDict1)\n",
"test max_temp computes conrrectly ... ok\n",
"test_wrong_build (__main__.TestMyStuff1)\n",
"test_wrong_build (__main__.TestWeatherStationDict1)\n",
"Tests that the station generation throws an error ... ok\n",
"\n",
"----------------------------------------------------------------------\n",
......@@ -328,7 +345,7 @@
"source": [
"# testing\n",
"\n",
"class TestMyStuff1(unittest.TestCase):\n",
"class TestWeatherStationDict1(unittest.TestCase):\n",
" \"\"\"Test the weather station \"\"\"\n",
"\n",
" def setUp(self):\n",
......@@ -358,7 +375,7 @@
" self.assertEqual(1, arg_max_temp(self.paris))\n",
"\n",
"\n",
"suite = unittest.TestLoader().loadTestsFromTestCase(TestMyStuff1)\n",
"suite = unittest.TestLoader().loadTestsFromTestCase(TestWeatherStationDict1)\n",
"_res = unittest.TextTestRunner(verbosity=2).run(suite)"
]
},
......@@ -373,25 +390,7 @@
"**Comments**\n",
"\n",
" - If the dedicated function `build_station` is used, the returned dictionary is well structured.\n",
" - If one changes `build_station`, only `max_temp` and `arg_max_temp` have to be changed accordingly\n",
" - We use a list comprehension to be able to have parameters wind and temp provided by any ordered iterable (e.g. see `test_build_station_with_iterable` wtih `range`)\n",
" - **BUT** if we have a new kind of station, *i.e.* that holds only wind and humidity, we want to avoid to be able to use `max_temp` with it. "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"**Improvement**\n",
" \n",
"We would like to \"embed\" the `max_temp` and the `arg_max_temp` in the \"dictionnary station\" in order to address the last point.\n",
" \n",
"And here comes the object-oriented programming ! \n",
" "
" - If one changes `build_station`, only `max_temp` and `arg_max_temp` have to be changed accordingly"
]
},
{
......@@ -513,7 +512,7 @@
"test test_max_temp function ... ok\n",
"\n",
"----------------------------------------------------------------------\n",
"Ran 5 tests in 0.012s\n",
"Ran 5 tests in 0.009s\n",
"\n",
"OK\n"
]
......@@ -578,7 +577,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 9,
"metadata": {},
"outputs": [
{
......@@ -605,7 +604,7 @@
"test that the perceived temp is the same without wind ... ok\n",
"\n",
"----------------------------------------------------------------------\n",
"Ran 9 tests in 0.013s\n",
"Ran 9 tests in 0.014s\n",
"\n",
"OK\n"
]
......@@ -620,8 +619,8 @@
" Precondition: wind and temperature must have the same length\n",
" :param wind: any ordered iterable\n",
" :param temperature: any ordered iterable\"\"\"\n",
" self.wind = [x for x in wind]\n",
" self.temp = [x for x in temperature]\n",
" self.wind = list(wind)\n",
" self.temp = list(temperature)\n",
" if len(self.wind) != len(self.temp):\n",
" raise ValueError(\n",
" \"wind and temperature should have the same size\"\n",
......@@ -789,7 +788,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
"version": "3.7.2"
}
},
"nbformat": 4,
......
......@@ -847,7 +847,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
"version": "3.7.2"
}
},
"nbformat": 4,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment