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

BCVTB, PythonActor external file read and write issue

asked 2017-10-20 06:40:54 -0500

cs_bot gravatar image

updated 2017-11-07 05:52:31 -0500

I am attempting to read (and/or) write onto external text file in the fired PythonActor.py function. When I use the 'with' keyword, there seems to be an error. (the below method is synchronous, thus more preferable)

with open('Path/to/file', 'r') as content_file:
    content = content_file.read()

But when I use the conventional methods (not the latest, though), it is working properly. But since this method is asynchronous, the method is not coherent & not (always) sequential

content_file = open(file_path,'r')
f.write(data)
f.close()

Any guidance would be helpful!! This is a part of my model:

image description

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-10-31 12:43:52 -0500

cxbrooks gravatar image

The PythonActor uses Jython and not all of Python is supported by the version of Jython that ships with BCVTB and Ptolemy II. It looks like it is fairly old:

bash-3.2$ svn log $PTII/lib/jython.jar
------------------------------------------------------------------------
r70691 | hudson@moog.eecs.berkeley.edu | 2014-11-21 08:32:29 -0800 (Fri, 21 Nov 2014) | 1 line

Updated to Jython 2.5.4-rc1

Jython 2.7.0 is supposed to support all of what is in the C version of Python 2.7.

The Jython 2.7.0 jar file is 37Mb, which is an increase from the Jython 2.5.4-rc1 size of 10Mb. I replaced $PTII/lib/jython.jar with the 2.7.0 jar file and the tests passed

wget -O jython.jar http://search.maven.org/remotecontent?filepath=org/python/jython-standalone/2.7.0/jython-standalone-2.7.0.jar
mv jython.jar $PTII/lib/jython.jar
cd $PTII/ptolemy/actor/lib/python/test/
make

Perhaps try updating your copy of jython.jar and then see if the with statement works.

I committed an updated version of jython.jar to the ptII tree, so I'll see what breaks in the tests over the next few days.

edit flag offensive delete link more

Comments

Thanks for the answer!

I tried changing (just replace the new jar with the old one) the jython.jar file but still not able to use the 'with' keyword in file handling

Is there any other operation I need to perform in order to commit the new jar?

cs_bot gravatar image cs_bot  ( 2017-11-02 07:15:09 -0500 )edit

It looks like the with statement is also present in Jython 2.5.2 because http://www.jython.org/docs/whatsnew/2... says that PEP 343 is present.

I downloaded https://chess.eecs.berkeley.edu/ptext..., created a file.txt file, changed the fire block to the following and it worked fine.

  def fire(self) :
    with open('file.txt', 'r') as f:
       read_data = f.read()
    print(read_data)
    self.output.broadcast(ptolemy.data.StringToken(read_data))

What is the error you are seeing? Can you post your model?

cxbrooks gravatar image cxbrooks  ( 2017-11-02 09:36:52 -0500 )edit

I constructed a model from scratch as above and it worked.

But, as of the existing model that has dynamic (at each zone timestep) data exchange between the PythonActor and Energyplus, I get the following exception(?) on the console(AFTER the completion of my simulation).

call: >java -jar D:\bcvtb\bin\BCVTB.jar model.xml
Note: failed to open syntax-directed editor: org.fife.ui.rsyntaxtextarea.RSyntaxDocument
Note: failed to open syntax-directed editor: ptolemy.actor.gui.syntax.SyntaxTextEditorForStringAttributes

I've also included the partial model in the question description

cs_bot gravatar image cs_bot  ( 2017-11-07 01:30:14 -0500 )edit

Asking about a missing class is a probably a different question than how to open a file, but I'll comment here. The class in question is located in $PTII/rsyntaxtextarea-2.6.0-SNAPSHOT.jar. I'm not sure if that jar is included in BCVTB or not. It appears that it is not included in BCVTB.jar. You can find a copy at https://chess.eecs.berkeley.edu/ptext...

You would use a command like:

java -jar D:\bcvtb\bin\BCVTB.jar;D:\bcvtb\bin\rsyntaxtextarea-2.6.0-SNAPSHOT.jar model.xml
cxbrooks gravatar image cxbrooks  ( 2017-11-13 10:09:01 -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

1 follower

Stats

Asked: 2017-10-20 06:40:54 -0500

Seen: 316 times

Last updated: Nov 07 '17