<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://jitterjames.net/voxcommandoWiki/index.php?action=history&amp;feed=atom&amp;title=Python</id>
	<title>Python - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://jitterjames.net/voxcommandoWiki/index.php?action=history&amp;feed=atom&amp;title=Python"/>
	<link rel="alternate" type="text/html" href="https://jitterjames.net/voxcommandoWiki/index.php?title=Python&amp;action=history"/>
	<updated>2026-10-16T09:49:33Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.12</generator>
	<entry>
		<id>https://jitterjames.net/voxcommandoWiki/index.php?title=Python&amp;diff=253&amp;oldid=prev</id>
		<title>James: 1 revision</title>
		<link rel="alternate" type="text/html" href="https://jitterjames.net/voxcommandoWiki/index.php?title=Python&amp;diff=253&amp;oldid=prev"/>
		<updated>2021-01-13T17:25:02Z</updated>

		<summary type="html">&lt;p&gt;1 revision&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 17:25, 13 January 2021&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>James</name></author>
		
	</entry>
	<entry>
		<id>https://jitterjames.net/voxcommandoWiki/index.php?title=Python&amp;diff=252&amp;oldid=prev</id>
		<title>Jitterjames: /* Testing Code */</title>
		<link rel="alternate" type="text/html" href="https://jitterjames.net/voxcommandoWiki/index.php?title=Python&amp;diff=252&amp;oldid=prev"/>
		<updated>2017-11-26T12:54:49Z</updated>

		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Testing Code&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=The IronPython Plugin &amp;quot;PY&amp;quot;=&lt;br /&gt;
