fork download
  1. #!/usr/bin/env python3
  2.  
  3. import os, pickle, re, sys
  4.  
  5. class Content(object):
  6. def __init__(self, title, artist, rating, ripped):
  7. self.title = title
  8. self.artist = artist
  9. self.rating = rating
  10. self.ripped = ripped
  11. def __repr__(self):
  12. return f'Title: {self.title}\nArtist: {self.artist}\nRating: {self.rating}\nRipped: {self.ripped}\n'
  13.  
  14. class Env(object):
  15. def __init__(self, package, ls = [], fname="", flag = False):
  16. self.package = package
  17. self.ls = ls
  18. self.fname = fname
  19. self.flag = flag
  20. def __repr__(self):
  21. return f'<package: {self.package} list: {self.ls} filename: {self.fname} flag: {self.flag}>'
  22.  
  23. def install_package():
  24. def insert_value(arg, env):
  25. return Env(env.package, ls = [Content(*arg)] + env.ls, fname = env.fname)
  26. def select(arg, env):
  27. return Env(env.package, ls = env.ls, fname = env.fname, flag = True)
  28. def new_file(arg, env):
  29. with open(''.join(arg) + ".dat", "wb") as f:
  30. pass
  31. return Env(env.package, ls = env.ls, fname = arg)
  32. def open_file(arg, env):
  33. fname = ''.join(arg)
  34. with open(fname, "rb") as f:
  35. ls = pickle.load(f)
  36. return Env(env.package, ls = ls, fname = fname)
  37. def save_file(arg, env):
  38. if env.fname == '':
  39. return save_file_as('tmp.dat', env)
  40. else:
  41. os.rename(env.fname, env.fname + '.bak')
  42. with open(env.fname, "wb") as f:
  43. pickle.dump(env.ls, f)
  44. return Env(env.package, ls = env.ls, fname = env.fname)
  45. def save_file_as(arg, env):
  46. fname = ''.join(arg)
  47. with open(fname, "wb") as f:
  48. pickle.dump(env.ls, f)
  49. return Env(env.package, ls = env.ls, fname = fname)
  50. def delete(arg, env):
  51. return Env(env.package,
  52. ls = [i for i in env.ls if i != env.ls[int(arg[0])]],
  53. fname = env.fname)
  54. return {'insert_value': insert_value,
  55. 'select': select,
  56. 'new_file': new_file,
  57. 'open_file': open_file,
  58. 'save_file': save_file,
  59. 'save_file_as': save_file_as,
  60. 'quit': lambda arg, env: sys.exit(),
  61. 'delete': delete}
  62.  
  63. # Read
  64. def read(env):
  65. return [i.strip() for i in
  66. re.split("\(|,|、|\)", input("simple database> "))
  67. if i != ""]
  68.  
  69. # Eval
  70. def interp(x, env):
  71. return env.package[x[0]](x[1:], env)
  72.  
  73. # Print
  74. def display(env):
  75. if env.flag:
  76. [print(i) for i in env.ls]
  77. # print(env) # デバッグ用
  78. return env
  79.  
  80. if __name__ == '__main__':
  81. env = display(Env(install_package()))
  82. while True:
  83. try:
  84. env = display(interp(read(env), env))
  85. except KeyError:
  86. env
  87. except ValueError:
  88. env
  89. except IndexError:
  90. env
  91.  
Runtime error #stdin #stdout #stderr 0.14s 25660KB
stdin
Standard input is empty
stdout
simple database> 
stderr
Traceback (most recent call last):
  File "./prog.py", line 84, in <module>
  File "./prog.py", line 66, in read
EOFError: EOF when reading a line