Python basics · Learning
English translation
Topics covered
Sensor records include missing and boundary values. Try handling normal and exceptional data with the same function.
Overall workflow
Sensor and request → format and unit checks → storage and state changes → query and response checks
Meaning of one measurement
A device ID, measurement time, value, and unit are needed to interpret sensor numbers. A dictionary names the fields, and a list holds multiple measurements. None indicates a failed observation, distinct from an actual value of 0.
Define processing rules in a function
Handling normal values, missing values, and empty data separately with ad hoc fixes can lead to inconsistent results. Combine input validation and summary calculations in a small function, and check two or three boundary cases.
Worked example
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].
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))
valid count 4 mean 21.25
Reference example
View more examples