#!/usr/bin/env python
import os # for the 'cls' command to clear the screen
import datetime # for date.today, timedelta and date.weekday functions
import webbrowser
import DatabaseManager as dbm
import helper_functions as h
[docs]def dateSuffix(date):
if date == 1 or date == 21 or date == 31:
string= "st "
elif date == 2 or date == 22:
string = "nd "
elif date == 3 or date == 23:
string = "rd "
else:
string = "th "
return string
[docs]def printReadings(readings, fuelString):
if fuelString == 'Electricity':
extra = "========"
elif fuelString == 'New electricity':
extra = "============"
elif fuelString == 'New gas':
extra = "===="
else:
extra = ""
print(" ")
print(fuelString + ' readings:')
print('=============' + extra)
for x in readings:
count = readings.index(x) + 1
try:
previous_entry = readings[count]
string = dateSuffix(x[0])
if x[3] > previous_entry[3]:
if (previous_entry[3]%4) == 0: # looking for leap years - fails with 2100, 2200, 2300, 2500, etc
days = int(366 + x[4] - previous_entry[4])
else:
days = int(365 + x[4] - previous_entry[4])
else:
days = x[4] - previous_entry[4]
units_used_per_week = int(7 * (x[5] - previous_entry[5])/days)
day = str(x[3]) + " Week " + str(x[1]) + ":\t" + str(x[0]) + string + str(x[2] + "\t")
print(day, "-\t", x[5], "\t", "Units used per week:\t", units_used_per_week)
except IndexError:
break
[docs]def main():
"""Main loop of programme"""
zeropi = {
'user': 'andrew',
'password': 'password123',
'host': 'zeropi',
'port': '3306',
'raise_on_warnings': True
}
h.cls()
dB = dbm.DbMan(zeropi, 'meterReadings')
while dB.status == "Connected":
print("")
print("")
print("You are looking at the " + dB.database + " database hosted on " + dB.host)
print("The latest set of readings on the " + dB.database + " database are:")
elecReadings = []
gasReadings = []
sqlQuery = "SELECT * FROM Readings ORDER BY readingID DESC LIMIT 12"
result = dB.queryTable(sqlQuery)
elecReadings = formatReading(result, 0)
gasReadings = formatReading(result, 1)
printReadings(gasReadings, "Gas")
printReadings(elecReadings, "Electricity")
print("")
weekdays = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") # Weekdays represented as a tuple dataset (round brackets)
today = datetime.date.today() # using the date.today call in datetime to get today's date in yyyy-mm-dd format
dayOfWeek = today.weekday() # using the date.weekday call in datetime to get an integer representation of the day of the week (where 0=Monday, 6=Sunday)
if dayOfWeek == 6: # or !=
print("Today is: " + str(today) + " which is a Sunday") # n.b. had to use the str() function to turn the date into a string so that it could be concatenated into a single string to be printed
readingDate = today # set readingDate to today's date
go = input("Do you want to enter readings for today? (y/n)") # Record the readings against today's date if user so wishes
else:
lastSunday = today - datetime.timedelta(days=today.weekday()) + datetime.timedelta(days=6, weeks=-1) # find last Sunday by taking today's number away from today (i.e. go back to Monday) and then adding a delta of Sunday (6) of last week (-1)
print("Today is " + str(today) + ", which is a " + weekdays[dayOfWeek] + ". Last Sunday was " + str(lastSunday) + ".")
readingDate = lastSunday
go = input("Do you want to enter readings for last Sunday? (y/n)")
if go == 'n':
print("Then you'll have to enter the readings yourself!") # if user's input was 'n' then program stops here! Need to consider error conditions, or maybe other choices
exit()
else:
print("The date recorded for these readings will be " + str(readingDate)) # if user's input was 'y' then confirm date (either today or last Sunday depending on choices above) and proceed to input readings
print("")
print("")
y = "SELECT * FROM Readings ORDER BY readingID DESC LIMIT 1"
result = dB.queryTable(sqlQuery)
gas = input("Gas reading: ")
electric = input("Electricity reading: ")
index1 = result[0][0] + 1
index2 = index1 + 1
records = [
(index1, readingDate, 0, electric),
(index2, readingDate, 1, gas)
]
sqlQuery = "INSERT INTO Readings (readingID, readingDate, readingType, readingValue) VALUES (%s, %s, %s, %s)"
dB.updateTable(sqlQuery, records)
elecReadings = []
gasReadings = []
sqlQuery = "SELECT * FROM Readings ORDER BY readingID DESC LIMIT 12"
result = dB.queryTable(sqlQuery)
elecReadings = formatReading(result, 0)
gasReadings = formatReading(result, 1)
print("")
print("")
printReadings(gasReadings, "New gas")
printReadings(elecReadings, "New electricity")
dB.close()
webbrowser.open("https://octopus.energy/dashboard/accounts/A-A4570FF5/properties/1327603/meter-readings/meter/")
if __name__ == "__main__":
main()