How to use the klepto.validate function in klepto

To help you get started, we’ve selected a few klepto examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github uqfoundation / klepto / tests / test_validate.py View on Github external
try:
        re_1 = validate(p,1)
        re_2 = Exception()
    except:
        re_1,re_2 = sys.exc_info()[:2]
    #print(res2)
    #print(re_2)
    assert res1 == re_1
    if hex(sys.hexversion).startswith('0x207'):
        assert str(res2)[:20] == str(TypeError("foo() takes at least 3 arguments (3 given)"))[:20] # PYPY TypeError is slightly different
    elif hex(sys.hexversion) < '0x30300f0': # all versions slightly different
        assert str(res2)[:21] == str(re_2)[:21]
    else:
        pass # python 3.3 gives "foo() missing 1 required positional argument: 'z'"

    assert validate(p,1,2) == None #XXX: better return ((1,2),{}) ?
    '''
    >>> p(1,2)
github uqfoundation / klepto / tests / test_validate.py View on Github external
def test_partial_foo_xy():
    p = partial(foo, 0,1)
    try:
        res1 = p(1,2,3,4,b=5)
        res2 = Exception()
    except:
        res1,res2 = sys.exc_info()[:2]
    try:
        re_1 = validate(p,1,2,3,4,b=5)
        re_2 = Exception()
    except:
        re_1,re_2 = sys.exc_info()[:2]
    #print(res2)
    #print(re_2)
    assert res1 == re_1
    if hex(sys.hexversion).startswith('0x207'):
        assert str(res2)[:20] == str(TypeError("foo() takes at most 5 arguments (7 given)"))[:20] # PYPY TypeError is slightly different
    elif hex(sys.hexversion) < '0x30300f0': # all versions slightly different
        assert str(res2)[:20] == str(re_2)[:20]
    else:
        pass # python 3.3 gives "foo() got multiple values for argument 'b'"

    try:
        res1 = p()
        res2 = Exception()
github uqfoundation / klepto / tests / test_validate.py View on Github external
def test_partial_foo_xa():
    p = partial(foo, 0,a=2)
    try:
        res1 = p(1)
        res2 = Exception()
    except:
        res1,res2 = sys.exc_info()[:2]
    try:
        re_1 = validate(p,1)
        re_2 = Exception()
    except:
        re_1,re_2 = sys.exc_info()[:2]
    #print(res2)
    #print(re_2)
    assert res1 == re_1
    if hex(sys.hexversion).startswith('0x207'):
        assert str(res2)[:20] == str(TypeError("foo() takes at least 3 arguments (3 given)"))[:20] # PYPY TypeError is slightly different
    elif hex(sys.hexversion) < '0x30300f0': # all versions slightly different
        assert str(res2)[:21] == str(re_2)[:21]
    else:
        pass # python 3.3 gives "foo() missing 1 required positional argument: 'z'"

    assert validate(p,1,2) == None #XXX: better return ((1,2),{}) ?
    '''
    >>> p(1,2)
github uqfoundation / klepto / tests / test_validate.py View on Github external
def test_partial_foo_xx():
    p = partial(foo, 0,x=4)
    try:
        res1 = p(1,2,3,4,r=5)
        res2 = Exception()
    except:
        res1,res2 = sys.exc_info()[:2]
    try:
        re_1 = validate(p,1,2,3,4,r=5)
        re_2 = Exception()
    except:
        re_1,re_2 = sys.exc_info()[:2]
    #print(res2)
    #print(re_2)
    assert res1 == re_1
    if hex(sys.hexversion).startswith('0x207'):
        assert str(res2) == str(TypeError("foo() got multiple values for keyword argument 'x'"))
    elif hex(sys.hexversion) < '0x30300f0': # all versions slightly different
        assert str(res2)[:25] == str(re_2)[:25]
    else:
        pass # python 3.3 gives "foo() got unexpected keyword argument 'r'"
github uqfoundation / klepto / tests / test_validate.py View on Github external
#print(re_2)
    assert res1 == re_1
    if hex(sys.hexversion).startswith('0x207'):
        assert str(res2)[:20] == str(TypeError("foo() takes at most 5 arguments (7 given)"))[:20] # PYPY TypeError is slightly different
    elif hex(sys.hexversion) < '0x30300f0': # all versions slightly different
        assert str(res2)[:20] == str(re_2)[:20]
    else:
        pass # python 3.3 gives "foo() got multiple values for argument 'b'"

    try:
        res1 = p()
        res2 = Exception()
    except:
        res1,res2 = sys.exc_info()[:2]
    try:
        re_1 = validate(p)
        re_2 = Exception()
    except:
        re_1,re_2 = sys.exc_info()[:2]
    #print(res2)
    #print(re_2)
    assert res1 == re_1
    if hex(sys.hexversion).startswith('0x207'):
        assert str(res2) == str(TypeError("foo() takes at least 3 arguments (2 given)"))
    elif hex(sys.hexversion) < '0x30300f0': # all versions slightly different
        assert str(res2)[:21] == str(re_2)[:21]
    else:
        pass # python 3.3 gives "foo() missing 1 required positional argument 'z'"

    try:
        res1 = p(1,2,3,4,r=5)
        res2 = Exception()
github uqfoundation / klepto / tests / test_validate.py View on Github external
def test_partial_foo_xzb():
    p = partial(foo, 0,z=1,b=2)
    try:
        res1 = p()
        res2 = Exception()
    except:
        res1,res2 = sys.exc_info()[:2]
    try:
        re_1 = validate(p)
        re_2 = Exception()
    except:
        re_1,re_2 = sys.exc_info()[:2]
    #print(res2)
    #print(re_2)
    assert res1 == re_1
    if hex(sys.hexversion).startswith('0x207'):
        assert str(res2)[:20] == str(TypeError("foo() takes at least 3 arguments (3 given)"))[:20] # PYPY TypeError is slightly different
    elif hex(sys.hexversion) < '0x30300f0': # all versions slightly different
        assert str(res2)[:21] == str(re_2)[:21]
    else:
        pass # python 3.3 gives "foo() missing 1 required positional argument: 'y'"
github uqfoundation / mystic / mystic / abstract_solver.py View on Github external
"""decorate the cost function with bounds, penalties, monitors, etc"""
        _cost,_raw,_args = self._cost
        # check if need to 'wrap' or can return the stored cost
        if (cost is None or cost is _raw or cost is _cost) and \
           (ExtraArgs is None or ExtraArgs is _args):
            return
        # get cost and args if None was given
        if cost is None: cost = _raw
        args = _args if ExtraArgs is None else ExtraArgs
        args = () if args is None else args
        # quick validation check (so doesn't screw up internals)
        if not isvalid(cost, [0]*self.nDim, *args):
            try: name = cost.__name__
            except AttributeError: # raise new error for non-callables
                cost(*args)
            validate(cost, None, *args)
           #val = len(args) + 1  #XXX: 'klepto.validate' for better error?
           #msg = '%s() invalid number of arguments (%d given)' % (name, val)
           #raise TypeError(msg)
        # hold on to the 'raw' cost function
        self._cost = (None, cost, ExtraArgs)
        self._live = False
        return