By leveraging the power of [http://ironpython.net/ IronPython], the &amp;quot;PY&amp;quot; plugin allows us to run Python scripts in VoxCommando.  Scripts can interact with VoxCommando, reading and setting variables, triggering events and executing other actions.  This allows us to do many new things that VoxCommando was not able to do very well before such as:&lt;br /&gt;
&lt;br /&gt;
* Math&lt;br /&gt;
* Complex logic&lt;br /&gt;
* Looping (for loop, while loop, etc)&lt;br /&gt;
* and pretty much anything else we can do with Python!&lt;br /&gt;
&lt;br /&gt;
Remember, Python is '''case sensitive''', and depends on '''proper indentation'''.  Be careful not to mix spaces and tabs in your indentation.  This is a common mistake with Python.&lt;br /&gt;
&lt;br /&gt;
==IronPython Is Not Quite Python==&lt;br /&gt;
*IronPython is based on Python version 2.7. If you grab code that's written for Python 3.x, you might need to make some modifications to make it compatible.&lt;br /&gt;
*IronPython has advantages over regular Python in that we have direct access to various components of VoxCommando.&lt;br /&gt;
*IronPython also gives us access to many of the programming tools available in .Net. For example, in our [[Arduino]] Python scripts, we are using .Net's &amp;lt;html&amp;gt;&amp;lt;a href=&amp;quot;https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx&amp;quot;&amp;gt;serialport&amp;lt;/a&amp;gt;&amp;lt;/html&amp;gt; class.&lt;br /&gt;
*Not everything that works correctly in standard Python will work in IronPython, but most things do.&lt;br /&gt;
&lt;br /&gt;
=Setting up the Python Libraries=&lt;br /&gt;
[[File:InstallComponents.png|left|400px]]You do not need to install Python to use this plugin. Everything that is required is provided with VoxCommando.&lt;br /&gt;
&lt;br /&gt;
During installation, make sure the Python Libraries checkbox is selected. If you initially unchecked the Python Libraries box upon installing VC, simply run the installation file again, with the box selected.&lt;br /&gt;
&lt;br /&gt;
To use the Python plugin, simply enable the plugin as you would any other [[Plugins|plugin]] in VoxCommando.&lt;br /&gt;
&lt;br /&gt;
=Plugin Actions=&lt;br /&gt;
Currently this plugin allows us to call the following actions from our VoxCommando commands.&lt;br /&gt;
&lt;br /&gt;
==PY.ExecString==&lt;br /&gt;
Executes a single line of Python code.  For example, we can use it to handle &amp;lt;html&amp;gt;&amp;lt;a href=&amp;quot;http://voxcommando.com/forum/index.php?topic=2165.0&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;basic calculations&amp;lt;/a&amp;gt;&amp;lt;/html&amp;gt; within a VC command macro, or &amp;lt;html&amp;gt;&amp;lt;a href=&amp;quot;http://voxcommando.com/forum/index.php?topic=1548.msg13508#msg13508&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;pass VC data&amp;lt;/a&amp;gt;&amp;lt;/html&amp;gt; to a function we've already defined and loaded as a Python script (using PY.ExecFile), etc. But if we want to do anything fancy, we should probably use PY.ExecFile.&lt;br /&gt;
&lt;br /&gt;
==PY.ExecFile==&lt;br /&gt;
Executes all of the code in the file specified.&lt;br /&gt;
You can use relative paths, and they will be interpreted relative to the main VoxCommando folder.  It is generally a good idea to put all Python scripts into a folder such as ..\VoxCommando\PY.&lt;br /&gt;
&lt;br /&gt;
If you need to pass parameters from VoxCommando to a Python function, you should define the function first using PY.ExecFile, then use PY.ExecString to call the function and pass in parameters. Search the forum for examples (&amp;lt;html&amp;gt;&amp;lt;a href=&amp;quot;http://voxcommando.com/forum/index.php?topic=1548.msg13508#msg13508&amp;quot;&amp;gt;here's one&amp;lt;/a&amp;gt;&amp;lt;/html&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Functions only need to be defined once. You may wish to put all your Python functions into a single ExecFile that gets loaded when VoxCommando first starts up (i.e., triggered using a VC.Loaded [[Command Tree Editor|event]]). Any function in this file can then be called within individual commands using the PY.ExecString action.&lt;br /&gt;
&lt;br /&gt;
==PY.*==&lt;br /&gt;
List of [[Plugin_PY|all actions]] available in the [[Logical_Command_Builder|LCB]].&lt;br /&gt;
&lt;br /&gt;
=Accessing VoxCommando Methods from Python Code=&lt;br /&gt;
Through the vc object, Python code can call the following methods available to all plugins:&lt;br /&gt;
&lt;br /&gt;
* '''void''' triggerEvent(string strEventName, List&amp;lt;string&amp;gt; payloads);&lt;br /&gt;
* [[actionResult]] callAction(string strActionType, string strParams, List&amp;lt;string&amp;gt; payloads);&lt;br /&gt;
**[[#Actions|more info]]&lt;br /&gt;
* '''void''' doWav(string strWavPath);&lt;br /&gt;
**Evaluates an audio file and tries to recognize speech&lt;br /&gt;
* '''object''' getObject(string strObject, string strSubObject);&lt;br /&gt;
** [[#getObject|more info]]&lt;br /&gt;
&lt;br /&gt;
* '''void''' log(string strLogText);&lt;br /&gt;
** Writes a line of text to the VoxCommando log file (if logging is enabled)&lt;br /&gt;
* '''bool''' savePayloadFile(string strPath, Dictionary&amp;lt;string, string&amp;gt; payloadPairs, bool subsetMatching);&lt;br /&gt;
* '''bool''' savePayloadListToXML(string strPath, List&amp;lt;xmlPayload&amp;gt; myxmlpayloads);&lt;br /&gt;
** probably too complicated for most users&lt;br /&gt;
* '''string''' getLastResult();&lt;br /&gt;
** returns the last result (a string) that was successfully returned by an action&lt;br /&gt;
* '''void''' setResultList(List&amp;lt;string&amp;gt; lstResults);&lt;br /&gt;
&lt;br /&gt;
==Triggering an Event==&lt;br /&gt;
===Event With No Payloads===&lt;br /&gt;
For example, if you wanted to trigger an event in VC called &amp;quot;MyEvent.DoIt&amp;quot; you could use the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vc.triggerEvent(&amp;quot;MyEvent.DoIt&amp;quot;,None)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: &amp;quot;None&amp;quot; is the Python equivalent of &amp;quot;Null&amp;quot; and we use it if we don't need payloads.&lt;br /&gt;
&lt;br /&gt;
===Event With Payloads===&lt;br /&gt;
To include payloads in the event:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from System.Collections.Generic import *&lt;br /&gt;
vc.triggerEvent(&amp;quot;MyEvent.DoIt&amp;quot;, List[str]([&amp;quot;this is payload1&amp;quot;,&amp;quot;and this is payload2&amp;quot;]))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Actions==&lt;br /&gt;
You can execute other VoxCommando actions by using vc.callAction, like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vc.callAction(&amp;quot;OSD.ShowText&amp;quot;,&amp;quot;Display me for 5 seconds at the top&amp;amp;&amp;amp;5000&amp;amp;&amp;amp;-10&amp;quot;, None)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As with vc.triggerEvent above, the final parameter is ''None'' because we are not passing any payloads. In other words:&lt;br /&gt;
&lt;br /&gt;
[[File:primer.png]]&lt;br /&gt;
&lt;br /&gt;
We can make VoxCommando count to ten.&lt;br /&gt;
&lt;br /&gt;
We can use Python to execute a loop.  On each iteration of the loop, we call the &amp;quot;TTS.Speak&amp;quot; action.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for x in range(1, 11):   &lt;br /&gt;
    vc.callAction(&amp;quot;TTS.Speak&amp;quot;,&amp;quot;%s&amp;quot;%x, None)    &lt;br /&gt;
vc.callAction(&amp;quot;TTS.Speak&amp;quot;,&amp;quot;. There, I counted to ten&amp;quot;, None)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;%s&amp;quot;%x is a Python method of formatting a string.  In this case we are using it to convert an integer to a string&lt;br /&gt;
%s will be replaced by the value of x&lt;br /&gt;
*Note that in this case we are not using payloads, so we must send &amp;quot;None&amp;quot; which is the Python way of saying &amp;quot;null&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Generating Payload XML Using a Dictionary in Python==&lt;br /&gt;
&lt;br /&gt;
We can generate a payload XML file from a dictionary of key:value pairs.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from System.Collections.Generic import *&lt;br /&gt;
&lt;br /&gt;
myPayloads = {&amp;quot;payload value 1&amp;quot;:&amp;quot;payload phrase 1&amp;quot;,&amp;quot;payload value 2&amp;quot;:&amp;quot;payload phrase 2&amp;quot;,&amp;quot;payload value 3&amp;quot;:&amp;quot;payload phrase 3&amp;quot;}&lt;br /&gt;
path = &amp;quot;C:/VoxCommando/payloads/payloadtest.xml&amp;quot;&lt;br /&gt;
&lt;br /&gt;
vc.savePayloadFile(path,Dictionary[str,str](myPayloads),False)&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
CAUTION: The dictionary keys will become the *values* in your payload XML file. The dictionary values will become your payload phrases.&lt;br /&gt;
&lt;br /&gt;
==getObject==&lt;br /&gt;
You can request data from VC by calling vc.getObject(string strObject, string strSubObject)&lt;br /&gt;
&lt;br /&gt;
For example, to print the LastResult variable which was set by a previous action:&lt;br /&gt;
&amp;lt;pre&amp;gt;print vc.getObject(&amp;quot;LastResult&amp;quot;,&amp;quot;&amp;quot;)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===strObject ===&lt;br /&gt;
Valid values for strObject are:&lt;br /&gt;
*&amp;quot;lastresult&amp;quot;: returns a string&lt;br /&gt;
*&amp;quot;matches&amp;quot;: returns a list of strings&lt;br /&gt;
*&amp;quot;displaylanguage&amp;quot;: returns a string&lt;br /&gt;
*&amp;quot;payloads&amp;quot;: returns a list of strings&lt;br /&gt;
*&amp;quot;fpayloads&amp;quot;: returns a list of strings (in this case, [[Variables#.7BPF.1.7D_etc.|friendly payloads]])&lt;br /&gt;
*&amp;quot;evalvars&amp;quot; : returns a string where known VC variables in curly brackets have been replaced with their values&lt;br /&gt;
** available only in VoxCommando version '''2.2.1.7''' or later&lt;br /&gt;
&amp;lt;pre&amp;gt;#example for evalvars&lt;br /&gt;
info = vc.getObject(&amp;quot;evalvars&amp;quot;,&amp;quot;{LastSpoken}&amp;quot;)&lt;br /&gt;
print info&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===strSubObject===&lt;br /&gt;
At the moment, no objects make use of the strSubObject parameter&lt;br /&gt;
&lt;br /&gt;
=Returning a Result=&lt;br /&gt;
To send a result back to VoxCommando you need only set the &amp;quot;result&amp;quot; variable.  The value will then be accessible to subsequent actions using {LastResult}&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
'''Example 1.''' &lt;br /&gt;
&lt;br /&gt;
If we execute the following Python code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
result= 5.0*4/(3+4)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
then {LastResult} will be equal to &amp;quot;2.85714285714286&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Example 2.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html&amp;gt;&amp;lt;a href=&amp;quot;http://voxcommando.com/forum/index.php?topic=1341.msg11572&amp;quot;&amp;gt;Verify Internet Status&amp;lt;/a&amp;gt;&amp;lt;/html&amp;gt; (forum thread)&lt;br /&gt;
&lt;br /&gt;
'''Example 3.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html&amp;gt;&amp;lt;a href=&amp;quot;http://voxcommando.com/forum/index.php?topic=1548.msg13507#msg13507&amp;quot;&amp;gt;For Loop&amp;lt;/a&amp;gt;&amp;lt;/html&amp;gt; (forum thread)&lt;br /&gt;
&lt;br /&gt;
=Python Scripting Forum Board=&lt;br /&gt;
More use scenarios and explanations on issues such as:&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;html&amp;gt;&amp;lt;a href=&amp;quot;http://voxcommando.com/forum/index.php?topic=1548.msg13488#msg13488&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;complex logic in python&amp;lt;/a&amp;gt;&amp;lt;/html&amp;gt;&lt;br /&gt;
*&amp;lt;html&amp;gt;&amp;lt;a href=&amp;quot;http://voxcommando.com/forum/index.php?topic=1548.msg13508#msg13508&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;creating python functions&amp;lt;/a&amp;gt;&amp;lt;/html&amp;gt;&lt;br /&gt;
*&amp;lt;html&amp;gt;&amp;lt;a href=&amp;quot;http://voxcommando.com/forum/index.php?topic=1548.msg13488#msg13488&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;passing information between VC and python&amp;lt;/a&amp;gt;&amp;lt;/html&amp;gt;&lt;br /&gt;
*&amp;lt;html&amp;gt;&amp;lt;a href=&amp;quot;http://voxcommando.com/forum/index.php?topic=1541.msg13573#msg13573&amp;quot;&amp;gt;comparing python solutions to VC-only solutions&amp;lt;/a&amp;gt;&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
can be found on the forum. In particular (though not exclusively), on the &amp;lt;html&amp;gt;&amp;lt;a href=&amp;quot;http://voxcommando.com/forum/index.php?board=41.0&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;Python Scripting&amp;lt;/a&amp;gt;&amp;lt;/html&amp;gt; board.&lt;br /&gt;
&lt;br /&gt;
=Testing Code=&lt;br /&gt;
[[File:Pypluginwindow.png |thumb|400px]]&lt;br /&gt;
*Open the &amp;quot;PY&amp;quot; plugin settings page to access a Python editor where you can test your code. Click &amp;quot;Execute&amp;quot; to see what happens.&lt;br /&gt;
&lt;br /&gt;
*Another way to test code would be to edit a Python file in another program such as &amp;quot;Notepad++&amp;quot; and then test the code by calling the PY.ExecFile action from VoxCommando.&lt;br /&gt;
Notepad++ is a good way to edit your Python code and offers many advantages such as code highlighting.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br style=&amp;quot;clear: both&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Reinitializing the Python Engine=&lt;br /&gt;
Sometimes you will need to clear out the various classes and methods that have been loaded into the Python engine.  This can be done by:&lt;br /&gt;
* restarting VoxCommando, or, &lt;br /&gt;
* choosing &amp;quot;File &amp;gt;&amp;gt; Reinitialize Python&amp;quot; in the Python plugin window, or &lt;br /&gt;
* within a command macro, using the [[Plugin_PY#ReInit|PY.ReInit]] action.&lt;br /&gt;
&lt;br /&gt;
==Killing Python Threads==&lt;br /&gt;
&lt;br /&gt;
* It is important to note that if you've used threading in your Python, these Python threads will continue to run in the background until they terminate themselves (or VoxCommando is closed).&lt;br /&gt;
&lt;br /&gt;
* Choosing &amp;quot;File &amp;gt;&amp;gt; Reinitialize Python&amp;quot; will set a variable named &amp;quot;'''killPy'''&amp;quot; to true.  You can use this to end threaded loops by writing your threaded loops as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;width:450px;&amp;quot;&amp;gt;&lt;br /&gt;
while not killPy:&lt;br /&gt;
        try:&lt;br /&gt;
            ...do your threaded loop stuff here&lt;br /&gt;
            ...do your threaded loop stuff here        &lt;br /&gt;
        except:&lt;br /&gt;
            ...handle errors etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jitterjames</name></author>
		
	</entry>
</feed>