Advertisement
Guest User

Fixed Code

a guest
Sep 29th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.40 KB | None | 0 0
  1. # /usr/bin/python3.5
  2. import datetime
  3. import os
  4. import shutil
  5.  
  6. now = datetime.datetime.now()
  7.  
  8. script_name = "Something v1.0"
  9. length = 35  # length of "-"
  10.  
  11. dir_path = os.path.dirname(os.path.realpath(__file__))
  12. new_dir_name = "/data"
  13. new_dir_path = dir_path + new_dir_name
  14.  
  15. name_file = "/name.txt"
  16. age_file = "/year_born.txt"
  17. about_file = "/about.txt"
  18.  
  19. var_name = "Name"
  20. var_age = "Age"
  21. var_about = "About"
  22. var_year_born = "Year Born"
  23. header_of_data = "DATA"
  24.  
  25. help_bar_text = "\n(1)\tShow Data\n(2)\tEdit items in Data" \
  26.                 "\n(3)\tEdit About in item\n(4)\tDel all Data\n(end)\texit"
  27.  
  28. # Windows="cls", osX="clear"
  29. clear_term = ("cls" if os.name == "nt" else "clear")
  30.  
  31.  
  32. class Data:
  33.     name_of_person = ""
  34.     age_of_person = 0
  35.     about_person = ""
  36.  
  37.     def __init__(self, name, year_born, about):
  38.         self.name_of_person = name
  39.         self.about_person = about
  40.  
  41.         if year_born is None:
  42.             self.age_of_person = ""
  43.         elif year_born < 0:
  44.             self.age_of_person = ""
  45.         elif year_born > now.year:
  46.             self.age_of_person = ""
  47.         else:
  48.             self.age_of_person = now.year - year_born
  49.  
  50.     def get_name(self):
  51.         return self.name_of_person
  52.  
  53.     def get_age(self):
  54.         return self.age_of_person
  55.  
  56.     def get_about(self):
  57.         return self.about_person
  58.  
  59.     def get_data(self):
  60.         return_str = "\n" + var_name + ":\t {}"
  61.         return_str += "\n" + var_age + ":\t {}"
  62.         return_str += "\n" + var_about + ":\n {}"
  63.         return return_str.format(self.name_of_person,
  64.                                  self.age_of_person,
  65.                                  self.about_person)
  66.  
  67.  
  68. # a_j == a-j
  69. def create_txt(a_j, name, year_born):
  70.     _name = open(_dir(a_j) + name_file, "w")
  71.     _name.write(name)
  72.     _name.close()
  73.  
  74.     _age = open(_dir(a_j) + age_file, "w")
  75.     _age.write(year_born)
  76.     _age.close()
  77.  
  78.  
  79. # a_j == a-j
  80. def read_txt(a_j, n_or_y):
  81.     if n_or_y == "n":
  82.         _name = open(_dir(a_j) + name_file, "r")
  83.         output = _name.read()
  84.         _name.close()
  85.         return output
  86.     elif n_or_y == "y":
  87.         _age = open(_dir(a_j) + age_file, "r")
  88.         output = _age.read()
  89.         _age.close()
  90.         if output == "":
  91.             return None
  92.         else:
  93.             return int(output)
  94.  
  95.  
  96. # a_j == a-j
  97. def write_story(a_j, story):
  98.     _story = open(_dir(a_j) + about_file, "w")
  99.     _story.write(story)
  100.     _story.close()
  101.  
  102.  
  103. # a_j == a-j
  104. def read_story(a_j):
  105.     _story = open(_dir(a_j) + about_file, "r")
  106.     output = _story.read()
  107.     _story.close()
  108.     return output
  109.  
  110.  
  111. def del_about(a_j):
  112.     write_story(a_j, "")
  113.  
  114.  
  115. def check_if_dir_exist():
  116.     if not os.path.exists(new_dir_path):
  117.         os.makedirs(new_dir_path)
  118.         for directory in lists("dir"):
  119.             os.makedirs(dir(directory))
  120.         return True
  121.     else:
  122.         return True
  123.  
  124.  
  125. # a_j == a-j
  126. def _dir(a_j):
  127.     return new_dir_path + "/" + a_j
  128.  
  129.  
  130. def read_name(a_j):
  131.     output = []
  132.     for char in a_j:
  133.         number = ord(char) - 97
  134.         output.append(number)
  135.     return lists("name")[output[0]]
  136.  
  137.  
  138. # a_j == a-j
  139. def read_data(a_j):
  140.     _data = Data(read_txt(a_j, "n"), read_txt(a_j, "y"), read_story(a_j))
  141.     return _data
  142.  
  143.  
  144. # noinspection PyGlobalUndefined
  145. def update_data():
  146.     global a, b, c, d, e, f, g, h, i, j
  147.  
  148.     a = read_data("a")
  149.     b = read_data("b")
  150.     c = read_data("c")
  151.     d = read_data("d")
  152.     e = read_data("e")
  153.     f = read_data("f")
  154.     g = read_data("g")
  155.     h = read_data("h")
  156.     i = read_data("i")
  157.     j = read_data("j")
  158.  
  159.  
  160. def read_all_data():
  161.     check_if_dir_exist()
  162.     update_data()
  163.  
  164.  
  165. def database():
  166.     return_str = "-" * length
  167.     return_str += "\n" + header_of_data + ":\n"
  168.     for dir_key in lists("dir"):
  169.         return_str += "\n\t" + dir_key + ":\t{}"
  170.     return_str += "\n\n"
  171.     return_str += "-" * length
  172.     return return_str.format(lists("name")[0], lists("name")[1], lists("name")[2], lists("name")[3], lists("name")[4],
  173.                              lists("name")[5], lists("name")[6], lists("name")[7], lists("name")[8], lists("name")[9])
  174.  
  175.  
  176. def lists(list_type):
  177.     update_data()
  178.     name_list = [a.get_name(), b.get_name(), c.get_name(), d.get_name(), e.get_name(),
  179.                  f.get_name(), g.get_name(), h.get_name(), i.get_name(), j.get_name()]
  180.  
  181.     data_list = [a.get_data(), b.get_data(), c.get_data(), d.get_data(), e.get_data(),
  182.                  f.get_data(), g.get_data(), h.get_data(), i.get_data(), j.get_data()]
  183.  
  184.     dir_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  185.  
  186.     if list_type == "name":
  187.         return name_list
  188.     elif list_type == "data":
  189.         return data_list
  190.     elif list_type == "dir":
  191.         return dir_list
  192.  
  193.  
  194. def main():
  195.     read_all_data()
  196.     script_open = True
  197.     while script_open:
  198.  
  199.         absolutely_unused_variable = os.system(clear_term)
  200.  
  201.         print(script_name)
  202.         print("-" * length)
  203.         print(help_bar_text + "\n")
  204.         print("-" * length)
  205.  
  206.         choice = input("\nChoose: ")
  207.  
  208.         if choice == "1":
  209.             show_items_open = True
  210.             while show_items_open:
  211.                 absolutely_unused_variable = os.system(clear_term)
  212.  
  213.                 print(script_name)
  214.                 print(database())
  215.                 choice_database = input("Show Item: ")
  216.  
  217.                 try:
  218.                     if read_name(choice_database) == "":
  219.                         show_items_open = True
  220.                     elif choice_database in lists("dir"):
  221.                         absolutely_unused_variable = os.system(clear_term)
  222.  
  223.                         print("Item=" + choice_database + "\n" + "-" * length)
  224.                         output = []
  225.                         for char in choice_database:
  226.                             number = ord(char) - 97
  227.                             output.append(number)
  228.  
  229.                         print(lists("name")[output[0]])
  230.                         print("-" * length)
  231.                         input()
  232.                 except IndexError:
  233.                     show_items_open = True
  234.  
  235.                 if choice_database == "":
  236.                     show_items_open = False
  237.  
  238.         if choice == "2":
  239.             edit_items_open = True
  240.             while edit_items_open:
  241.                 absolutely_unused_variable = os.system(clear_term)
  242.  
  243.                 print(script_name)
  244.                 print(database())
  245.                 choice_edit_items = input("Edit Item: ")
  246.  
  247.                 if choice_edit_items in lists("dir"):
  248.                     absolutely_unused_variable = os.system(clear_term)
  249.  
  250.                     print("Item=" + choice_edit_items + "\n" + "-" * length)
  251.                     name = input("Set " + var_name + ": ")
  252.                     name_read = read_name(choice_edit_items)
  253.  
  254.                     if name == "":
  255.                         print("Are you sure your want to delete item (" + choice_edit_items + ")?")
  256.                         del_choice = input("y/n")
  257.  
  258.                         if del_choice == "y":
  259.                             age = ""
  260.                             del_about(choice_edit_items)
  261.                             create_txt(choice_edit_items, name, age)
  262.                             update_data()
  263.  
  264.                         else:
  265.                             edit_items_open = False
  266.  
  267.                     elif name != name_read:
  268.                         age = input("Set " + var_year_born + ": ")
  269.                         del_about(choice_edit_items)
  270.                         create_txt(choice_edit_items, name, age)
  271.                         update_data()
  272.  
  273.                 elif choice_edit_items == "":
  274.                     edit_items_open = False
  275.  
  276.         if choice == "3":
  277.             edit_about_open = True
  278.             while edit_about_open:
  279.                 absolutely_unused_variable = os.system(clear_term)
  280.                 print(script_name)
  281.                 print(database())
  282.                 choice_edit_items = input("Edit " + var_about + " in Item: ")
  283.  
  284.                 if choice_edit_items in lists("dir"):
  285.                     absolutely_unused_variable = os.system(clear_term)
  286.                     print("Item=" + choice_edit_items + "\n" + "-" * length)
  287.                     name_read = read_name(choice_edit_items)
  288.  
  289.                     if name_read == "":
  290.                         edit_about_open = False
  291.  
  292.                     else:
  293.                         print(var_about + ":\n")
  294.                         main_story = ""
  295.                         while True:
  296.                             story = input()
  297.                             if story == "":
  298.                                 break
  299.                             main_story = main_story + "\n" + story
  300.                             write_story(choice_edit_items, main_story)
  301.                             update_data()
  302.  
  303.                 elif choice_edit_items == "":
  304.                     edit_about_open = False
  305.  
  306.         if choice == "4":
  307.             absolutely_unused_variable = os.system(clear_term)
  308.             print("\nAre you sure you want to delete all data?")
  309.             del_y_or_n = input("y/n: ")
  310.  
  311.             if del_y_or_n == "y":
  312.                 shutil.rmtree(new_dir_path)
  313.                 check_if_dir_exist()
  314.                 update_data()
  315.  
  316.         if choice == "end":
  317.             absolutely_unused_variable = os.system(clear_term)
  318.             script_open = False
  319.  
  320.  
  321. if __name__ == "__main__":
  322.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement