database.py 820 B

12345678910111213141516171819202122232425262728293031323334
  1. import csv
  2. class Table:
  3. columnNames = []
  4. rows = []
  5. def __init__(self, columns):
  6. self.columnNames = columns
  7. def addRow(self, values):
  8. self.rows.append(values)
  9. def getLastRow(self):
  10. if len(self.rows) > 0:
  11. return self.rows[len(self.rows)-1]
  12. else:
  13. return [0] * len(self.columnNames)
  14. def getLastValue(self, column):
  15. col = self.columnNames.index(column)
  16. return self.getLastRow(col)
  17. def saveAsCsv(self, filename):
  18. with open(filename,"w+", newline='') as my_csv:
  19. csvWriter = csv.writer(my_csv,delimiter=',')
  20. csvWriter.writerow(self.columnNames)
  21. csvWriter.writerows(self.rows)
  22. if __name__ == "__main__":
  23. t = Table(["time", "col1", "col2"])
  24. t.addRow([time.time(), 1, 2])
  25. t.addRow([time.time(), 3, 4])
  26. print(len(t.rows))
  27. print(t.getLastRow())