Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def add(listah, number, name):
- string=number+'#'+name
- listah.append(string)
- def lookup(listah, find):
- print 'Results found'
- for item in listah:
- if find in item:
- fix(listah[listah.index(item)])
- def fix(string):
- string="number: "+string
- string=string.replace("#", " name: ")
- string=string.replace("@", ", ")
- string=string.replace("&", " alias: ")
- print string
- def nameinlist(listah, name):
- if any(name in string for string in listah):
- return True
- else:
- return False
- def alias(data, name, aliaz):
- return data[helpstring(data, name)]+'&'+aliaz
- def helpstring(data, helpstring): #Finds index where substring is located
- for i, s in enumerate(data): #Enumerate iterates(creates tupler of all elements) the DB to allow every item to be checked
- if helpstring in s:
- return i #Returns index of interest
- return 0
- def change(data, name, number): #changes number
- string = data[helpstring(data, name)] #Find index of name
- string = string.split("#") #split string
- string = number+"#"+string[1] #keep the name and aliases, add new number to beginning
- data[helpstring(data, name)] = string #set the item in the DB to manipulated string
- def save(data, file):
- f = open(file, "wb")
- for item in data:
- f.write("%s\n" % item) #Writes every item on a new line
- f.close()
- def load(data, file):
- try:
- f = open(file, "rb")
- for line in f:
- val = line.replace("\n","") #Remove newline to retain structure
- if val not in data:
- data.append(val) #Appends every line as a new item
- f.close()
- except IOError: print("ERROR ERROR ERROR")
- data=[]
- while True:
- keno=raw_input('Telefonbok>')
- if keno=="":
- continue
- keno=keno.replace("&","")
- keno=keno.replace("@","")
- keno=keno.replace("#","")
- keno=keno.split()
- keno[0]=keno[0].lower()
- if keno[0]=='add':
- if len(keno)==3:
- if nameinlist(data, keno[1])==False:
- add(data, keno[2], keno[1])
- else:
- print 'That name already exists'
- else:
- print 'No you are doing it wrong!'
- elif keno[0]=='lookup':
- if len(keno)==2:
- lookup(data, keno[1])
- else:
- print 'No you are doing it wrong!'
- elif keno[0]=='alias':
- if len(keno)==3 and keno[0]=='alias':
- data[helpstring(data, keno[1])] = alias(data, keno[1], keno[2])
- elif keno[0]=='change' and len(keno)==3:
- try:
- val = int(keno[2]) #InputError, check if we actually get a number.
- change(data, keno[1], keno[2])
- except ValueError: #here we call error handling
- print("This is not a number")
- elif keno[0]=='save':
- save(data, keno[1])
- elif keno[0]=='load':
- load(data, keno[1])
- elif keno[0]=='quit':
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement