Created
February 27, 2022 02:18
-
-
Save makvoid/59388bd4537297e7dc658ae22bc53fb7 to your computer and use it in GitHub Desktop.
AHT20 save_entry method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Save climate information locally | |
def save_entry(location, temperature, humidity): | |
# Ensure the file exists before attempting to read | |
if not os.path.isfile('entries.json'): | |
pathlib.Path('entries.json').touch(exist_ok=False) | |
entries = [] | |
else: | |
# Load any old entries | |
with open("entries.json", "r") as f: | |
try: | |
entries = json.loads(f.read()) | |
except Exception as e: | |
print('Error: Parsing entries.json failed') | |
raise e | |
# Add this new entry to the list | |
entries.append({ | |
'location': location, | |
'temperature': temperature, | |
'humidity': humidity, | |
'date': datetime.datetime.now().isoformat() | |
}) | |
# Save the list | |
with open("entries.json", "w") as f: | |
try: | |
f.write(json.dumps(entries, indent=2)) | |
except Exception as e: | |
print('Error: Saving entries.json failed') | |
raise e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment