Question-and-Answer Resource for the Building Energy Modeling Community
Get s tarted with the Help page
Ask Your Question
2

[BCVTB] Is it possible to use print statement for debugging purposes in pythonActor.py ?

asked 2017-10-24 00:57:45 -0500

cs_bot gravatar image

updated 2017-10-24 11:06:24 -0500

I am new to thefield and I want to explore more into the programming capabilities of the PyhtonActor component in BCVTB. If I am to attempt debugging in the fire() of the PythonActor, is it possible to have dummy print statements? Or every correspondence wrt debugging has to be via files?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-10-31 12:29:19 -0500

cxbrooks gravatar image

I'm not much of a Python programmer, but you should be able to print to stdout from within a PythonScript actor, see below where I added to print() calls to the script.

BCVTB used Ptolemy II as its execution engine. In Ptolemy II, it is possible to listen to actors by right clicking on the actor and selecting "Listen to Actor". At runtime, messages will be displayed in a separate window. To write to the listener from within your Python code, see https://chess.eecs.berkeley.edu/ptext...:

In the script, use self.actor to access the actor. For example, self.actor.getDirector() returns the current director of the actor. For debugging, use self.actor.debug(someMessage). The final message sent to the debug listeners of the actor will have the string "From script: " inserted at the beginning. To avoid generating the debug message when there are no listeners, use:

if self.actor.isDebugging() : 
    self.actor.debug(someMessage)

I'm not sure about the indentation of the above.

To print to an output, see $PTII/ptolemy/actor/lib/python/test/PythonSysPath.xml , which has a PythonActor with the following. Note that I added two print() calls, which seem to work.

import sys.path

import ptolemy.data.type.BaseType
import ptolemy.data.StringToken

class Main :
  "Report the components of Jython's syspath"
  def preinitialize(self):
    self.output.setTypeEquals(ptolemy.data.type.BaseType.STRING)
  def prefire(self):
    return self.input.hasToken(0)
  def fire(self) :
    y=self.input.get(0) # retrieve and discard the trigger
    result=""
    sp=sys.path
    for i in range(len(sp)) :
      if (len(result) > 0):
          result+="\n"
      result+=sp[i]
    print('The result is')
    print(result)
    self.output.broadcast(ptolemy.data.StringToken(result))
    return
edit flag offensive delete link more

Comments

Thanks for the answer! But unfortunately, I am facing two issues.

  1. I am pretty sure that I don't have the XML file available ( I dont have a 'test' folder inside the 'python' directory ) in the given installed set of directories.

  2. I tried adding some print() statements in the fire function, and when i did press 'Listen to Actor', I am only able to see this, repeatedly:

    "Called prefire() Called fire() Called postfire() Called iterate(1)"

Any further help will be appreciated!

cs_bot gravatar image cs_bot  ( 2017-11-02 06:32:26 -0500 )edit
1

The file is available at https://chess.eecs.berkeley.edu/ptext...

For listening to work, you would need to add

if self.actor.isDebugging() : 
    self.actor.debug(someMessage)

The output of the print statements would appear on stdout, which requires that you start BCVTB from a shell. The output of the print() statement does not appear in the listener window. BCVTB might have the functionality to display the console by doing View -> Console.

cxbrooks gravatar image cxbrooks  ( 2017-11-02 09:18:39 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

 

Question Tools

2 followers

Stats

Asked: 2017-10-24 00:57:45 -0500

Seen: 94 times

Last updated: Oct 24 '17