tasking 1.0.2__tar.gz → 2.0.0__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 (40) hide show
  1. tasking-2.0.0/HISTORY.rst +15 -0
  2. {tasking-1.0.2 → tasking-2.0.0}/PKG-INFO +7 -5
  3. {tasking-1.0.2 → tasking-2.0.0}/README.rst +1 -1
  4. {tasking-1.0.2 → tasking-2.0.0}/doc/conf.py +7 -2
  5. {tasking-1.0.2 → tasking-2.0.0}/doc/readme.rst +1 -1
  6. {tasking-1.0.2 → tasking-2.0.0}/pyproject.toml +8 -3
  7. {tasking-1.0.2 → tasking-2.0.0}/requirements.txt +1 -0
  8. {tasking-1.0.2 → tasking-2.0.0}/src/tasking/__init__.py +4 -2
  9. {tasking-1.0.2 → tasking-2.0.0}/src/tasking/scheduler.py +14 -13
  10. {tasking-1.0.2 → tasking-2.0.0}/src/tasking/task.py +32 -7
  11. {tasking-1.0.2 → tasking-2.0.0}/src/tasking/version.py +2 -2
  12. {tasking-1.0.2 → tasking-2.0.0}/src/tasking.egg-info/PKG-INFO +7 -5
  13. {tasking-1.0.2 → tasking-2.0.0}/src/tasking.egg-info/requires.txt +1 -0
  14. {tasking-1.0.2 → tasking-2.0.0}/test/test_scheduler.py +87 -13
  15. tasking-2.0.0/test/test_task.py +53 -0
  16. tasking-1.0.2/HISTORY.rst +0 -8
  17. tasking-1.0.2/test/test_task.py +0 -21
  18. {tasking-1.0.2 → tasking-2.0.0}/AUTHORS.rst +0 -0
  19. {tasking-1.0.2 → tasking-2.0.0}/CONTRIBUTING.rst +0 -0
  20. {tasking-1.0.2 → tasking-2.0.0}/LICENSE +0 -0
  21. {tasking-1.0.2 → tasking-2.0.0}/doc/Makefile +0 -0
  22. {tasking-1.0.2 → tasking-2.0.0}/doc/authors.rst +0 -0
  23. {tasking-1.0.2 → tasking-2.0.0}/doc/badges/listing.rst +0 -0
  24. {tasking-1.0.2 → tasking-2.0.0}/doc/biblio/biblio.rst +0 -0
  25. {tasking-1.0.2 → tasking-2.0.0}/doc/contributing.rst +0 -0
  26. {tasking-1.0.2 → tasking-2.0.0}/doc/history.rst +0 -0
  27. {tasking-1.0.2 → tasking-2.0.0}/doc/index.rst +0 -0
  28. {tasking-1.0.2 → tasking-2.0.0}/doc/installation.rst +0 -0
  29. {tasking-1.0.2 → tasking-2.0.0}/doc/make.bat +0 -0
  30. {tasking-1.0.2 → tasking-2.0.0}/doc/management.rst +0 -0
  31. {tasking-1.0.2 → tasking-2.0.0}/doc/usage.rst +0 -0
  32. {tasking-1.0.2 → tasking-2.0.0}/doc/user/index.rst +0 -0
  33. {tasking-1.0.2 → tasking-2.0.0}/doc/user/overview.rst +0 -0
  34. {tasking-1.0.2 → tasking-2.0.0}/requirements_minimal.txt +0 -0
  35. {tasking-1.0.2 → tasking-2.0.0}/setup.cfg +0 -0
  36. {tasking-1.0.2 → tasking-2.0.0}/src/tasking.egg-info/SOURCES.txt +0 -0
  37. {tasking-1.0.2 → tasking-2.0.0}/src/tasking.egg-info/dependency_links.txt +0 -0
  38. {tasking-1.0.2 → tasking-2.0.0}/src/tasking.egg-info/top_level.txt +0 -0
  39. {tasking-1.0.2 → tasking-2.0.0}/test/conftest.py +0 -0
  40. {tasking-1.0.2 → tasking-2.0.0}/test/test_packaging.py +0 -0
