Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.48 KB | None | 0 0
  1. import json
  2. import pprint
  3.  
  4. def readfile(filename):
  5.     """
  6.    Returns the content of a file
  7.    """
  8.     handle = open(filename, 'r')
  9.     data = handle.read()
  10.     handle.close()
  11.     return data
  12.  
  13.  
  14. def printheader(text):
  15.     """
  16.    Format a header depending on the given text
  17.    """
  18.     # 79 times '-'
  19.     print("-" * 79)
  20.  
  21.     print(text)
  22.  
  23.     # same as above
  24.     print("-" * 79)
  25.  
  26.  
  27. def jsonfiletodict(filename):
  28.     """
  29.    Returns a dictionary of a JSON file
  30.    """
  31.     plaintext = readfile(filename)
  32.     return json.loads(plaintext)
  33.  
  34.  
  35. def createcourseslist(data):
  36.     """
  37.    Returns a list (courses, important)
  38.    """
  39.     course_id = 1
  40.     courses = []
  41.     for course in data:
  42.         if not (course["title"], 0) in courses:
  43.             courses.append((course["title"], False))
  44.             course_id += 1
  45.     return courses
  46.  
  47.  
  48. def viewcourseslist(courses):
  49.     """
  50.    Prints courses list
  51.    """
  52.     printheader("Courses list")
  53.     for i in range(len(courses)):
  54.         (name, important) = courses[i]
  55.         print(i + 1, ": ", name, end="")
  56.         if important:
  57.             print("    [Important]")
  58.         else:
  59.             print("    [Not important]")
  60.  
  61.  
  62. def tagasimportant(courses):
  63.     """
  64.    Tags a title as important
  65.    """
  66.     course_id = int(getinput("Please enter a course id: "))
  67.     (name, important) = courses[course_id - 1]
  68.     courses[course_id - 1] = (name, True)
  69.  
  70.  
  71. def untagasimportant(courses):
  72.     """
  73.    Untags a title as important
  74.    """
  75.     course_id = int(getinput("Please enter a course id: "))
  76.     (name, important) = courses[course_id - 1]
  77.     courses[course_id - 1] = (name, False)
  78.  
  79.  
  80. def exportcourses(data, filename):
  81.     """
  82.    Exports the data into a file
  83.    """
  84.     filename = getinput("Please enter a filename: ")
  85.     handle = open(filename, 'w')
  86.     handle.write(data)
  87.     handle.close()
  88.     print("File exported with success !")
  89.  
  90.  
  91. def savequery(search, courses, filename):
  92.     """
  93.    Save courses
  94.    """
  95.     handle = open(filename, 'w')
  96.     handle.write(search + ":\n")
  97.     for course in courses:
  98.         handle.write("    " + course + '\n')
  99.     handle.close()
  100.  
  101.  
  102. def querybycategory(data):
  103.     category = getinput("Please enter a category: ")
  104.     filename = getinput("Please enter a filename: ")
  105.     courses = []
  106.     for course in data:
  107.         if course["category"] == category and course["title"] != "":
  108.             courses.append(course["title"])
  109.     savequery(category, courses, filename)
  110.     return courses
  111.  
  112.  
  113. def querybysubject(data):
  114.     subject = getinput("Please enter a subject: ")
  115.     filename = getinput("Please enter a filename: ")
  116.     courses = []
  117.     for i in range(len(data)):
  118.         course = data[i]
  119.         if course["subject"] == subject and course["title"] != "":
  120.             courses.append(course["title"])
  121.     savequery(subject, courses, filename)
  122.     return courses
  123.  
  124.  
  125. def savequerydate(date_min, date_max, courses, filename):  
  126.     handle = open(filename, 'w')
  127.     handle.write("Courses between " + str(date_min) + " and " + str(date_max) + '\n')
  128.     for (course, year) in courses:
  129.         handle.write(str(year) + ": " + course + '\n')
  130.     handle.close()
  131.  
  132.  
  133. def querybydate(data):
  134.     date_min = int(getinput("Please enter the first year: "))
  135.     date_max = int(getinput("Please enter the second year: "))
  136.     filename = getinput("Please enter a filename: ")
  137.     courses = []
  138.     for course in data:
  139.         year = int(course["date"])
  140.         if year >= date_min and year <= date_max and course["title"] != "":
  141.             courses.append((course["title"], course["date"]))
  142.     savequerydate(date_min, date_max, courses, filename)
  143.     return courses
  144.  
  145.  
  146. def getinput(text):
  147.     print(text, end="")
  148.     return input()
  149.  
  150.  
  151. def changecourse(data):
  152.     """
  153.    Changes subject and category of a course
  154.    """
  155.     title = getinput("Please enter a title: ")
  156.     new_subject = getinput("Please enter a new subject for the course: ")
  157.     new_category = getinput("Please enter a new category for the course: ")
  158.     i = 0
  159.     while i < len(data):
  160.         course = data[i]
  161.         if course["title"] == title:
  162.             break
  163.         i += 1
  164.  
  165.     data[i]["category"] = new_category
  166.     data[i]["new_subject"] = new_subject
  167.     return data
  168.  
  169.  
  170. def printchoices():
  171.     print("Please enter a number to select an option.")
  172.     print("1: View courses list")
  173.     print("2: Tag a course as important")
  174.     print("3: Untag a course as important")
  175.     print("4: Query courses by a category")
  176.     print("5: Query courses by a subject")
  177.     print("6: Query courses by a date range")
  178.     print("7: Change a course")
  179.     print("8: Export courses")
  180.     print("9: Leave the app")
  181.  
  182.  
  183. def main():
  184.     data = jsonfiletodict("metadata.json")
  185.     courses = createcourseslist(data)
  186.     while True:
  187.         printheader("Welcome on the app !")
  188.         printchoices()
  189.         choice = int(input())
  190.         if choice == 1:
  191.             viewcourseslist(courses)
  192.         elif choice == 2:
  193.             tagasimportant(courses)
  194.         elif choice == 3:
  195.             untagasimportant(courses)
  196.         elif choice == 4:
  197.             querybycategory(data)
  198.         elif choice == 5:
  199.             querybysubject(data)
  200.         elif choice == 6:
  201.             querybydate(data)
  202.         elif choice == 7:
  203.             data = changecourse(data)
  204.         elif choice == 8:
  205.             exportcourses(data)
  206.         elif choice == 9:
  207.             break
  208.  
  209.  
  210. if __name__ == "__main__":
  211.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement