Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Python tutorial



The first tutorial presented the coding and configuration of a very simple system with two modules. In this Python tutorial, one of the modules will be reimplemented in the Python scripting language, using UDP sockets.

Note that the second python tutorial will show how to use the start EntryPoint in Python.

The code of this tutorial is in the samples/python/sample3 directory.

Overview

In the first tutorial, we had two modules, both of which implemented in Java:
  • The PublishModule module increments or decrements a value cyclically
  • The EventModule module allows to click on a toggle to set if the first module should increment or decrement the value, and shows the value
We will reimplement the PublishModule in the Python scripting language.

Architecture

The first tutorial had two applications coded in Java:
  • The first PublishModule which:
    • Increment or decrement the value
    • Publish cyclically the value
    • Listen to the toggle event to set if the value should increment or decrement
  • The second EventModule which:
    • Subscribe to the published value and show this value
    • Show a toggle and sends an event when the user clicks on this toggle

tuto1java
We will reimplement the PublishModule in Python:
tutopython

XML configuration

Only the applications.xml XML file has to be modified to switch from a Java module implementation to a Python module implementation.

Filelist configuration

We will define the path of the Python runtime in the filelist configuration:
      <files>
         <file url="applications.xml" />
         <file url="services.xml" />
         <file url="types.xml" />
         <pythonRuntime path="<my_python_path>/python.exe" />
      </files>
If we want to simplify debugging if we encounter errors in the script execution, we can set the debugScripts property:
      <files>
         <file url="applications.xml" />
         <file url="services.xml" />
         <file url="types.xml" />
         <property key="debugScripts" value="true" />
         <pythonRuntime path="<my_python_path>/python.exe" />
      </files>

Using an Anaconda Python runtime

If we want to use an Anaconda Python runtime, we can specify:
      <files>
         <file url="applications.xml" />
         <file url="services.xml" />
         <file url="types.xml" />
         <property key="debugScripts" value="true" />
         <pythonRuntime path="<my_Anaconda_path>/python.exe" anaconda="true" />
      </files>

Applications definition

We will change the applications.xml XML file, by modifying the PublishModule configuration:
      <application name="publishAppli">
         <modules>
            <pythonModule name="PublishModule" >
               <pythonImplementation path="pythonAppli">
                  <defaultSendEntryPoint method="publish" />
               </pythonImplementation>
               <interfaces>
                  <eventReceived service="event"/>
                  <cyclic service="published" frequency="200ms" attach="attach"/>
               </interfaces>
            </pythonModule>
         </modules>
      </application>
As you can see, we specified that the implementation of the PublishModule is in the pythonApply.py file, in the same directory as the application.xml file. We will also have a send method in the Groovy script will be called each time the published Service is cyclically invoked.

Generating the Python library

We will generate the two following scripts by the Browser:
  • The pythonModule.py script
  • The pythonUtils.py script
These two supporting scripts do not depend on the content of the Python script and can be retrieved in the Browser by performing the command Tools => Generate Python Library.

Code the PublishModule in python

The Python code is different of the Java code used for the first tutorial because of the difference between Java and Python syntax.

As for the first tutorial, we don't have to bother about how to send cyclically the value, because we already specified that we wanted the published service to be invoked cyclically every 200 milliseconds in the module XML specification:
     <cyclic service="published" frequency="200ms" attach="attach"/>
The pythonApply.py script will implement the behavior of this Module:
  • a subscribe method will be notified of the event Service. Depending on the value of the event data, a step variable will have a 1 or -1 value
  • a send method will be invoked cyclically for the published Service. The current value will be incremented or decremented, and the Service will be notified with this current value
      from pythonUtils import PythonUtils

      class PythonAppli:
        step = 1
        count = 1

        def subscribe(self, pythonUtils, serviceName):
          eventService = pythonUtils.getService("event");
          eventValue = eventService["event"]
          if eventValue:
            self.step = -1
          else:
            self.step = 1

        def publish(self, pythonUtils, serviceName):
          publishService = pythonUtils.getService("published");
          pythonUtils.setValue(publishService, "value", self.count)
          self.count = self.count + self.step
          pythonUtils.send("published")

See also


Categories: tutorials

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence