Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. def add(listah, number, name):
  2.     string=number+'#'+name
  3.     listah.append(string)
  4.  
  5. def lookup(listah, find):
  6.     print 'Results found'
  7.     for item in listah:
  8.         if find in item:
  9.             fix(listah[listah.index(item)])
  10.  
  11. def fix(string):
  12.     string="number: "+string
  13.     string=string.replace("#", " name: ")
  14.     string=string.replace("@", ", ")
  15.     string=string.replace("&", " alias: ")
  16.     print string
  17.  
  18. def nameinlist(listah, name):
  19.     if any(name in string for string in listah):
  20.         return True
  21.     else:
  22.         return False
  23.    
  24. def alias(data, name, aliaz):
  25.     return data[helpstring(data, name)]+'&'+aliaz
  26.  
  27. def helpstring(data, helpstring): #Finds index where substring is located
  28.     for i, s in enumerate(data): #Enumerate iterates(creates tupler of all elements) the DB to allow every item to be checked
  29.         if helpstring in s:
  30.               return i #Returns index of interest
  31.     return 0
  32.  
  33. def change(data, name, number): #changes number
  34.     string = data[helpstring(data, name)] #Find index of name
  35.     string = string.split("#") #split string
  36.     string = number+"#"+string[1] #keep the name and aliases, add new number to beginning
  37.     data[helpstring(data, name)] = string #set the item in the DB to manipulated string
  38.  
  39. def save(data, file):
  40.         f = open(file, "wb")
  41.         for item in data:
  42.                 f.write("%s\n" % item) #Writes every item on a new line
  43.         f.close()
  44.  
  45. def load(data, file):
  46.     try:
  47.         f = open(file, "rb")
  48.         for line in f:
  49.                 val = line.replace("\n","") #Remove newline to retain structure
  50.                 if val not in data:
  51.                         data.append(val) #Appends every line as a new item
  52.         f.close()
  53.     except IOError: print("ERROR ERROR ERROR")
  54. data=[]
  55.  
  56. while True:
  57.     keno=raw_input('Telefonbok>')
  58.     if keno=="":
  59.         continue
  60.     keno=keno.replace("&","")
  61.     keno=keno.replace("@","")
  62.     keno=keno.replace("#","")
  63.     keno=keno.split()
  64.     keno[0]=keno[0].lower()
  65.     if keno[0]=='add':
  66.         if len(keno)==3:
  67.  
  68.             if nameinlist(data, keno[1])==False:                            
  69.                 add(data, keno[2], keno[1])
  70.             else:
  71.                 print 'That name already exists'
  72.         else:
  73.             print 'No you are doing it wrong!'
  74.  
  75.     elif keno[0]=='lookup':
  76.         if len(keno)==2:
  77.             lookup(data, keno[1])
  78.         else:
  79.             print 'No you are doing it wrong!'
  80.  
  81.     elif keno[0]=='alias':
  82.         if len(keno)==3 and keno[0]=='alias':
  83.             data[helpstring(data, keno[1])] = alias(data, keno[1], keno[2])
  84.  
  85.     elif keno[0]=='change' and len(keno)==3:
  86.         try:
  87.             val = int(keno[2])        #InputError, check if we actually get a number.
  88.             change(data, keno[1], keno[2])
  89.         except ValueError:        #here we call error handling
  90.             print("This is not a number")
  91.  
  92.     elif keno[0]=='save':
  93.         save(data, keno[1])
  94.  
  95.     elif keno[0]=='load':
  96.         load(data, keno[1])
  97.  
  98.     elif keno[0]=='quit':
  99.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement