Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # /usr/bin/python3.5
- import datetime
- import os
- import shutil
- now = datetime.datetime.now()
- script_name = "Something v1.0"
- length = 35 # length of "-"
- dir_path = os.path.dirname(os.path.realpath(__file__))
- new_dir_name = "/data"
- new_dir_path = dir_path + new_dir_name
- name_file = "/name.txt"
- age_file = "/year_born.txt"
- about_file = "/about.txt"
- var_name = "Name"
- var_age = "Age"
- var_about = "About"
- var_year_born = "Year Born"
- header_of_data = "DATA"
- help_bar_text = "\n(1)\tShow Data\n(2)\tEdit items in Data" \
- "\n(3)\tEdit About in item\n(4)\tDel all Data\n(end)\texit"
- # Windows="cls", osX="clear"
- clear_term = ("cls" if os.name == "nt" else "clear")
- class Data:
- name_of_person = ""
- age_of_person = 0
- about_person = ""
- def __init__(self, name, year_born, about):
- self.name_of_person = name
- self.about_person = about
- if year_born is None:
- self.age_of_person = ""
- elif year_born < 0:
- self.age_of_person = ""
- elif year_born > now.year:
- self.age_of_person = ""
- else:
- self.age_of_person = now.year - year_born
- def get_name(self):
- return self.name_of_person
- def get_age(self):
- return self.age_of_person
- def get_about(self):
- return self.about_person
- def get_data(self):
- return_str = "\n" + var_name + ":\t {}"
- return_str += "\n" + var_age + ":\t {}"
- return_str += "\n" + var_about + ":\n {}"
- return return_str.format(self.name_of_person,
- self.age_of_person,
- self.about_person)
- # a_j == a-j
- def create_txt(a_j, name, year_born):
- _name = open(_dir(a_j) + name_file, "w")
- _name.write(name)
- _name.close()
- _age = open(_dir(a_j) + age_file, "w")
- _age.write(year_born)
- _age.close()
- # a_j == a-j
- def read_txt(a_j, n_or_y):
- if n_or_y == "n":
- _name = open(_dir(a_j) + name_file, "r")
- output = _name.read()
- _name.close()
- return output
- elif n_or_y == "y":
- _age = open(_dir(a_j) + age_file, "r")
- output = _age.read()
- _age.close()
- if output == "":
- return None
- else:
- return int(output)
- # a_j == a-j
- def write_story(a_j, story):
- _story = open(_dir(a_j) + about_file, "w")
- _story.write(story)
- _story.close()
- # a_j == a-j
- def read_story(a_j):
- _story = open(_dir(a_j) + about_file, "r")
- output = _story.read()
- _story.close()
- return output
- def del_about(a_j):
- write_story(a_j, "")
- def check_if_dir_exist():
- if not os.path.exists(new_dir_path):
- os.makedirs(new_dir_path)
- for directory in lists("dir"):
- os.makedirs(dir(directory))
- return True
- else:
- return True
- # a_j == a-j
- def _dir(a_j):
- return new_dir_path + "/" + a_j
- def read_name(a_j):
- output = []
- for char in a_j:
- number = ord(char) - 97
- output.append(number)
- return lists("name")[output[0]]
- # a_j == a-j
- def read_data(a_j):
- _data = Data(read_txt(a_j, "n"), read_txt(a_j, "y"), read_story(a_j))
- return _data
- # noinspection PyGlobalUndefined
- def update_data():
- global a, b, c, d, e, f, g, h, i, j
- a = read_data("a")
- b = read_data("b")
- c = read_data("c")
- d = read_data("d")
- e = read_data("e")
- f = read_data("f")
- g = read_data("g")
- h = read_data("h")
- i = read_data("i")
- j = read_data("j")
- def read_all_data():
- check_if_dir_exist()
- update_data()
- def database():
- return_str = "-" * length
- return_str += "\n" + header_of_data + ":\n"
- for dir_key in lists("dir"):
- return_str += "\n\t" + dir_key + ":\t{}"
- return_str += "\n\n"
- return_str += "-" * length
- return return_str.format(lists("name")[0], lists("name")[1], lists("name")[2], lists("name")[3], lists("name")[4],
- lists("name")[5], lists("name")[6], lists("name")[7], lists("name")[8], lists("name")[9])
- def lists(list_type):
- update_data()
- name_list = [a.get_name(), b.get_name(), c.get_name(), d.get_name(), e.get_name(),
- f.get_name(), g.get_name(), h.get_name(), i.get_name(), j.get_name()]
- data_list = [a.get_data(), b.get_data(), c.get_data(), d.get_data(), e.get_data(),
- f.get_data(), g.get_data(), h.get_data(), i.get_data(), j.get_data()]
- dir_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
- if list_type == "name":
- return name_list
- elif list_type == "data":
- return data_list
- elif list_type == "dir":
- return dir_list
- def main():
- read_all_data()
- script_open = True
- while script_open:
- absolutely_unused_variable = os.system(clear_term)
- print(script_name)
- print("-" * length)
- print(help_bar_text + "\n")
- print("-" * length)
- choice = input("\nChoose: ")
- if choice == "1":
- show_items_open = True
- while show_items_open:
- absolutely_unused_variable = os.system(clear_term)
- print(script_name)
- print(database())
- choice_database = input("Show Item: ")
- try:
- if read_name(choice_database) == "":
- show_items_open = True
- elif choice_database in lists("dir"):
- absolutely_unused_variable = os.system(clear_term)
- print("Item=" + choice_database + "\n" + "-" * length)
- output = []
- for char in choice_database:
- number = ord(char) - 97
- output.append(number)
- print(lists("name")[output[0]])
- print("-" * length)
- input()
- except IndexError:
- show_items_open = True
- if choice_database == "":
- show_items_open = False
- if choice == "2":
- edit_items_open = True
- while edit_items_open:
- absolutely_unused_variable = os.system(clear_term)
- print(script_name)
- print(database())
- choice_edit_items = input("Edit Item: ")
- if choice_edit_items in lists("dir"):
- absolutely_unused_variable = os.system(clear_term)
- print("Item=" + choice_edit_items + "\n" + "-" * length)
- name = input("Set " + var_name + ": ")
- name_read = read_name(choice_edit_items)
- if name == "":
- print("Are you sure your want to delete item (" + choice_edit_items + ")?")
- del_choice = input("y/n")
- if del_choice == "y":
- age = ""
- del_about(choice_edit_items)
- create_txt(choice_edit_items, name, age)
- update_data()
- else:
- edit_items_open = False
- elif name != name_read:
- age = input("Set " + var_year_born + ": ")
- del_about(choice_edit_items)
- create_txt(choice_edit_items, name, age)
- update_data()
- elif choice_edit_items == "":
- edit_items_open = False
- if choice == "3":
- edit_about_open = True
- while edit_about_open:
- absolutely_unused_variable = os.system(clear_term)
- print(script_name)
- print(database())
- choice_edit_items = input("Edit " + var_about + " in Item: ")
- if choice_edit_items in lists("dir"):
- absolutely_unused_variable = os.system(clear_term)
- print("Item=" + choice_edit_items + "\n" + "-" * length)
- name_read = read_name(choice_edit_items)
- if name_read == "":
- edit_about_open = False
- else:
- print(var_about + ":\n")
- main_story = ""
- while True:
- story = input()
- if story == "":
- break
- main_story = main_story + "\n" + story
- write_story(choice_edit_items, main_story)
- update_data()
- elif choice_edit_items == "":
- edit_about_open = False
- if choice == "4":
- absolutely_unused_variable = os.system(clear_term)
- print("\nAre you sure you want to delete all data?")
- del_y_or_n = input("y/n: ")
- if del_y_or_n == "y":
- shutil.rmtree(new_dir_path)
- check_if_dir_exist()
- update_data()
- if choice == "end":
- absolutely_unused_variable = os.system(clear_term)
- script_open = False
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement