我找到的最好的持久性ZODB示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我找到的最好的持久性ZODB示例相关的知识,希望对你有一定的参考价值。

  1. import sys
  2. import cmd
  3. import shlex
  4.  
  5. import persistent
  6. import transaction
  7. import ZODB
  8. import ZODB.FileStorage
  9.  
  10. class Project(persistent.Persistent):
  11. def __init__(self, name, title):
  12. self.name = name
  13. self.title = title
  14. self.tasks = {}
  15.  
  16. def addTask(self, name, description):
  17. task = Task(name, description)
  18. self.tasks[name] = task
  19. self._p_changed = True
  20.  
  21. class Task(persistent.Persistent):
  22. def __init__(self, name, description):
  23. self.name = name
  24. self.description = description
  25. self.bookings = []
  26.  
  27. def bookTime(self, time, description=''):
  28. booking = Booking(time, description)
  29. self.bookings.append(booking)
  30. self._p_changed = True
  31.  
  32. class Booking(persistent.Persistent):
  33. def __init__(self, time, description):
  34. self.time = time
  35. self.description = description
  36.  
  37. class TimeTrax(cmd.Cmd, object):
  38. def __init__(self, intro="TimeTrax time tracking helper",
  39. prompt="timetrax: ", db_path="projects.fs"):
  40. super(TimeTrax, self).__init__()
  41. self.intro = intro
  42. self.prompt = prompt
  43. self.db = ZODB.DB(ZODB.FileStorage.FileStorage(db_path))
  44. self.projects = self.db.open().root()
  45.  
  46. def addProject(self, name, title):
  47. project = Project(name, title)
  48. self.projects[name] = project
  49. transaction.commit()
  50.  
  51. def dropProject(self, name):
  52. del self.projects[name]
  53. transaction.commit()
  54.  
  55. def addTask(self, project, name, description):
  56. self.projects[project].addTask(name, description)
  57. transaction.commit()
  58.  
  59. def bookTime(self, project, task, time, description):
  60. self.projects[project].tasks[task].bookTime(time, description)
  61. transaction.commit()
  62.  
  63. def postloop(self):
  64. print
  65.  
  66. def postcmd(self, stop, line):
  67. if line=='EOF':
  68. return self.do_EOF(self)
  69. if not line.startswith('help'):
  70. print
  71.  
  72. def precmd(self, line):
  73. if not line.startswith('help'):
  74. print
  75. return line
  76.  
  77. def emptyline(self):
  78. print
  79.  
  80. def help_help(self):
  81. print "Show this help"
  82.  
  83. def do_EOF(self, line):
  84. "Exit the shell"
  85. return True
  86.  
  87. def do_create(self, line):
  88. name, title = shlex.split(line)
  89. self.addProject(name, title)
  90. print "created project %s" % name
  91.  
  92. def help_create(self):
  93. print "create project_name project_title"
  94. print "Create a new project with unique name project_name"
  95.  
  96. def do_drop(self, line):
  97. if line in self.projects.keys():
  98. self.dropProject(line)
  99. print "dropped project %s" % line
  100. else:
  101. print "%s is not a recognized project" % line
  102.  
  103. def help_drop(self):
  104. print "drop project_name"
  105. print "drop a project named project_name from the database"
  106.  
  107. def do_list(self, line):
  108. if not line:
  109. return self.list_projects()
  110. args = shlex.split(line)
  111. if len(args) == 1:
  112. return self.list_tasks(line)
  113. return self.list_bookings(args[0], args[1])
  114.  
  115. def help_list(self):
  116. print "list [project_name] [task_name]"
  117. print "List all projects if no arguments are given"
  118. print "List all tasks in project_name"
  119. print "List all time bookings for task_name in project_name"
  120.  
  121. def list_projects(self):
  122. print "Project Title"
  123. print "======= ====="
  124. for name, project in self.projects.items():
  125. print "%-20s%s" % (name, project.title)
  126.  
  127. def list_tasks(self, project):
  128. print "Project: %s" % project
  129. print
  130. print "Task Time Description"
  131. print "==== ==== ==========="
  132. for name, task in self.projects[project].tasks.items():
  133. total_time = sum([booking.time for booking in task.bookings])
  134. print "%-20s%-8s%s" % (name, total_time, task.description)
  135.  
  136. def list_bookings(self, project, task):
  137. task = self.projects[project].tasks[task]
  138. total_time = 0
  139. print "Project: %s" % project
  140. print "task: %s" % task.description
  141. print
  142. print "Time Description"
  143. print "==== ==========="
  144. for booking in task.bookings:
  145. print "%-8s%s" % (booking.time, booking.description)
  146. total_time = total_time + booking.time
  147. print "---- -----------"
  148. print "%-8sTotal" % total_time
  149.  
  150. def do_add(self, line):
  151. project, task, description = shlex.split(line)
  152. self.addTask(project, task, description)
  153. print "Added task %s to project %s" % (task, project)
  154.  
  155. def help_add(self):
  156. print "add project_name task_name task_description"
  157. print "Add a new task to project_name"
  158. print "task_description is required"
  159.  
  160. def do_book(self, line):
  161. args = shlex.split(line)
  162. if len(args) == 3:
  163. project, task, time= shlex.split(line)
  164. description = ''
  165. else:
  166. project, task, time, description = shlex.split(line)
  167. time = int(time)
  168. self.bookTime(project, task, time, description)
  169. print "booked %s hours for task %s in project %s" % (time,
  170. task,
  171. project)
  172.  
  173. def help_book(self):
  174. print "book project_name task_name hours work_description"
  175. print "Book time in hours for a task in project_name"
  176. print "work_description is required"
  177.  
  178. if __name__ == '__main__':
  179. if len(sys.argv) > 1:
  180. args = [(' ' in arg and '"%s"' % arg) or arg for arg in sys.argv[1:]]
  181. TimeTrax().onecmd(' '.join(args))
  182. else:
  183. TimeTrax().cmdloop()

以上是关于我找到的最好的持久性ZODB示例的主要内容,如果未能解决你的问题,请参考以下文章

python的对象数据库ZODB的使用(python3经典编程案例)

如何从已排序的流中最好地构建持久二叉树

一个简单用例上的 Neo4j 节点持久性问题

Delphi对象持久化,最好的方法是啥

没有缓存/持久性的nodejs最好的方法是啥?

反应式 UI 数据持久性