cooptools 1.78__tar.gz → 1.80__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (198) hide show
  1. {cooptools-1.78 → cooptools-1.80}/PKG-INFO +1 -1
  2. {cooptools-1.78 → cooptools-1.80}/cooptools/commandDesignPattern/commandController.py +45 -22
  3. {cooptools-1.78 → cooptools-1.80}/cooptools.egg-info/PKG-INFO +1 -1
  4. {cooptools-1.78 → cooptools-1.80}/setup.py +1 -1
  5. cooptools-1.80/tests/test_commandDesignPattern.py +351 -0
  6. cooptools-1.78/tests/test_commandDesignPattern.py +0 -183
  7. {cooptools-1.78 → cooptools-1.80}/MANIFEST.in +0 -0
  8. {cooptools-1.78 → cooptools-1.80}/README.md +0 -0
  9. {cooptools-1.78 → cooptools-1.80}/cooptools/__init__.py +0 -0
  10. {cooptools-1.78 → cooptools-1.80}/cooptools/anchor.py +0 -0
  11. {cooptools-1.78 → cooptools-1.80}/cooptools/asyncable.py +0 -0
  12. {cooptools-1.78 → cooptools-1.80}/cooptools/cli/CliAtomicUserInteraction.py +0 -0
  13. {cooptools-1.78 → cooptools-1.80}/cooptools/cli/CliMenu.py +0 -0
  14. {cooptools-1.78 → cooptools-1.80}/cooptools/cli/__init__.py +0 -0
  15. {cooptools-1.78 → cooptools-1.80}/cooptools/cli/fileContentReturn.py +0 -0
  16. {cooptools-1.78 → cooptools-1.80}/cooptools/cnxn_info.py +0 -0
  17. {cooptools-1.78 → cooptools-1.80}/cooptools/colors.py +0 -0
  18. {cooptools-1.78 → cooptools-1.80}/cooptools/commandDesignPattern/__init__.py +0 -0
  19. {cooptools-1.78 → cooptools-1.80}/cooptools/commandDesignPattern/commandProtocol.py +0 -0
  20. {cooptools-1.78 → cooptools-1.80}/cooptools/commandDesignPattern/exceptions.py +0 -0
  21. {cooptools-1.78 → cooptools-1.80}/cooptools/common.py +0 -0
  22. {cooptools-1.78 → cooptools-1.80}/cooptools/config.py +0 -0
  23. {cooptools-1.78 → cooptools-1.80}/cooptools/coopDataclass.py +0 -0
  24. {cooptools-1.78 → cooptools-1.80}/cooptools/coopEnum.py +0 -0
  25. {cooptools-1.78 → cooptools-1.80}/cooptools/coopthreading.py +0 -0
  26. {cooptools-1.78 → cooptools-1.80}/cooptools/currency.py +0 -0
  27. {cooptools-1.78 → cooptools-1.80}/cooptools/dataRefresher/__init__.py +0 -0
  28. {cooptools-1.78 → cooptools-1.80}/cooptools/dataRefresher/dataHub.py +0 -0
  29. {cooptools-1.78 → cooptools-1.80}/cooptools/dataRefresher/dataRefresher.py +0 -0
  30. {cooptools-1.78 → cooptools-1.80}/cooptools/dataStore/__init__.py +0 -0
  31. {cooptools-1.78 → cooptools-1.80}/cooptools/dataStore/dataProcessor.py +0 -0
  32. {cooptools-1.78 → cooptools-1.80}/cooptools/dataStore/dataStoreProtocol.py +0 -0
  33. {cooptools-1.78 → cooptools-1.80}/cooptools/dataStore/dbConnectionURI.py +0 -0
  34. {cooptools-1.78 → cooptools-1.80}/cooptools/dataStore/inMemoryDataStore.py +0 -0
  35. {cooptools-1.78 → cooptools-1.80}/cooptools/date_utils.py +0 -0
  36. {cooptools-1.78 → cooptools-1.80}/cooptools/decay.py +0 -0
  37. {cooptools-1.78 → cooptools-1.80}/cooptools/decor.py +0 -0
  38. {cooptools-1.78 → cooptools-1.80}/cooptools/dictPolicies.py +0 -0
  39. {cooptools-1.78 → cooptools-1.80}/cooptools/exceptions.py +0 -0
  40. {cooptools-1.78 → cooptools-1.80}/cooptools/expertise/__init__.py +0 -0
  41. {cooptools-1.78 → cooptools-1.80}/cooptools/expertise/expertiseArgs.py +0 -0
  42. {cooptools-1.78 → cooptools-1.80}/cooptools/expertise/expertiseSchedules.py +0 -0
  43. {cooptools-1.78 → cooptools-1.80}/cooptools/expertise/expertise_state.py +0 -0
  44. {cooptools-1.78 → cooptools-1.80}/cooptools/finance/__init__.py +0 -0
  45. {cooptools-1.78 → cooptools-1.80}/cooptools/finance/dicounted_cashflow_analysis.py +0 -0
  46. {cooptools-1.78 → cooptools-1.80}/cooptools/finance/fund_projection.py +0 -0
  47. {cooptools-1.78 → cooptools-1.80}/cooptools/finance/fund_projection_renderer.py +0 -0
  48. {cooptools-1.78 → cooptools-1.80}/cooptools/finance/futureValueProjector.py +0 -0
  49. {cooptools-1.78 → cooptools-1.80}/cooptools/finance/growth_projections.py +0 -0
  50. {cooptools-1.78 → cooptools-1.80}/cooptools/finance/reports.py +0 -0
  51. {cooptools-1.78 → cooptools-1.80}/cooptools/finance/utils.py +0 -0
  52. {cooptools-1.78 → cooptools-1.80}/cooptools/geometry_utils/__init__.py +0 -0
  53. {cooptools-1.78 → cooptools-1.80}/cooptools/geometry_utils/circle_utils.py +0 -0
  54. {cooptools-1.78 → cooptools-1.80}/cooptools/geometry_utils/common.py +0 -0
  55. {cooptools-1.78 → cooptools-1.80}/cooptools/geometry_utils/curve_utils.py +0 -0
  56. {cooptools-1.78 → cooptools-1.80}/cooptools/geometry_utils/curves.py +0 -0
  57. {cooptools-1.78 → cooptools-1.80}/cooptools/geometry_utils/line_utils.py +0 -0
  58. {cooptools-1.78 → cooptools-1.80}/cooptools/geometry_utils/polygon_utils.py +0 -0
  59. {cooptools-1.78 → cooptools-1.80}/cooptools/geometry_utils/rect_utils.py +0 -0
  60. {cooptools-1.78 → cooptools-1.80}/cooptools/geometry_utils/triangle_utils.py +0 -0
  61. {cooptools-1.78 → cooptools-1.80}/cooptools/geometry_utils/vector_utils.py +0 -0
  62. {cooptools-1.78 → cooptools-1.80}/cooptools/graphs/__init__.py +0 -0
  63. {cooptools-1.78 → cooptools-1.80}/cooptools/graphs/astar_results_viewer.py +0 -0
  64. {cooptools-1.78 → cooptools-1.80}/cooptools/graphs/draw.py +0 -0
  65. {cooptools-1.78 → cooptools-1.80}/cooptools/graphs/flow_field.py +0 -0
  66. {cooptools-1.78 → cooptools-1.80}/cooptools/graphs/graph.py +0 -0
  67. {cooptools-1.78 → cooptools-1.80}/cooptools/graphs/graph_dcs.py +0 -0
  68. {cooptools-1.78 → cooptools-1.80}/cooptools/graphs/graph_definitions.py +0 -0
  69. {cooptools-1.78 → cooptools-1.80}/cooptools/graphs/utils.py +0 -0
  70. {cooptools-1.78 → cooptools-1.80}/cooptools/ideas/__init__.py +0 -0
  71. {cooptools-1.78 → cooptools-1.80}/cooptools/ideas/actionItemTracker/__init__.py +0 -0
  72. {cooptools-1.78 → cooptools-1.80}/cooptools/ideas/actionItemTracker/ai_tracker.py +0 -0
  73. {cooptools-1.78 → cooptools-1.80}/cooptools/ideas/actionItemTracker/dcs.py +0 -0
  74. {cooptools-1.78 → cooptools-1.80}/cooptools/ideas/discreteEventSimulator/__init__.py +0 -0
  75. {cooptools-1.78 → cooptools-1.80}/cooptools/ideas/discreteEventSimulator/discreteEventSimulator.py +0 -0
  76. {cooptools-1.78 → cooptools-1.80}/cooptools/ideas/jsonIO.py +0 -0
  77. {cooptools-1.78 → cooptools-1.80}/cooptools/ideas/metrics.py +0 -0
  78. {cooptools-1.78 → cooptools-1.80}/cooptools/ideas/triggerActionSystem/__init__.py +0 -0
  79. {cooptools-1.78 → cooptools-1.80}/cooptools/ideas/triggerActionSystem/signal.py +0 -0
  80. {cooptools-1.78 → cooptools-1.80}/cooptools/ideas/triggerActionSystem/trigger.py +0 -0
  81. {cooptools-1.78 → cooptools-1.80}/cooptools/loggingHelpers.py +0 -0
  82. {cooptools-1.78 → cooptools-1.80}/cooptools/marchingSquares.py +0 -0
  83. {cooptools-1.78 → cooptools-1.80}/cooptools/materialHandling/__init__.py +0 -0
  84. {cooptools-1.78 → cooptools-1.80}/cooptools/materialHandling/cli.py +0 -0
  85. {cooptools-1.78 → cooptools-1.80}/cooptools/materialHandling/dcs.py +0 -0
  86. {cooptools-1.78 → cooptools-1.80}/cooptools/materialHandling/selectionCriteria.py +0 -0
  87. {cooptools-1.78 → cooptools-1.80}/cooptools/matrixManipulation.py +0 -0
  88. {cooptools-1.78 → cooptools-1.80}/cooptools/os_manip.py +0 -0
  89. {cooptools-1.78 → cooptools-1.80}/cooptools/pandasHelpers.py +0 -0
  90. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/__init__.py +0 -0
  91. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/catmullRomFollower.py +0 -0
  92. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/demo/__init__.py +0 -0
  93. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/demo/server.py +0 -0
  94. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/kinematicGoalSeeker.py +0 -0
  95. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/motion_profile_utils.py +0 -0
  96. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/orbit_fuzzer.py +0 -0
  97. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/path_utils.py +0 -0
  98. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/straightLineFollower.py +0 -0
  99. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/trajectory_provider.py +0 -0
  100. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/waypoint.py +0 -0
  101. {cooptools-1.78 → cooptools-1.80}/cooptools/pathing/waypointFollower.py +0 -0
  102. {cooptools-1.78 → cooptools-1.80}/cooptools/persistence/__init__.py +0 -0
  103. {cooptools-1.78 → cooptools-1.80}/cooptools/persistence/enum_state.py +0 -0
  104. {cooptools-1.78 → cooptools-1.80}/cooptools/persistence/stateful.py +0 -0
  105. {cooptools-1.78 → cooptools-1.80}/cooptools/physics/__init__.py +0 -0
  106. {cooptools-1.78 → cooptools-1.80}/cooptools/physics/kinematic.py +0 -0
  107. {cooptools-1.78 → cooptools-1.80}/cooptools/physics/kinematic_utils.py +0 -0
  108. {cooptools-1.78 → cooptools-1.80}/cooptools/physics/movement.py +0 -0
  109. {cooptools-1.78 → cooptools-1.80}/cooptools/physics/trajectory.py +0 -0
  110. {cooptools-1.78 → cooptools-1.80}/cooptools/plotting.py +0 -0
  111. {cooptools-1.78 → cooptools-1.80}/cooptools/printing.py +0 -0
  112. {cooptools-1.78 → cooptools-1.80}/cooptools/protocols.py +0 -0
  113. {cooptools-1.78 → cooptools-1.80}/cooptools/qualifiers/__init__.py +0 -0
  114. {cooptools-1.78 → cooptools-1.80}/cooptools/qualifiers/cli.py +0 -0
  115. {cooptools-1.78 → cooptools-1.80}/cooptools/qualifiers/qualifier.py +0 -0
  116. {cooptools-1.78 → cooptools-1.80}/cooptools/randoms.py +0 -0
  117. {cooptools-1.78 → cooptools-1.80}/cooptools/register.py +0 -0
  118. {cooptools-1.78 → cooptools-1.80}/cooptools/reservation/__init__.py +0 -0
  119. {cooptools-1.78 → cooptools-1.80}/cooptools/reservation/dcs.py +0 -0
  120. {cooptools-1.78 → cooptools-1.80}/cooptools/reservation/enums.py +0 -0
  121. {cooptools-1.78 → cooptools-1.80}/cooptools/reservation/reservationmanager.py +0 -0
  122. {cooptools-1.78 → cooptools-1.80}/cooptools/retry.py +0 -0
  123. {cooptools-1.78 → cooptools-1.80}/cooptools/sectors/__init__.py +0 -0
  124. {cooptools-1.78 → cooptools-1.80}/cooptools/sectors/grids/__init__.py +0 -0
  125. {cooptools-1.78 → cooptools-1.80}/cooptools/sectors/grids/gridState.py +0 -0
  126. {cooptools-1.78 → cooptools-1.80}/cooptools/sectors/grids/grid_base.py +0 -0
  127. {cooptools-1.78 → cooptools-1.80}/cooptools/sectors/grids/hexGrid.py +0 -0
  128. {cooptools-1.78 → cooptools-1.80}/cooptools/sectors/grids/rectGrid.py +0 -0
  129. {cooptools-1.78 → cooptools-1.80}/cooptools/sectors/hex_utils.py +0 -0
  130. {cooptools-1.78 → cooptools-1.80}/cooptools/sectors/sect_utils.py +0 -0
  131. {cooptools-1.78 → cooptools-1.80}/cooptools/sectors/sectorTree/__init__.py +0 -0
  132. {cooptools-1.78 → cooptools-1.80}/cooptools/sectors/sectorTree/sectorTree.py +0 -0
  133. {cooptools-1.78 → cooptools-1.80}/cooptools/statistics/__init__.py +0 -0
  134. {cooptools-1.78 → cooptools-1.80}/cooptools/statistics/activityReport.py +0 -0
  135. {cooptools-1.78 → cooptools-1.80}/cooptools/statistics/controlChart/__init__.py +0 -0
  136. {cooptools-1.78 → cooptools-1.80}/cooptools/statistics/controlChart/controlChart.py +0 -0
  137. {cooptools-1.78 → cooptools-1.80}/cooptools/statistics/controlChart/plotting.py +0 -0
  138. {cooptools-1.78 → cooptools-1.80}/cooptools/statistics/oee/__init__.py +0 -0
  139. {cooptools-1.78 → cooptools-1.80}/cooptools/statistics/oee/oeeHub.py +0 -0
  140. {cooptools-1.78 → cooptools-1.80}/cooptools/taskProcessing/__init__.py +0 -0
  141. {cooptools-1.78 → cooptools-1.80}/cooptools/taskProcessing/dcs.py +0 -0
  142. {cooptools-1.78 → cooptools-1.80}/cooptools/taskProcessing/taskProcessor.py +0 -0
  143. {cooptools-1.78 → cooptools-1.80}/cooptools/timeTracker/__init__.py +0 -0
  144. {cooptools-1.78 → cooptools-1.80}/cooptools/timeTracker/decay.py +0 -0
  145. {cooptools-1.78 → cooptools-1.80}/cooptools/timeTracker/timeTracker.py +0 -0
  146. {cooptools-1.78 → cooptools-1.80}/cooptools/timeWindow.py +0 -0
  147. {cooptools-1.78 → cooptools-1.80}/cooptools/toggles.py +0 -0
  148. {cooptools-1.78 → cooptools-1.80}/cooptools/transform.py +0 -0
  149. {cooptools-1.78 → cooptools-1.80}/cooptools/trends.py +0 -0
  150. {cooptools-1.78 → cooptools-1.80}/cooptools/typeProviders.py +0 -0
  151. {cooptools-1.78 → cooptools-1.80}/cooptools/typevalidation.py +0 -0
  152. {cooptools-1.78 → cooptools-1.80}/cooptools/version.py +0 -0
  153. {cooptools-1.78 → cooptools-1.80}/cooptools.egg-info/SOURCES.txt +0 -0
  154. {cooptools-1.78 → cooptools-1.80}/cooptools.egg-info/dependency_links.txt +0 -0
  155. {cooptools-1.78 → cooptools-1.80}/cooptools.egg-info/not-zip-safe +0 -0
  156. {cooptools-1.78 → cooptools-1.80}/cooptools.egg-info/requires.txt +0 -0
  157. {cooptools-1.78 → cooptools-1.80}/cooptools.egg-info/top_level.txt +0 -0
  158. {cooptools-1.78 → cooptools-1.80}/requirements.txt +0 -0
  159. {cooptools-1.78 → cooptools-1.80}/setup.cfg +0 -0
  160. {cooptools-1.78 → cooptools-1.80}/tests/test_CliAtomicUserInteraction.py +0 -0
  161. {cooptools-1.78 → cooptools-1.80}/tests/test_asyncable.py +0 -0
  162. {cooptools-1.78 → cooptools-1.80}/tests/test_catmullRomFollower.py +0 -0
  163. {cooptools-1.78 → cooptools-1.80}/tests/test_cdp_bugs.py +0 -0
  164. {cooptools-1.78 → cooptools-1.80}/tests/test_common.py +0 -0
  165. {cooptools-1.78 → cooptools-1.80}/tests/test_config.py +0 -0
  166. {cooptools-1.78 → cooptools-1.80}/tests/test_coopEnum.py +0 -0
  167. {cooptools-1.78 → cooptools-1.80}/tests/test_currency.py +0 -0
  168. {cooptools-1.78 → cooptools-1.80}/tests/test_curves.py +0 -0
  169. {cooptools-1.78 → cooptools-1.80}/tests/test_date_utils.py +0 -0
  170. {cooptools-1.78 → cooptools-1.80}/tests/test_expertise_state.py +0 -0
  171. {cooptools-1.78 → cooptools-1.80}/tests/test_flow_field.py +0 -0
  172. {cooptools-1.78 → cooptools-1.80}/tests/test_flow_field_perf.py +0 -0
  173. {cooptools-1.78 → cooptools-1.80}/tests/test_fund_projection.py +0 -0
  174. {cooptools-1.78 → cooptools-1.80}/tests/test_graph_node_classification.py +0 -0
  175. {cooptools-1.78 → cooptools-1.80}/tests/test_kinematicGoalSeeker.py +0 -0
  176. {cooptools-1.78 → cooptools-1.80}/tests/test_kinematic_utils.py +0 -0
  177. {cooptools-1.78 → cooptools-1.80}/tests/test_os_manip.py +0 -0
  178. {cooptools-1.78 → cooptools-1.80}/tests/test_pandas.py +0 -0
  179. {cooptools-1.78 → cooptools-1.80}/tests/test_path_utils.py +0 -0
  180. {cooptools-1.78 → cooptools-1.80}/tests/test_persistence.py +0 -0
  181. {cooptools-1.78 → cooptools-1.80}/tests/test_printing.py +0 -0
  182. {cooptools-1.78 → cooptools-1.80}/tests/test_qualifiers.py +0 -0
  183. {cooptools-1.78 → cooptools-1.80}/tests/test_sectors.py +0 -0
  184. {cooptools-1.78 → cooptools-1.80}/tests/test_sectors_hex.py +0 -0
  185. {cooptools-1.78 → cooptools-1.80}/tests/test_sectors_sectorTree.py +0 -0
  186. {cooptools-1.78 → cooptools-1.80}/tests/test_statistics.py +0 -0
  187. {cooptools-1.78 → cooptools-1.80}/tests/test_straightLineFollower.py +0 -0
  188. {cooptools-1.78 → cooptools-1.80}/tests/test_target_seek.py +0 -0
  189. {cooptools-1.78 → cooptools-1.80}/tests/test_tasks.py +0 -0
  190. {cooptools-1.78 → cooptools-1.80}/tests/test_toggles.py +0 -0
  191. {cooptools-1.78 → cooptools-1.80}/tests/test_trajectory.py +0 -0
  192. {cooptools-1.78 → cooptools-1.80}/tests/test_trends.py +0 -0
  193. {cooptools-1.78 → cooptools-1.80}/tests/test_typevalidation.py +0 -0
  194. {cooptools-1.78 → cooptools-1.80}/tests/test_vector_utils.py +0 -0
  195. {cooptools-1.78 → cooptools-1.80}/tests/test_version.py +0 -0
  196. {cooptools-1.78 → cooptools-1.80}/tests/test_waypointFollower.py +0 -0
  197. {cooptools-1.78 → cooptools-1.80}/tests/tests_graph.py +0 -0
  198. {cooptools-1.78 → cooptools-1.80}/tests/tests_gridsystem.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cooptools
