Skip to content

Instantly share code, notes, and snippets.

@jmarrec
Last active April 29, 2025 20:11
Show Gist options
  • Save jmarrec/27c8d5c93bbfc233af04b1d307ca45d1 to your computer and use it in GitHub Desktop.
Save jmarrec/27c8d5c93bbfc233af04b1d307ca45d1 to your computer and use it in GitHub Desktop.
def freed_from_desire():
return "NANANANANANANANAN NAAAAAAAA NAAAA NAAAA NAAA"
import typing
import openstudio
from resources.helpers import freed_from_desire
class PythonWithResources(openstudio.measure.ModelMeasure):
"""A ModelMeasure."""
def name(self):
return "A measure with a resources/ helper"
def description(self):
return "DESCRIPTION_TEXT"
def modeler_description(self):
return "MODELER_DESCRIPTION_TEXT"
def arguments(self, model: typing.Optional[openstudio.model.Model] = None):
args = openstudio.measure.OSArgumentVector()
return args
def run(
self,
model: openstudio.model.Model,
runner: openstudio.measure.OSRunner,
user_arguments: openstudio.measure.OSArgumentMap,
):
"""Defines what happens when the measure is run."""
super().run(model, runner, user_arguments) # Do **NOT** remove this line
if not (runner.validateUserArguments(self.arguments(model), user_arguments)):
return False
# report initial condition of model
runner.registerInitialCondition(freed_from_desire())
# report final condition of model
runner.registerFinalCondition("Done")
return True
# register the measure to be used by the application
PythonWithResources().registerWithApplication()
<?xml version="1.0"?>
<measure>
<schema_version>3.1</schema_version>
<name>python_with_resources</name>
<uid>ab2248a3-357d-44f6-abe8-0c40c79f747d</uid>
<version_id>c17fc3d0-e57f-46e1-b70e-e708f461e20f</version_id>
<version_modified>2025-04-29T20:06:58Z</version_modified>
<xml_checksum>39C5D9F7</xml_checksum>
<class_name>PythonWithResources</class_name>
<display_name>A measure with a resources/ helper</display_name>
<description>DESCRIPTION_TEXT</description>
<modeler_description>MODELER_DESCRIPTION_TEXT</modeler_description>
<arguments />
<outputs />
<provenances />
<tags>
<tag>HVAC.Heating</tag>
</tags>
<attributes>
<attribute>
<name>Measure Type</name>
<value>ModelMeasure</value>
<datatype>string</datatype>
</attribute>
<attribute>
<name>Measure Language</name>
<value>Python</value>
<datatype>string</datatype>
</attribute>
</attributes>
<files>
<file>
<version>
<software_program>OpenStudio</software_program>
<identifier>3.9.0</identifier>
<min_compatible>3.9.0</min_compatible>
</version>
<filename>measure.py</filename>
<filetype>py</filetype>
<usage_type>script</usage_type>
<checksum>35BFD92C</checksum>
</file>
<file>
<filename>helpers.py</filename>
<filetype>py</filetype>
<usage_type>resource</usage_type>
<checksum>ECCD194F</checksum>
</file>
<file>
<filename>test_python_with_resources.py</filename>
<filetype>py</filetype>
<usage_type>test</usage_type>
<checksum>2231F81D</checksum>
</file>
</files>
</measure>
"""insert your copyright here."""
import sys
from pathlib import Path
import openstudio
import pytest
CURRENT_DIR_PATH = Path(__file__).parent.absolute()
sys.path.insert(0, str(CURRENT_DIR_PATH.parent))
from measure import PythonWithResources
from resources.helpers import freed_from_desire
sys.path.pop(0)
del sys.modules["measure"]
class TestPythonWithResources:
"""Py.test module for PythonWithResources."""
def test_number_of_arguments_and_argument_names(self):
"""Test that the arguments are what we expect."""
# create an instance of the measure
measure = PythonWithResources()
# make an empty model
model = openstudio.model.Model()
# get arguments and test that they are what we are expecting
arguments = measure.arguments(model)
assert arguments.size() == 0
def test_good_argument_values(self):
"""Test running the measure with appropriate arguments.
Asserts that the measure runs fine and with expected results.
"""
# create an instance of the measure
measure = PythonWithResources()
# create runner with empty OSW
osw = openstudio.WorkflowJSON()
runner = openstudio.measure.OSRunner(osw)
model = openstudio.model.Model()
# store the number of spaces in the seed model
num_spaces_seed = len(model.getSpaces())
# get arguments
arguments = measure.arguments(model)
argument_map = openstudio.measure.convertOSArgumentVectorToMap(arguments)
# run the measure
measure.run(model, runner, argument_map)
result = runner.result()
# show the output
# show_output(result)
print(f"results: {result}")
# assert that it ran correctly
assert result.value().valueName() == "Success"
assert len(result.stepWarnings()) == 0
assert result.stepInitialCondition()
assert result.stepInitialCondition().get() == freed_from_desire()
# This allows running openstudio CLI on this file (`openstudio test_measure.py`, maybe with extra args)
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
pytest.main([__file__] + sys.argv[1:])
else:
pytest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment