asimpy 0.1.0__py3-none-any.whl → 0.2.0__py3-none-any.whl

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.
asimpy/__init__.py CHANGED
@@ -4,5 +4,5 @@ from .environment import Environment as Environment
4
4
  from .gate import Gate as Gate
5
5
  from .interrupt import Interrupt as Interrupt
6
6
  from .process import Process as Process
7
- from .queue import Queue as Queue
7
+ from .queue import Queue as Queue, PriorityQueue as PriorityQueue
8
8
  from .resource import Resource as Resource
asimpy/queue.py CHANGED
@@ -1,18 +1,33 @@
1
1
  """Queues."""
2
2
 
3
3
  from abc import ABC, abstractmethod
4
+ import heapq
5
+ from typing import TYPE_CHECKING
4
6
  from .actions import BaseAction
5
7
 
8
+ if TYPE_CHECKING:
9
+ from .environment import Environment
10
+
6
11
 
7
12
  class BaseQueue(ABC):
8
- def __init__(self, env):
13
+ """Base class for all queues."""
14
+
15
+ def __init__(self, env: "Environment"):
16
+ """
17
+ Generic queue construction.
18
+
19
+ Args:
20
+ env: simulation environment.
21
+ """
9
22
  self._env = env
10
23
  self._gets = []
11
24
 
12
25
  async def get(self):
26
+ """Get an item from the queue when one is available."""
13
27
  return await _Get(self)
14
28
 
15
29
  async def put(self, obj):
30
+ """Add an item to the queue."""
16
31
  await _Put(self, obj)
17
32
 
18
33
  @abstractmethod
@@ -28,8 +43,19 @@ class BaseQueue(ABC):
28
43
  pass
29
44
 
30
45
 
46
+ # ----------------------------------------------------------------------
47
+
48
+
31
49
  class Queue(BaseQueue):
50
+ """FIFO queue."""
51
+
32
52
  def __init__(self, env):
53
+ """
54
+ Construct queue.
55
+
56
+ Args:
57
+ env: simulation environment.
58
+ """
33
59
  super().__init__(env)
34
60
  self._items = []
35
61
 
@@ -50,7 +76,39 @@ class Queue(BaseQueue):
50
76
  # ----------------------------------------------------------------------
51
77
 
52
78
 
79
+ class PriorityQueue(BaseQueue):
80
+ """Priority queue (lower values have higher priority)."""
81
+
82
+ def __init__(self, env):
83
+ """
84
+ Construct queue.
85
+
86
+ Args:
87
+ env: simulation environment.
88
+ """
89
+ super().__init__(env)
90
+ self._items = []
91
+
92
+ def _dequeue(self):
93
+ assert len(self._items) > 0
94
+ return heapq.heappop(self._items)
95
+
96
+ def _enqueue(self, obj):
97
+ heapq.heappush(self._items, obj)
98
+
99
+ def _empty(self):
100
+ return len(self._items) == 0
101
+
102
+ def __str__(self):
103
+ return f"PriorityQueue({', '.join(str(i) for i in self._items)})"
104
+
105
+
106
+ # ----------------------------------------------------------------------
107
+
108
+
53
109
  class _Get(BaseAction):
110
+ """Get an item from the queue."""
111
+
54
112
  def __init__(self, queue):
55
113
  super().__init__(queue._env)
56
114
  self._queue = queue
@@ -71,6 +129,8 @@ class _Get(BaseAction):
71
129
 
72
130
 
73
131
  class _Put(BaseAction):
132
+ """Put an item in a queue."""
133
+
74
134
  def __init__(self, queue, obj):
75
135
  super().__init__(queue._env)
76
136
  self._queue = queue
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: asimpy
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: A simple discrete event simulator using async/await
5
5
  Author-email: Greg Wilson <gvwilson@third-bit.com>
6
6
  Maintainer-email: Greg Wilson <gvwilson@third-bit.com>
@@ -13,6 +13,7 @@ Requires-Python: >=3.13
13
13
  Provides-Extra: dev
14
14
  Requires-Dist: build>=1.4.0; extra == 'dev'
15
15
  Requires-Dist: markdown-include>=0.8.1; extra == 'dev'
16
+ Requires-Dist: mkdocs-awesome-pages-plugin>=2.10.1; extra == 'dev'
16
17
  Requires-Dist: mkdocs-material>=9.7.1; extra == 'dev'
17
18
  Requires-Dist: mkdocs>=1.6.1; extra == 'dev'
18
19
  Requires-Dist: mkdocstrings[python]>=1.0.0; extra == 'dev'
@@ -25,3 +26,16 @@ Description-Content-Type: text/markdown
25
26
  # asimpy
26
27
 
27
28
  A simple discrete event simulation framework in Python using `async`/`await`.
29
+
30
+ - [Documentation][docs]
31
+ - [Package][package]
32
+ - [Repository][repo]
33
+ - [Examples][examples]
34
+
35
+ *Thanks to the creators of [SimPy][simpy] for inspiration.*
36
+
37
+ [docs]: https://gvwilson.github.io/asimpy
38
+ [examples]: https://gvwilson.github.io/asimpy/examples/
39
+ [package]: https://pypi.org/project/asimpy/
40
+ [repo]: https://github.com/gvwilson/asimpy
41
+ [simpy]: https://simpy.readthedocs.io/
@@ -1,12 +1,12 @@
1
- asimpy/__init__.py,sha256=BJoqSPhuG6zSHkEqYh8OBZPrqbAn52EYg9Hjf236xRk,298
1
+ asimpy/__init__.py,sha256=i_T5R48aoPnE3zhHaDIWmCOmvjOb32OfmSnhhUWL37g,330
2
2
  asimpy/actions.py,sha256=G9f4Zt3Mgt1dvwOgXKbdZwmoy8Z-7pxVvre2C1J-WD0,546
3
3
  asimpy/environment.py,sha256=9KooPy0v4gERqyX7H1O7cJQyYmTA04wWT8WExrqHAGI,3098
4
4
  asimpy/gate.py,sha256=MFepFLcb8TNhzbaRAF5jJFFbprbRB-Q5phf1uMdl3LA,1381
5
5
  asimpy/interrupt.py,sha256=G925y6jHB6b6y30sbq4e3jjutZDIW7Dwg9YY0mgf24Y,572
6
6
  asimpy/process.py,sha256=6LRQRHzHI8PVt_AtckSQgsAPqv65BlUNJ8xOOVx9bXs,1550
7
- asimpy/queue.py,sha256=qQOmQfuZ_MBS5aRax7pEuWrR6swlRmI-MU0iiWD6s-0,1938
7
+ asimpy/queue.py,sha256=MVCEsDU5a2774Y5xKD0HlTZAnZwpBDeThbjFEXpkd8s,3287
8
8
  asimpy/resource.py,sha256=O18VJARLIZHbvntOxtTfYefGO5Zibz3EcsKWretIlxE,2107
9
- asimpy-0.1.0.dist-info/METADATA,sha256=oArpUAofvClqpvODhBOE_DyY2vyPWPZikHOTSQrA8MY,1044
10
- asimpy-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
11
- asimpy-0.1.0.dist-info/licenses/LICENSE.md,sha256=IjTDUvBk8xdl_n50CG1Vtk4FYdrS-C3uEYrRWAoOQqQ,1066
12
- asimpy-0.1.0.dist-info/RECORD,,
9
+ asimpy-0.2.0.dist-info/METADATA,sha256=XxrAW1lY1xpFpn5xPF00--rFEe8pZQGcOC8WjjVaTWU,1495
10
+ asimpy-0.2.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
11
+ asimpy-0.2.0.dist-info/licenses/LICENSE.md,sha256=IjTDUvBk8xdl_n50CG1Vtk4FYdrS-C3uEYrRWAoOQqQ,1066
12
+ asimpy-0.2.0.dist-info/RECORD,,
File without changes