Sfoglia il codice sorgente

Module documentation update

master
Dirk Alders 4 anni fa
parent
commit
2bd8162975
3 ha cambiato i file con 63 aggiunte e 2 eliminazioni
  1. 2
    2
      __init__.py
  2. 11
    0
      _examples_/Makefile
  3. 50
    0
      _examples_/example.py

+ 2
- 2
__init__.py Vedi File

@@ -61,9 +61,9 @@ class state_machine(object):
61 61
 
62 62
     **Example:**
63 63
 
64
-    .. literalinclude:: ../examples/example.py
64
+    .. literalinclude:: state_machine/_examples_/example.py
65 65
 
66
-    .. literalinclude:: ../examples/example.log
66
+    .. literalinclude:: state_machine/_examples_/example.log
67 67
     """
68 68
     TRANSITIONS = {}
69 69
     LOG_PREFIX = 'StateMachine:'

+ 11
- 0
_examples_/Makefile Vedi File

@@ -0,0 +1,11 @@
1
+EXAMPLES = $(wildcard *.py)
2
+LOGFILES = ${EXAMPLES:.py=.log}
3
+
4
+.PHONY: all
5
+all: $(LOGFILES)
6
+
7
+%.log: %.py
8
+	python3 $< > $@
9
+
10
+clean:
11
+	rm $(LOGFILES)

+ 50
- 0
_examples_/example.py Vedi File

@@ -0,0 +1,50 @@
1
+#!/usr/bin/env python
2
+# -*- coding: UTF-8 -*-
3
+
4
+import sys
5
+sys.path.append('../..')
6
+import logging
7
+import os
8
+
9
+import report
10
+import state_machine
11
+
12
+logger = logging.getLogger('root')
13
+report.stdoutLoggingConfigure(log_name_lvl=[('root', 'DEBUG'), ])
14
+
15
+
16
+class trafic_lights(state_machine.state_machine):
17
+    LOG_PREFIX = 'TraficLights:'
18
+
19
+    STATE_RED = 'state_red'
20
+    STATE_GREEN = 'state_green'
21
+
22
+    CONDITION_TRUE = 'condition_true'
23
+    CONDITION_PEDASTRIAN_REQUEST = 'condition_pedastrian_request'
24
+
25
+    TRANSITIONS = {
26
+        STATE_RED: (
27
+            (CONDITION_PEDASTRIAN_REQUEST, 1, STATE_GREEN),
28
+        ),
29
+        STATE_GREEN: (
30
+            (CONDITION_TRUE, 3, STATE_RED),
31
+        )
32
+    }
33
+
34
+    def condition_true(self):
35
+        return True
36
+
37
+    def set_padestrian_request(self):
38
+        logger.log(self.__log_lvl__, '%s Pedestrian gave state change request.', self.LOG_PREFIX)
39
+        self.pedastrian_request = True
40
+
41
+    def condition_pedastrian_request(self):
42
+        return self.pedastrian_request
43
+
44
+
45
+sm = trafic_lights(trafic_lights.STATE_RED, logging.INFO, pedastrian_request=False)
46
+sm.register_state_change_callback(sm.STATE_GREEN, sm.CONDITION_PEDASTRIAN_REQUEST, logger.info, 'Callback information: Traffic light had been changed to green caused by pedastrian request')
47
+while not sm.previous_state_was(sm.STATE_GREEN):
48
+    if sm.this_state_is(sm.STATE_RED) and sm.this_state_duration() > 0.2 and not sm.condition_pedastrian_request():
49
+        sm.set_padestrian_request()
50
+    sm.work()

Loading…
Annulla
Salva