I am writing a Python program that maintains a list of contacts, each having 3 fields:
- Name
- Phone Number
Contacts need to be saved in a YAML structured file and the program is supposed to provide the facility of adding new contacts.
My Code for this is:
class contacts:
def add_contact(self,file,contact):
if not os.path.exists(file):
#Creating for the first time
temp = []
temp.append(contact)
with open(file, "w") as file_desc:
yaml.dump(temp, file_desc, default_flow_style=False)
file_desc.close()
else:
#Second onwards
with open(file, "r") as file_desc:
loaded = yaml.safe_load(file_desc)
loaded.append(contact)
with open(file, "w") as file_desc2:
yaml.dump(loaded, file_desc2, default_flow_style=False)
file_desc2.close()
file_desc.close()
if __name__ == "__main__":
data1 = {'name' :'Abcd', 'phone': 1234, 'email': 'abcd@gmail.com'}
data2 = {'name': 'efgh', 'phone': 5678, 'email': 'efgh@gmail.com'}
contact = contacts()
contact.add_contact("contacts.yaml", data1)
contact.add_contact("contacts.yaml",data2)
I think this is an inefficient implementation. If we have 1 million contacts, and we want to add a new one, this will first read all of them, append one to the list and write all the 1 Million + 1 contacts again. Is there a way to just add the new contacts without having to write the whole file again. I guess reading is important as I don't want to store duplicate contacts and that would need comparison. Any other efficient approach would also be appreciated.