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

Third python tutorial



In the second Python tutorial, one of the modules was reimplemented in the Python scripting language, and we defined a start EntryPoint setting a factor for the incrementation or decrementation step. In this tutorial, we will set the factor by a property.

Overview

In the second Python tutorial, one of the module was implemented in Python:
  • The PublishModule module increments or decrements a value cyclically
We will change the PublishModule by setting the factor by using a property.

XML configuration

The applications.xml XML file will be modified to specify an init EntryPoint. We will also define an int property for the factor.

Add a properties file

We will add a properties file to specify the properties. For example:
      <properties>
         <application name="publishAppli" >
            <module name="PublishModule" >
               <moduleProperty key="factor" value="10" />
            </module>
         </application>
      </properties>
This file must be specified in the filelist configuration:
      <files>
         <file url="applications.xml" />
         <file url="services.xml" />
         <file url="types.xml" />
         <file url="properties.xml" />
         <property key="debugScripts" value="true" />
         <pythonRuntime path="<my_python_path>/python.exe" />
      </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">
                  <initEntryPoint method="init" />
                  <defaultSendEntryPoint method="publish" />
               </pythonImplementation>
               <interfaces>
                  <eventReceived service="event"/>
                  <cyclic service="published" frequency="200ms" attach="attach"/>
               </interfaces>
               <properties>
                  <property key="factor" type="int" />
               </properties>
            </pythonModule>
         </modules>
      </application>

Code the PublishModule in Python

The Python code is different of the Python code used for the first Python tutorial because we will implement the start method to set the step.

      from pythonUtils import PythonUtils

      class PythonAppli:
        step = 1
        count = 1
        factor = 1;

        def init(self, pythonUtils):
          self.factor = pythonUtils.getProperty("factor")

        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 * self.factor
          pythonUtils.send("published")

See also


Categories: tutorials

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