Esempio n. 1
0
    def test6a_kwds_return_apidata(self):
        #"""Outputs specified in docstring: keyword arguments, non-data return values with data"""
        @pyomo_api
        def test6a(data, x=1, y=2):
            """
            Required:
                data: input data
                x: integer
            Optional:
                y: integer
            Return:
                data: output data
                z: integer
            """
            data.a = y
            data.b[0] = x
            return PyomoAPIData(data=data, z=x)

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test6a(options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test6a(data=options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        self.assertEqual(retval.z, 2)
Esempio n. 2
0
    def test4_kwds_return_data(self):
        #"""Simple test: keyword arguments, simple return value"""
        @pyomo_api
        def test4(data, x=1, y=2):
            """
            Required:
                data: input data
                x: integer
            Optional:
                y: integer
            Return:
                data: output data
            """
            data.a = y
            data.b[0] = x
            return data

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test4(options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test4(data=options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
Esempio n. 3
0
    def test5_kwds_return_apidata(self):
        #"""Simple test: keyword arguments, non-data return values"""
        @pyomo_api(outputs=('z'))
        def test5(data, x=1, y=2):
            """
            Required:
                data: input data
                x: integer
            Optional:
                y: integer
            Return:
                data: output data
                z: integer
            """
            data.a = y
            data.b[0] = x
            return PyomoAPIData(z=x)

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test5(options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test5(data=options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        self.assertEqual(retval.z, 2)
Esempio n. 4
0
    def test1b_data_kwds_no_return(self):
        #"""Simple test: data keyword argument, no return values"""
        @pyomo_api
        def test1b(data=None):
            data.a = 2
            data.b[0] = 2

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test1b(options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test1b(data=options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
Esempio n. 5
0
 def test_print_PyomoAPIData_repr(self):
     #"""Print PyomoAPIData representation"""
     data = PyomoAPIData()
     data.a = 1
     data.b = [1, 2]
     data.c = PyomoAPIData()
     data.c.x = 1
     data.c.y = 2
     data['aa'] = 'here is more'
     data.clean()
     # Because the PyomoAPIData is a dict, the repr() is subject to
     # change between python versions.  Cast back to a dict and
     # verify.
     self.assertEqual(
         eval(repr(data)),
         eval("{'a': 1, 'aa': 'here is more', 'b': [1, 2], "
              "'c': {'y': 2, 'x': 1}}"))
     self.assertEqual(len(data._dirty_), 0)
Esempio n. 6
0
    def test2_implicit_no_kwds_return_data(self):
        #"""Simple test: no keyword arguments, returning data"""
        @pyomo_api
        def test2(data):
            data.a = 2
            data.b[0] = 2
            return data

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test2(options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test2(data=options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
Esempio n. 7
0
    def test_print_PyomoAPIData_string(self):
        #"""Print PyomoAPIData string"""
        data = PyomoAPIData()
        data.a = 1
        data.b = [1, 2]
        data.c = PyomoAPIData()
        data.c.x = 1
        data.c.y = 2
        data['aa'] = 'here is more'
        data.clean()
        self.assertEqual(sorted(data.unused()), ['a', 'aa', 'b', 'c'])
        self.assertEqual(
            str(data), """a: 1
aa: here is more
b: [1, 2]
c:
    x: 1
    y: 2""")
        self.assertEqual(len(data._dirty_), 0)
Esempio n. 8
0
    def test9_complex_documentation(self):
        #"""Simple test: no keyword arguments or return values"""
        @pyomo_api
        def test9(data):
            """
            This is the
            short
            documentation.

            This

            is

            the

            long documentation.
            Required:
                data: multiline
                      description of
                      input data object
                data.a: More data
            """
            data.a = 2
            data.b[0] = 2

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test9(options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test9(data=options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        #
        self.assertTrue('test1' in PyomoAPIFactory.services())
        self.assertEqual(test9.__short_doc__,
                         'This is the\nshort\ndocumentation.')
        self.assertEqual(test9.__long_doc__,
                         'This\n\nis\n\nthe\n\nlong documentation.')
Esempio n. 9
0
    def test1_no_kwds_no_return(self):
        #"""Simple test: no keyword arguments or return values"""
        @pyomo_api
        def test1(data):
            """
            Required:
                data: input data
            """
            data.a = 2
            data.b[0] = 2

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test1(options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test1(data=options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        #
        self.assertTrue('test1' in PyomoAPIFactory.services())
Esempio n. 10
0
    def test8_nested_data(self):
        #"""Simple test with required nested data"""
        @pyomo_api
        def test8(data):
            """
            Required:
                data: input data
                data.foo.bar:
            """
            data.foo.foo = 3
            #data.a = 2
            #data.b[0] = 2

        #
        options = PyomoAPIData()
        options.foo = PyomoAPIData()
        options.foo.bar = 1
        options.a = 1
        options.b = [1, 2]
        retval = test8(options)
        self.assertEqual(retval.data.a, 1)
        self.assertEqual(retval.data.b, [1, 2])
        self.assertEqual(retval.data.foo.foo, 3)
Esempio n. 11
0
    def test1a_no_kwds_single_return(self):
        #"""Simple test: no keyword arguments or return values"""
        @pyomo_api
        def test1a(data):
            """
            Required:
                data: input data
            Return:
                data: output data
            """
            data.a = 2
            data.b[0] = 2

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test1a(options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test1a(data=options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
Esempio n. 12
0
    def test_err10_test_with_nested_data(self):
        #"""Simple test with required nested data"""
        @pyomo_api
        def err10(data):
            """
            Required:
                data: input data
                data.foo.bar:
            """
            data.foo.foo = 3
            with self.assertRaisesRegexp(AttributeError,
                                         "Undeclared attribute 'a'"):
                data.a = 2

        #
        options = PyomoAPIData()
        options.foo = PyomoAPIData()
        options.foo.bar = 1
        options.a = 1
        options.b = [1, 2]
        retval = err10(options)
        self.assertEqual(retval.data.a, 1)
        self.assertEqual(retval.data.b, [1, 2])
        self.assertEqual(retval.data.foo.foo, 3)
Esempio n. 13
0
    def test3a_kwds_no_return(self):
        #"""Simple test: keyword arguments, no return values"""
        @pyomo_api
        def test3a(x=1, y=2, data=None):
            """
            Required:
                data: input data
                x: integer
            Optional:
                y: integer
            """
            data.a = y
            data.b[0] = x

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test3a(options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test3a(data=options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])