3
- Version: 1.78
3
+ Version: 1.80
4
4
  Summary: Generic Tooling
5
5
  Home-page: https://github.com/tylertjburns/cooptools
6
6
  Author: tburns
@@ -11,6 +11,13 @@ logger = logging.getLogger(__name__)
11
11
 
12
12
  T = TypeVar('T')
13
13
 
14
+
15
+ def share_state(state: T) -> T:
16
+ """A `copy_state` for controllers whose commands always return a new state and
17
+ never modify the one they are given. Snapshots then cost nothing, because an old
18
+ state can never change underneath the reference kept to it."""
19
+ return state
20
+
14
21
  class CommandStore(Protocol):
15
22
  def __init__(self, reset_state: bool = False):
16
23
  if reset_state:
@@ -51,8 +58,8 @@ class CommandStore(Protocol):
51
58
  return last_cached_state, max_cached_idx
52
59
 
53
60
  def clear_cache(self):
54
- cached = self.get_cached()
55
- for cursor in cached.keys():
61
+ # the keys are snapshotted because remove_cached_at_cursor mutates the dict get_cached returns
62
+ for cursor in list(self.get_cached().keys()):
56
63
  self.remove_cached_at_cursor(cursor)
57
64
 
58
65
  def clear_commands(self):
@@ -99,9 +106,15 @@ class InMemoryCommandStore(CommandStore):
99
106
 
100
107
  @dataclass
101
108
  class CommandController:
109
+ """`copy_state` takes every snapshot the controller keeps: the initial state, the
110
+ rollback point before a batch, cached states, and the starting point of a replay.
111
+ It defaults to a deep copy, which is required when commands modify state in place.
112
+ Pass `share_state` when commands return new state instead; with large or frequently
113
+ changed state the deep copies otherwise dominate the cost of `execute`."""
102
114
  init_state: T = None
103
115
  cache_interval: int = 100
104
116
  command_store: CommandStore = field(default_factory=InMemoryCommandStore)
117
+ copy_state: Callable[[T], T] = copy.deepcopy
105
118
  cursor: int = field(default=0, init=False)
106
119
  _lock: threading.RLock = field(default_factory=threading.RLock, init=False)
107
120
  _live_state: T = field(default=None, init=False)
@@ -117,17 +130,16 @@ class CommandController:
117
130
  raise ValueError(f"The init state must be set for command stores that have not been initialized")
118
131
 
119
132
  if not has_init and self.init_state is not None:
120
- self._live_state = copy.deepcopy(self.init_state)
121
- self.command_store.add_cached(copy.deepcopy(self._live_state), self.cursor)
133
+ self._live_state = self.copy_state(self.init_state)
134
+ self.command_store.add_cached(self.copy_state(self._live_state), self.cursor)
122
135
  else:
