Revisions for ⁨State is lost when passing variable through the function action⁩

View the changes made to this paste.

unlisted ⁨1⁩ ⁨file⁩ 2019-08-08 10:15:57 UTC

function_action_state.py

@@ -0,0 +1,51 @@

+  from dragonfly import *
+import nsformat
+
+dictation_length = 0 # for "scratch that" purposes
+# This file shows that if you pass in the input state into the function using the function action rather than just accessing it
+# from within the function using the fact that it is in global scope,  the input state will not be updated 
+#  when you run the dictation command multiple times
+
+
+formatting_state = None
+def format_dictation(dictation, input_state):
+    global dictation_length, formatting_state
+    print("input_state: ", input_state)
+    formatted_output, output_state = nsformat.formatWords(dictation.words, state=input_state)
+    print("output_state: ", output_state)
+    formatted_output = str(formatted_output)
+    Text(formatted_output).execute()
+    dictation_length = len(formatted_output)
+    formatting_state = output_state
+
+def scratch_that():
+    for i in range(dictation_length):
+        Key("backspace").execute()
+         
+class CommandRule(MappingRule):
+    mapping = {
+        "strike":  Function(scratch_that),
+        }
+command_rule = CommandRule()
+class DictationRule(MappingRule):
+    
+    mapping = {
+        "<dictation>": Function(format_dictation, input_state=formatting_state),
+    }
+    extras = [ Dictation("dictation") ]
+dictation_rule = DictationRule()
+
+dict_cmd_sequence = Repetition(Alternative([RuleRef(dictation_rule), RuleRef(command_rule)]),
+    min=1, max=10, name="dict_cmd_sequence")
+
+class SequenceRule(CompoundRule):
+    spec = "<dict_cmd_sequence>"
+    extras = [dict_cmd_sequence]
+    def _process_recognition(self, node, extras):
+        for action in extras["dict_cmd_sequence"]:
+                action.execute()
+
+grammar = Grammar("zurow")
+sequence_rule = SequenceRule()
+grammar.add_rule(sequence_rule)
+grammar.load()
\ No newline at end of file