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

Why does model.save(path) fail with path an openstudio.path()?

asked 2024-03-29 13:44:01 -0500

mattkoch's avatar

updated 2024-03-29 13:44:48 -0500

I just upgraded from openstudio-3.6.0 SDK to opnstudio-3.7.0 SDK in Python. The following simple test used to work in 3.6.0:

import openstudio
model = openstudio.model.Model()
path = openstudio.path("./ModelTest.osm")
model.save(path)

However, in 3.7.0, I receive the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python-3.10.11\lib\site-packages\openstudio\openstudioutilitiesidf.py", line 1997, in save
    return _openstudioutilitiesidf.Workspace_save(self, p, overwrite)
TypeError: Wrong number or type of arguments for overloaded function 'Workspace_save'.
  Possible C/C++ prototypes are:
    openstudio::Workspace::save(openstudio::path const &,bool)
    openstudio::Workspace::save(openstudio::path const &)

Interestingly, the following does seem to work instead, namely using a simple Python String instead of an OpenStudio Path:

import openstudio
model = openstudio.model.Model()
path = "./ModelTest.osm"
model.save(path)

Is there some type incompatibility going on? A "pip freeze" does confirm that I am using openstudio-3.7.0 in my Python-3.10.11.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2024-03-29 14:14:22 -0500

updated 2024-03-29 14:18:10 -0500

Sorry for the trouble, I added a conversion to/from openstudio.Path to python's pathlib.Path, and apprently messed something up.

Basically this comes from (an honestly good) intention, which was to allow seemless conversion to/from pathlib.Path but it seems it does have the side effect that it rejects an openstudio.Path now.

You might have noticed that anything returning an openstudio::path now converts to a pathlib.Path

In [1]: osw = openstudio.WorkflowJSON()

In [2]: osw.absoluteRootDir()
Out[2]: PosixPath('......')

Similarly you can pass a pathlib.Path to something taking an openstudio::Path. Or a string.

In [1]: from pathlib import Path
        import openstudio
        m = openstudio.model.Model()

 In [5]: m.save('model.osm', True)
 Out[5]: True

 In [6]: m.save(Path('model.osm'), True)
 Out[6]: True

Bottom line, use a python string or a pathtlib.Path. Use str(the_path) if you don't know what you're getting in.

I filed it at NREL/OpenStudio#5133


Please note that such changes may be unexpected (I mean such as returning a pathlib.Path, not breaking existing workflows), but support for Python Measures was not effective before so it was a more than appropriate time to perform usability-related API breaks before we got stuck with the status quo.

edit flag offensive delete link more

Comments

No problem, Julien Marrec. If you are the driving force behind the Python port of openstudio, then the world owes you a huge debt of gratitude no matter what you do with openstudio.path. Being able to work in Python and make use of its near-infinite universe is such a relief! Python is such a natural language that it speeds up my work by a factor two to three compared to Ruby! Oh, and by the way, os.path also works on both model.load() and model.save().

mattkoch's avatar mattkoch  ( 2024-03-29 15:24:08 -0500 )edit

Your Answer

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

Add Answer

Careers

Question Tools

Stats

Asked: 2024-03-29 13:44:01 -0500

Seen: 71 times

Last updated: Mar 29