Estou tentando reproduzir o código com o DAO, porém está aparecendo esse erro quando chamo a classe, e não sei o que estou errando, alguém poderia me ajudar?
Abaixo está a implementação da classe, o main, e o TypeError
Classe:
from abc import ABCMeta, abstractmethod
import csv, pickle, json
from contact import Contact
class ContactDao(metaclass=ABCMeta):
@abstractmethod
def search_all(self, path):
pass
@abstractmethod
def save(self, contacts, path):
pass
class ContactDaoJson(ContactDao):
@abstractmethod
def search_all(self, path):
contacts = []
with open(path, mode='r') as file:
contacts_json = json.load(file)
for contact in contacts_json:
c = Contact(id=contact["id"], name=contact["name"], email=contact["email"])
contact.append(c)
return contacts
@abstractmethod
def save(self, contacts, path):
with open(path, mode='w') as file:
json.dump(contacts, file, default=lambda object: object.__dict__)
class ContactDaoPickle(ContactDao):
@abstractmethod
def search_all(self, path):
with open(path, mode='rb') as file:
contacts = pickle.load(file)
return contacts
@abstractmethod
def save(self, contacts, path):
with open(path, mode='w') as file:
pickle.dump(contacts, file)
class ContactDaoCsv(ContactDao):
@abstractmethod
def search_all(self, path):
contacts = []
with open(path, encoding= 'latim_1') as file:
reader = csv.reader(file)
for line in reader:
id = line[0]
name = line[1]
email = line[2]
c = Contact(id, name, email)
contacts.append(c)
return contacts
@abstractmethod
def save(self, contacts, path):
pass
main:
from useful_contacts import *
try:
contacts = ContactDaoCsv.search_all('font_files/contacts.csv')
ContactDaoPickle.save(contacts, 'font_files/contacts.pickle')
contacts = ContactDaoPickle.search_all('font_files/contacts.pickle')
# ContactDaoJson.save(contacts, 'font_files/contacts.json')
# contacts = ContactDaoJson.search_all('font_files/contacts.json')
for contact in contacts:
print(f'{contact.id} - {contact.name} - {contact.email}')
except FileNotFoundError:
print("file not found")
except PermissionError:
print("no write permission")
TypeError: