- Decoding Data App
- Status
- Module 2: The HomeData Class
- Local Verification Instructions
- M2: Task 1: Create a Class
- M2: Task 2: Create Method to Select Data by Area
- M2: Task 3: Filter Data Records by Area
- M2: Task 4: Create Method to Select Data by Date
- M2: Task 5: Filter Data Records by Date
- M2: Task 6: Get HouseInfo Data by Area with sensor_app
- M2: Task 7: Get HouseInfo Data by Date with sensor_app
Draft.
To test this module locally:
- Open a terminal at the root of the project
- Run the command
pytest -k module2
In this module, you will create a HouseInfo class that will help us process the sensor data records. This class will later serve as base class for other classes.
To start, open the file called house_info.py in the sensor folder.
Create a class called HouseInfo. Next, create a HouseInfo class constructor with two parameters, self and data.
In the body of the constructor, assign the data input parameter to a class attribute with the same name. Hint: class attributes are prefixed with self.
Still in the HouseInfo class, create a method called get_data_by_area() with three parameters, self, field, and rec_area. The rec_area parameter should have a default value of 0, which translates to all records. The purpose of this method is to filter the data using rec_area as the key, and field as the value.
Note: The valid field values are the column names of the data files in the datasets folder.
In the body of the get_data_by_area() method, create a variable called field_data and set it to an empty list.
On a new line in the get_data_by_area method, create a for loop to iterate over self.data. Use record as your iterator variable.
Note: The self.data class variable is a list of dictionaries. The dictionary keys are equal to the columns names in the data files. e.g. when the field input parameter is set to "id" then, the record[field] value corresponds to the "id" column values. In this method, the rec_area variable maps to 'area' column values.
In the body of the for loop, select records according to the following control structures:
-
Create an if statement that is true when
rec_areais equal to 0- In the body of the
if, append to therecord[field]values to thefield_datalist.
- In the body of the
-
Create an else if statement that is true when
rec_areais equal torecord['area']value. In order to compare therec_areainteger object to therecord['area']string object, the later needs to be converted to an integer using the int() constructor.- In the body of the
elif, append to therecord[field]values to thefield_datalist.
- In the body of the
Finally, your method should return field_data (outside of the for loop, and the very end of the method).
Still in the HouseInfo class, create another method called get_data_by_date() with three parameters, self, field, and rec_date. The rec_date parameter should have a default value to today's day. You can accomplish this using the date module.
At the top of the file, import date and datetime from the datetime module. See date information and datetime information
In the body of the get_data_by_date method, create a variable called field_data and set it to an empty list.
On a new line in the get_data_by_date method, create a for loop to iterate over self.data. Use record as your iterator variable.
Note: In this method, the rec_date input parameter maps to 'date' column values.
Now, in the body of the for loop, create an if statement that's true when the current record's date is equal to rec_date. In order to compare record['date'] string object to rec_date which is date object, convert rec_date to string using the strftime method method. The strftime() should take date format "%m/%d/%y", which is the format of your data.
In the body of the if statement, append to field_data the dictionary values whose record key is equal to field input parameter.
Finally, your method should return field_data (outside of the for loop, and the very end of the method).
Open the sensor_app.py file in the sensor directory. At the top of the file, from the house_info module, import the HouseInfo.
At the bottom of the file, create an instance of the HouseInfo class, and assign it to a variable named house_info. Pass the HouseInfo constructor data as its argument.
Next, create a variable called recs and set it to the get_data_by_area() method of the house_info object. The get_data_by_area() method should take "id" as the first argument, and rec_area=1 as the second argument.
Print the length of the recs list using the formatted string:
print("\nHouse sensor records for area 1 = {}".format(len(recs)))To preview your app, open a terminal at the root of the project and run the following command:
python sensor/sensor_app.pySample output including previous tasks:
Sensor Data App
Loaded records: 2000
House sensor records for area 1 = 1000FYI: the app will not validate your print() statements.
Still in the sensor_app file, at the top of the file, from the datetime module, import the date and datetime.
At the bottom of the file, create a variable called test_date and set it to the call of datetime.strptime() which takes "5/9/20" as the first argument, and "%m/%d/%y" as the second argument.
Next, set the recs variable to the return value of get_data_by_date() method of the house_info object. The method takes "id" as the first argument, and rec_date=test_date as the second argument.
Print the test_date using the strftime() passing "%m/%d/%y" as the input parameter, and the length of the recs list using the formatted string:
print("House sensor records for date: {} = {}".format(test_date.strftime("%m/%d/%y"), len(recs)))To preview your app, open a terminal at the root of the project and run the following command:
python sensor/sensor_app.pySample output including previous tasks:
Sensor Data App
Loaded records: 2000
House sensor records for area 1 = 1000
House sensor records for date: 05/09/20 = 20FYI: the app will not validate your print() statements.