def _init_axis(model): """ensure axis is a keyword for the model Input: model: a function of the form y = model(x) Returns: a function of the form y = model(x, axis=None) """ from klepto import signature signature = signature(model)[0] if type(model) is OUQModel or 'axis' in signature: #XXX: check len? kwds? return model # add axis (as second argument) #XXX: better utilize signature? def dummy(x, axis=None, **kwds): #XXX: *args? "model of form, y = model(x, axis=None)" result = model(x, **kwds) if axis is None or not hasattr(result, '__len__'): return result return result[axis] # copy attributes dummy.__orig__ = model #XXX: better name, __wrap__ ? #dummy.__doc__ = model.__doc__ or "model of form, y = model(x, axis=None)" d = model.__dict__.items() dummy.__dict__.update((i, j) for (i, j) in d if i not in dummy.__dict__) return dummy
def test_special(): p = partial(add, 0, x=0) p2 = partial(add, z=0) p3 = partial(add, 0) if IS_PYPY: # builtins in PYPY are python functions if hex(sys.hexversion) < '0x3080cf0': base, exp, mod = 'base', 'exponent', 'modulus' else: base, exp, mod = 'base', 'exp', 'mod' assert signature(pow, safe=True) == ((base, exp, mod), { mod: None }, '', '') else: assert signature(pow, safe=True) == (None, None, None, None) assert signature(p, safe=True) == (None, None, None, None) assert signature(p2, safe=True) == (('x', 'y'), {'z': 0}, '', '') assert signature(p3, safe=True) == (('y', ), {'!x': 0}, '', '') if IS_PYPY: # PYPY bug in ArgSpec for min, so use pow assert isvalid(pow, 0, 1) == True assert isvalid(pow, 0) == False assert isvalid(pow) == False else: # python >= 3.5 bug in ArgSpec for pow, so use min assert isvalid(min, 0, 1) == True assert isvalid(min, 0) == False assert isvalid(min) == False assert isvalid(p, 0, 1) == False assert isvalid(p, 0) == False assert isvalid(p) == False assert isvalid(p2, 0, 1) == False assert isvalid(p2, 0) == False assert isvalid(p2) == False assert isvalid(p3, 0, 1) == False assert isvalid(p3, 0) == True assert isvalid(p3) == False assert _keygen(p3, [], 0) == ((), {'y': 0}) assert _keygen(p2, [], 0) == ((), {'x': 0, 'z': 0}) assert _keygen(p, [], 0) == ((0, ), {}) assert _keygen(min, [], x=0, y=1) == ((), {'y': 1, 'x': 0}) assert _keygen(min, [], 0, 1) == ((0, 1), {}) assert _keygen(min, [], 0) == ((0, ), {}) assert _keygen(min, 'x', 0) == ((0, ), {}) assert _keygen(min, ['x', 'y'], 0) == ((0, ), {}) assert _keygen(min, [0, 1], 0) == ((NULL, ), {}) if IS_PYPY else ((0, ), {}) assert _keygen(min, ['*'], 0) == ((), {}) if IS_PYPY else ((0, ), {})
def test_signature(): s = signature(bar) assert s == (("x", "y", "z", "a", "b"), {"a": 1, "b": 2}, "args", "") # a partial with a 'fixed' x, thus x is 'unsettable' as a keyword p = partial(bar, 0) s = signature(p) assert s == (("y", "z", "a", "b"), {"a": 1, "!x": 0, "b": 2}, "args", "") """ >>> p(0,1) 4 >>> p(0,1,2,3,4,5) 6 """ # a partial where y is 'unsettable' as a positional argument p = partial(bar, y=10) s = signature(p) assert s == (("x", "!y", "z", "a", "b"), {"a": 1, "y": 10, "b": 2}, "args", "") """
def test_signature(): s = signature(bar) assert s == (('x', 'y', 'z', 'a', 'b'), {'a': 1, 'b': 2}, 'args', '') # a partial with a 'fixed' x, thus x is 'unsettable' as a keyword p = partial(bar, 0) s = signature(p) assert s == (('y', 'z', 'a', 'b'), {'a': 1, '!x': 0, 'b': 2}, 'args', '') ''' >>> p(0,1) 4 >>> p(0,1,2,3,4,5) 6 ''' # a partial where y is 'unsettable' as a positional argument p = partial(bar, y=10) s = signature(p) assert s == (('x', '!y', 'z', 'a', 'b'), {'a': 1, 'y': 10, 'b': 2}, 'args', '') '''
def test_special(): p = partial(add, 0,x=0) p2 = partial(add, z=0) p3 = partial(add, 0) if IS_PYPY: # builtins in PYPY are python functions assert signature(pow, safe=True) == (('base', 'exponent', 'modulus'), {'modulus': None}, '', '') else: assert signature(pow, safe=True) == (None, None, None, None) assert signature(p, safe=True) == (None, None, None, None) assert signature(p2, safe=True) == (('x', 'y'), {'z': 0}, '', '') assert signature(p3, safe=True) == (('y',), {'!x': 0}, '', '') if IS_PYPY: # PYPY bug in ArgSpec for min, so use pow assert isvalid(pow, 0,1) == True assert isvalid(pow, 0) == False assert isvalid(pow) == False else: # python >= 3.5 bug in ArgSpec for pow, so use min assert isvalid(min, 0,1) == True assert isvalid(min, 0) == False assert isvalid(min) == False assert isvalid(p, 0,1) == False assert isvalid(p, 0) == False assert isvalid(p) == False assert isvalid(p2, 0,1) == False assert isvalid(p2, 0) == False assert isvalid(p2) == False assert isvalid(p3, 0,1) == False assert isvalid(p3, 0) == True assert isvalid(p3) == False assert _keygen(p3, [], 0) == ((), {'y': 0}) assert _keygen(p2, [], 0) == ((), {'x': 0, 'z': 0}) assert _keygen(p, [], 0) == ((0,), {}) assert _keygen(min, [], x=0,y=1) == ((), {'y': 1, 'x': 0}) assert _keygen(min, [], 0,1) == ((0,1), {}) assert _keygen(min, [], 0) == ((0,), {}) assert _keygen(min, 'x', 0) == ((0,), {}) assert _keygen(min, ['x','y'], 0) == ((0,), {}) assert _keygen(min, [0,1], 0) == ((NULL,), {}) if IS_PYPY else ((0,), {}) assert _keygen(min, ['*'], 0) == ((), {}) if IS_PYPY else ((0,), {})
def test_special(): p = partial(add, 0, x=0) p2 = partial(add, z=0) p3 = partial(add, 0) if IS_PYPY: # builtins in PYPY are python functions assert signature(pow, safe=True) == (("base", "exponent", "modulus"), {"modulus": None}, "", "") else: assert signature(pow, safe=True) == (None, None, None, None) assert signature(p, safe=True) == (None, None, None, None) assert signature(p2, safe=True) == (("x", "y"), {"z": 0}, "", "") assert signature(p3, safe=True) == (("y",), {"!x": 0}, "", "") if IS_PYPY: # PYPY bug in ArgSpec for min, so use pow assert isvalid(pow, 0, 1) == True assert isvalid(pow, 0) == False assert isvalid(pow) == False else: # python >= 3.5 bug in ArgSpec for pow, so use min assert isvalid(min, 0, 1) == True assert isvalid(min, 0) == False assert isvalid(min) == False assert isvalid(p, 0, 1) == False assert isvalid(p, 0) == False assert isvalid(p) == False assert isvalid(p2, 0, 1) == False assert isvalid(p2, 0) == False assert isvalid(p2) == False assert isvalid(p3, 0, 1) == False assert isvalid(p3, 0) == True assert isvalid(p3) == False assert _keygen(p3, [], 0) == ((), {"y": 0}) assert _keygen(p2, [], 0) == ((), {"x": 0, "z": 0}) assert _keygen(p, [], 0) == ((0,), {}) assert _keygen(min, [], x=0, y=1) == ((), {"y": 1, "x": 0}) assert _keygen(min, [], 0, 1) == ((0, 1), {}) assert _keygen(min, [], 0) == ((0,), {}) assert _keygen(min, "x", 0) == ((0,), {}) assert _keygen(min, ["x", "y"], 0) == ((0,), {}) assert _keygen(min, [0, 1], 0) == ((NULL,), {}) if IS_PYPY else ((0,), {}) assert _keygen(min, ["*"], 0) == ((), {}) if IS_PYPY else ((0,), {})
def test_keygen(): # a partial with a 'fixed' x, and positionally 'unsettable' b p = partial(bar, 0, b=10) s = signature(p) assert s == (('y', 'z', 'a', '!b'), {'a': 1, '!x': 0, 'b': 10}, 'args', '') ignored = (0, 1, 3, 5, '*', 'b', 'c') user_args = ('0', '1', '2', '3', '4', '5', '6') user_kwds = {'a': '10', 'b': '20', 'c': '30', 'd': '40'} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == { 'a': '2', 'c': NULL, 'b': NULL, 'd': '40', 'y': NULL, 'z': NULL } ignored = (0, 1, 3, 5, '**', 'b', 'c') user_args = ('0', '1', '2', '3', '4', '5', '6') user_kwds = {'a': '10', 'b': '20', 'c': '30', 'd': '40'} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ('4', NULL, '6') assert key_kwds == {'a': '2', 'b': NULL, 'y': NULL, 'z': NULL} ignored = ('*', '**') user_args = ('0', '1', '2', '3', '4', '5', '6') user_kwds = {'a': '10', 'b': '20', 'c': '30', 'd': '40'} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {'a': '2', 'b': '3', 'y': '0', 'z': '1'} ignored = (0, 2) user_args = ('0', '1', '2', '3', '4', '5', '6') user_kwds = {'a': '10', 'b': '20', 'c': '30', 'd': '40'} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ('4', '5', '6') assert key_kwds == { 'a': NULL, 'c': '30', 'b': '3', 'd': '40', 'y': NULL, 'z': '1' } ignored = (0, ) user_args = ('0', '1', '2', '3', '4', '5', '6') user_kwds = {'a': '10', 'b': '20', 'c': '30', 'd': '40', 'y': 50} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ('4', '5', '6') assert key_kwds == { 'a': '2', 'c': '30', 'b': '3', 'd': '40', 'y': NULL, 'z': '1' } ignored = ('a', 'y', 'c') user_args = ('0', '1', '2', '3', '4', '5', '6') user_kwds = {'a': '10', 'b': '20', 'c': '30', 'd': '40', 'y': 50} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ('4', '5', '6') assert key_kwds == { 'a': NULL, 'c': NULL, 'b': '3', 'd': '40', 'y': NULL, 'z': '1' } ignored = (1, 5, 'a', 'y', 'c') user_args = ('0', '1') user_kwds = {} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {'a': NULL, 'y': NULL, 'b': 10, 'z': NULL} #XXX: c? ignored = (1, 5, 'a', 'y', 'c') user_args = () user_kwds = {'c': '30', 'd': '40', 'y': 50} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == { 'a': NULL, 'y': NULL, 'c': NULL, 'd': '40', 'b': 10, 'z': NULL } ignored = (1, 5, 'a', 'c') user_args = ('0', '1') user_kwds = {} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {'a': NULL, 'y': '0', 'b': 10, 'z': NULL} #XXX: c? ignored = () user_args = ('0', ) user_kwds = {'c': '30'} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {'a': 1, 'y': '0', 'b': 10, 'c': '30'}
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation) # Copyright (c) 2013-2014 California Institute of Technology. # License: 3-clause BSD. The full license text is available at: # - http://trac.mystic.cacr.caltech.edu/project/pathos/browser/klepto/LICENSE import sys from functools import partial from klepto.keymaps import hashmap from klepto import NULL from klepto import signature, keygen from klepto import _keygen, isvalid def bar(x,y,z,a=1,b=2,*args): return x+y+z+a+b s = signature(bar) assert s == (('x', 'y', 'z', 'a', 'b'), {'a': 1, 'b': 2}, 'args', '') # a partial with a 'fixed' x, thus x is 'unsettable' as a keyword p = partial(bar, 0) s = signature(p) assert s == (('y', 'z', 'a', 'b'), {'a': 1, '!x': 0, 'b': 2}, 'args', '') ''' >>> p(0,1) 4 >>> p(0,1,2,3,4,5) 6 ''' # a partial where y is 'unsettable' as a positional argument p = partial(bar, y=10) s = signature(p)
def test_keygen(): # a partial with a 'fixed' x, and positionally 'unsettable' b p = partial(bar, 0, b=10) s = signature(p) assert s == (("y", "z", "a", "!b"), {"a": 1, "!x": 0, "b": 10}, "args", "") ignored = (0, 1, 3, 5, "*", "b", "c") user_args = ("0", "1", "2", "3", "4", "5", "6") user_kwds = {"a": "10", "b": "20", "c": "30", "d": "40"} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {"a": "2", "c": NULL, "b": NULL, "d": "40", "y": NULL, "z": NULL} ignored = (0, 1, 3, 5, "**", "b", "c") user_args = ("0", "1", "2", "3", "4", "5", "6") user_kwds = {"a": "10", "b": "20", "c": "30", "d": "40"} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ("4", NULL, "6") assert key_kwds == {"a": "2", "b": NULL, "y": NULL, "z": NULL} ignored = ("*", "**") user_args = ("0", "1", "2", "3", "4", "5", "6") user_kwds = {"a": "10", "b": "20", "c": "30", "d": "40"} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {"a": "2", "b": "3", "y": "0", "z": "1"} ignored = (0, 2) user_args = ("0", "1", "2", "3", "4", "5", "6") user_kwds = {"a": "10", "b": "20", "c": "30", "d": "40"} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ("4", "5", "6") assert key_kwds == {"a": NULL, "c": "30", "b": "3", "d": "40", "y": NULL, "z": "1"} ignored = (0,) user_args = ("0", "1", "2", "3", "4", "5", "6") user_kwds = {"a": "10", "b": "20", "c": "30", "d": "40", "y": 50} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ("4", "5", "6") assert key_kwds == {"a": "2", "c": "30", "b": "3", "d": "40", "y": NULL, "z": "1"} ignored = ("a", "y", "c") user_args = ("0", "1", "2", "3", "4", "5", "6") user_kwds = {"a": "10", "b": "20", "c": "30", "d": "40", "y": 50} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ("4", "5", "6") assert key_kwds == {"a": NULL, "c": NULL, "b": "3", "d": "40", "y": NULL, "z": "1"} ignored = (1, 5, "a", "y", "c") user_args = ("0", "1") user_kwds = {} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {"a": NULL, "y": NULL, "b": 10, "z": NULL} # XXX: c? ignored = (1, 5, "a", "y", "c") user_args = () user_kwds = {"c": "30", "d": "40", "y": 50} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {"a": NULL, "y": NULL, "c": NULL, "d": "40", "b": 10, "z": NULL} ignored = (1, 5, "a", "c") user_args = ("0", "1") user_kwds = {} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {"a": NULL, "y": "0", "b": 10, "z": NULL} # XXX: c? ignored = () user_args = ("0",) user_kwds = {"c": "30"} key_args, key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {"a": 1, "y": "0", "b": 10, "c": "30"}
def test_keygen(): # a partial with a 'fixed' x, and positionally 'unsettable' b p = partial(bar, 0,b=10) s = signature(p) assert s == (('y', 'z', 'a', '!b'), {'a': 1, '!x': 0, 'b': 10}, 'args', '') ignored = (0,1,3,5,'*','b','c') user_args = ('0','1','2','3','4','5','6') user_kwds = {'a':'10','b':'20','c':'30','d':'40'} key_args,key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {'a': '2', 'c': NULL, 'b': NULL, 'd': '40', 'y': NULL, 'z': NULL} ignored = (0,1,3,5,'**','b','c') user_args = ('0','1','2','3','4','5','6') user_kwds = {'a':'10','b':'20','c':'30','d':'40'} key_args,key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ('4', NULL, '6') assert key_kwds == {'a': '2', 'b': NULL, 'y': NULL, 'z': NULL} ignored = ('*','**') user_args = ('0','1','2','3','4','5','6') user_kwds = {'a':'10','b':'20','c':'30','d':'40'} key_args,key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {'a': '2', 'b': '3', 'y': '0', 'z': '1'} ignored = (0,2) user_args = ('0','1','2','3','4','5','6') user_kwds = {'a':'10','b':'20','c':'30','d':'40'} key_args,key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ('4', '5', '6') assert key_kwds == {'a': NULL, 'c': '30', 'b': '3', 'd':'40', 'y': NULL, 'z': '1'} ignored = (0,) user_args = ('0','1','2','3','4','5','6') user_kwds = {'a':'10','b':'20','c':'30','d':'40','y':50} key_args,key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ('4', '5', '6') assert key_kwds == {'a': '2', 'c': '30', 'b': '3', 'd':'40', 'y': NULL, 'z': '1'} ignored = ('a','y','c') user_args = ('0','1','2','3','4','5','6') user_kwds = {'a':'10','b':'20','c':'30','d':'40','y':50} key_args,key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == ('4', '5', '6') assert key_kwds == {'a': NULL, 'c': NULL, 'b': '3', 'd':'40', 'y': NULL, 'z': '1'} ignored = (1,5,'a','y','c') user_args = ('0','1') user_kwds = {} key_args,key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {'a': NULL, 'y': NULL, 'b': 10, 'z': NULL} #XXX: c? ignored = (1,5,'a','y','c') user_args = () user_kwds = {'c':'30','d':'40','y':50} key_args,key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {'a': NULL, 'y': NULL, 'c': NULL, 'd': '40', 'b': 10, 'z': NULL} ignored = (1,5,'a','c') user_args = ('0','1') user_kwds = {} key_args,key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {'a': NULL, 'y': '0', 'b': 10, 'z': NULL} #XXX: c? ignored = () user_args = ('0',) user_kwds = {'c':'30'} key_args,key_kwds = _keygen(p, ignored, *user_args, **user_kwds) assert key_args == () assert key_kwds == {'a': 1, 'y': '0', 'b': 10, 'c': '30'}
# License: 3-clause BSD. The full license text is available at: # - http://trac.mystic.cacr.caltech.edu/project/pathos/browser/klepto/LICENSE import sys from functools import partial from klepto.keymaps import hashmap from klepto import NULL from klepto import signature, keygen from klepto import _keygen, isvalid def bar(x, y, z, a=1, b=2, *args): return x + y + z + a + b s = signature(bar) assert s == (('x', 'y', 'z', 'a', 'b'), {'a': 1, 'b': 2}, 'args', '') # a partial with a 'fixed' x, thus x is 'unsettable' as a keyword p = partial(bar, 0) s = signature(p) assert s == (('y', 'z', 'a', 'b'), {'a': 1, '!x': 0, 'b': 2}, 'args', '') ''' >>> p(0,1) 4 >>> p(0,1,2,3,4,5) 6 ''' # a partial where y is 'unsettable' as a positional argument p = partial(bar, y=10) s = signature(p)