ADVANCED DATASTRUCTURES

0 comments

In this exercise the idea is to build a notebook which applies the Python data structure list as a note manipulation method when the program is executed, and saves the data in a bitwise mode with pickle. The program has the following features:
A) It is possible to add notes, with similar timestamps as earlier. 
B)It is possible to edit a note by selecting it from the list, and rewriting it with new data.
C) It is possible to delete notes separately based on the selection and 
D) Notebook loads an existing notebook file from the file “notebook.dat” if such a file exists.

When the program starts, the system should check for a notebook datafile “notebook.dat” and load it with pickle. If no such file exists, or there was a problem with the file, a new file is created and the user is notified “No default notebook was found, created one.”. After this the basic main menu works as in the earlier notebook:

code:

import time  

import pickle 

mylist=[]

def check(z):

 try:

  s1 = open(“notebook.dat”,”rb”)

  contents=pickle.load(z)

  for i in contents:

   mylist.append(i)  

  s1.close()

  #s1.close()

 except IOError:

  print(“No default notebook was found, created one.”)

  s1 = open(“notebook.dat”,”wb”)

  s1.close()

   

a= True

nm=”notebook.dat”

check(nm)

s1 = open(nm,”rb”)

s1.close()

while a:

# print(“Now using file “,nm)

 print(“(1) Read the notebookn(2) Add noten(3) Edit a note n(4) Delete a note n(5) Save and quit”)

 ch=int(input(“Please select one: “))

 if ch==1:

  for i in mylist:

   print(i)

   

 elif ch==2:

   s2=input(“Write a new note: “)

   s2+=”:::”

   s2+=time.strftime(“%X %x”)

   mylist.append(s2)

   

 elif ch==3:

   print(“The list has “,len(mylist),” notes.”)

   chh=int(input(“Which of them will be changed?: “))

   print(mylist[chh])

   s22=input(“Give the new note: “)

   s22+=”:::”

   s22+=time.strftime(“%X %x”)

   mylist[chh]=s22

 elif ch==4:

   

   print(“The list has “,len(mylist),” notes.”)

   chh=int(input(“Which of them will be deleted?: “))

   print(“Deleted note “,mylist[chh]) 

   mylist.pop(chh)

   

 elif ch==5:

  # print(mylist)

   a=False

   s1 = open(“notebook.dat”,”wb”)

   #mylist.append(“n”)

   pickle.dump(mylist,s1)

   s1.close()

   print(“Notebook shutting down, thank you.”)

   

   

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}