Advertisement
Guest User

database

a guest
Jan 27th, 2020
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. import json
  2. class Database:
  3.  
  4.     def __init__(self, filename):
  5.         self.__lista = []
  6.         self.__filename = filename
  7.  
  8.         try:
  9.             f = open(self.__filename, "r")
  10.             self.__lista = json.load(f)
  11.         except:
  12.             f = open(self.__filename, "w")
  13.             f.close()
  14.  
  15.     def add(self, c):
  16.         self.__lista.append(c)
  17.  
  18.     def save(self):
  19.         with open(self.__filename, 'w') as json_file:
  20.             json.dump(self.__lista, json_file, indent=2)
  21.  
  22. ERRORE : raise TypeError(f'Object of type {o.__class__.__name__} '
  23.         TypeError: Object of type Cocktail is not JSON serializable
  24.  
  25. (cocktail è un dizionario)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement