Aug-15-2017, 04:25 PM
Hi! I'm rather new to programming in Python and trying to figure out how to store my sensor data from my RPi in a database. For example I have temperature sensors and humidity sensors. And I would like to store the readings with one decimal. To save space I'm storing these in the database as integer instead of float by adding the value by 10 and convert/round it to an int.
1. What is the proper name for this procedure? Must be a name out there for doing this.
2. What's the best pythonic way to do this conversion? Is there a built in feature for this?
Currently I'm doing it like this. But this is sooooooooo ugly! Please help! :)
1. What is the proper name for this procedure? Must be a name out there for doing this.
2. What's the best pythonic way to do this conversion? Is there a built in feature for this?
Currently I'm doing it like this. But this is sooooooooo ugly! Please help! :)
def to_db(self, value): ''' Rounds value to 1 decimal and multiplies by 10, returns integer.''' return int((round(value, 1) * 10)) def from_db(self, value): ''' Converts db data from integer to float by dividing by 10.''' return (value / 10)
''' Storing to database ''' reading = DHT22( time=datetime.datetime.now(), temperature=self.to_db(self.dht22_internal.temperature), humidity=self.to_db(self.dht22_internal.humidity)) db_session.add(reading)