Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.31 KB | None | 0 0
  1. # Project 1
  2. #
  3. # Sample data file
  4. # ----
  5. # Name: Carol
  6. # Gender: F
  7. # Country: USA
  8. # Acceptable_country: Singapore, China
  9. # Age: 23
  10. # Acceptable_age_range: 23-50
  11. # Likes: Chicken rice,hotpot, Carrot cake, chilli crab, roses, movies
  12. # Dislikes: football, hunting, swimming
  13. #
  14. # Books:
  15. # Christian reflections
  16. # The red tent
  17. # The God who is there
  18. # God in the Dock: Essays on Theology and Ethics
  19. # Total truth: liberating christianity from its cultural captivity
  20. # Redeeming love
  21. # ----
  22. # Keys:
  23. # 1. Name
  24. # 2. Gender
  25. # 3. Country
  26. # 4. Acceptable_country
  27. # 5. Age
  28. # 6. Acceptable_age_range
  29. # 7. Likes
  30. # 8. Dislikes
  31. # 9. Books
  32. # ----
  33. # how the dict should look like (please verify)
  34. # *not complete information
  35. # studentprofiles = {
  36. #       "student1" : {
  37. #                     'name' : 'Angela White',
  38. #                     'gender' : 'F'
  39. #                     'country' : ['Singapore', 'China', 'USA'],
  40. #                     'age' : 22,
  41. #                     'books' : ['xxx111', 'yyy222', 'zzz333']
  42. #                     },
  43. #       "student2" : {
  44. #                     'name' : 'Bobby Brown',
  45. #                     'gender' : 'M'
  46. #                     'country' : ['Singapore', 'China'],
  47. #                     'age' : 24,
  48. #                     'books' : ['Fat cat', 'Zack and Cody', 'Journey to the West']
  49. #                     }
  50. # }
  51. # Regex
  52. # This regex will sort each key pair value Country
  53. # Odd Number is Key, Even Number is Value
  54. # Use regex alr, sitll need to split the commas out
  55. # (Name:)(.*)\n+(Gender:)(.*)\n+(Country:)(.*)\n+(Acceptable_country:)(.*)\n+(Age:)(.*)\n+(Acceptable_age_range:)(.*)\n+(Likes:)(.*)\n+(Dislikes:)(.*)\n+(Books:)((?<=Books:)\D+)
  56.  
  57.  
  58. import os
  59. from os.path import isfile, join
  60. import sys
  61. import re
  62. # import subprocess as sp
  63.  
  64. def exitProg():
  65.     print('Thank you! Byebye!')
  66.     print('================================')
  67.     exitBit = 1
  68.     return(exitBit)
  69.  
  70. def welcomeMsg():
  71.     print('Welcome to the program!')
  72.     print('Press 1 for a list of the files in the PWD')
  73.     print('Press 2 to view the student profiles')
  74.     print('Press 3 to add a new student profile')
  75.     print('Press 4 to delete a students profile')
  76.     print('Press 5 to xxx')
  77.     print('Press 10 to exit')
  78.  
  79. def start():
  80.     welcomeMsg()
  81.     try:
  82.         menuSelection = int(raw_input('Your selection? => '))
  83.         if int(menuSelection) == 1:         # maybe can allow user to input the dir also or use the default './SampleData'
  84.             fileCounter = 1
  85.             parent_dir = './SampleData'
  86.  
  87.             print('\n' * 100)
  88.             print('================================')
  89.             print('----------Start of Dir----------')
  90.             data_file = [file for file in os.listdir('./SampleData/') if file.endswith('.txt')]
  91.             for i in sorted(data_file):
  92.                 print("Data File %s => \"%s\"" % (fileCounter, i))
  93.                 fileCounter += 1
  94.             print('-----------End of Dir-----------')
  95.             print('================================')
  96.             exitProg()
  97.         elif int(menuSelection) == 2:
  98.             counter = 1
  99.             parent_dir = './SampleData/'
  100.             print('================================')
  101.             data_file = [file for file in os.listdir('./SampleData/') if file.endswith('.txt')]
  102.             for i in sorted(data_file):
  103.                 print('-------------Student %d-------------' %counter)
  104.                 toread = parent_dir+i
  105.                 with open(toread) as f:
  106.                     for line in f:
  107.                         print line.rstrip('\n\r')
  108.                 print('---------end of Student %d----------' %counter)
  109.                 counter += 1
  110.             print('================================')
  111.             exitProg()
  112.  
  113.  
  114.         elif int(menuSelection) == 3:
  115.             print('\n' * 100)
  116.             print('================================')
  117.             print('Nothing here yet!')
  118.             print('We\'ll let you choose again!')
  119.             print('================================')
  120.             start()
  121.         elif int(menuSelection) == 4:
  122.             print('\n' * 100)
  123.             print('================================')
  124.             print('Nothing here yet!')
  125.             print('We\'ll let you choose again!')
  126.             print('================================')
  127.             start()
  128.         elif int(menuSelection) == 5:
  129.             print('\n' * 100)
  130.             print('================================')
  131.             print('Nothing here yet!')
  132.             print('We\'ll let you choose again!')
  133.             print('================================')
  134.             start()
  135.         elif int(menuSelection) == 10:
  136.             print('================================')
  137.             exitProg()
  138.         else:
  139.             print('\n' * 100)
  140.             print('================================')
  141.             print('Invalid selection.')
  142.             print('Please choose again!')
  143.             print('================================')
  144.             start()
  145.     except:
  146.         print('\n' * 100)
  147.         print('================================')
  148.         print('Something went wrong!')
  149.         print('Please choose again!')
  150.         print('================================')
  151.         start()
  152.  
  153.  
  154. print(sys.version)
  155. print('================================')
  156. exitBit = 0
  157. if exitBit > 0:
  158.     sys.exit()
  159. else:
  160.     start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement