Skip to content

Commit

Permalink
Add active_writer_version, test writer activation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiirola committed Aug 9, 2024
1 parent 0855619 commit cbd702d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pyomo/repn/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ def activate_writer_version(name, ver):
doc = WriterFactory.doc(name)
WriterFactory.unregister(name)
WriterFactory.register(name, doc)(WriterFactory.get_class(f'{name}_v{ver}'))


def active_writer_version(name):
"""DEBUGGING TOOL to switch the "default" writer implementation"""
from pyomo.opt import WriterFactory

ref = WriterFactory.get_class(name)
ver = 1
try:
while 1:
if WriterFactory.get_class(f'{name}_v{ver}') is ref:
return ver
ver += 1
except KeyError:
return None
50 changes: 50 additions & 0 deletions pyomo/repn/tests/test_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# ___________________________________________________________________________
#
# Pyomo: Python Optimization Modeling Objects
# Copyright (c) 2008-2024
# National Technology and Engineering Solutions of Sandia, LLC
# Under the terms of Contract DE-NA0003525 with National Technology and
# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________

from pyomo.common import unittest

from pyomo.opt import WriterFactory
from pyomo.repn.plugins import activate_writer_version, active_writer_version

import pyomo.environ


class TestPlugins(unittest.TestCase):
def test_active(self):
with self.assertRaises(KeyError):
active_writer_version('nonexistant_writer')
ver = active_writer_version('lp')
self.assertIs(
WriterFactory.get_class('lp'), WriterFactory.get_class(f'lp_v{ver}')
)

class TMP(object):
pass

WriterFactory.register('test_writer')(TMP)
try:
self.assertIsNone(active_writer_version('test_writer'))
finally:
WriterFactory.unregister('test_writer')

def test_activate(self):
ver = active_writer_version('lp')
try:
activate_writer_version('lp', 2)
self.assertIs(
WriterFactory.get_class('lp'), WriterFactory.get_class(f'lp_v2')
)
activate_writer_version('lp', 1)
self.assertIs(
WriterFactory.get_class('lp'), WriterFactory.get_class(f'lp_v1')
)
finally:
activate_writer_version('lp', ver)

0 comments on commit cbd702d

Please sign in to comment.