Changeset 2433


Ignore:
Timestamp:
Mar 12, 2010 3:42:35 AM (9 years ago)
Author:
wehart
Message:

Merged revisions 2388-2432 via svnmerge from
https://software.sandia.gov/svn/public/coopr/coopr.pyomo/trunk

........

r2399 | wehart | 2010-02-24 11:36:56 -0700 (Wed, 24 Feb 2010) | 2 lines


Adding a work-around when pstats cannot be imported.

........

r2400 | wehart | 2010-02-24 14:51:08 -0700 (Wed, 24 Feb 2010) | 2 lines


Misc documentation updates.

........

r2408 | wehart | 2010-02-28 05:41:04 -0700 (Sun, 28 Feb 2010) | 3 lines


Allow an earlier version of Python. I'm still not sure this is a good idea,
but it's necessary for Jython.

........

r2409 | wehart | 2010-02-28 05:42:33 -0700 (Sun, 28 Feb 2010) | 7 lines


Rework of profiling imports.


Refinement of Pyomo command-line parsing, which is more
specific now.


Adding automatic import of pyomo to package.

........

r2415 | jwatson | 2010-03-08 20:53:13 -0700 (Mon, 08 Mar 2010) | 3 lines


Significantly improved performance of PyomoModel? _clear_attribute method, mainly by eliminate unnecessary calls to it through _setattr_exec_.

........

r2416 | jwatson | 2010-03-09 16:45:22 -0700 (Tue, 09 Mar 2010) | 5 lines


Modified the AMPL dat file parser to instantiate the lexer and yaccer objects only once, for the lifetime of the module.


They were being inadvertently created at each invocation, which pyomo users wouldn't notice. But PySP users, who can be creating 1K or more instances, do!

........

r2422 | wehart | 2010-03-11 16:01:09 -0700 (Thu, 11 Mar 2010) | 3 lines


Rework of unit tests to (a) import pyutilib.th as 'unittest' and
(b) employ test skipping.

........

r2430 | wehart | 2010-03-11 23:38:22 -0700 (Thu, 11 Mar 2010) | 2 lines


Reworking class decorators.

........

Location:
coopr.pyomo/stable/2.3
Files:
23 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/stable/2.3

  • coopr.pyomo/stable/2.3/coopr/pyomo/base/PyomoModel.py

    r2360 r2433  
    187187    def _clear_attribute(self,name):
    188188        #
    189         # Cleanup Old Model Declaration First
     189        # cleanup the pre-existing model attribute
    190190        #
    191191        i=0
     
    202202
    203203    def _setattr_exec(self,name,val):
    204         self._clear_attribute(name)
     204        if name in self.__dict__:
     205           self._clear_attribute(name)
    205206        val.name=name
    206207        self._add_temporary_set(val)
  • coopr.pyomo/stable/2.3/coopr/pyomo/base/intrinsic_functions.py

    r2133 r2433  
    1515#        intrinsic function with the value of that expression?
    1616# TODO:  Do we need to register these expression objects?
     17# TODO:  Verify why we need to allow Components as arguments.
    1718#
    1819
  • coopr.pyomo/stable/2.3/coopr/pyomo/data/parse_datacmds.py

    r2360 r2433  
    1919from pyutilib.misc import flatten
    2020from pyutilib.ply import t_newline, t_ignore, _find_column, p_error, ply_init
    21 
    22 tabmodule = 'parse_table_datacmds'
    23 outputdir = os.path.dirname(os.path.abspath(__file__))+os.sep
    2421
    2522## -----------------------------------------------------------
     
    312309
    313310#
     311# the ampl dat file lexer and yaccer only need to be
     312# created once, so have the corresponding objects
     313# accessible at module scope.
     314#
     315
     316tabmodule = 'parse_table_datacmds'
     317outputdir = os.path.dirname(os.path.abspath(__file__))+os.sep
     318
     319ampl_dat_lexer = None
     320ampl_dat_yaccer = None
     321
     322#
    314323# The function that performs the parsing
    315324#
    316325def parse_data_commands(data=None, filename=None, debug=0):
     326
    317327    global debugging
    318     #
    319     # Always remove the parser.out file, which is generated to create debugging
    320     #
    321     if os.path.exists("parser.out"):        #pragma:nocover
    322        os.remove("parser.out")
    323     if debug > 0:                           #pragma:nocover
    324         #
    325         # Remove the parsetab.py* files.  These apparently need to be removed
    326         # to ensure the creation of a parser.out file.
    327         #
    328         if os.path.exists("parsetab.py"):
    329            os.remove("parsetab.py")
    330         if os.path.exists("parsetab.pyc"):
    331            os.remove("parsetab.pyc")
    332         debugging=True
    333     #
    334     # Build lexer
    335     #
    336     lex.lex()
     328    global ampl_dat_lexer
     329    global ampl_dat_yaccer
     330
     331    # if the lexer/yaccer haven't been initialized, do so.
     332    if ampl_dat_lexer is None:
     333       #
     334       # Always remove the parser.out file, which is generated to create debugging
     335       #
     336       if os.path.exists("parser.out"):        #pragma:nocover
     337           os.remove("parser.out")
     338       if debug > 0:                           #pragma:nocover
     339           #
     340           # Remove the parsetab.py* files.  These apparently need to be removed
     341           # to ensure the creation of a parser.out file.
     342           #
     343           if os.path.exists("parsetab.py"):
     344              os.remove("parsetab.py")
     345           if os.path.exists("parsetab.pyc"):
     346              os.remove("parsetab.pyc")
     347           debugging=True
     348
     349       ampl_dat_lexer = lex.lex()
     350       ampl_dat_yaccer = yacc.yacc(debug=debug, tabmodule=tabmodule, outputdir=outputdir)
     351           
    337352    #
    338353    # Initialize parse object
     
    340355    global _parse_info
    341356    _parse_info = []
    342     #
    343     # Build yaccer
    344     #
    345     yacc.yacc(debug=debug, tabmodule=tabmodule, outputdir=outputdir)
     357
    346358    #
    347359    # Parse the file
     
    351363        _parsedata=data
    352364        ply_init(_parsedata)
    353         yacc.parse(data,debug=debug)
     365        ampl_dat_yaccer.parse(data, lexer=ampl_dat_lexer, debug=debug)
    354366    elif not filename is None:
    355367        f = open(filename)
     
    358370        _parsedata=data
    359371        ply_init(_parsedata)
    360         yacc.parse(data, debug=debug)
     372        ampl_dat_yaccer.parse(data, lexer=ampl_dat_lexer, debug=debug)
    361373    else:
    362374        _parse_info = None
  • coopr.pyomo/stable/2.3/coopr/pyomo/scripting/__init__.py

    r2315 r2433  
    99
    1010from convert import *
     11import pyomo
     12import util
     13
  • coopr.pyomo/stable/2.3/coopr/pyomo/scripting/pyomo.py

    r2315 r2433  
    1313from coopr.pyomo import pyomo
    1414from coopr.opt.base import SolverFactory
     15from coopr.opt.parallel import SolverManagerFactory
    1516from pyutilib.misc import Options, Container
    1617
     
    4849        action="store",
    4950        dest="solver",
    50         type="string",
     51        type="choice",
     52        choices=filter(lambda x: x[0] != '_', SolverFactory()),
    5153        default="glpk")
    5254    parser.add_option("--path",
     
    6567            action="append",
    6668            dest="debug",
    67             choices=debug_choices)
     69            type='choice',
     70            choices=debug_choices,
     71            default=[]
     72            )
    6873    parser.add_option("-k","--keepfiles",
    6974            help="Keep temporary files",
     
    7580            action="store",
    7681            dest="tempdir",
     82            type="string",
    7783            default=None)
    7884    parser.add_option("-q","--quiet",
     
    9096            action="store",
    9197            dest="logfile",
     98            type="string",
    9299            default=None)
    93100    parser.add_option("-s","--summary",
     
    105112            action="store",
    106113            dest="profile",
     114            type="int",
    107115            default=0)
    108116    parser.add_option("--timelimit",
     
    166174            action="store",
    167175            dest="smanager_type",
     176            type="choice",
     177            choices=SolverManagerFactory(),
    168178            default="serial")
    169179    parser.add_option("--stream-output",
     
    176186            action="store",
    177187            dest="save_model",
     188            type="string",
    178189            default=None)
    179190    #
  • coopr.pyomo/stable/2.3/coopr/pyomo/scripting/util.py

    r2315 r2433  
    1313import textwrap
    1414import traceback
    15 import cProfile
    16 import pstats
     15try:
     16    import cProfile as profile
     17except ImportError:
     18    import profile
     19try:
     20    import pstats
     21    pstats_available=True
     22except ImportError:
     23    pstats_available=False
    1724import gc
    1825from coopr.pyomo import *
     
    319326    #
    320327    if options.profile > 0:
     328        if not pstats_available:
     329            raise ValueError, "Cannot use the 'profile' option.  The Python 'pstats' package cannot be imported!"
    321330        tfile = pyutilib.services.TempfileManager.create_tempfile(suffix=".profile")
    322         tmp = cProfile.runctx(command.__name__+'(options=options,args=nargs,parser=parser)',globals(),locals(),tfile)
     331        tmp = profile.runctx(command.__name__+'(options=options,args=nargs,parser=parser)',globals(),locals(),tfile)
    323332        p = pstats.Stats(tfile).strip_dirs()
    324333        p.sort_stats('time', 'cum')
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/examples/test_amplbook2.py

    r1768 r2433  
    33#
    44
    5 import unittest
    65import os
    76import sys
     
    1211
    1312from coopr.pyomo import *
    14 import pyutilib.th
     13import pyutilib.th as unittest
    1514import glob
    1615
    17 class TestAmplbook2(pyutilib.th.TestCase): pass
     16class TestAmplbook2(unittest.TestCase): pass
    1817
    1918#
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/examples/test_pyomo.py

    r2360 r2433  
    1313import pyutilib.services
    1414import pyutilib.subprocess
    15 import pyutilib.th
     15import pyutilib.th as unittest
    1616import coopr.pyomo.scripting.pyomo as main
    1717import StringIO
     
    2323    executable=sys.executable
    2424
    25 class Test(pyutilib.th.TestCase):
    2625
    27     def run(self, result=None):
    28         """
    29         Run the tests if GLPK is available
    30         """
    31         if pyutilib.services.registered_executable("glpsol") is None:
    32             return
    33         pyutilib.th.TestCase.run(self, result)
     26class Test(unittest.TestCase):
    3427
    3528    def pyomo(self, cmd, **kwds):
     
    113106        os.remove(currdir+'test12.out')
    114107
     108Test = unittest.skipIf(pyutilib.services.registered_executable("glpsol") is None, "The 'glpsol' executable is not available")(Test)
    115109
    116110if __name__ == "__main__":
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/examples/test_tutorials.py

    r1813 r2433  
    33#
    44
    5 import unittest
    65import os
    76import sys
     
    1413from coopr.pyomo import *
    1514import pyutilib.misc
    16 import pyutilib.th
     15import pyutilib.th as unittest
    1716
    1817try:
     
    2221  _win32com=False #pragma:nocover
    2322
    24 class PyomoTutorials(pyutilib.th.TestCase):
     23class PyomoTutorials(unittest.TestCase):
    2524
    2625    def setUp(self):
     
    3433        self.failUnlessFileEqualsBaseline(currdir+"data.log", tutorial_dir+"data.out")
    3534
     35    @unittest.skipIf(not _win32com, "Cannot import 'win32com'")
    3636    def test_excel(self):
    37         if not _win32com:
    38            return
    3937        pyutilib.misc.run_file(tutorial_dir+"excel.py", logfile=currdir+"excel.log", execdir=tutorial_dir)
    4038        self.failUnlessFileEqualsBaseline(currdir+"excel.log", tutorial_dir+"excel.out")
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/expr/test_canonical.py

    r2360 r2433  
    33#
    44
    5 import unittest
    65import os
    76import sys
     
    1312from coopr.opt import *
    1413#from coopr.pyomo.base.var import _VarElement
    15 import pyutilib.th
     14import pyutilib.th as unittest
    1615import pyutilib.services
    1716from coopr.pyomo.preprocess.simple_preprocessor import SimplePreprocessor
     
    2726
    2827
    29 class TestBase(pyutilib.th.TestCase):
     28class TestBase(unittest.TestCase):
    3029
    3130    def setUp(self):
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_con.py

    r1994 r2433  
    66#
    77
    8 import unittest
    98import os
    109import sys
     
    1716from coopr.opt import *
    1817from coopr.pyomo.base.var import _VarElement
    19 import pyutilib.th
     18import pyutilib.th as unittest
    2019import pyutilib.services
    2120
    22 class PyomoModel(pyutilib.th.TestCase):
     21class PyomoModel(unittest.TestCase):
    2322
    2423    def setUp(self):
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_concrete.py

    r2360 r2433  
    33#
    44
    5 import unittest
    65import os
    76import sys
     
    1211from coopr.pyomo import *
    1312from coopr.opt import *
    14 import pyutilib.th
     13import pyutilib.th as unittest
    1514import pyutilib.services
    1615
    17 class Test(pyutilib.th.TestCase):
     16class Test(unittest.TestCase):
    1817
    1918    def test_blending(self):
    2019        """ The blending example from the PuLP documentation """
    21         if not pyutilib.services.registered_executable("glpsol"):
    22             return
    2320        model = Model()
    2421        model.concrete_mode()
     
    4239        self.failUnlessFileEqualsBaseline(currdir+"blend.out",currdir+"blend.txt")
    4340
     41Test = unittest.skipIf(not pyutilib.services.registered_executable("glpsol"), "The 'glpsol' executable is not available")(Test)
    4442
    4543if __name__ == "__main__":
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_expr.py

    r2050 r2433  
    66#
    77
    8 import unittest
    98import os
    109import sys
     
    1312currdir = dirname(abspath(__file__))+os.sep
    1413
    15 import pyutilib.th
     14import pyutilib.th as unittest
    1615from coopr.pyomo import *
    1716from nose.tools import nottest
    1817
    19 class PyomoModel(pyutilib.th.TestCase):
     18class PyomoModel(unittest.TestCase):
    2019
    2120    def setUp(self):
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_misc.py

    r1768 r2433  
    33#
    44
    5 import unittest
    65import os
    76import sys
     
    1110
    1211from coopr.pyomo import *
    13 import pyutilib.th
     12import pyutilib.th as unittest
    1413
    1514def rule1(model):
     
    1817    return (1,model.x+model.y[1]+i,2)
    1918
    20 class PyomoModel(pyutilib.th.TestCase):
     19class PyomoModel(unittest.TestCase):
    2120
    2221    def setUp(self):
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_model.py

    r2053 r2433  
    55#
    66
    7 import unittest
    87import os
    98import sys
     
    1615from coopr.opt import *
    1716from coopr.pyomo.base.var import _VarElement
    18 import pyutilib.th
     17import pyutilib.th as unittest
    1918import pyutilib.services
    2019
    21 class PyomoModel(pyutilib.th.TestCase):
     20class PyomoModel(unittest.TestCase):
    2221
    2322    def setUp(self):
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_modeldata.py

    r2315 r2433  
    33#
    44
    5 import unittest
    65import os
    76import sys
     
    1211import coopr
    1312import pyutilib.common
     13import pyutilib.th as unittest
    1414
    1515currdir=dirname(abspath(__file__))+os.sep
     
    2727class PyomoTableData(unittest.TestCase):
    2828
    29     def run(self, result=None):
    30         """ Disable the tests if win32com is not available """
    31         if not _win32com:
    32            return
    33         unittest.TestCase.run(self,result)
    34 
    3529    def setUp(self):
    3630        pass
     
    4135    def test_read_set(self):
    4236        td = DataManagerFactory('xls')
    43         td.initialize(currdir+"Book1.xls", range="TheRange", format='set', set="X")
     37        td.initialize(currdir+"Book1.xls", range="TheRange", format='set', set="X")
    4438        try:
    4539           td.open()
     
    5246    def test_read_param1(self):
    5347        td = DataManagerFactory('xls')
    54         td.initialize(currdir+"Book1.xls", range="TheRange", index=['aa'], param=['bb','cc','dd'])
     48        td.initialize(currdir+"Book1.xls", range="TheRange", index=['aa'], param=['bb','cc','dd'])
    5549        try:
    5650          td.open()
     
    6357    def test_read_param2(self):
    6458        td = DataManagerFactory('xls')
    65         td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa'], param=['bb','cc','dd'])
     59        td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa'], param=['bb','cc','dd'])
    6660        try:
    6761          td.open()
     
    7468    def test_read_param3(self):
    7569        td = DataManagerFactory('xls')
    76         td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa','bb','cc'], param=["dd"], param_name={'dd':'a'})
     70        td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa','bb','cc'], param=["dd"], param_name={'dd':'a'})
    7771        try:
    7872          td.open()
     
    8579    def test_read_param4(self):
    8680        td = DataManagerFactory('xls')
    87         td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa','bb'], param=['cc','dd'], param_name={'cc':'a', 'dd':'b'})
     81        td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa','bb'], param=['cc','dd'], param_name={'cc':'a', 'dd':'b'})
    8882        try:
    8983          td.open()
     
    9690    def test_read_array1(self):
    9791        td = DataManagerFactory('xls')
    98         td.initialize(currdir+"Book1.xls",range="TheRange", param="X", format="array")
     92        td.initialize(currdir+"Book1.xls",range="TheRange", param="X", format="array")
    9993        try:
    10094          td.open()
     
    107101    def test_read_array2(self):
    108102        td = DataManagerFactory('xls')
    109         td.initialize(currdir+"Book1.xls",range="TheRange",param="X",format="transposed_array")
     103        td.initialize(currdir+"Book1.xls",range="TheRange",param="X",format="transposed_array")
    110104        try:
    111105          td.open()
     
    118112    def test_error1(self):
    119113        td = DataManagerFactory('xls')
    120         td.initialize("bad")
     114        td.initialize("bad")
    121115        try:
    122116            td.open()
     
    136130        td = DataManagerFactory('txt')
    137131        try:
    138             td.initialize(currdir+"display.txt")
     132            td.initialize(currdir+"display.txt")
    139133            td.open()
    140134            self.fail("Expected IOError because of bad file type")
     
    145139        td = DataManagerFactory('txt')
    146140        try:
    147             td.initialize(filename=currdir+"dummy")
     141            td.initialize(filename=currdir+"dummy")
    148142            td.open()
    149143            self.fail("Expected IOError because of bad file type")
     
    153147    def test_error5(self):
    154148        td = DataManagerFactory('tab')
    155         td.initialize(example_dir+"D.tab", param="D", format="foo")
     149        td.initialize(example_dir+"D.tab", param="D", format="foo")
    156150        td.open()
    157151        try:
     
    161155            pass
    162156
     157PyomoTableData = unittest.skipIf(not _win32com, "Cannot import 'win32com'")(PyomoTableData)
    163158
    164159class PyomoModelData(unittest.TestCase):
     
    720715class TestSpreadsheet(unittest.TestCase):
    721716
    722     def run(self, result=None):
    723         """ Disable the tests if win32com is not available """
    724         if not _win32com:
    725            return
    726         unittest.TestCase.run(self,result)
    727 
    728717    def test_tableA1(self):
    729718        """Importing a single column of data"""
     
    740729        """Importing a single column of data"""
    741730        pyutilib.misc.setup_redirect(currdir+'importA2.dat')
    742         print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Atable ;"
     731        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Atable ;"
    743732        pyutilib.misc.reset_redirect()
    744733        model=Model()
     
    941930        os.remove(currdir+'importPO.dat')
    942931
     932TestSpreadsheet = unittest.skipIf(not _win32com, "Cannot import 'win32com'")(TestSpreadsheet)
     933
    943934
    944935class TestModelData(unittest.TestCase):
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_obj.py

    r1994 r2433  
    66#
    77
    8 import unittest
    98import os
    109import sys
     
    1716from coopr.opt import *
    1817from coopr.pyomo.base.var import _VarElement
    19 import pyutilib.th
     18import pyutilib.th as unittest
    2019import pyutilib.services
    2120
    22 class PyomoModel(pyutilib.th.TestCase):
     21class PyomoModel(unittest.TestCase):
    2322
    2423    def setUp(self):
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_preprocess.py

    r2360 r2433  
    33#
    44
    5 import unittest
    65import os
    76import sys
     
    109currdir = dirname(abspath(__file__))+os.sep
    1110
    12 import pyutilib.th
     11import pyutilib.th as unittest
    1312from coopr.pyomo import *
    1413from nose.tools import nottest
    1514
    1615
    17 class TestPreprocess(pyutilib.th.TestCase):
     16class TestPreprocess(unittest.TestCase):
    1817
    1918    def test_label1(self):
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_transform.py

    r2201 r2433  
    33#
    44
    5 import unittest
    65import os
    76import sys
     
    1211from coopr.pyomo import *
    1312from coopr.opt import *
    14 import pyutilib.th
     13import pyutilib.th as unittest
    1514import pyutilib.services
    1615from pyutilib.component.core import Plugin
    1716
    18 class Test(pyutilib.th.TestCase):
     17class Test(unittest.TestCase):
    1918
    2019    def setUp(self):
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_util.py

    r1768 r2433  
    33#
    44
    5 import unittest
    65import os
    76import sys
     
    109currdir = dirname(abspath(__file__))+os.sep
    1110
    12 import pyutilib.th
     11import pyutilib.th as unittest
    1312from coopr.pyomo import *
    1413from nose.tools import nottest
     
    1615
    1716
    18 class TestNumericValue(pyutilib.th.TestCase):
     17class TestNumericValue(unittest.TestCase):
    1918
    2019    def test_expr1(self):
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_var.py

    r1994 r2433  
    66#
    77
    8 import unittest
    98import os
    109import sys
     
    1716from coopr.opt import *
    1817from coopr.pyomo.base.var import _VarElement
    19 import pyutilib.th
     18import pyutilib.th as unittest
    2019import pyutilib.services
    2120
    22 class PyomoModel(pyutilib.th.TestCase):
     21class PyomoModel(unittest.TestCase):
    2322
    2423    def setUp(self):
  • coopr.pyomo/stable/2.3/scripts/pyomo

    r2146 r2433  
    1717import traceback
    1818import sys
    19 if sys.version_info[0:2] < (2,5,3):
     19if sys.version_info[0:3] < (2,5,1):
    2020   print ""
    21    print "ERROR: Pyomo requires Python 2.5.3 or newer"
     21   print "ERROR: Pyomo requires Python 2.5.1 or newer"
    2222   sys.exit(1)
    2323from os.path import abspath, dirname
Note: See TracChangeset for help on using the changeset viewer.