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

Second python tutorial



In the first Python tutorial, one of the modules was reimplemented in the Python scripting language, using UDP sockets. In this new tutorial, we will define a start EntryPoint.

Overview

In the first 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 a factor for the incrementation or decrementation step in the start method.

XML configuration

Only the applications.xml XML file will be modified to specify a start EntryPoint.

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">
                  <startEntryPoint method="start" />
                  <defaultSendEntryPoint method="publish" />
               </pythonImplementation>
               <interfaces>
                  <eventReceived service="event"/>
                  <cyclic service="published" frequency="200ms" attach="attach"/>
               </interfaces>
            </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 start(self, pythonUtils):
          self.factor = 10

        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