136
+ # the cursor has to land before the resolve, because resolve() replays up to it; a store
137
+ # restored from persistence otherwise resolves at 0 and hands back the (absent) init state
138
+ self.cursor = len(self.command_store.get_commands(start_cursor=idx)) + idx
123
139
  self._live_state = self.resolve()
124
140
 
125
- logger.info(f"Init State: {pprint.pformat(self._live_state)}")
126
-
127
- if idx is None:
128
- self.cursor = 0
129
- else:
130
- self.cursor = len(self.command_store.get_commands(start_cursor=idx)) + idx
141
+ if logger.isEnabledFor(logging.INFO):
142
+ logger.info(f"Init State: {pprint.pformat(self._live_state)}")
131
143
 
132
144
  def _needsLock(foo):
133
145
  def magic(self, *args, **kwargs):
@@ -140,8 +152,9 @@ class CommandController:
140
152
 
141
153
  @_needsLock
142
154
  def execute(self, commands: List[CommandProtocol]) -> T:
143
- # delete any registered commands after the current cursor
144
- self.command_store.remove_commands(self.cursor + 1)
155
+ # a cursor of N means N commands have been applied, so the active commands are exactly
156
+ # the first N in the store and anything from N onward is a branch being discarded
157
+ self.command_store.remove_commands(self.cursor)
145
158
 
146
159
  # delete any cached states after the current cursor
147
160
  stale = [ii for ii in self.command_store.get_cached() if ii > self.cursor]
@@ -149,10 +162,11 @@ class CommandController:
149
162
  self.command_store.remove_cached_at_cursor(ii)
150
163
 
151
164
  # snapshot for atomic rollback on failure
152
- pre_batch_state = copy.deepcopy(self._live_state)
165
+ pre_batch_state = self.copy_state(self._live_state)
153
166
  pre_batch_cursor = self.cursor
154
167
 
155
- logger.info(f"Executing commands {pprint.pformat(commands)} [idx: {self.cursor}]")
168
+ if logger.isEnabledFor(logging.INFO):
169
+ logger.info(f"Executing commands {pprint.pformat(commands)} [idx: {self.cursor}]")
156
170
 
157
171
  command = None
158
172
  try:
@@ -165,11 +179,13 @@ class CommandController:
165
179
  except Exception as e:
166
180
  self._live_state = pre_batch_state
167
181
  self.cursor = pre_batch_cursor
168
- self.command_store.remove_commands(pre_batch_cursor + 1)
182
+ self.command_store.remove_commands(pre_batch_cursor)
169
183
  raise ResolveStateException(command=command, inner=e) from e
170
184
 
171
- if self.cursor - max(x for x in self.command_store.get_cached().keys()) >= self.cache_interval:
172
- self.command_store.add_cached(copy.deepcopy(self._live_state), self.cursor)
185
+ cached_cursors = self.command_store.get_cached().keys()
186
+ last_cached_cursor = max(cached_cursors) if cached_cursors else 0
187
+ if self.cursor - last_cached_cursor >= self.cache_interval:
188
+ self.command_store.add_cached(self.copy_state(self._live_state), self.cursor)
173
189
 
174
190
  return self._live_state
175
191
 
@@ -180,20 +196,25 @@ class CommandController:
180
196
  idx = self.cursor
181
197
 
182
198
  if idx == 0:
183
- return copy.deepcopy(self.init_state)
199
+ # a store restored from persistence has a cursor-0 snapshot but no init_state in hand
200
+ cached_at_origin = self.command_store.get_cached().get(0, None)
201
+ if cached_at_origin is not None:
202
+ return self.copy_state(cached_at_origin)
203
+ return self.copy_state(self.init_state)
184
204
 
185
205
  try:
186
206
  # Get latest cached
187
207
  last_cached_state, last_cached_idx = self.command_store.last_cached(max_idx=idx)
188
208
 
189
- last_cached_state = copy.deepcopy(last_cached_state)
209
+ last_cached_state = self.copy_state(last_cached_state)
190
210
 
191
211
  for command in self.command_store.get_commands(last_cached_idx, idx):
192
212
  last_cached_state = command.execute(last_cached_state)
193
213
  if last_cached_state is None:
194
214
  raise Exception("The command.execute() operation returned a None value")
195
215
 
196
- logger.info(pprint.pformat(last_cached_state))
216
+ if logger.isEnabledFor(logging.INFO):
217
+ logger.info(pprint.pformat(last_cached_state))
197
218
  return last_cached_state
198
219
  except Exception as e:
199
220
  # raise the exception on the command that failed
@@ -208,7 +229,8 @@ class CommandController:
208
229
  logger.info(f"Undo - [idx: {self.cursor}]")
209
230
 
210
231
  self._live_state = self.resolve(self.cursor)
211
- logger.info(pprint.pformat(self._live_state))
232
+ if logger.isEnabledFor(logging.INFO):
233
+ logger.info(pprint.pformat(self._live_state))
212
234
  return self._live_state
213
235
 
214
236
  @_needsLock
@@ -220,7 +242,8 @@ class CommandController:
220
242
  logger.info(f"Redo - [idx: {self.cursor}]")
221
243
 
222
244
  self._live_state = self.resolve(self.cursor)
223
- logger.info(pprint.pformat(self._live_state))
245
+ if logger.isEnabledFor(logging.INFO):
246
+ logger.info(pprint.pformat(self._live_state))
224
247
  return self._live_state
225
248
 
226
249
  @property
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cooptools
3
- Version: 1.78
3
+ Version: 1.80
4
4
  Summary: Generic Tooling
5
5
  Home-page: https://github.com/tylertjburns/cooptools
6
6
  Author: tburns
@@ -7,7 +7,7 @@ with open('requirements.txt') as f:
7
7
  requirements = f.read().splitlines()
8
8
 
9
9
  setuptools.setup(name='cooptools',
10
- version='1.78',
10
+ version='1.80',
11
11
  description='Generic Tooling',
12
12
  url='https://github.com/tylertjburns/cooptools',
13
13
  author='tburns',
@@ -0,0 +1,351 @@
1
+ import unittest
2
+ from cooptools.commandDesignPattern import CommandBatch, CommandProtocol, CommandController, InMemoryCommandStore, share_state
3
+ import copy
4
+ from dataclasses import dataclass
5
+ from cooptools.commandDesignPattern.exceptions import ResolveBatchException, ResolveStateException
6
+
7
+ @dataclass
8
+ class Dummy_State:
9
+ amount: int = 0
10
+
11
+ @dataclass
12
+ class Dummy_Command(CommandProtocol):
13
+ adjustment: int
14
+
15
+ def execute(self, state):
16
+ state.amount += self.adjustment
17
+ return state
18
+ # return Dummy_State(amount=state.amount + self.adjustment)
19
+
20
+ @dataclass
21
+ class Dummy_CommandThatFails(CommandProtocol):
22
+ adjustment: int
23
+
24
+ def execute(self, state):
25
+ raise Exception(f"Dummy Exception")
26
+
27
+ @dataclass
28
+ class Dummy_PureCommand(CommandProtocol):
29
+ adjustment: int
30
+
31
+ def execute(self, state):
32
+ return Dummy_State(amount=state.amount + self.adjustment)
33
+
34
+ class Test_CommandDesignPattern(unittest.TestCase):
35
+
36
+ def test__register_commands(self):
37
+ # arrange
38
+ state = Dummy_State()
39
+ controller = CommandController(state)
40
+ commands = [
41
+ Dummy_Command(adjustment=1000),
42
+ Dummy_Command(adjustment=500)
43
+ ]
44
+
45
+
46
+ # act
47
+ controller.execute(commands=commands)
48
+
49
+ # assert
50
+ self.assertEqual(len(controller.ActiveCommands), len(commands))
51
+ self.assertEqual(controller.cursor, len(commands))
52
+
53
+ def test__resolve_state(self):
54
+
55
+ # arrange
56
+ state = Dummy_State()
57
+ controller = CommandController(state)
58
+ commands = [
59
+ Dummy_Command(adjustment=1000),
60
+ Dummy_Command(adjustment=500)
61
+ ]
62
+
63
+ # act
64
+ state = controller.execute(commands=commands)
65
+
66
+ # assert
67
+ self.assertEqual(len(controller.ActiveCommands), len(commands))
68
+ self.assertEqual(state.amount, 1500)
69
+ self.assertEqual(controller.cursor, len(commands))
70
+
71
+ def test__add_batch_command(self):
72
+ # arrange
73
+ state = Dummy_State()
74
+ controller = CommandController(state)
75
+ commands =[
76
+ Dummy_Command(adjustment=1000),
77
+ Dummy_Command(adjustment=500)
78
+ ]
79
+ batch = CommandBatch(commands=commands)
80
+
81
+ # act
82
+ state = controller.execute(commands=[batch])
83
+
84
+ # assert
85
+ self.assertEqual(len(controller.ActiveCommands), 1)
86
+ self.assertEqual(state.amount, 1500)
87
+ self.assertEqual(controller.cursor, 1)
88
+
89
+ def test__add_batch_and_command(self):
90
+ # arrange
91
+ state = Dummy_State()
92
+ controller = CommandController(state)
93
+
94
+ batch = CommandBatch(commands=[
95
+ Dummy_Command(adjustment=1000),
96
+ Dummy_Command(adjustment=500)
97
+ ])
98
+ commands = [
99
+ batch,
100
+ Dummy_Command(adjustment=500)
101
+ ]
102
+
103
+ # act
104
+ state = controller.execute(commands=commands)
105
+
106
+ # assert
107
+ self.assertEqual(len(controller.ActiveCommands), 2)
108
+ self.assertEqual(state.amount, 2000)
109
+
110
+ def test__undo_resolve(self):
111
+ # arrange
112
+ state = Dummy_State()
113
+ controller = CommandController(state)
114
+
115
+ batch = CommandBatch(commands=[
116
+ Dummy_Command(adjustment=1000),
117
+ Dummy_Command(adjustment=500)
118
+ ])
119
+ commands = [
120
+ batch,
121
+ Dummy_Command(adjustment=500)
122
+ ]
123
+
124
+ # act
125
+ state = controller.execute(commands=commands)
126
+ state = controller.undo()
127
+
128
+ # assert
129
+ self.assertEqual(len(controller.ActiveCommands), 1)
130
+ self.assertEqual(state.amount, 1500)
131
+ self.assertEqual(controller.cursor, len(commands) - 1)
132
+
133
+ def test__undo_redo_resolve(self):
134
+ # arrange
135
+ state = Dummy_State()
136
+ controller = CommandController(state)
137
+
138
+ batch = CommandBatch(commands=[
139
+ Dummy_Command(adjustment=1000),
140
+ Dummy_Command(adjustment=500)
141
+ ])
142
+ commands = [
143
+ batch,
144
+ Dummy_Command(adjustment=500)
145
+ ]
146
+
147
+ # act
148
+ state = controller.execute(commands=commands)
149
+ state = controller.undo()
150
+ state = controller.redo()
151
+
152
+ # assert
153
+ self.assertEqual(len(controller.ActiveCommands), 2)
154
+ self.assertEqual(state.amount, 2000)
155
+
156
+ self.assertEqual(controller.cursor, len(commands))
157
+
158
+ def test__resolve_fails_revert(self):
159
+ # arrange
160
+ state = Dummy_State()
161
+ controller = CommandController(state)
162
+
163
+ commands=[
164
+ Dummy_Command(adjustment=1000),
165
+ Dummy_CommandThatFails(adjustment=500)
166
+ ]
167
+
168
+ # act
169
+
170
+
171
+ # assert
172
+ self.assertRaises(ResolveStateException, lambda: controller.execute(commands=commands))
173
+ # execute() rolls back _live_state atomically on failure
174
+ self.assertEqual(controller.State.amount, 0)
175
+
176
+
177
+ def test__resolve_fails_no_revert(self):
178
+ # arrange
179
+ state = Dummy_State()
180
+ controller = CommandController(state)
181
+
182
+ commands=[
183
+ Dummy_Command(adjustment=1000),
184
+ Dummy_CommandThatFails(adjustment=500)
185
+ ]
186
+
187
+ # act
188
+
189
+ # assert
190
+ self.assertRaises(ResolveStateException, lambda: controller.execute(commands=commands))
191
+
192
+ def test__execute_after_undo__discards_the_abandoned_branch(self):
193
+ # arrange
194
+ controller = CommandController(Dummy_State())
195
+ controller.execute([Dummy_PureCommand(adjustment=100)])
196
+ controller.execute([Dummy_PureCommand(adjustment=200)])
197
+ controller.undo()
198
+
199
+ # act
200
+ controller.execute([Dummy_PureCommand(adjustment=999)])
201
+
202
+ # assert
203
+ self.assertEqual(controller.ActiveCommands,
204
+ [Dummy_PureCommand(adjustment=100), Dummy_PureCommand(adjustment=999)])
205
+ self.assertEqual(controller.cursor, 2)
206
+
207
+ def test__execute_after_undo__live_state_matches_resolved_state(self):
208
+ # arrange
209
+ controller = CommandController(Dummy_State())
210
+ controller.execute([Dummy_PureCommand(adjustment=100)])
211
+ controller.execute([Dummy_PureCommand(adjustment=200)])
212
+ controller.undo()
213
+
214
+ # act
215
+ controller.execute([Dummy_PureCommand(adjustment=999)])
216
+
217
+ # assert
218
+ self.assertEqual(controller.State.amount, 1099)
219
+ self.assertEqual(controller.resolve(controller.cursor).amount, controller.State.amount)
220
+
221
+ def test__failed_batch__leaves_no_commands_in_the_store(self):
222
+ # arrange
223
+ controller = CommandController(Dummy_State())
224
+
225
+ # act
226
+ self.assertRaises(ResolveStateException, lambda: controller.execute(
227
+ [Dummy_PureCommand(adjustment=1000), Dummy_CommandThatFails(adjustment=500)]))
228
+
229
+ # assert
230
+ self.assertEqual(controller.command_store.get_commands(), [])
231
+ self.assertEqual(controller.cursor, 0)
232
+
233
+ def test__execute_after_failed_batch__live_state_matches_resolved_state(self):
234
+ # arrange
235
+ controller = CommandController(Dummy_State())
236
+ self.assertRaises(ResolveStateException, lambda: controller.execute(
237
+ [Dummy_PureCommand(adjustment=1000), Dummy_CommandThatFails(adjustment=500)]))
238
+
239
+ # act
240
+ controller.execute([Dummy_PureCommand(adjustment=7)])
241
+
242
+ # assert
243
+ self.assertEqual(controller.State.amount, 7)
244
+ self.assertEqual(controller.resolve(controller.cursor).amount, controller.State.amount)
245
+
246
+ def test__clear_cache__removes_every_cached_state(self):
247
+ # arrange
248
+ controller = CommandController(Dummy_State(), cache_interval=1)
249
+ controller.execute([Dummy_PureCommand(adjustment=1)])
250
+ controller.execute([Dummy_PureCommand(adjustment=1)])
251
+ self.assertTrue(len(controller.command_store.get_cached()) > 1)
252
+
253
+ # act
254
+ controller.command_store.clear_cache()
255
+
256
+ # assert
257
+ self.assertEqual(controller.command_store.get_cached(), {})
258
+
259
+ def test__execute_with_no_cached_states__does_not_raise(self):
260
+ # arrange
261
+ controller = CommandController(Dummy_State())
262
+ controller.command_store.clear_cache()
263
+
264
+ # act
265
+ state = controller.execute([Dummy_PureCommand(adjustment=42)])
266
+
267
+ # assert
268
+ self.assertEqual(state.amount, 42)
269
+
270
+ def test__controller_restored_from_store__recovers_state_and_cursor(self):
271
+ # arrange
272
+ store = InMemoryCommandStore()
273
+ original = CommandController(Dummy_State(), command_store=store)
274
+ original.execute([Dummy_PureCommand(adjustment=100)])
275
+ original.execute([Dummy_PureCommand(adjustment=200)])
276
+
277
+ # act
278
+ restored = CommandController(command_store=store)
279
+
280
+ # assert
281
+ self.assertEqual(restored.State.amount, 300)
282
+ self.assertEqual(restored.cursor, original.cursor)
283
+
284
+ def test__controller_restored_from_store__undo_to_origin_returns_origin_state(self):
285
+ # arrange
286
+ store = InMemoryCommandStore()
287
+ original = CommandController(Dummy_State(), command_store=store)
288
+ original.execute([Dummy_PureCommand(adjustment=100)])
289
+ restored = CommandController(command_store=store)
290
+
291
+ # act
292
+ state = restored.undo()
293
+
294
+ # assert
295
+ self.assertEqual(state.amount, 0)
296
+
297
+ def test__copy_state__takes_every_snapshot(self):
298
+ # arrange
299
+ copies = []
300
+ def spy(state):
301
+ copies.append(state)
302
+ return state
303
+ init = Dummy_State()
304
+ controller = CommandController(init, copy_state=spy)
305
+
306
+ # act
307
+ controller.execute([Dummy_PureCommand(adjustment=5)])
308
+
309
+ # assert
310
+ self.assertEqual(len(copies), 3) # live state at init, cached origin, pre-batch rollback point
311
+ self.assertIs(controller.CachedStates[0][1], init)
312
+ self.assertEqual(controller.State.amount, 5)
313
+
314
+ def test__share_state__failed_batch_rolls_back_to_the_previous_state(self):
315
+ # arrange
316
+ controller = CommandController(Dummy_State(), copy_state=share_state)
317
+ controller.execute([Dummy_PureCommand(adjustment=5)])
318
+ before = controller.State
319
+
320
+ # act
321
+ with self.assertRaises(ResolveStateException):
322
+ controller.execute([Dummy_PureCommand(adjustment=1), Dummy_CommandThatFails(adjustment=1)])
323
+
324
+ # assert
325
+ self.assertIs(controller.State, before)
326
+ self.assertEqual(controller.State.amount, 5)
327
+
328
+ def test__share_state__undo_resolves_earlier_states(self):
329
+ # arrange
330
+ controller = CommandController(Dummy_State(), cache_interval=2, copy_state=share_state)
331
+ for _ in range(5):
332
+ controller.execute([Dummy_PureCommand(adjustment=10)])
333
+
334
+ # act
335
+ state = controller.undo()
336
+
337
+ # assert
338
+ self.assertEqual(state.amount, 40)
339
+ self.assertEqual(controller.resolve(1).amount, 10)
340
+
341
+ def test__default_copy__isolates_snapshots_from_in_place_commands(self):
342
+ # arrange
343
+ controller = CommandController(Dummy_State())
344
+ controller.execute([Dummy_Command(adjustment=5)])
345
+
346
+ # act
347
+ state = controller.undo()
348
+
349
+ # assert
350
+ self.assertEqual(state.amount, 0)
351
+ self.assertIs(CommandController.copy_state, copy.deepcopy)