cooptools 1.77__tar.gz → 1.79__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.
- {cooptools-1.77 → cooptools-1.79}/PKG-INFO +1 -1
- {cooptools-1.77 → cooptools-1.79}/cooptools/commandDesignPattern/commandController.py +16 -11
- cooptools-1.79/cooptools/expertise/__init__.py +6 -0
- cooptools-1.79/cooptools/expertise/expertise_state.py +58 -0
- cooptools-1.79/cooptools/persistence/__init__.py +36 -0
- cooptools-1.79/cooptools/persistence/enum_state.py +81 -0
- cooptools-1.79/cooptools/persistence/stateful.py +148 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools.egg-info/PKG-INFO +1 -1
- {cooptools-1.77 → cooptools-1.79}/cooptools.egg-info/SOURCES.txt +6 -0
- {cooptools-1.77 → cooptools-1.79}/setup.py +1 -1
- {cooptools-1.77 → cooptools-1.79}/tests/test_commandDesignPattern.py +112 -1
- cooptools-1.79/tests/test_expertise_state.py +120 -0
- cooptools-1.79/tests/test_persistence.py +176 -0
- cooptools-1.77/cooptools/expertise/__init__.py +0 -2
- {cooptools-1.77 → cooptools-1.79}/MANIFEST.in +0 -0
- {cooptools-1.77 → cooptools-1.79}/README.md +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/anchor.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/asyncable.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/cli/CliAtomicUserInteraction.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/cli/CliMenu.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/cli/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/cli/fileContentReturn.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/cnxn_info.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/colors.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/commandDesignPattern/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/commandDesignPattern/commandProtocol.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/commandDesignPattern/exceptions.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/common.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/config.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/coopDataclass.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/coopEnum.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/coopthreading.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/currency.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/dataRefresher/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/dataRefresher/dataHub.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/dataRefresher/dataRefresher.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/dataStore/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/dataStore/dataProcessor.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/dataStore/dataStoreProtocol.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/dataStore/dbConnectionURI.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/dataStore/inMemoryDataStore.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/date_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/decay.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/decor.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/dictPolicies.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/exceptions.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/expertise/expertiseArgs.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/expertise/expertiseSchedules.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/finance/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/finance/dicounted_cashflow_analysis.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/finance/fund_projection.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/finance/fund_projection_renderer.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/finance/futureValueProjector.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/finance/growth_projections.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/finance/reports.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/finance/utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/geometry_utils/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/geometry_utils/circle_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/geometry_utils/common.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/geometry_utils/curve_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/geometry_utils/curves.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/geometry_utils/line_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/geometry_utils/polygon_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/geometry_utils/rect_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/geometry_utils/triangle_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/geometry_utils/vector_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/graphs/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/graphs/astar_results_viewer.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/graphs/draw.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/graphs/flow_field.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/graphs/graph.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/graphs/graph_dcs.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/graphs/graph_definitions.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/graphs/utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/ideas/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/ideas/actionItemTracker/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/ideas/actionItemTracker/ai_tracker.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/ideas/actionItemTracker/dcs.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/ideas/discreteEventSimulator/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/ideas/discreteEventSimulator/discreteEventSimulator.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/ideas/jsonIO.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/ideas/metrics.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/ideas/triggerActionSystem/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/ideas/triggerActionSystem/signal.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/ideas/triggerActionSystem/trigger.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/loggingHelpers.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/marchingSquares.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/materialHandling/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/materialHandling/cli.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/materialHandling/dcs.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/materialHandling/selectionCriteria.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/matrixManipulation.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/os_manip.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pandasHelpers.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/catmullRomFollower.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/demo/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/demo/server.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/kinematicGoalSeeker.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/motion_profile_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/orbit_fuzzer.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/path_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/straightLineFollower.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/trajectory_provider.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/waypoint.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/pathing/waypointFollower.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/physics/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/physics/kinematic.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/physics/kinematic_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/physics/movement.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/physics/trajectory.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/plotting.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/printing.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/protocols.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/qualifiers/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/qualifiers/cli.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/qualifiers/qualifier.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/randoms.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/register.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/reservation/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/reservation/dcs.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/reservation/enums.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/reservation/reservationmanager.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/retry.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/sectors/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/sectors/grids/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/sectors/grids/gridState.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/sectors/grids/grid_base.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/sectors/grids/hexGrid.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/sectors/grids/rectGrid.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/sectors/hex_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/sectors/sect_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/sectors/sectorTree/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/sectors/sectorTree/sectorTree.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/statistics/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/statistics/activityReport.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/statistics/controlChart/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/statistics/controlChart/controlChart.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/statistics/controlChart/plotting.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/statistics/oee/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/statistics/oee/oeeHub.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/taskProcessing/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/taskProcessing/dcs.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/taskProcessing/taskProcessor.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/timeTracker/__init__.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/timeTracker/decay.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/timeTracker/timeTracker.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/timeWindow.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/toggles.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/transform.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/trends.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/typeProviders.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/typevalidation.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools/version.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools.egg-info/dependency_links.txt +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools.egg-info/not-zip-safe +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools.egg-info/requires.txt +0 -0
- {cooptools-1.77 → cooptools-1.79}/cooptools.egg-info/top_level.txt +0 -0
- {cooptools-1.77 → cooptools-1.79}/requirements.txt +0 -0
- {cooptools-1.77 → cooptools-1.79}/setup.cfg +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_CliAtomicUserInteraction.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_asyncable.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_catmullRomFollower.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_cdp_bugs.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_common.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_config.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_coopEnum.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_currency.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_curves.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_date_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_flow_field.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_flow_field_perf.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_fund_projection.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_graph_node_classification.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_kinematicGoalSeeker.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_kinematic_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_os_manip.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_pandas.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_path_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_printing.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_qualifiers.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_sectors.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_sectors_hex.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_sectors_sectorTree.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_statistics.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_straightLineFollower.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_target_seek.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_tasks.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_toggles.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_trajectory.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_trends.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_typevalidation.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_vector_utils.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_version.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/test_waypointFollower.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/tests_graph.py +0 -0
- {cooptools-1.77 → cooptools-1.79}/tests/tests_gridsystem.py +0 -0
|
@@ -51,8 +51,8 @@ class CommandStore(Protocol):
|
|
|
51
51
|
return last_cached_state, max_cached_idx
|
|
52
52
|
|
|
53
53
|
def clear_cache(self):
|
|
54
|
-
|
|
55
|
-
for cursor in
|
|
54
|
+
# the keys are snapshotted because remove_cached_at_cursor mutates the dict get_cached returns
|
|
55
|
+
for cursor in list(self.get_cached().keys()):
|
|
56
56
|
self.remove_cached_at_cursor(cursor)
|
|
57
57
|
|
|
58
58
|
def clear_commands(self):
|
|
@@ -120,15 +120,13 @@ class CommandController:
|
|
|
120
120
|
self._live_state = copy.deepcopy(self.init_state)
|
|
121
121
|
self.command_store.add_cached(copy.deepcopy(self._live_state), self.cursor)
|
|
122
122
|
else:
|
|
123
|
+
# the cursor has to land before the resolve, because resolve() replays up to it; a store
|
|
124
|
+
# restored from persistence otherwise resolves at 0 and hands back the (absent) init state
|
|
125
|
+
self.cursor = len(self.command_store.get_commands(start_cursor=idx)) + idx
|
|
123
126
|
self._live_state = self.resolve()
|
|
124
127
|
|
|
125
128
|
logger.info(f"Init State: {pprint.pformat(self._live_state)}")
|
|
126
129
|
|
|
127
|
-
if idx is None:
|
|
128
|
-
self.cursor = 0
|
|
129
|
-
else:
|
|
130
|
-
self.cursor = len(self.command_store.get_commands(start_cursor=idx)) + idx
|
|
131
|
-
|
|
132
130
|
def _needsLock(foo):
|
|
133
131
|
def magic(self, *args, **kwargs):
|
|
134
132
|
with self._lock:
|
|
@@ -140,8 +138,9 @@ class CommandController:
|
|
|
140
138
|
|
|
141
139
|
@_needsLock
|
|
142
140
|
def execute(self, commands: List[CommandProtocol]) -> T:
|
|
143
|
-
#
|
|
144
|
-
|
|
141
|
+
# a cursor of N means N commands have been applied, so the active commands are exactly
|
|
142
|
+
# the first N in the store and anything from N onward is a branch being discarded
|
|
143
|
+
self.command_store.remove_commands(self.cursor)
|
|
145
144
|
|
|
146
145
|
# delete any cached states after the current cursor
|
|
147
146
|
stale = [ii for ii in self.command_store.get_cached() if ii > self.cursor]
|
|
@@ -165,10 +164,12 @@ class CommandController:
|
|
|
165
164
|
except Exception as e:
|
|
166
165
|
self._live_state = pre_batch_state
|
|
167
166
|
self.cursor = pre_batch_cursor
|
|
168
|
-
self.command_store.remove_commands(pre_batch_cursor
|
|
167
|
+
self.command_store.remove_commands(pre_batch_cursor)
|
|
169
168
|
raise ResolveStateException(command=command, inner=e) from e
|
|
170
169
|
|
|
171
|
-
|
|
170
|
+
cached_cursors = self.command_store.get_cached().keys()
|
|
171
|
+
last_cached_cursor = max(cached_cursors) if cached_cursors else 0
|
|
172
|
+
if self.cursor - last_cached_cursor >= self.cache_interval:
|
|
172
173
|
self.command_store.add_cached(copy.deepcopy(self._live_state), self.cursor)
|
|
173
174
|
|
|
174
175
|
return self._live_state
|
|
@@ -180,6 +181,10 @@ class CommandController:
|
|
|
180
181
|
idx = self.cursor
|
|
181
182
|
|
|
182
183
|
if idx == 0:
|
|
184
|
+
# a store restored from persistence has a cursor-0 snapshot but no init_state in hand
|
|
185
|
+
cached_at_origin = self.command_store.get_cached().get(0, None)
|
|
186
|
+
if cached_at_origin is not None:
|
|
187
|
+
return copy.deepcopy(cached_at_origin)
|
|
183
188
|
return copy.deepcopy(self.init_state)
|
|
184
189
|
|
|
185
190
|
try:
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Expertise across the JSON boundary.
|
|
2
|
+
|
|
3
|
+
Beside the calculator it serializes rather than in a persistence package, because
|
|
4
|
+
what these two functions know about is *expertise* -- which accumulator holds the
|
|
5
|
+
truth and which views are derived -- and only incidentally that the destination is
|
|
6
|
+
JSON. Grouped by mechanism they would sit next to enum conversion, which they have
|
|
7
|
+
nothing in common with; grouped by subject they sit next to the thing that breaks
|
|
8
|
+
them when it changes.
|
|
9
|
+
|
|
10
|
+
Functions over `ExpertiseCalculator` rather than methods on it, so that reading
|
|
11
|
+
and writing its state is available without widening the calculator's own API.
|
|
12
|
+
|
|
13
|
+
Deliberately asymmetric, and worth knowing why:
|
|
14
|
+
|
|
15
|
+
- Reading has to reach `_expertise_args`, because the calculator exposes only
|
|
16
|
+
derived views (`PercExpert`, `CurrentTimeReductionPerc`) and neither can be
|
|
17
|
+
inverted back into the accumulator that produced it.
|
|
18
|
+
- Writing does not, because `schedule` is public and the increments are public,
|
|
19
|
+
so a fresh calculator can be built and wound forward. That is also what makes
|
|
20
|
+
applying twice give the same answer as applying once -- incrementing an
|
|
21
|
+
existing calculator would accumulate instead of replace.
|
|
22
|
+
|
|
23
|
+
The read is the half that would break if the calculator's internals moved, which
|
|
24
|
+
is the argument for these eventually becoming methods on it.
|
|
25
|
+
"""
|
|
26
|
+
import logging
|
|
27
|
+
from typing import Any, Dict
|
|
28
|
+
|
|
29
|
+
from cooptools.expertise.expertiseSchedules import ExpertiseCalculator
|
|
30
|
+
|
|
31
|
+
logger = logging.getLogger(__name__)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def expertise_to_state(calculator: ExpertiseCalculator) -> Dict[str, Any]:
|
|
35
|
+
"""The accumulators behind an expertise calculator.
|
|
36
|
+
|
|
37
|
+
The schedule is not written -- it is definition, rebuilt from tuning by
|
|
38
|
+
whatever constructs the calculator's owner.
|
|
39
|
+
"""
|
|
40
|
+
args = calculator._expertise_args
|
|
41
|
+
return {'n_runs': args.n_runs,
|
|
42
|
+
'accumulated_s': args.accumulated_s,
|
|
43
|
+
'exp': args.exp}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def expertise_from_state(state: Dict[str, Any],
|
|
47
|
+
calculator: ExpertiseCalculator) -> ExpertiseCalculator:
|
|
48
|
+
"""A calculator on `calculator`'s schedule, wound to the saved accumulators.
|
|
49
|
+
|
|
50
|
+
Returns a new instance rather than mutating: the caller reassigns, which is
|
|
51
|
+
what makes this replace rather than accumulate.
|
|
52
|
+
"""
|
|
53
|
+
restored = ExpertiseCalculator(schedule=calculator.schedule)
|
|
54
|
+
saved = state or {}
|
|
55
|
+
restored.increment_n_runs(saved.get('n_runs', 0))
|
|
56
|
+
restored.increment_s_producting(saved.get('accumulated_s', 0))
|
|
57
|
+
restored.increment_exp(saved.get('exp', 0))
|
|
58
|
+
return restored
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Handing an object's state out and taking it back, without a base class.
|
|
2
|
+
|
|
3
|
+
`Stateful` is a structural `typing.Protocol`, so a class conforms by having
|
|
4
|
+
`to_state`/`apply_state` and importing nothing to do it. That is what lets a type
|
|
5
|
+
in one package be persisted by another with no dependency edge pointing back --
|
|
6
|
+
and it is why this belongs in a shared library rather than in whichever consumer
|
|
7
|
+
happened to need it first.
|
|
8
|
+
|
|
9
|
+
Alongside it is the conversion every state dict of any size ends up needing:
|
|
10
|
+
enums as dict keys, because JSON keys must be strings.
|
|
11
|
+
|
|
12
|
+
Conversions for a *particular* type live with that type instead --
|
|
13
|
+
`cooptools.expertise.expertise_state` is the worked example. What those functions
|
|
14
|
+
know is which of an expertise calculator's fields holds the truth and which are
|
|
15
|
+
derived; that they happen to end up as JSON is incidental, and grouping them here
|
|
16
|
+
would put them beside enum conversion, which they have nothing in common with.
|
|
17
|
+
|
|
18
|
+
What is deliberately *not* here: anything about where state is written. A file
|
|
19
|
+
format, a directory of saves, a version envelope around a whole document -- those
|
|
20
|
+
are decisions about an application, not about an object handing over its state.
|
|
21
|
+
"""
|
|
22
|
+
from cooptools.persistence.stateful import (
|
|
23
|
+
StateDict,
|
|
24
|
+
StateError,
|
|
25
|
+
StateVersionError,
|
|
26
|
+
Stateful,
|
|
27
|
+
VERSION_KEY,
|
|
28
|
+
check_version,
|
|
29
|
+
version_of,
|
|
30
|
+
)
|
|
31
|
+
from cooptools.persistence.enum_state import (
|
|
32
|
+
enum_from_state,
|
|
33
|
+
enum_keyed_from_state,
|
|
34
|
+
enum_keyed_to_state,
|
|
35
|
+
enum_to_state,
|
|
36
|
+
)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Enums across the JSON boundary.
|
|
2
|
+
|
|
3
|
+
Most state blocks of any size carry at least one enum-keyed
|
|
4
|
+
quantity map -- what a stockpile holds, what a villager carries, what a recipe
|
|
5
|
+
costs -- and JSON has no enum and no non-string key. Left to each call site, that
|
|
6
|
+
translation gets written seven ways: by `.value` in one place and `.name` in
|
|
7
|
+
another, crashing on an unknown member here and silently dropping it there.
|
|
8
|
+
|
|
9
|
+
**Names, not values.** A member's `name` is the thing the rest of the codebase
|
|
10
|
+
already treats as its stable identity (log lines, config keys, file keys). Its
|
|
11
|
+
`value` is frequently `auto()`, which renumbers the moment somebody inserts a
|
|
12
|
+
member -- so a file written by value would read back as a *different* resource
|
|
13
|
+
after an unrelated edit, with nothing to report it.
|
|
14
|
+
"""
|
|
15
|
+
import logging
|
|
16
|
+
from enum import Enum
|
|
17
|
+
from typing import Dict, Mapping, Optional, Type, TypeVar
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
E = TypeVar('E', bound=Enum)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def enum_to_state(value: Optional[Enum]) -> Optional[str]:
|
|
25
|
+
"""One enum member as its name, passing None through."""
|
|
26
|
+
return value.name if value is not None else None
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def enum_from_state(name: Optional[str], enum_cls: Type[E], *,
|
|
30
|
+
owner: str = None) -> Optional[E]:
|
|
31
|
+
"""One enum member back from its name.
|
|
32
|
+
|
|
33
|
+
An unknown name yields None with a warning rather than raising. A member
|
|
34
|
+
removed from the code between the write and the read is a real, expected event
|
|
35
|
+
during development, and it should cost the field rather than the file --
|
|
36
|
+
every caller already handles None for these (a region with no building, a
|
|
37
|
+
site with no target tier).
|
|
38
|
+
"""
|
|
39
|
+
if name is None:
|
|
40
|
+
return None
|
|
41
|
+
try:
|
|
42
|
+
return enum_cls[name]
|
|
43
|
+
except KeyError:
|
|
44
|
+
logger.warning(f"{owner or enum_cls.__name__}: '{name}' is not a member of "
|
|
45
|
+
f"{enum_cls.__name__} any more -- dropped")
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def enum_keyed_to_state(quantities: Optional[Mapping[Enum, float]]) -> Dict[str, float]:
|
|
50
|
+
"""An enum-keyed quantity map as a name-keyed one.
|
|
51
|
+
|
|
52
|
+
Written in the enum's own declaration order rather than the mapping's, so two
|
|
53
|
+
writes of the same state are byte-identical -- which is what lets a round-trip
|
|
54
|
+
test compare dicts, and lets two files diff usefully.
|
|
55
|
+
"""
|
|
56
|
+
if not quantities:
|
|
57
|
+
return {}
|
|
58
|
+
members = sorted(quantities.keys(), key=lambda member: list(type(member)).index(member))
|
|
59
|
+
return {member.name: quantities[member] for member in members}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def enum_keyed_from_state(quantities: Optional[Mapping[str, float]],
|
|
63
|
+
enum_cls: Type[E], *,
|
|
64
|
+
owner: str = None) -> Dict[E, float]:
|
|
65
|
+
"""A name-keyed quantity map back to an enum-keyed one.
|
|
66
|
+
|
|
67
|
+
Unknown names are dropped with a warning, for the reason `enum_from_state`
|
|
68
|
+
gives -- but here the loss is quantified in the log, since goods vanishing
|
|
69
|
+
from a stockpile is the kind of thing that otherwise gets noticed as a
|
|
70
|
+
balance mystery three sessions later.
|
|
71
|
+
"""
|
|
72
|
+
if not quantities:
|
|
73
|
+
return {}
|
|
74
|
+
result: Dict[E, float] = {}
|
|
75
|
+
for name, qty in quantities.items():
|
|
76
|
+
try:
|
|
77
|
+
result[enum_cls[name]] = qty
|
|
78
|
+
except KeyError:
|
|
79
|
+
logger.warning(f"{owner or enum_cls.__name__}: dropping {qty:g} of '{name}', "
|
|
80
|
+
f"which is not a member of {enum_cls.__name__} any more")
|
|
81
|
+
return result
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"""The contract every persistable thing implements: hand out your state, and
|
|
2
|
+
take it back.
|
|
3
|
+
|
|
4
|
+
Deliberately a `typing.Protocol` rather than a base class, because conformance is
|
|
5
|
+
*structural*: a class satisfies this by having the two methods, without importing
|
|
6
|
+
anything from here. That is what lets a class in one package be persisted by
|
|
7
|
+
another with no dependency edge pointing back -- which is the whole reason this
|
|
8
|
+
is a library type rather than one belonging to whoever is doing the persisting.
|
|
9
|
+
|
|
10
|
+
## State is not definition
|
|
11
|
+
|
|
12
|
+
The split this protocol exists to enforce: **state is what use changed;
|
|
13
|
+
definition is what built the object.** A production station is built from its
|
|
14
|
+
recipe -- inputs, outputs, a timer callback, a schedule -- and *holds* the
|
|
15
|
+
contents of its stores and how far its current run has got. Only the second half
|
|
16
|
+
belongs in a state dict. The first half is reconstructed by whatever built the
|
|
17
|
+
object the first time, from the same source it used then.
|
|
18
|
+
|
|
19
|
+
Getting this line wrong in either direction is expensive. Persisting definition
|
|
20
|
+
means a restored object silently pins yesterday's configuration and stops
|
|
21
|
+
tracking the file it should be reading. Persisting too little means the object
|
|
22
|
+
comes back subtly wrong, usually in a way nothing notices for a while.
|
|
23
|
+
|
|
24
|
+
## Why `apply_state` mutates instead of `from_state` constructing
|
|
25
|
+
|
|
26
|
+
Almost nothing worth persisting can be rebuilt from a dict alone. The objects
|
|
27
|
+
that carry interesting state are usually constructed with injected collaborators
|
|
28
|
+
-- providers, callbacks, the graph they read -- none of which survive a round trip
|
|
29
|
+
through JSON. A `from_state` classmethod would force every one of them to grow a
|
|
30
|
+
second construction path whose only caller is the loader.
|
|
31
|
+
|
|
32
|
+
`apply_state` matches how a restore actually goes instead: the object graph is
|
|
33
|
+
rebuilt the ordinary way, so everything exists with its collaborators already
|
|
34
|
+
wired, and the saved state is then poured into objects that are otherwise ready to
|
|
35
|
+
run.
|
|
36
|
+
|
|
37
|
+
A leaf that genuinely owns nothing may additionally offer a `from_state`
|
|
38
|
+
classmethod as a convenience. The protocol does not ask for one.
|
|
39
|
+
|
|
40
|
+
## Versioning
|
|
41
|
+
|
|
42
|
+
Each block carries its own `STATE_VERSION`, stamped under `VERSION_KEY`, and each
|
|
43
|
+
`apply_state` is responsible for reading older shapes of its own block. Versioning
|
|
44
|
+
per class rather than once for the whole file is what stops a new field on one
|
|
45
|
+
class invalidating every file in existence -- and it keeps a migration next to the
|
|
46
|
+
invariant it is migrating, which is the same reason the state lives on the object
|
|
47
|
+
rather than in a central writer.
|
|
48
|
+
"""
|
|
49
|
+
import logging
|
|
50
|
+
from typing import Any, ClassVar, Dict, Protocol, runtime_checkable
|
|
51
|
+
|
|
52
|
+
logger = logging.getLogger(__name__)
|
|
53
|
+
|
|
54
|
+
# A JSON-safe mapping: dicts, lists, strings, numbers, bools and None, and
|
|
55
|
+
# nothing else. Enum keys are written as `.name` (see enum_state.py), and any
|
|
56
|
+
# identity-bearing type -- a uuid, a graph Node -- is written as whatever stable
|
|
57
|
+
# name addresses it, never as the object.
|
|
58
|
+
StateDict = Dict[str, Any]
|
|
59
|
+
|
|
60
|
+
# Where a block's own version lives. Short because it appears once per block, and
|
|
61
|
+
# a file of any size holds a great many blocks.
|
|
62
|
+
VERSION_KEY = "v"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class StateError(ValueError):
|
|
66
|
+
"""A state dict could not be read."""
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class StateVersionError(StateError):
|
|
70
|
+
"""A state dict is a version this build does not understand."""
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@runtime_checkable
|
|
74
|
+
class Stateful(Protocol):
|
|
75
|
+
"""Hands out its own state, and takes it back.
|
|
76
|
+
|
|
77
|
+
Two things to know about checking conformance against this at runtime:
|
|
78
|
+
|
|
79
|
+
- **`isinstance` works and covers all three members**, `STATE_VERSION`
|
|
80
|
+
included, so a class that has the methods but never declared a version is
|
|
81
|
+
correctly refused.
|
|
82
|
+
- **`issubclass` raises `TypeError`** on every Python version, because this
|
|
83
|
+
protocol has a non-method member. Check an instance, never a class.
|
|
84
|
+
|
|
85
|
+
And what `isinstance` does *not* buy: `runtime_checkable` compares member
|
|
86
|
+
presence, never signatures. It catches a conformer that dropped or renamed a
|
|
87
|
+
member -- the realistic failure for a class in another package that never
|
|
88
|
+
imports this -- and says nothing about one that changed an argument.
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
STATE_VERSION: ClassVar[int]
|
|
92
|
+
|
|
93
|
+
def to_state(self) -> StateDict:
|
|
94
|
+
"""This object's play-state, as JSON-safe primitives.
|
|
95
|
+
|
|
96
|
+
Must include `VERSION_KEY`. Must not include anything reconstructed by
|
|
97
|
+
whatever builds this object -- see the module docstring on state vs.
|
|
98
|
+
definition.
|
|
99
|
+
"""
|
|
100
|
+
...
|
|
101
|
+
|
|
102
|
+
def apply_state(self, state: StateDict) -> None:
|
|
103
|
+
"""Adopts `state`, replacing whatever this object currently holds.
|
|
104
|
+
|
|
105
|
+
Called on an object that is fully constructed and wired to its
|
|
106
|
+
collaborators. Implementations replace rather than merge: applying a state
|
|
107
|
+
twice must leave the same result as applying it once, and a field absent
|
|
108
|
+
from `state` takes its documented default rather than whatever happened to
|
|
109
|
+
be there.
|
|
110
|
+
"""
|
|
111
|
+
...
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def version_of(state: StateDict) -> int:
|
|
115
|
+
"""The version stamped on a state block.
|
|
116
|
+
|
|
117
|
+
Raises rather than defaulting: an unstamped block is either corrupt or predates
|
|
118
|
+
versioning, and both are cases where guessing a version reads the rest of the
|
|
119
|
+
block under rules it was not written to.
|
|
120
|
+
"""
|
|
121
|
+
if VERSION_KEY not in state:
|
|
122
|
+
raise StateVersionError(
|
|
123
|
+
f"State block carries no '{VERSION_KEY}' -- it is corrupt, or was written "
|
|
124
|
+
f"before this block was versioned")
|
|
125
|
+
version = state[VERSION_KEY]
|
|
126
|
+
if not isinstance(version, int) or isinstance(version, bool):
|
|
127
|
+
raise StateVersionError(
|
|
128
|
+
f"State block's '{VERSION_KEY}' is {version!r}, which is not a version number")
|
|
129
|
+
return version
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def check_version(state: StateDict, *, owner: str, supported: int) -> int:
|
|
133
|
+
"""Returns the block's version, refusing one this build cannot read.
|
|
134
|
+
|
|
135
|
+
`owner` names the class in the error, because a load failure often reaches a
|
|
136
|
+
user as a single line, and "which part of this file is too new" is the whole of
|
|
137
|
+
what they can act on.
|
|
138
|
+
|
|
139
|
+
A version *below* `supported` is returned rather than refused -- that is the
|
|
140
|
+
case `apply_state` migrates, and this function has no way to know whether it
|
|
141
|
+
can.
|
|
142
|
+
"""
|
|
143
|
+
version = version_of(state)
|
|
144
|
+
if version > supported:
|
|
145
|
+
raise StateVersionError(
|
|
146
|
+
f"{owner} state is version {version}, but this build understands up to "
|
|
147
|
+
f"{supported} -- it was written by a newer build")
|
|
148
|
+
return version
|
|
@@ -61,6 +61,7 @@ cooptools/dataStore/inMemoryDataStore.py
|
|
|
61
61
|
cooptools/expertise/__init__.py
|
|
62
62
|
cooptools/expertise/expertiseArgs.py
|
|
63
63
|
cooptools/expertise/expertiseSchedules.py
|
|
64
|
+
cooptools/expertise/expertise_state.py
|
|
64
65
|
cooptools/finance/__init__.py
|
|
65
66
|
cooptools/finance/dicounted_cashflow_analysis.py
|
|
66
67
|
cooptools/finance/fund_projection.py
|
|
@@ -114,6 +115,9 @@ cooptools/pathing/waypoint.py
|
|
|
114
115
|
cooptools/pathing/waypointFollower.py
|
|
115
116
|
cooptools/pathing/demo/__init__.py
|
|
116
117
|
cooptools/pathing/demo/server.py
|
|
118
|
+
cooptools/persistence/__init__.py
|
|
119
|
+
cooptools/persistence/enum_state.py
|
|
120
|
+
cooptools/persistence/stateful.py
|
|
117
121
|
cooptools/physics/__init__.py
|
|
118
122
|
cooptools/physics/kinematic.py
|
|
119
123
|
cooptools/physics/kinematic_utils.py
|
|
@@ -160,6 +164,7 @@ tests/test_coopEnum.py
|
|
|
160
164
|
tests/test_currency.py
|
|
161
165
|
tests/test_curves.py
|
|
162
166
|
tests/test_date_utils.py
|
|
167
|
+
tests/test_expertise_state.py
|
|
163
168
|
tests/test_flow_field.py
|
|
164
169
|
tests/test_flow_field_perf.py
|
|
165
170
|
tests/test_fund_projection.py
|
|
@@ -169,6 +174,7 @@ tests/test_kinematic_utils.py
|
|
|
169
174
|
tests/test_os_manip.py
|
|
170
175
|
tests/test_pandas.py
|
|
171
176
|
tests/test_path_utils.py
|
|
177
|
+
tests/test_persistence.py
|
|
172
178
|
tests/test_printing.py
|
|
173
179
|
tests/test_qualifiers.py
|
|
174
180
|
tests/test_sectors.py
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import unittest
|
|
2
|
-
from cooptools.commandDesignPattern import CommandBatch, CommandProtocol, CommandController
|
|
2
|
+
from cooptools.commandDesignPattern import CommandBatch, CommandProtocol, CommandController, InMemoryCommandStore
|
|
3
3
|
from dataclasses import dataclass
|
|
4
4
|
from cooptools.commandDesignPattern.exceptions import ResolveBatchException, ResolveStateException
|
|
5
5
|
|
|
@@ -23,6 +23,13 @@ class Dummy_CommandThatFails(CommandProtocol):
|
|
|
23
23
|
def execute(self, state):
|
|
24
24
|
raise Exception(f"Dummy Exception")
|
|
25
25
|
|
|
26
|
+
@dataclass
|
|
27
|
+
class Dummy_PureCommand(CommandProtocol):
|
|
28
|
+
adjustment: int
|
|
29
|
+
|
|
30
|
+
def execute(self, state):
|
|
31
|
+
return Dummy_State(amount=state.amount + self.adjustment)
|
|
32
|
+
|
|
26
33
|
class Test_CommandDesignPattern(unittest.TestCase):
|
|
27
34
|
|
|
28
35
|
def test__register_commands(self):
|
|
@@ -181,3 +188,107 @@ class Test_CommandDesignPattern(unittest.TestCase):
|
|
|
181
188
|
# assert
|
|
182
189
|
self.assertRaises(ResolveStateException, lambda: controller.execute(commands=commands))
|
|
183
190
|
|
|
191
|
+
def test__execute_after_undo__discards_the_abandoned_branch(self):
|
|
192
|
+
# arrange
|
|
193
|
+
controller = CommandController(Dummy_State())
|
|
194
|
+
controller.execute([Dummy_PureCommand(adjustment=100)])
|
|
195
|
+
controller.execute([Dummy_PureCommand(adjustment=200)])
|
|
196
|
+
controller.undo()
|
|
197
|
+
|
|
198
|
+
# act
|
|
199
|
+
controller.execute([Dummy_PureCommand(adjustment=999)])
|
|
200
|
+
|
|
201
|
+
# assert
|
|
202
|
+
self.assertEqual(controller.ActiveCommands,
|
|
203
|
+
[Dummy_PureCommand(adjustment=100), Dummy_PureCommand(adjustment=999)])
|
|
204
|
+
self.assertEqual(controller.cursor, 2)
|
|
205
|
+
|
|
206
|
+
def test__execute_after_undo__live_state_matches_resolved_state(self):
|
|
207
|
+
# arrange
|
|
208
|
+
controller = CommandController(Dummy_State())
|
|
209
|
+
controller.execute([Dummy_PureCommand(adjustment=100)])
|
|
210
|
+
controller.execute([Dummy_PureCommand(adjustment=200)])
|
|
211
|
+
controller.undo()
|
|
212
|
+
|
|
213
|
+
# act
|
|
214
|
+
controller.execute([Dummy_PureCommand(adjustment=999)])
|
|
215
|
+
|
|
216
|
+
# assert
|
|
217
|
+
self.assertEqual(controller.State.amount, 1099)
|
|
218
|
+
self.assertEqual(controller.resolve(controller.cursor).amount, controller.State.amount)
|
|
219
|
+
|
|
220
|
+
def test__failed_batch__leaves_no_commands_in_the_store(self):
|
|
221
|
+
# arrange
|
|
222
|
+
controller = CommandController(Dummy_State())
|
|
223
|
+
|
|
224
|
+
# act
|
|
225
|
+
self.assertRaises(ResolveStateException, lambda: controller.execute(
|
|
226
|
+
[Dummy_PureCommand(adjustment=1000), Dummy_CommandThatFails(adjustment=500)]))
|
|
227
|
+
|
|
228
|
+
# assert
|
|
229
|
+
self.assertEqual(controller.command_store.get_commands(), [])
|
|
230
|
+
self.assertEqual(controller.cursor, 0)
|
|
231
|
+
|
|
232
|
+
def test__execute_after_failed_batch__live_state_matches_resolved_state(self):
|
|
233
|
+
# arrange
|
|
234
|
+
controller = CommandController(Dummy_State())
|
|
235
|
+
self.assertRaises(ResolveStateException, lambda: controller.execute(
|
|
236
|
+
[Dummy_PureCommand(adjustment=1000), Dummy_CommandThatFails(adjustment=500)]))
|
|
237
|
+
|
|
238
|
+
# act
|
|
239
|
+
controller.execute([Dummy_PureCommand(adjustment=7)])
|
|
240
|
+
|
|
241
|
+
# assert
|
|
242
|
+
self.assertEqual(controller.State.amount, 7)
|
|
243
|
+
self.assertEqual(controller.resolve(controller.cursor).amount, controller.State.amount)
|
|
244
|
+
|
|
245
|
+
def test__clear_cache__removes_every_cached_state(self):
|
|
246
|
+
# arrange
|
|
247
|
+
controller = CommandController(Dummy_State(), cache_interval=1)
|
|
248
|
+
controller.execute([Dummy_PureCommand(adjustment=1)])
|
|
249
|
+
controller.execute([Dummy_PureCommand(adjustment=1)])
|
|
250
|
+
self.assertTrue(len(controller.command_store.get_cached()) > 1)
|
|
251
|
+
|
|
252
|
+
# act
|
|
253
|
+
controller.command_store.clear_cache()
|
|
254
|
+
|
|
255
|
+
# assert
|
|
256
|
+
self.assertEqual(controller.command_store.get_cached(), {})
|
|
257
|
+
|
|
258
|
+
def test__execute_with_no_cached_states__does_not_raise(self):
|
|
259
|
+
# arrange
|
|
260
|
+
controller = CommandController(Dummy_State())
|
|
261
|
+
controller.command_store.clear_cache()
|
|
262
|
+
|
|
263
|
+
# act
|
|
264
|
+
state = controller.execute([Dummy_PureCommand(adjustment=42)])
|
|
265
|
+
|
|
266
|
+
# assert
|
|
267
|
+
self.assertEqual(state.amount, 42)
|
|
268
|
+
|
|
269
|
+
def test__controller_restored_from_store__recovers_state_and_cursor(self):
|
|
270
|
+
# arrange
|
|
271
|
+
store = InMemoryCommandStore()
|
|
272
|
+
original = CommandController(Dummy_State(), command_store=store)
|
|
273
|
+
original.execute([Dummy_PureCommand(adjustment=100)])
|
|
274
|
+
original.execute([Dummy_PureCommand(adjustment=200)])
|
|
275
|
+
|
|
276
|
+
# act
|
|
277
|
+
restored = CommandController(command_store=store)
|
|
278
|
+
|
|
279
|
+
# assert
|
|
280
|
+
self.assertEqual(restored.State.amount, 300)
|
|
281
|
+
self.assertEqual(restored.cursor, original.cursor)
|
|
282
|
+
|
|
283
|
+
def test__controller_restored_from_store__undo_to_origin_returns_origin_state(self):
|
|
284
|
+
# arrange
|
|
285
|
+
store = InMemoryCommandStore()
|
|
286
|
+
original = CommandController(Dummy_State(), command_store=store)
|
|
287
|
+
original.execute([Dummy_PureCommand(adjustment=100)])
|
|
288
|
+
restored = CommandController(command_store=store)
|
|
289
|
+
|
|
290
|
+
# act
|
|
291
|
+
state = restored.undo()
|
|
292
|
+
|
|
293
|
+
# assert
|
|
294
|
+
self.assertEqual(state.amount, 0)
|