Advertisement
Guest User

Just a Script (py3.5)

a guest
Sep 28th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.34 KB | None | 0 0
  1. #/usr/bin/python3.5
  2. import datetime
  3. import os
  4. import shutil
  5. now = datetime.datetime.now()
  6.  
  7. nameOfScript = "Something v1.0"
  8.  
  9. #Windows="cls", osX="clear"
  10. clearTerm = ("cls" if os.name == "nt" else "clear")
  11.  
  12. dirPath = os.path.dirname(os.path.realpath(__file__))
  13. dirName = "/data"
  14. newDirPath = dirPath + dirName
  15.  
  16.  
  17. nameFile = "/name.txt"
  18. ageFile = "/yearBorn.txt"
  19. storyFile = "/about.txt"
  20.  
  21. getDataName = "Name"
  22. getDataAge = "Age"
  23. getDataStory = "About"
  24. setYearBorn = "yearBorn"
  25.  
  26. headerName = "DATA"
  27.  
  28. helpBarText = \
  29.     ("\n(1)\tShow Data"
  30.      "\n(2)\tEdit items in Data"
  31.      "\n(3)\tEdit About in item"
  32.      "\n(4)\tDel all Data"
  33.      "\n(end)\texit")
  34.  
  35.  
  36.  
  37. class data:
  38.     _navn = ""
  39.     _alder = 0
  40.  
  41.     _story = ""
  42.  
  43.     def __init__(self, navn, yearBorn, story):
  44.         self._navn = navn
  45.         self._story = story
  46.  
  47.         if yearBorn == None:
  48.             self._alder = ""
  49.         elif yearBorn < 0:
  50.             self._alder = ""
  51.         elif yearBorn > now.year:
  52.             self._alder = ""
  53.         else:
  54.             self._alder = now.year - yearBorn
  55.  
  56.  
  57.     def getNavn(self):
  58.         return self._navn
  59.  
  60.     def getAlder(self):
  61.         return self._alder
  62.  
  63.     def getStory(self):
  64.         return self._story
  65.  
  66.     def getData(self):
  67.         return("\n"+ getDataName +":\t {}"
  68.                "\n"+ getDataAge +":\t {}"
  69.                "\n"+ getDataStory +":\n {}")\
  70.                .format(self._navn,
  71.                        self._alder,
  72.                        self._story)
  73.  
  74. # type == a-j
  75. def createTxt(type, name, yearBorn):
  76.         _name = open(dir(type) + nameFile, "w")
  77.         _name.write(name)
  78.         _name.close()
  79.  
  80.         _age = open(dir(type) + ageFile, "w")
  81.         _age.write(yearBorn)
  82.         _age.close()
  83.  
  84.  
  85. # type == a-j
  86. def readTxt(type , nORy):
  87.         if nORy == "n":
  88.             _name = open(dir(type) + nameFile, "r")
  89.             output = _name.read()
  90.             _name.close()
  91.             return output
  92.         elif nORy == "y":
  93.             _age = open(dir(type) + ageFile, "r")
  94.             output = _age.read()
  95.             _age.close()
  96.             if output == "":
  97.                 return None
  98.             else:
  99.                 return int(output)
  100.  
  101.  
  102. # type == a-j
  103. def writeStory(type, story):
  104.     _story = open(dir(type) + storyFile, "w")
  105.     _story.write(story)
  106.     _story.close()
  107.  
  108.  
  109. # type == a-j
  110. def readStory(type):
  111.     _story = open(dir(type) + storyFile, "r")
  112.     output = _story.read()
  113.     _story.close()
  114.     return output
  115.  
  116.  
  117. def delStory(type):
  118.     writeStory(type, "")
  119.     pass
  120.  
  121.  
  122. def checkDir():
  123.     if not os.path.exists(newDirPath):
  124.         os.makedirs(newDirPath)
  125.  
  126.         os.makedirs(dir("a"))
  127.         os.makedirs(dir("b"))
  128.         os.makedirs(dir("c"))
  129.         os.makedirs(dir("d"))
  130.         os.makedirs(dir("e"))
  131.         os.makedirs(dir("f"))
  132.         os.makedirs(dir("g"))
  133.         os.makedirs(dir("h"))
  134.         os.makedirs(dir("i"))
  135.         os.makedirs(dir("j"))
  136.  
  137.         createTxt("a", "", "")
  138.         createTxt("b", "", "")
  139.         createTxt("c", "", "")
  140.         createTxt("d", "", "")
  141.         createTxt("e", "", "")
  142.         createTxt("f", "", "")
  143.         createTxt("g", "", "")
  144.         createTxt("h", "", "")
  145.         createTxt("i", "", "")
  146.         createTxt("j", "", "")
  147.  
  148.         writeStory("a", "")
  149.         writeStory("b", "")
  150.         writeStory("c", "")
  151.         writeStory("d", "")
  152.         writeStory("e", "")
  153.         writeStory("f", "")
  154.         writeStory("g", "")
  155.         writeStory("h", "")
  156.         writeStory("i", "")
  157.         writeStory("j", "")
  158.         return
  159.     else:
  160.         return
  161.  
  162.  
  163. # type == a-j
  164. def dir(type):
  165.         return(newDirPath + "/" + type)
  166.  
  167. def readName(type):
  168.     if type == "a":
  169.         return a.getNavn()
  170.     if type == "b":
  171.         return b.getNavn()
  172.     if type == "c":
  173.         return c.getNavn()
  174.     if type == "d":
  175.         return d.getNavn()
  176.     if type == "e":
  177.         return e.getNavn()
  178.     if type == "f":
  179.         return f.getNavn()
  180.     if type == "g":
  181.         return g.getNavn()
  182.     if type == "h":
  183.         return h.getNavn()
  184.     if type == "i":
  185.         return i.getNavn()
  186.     if type == "j":
  187.         return j.getNavn()
  188.  
  189. # type == a-j
  190. def readData(type):
  191.     _data = data(readTxt(type, "n"), readTxt(type, "y"), readStory(type))
  192.     return _data
  193.  
  194. def updateData():
  195.     global a
  196.     global b
  197.     global c
  198.     global d
  199.     global e
  200.     global f
  201.     global g
  202.     global h
  203.     global i
  204.     global j
  205.  
  206.     a = readData("a")
  207.     b = readData("b")
  208.     c = readData("c")
  209.     d = readData("d")
  210.     e = readData("e")
  211.     f = readData("f")
  212.     g = readData("g")
  213.     h = readData("h")
  214.     i = readData("i")
  215.     j = readData("j")
  216.  
  217. checkDir()
  218. a = readData("a")
  219. b = readData("b")
  220. c = readData("c")
  221. d = readData("d")
  222. e = readData("e")
  223. f = readData("f")
  224. g = readData("g")
  225. h = readData("h")
  226. i = readData("i")
  227. j = readData("j")
  228.  
  229.  
  230. def database():
  231.     return("----------------------------"
  232.            "\n"+ headerName +":\n"
  233.            "\n\ta:\t{}"
  234.            "\n\tb:\t{}"
  235.            "\n\tc:\t{}"
  236.            "\n\td:\t{}"
  237.            "\n\te:\t{}"
  238.            "\n\tf:\t{}"
  239.            "\n\tg:\t{}"
  240.            "\n\th:\t{}"
  241.            "\n\ti:\t{}"
  242.            "\n\tj:\t{}"
  243.            "\n\n"
  244.            "----------------------------")\
  245.             .format(a.getNavn(),
  246.                     b.getNavn(),
  247.                     c.getNavn(),
  248.                     d.getNavn(),
  249.                     e.getNavn(),
  250.                     f.getNavn(),
  251.                     g.getNavn(),
  252.                     h.getNavn(),
  253.                     i.getNavn(),
  254.                     j.getNavn())
  255.  
  256.  
  257.  
  258. def main():
  259.     close = False
  260.     while not close:
  261.         absolutely_unused_variable = os.system(clearTerm)
  262.         print(nameOfScript)
  263.         print("----------------------------")
  264.         print(helpBarText)
  265.         print("\n----------------------------")
  266.         choice = input("\nChoose: ")
  267.  
  268.         if choice == "1":
  269.             closeShowItems = False
  270.             while not closeShowItems:
  271.                 absolutely_unused_variable = os.system(clearTerm)
  272.                 print(nameOfScript)
  273.                 print(database())
  274.                 choiceDatabase = input("Show Item: ")
  275.                 if readName(choiceDatabase) == "":
  276.                     None
  277.                 else:
  278.                     if choiceDatabase == "a":
  279.                         absolutely_unused_variable = os.system(clearTerm)
  280.                         print("Item=" + choiceDatabase + "\n----------------------------")
  281.                         print(a.getData())
  282.                         print("----------------------------")
  283.                         input()
  284.                     if choiceDatabase == "b":
  285.                         absolutely_unused_variable = os.system(clearTerm)
  286.                         print("Item=" + choiceDatabase + "\n----------------------------")
  287.                         print(b.getData())
  288.                         print("----------------------------")
  289.                         input()
  290.                     if choiceDatabase == "c":
  291.                         absolutely_unused_variable = os.system(clearTerm)
  292.                         print("Item=" + choiceDatabase + "\n----------------------------")
  293.                         print(c.getData())
  294.                         print("----------------------------")
  295.                         input()
  296.                     if choiceDatabase == "d":
  297.                         absolutely_unused_variable = os.system(clearTerm)
  298.                         print("Item=" + choiceDatabase + "\n----------------------------")
  299.                         print(d.getData())
  300.                         print("----------------------------")
  301.                         input()
  302.                     if choiceDatabase == "e":
  303.                         absolutely_unused_variable = os.system(clearTerm)
  304.                         print("Item=" + choiceDatabase + "\n----------------------------")
  305.                         print(e.getData())
  306.                         print("----------------------------")
  307.                         input()
  308.                     if choiceDatabase == "f":
  309.                         absolutely_unused_variable = os.system(clearTerm)
  310.                         print("Item=" + choiceDatabase + "\n----------------------------")
  311.                         print(f.getData())
  312.                         print("----------------------------")
  313.                         input()
  314.                     if choiceDatabase == "g":
  315.                         absolutely_unused_variable = os.system(clearTerm)
  316.                         print("Item=" + choiceDatabase + "\n----------------------------")
  317.                         print(g.getData())
  318.                         print("----------------------------")
  319.                         input()
  320.                     if choiceDatabase == "h":
  321.                         absolutely_unused_variable = os.system(clearTerm)
  322.                         print("Item=" + choiceDatabase + "\n----------------------------")
  323.                         print(h.getData())
  324.                         print("----------------------------")
  325.                         input()
  326.                     if choiceDatabase == "i":
  327.                         absolutely_unused_variable = os.system(clearTerm)
  328.                         print("Item=" + choiceDatabase + "\n----------------------------")
  329.                         print(i.getData())
  330.                         print("----------------------------")
  331.                         input()
  332.                     if choiceDatabase == "j":
  333.                         absolutely_unused_variable = os.system(clearTerm)
  334.                         print("Item=" + choiceDatabase + "\n----------------------------")
  335.                         print(j.getData())
  336.                         print("----------------------------")
  337.                         input()
  338.                     if choiceDatabase == "":
  339.                         closeShowItems = True
  340.  
  341.         if choice == "2":
  342.             closeEditItems = False
  343.             while not closeEditItems:
  344.                 absolutely_unused_variable = os.system(clearTerm)
  345.                 print(nameOfScript)
  346.                 print(database())
  347.                 spes = input("Edit Item: ")
  348.                 if spes in ("a",
  349.                             "b",
  350.                             "c",
  351.                             "d",
  352.                             "e",
  353.                             "f",
  354.                             "g",
  355.                             "h",
  356.                             "i"
  357.                             "j"):
  358.                     absolutely_unused_variable = os.system(clearTerm)
  359.                     print("Item=" + spes +"\n----------------------------")
  360.                     name = input("Set " + getDataName +": ")
  361.                     navn = readName(spes)
  362.                     if name == "":
  363.                         print("Are you sure your want to delete item (" + spes + ")?")
  364.                         delChoice = input("y/n")
  365.                         if delChoice == "y":
  366.                             age = ""
  367.                             delStory(spes)
  368.                             createTxt(spes, name, age)
  369.                             updateData()
  370.                         else:
  371.                             closeEditItems = True
  372.                     elif name != navn:
  373.                         age = input("Set " + setYearBorn + ": ")
  374.                         delStory(spes)
  375.                         createTxt(spes,name,age)
  376.                         updateData()
  377.                 elif spes == "":
  378.                     closeEditItems = True
  379.         if choice == "3":
  380.             closeEditAbout = False
  381.             while not closeEditAbout:
  382.                 absolutely_unused_variable = os.system(clearTerm)
  383.                 print(nameOfScript)
  384.                 print(database())
  385.                 spes = input("Edit " + getDataStory +" in Item: ")
  386.                 if spes in ("a",
  387.                             "b",
  388.                             "c",
  389.                             "d",
  390.                             "e",
  391.                             "f",
  392.                             "g",
  393.                             "h",
  394.                             "i"
  395.                             "j"):
  396.                     absolutely_unused_variable = os.system(clearTerm)
  397.                     print("Item=" + spes + "\n----------------------------")
  398.                     imName = readName(spes)
  399.                     if imName == "":
  400.                         None
  401.                     else:
  402.                         print(getDataStory + ":\n")
  403.                         mainStory = ""
  404.                         while True:
  405.                             story = input()
  406.                             if story == '': break
  407.                             mainStory = mainStory + "\n" + story
  408.                             writeStory(spes, mainStory)
  409.                             updateData()
  410.                 elif spes == "":
  411.                     closeEditAbout = True
  412.         if choice == "4":
  413.             absolutely_unused_variable = os.system(clearTerm)
  414.             print("\nAre you sure you want to delete all data?")
  415.             deld = input("y/n: ")
  416.             if deld == "y":
  417.                 shutil.rmtree(newDirPath)
  418.                 checkDir()
  419.                 updateData()
  420.  
  421.         if choice == "end":
  422.             absolutely_unused_variable = os.system(clearTerm)
  423.             close = True
  424.  
  425. if __name__ == "__main__":
  426.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement