Ejemplo n.º 1
0
def test_inspect():
    model_code = """\
import wtforms as wtf
from parampool.html5.flask.fields import FloatField

class Test(wtf.Form):
    a        = FloatField(validators=[wtf.validators.InputRequired()])
    b        = wtf.IntegerField(default=1,
                             validators=[wtf.validators.InputRequired()])
    c        = FloatField(default=1.0,
                             validators=[wtf.validators.InputRequired()])
    func     = wtf.TextField(default='y(x)',
                             validators=[wtf.validators.InputRequired()])
    mybool   = wtf.BooleanField(default=True)
    filename = wtf.FileField(validators=[wtf.validators.InputRequired()])
"""

    def compute(a, b=1, c=1.0, func='y(x)', mybool=True, filename='tmp.tmp'):
        return None

    generated_code = generate_model(compute, "Test", outfile=None,
                                     default_field="FloatField")
    assert_equal_text(generated_code, model_code,
                      'newly generated', 'reference result',
                      msg='Error in generated text!')
Ejemplo n.º 2
0
def test_inspect():
    model_code = """\
import wtforms as wtf
from parampool.html5.flask.fields import FloatField

class Test(wtf.Form):
    a        = FloatField(validators=[wtf.validators.InputRequired()])
    b        = wtf.IntegerField(default=1,
                             validators=[wtf.validators.InputRequired()])
    c        = FloatField(default=1.0,
                             validators=[wtf.validators.InputRequired()])
    func     = wtf.TextField(default='y(x)',
                             validators=[wtf.validators.InputRequired()])
    mybool   = wtf.BooleanField(default=True)
    filename = wtf.FileField(validators=[wtf.validators.InputRequired()])
"""

    def compute(a, b=1, c=1.0, func='y(x)', mybool=True, filename='tmp.tmp'):
        return None

    generated_code = generate_model(compute,
                                    "Test",
                                    outfile=None,
                                    default_field="FloatField")
    assert_equal_text(generated_code,
                      model_code,
                      'newly generated',
                      'reference result',
                      msg='Error in generated text!')
Ejemplo n.º 3
0
def test_load_pool_from_file():
    import StringIO
    file_content = """
subpool main
print intermediate results
U = 1.0      ! m/s  # velocity

subpool fluid properties
rho = 1.2    ! kg/m**3  # density
mu = 5E.5
end

subpool body properties
m = 3  ! kg  # mass
V = 4  ! m/s
A
d
C_D
end
"""
    m = load_pool_from_file(StringIO.StringIO(file_content))
    print m
    reference = """\
sub pool "main" (level=0)
    print intermediate results
    U
    subsub pool "fluid properties" (level=1)
        rho
        mu
    subsub pool "body properties" (level=1)
        m
        V
        A
        d
        C_D"""
    assert_equal_text(str(m), reference)

    reference = """\
subpool main
    print intermediate results
    U = 1.0   !  m/s     #  velocity
    subpool fluid properties
        rho = 1.2   !  kg/m**3     #  density
        mu =  5E.5
    end

    subpool body properties
        m = 3   !  kg     #  mass
        V = 4
        A
        d
        C_D
    end

end
"""
    assert_equal_text(write_poolfile(m), reference)
Ejemplo n.º 4
0
def test_load_pool_from_file():
    import StringIO
    file_content = """
subpool main
print intermediate results
U = 1.0      ! m/s  # velocity

subpool fluid properties
rho = 1.2    ! kg/m**3  # density
mu = 5E.5
end

subpool body properties
m = 3  ! kg  # mass
V = 4  ! m/s
A
d
C_D
end
"""
    m = load_pool_from_file(StringIO.StringIO(file_content))
    print m
    reference = """\
sub pool "main" (level=0)
    print intermediate results
    U
    subsub pool "fluid properties" (level=1)
        rho
        mu
    subsub pool "body properties" (level=1)
        m
        V
        A
        d
        C_D"""
    assert_equal_text(str(m), reference)

    reference = """\
subpool main
    print intermediate results
    U = 1.0   !  m/s     #  velocity
    subpool fluid properties
        rho = 1.2   !  kg/m**3     #  density
        mu =  5E.5
    end

    subpool body properties
        m = 3   !  kg     #  mass
        V = 4
        A
        d
        C_D
    end

end
"""
    assert_equal_text(write_poolfile(m), reference)
Ejemplo n.º 5
0
def test_listtree2Pool():
    from math import pi
    tree = [
        'main',
        [
            dict(name='print intermediate results', default=False),
            dict(name='U',
                 default=120,
                 unit='km/h',
                 help='velocity of body',
                 str2type=eval),
            'fluid properties',
            [
                dict(name='rho', default=1.2, unit='kg/m**3', help='density'),
                dict(name='mu', default=2E-5, help='viscosity'),
            ],
            'body properties',
            [
                dict(name='m', default=0.43, unit='kg', help='mass'),
                dict(name='V', default=pi * 0.11**3, help='volume'),
                dict(name='A',
                     default=pi * 0.11**2,
                     unit='m**2',
                     help='cross section area'),
                dict(name='d', default=2 * 0.11, unit='m', help='diameter'),
                dict(name='C_D',
                     default=0.2,
                     minmax=[0, 1],
                     help='drag coefficient'),
            ],
        ],
    ]
    m = listtree2Pool(tree)
    reference = """\
sub pool "main" (level=0)
    print intermediate results
    U
    subsub pool "fluid properties" (level=1)
        rho
        mu
    subsub pool "body properties" (level=1)
        m
        V
        A
        d
        C_D"""
    assert_equal_text(str(m), reference)
    return m
Ejemplo n.º 6
0
def test_listtree2Pool():
    from math import pi
    tree = [
        'main', [
            dict(name='print intermediate results', default=False),
            dict(name='U', default=120, unit='km/h',
                 help='velocity of body', str2type=eval),
            'fluid properties', [
                dict(name='rho', default=1.2, unit='kg/m**3',
                     help='density'),
                dict(name='mu', default=2E-5, help='viscosity'),
                ],
            'body properties', [
                dict(name='m', default=0.43, unit='kg', help='mass'),
                dict(name='V', default=pi*0.11**3, help='volume'),
                dict(name='A', default=pi*0.11**2, unit='m**2',
                     help='cross section area'),
                dict(name='d', default=2*0.11, unit='m',
                     help='diameter'),
                dict(name='C_D', default=0.2, minmax=[0,1],
                     help='drag coefficient'),
                ],
            ],
        ]
    m = listtree2Pool(tree)
    reference = """\
sub pool "main" (level=0)
    print intermediate results
    U
    subsub pool "fluid properties" (level=1)
        rho
        mu
    subsub pool "body properties" (level=1)
        m
        V
        A
        d
        C_D"""
    assert_equal_text(str(m), reference)
    return m
Ejemplo n.º 7
0
def test_Tree_basics():
    t = Tree(root_name="main")

    class Leaf:
        def __init__(self, name, default):
            self.name = name
            self.default = default

        def __str__(self):
            return '%s "%s"' % (self.__class__.__name__, self.name)

    t.add_leaf(Leaf(name="item1", default=1.0))
    nt.assert_equal(t.locator.name, "main")
    nt.assert_equal(t.locator.tree[-1].name, "item1")

    t.add_leaf(Leaf(name="item2", default=2.0))
    t.subtree("sub1")
    nt.assert_equal(t.locator.name, "sub1")

    t.add_leaf(Leaf(name="item3", default=3))
    nt.assert_equal(t.locator.name, "sub1")
    nt.assert_equal(t.locator.tree[-1].name, "item3")

    t.subtree("../sub2")
    t.add_leaf(Leaf(name="item4", default=4))
    nt.assert_equal(t.locator.name, "sub2")
    nt.assert_equal(t.locator.tree[-1].name, "item4")
    nt.assert_equal(str(t.locator), '[Leaf "item4"]')
    nt.assert_equal(t.locator.get_parent().name, "main")

    t.subtree("sub3")
    t.add_leaf(Leaf(name="item5", default=5))
    t.subtree("sub4")
    t.add_leaf(Leaf(name="item6", default=6))
    t.add_leaf(Leaf(name="item7", default=7))
    t.add_leaf(Leaf(name="item8", default=8))
    t.add_leaf(Leaf(name="item9", default=9))
    t.subtree("..")
    nt.assert_equal(t.locator.name, "sub3")

    t.add_leaf(Leaf(name="item10", default=10))
    t.subtree("sub4")
    t.subtree("../../sub5")
    t.add_leaf(Leaf(name="item11", default=11))
    t.subtree("/sub2/sub3/sub4")
    t.add_leaf(Leaf(name="item12", default=12))
    reference = """\
item1
item2
sub tree "sub1" (level=0)
    item3
sub tree "sub2" (level=0)
    item4
    subsub tree "sub3" (level=1)
        item5
        subsubsub tree "sub4" (level=2)
            item6
            item7
            item8
            item9
            item12
        item10
    subsub tree "sub5" (level=1)
        item11"""
    assert_equal_text(str(t), reference)

    reference = '''\
Leaf "item1"
Leaf "item2"
sub tree "sub1" (level=0)
    Leaf "item3"
sub tree "sub2" (level=0)
    Leaf "item4"
    subsub tree "sub3" (level=1)
        Leaf "item5"
        subsubsub tree "sub4" (level=2)
            Leaf "item6"
            Leaf "item7"
            Leaf "item8"
            Leaf "item9"
            Leaf "item12"
        Leaf "item10"
    subsub tree "sub5" (level=1)
        Leaf "item11"'''
    assert_equal_text(dump(t), reference)
Ejemplo n.º 8
0
def test_pool():
    from math import pi
    from parampool.pool.UI import listtree2Pool

    model_code = """\
import wtforms as wtf
from parampool.html5.flask.fields import FloatField, RangeFloatField

class Test(wtf.Form):
     a     = wtf.IntegerField(u'velocity of body (km/h)',
                        default=120,
                        validators=[wtf.validators.InputRequired()])
     b     = FloatField(u'mass (kg)',
                        default=0.43,
                        validators=[wtf.validators.InputRequired()])
     c     = FloatField(u'volume',
                        default=0.00418146,
                        validators=[wtf.validators.InputRequired()])
     C_D   = FloatField(u'drag coefficient',
                        default=0.2,
                        validators=[wtf.validators.InputRequired(),
                                    wtf.validators.NumberRange(0,
                                                               1)])
     test1 = RangeFloatField(u'rangetest',
                        onchange="showValue(this.value)",
                        min=0,
                        max=1,
                        default=0.5,
                        validators=[wtf.validators.InputRequired(),
                                    wtf.validators.NumberRange(0,
                                                               1)])
     test2 = wtf.FileField(u'filetest',
                        validators=[wtf.validators.InputRequired())
     test3 = wtf.SelectField(u'choicetest',
                        default='y',
                        validators=[wtf.validators.InputRequired()],
                        choices=[('y', 'y'), ('y3', 'y^3'), ('siny', 'sin(y)')])
     test4 = wtf.TextField(u'texttest',
                        default='Test',
                        validators=[wtf.validators.InputRequired()])
     test5 = wtf.BooleanField(u'booltest', default=True)
"""

    pool_tree = [
        'main',
        [
            dict(name='a',
                 default=120,
                 unit='km/h',
                 help='velocity of body',
                 str2type=eval),
            'test properties1',
            [
                dict(name='b', default=0.43, unit='kg', help='mass'),
                dict(name='c', default=pi * 0.11**3, help='volume'),
                dict(name='C_D',
                     default=0.2,
                     minmax=[0, 1],
                     help='drag coefficient'),
            ],
            'test properties2',
            [
                dict(name='test1',
                     default=0.5,
                     help='rangetest',
                     widget='RangeField',
                     minmax=[0, 1]),
                dict(name='test2',
                     default='',
                     help='filetest',
                     widget='FileField'),
                dict(name='test3',
                     default='y',
                     help='choicetest',
                     options=[('y', 'y'), ('y3', 'y^3'), ('siny', 'sin(y)')]),
                dict(name='test4', default='Test', help='texttest'),
                dict(name='test5', default=True, help='booltest'),
            ],
        ],
    ]

    pool = listtree2Pool(pool_tree)

    def compute_func(a, b, c, C_D, test1, test2, test3, test4):
        return None

    generated_code = generate_model(compute_func,
                                    "Test",
                                    outfile=None,
                                    default_field="FloatField",
                                    pool=pool)
    assert_equal_text(generated_code,
                      model_code,
                      'newly generated',
                      'reference result',
                      msg='Error in generated text!')
Ejemplo n.º 9
0
def test_Tree_basics():
    t = Tree(root_name='main')

    class Leaf:
        def __init__(self, name, default):
            self.name = name
            self.default = default

        def __str__(self):
            return '%s "%s"' % (self.__class__.__name__, self.name)

    t.add_leaf(Leaf(name='item1', default=1.0))
    nt.assert_equal(t.locator.name, 'main')
    nt.assert_equal(t.locator.tree[-1].name, 'item1')

    t.add_leaf(Leaf(name='item2', default=2.0))
    t.subtree('sub1')
    nt.assert_equal(t.locator.name, 'sub1')

    t.add_leaf(Leaf(name='item3', default=3))
    nt.assert_equal(t.locator.name, 'sub1')
    nt.assert_equal(t.locator.tree[-1].name, 'item3')

    t.subtree('../sub2')
    t.add_leaf(Leaf(name='item4', default=4))
    nt.assert_equal(t.locator.name, 'sub2')
    nt.assert_equal(t.locator.tree[-1].name, 'item4')
    nt.assert_equal(str(t.locator), '[Leaf "item4"]')
    nt.assert_equal(t.locator.get_parent().name, 'main')

    t.subtree('sub3')
    t.add_leaf(Leaf(name='item5', default=5))
    t.subtree('sub4')
    t.add_leaf(Leaf(name='item6', default=6))
    t.add_leaf(Leaf(name='item7', default=7))
    t.add_leaf(Leaf(name='item8', default=8))
    t.add_leaf(Leaf(name='item9', default=9))
    t.subtree('..')
    nt.assert_equal(t.locator.name, 'sub3')

    t.add_leaf(Leaf(name='item10', default=10))
    t.subtree('sub4')
    t.subtree('../../sub5')
    t.add_leaf(Leaf(name='item11', default=11))
    t.subtree('/sub2/sub3/sub4')
    t.add_leaf(Leaf(name='item12', default=12))
    reference = """\
item1
item2
sub tree "sub1" (level=0)
    item3
sub tree "sub2" (level=0)
    item4
    subsub tree "sub3" (level=1)
        item5
        subsubsub tree "sub4" (level=2)
            item6
            item7
            item8
            item9
            item12
        item10
    subsub tree "sub5" (level=1)
        item11"""
    assert_equal_text(str(t), reference)

    reference = '''\
Leaf "item1"
Leaf "item2"
sub tree "sub1" (level=0)
    Leaf "item3"
sub tree "sub2" (level=0)
    Leaf "item4"
    subsub tree "sub3" (level=1)
        Leaf "item5"
        subsubsub tree "sub4" (level=2)
            Leaf "item6"
            Leaf "item7"
            Leaf "item8"
            Leaf "item9"
            Leaf "item12"
        Leaf "item10"
    subsub tree "sub5" (level=1)
        Leaf "item11"'''
    assert_equal_text(dump(t), reference)
Ejemplo n.º 10
0
def test_Pool():
    p = Pool()
    p.add_data_item(name="item1", default=1.0)
    nt.assert_equal(p.locator.name, "main")
    nt.assert_equal(p.locator.tree[-1].name, "item1")

    p.add_data_item(name="item2", default=2.0)
    p.subtree("sub1")
    nt.assert_equal(p.locator.name, "sub1")

    p.add_data_item(name="item3", default=3)
    nt.assert_equal(p.locator.name, "sub1")
    nt.assert_equal(p.locator.tree[-1].name, "item3")

    p.subtree("../sub2")
    p.add_data_item(name="item4", default=4)
    nt.assert_equal(p.locator.name, "sub2")
    nt.assert_equal(p.locator.tree[-1].name, "item4")
    nt.assert_equal(str(p.locator), '[DataItem "item4"]')
    nt.assert_equal(p.locator.get_parent().name, "main")

    p.subtree("sub3")
    p.add_data_item(name="item5", default=5)
    p.subtree("sub4")
    p.add_data_item(name="item6", default=6)
    p.add_data_item(name="item7", default=7)
    p.add_data_item(name="item8", default=8)
    p.add_data_item(name="item9", default=9)
    p.subtree("..")
    nt.assert_equal(p.locator.name, "sub3")

    p.add_data_item(name="item10", default=10)
    p.subtree("sub4")
    p.subtree("../../sub5")
    p.add_data_item(name="item11", default=11)
    p.subtree("/sub2/sub3/sub4")
    p.add_data_item(name="item12", default=12)
    p.update()

    reference = """\
item1
item2
sub pool "sub1" (level=0)
    item3
sub pool "sub2" (level=0)
    item4
    subsub pool "sub3" (level=1)
        item5
        subsubsub pool "sub4" (level=2)
            item6
            item7
            item8
            item9
            item12
        item10
    subsub pool "sub5" (level=1)
        item11"""
    assert_equal_text(str(p), reference)

    # Test setting values
    return p
Ejemplo n.º 11
0
def test_Pool():
    p = Pool()
    p.add_data_item(name='item1', default=1.0)
    nt.assert_equal(p.locator.name, 'main')
    nt.assert_equal(p.locator.tree[-1].name, 'item1')

    p.add_data_item(name='item2', default=2.0)
    p.subtree('sub1')
    nt.assert_equal(p.locator.name, 'sub1')

    p.add_data_item(name='item3', default=3)
    nt.assert_equal(p.locator.name, 'sub1')
    nt.assert_equal(p.locator.tree[-1].name, 'item3')

    p.subtree('../sub2')
    p.add_data_item(name='item4', default=4)
    nt.assert_equal(p.locator.name, 'sub2')
    nt.assert_equal(p.locator.tree[-1].name, 'item4')
    nt.assert_equal(str(p.locator), '[DataItem "item4"]')
    nt.assert_equal(p.locator.get_parent().name, 'main')

    p.subtree('sub3')
    p.add_data_item(name='item5', default=5)
    p.subtree('sub4')
    p.add_data_item(name='item6', default=6)
    p.add_data_item(name='item7', default=7)
    p.add_data_item(name='item8', default=8)
    p.add_data_item(name='item9', default=9)
    p.subtree('..')
    nt.assert_equal(p.locator.name, 'sub3')

    p.add_data_item(name='item10', default=10)
    p.subtree('sub4')
    p.subtree('../../sub5')
    p.add_data_item(name='item11', default=11)
    p.subtree('/sub2/sub3/sub4')
    p.add_data_item(name='item12', default=12)
    p.update()

    reference = """\
item1
item2
sub pool "sub1" (level=0)
    item3
sub pool "sub2" (level=0)
    item4
    subsub pool "sub3" (level=1)
        item5
        subsubsub pool "sub4" (level=2)
            item6
            item7
            item8
            item9
            item12
        item10
    subsub pool "sub5" (level=1)
        item11"""
    assert_equal_text(str(p), reference)

    # Test setting values
    return p
Ejemplo n.º 12
0
def test_pool():
    from math import pi
    from parampool.pool.UI import listtree2Pool

    model_code = """\
import wtforms as wtf
from parampool.html5.flask.fields import HTML5FloatField, FloatRangeField, IntegerRangeField
import flask.ext.wtf.html5 as html5

class Test(wtf.Form):
     a     = wtf.TextField(u'velocity of body (km/h)',
                        default='120',
                        validators=[wtf.validators.InputRequired()])
     b     = FloatField(u'mass (kg)',
                        default=0.43,
                        validators=[wtf.validators.InputRequired()])
     c     = FloatField(u'volume',
                        default=0.00418146,
                        validators=[wtf.validators.InputRequired()])
     C_D   = FloatField(u'drag coefficient',
                        default=0.2,
                        validators=[wtf.validators.InputRequired(),
                                    wtf.validators.NumberRange(0,
                                                               1)])
     test1 = FloatRangeField(u'rangetest',
                        onchange="showValue(this.value)",
                        min=0,
                        max=1,
                        default=0.5,
                        validators=[wtf.validators.InputRequired(),
                                    wtf.validators.NumberRange(0,
                                                               1)])
     test2 = wtf.FileField(u'filetest',
                        validators=[wtf.validators.InputRequired()])
     test3 = wtf.SelectField(u'choicetest',
                        default='y',
                        validators=[wtf.validators.InputRequired()],
                        choices=[('y', 'y'), ('y3', 'y^3'), ('siny', 'sin(y)')])
     test4 = wtf.TextField(u'texttest',
                        default='Test',
                        validators=[wtf.validators.InputRequired()])
     test5 = wtf.BooleanField(u'booltest', default=True)
"""

    pool_tree = [
        "main",
        [
            dict(name="a", default="120", unit="km/h", help="velocity of body", str2type=eval),
            "test properties1",
            [
                dict(name="b", default=0.43, unit="kg", help="mass"),
                dict(name="c", default=pi * 0.11 ** 3, help="volume"),
                dict(name="C_D", default=0.2, minmax=[0, 1], help="drag coefficient"),
            ],
            "test properties2",
            [
                dict(name="test1", default=0.5, help="rangetest", widget="range", minmax=[0, 1]),
                dict(name="test2", default="", help="filetest", widget="file"),
                dict(
                    name="test3",
                    default="y",
                    help="choicetest",
                    widget="select",
                    options=[("y", "y"), ("y3", "y^3"), ("siny", "sin(y)")],
                ),
                dict(name="test4", default="Test", help="texttest"),
                dict(name="test5", default=True, str2type=bool, help="booltest"),
            ],
        ],
    ]

    pool = listtree2Pool(pool_tree)

    def compute_func(a, b, c, C_D, test1, test2, test3, test4):
        return None

    generated_code = generate_model(compute_func, "Test", outfile=None, default_field="FloatField", pool=pool)
    assert_equal_text(generated_code, model_code, "newly generated", "reference result", msg="Error in generated text!")
Ejemplo n.º 13
0
def test_pool():
    from math import pi
    from parampool.pool.UI import listtree2Pool

    model_code = """\
import wtforms as wtf
from parampool.html5.flask.fields import FloatField, RangeFloatField

class Test(wtf.Form):
     a     = wtf.IntegerField(u'velocity of body (km/h)',
                        default=120,
                        validators=[wtf.validators.InputRequired()])
     b     = FloatField(u'mass (kg)',
                        default=0.43,
                        validators=[wtf.validators.InputRequired()])
     c     = FloatField(u'volume',
                        default=0.00418146,
                        validators=[wtf.validators.InputRequired()])
     C_D   = FloatField(u'drag coefficient',
                        default=0.2,
                        validators=[wtf.validators.InputRequired(),
                                    wtf.validators.NumberRange(0,
                                                               1)])
     test1 = RangeFloatField(u'rangetest',
                        onchange="showValue(this.value)",
                        min=0,
                        max=1,
                        default=0.5,
                        validators=[wtf.validators.InputRequired(),
                                    wtf.validators.NumberRange(0,
                                                               1)])
     test2 = wtf.FileField(u'filetest',
                        validators=[wtf.validators.InputRequired())
     test3 = wtf.SelectField(u'choicetest',
                        default='y',
                        validators=[wtf.validators.InputRequired()],
                        choices=[('y', 'y'), ('y3', 'y^3'), ('siny', 'sin(y)')])
     test4 = wtf.TextField(u'texttest',
                        default='Test',
                        validators=[wtf.validators.InputRequired()])
     test5 = wtf.BooleanField(u'booltest', default=True)
"""

    pool_tree = [
        'main', [
            dict(name='a', default=120, unit='km/h',
                 help='velocity of body', str2type=eval),
            'test properties1', [
                dict(name='b', default=0.43, unit='kg', help='mass'),
                dict(name='c', default=pi*0.11**3, help='volume'),
                dict(name='C_D', default=0.2, minmax=[0,1],
                     help='drag coefficient'),
                ],
            'test properties2', [
                dict(name='test1', default=0.5, help='rangetest',
                     widget='RangeField', minmax=[0,1]),
                dict(name='test2', default='', help='filetest',
                     widget='FileField'),
                dict(name='test3', default='y', help='choicetest',
                     options=[('y', 'y'), ('y3', 'y^3'), ('siny', 'sin(y)')]),
                dict(name='test4', default='Test', help='texttest'),
                dict(name='test5', default=True, help='booltest'),
                ],
            ],
        ]

    pool = listtree2Pool(pool_tree)
    def compute_func(a, b, c, C_D, test1, test2, test3, test4):
        return None

    generated_code = generate_model(compute_func, "Test", outfile=None,
                                    default_field="FloatField", pool=pool)
    assert_equal_text(generated_code, model_code,
                      'newly generated', 'reference result',
                      msg='Error in generated text!')