Test Driven Development using Pandas
- katiebrown0729
- Sep 23, 2018
- 3 min read
How to Resume a Blog After 6 Months?
Programming Church is alive and well; however, my motivation to keep up a blog with it has not. I believed I needed a very technical, in-depth look at what was done so that others could follow along. My technique has changed to trying out a more high level look at the code, this way not only is there less work to do, but a larger audience would be able to understand.
In the 6 months, many things have happened.
I’ve started working at my dream job, as a legitimate Rocket Scientist. I am even allowed to sign off my emails with that title. Hence, blog title had to be changed to exclude “unemployed mechanical engineer.”
Programming Church includes Shane regularly, and our latest addition has been Brucester. He used to work on computers back in the day, so he is working on catching up to the progress we’ve made since we started in January.
I’ve adopted 2 additional dogs. Now I feel too guilty to only bring sweet Potato. She is dearly missed. Now, my husband is my trusty programming sidekick. He will have to do.
We are actually writing code and learning different things. We’re working up to being good enough to compete in a hackathon. We will still for sure lose, but as with all new skills, practice is key. More to come (maybe) on the technical details of the last 6 months.
September 23rd, 2018
Mentor John was out of town this week, so the venue had been relocated to Arvada public library. We had an additional guest to observe, who I liked to call David #2.
The Assignment
Create a new module in your Flask-Pi-IOT project for storing sensor data.
Using test driven development (TDD) methods, create a new object to store the sensor data using a Pandas dataframe.
This object should expose high-level domain methods to the outside world as part of its API rather than exposing Pandas constructs. Good examples include:
Add a single reading
List all readings
Get reading for a given serial number
Get readings for a given date and/or time range
A great question to ask yourself is: how will we render the list of readings as html, and how will we expose graphs? While Pandas provides a method to do so, determine how do you expose that in your API that other coders would use.
Hook up your new module to the main Flask-Pi-IOT project.
The Solution
The first thing to note is this is the first major assignment that Professor Funk has provided us without a solution. This reminds me of the homework in engineering school, where I’d struggle for hours on end, both alone and with a team, yet we only wind up with something that is partially correct. This assignment was given at our August 26th session, and we are still working towards a solution This is a very good way to learn, but I must admit I am a little disappointed in myself for not being better at this point.
Definitions
DataFrame: 2-dimensional labeled data structure with columns of potentially different types, like an array or spreadsheet. This is the primary object time in Pandas.
Professor Funk: also known as Mentor John, John Funk, the Funkmeister, etc.
Pandas: Python package for data science. This essentially allows the programmer to use Python as if it were MATLAB, which has a vast library of data analytics tools
Links to Code
This session we worked on the test_get_all_data_as_list method. For some reason, this one is particularly challenging, which is evident by the fact that we worked on this last week as well. The 5 of us worked together on the same version of the code.

The first seven lines of code look similar to the code previously written. Self is passed through the method to indicate that it is a function within the class. The print statement allows us to see what we are actively testing. Our StoredReadings variable is instantiated as aSR. A simple for loop generates a set of 3 random numbers for our positional coordinates. The code works up to this point.

Line 79 shows aSR.get_all_data_as_list() is instantiated as adal for ease of typing. The type of adal is confirmed to be a list prior to testing on line 83 using an assertTrue statement. These work with the unittest to pass if the condition is in fact true, and fail if it is false. To test the function get_all_data_as_list(), we chose to assert the length of adal is equal to the number of readings. After several hours and many arguments, we finally got all 5 tests to pass.
Comments