@@ -0,0 +1,15 @@
1
+ =======
2
+ History
3
+ =======
4
+
5
+ unreleased
6
+ ----------
7
+
8
+ * Breaking: task ``update`` (and ``TaskCached.need_update``) now receive
9
+ ``prev_clock`` after ``clock``: clock of previous call of the same task
10
+ (scheduler clock when task was added, on first call).
11
+
12
+ creation (2021-09-13)
13
+ ------------------------
14
+
15
+ * First release on PyPI.
@@ -1,14 +1,14 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: tasking
3
- Version: 1.0.2
3
+ Version: 2.0.0
4
4
  Summary: Perform time simulation, using scheduled tasks to update current state
5
5
  Author-email: revesansparole <revesansparole@gmail.com>
6
6
  Maintainer-email: revesansparole <revesansparole@gmail.com>, Jerome Chopard <revesansparole@gmail.com>
7
7
  License: cc_by_nc
8
8
  Project-URL: repository, https://gitlab.com/revesansparole/tasking
9
- Project-URL: pip, https://pypi.org/project/tasking/1.0.2/
9
+ Project-URL: pip, https://pypi.org/project/tasking/2.0.0/
10
10
  Project-URL: conda, https://anaconda.org/revesansparole/tasking
11
- Project-URL: doc, https://revesansparole.gitlab.io/tasking/
11
+ Project-URL: documentation, https://revesansparole.gitlab.io/tasking/
12
12
  Project-URL: pypi, https://badge.fury.io/py/tasking
13
13
  Classifier: Development Status :: 2 - Pre-Alpha
14
14
  Classifier: Intended Audience :: Developers
@@ -31,12 +31,14 @@ Requires-Dist: sphinx; extra == "doc"
31
31
  Requires-Dist: sphinx-gallery; extra == "doc"
32
32
  Requires-Dist: sphinx-rtd-theme; extra == "doc"
33
33
  Provides-Extra: dvlpt
34
+ Requires-Dist: black; extra == "dvlpt"
34
35
  Requires-Dist: twine; extra == "dvlpt"
35
36
  Provides-Extra: test
36
37
  Requires-Dist: coverage; extra == "test"
37
38
  Requires-Dist: pytest; extra == "test"
38
39
  Requires-Dist: pytest-cov; extra == "test"
39
40
  Requires-Dist: pytest-mock; extra == "test"
41
+ Dynamic: license-file
40
42
 
41
43
  ========================
42
44
  tasking
@@ -46,7 +48,7 @@ tasking
46
48
 
47
49
  .. image:: https://revesansparole.gitlab.io/tasking/_images/badge_pkging_pip.svg
48
50
  :alt: PyPI version
49
- :target: https://pypi.org/project/tasking/1.0.2/
51
+ :target: https://pypi.org/project/tasking/2.0.0/
50
52
 
51
53
  .. image:: https://revesansparole.gitlab.io/tasking/_images/badge_pkging_conda.svg
52
54
  :alt: Conda version
@@ -6,7 +6,7 @@ tasking
6
6
 
7
7
  .. image:: https://revesansparole.gitlab.io/tasking/_images/badge_pkging_pip.svg
8
8
  :alt: PyPI version
9
- :target: https://pypi.org/project/tasking/1.0.2/
9
+ :target: https://pypi.org/project/tasking/2.0.0/
10
10
 
11
11
  .. image:: https://revesansparole.gitlab.io/tasking/_images/badge_pkging_conda.svg
12
12
  :alt: Conda version
@@ -89,9 +89,9 @@ copyright = "2021, tasking"
89
89
  #
90
90
 
91
91
  # The short X.Y version.
92
- version = "1.0.2"
92
+ version = "2.0.0"
93
93
  # The full version, including alpha/beta/rc tags.
94
- release = "1.0.2"
94
+ release = "2.0.0"
95
95
 
96
96
 
97
97
  exclude_patterns = ['build', 'dist']
@@ -173,3 +173,8 @@ warnings.filterwarnings("ignore", category=UserWarning,
173
173
  ' non-GUI backend, so cannot show the figure.')
174
174
 
175
175
  # #}
176
+ # {# pkglts, glabpkg_dev, after sphinx
177
+ html_css_files = [
178
+ 'css/sphinx_glr_extra.css',
179
+ ]
180
+ # #}
@@ -5,7 +5,7 @@ Overview
5
5
 
6
6
  .. image:: https://revesansparole.gitlab.io/tasking/_images/badge_pkging_pip.svg
7
7
  :alt: PyPI version
8
- :target: https://pypi.org/project/tasking/1.0.2/
8
+ :target: https://pypi.org/project/tasking/2.0.0/
9
9
 
10
10
 
11
11
  .. image:: https://revesansparole.gitlab.io/tasking/_images/badge_pkging_conda.svg
@@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta" # we don't know if we need it
8
8
  # {# pkglts, pyproject.project
9
9
  [project] # https://packaging.python.org/en/latest/specifications/pyproject-toml/
10
10
  name = "tasking"
11
- version = "1.0.2"
11
+ version = "2.0.0"
12
12
  description = "Perform time simulation, using scheduled tasks to update current state"
13
13
  readme = "README.rst"
14
14
  requires-python = ">= 3.7"
@@ -46,6 +46,7 @@ doc = [
46
46
  "sphinx-rtd-theme",
47
47
  ]
48
48
  dvlpt = [
49
+ "black",
49
50
  "twine",
50
51
  ]
51
52
  test = [
@@ -58,9 +59,9 @@ test = [
58
59
 
59
60
  [project.urls]
60
61
  repository = "https://gitlab.com/revesansparole/tasking"
61
- pip = "https://pypi.org/project/tasking/1.0.2/"
62
+ pip = "https://pypi.org/project/tasking/2.0.0/"
62
63
  conda = "https://anaconda.org/revesansparole/tasking"
63
- doc = "https://revesansparole.gitlab.io/tasking/"
64
+ documentation = "https://revesansparole.gitlab.io/tasking/"
64
65
  pypi = "https://badge.fury.io/py/tasking"
65
66
 
66
67
 
@@ -73,6 +74,10 @@ where = ["src"]
73
74
  [tool.setuptools.package-data]
74
75
  "*" = ['*.json', '*.ini', '*.csv', '*.rst', '*.svg', ]
75
76
 
77
+ # #}
78
+ # {# pkglts, glabpkg_dev, after pyproject.project
79
+ [tool.black]
80
+ line-length = 120
76
81
  # #}
77
82
  # {# pkglts, test, after pyproject.project
78
83
  [tool.pytest.ini_options]
@@ -9,6 +9,7 @@ sphinx-gallery
9
9
  sphinx-rtd-theme
10
10
 
11
11
  # dvlpt
12
+ black
12
13
  twine
13
14
 
14
15
  # install
@@ -12,8 +12,10 @@ __version__ = version.__version__
12
12
 
13
13
  from datetime import timedelta
14
14
 
15
- from .scheduler import Scheduler
16
- from .task import Task, TaskRecord
15
+ from .scheduler import Scheduler as Scheduler
16
+ from .task import Task as Task
17
+ from .task import TaskCached as TaskCached
18
+ from .task import TaskRecord as TaskRecord
17
19
 
18
20
  one_day = timedelta(days=1)
19
21
  one_hour = timedelta(hours=1)
@@ -1,6 +1,7 @@
1
1
  """
2
2
  Simple scheduler to call task repeatedly.
3
3
  """
4
+
4
5
  import bisect
5
6
 
6
7
 
@@ -54,13 +55,13 @@ class Scheduler:
54
55
  UserWarning if none or multiple tasks found.
55
56
 
56
57
  Args:
57
- tasks (list): list of (clock, task)
58
+ tasks (list): list of (clock, prev_clock, task)
58
59
  task_cls (Task): task class
59
60
 
60
61
  Returns:
61
62
  (int): index of task in list
62
63
  """
63
- indices = [i for i, (t0, task) in enumerate(tasks) if isinstance(task, task_cls)]
64
+ indices = [i for i, (t0, prev, task) in enumerate(tasks) if isinstance(task, task_cls)]
64
65
  if len(indices) == 0:
65
66
  raise UserWarning(f"No task of type {task_cls} found in queue")
66
67
 
@@ -91,17 +92,17 @@ class Scheduler:
91
92
  """
92
93
  # compute list of tasks sorted by priority for reorganising pids
93
94
  # after editing queue
94
- tasks = [(t0, tsk) for t0, priority, tsk in sorted(self._queue, key=lambda tup: tup[1])]
95
+ tasks = [(t0, prev, tsk) for t0, priority, prev, tsk in sorted(self._queue, key=lambda tup: tup[1])]
95
96
 
96
97
  if after is not None:
97
98
  ref_ind = self._fetch_task(tasks, after)
98
- ref_t0, ref_task = tasks[ref_ind]
99
- tasks.insert(ref_ind + 1, (ref_t0, task))
99
+ ref_t0, _, _ = tasks[ref_ind]
100
+ tasks.insert(ref_ind + 1, (ref_t0, self._current_clock, task))
100
101
 
101
102
  elif before is not None:
102
103
  ref_ind = self._fetch_task(tasks, before)
103
- ref_t0, ref_task = tasks[ref_ind]
104
- tasks.insert(ref_ind, (ref_t0, task))
104
+ ref_t0, _, _ = tasks[ref_ind]
105
+ tasks.insert(ref_ind, (ref_t0, self._current_clock, task))
105
106
 
106
107
  else:
107
108
  if t0 is None:
@@ -109,12 +110,12 @@ class Scheduler:
109
110
  elif t0 < self._current_clock:
110
111
  raise UserWarning("You cannot start a task before actual simulation time")
111
112
 
112
- tasks.append((t0, task))
113
+ tasks.append((t0, self._current_clock, task))
113
114
 
114
115
  # reconstruct queue
115
116
  self._queue = []
116
- for priority, (t0, tsk) in enumerate(tasks):
117
- bisect.insort(self._queue, (t0, priority, tsk))
117
+ for priority, (t0, prev, tsk) in enumerate(tasks):
118
+ bisect.insort(self._queue, (t0, priority, prev, tsk))
118
119
 
119
120
  def run(self, clock_end):
120
121
  """Run simulation until end of time.
@@ -134,13 +135,13 @@ class Scheduler:
134
135
 
135
136
  while self._queue and self._queue[0][0] <= clock_end:
136
137
  # retrieve most pressing process to evaluate
137
- self._current_clock, priority, task = self._queue.pop(0)
138
+ self._current_clock, priority, prev_clock, task = self._queue.pop(0)
138
139
 
139
140
  # update world
140
- next_eval = task.update(self._current_clock, self._state, self._params)
141
+ next_eval = task.update(self._current_clock, prev_clock, self._state, self._params)
141
142
 
142
143
  # insert back task for next evaluation
143
144
  if next_eval is not None:
144
- bisect.insort(self._queue, (next_eval, priority, task))
145
+ bisect.insort(self._queue, (next_eval, priority, self._current_clock, task))
145
146
 
146
147
  return self._current_clock, self._state
@@ -4,16 +4,18 @@ Set of classes to be executed repeatedly
4
4
 
5
5
 
6
6
  class Task:
7
- """Base class for all task that will change the state of a simulation.
8
- """
7
+ """Base class for all task that will change the state of a simulation."""
9
8
 
10
- def update(self, clock, state, params):
9
+ def update(self, clock, prev_clock, state, params):
11
10
  """Update the state of the model.
12
11
 
13
12
  Notes: state is modified in place.
14
13
 
15
14
  Args:
16
15
  clock (Union[int, datetime]): current 'time'
16
+ prev_clock (Union[int, datetime]): 'time' of previous call of this
17
+ task (scheduler clock when task was added, on
18
+ first call)
17
19
  state (any): current state of the model
18
20
  params (any): extra parameters, i.e. values that are not modified
19
21
  throughout call to different tasks
@@ -22,12 +24,11 @@ class Task:
22
24
  (Union[int, datetime, None]): next time this task must be performed.
23
25
  if None, this task won't be evaluated anymore.
24
26
  """
25
- return None
27
+ return
26
28
 
27
29
 
28
30
  class TaskRecord(Task):
29
- """Simple task to store some information on the run.
30
- """
31
+ """Simple task to store some information on the run."""
31
32
 
32
33
  def __init__(self):
33
34
  super().__init__()
@@ -43,7 +44,7 @@ class TaskRecord(Task):
43
44
  if len(self._mem) > 0:
44
45
  return self._mem[-1]
45
46
  else:
46
- return None
47
+ return
47
48
 
48
49
  def append(self, data):
49
50
  """Append some data in memory.
@@ -63,3 +64,27 @@ class TaskRecord(Task):
63
64
  (list)
64
65
  """
65
66
  return self._mem
67
+
68
+
69
+ class TaskCached(Task):
70
+ """Helper task that cache its result and can be invalidated.
71
+
72
+ Require a duration attribute in state.
73
+ """
74
+
75
+ def __init__(self):
76
+ super().__init__()
77
+ self._need_update = True
78
+
79
+ def invalidate(self):
80
+ self._need_update = True
81
+
82
+ def update(self, clock, prev_clock, state, params):
83
+ if self._need_update:
84
+ self.need_update(clock, prev_clock, state, params)
85
+ self._need_update = False
86
+
87
+ return clock + state.duration # TODO don't rely on state.duration
88
+
89
+ def need_update(self, clock, prev_clock, state, params):
90
+ pass
@@ -6,13 +6,13 @@ Do not edit this file, use 'version' section of config.
6
6
  # {# pkglts, version
7
7
  # -*- coding: utf-8 -*-
8
8
 
9
- MAJOR = 1
9
+ MAJOR = 2
10
10
  """(int) Version major component."""
11
11
 
12
12
  MINOR = 0
13
13
  """(int) Version minor component."""
14
14
 
15
- POST = 2
15
+ POST = 0
16
16
  """(int) Version post or bugfix component."""
17
17
 
18
18
  __version__ = f"{MAJOR:d}.{MINOR:d}.{POST:d}"
@@ -1,14 +1,14 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: tasking
3
- Version: 1.0.2
3
+ Version: 2.0.0
4
4
  Summary: Perform time simulation, using scheduled tasks to update current state
5
5
  Author-email: revesansparole <revesansparole@gmail.com>
6
6
  Maintainer-email: revesansparole <revesansparole@gmail.com>, Jerome Chopard <revesansparole@gmail.com>
7
7
  License: cc_by_nc
8
8
  Project-URL: repository, https://gitlab.com/revesansparole/tasking
9
- Project-URL: pip, https://pypi.org/project/tasking/1.0.2/
9
+ Project-URL: pip, https://pypi.org/project/tasking/2.0.0/
10
10
  Project-URL: conda, https://anaconda.org/revesansparole/tasking
11
- Project-URL: doc, https://revesansparole.gitlab.io/tasking/
11
+ Project-URL: documentation, https://revesansparole.gitlab.io/tasking/
12
12
  Project-URL: pypi, https://badge.fury.io/py/tasking
13
13
  Classifier: Development Status :: 2 - Pre-Alpha
14
14
  Classifier: Intended Audience :: Developers
@@ -31,12 +31,14 @@ Requires-Dist: sphinx; extra == "doc"
31
31
  Requires-Dist: sphinx-gallery; extra == "doc"
32
32
  Requires-Dist: sphinx-rtd-theme; extra == "doc"
33
33
  Provides-Extra: dvlpt
34
+ Requires-Dist: black; extra == "dvlpt"
34
35
  Requires-Dist: twine; extra == "dvlpt"
35
36
  Provides-Extra: test
36
37
  Requires-Dist: coverage; extra == "test"
37
38
  Requires-Dist: pytest; extra == "test"
38
39
  Requires-Dist: pytest-cov; extra == "test"
39
40
  Requires-Dist: pytest-mock; extra == "test"
41
+ Dynamic: license-file
40
42
 
41
43
  ========================
42
44
  tasking
@@ -46,7 +48,7 @@ tasking
46
48
 
47
49
  .. image:: https://revesansparole.gitlab.io/tasking/_images/badge_pkging_pip.svg
48
50
  :alt: PyPI version
49
- :target: https://pypi.org/project/tasking/1.0.2/
51
+ :target: https://pypi.org/project/tasking/2.0.0/
50
52
 
51
53
  .. image:: https://revesansparole.gitlab.io/tasking/_images/badge_pkging_conda.svg
52
54
  :alt: Conda version
@@ -5,6 +5,7 @@ sphinx-gallery
5
5
  sphinx-rtd-theme
6
6
 
7
7
  [dvlpt]
8
+ black
8
9
  twine
9
10
 
10
11
  [test]
@@ -21,7 +21,7 @@ def test_scheduler_pass_state_and_params_to_tasks():
21
21
  task_args = []
22
22
 
23
23
  class MyTask(Task):
24
- def update(self, t, state, params):
24
+ def update(self, t, prev_t, state, params):
25
25
  task_args.append((t, state, params))
26
26
  return t + 1
27
27
 
@@ -54,12 +54,12 @@ def test_scheduler_runs_until_t_end():
54
54
  eval_nb = dict(d=0, h=0)
55
55
 
56
56
  class TaskD(Task):
57
- def update(self, t, state, params):
57
+ def update(self, t, prev_t, state, params):
58
58
  eval_nb['d'] += 1
59
59
  return t + 6
60
60
 
61
61
  class TaskH(Task):
62
- def update(self, t, state, params):
62
+ def update(self, t, prev_t, state, params):
63
63
  eval_nb['h'] += 1
64
64
  return t + 1
65
65
 
@@ -78,7 +78,7 @@ def test_scheduler_runs_until_t_end():
78
78
 
79
79
  def test_scheduler_returns_last_time_and_last_state():
80
80
  class MyTask(Task):
81
- def update(self, t, state, params):
81
+ def update(self, t, prev_t, state, params):
82
82
  state['tugudu'] = t
83
83
  return t + 1
84
84
 
@@ -148,17 +148,17 @@ def test_scheduler_add_task_handles_after():
148
148
  task_args = []
149
149
 
150
150
  class T0(Task):
151
- def update(self, t, state, params):
151
+ def update(self, t, prev_t, state, params):
152
152
  task_args.append(0)
153
153
  return t + 1
154
154
 
155
155
  class T1(Task):
156
- def update(self, t, state, params):
156
+ def update(self, t, prev_t, state, params):
157
157
  task_args.append(1)
158
158
  return t + 1
159
159
 
160
160
  class T2(Task):
161
- def update(self, t, state, params):
161
+ def update(self, t, prev_t, state, params):
162
162
  task_args.append(2)
163
163
  return t + 1
164
164
 
@@ -181,17 +181,17 @@ def test_scheduler_add_task_handles_before():
181
181
  task_args = []
182
182
 
183
183
  class T0(Task):
184
- def update(self, t, state, params):
184
+ def update(self, t, prev_t, state, params):
185
185
  task_args.append(0)
186
186
  return t + 1
187
187
 
188
188
  class T1(Task):
189
- def update(self, t, state, params):
189
+ def update(self, t, prev_t, state, params):
190
190
  task_args.append(1)
191
191
  return t + 1
192
192
 
193
193
  class T2(Task):
194
- def update(self, t, state, params):
194
+ def update(self, t, prev_t, state, params):
195
195
  task_args.append(2)
196
196
  return t + 1
197
197
 
@@ -214,17 +214,17 @@ def test_scheduler_respects_order_of_process_insertion():
214
214
  task_args = []
215
215
 
216
216
  class T0(Task):
217
- def update(self, t, state, params):
217
+ def update(self, t, prev_t, state, params):
218
218
  task_args.append(0)
219
219
  return t + 1
220
220
 
221
221
  class T1(Task):
222
- def update(self, t, state, params):
222
+ def update(self, t, prev_t, state, params):
223
223
  task_args.append(1)
224
224
  return t + 1
225
225
 
226
226
  class T2(Task):
227
- def update(self, t, state, params):
227
+ def update(self, t, prev_t, state, params):
228
228
  task_args.append(2)
229
229
  return t + 1
230
230
 
@@ -243,3 +243,77 @@ def test_scheduler_respects_order_of_process_insertion():
243
243
 
244
244
  _ = sch.run(3)
245
245
  assert task_args == [1, 2, 1, 2, 0, 1, 2]
246
+
247
+
248
+ def test_scheduler_pass_clock_at_add_as_prev_clock_on_first_call():
249
+ calls = []
250
+
251
+ class MyTask(Task):
252
+ def __init__(self, name):
253
+ super().__init__()
254
+ self.name = name
255
+
256
+ def update(self, t, prev_t, state, params):
257
+ calls.append((self.name, t, prev_t))
258
+ return None
259
+
260
+ sch = Scheduler(2, {})
261
+ sch.add_task(MyTask("a"))
262
+ sch.add_task(MyTask("b"), t0=5)
263
+ sch.run(10)
264
+ sch.add_task(MyTask("c"))
265
+ sch.run(10)
266
+
267
+ assert calls == [("a", 2, 2), ("b", 5, 2), ("c", 5, 5)]
268
+
269
+
270
+ def test_scheduler_pass_previous_call_clock_of_same_task():
271
+ calls = {"a": [], "b": []}
272
+
273
+ class MyTask(Task):
274
+ def __init__(self, name, dt):
275
+ super().__init__()
276
+ self.name = name
277
+ self.dt = dt
278
+
279
+ def update(self, t, prev_t, state, params):
280
+ calls[self.name].append((t, prev_t))
281
+ return t + self.dt
282
+
283
+ sch = Scheduler(0, {})
284
+ sch.add_task(MyTask("a", 1))
285
+ sch.add_task(MyTask("b", 3))
286
+ sch.run(6)
287
+
288
+ assert calls["a"] == [(0, 0), (1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5)]
289
+ assert calls["b"] == [(0, 0), (3, 0), (6, 3)]
290
+
291
+
292
+ def test_scheduler_add_task_preserves_prev_clock_of_other_tasks():
293
+ calls = []
294
+
295
+ class T0(Task):
296
+ def update(self, t, prev_t, state, params):
297
+ calls.append((0, t, prev_t))
298
+ return t + 2
299
+
300
+ class T1(Task):
301
+ def update(self, t, prev_t, state, params):
302
+ calls.append((1, t, prev_t))
303
+ return None
304
+
305
+ class T2(Task):
306
+ def update(self, t, prev_t, state, params):
307
+ calls.append((2, t, prev_t))
308
+ return None
309
+
310
+ sch = Scheduler(0, {})
311
+ sch.add_task(T0())
312
+ sch.run(2)
313
+ calls.clear()
314
+
315
+ sch.add_task(T1(), after=T0)
316
+ sch.add_task(T2(), before=T0)
317
+ sch.run(4)
318
+
319
+ assert calls == [(2, 4, 2), (0, 4, 2), (1, 4, 2)]
@@ -0,0 +1,53 @@
1
+ from tasking.task import TaskCached, TaskRecord
2
+
3
+
4
+ class TotoState:
5
+ duration: int = 1
6
+
7
+
8
+ class Toto(TaskCached):
9
+ def __init__(self):
10
+ super().__init__()
11
+ self.a = -1
12
+
13
+ def need_update(self, t, prev_t, state, params):
14
+ self.a = t
15
+
16
+
17
+ def test_task_record_stores_data():
18
+ task = TaskRecord()
19
+ task.append(1)
20
+ task.append('a')
21
+ task.append(3.14159)
22
+
23
+ assert tuple(task.retrieve()) == (1, 'a', 3.14159)
24
+
25
+
26
+ def test_task_record_returns_last_entry():
27
+ task = TaskRecord()
28
+ assert task.last_entry() is None
29
+
30
+ task.append(1)
31
+ assert task.last_entry() == 1
32
+
33
+ task.append((1, 2))
34
+ assert task.last_entry() == (1, 2)
35
+
36
+
37
+ def test_task_cached_only_update_when_required():
38
+ t = Toto()
39
+ assert t.a == -1
40
+
41
+ state = TotoState()
42
+ next_t = t.update(0, None, state, None)
43
+ assert next_t == 1
44
+ assert t.a == 0
45
+
46
+ next_t = t.update(next_t, None, state, None)
47
+ assert next_t == 2
48
+ assert t.a == 0
49
+
50
+ t.invalidate()
51
+ next_t = t.update(next_t, None, state, None)
52
+ assert next_t == 3
53
+ assert t.a == 2
tasking-1.0.2/HISTORY.rst DELETED
@@ -1,8 +0,0 @@
1
- =======
2
- History
3
- =======
4
-
5
- creation (2021-09-13)
6
- ------------------------
7
-
8
- * First release on PyPI.
@@ -1,21 +0,0 @@
1
- from tasking.task import TaskRecord
2
-
3
-
4
- def test_task_record_stores_data():
5
- task = TaskRecord()
6
- task.append(1)
7
- task.append('a')
8
- task.append(3.14159)
9
-
10
- assert tuple(task.retrieve()) == (1, 'a', 3.14159)
11
-
12
-
13
- def test_task_record_returns_last_entry():
14
- task = TaskRecord()
15
- assert task.last_entry() is None
16
-
17
- task.append(1)
18
- assert task.last_entry() == 1
19
-
20
- task.append((1, 2))
21
- assert task.last_entry() == (1, 2)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes