Python basics
English translation
Learning objectives
Sensor records include missing and boundary values. Try handling normal and exceptional data with the same function.
Represent sensor records with data structures
Starting conditions
The record is [(0,21.0),(1,21.5),(2,None),(3,22.0),(4,20.5)], where each pair is time [s] and temperature [°C].
Procedure
- Convert each list item to a dictionary with time and temperature.
- Calculate the count, mean, minimum, and maximum of samples excluding None.
- Write a summarize function that leaves the original input unchanged.
Results to retain
A 5-row record table, 4 summary statistics, and function code.
summarize(rows)input: a list of dictionaries withtimeandtemperature.Variable to summarize:
temperature.Returned fields: valid sample count, mean, minimum, and maximum. Keep the same fields.
records=[(0,21.),(1,21.5),(2,None),(3,22.),(4,20.5)]
valid=[value for _,value in records if value is not None]
print('valid count',len(valid),'mean',sum(valid)/len(valid))
# TODO: Add dict conversion and the summarize function.
Record results and self-check
t [s]:
Temperature [°C]:
Validity:
Points to check
- Did you avoid interpreting None as 0°C?
- Is the original list unchanged after the function call?
Boundary values and empty input
Starting conditions
Convert each of the three inputs in cases in the code to the same list of dictionaries as in Activity 1, then pass them to summarize. Leave the empty list unchanged, and map the first value of each pair to time and the second to temperature.
Procedure
- Define the behavior to return None for the mean when there are 0 valid samples.
- Check that the returned fields are the same for one sample and multiple samples.
- Use asserts to check empty input, input with missing values, and single-sample input separately.
Results to retain
- A 3-row boundary value table and 3 passing asserts.
cases=[[],[(0,None)],[(0,25.)]]
# TODO: Add summarize results and assert checks.
Record results and self-check
Input type:
Valid count:
Mean:
Reason for handling:
Points to check
- Does it avoid division by 0 for 0 samples?
- Do the returned fields stay the same regardless of input size?
Organize repeated measurements in a function
Starting conditions
Compare the two devices and B=[19,None,20,21]. The temperature unit is the same as in the original data.
Procedure
- Convert each device's temperature list to a list of dictionaries with
timeandtemperature, and pass it to the samesummarizefunction. Since these data have no times, usetime=Noneand summarize only the temperatures. - Record each device's valid sample count, mean, and range in one table.
- Briefly summarize what to check when comparing data with different sample counts and missing-value patterns.
Results to retain
- A device comparison table and a brief interpretation.
devices={'A':[21,22,23],'B':[19,None,20,21]}
# TODO: Use the same function to summarize each device.
Record results and self-check
Device:
Valid count:
Mean [°C]:
Minimum and maximum [°C]:
Missing count:
Points to check
- Did you reuse a function instead of copying the same calculation code and modifying it separately?
- Did you distinguish device differences from differences in sample composition?