Skip to content

Commit dd92ce2

Browse files
authored
docs: All doc files ported to .md (#332)
1 parent 6ea6e2d commit dd92ce2

18 files changed

Lines changed: 223 additions & 631 deletions

AUTHORS.rst

Lines changed: 0 additions & 24 deletions
This file was deleted.

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Please see [docs/contributing.md](docs/contributing).

README.rst renamed to README.md

Lines changed: 84 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,14 @@
1-
===================
2-
Python StateMachine
3-
===================
1+
# Python StateMachine
42

3+
[![pypi](https://img.shields.io/pypi/v/python-statemachine.svg)](https://pypi.python.org/pypi/python-statemachine)
4+
[![downloads](https://img.shields.io/pypi/dm/python-statemachine.svg)](https://pypi.python.org/pypi/python-statemachine)
5+
[![build status](https://github.com/fgmacedo/python-statemachine/actions/workflows/python-package.yml/badge.svg?branch=develop)](https://github.com/fgmacedo/python-statemachine/actions/workflows/python-package.yml?query=branch%3Adevelop)
6+
[![Coverage report](https://codecov.io/gh/fgmacedo/python-statemachine/branch/develop/graph/badge.svg)](https://codecov.io/gh/fgmacedo/python-statemachine)
7+
[![Documentation Status](https://readthedocs.org/projects/python-statemachine/badge/?version=latest)](https://python-statemachine.readthedocs.io/en/latest/?badge=latest)
8+
[![GitHub commits since last release (main)](https://img.shields.io/github/commits-since/fgmacedo/python-statemachine/main/develop)](https://github.com/fgmacedo/python-statemachine/compare/main...develop)
59

6-
.. image:: https://img.shields.io/pypi/v/python-statemachine.svg
7-
:target: https://pypi.python.org/pypi/python-statemachine
810

9-
.. image:: https://img.shields.io/pypi/dm/python-statemachine.svg
10-
:target: https://pypi.python.org/pypi/python-statemachine
11-
12-
.. image:: https://github.com/fgmacedo/python-statemachine/actions/workflows/python-package.yml/badge.svg?branch=develop
13-
:target: https://github.com/fgmacedo/python-statemachine/actions/workflows/python-package.yml?query=branch%3Adevelop
14-
:alt: Build status
15-
16-
.. image:: https://codecov.io/gh/fgmacedo/python-statemachine/branch/develop/graph/badge.svg
17-
:target: https://codecov.io/gh/fgmacedo/python-statemachine
18-
:alt: Coverage report
19-
20-
.. image:: https://readthedocs.org/projects/python-statemachine/badge/?version=latest
21-
:target: https://python-statemachine.readthedocs.io/en/latest/?badge=latest
22-
:alt: Documentation Status
23-
24-
.. image:: https://img.shields.io/github/commits-since/fgmacedo/python-statemachine/main/develop
25-
:alt: GitHub commits since last release (main)
26-
27-
28-
Python `finite-state machines <https://en.wikipedia.org/wiki/Finite-state_machine>`_ made easy.
11+
Python [finite-state machines](https://en.wikipedia.org/wiki/Finite-state_machine) made easy.
2912

3013

3114
* Free software: MIT license
@@ -52,26 +35,22 @@ A few reasons why you may consider using it:
5235
* 🚫 Prevents common mistakes and ensures that your system stays in a valid state at all times.
5336

5437

55-
Getting started
56-
===============
38+
## Getting started
5739

5840

5941
To install Python State Machine, run this command in your terminal:
6042

61-
.. code-block:: console
62-
63-
$ pip install python-statemachine
43+
pip install python-statemachine
6444

65-
To generate diagrams from your machines, you'll also need ``pydot`` and ``Graphviz``. You can
66-
install this library already with ``pydot`` dependency using the `extras` install option. See
45+
To generate diagrams from your machines, you'll also need `pydot` and `Graphviz`. You can
46+
install this library already with `pydot` dependency using the `extras` install option. See
6747
our docs for more details.
6848

69-
.. code-block:: console
70-
71-
$ pip install python-statemachine[diagrams]
49+
pip install python-statemachine[diagrams]
7250

7351
Define your state machine:
7452

53+
```py
7554
>>> from statemachine import StateMachine, State
7655

7756
>>> class TrafficLightMachine(StateMachine):
@@ -102,36 +81,53 @@ Define your state machine:
10281
... def on_exit_red(self):
10382
... print("Go ahead!")
10483

84+
```
10585

10686
You can now create an instance:
10787

88+
```py
10889
>>> traffic_light = TrafficLightMachine()
10990

91+
```
92+
11093
Then start sending events:
11194

95+
```py
11296
>>> traffic_light.cycle()
11397
'Running cycle from green to yellow'
11498

99+
```
100+
115101
You can inspect the current state:
116102

103+
```py
117104
>>> traffic_light.current_state.id
118105
'yellow'
119106

107+
```
108+
120109
Or get a complete state repr for debugging purposes:
121110

111+
```py
122112
>>> traffic_light.current_state
123113
State('Yellow', id='yellow', value='yellow', initial=False, final=False)
124114

115+
```
116+
125117
The ``State`` instance can also be checked by equality:
126118

119+
```py
127120
>>> traffic_light.current_state == TrafficLightMachine.yellow
128121
True
129122

130123
>>> traffic_light.current_state == traffic_light.yellow
131124
True
132125

126+
```
127+
133128
But for your convenience, can easily ask if a state is active at any time:
134129

130+
```py
135131
>>> traffic_light.green.is_active
136132
False
137133

@@ -141,56 +137,75 @@ True
141137
>>> traffic_light.red.is_active
142138
False
143139

140+
```
141+
144142
Easily iterate over all states:
145143

144+
```py
146145
>>> [s.id for s in traffic_light.states]
147146
['green', 'red', 'yellow']
148147

148+
```
149+
149150
Or over events:
150151

152+
```py
151153
>>> [t.name for t in traffic_light.events]
152154
['cycle', 'go', 'slowdown', 'stop']
153155

156+
```
157+
154158
Call an event by its name:
155159

160+
```py
156161
>>> traffic_light.cycle()
157162
Don't move.
158163
'Running cycle from yellow to red'
159164

165+
```
160166
Or send an event with the event name:
161167

168+
```py
162169
>>> traffic_light.send('cycle')
163170
Go ahead!
164171
'Running cycle from red to green'
165172

166173
>>> traffic_light.green.is_active
167174
True
168175

176+
```
169177
You can't run a transition from an invalid state:
170178

179+
```py
171180
>>> traffic_light.go()
172181
Traceback (most recent call last):
173182
statemachine.exceptions.TransitionNotAllowed: Can't go when in Green.
174183

184+
```
175185
Keeping the same state as expected:
176186

187+
```py
177188
>>> traffic_light.green.is_active
178189
True
179190

191+
```
192+
180193
And you can pass arbitrary positional or keyword arguments to the event, and
181194
they will be propagated to all actions and callbacks:
182195

196+
```py
183197
>>> traffic_light.cycle(message="Please, now slowdown.")
184198
'Running cycle from green to yellow. Please, now slowdown.'
185199

200+
```
186201

187-
Models
188-
------
202+
## Models
189203

190204
If you need to persist the current state on another object, or you're using the
191205
state machine to control the flow of another object, you can pass this object
192-
to the ``StateMachine`` constructor:
206+
to the `StateMachine` constructor:
193207

208+
```py
194209
>>> class MyModel(object):
195210
... def __init__(self, state):
196211
... self.state = state
@@ -219,13 +234,13 @@ True
219234
>>> traffic_light.yellow.is_active
220235
True
221236

237+
```
222238

223-
A more useful example
224-
---------------------
225-
226-
A simple didactic state machine for controlling an ``Order``:
239+
## A more useful example
227240

241+
A simple didactic state machine for controlling an `Order`:
228242

243+
```py
229244
>>> class OrderControl(StateMachine):
230245
... waiting_for_payment = State("Waiting for payment", initial=True)
231246
... processing = State("Processing")
@@ -263,10 +278,11 @@ A simple didactic state machine for controlling an ``Order``:
263278
... def on_enter_waiting_for_payment(self):
264279
... self.payment_received = False
265280

266-
281+
```
267282

268283
You can use this machine as follows.
269284

285+
```py
270286
>>> control = OrderControl()
271287

272288
>>> control.add_to_order(3)
@@ -308,6 +324,31 @@ True
308324
>>> control.completed.is_active
309325
True
310326

327+
```
311328

312329
There's a lot more to cover, please take a look at our docs:
313330
https://python-statemachine.readthedocs.io.
331+
332+
333+
## Contributing
334+
335+
<a class="github-button" href="https://github.com/fgmacedo/python-statemachine" data-icon="octicon-star" aria-label="Star fgmacedo/python-statemachine on GitHub">Star this project</a>
336+
<a class="github-button" href="https://github.com/fgmacedo/python-statemachine/issues" data-icon="octicon-issue-opened" aria-label="Issue fgmacedo/python-statemachine on GitHub">Open an Issue</a>
337+
<a class="github-button" href="https://github.com/fgmacedo/python-statemachine/fork" data-icon="octicon-repo-forked" aria-label="Fork fgmacedo/python-statemachine on GitHub">Fork</a>
338+
339+
- If you found this project helpful, please consider giving it a star on GitHub.
340+
341+
- **Contribute code**: If you would like to contribute code to this project, please submit a pull
342+
request. For more information on how to contribute, please see our [contributing.md](contributing.md) file.
343+
344+
- **Report bugs**: If you find any bugs in this project, please report them by opening an issue
345+
on our GitHub issue tracker.
346+
347+
- **Suggest features**: If you have a great idea for a new feature, please let us know by opening
348+
an issue on our GitHub issue tracker.
349+
350+
- **Documentation**: Help improve this project's documentation by submitting pull requests.
351+
352+
- **Promote the project**: Help spread the word about this project by sharing it on social media,
353+
writing a blog post, or giving a talk about it. Tag me on Twitter
354+
[@fgmacedo](https://twitter.com/fgmacedo) so I can share it too!

0 commit comments

Comments
 (0)