Skip to content

Commit 5865948

Browse files
authored
Merge pull request snychka#6 from pluralsight-projects/module1
Add module 1, task 5
2 parents d6571e4 + 1598faf commit 5865948

File tree

4 files changed

+112
-35
lines changed

4 files changed

+112
-35
lines changed

sensor/load_data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ def load_sensor_data():
1717
for row in data_reader:
1818
# Create a list of dictionaries
1919
sensor_data.append(row)
20+
2021
return sensor_data

sensor/sensor_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
data = load_sensor_data()
55
# print(f"Loaded records {len(data)}")
6-
print("Loaded records {}".format(len(data)))
6+
print("Loaded records: [{}]".format(len(data)))

tasks.md

Lines changed: 93 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
# Module 01 - The Sensor Class
1+
# Module 1 - The Sensor Class
22

3-
- [Module 01 - The Sensor Class](#module-01---the-sensor-class)
3+
- [Module 1 - The Sensor Class](#module-1---the-sensor-class)
44
- [Status](#status)
55
- [Module 1: Load Sensor Data From Files](#module-1-load-sensor-data-from-files)
6-
- [Task 1: Import os, glob, and csv](#task-1-import-os-glob-and-csv)
7-
- [Task 2: Create a Function to parse the data](#task-2-create-a-function-to-parse-the-data)
8-
- [Task 3: Set Python File Management](#task-3-set-python-file-management)
9-
- [Task 4: Read CSV Files](#task-4-read-csv-files)
6+
- [M1: Task 1: Import os, glob, and csv](#m1-task-1-import-os-glob-and-csv)
7+
- [M1: Task 2: Create a Function to parse the data](#m1-task-2-create-a-function-to-parse-the-data)
8+
- [M1: Task 3: Sensor Data File Management](#m1-task-3-sensor-data-file-management)
9+
- [M1: Task 4: Read Data Files](#m1-task-4-read-data-files)
10+
- [M1: Task 5: Get Sensor Data with sensor_app](#m1-task-5-get-sensor-data-with-sensorapp)
1011
- [Module 2: Create a Class HomeData](#module-2-create-a-class-homedata)
1112
- [Module 3: Analyze Temperature Data](#module-3-analyze-temperature-data)
1213
- [Module 4: Analyze Humidity Data](#module-4-analyze-humidity-data)
@@ -19,36 +20,111 @@ Draft.
1920

2021
## Module 1: Load Sensor Data From Files
2122

22-
### Task 1: Import os, glob, and csv
23+
### M1: Task 1: Import os, glob, and csv
2324

24-
[//]:# (@pytest.mark.test_load_data_function_module1)
25+
[//]:# (@pytest.mark.test_load_data_import_module1)
2526

2627
The dataset for this project is stored in several CSV files found in the `dataset` folder. It represents the data collected from a device with multiple sensors. The records include measurements of temperature, humidity, energy consumption, and particle count in the air over a given area. The data is collected over a period of 24 hours.
2728

2829
To start, open the file called `load_data.py` in the `sensor` folder - the rest of the tasks in this module happen in this same file.
2930

3031
At the top of the file create three import statements for `os`, `glob`, and `csv`. These libraries will allow us to work with a collection of files.
3132

32-
### Task 2: Create a Function to parse the data
33+
---
34+
To test this module locally:
35+
36+
- Open a terminal at the root of the project
37+
- Run the command `pytest -k module1`
38+
39+
### M1: Task 2: Create a Function to parse the data
3340

3441
[//]:# (@pytest.mark.test_load_data_load_sensor_func_module1)
3542

36-
Create a method called `load_sensor_data` that takes no arguments.
37-
In the body of the `load_sensor_data` function, create variable called `sensor_data` and set it as an empty `list` - the rest of the tasks in this module happen in this function.
43+
Create a function called `load_sensor_data` that takes no arguments.
44+
In the body of the `load_sensor_data` function, create variable called `sensor_data` and set it as an empty `list`.
45+
46+
---
47+
To test this module locally:
48+
49+
- Open a terminal at the root of the project
50+
- Run the command `pytest -k module1`
3851

39-
### Task 3: Set Python File Management
52+
### M1: Task 3: Sensor Data File Management
4053

4154
[//]:# (@pytest.mark.test_load_data_sensor_files_module1)
4255

43-
Below the `sensor_data` variable create another variable called `sensor_files` that is set to a call to the `glob.glob()` function.
56+
Next, create a variable called `sensor_files` that is set to a call to the `glob.glob()` function.
4457

45-
Pass `glob` function a single argument, a call to the `os.path.join()` function. In turn pass `os.path.join()` three arguments: `os.getcwd()`, `datasets`, and `*.csv`.
58+
Pass the glob function a single argument, a call to the `os.path.join()` function.
4659

47-
The `datasets` argument corresponds to the folder with your sensor data. The data files are in `csv` format. You may open them and explore the content of the records in them.
60+
In turn pass `os.path.join()` three arguments: `os.getcwd()`, `"datasets"`, and `"*.csv"`.
4861

49-
### Task 4: Read CSV Files
62+
Your statement should look like this:
5063

51-
[//]:# (@pytest.mark.test_load_data_load_sensor_func_module1)
64+
```python
65+
sensor_files = glob.glob(os.path.join(os.getcwd(), 'datasets', '*.csv'))
66+
```
67+
68+
---
69+
To test this module locally:
70+
71+
- Open a terminal at the root of the project
72+
- Run the command `pytest -k module1`
73+
74+
### M1: Task 4: Read Data Files
75+
76+
[//]:# (@pytest.mark.test_load_data_read_files_module1)
77+
78+
The `sensor_files` object contains a list of file names i.e. ['SENSOR_ROOM2', 'SENSOR_ROOM1']
79+
80+
To read the sensor data of these files, five steps are required:
81+
82+
1) Create one `for` loop that loops through `sensor_files` using `sensor_file` as the iterator variable.
83+
84+
2) In the body of this loop use a `with` statement to `open` the `sensor_file` and set the alias to `data_file`.
85+
86+
3) In the `with` body, set a variable called `data_reader` equal to `csv.DictReader()`. Pass in the current `data_file` as the first argument, and set the `delimiter=','` as the second argument. The `data_reader` will contain a list of dictionaries with the sensor data.
87+
88+
4) Create a second `for` loop to `data_file` to get access to each record. Use `row` as your iterator variable.
89+
90+
5) Inside the body of the second `for` loop, append each `row` record to the `sensor_data` list created on `Task 2`
91+
92+
Finally, your function should return `sensor_data` list containing a list of dictionaries.
93+
94+
---
95+
To test this module locally:
96+
97+
- Open a terminal at the root of the project
98+
- Run the command `pytest -k module1`
99+
100+
### M1: Task 5: Get Sensor Data with sensor_app
101+
102+
[//]:# (@pytest.mark.test_sensor_app_load_data_return_module1)
103+
oLet's set up the command line interface (CLI). Open the `sensor_app.py` file in the `sensor` directory of the project.
104+
105+
At the top, from the `load_data` module, `import` the `load_sensor_data` function.
106+
107+
Define variable called `data` and set it equal to `load_sensor_data()`.
108+
109+
Print the length of the `data` list using `formatted` print. Your output should look like this:
110+
111+
```bash
112+
Loaded records: [2000]
113+
```
114+
115+
---
116+
To test this task locally:
117+
118+
- Open a terminal at the root of the project
119+
- Run the command `python sensor/sensor_app.py`
120+
121+
---
122+
To test this module locally:
123+
124+
- Open a terminal at the root of the project
125+
- Run the command `pytest -k module1`
126+
127+
-
52128

53129
## Module 2: Create a Class HomeData
54130

tests/test_module1.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,26 @@ def test_load_data_sensor_files_module1(parse):
109109
), "Are you creating a variable called `sensor_files` and assigning it glob.glob() and passing os.path.join()? Are you passing 3 values to os.path.join()?"
110110

111111

112-
@pytest.mark.test_load_data_sensor_files_query_module1
113-
def test_load_data_sensor_files_query_module1(parse):
112+
# @pytest.mark.test_load_data_sensor_files_query_module1
113+
# def test_load_data_sensor_files_query_module1(parse):
114114

115-
# def load_sensor_data():
116-
# ....
117-
# sensor_files = glob.glob(os.path.join(os.getcwd(), 'datasets', '*.csv'))
115+
# # def load_sensor_data():
116+
# # ....
117+
# # sensor_files = glob.glob(os.path.join(os.getcwd(), 'datasets', '*.csv'))
118118

119-
load_data = parse("load_data")
120-
assert load_data.success, load_data.message
119+
# load_data = parse("load_data")
120+
# assert load_data.success, load_data.message
121121

122-
sensor_files = load_data.query(
123-
"glob.glob(os.path.join(os.getcwd(), 'datasets', '*.csv'))")
124-
sensor_files_exists = sensor_files.exists()
125-
assert (
126-
sensor_files_exists
127-
), "Are you including `glob.glob(os.path.join(os.getcwd(), 'datasets', '*.csv'))` in your code?"
122+
# sensor_files = load_data.query(
123+
# "glob.glob(os.path.join(os.getcwd(), 'datasets', '*.csv'))")
124+
# sensor_files_exists = sensor_files.exists()
125+
# assert (
126+
# sensor_files_exists
127+
# ), "Are you including `glob.glob(os.path.join(os.getcwd(), 'datasets', '*.csv'))` in your code?"
128128

129129

130-
@pytest.mark.test_load_data_for_files_module1
131-
def test_load_data_for_files_module1(parse):
130+
@pytest.mark.test_load_data_read_files_module1
131+
def test_load_data_read_files_module1(parse):
132132

133133
# def load_sensor_data():
134134
# ....
@@ -260,8 +260,8 @@ def test_load_data_for_files_module1(parse):
260260
), 'Are you returning `sensor_data` from `load_sensor_data` function?'
261261

262262

263-
@pytest.mark.test_load_data_return_module1
264-
def test_sensor_load_data_return_module1(parse):
263+
@pytest.mark.test_sensor_app_load_data_return_module1
264+
def test_sensor_app_load_data_return_module1(parse):
265265
# from load_data import load_sensor_data
266266
# data = load_sensor_data()
267267
# print("Loaded records {}".format(len(data)))

0 commit comments

Comments
 (0)