def _vartype_arg(f): argspec = getargspec(f) def _enforce_single_arg(name, args, kwargs): try: vartype = kwargs[name] except KeyError: raise TypeError('vartype argument missing') kwargs[name] = as_vartype(vartype) @wraps(f) def new_f(*args, **kwargs): # bound actual f arguments (including defaults) to f argument names # (note: if call arguments don't match actual function signature, # we'll fail here with the standard `TypeError`) bound_args = inspect.getcallargs(f, *args, **kwargs) # `getcallargs` doesn't merge additional positional/keyword arguments, # so do it manually final_args = list(bound_args.pop(argspec.varargs, ())) final_kwargs = bound_args.pop(argspec.keywords, {}) final_kwargs.update(bound_args) for name in arg_names: _enforce_single_arg(name, final_args, final_kwargs) return f(*final_args, **final_kwargs) return new_f
def _graph_arg(f): argspec = getargspec(f) def _enforce_single_arg(name, args, kwargs): try: G = kwargs[name] except KeyError: raise TypeError('Graph argument missing') if hasattr(G, 'edges') and hasattr(G, 'nodes'): # networkx or perhaps a named tuple kwargs[name] = (list(G.nodes), list(G.edges)) elif isinstance(G, integer_types): # an integer, cast to a complete graph kwargs[name] = (list(range(G)), list(itertools.combinations(range(G), 2))) elif isinstance(G, Sequence) and len(G) == 2: # is a pair nodes/edges if isinstance(G[0], integer_types): # if nodes is an int kwargs[name] = (list(range(G[0])), G[1]) elif isinstance(G, float) and G.is_integer(): G = int(G) kwargs[name] = (list(range(G)), list(itertools.combinations(range(G), 2))) else: raise ValueError('Unexpected graph input form') return @wraps(f) def new_f(*args, **kwargs): # bound actual f arguments (including defaults) to f argument names # (note: if call arguments don't match actual function signature, # we'll fail here with the standard `TypeError`) bound_args = inspect.getcallargs(f, *args, **kwargs) # `getcallargs` doesn't merge additional positional/keyword arguments, # so do it manually final_args = list(bound_args.pop(argspec.varargs, ())) final_kwargs = bound_args.pop(argspec.keywords, {}) final_kwargs.update(bound_args) for name in arg_names: _enforce_single_arg(name, final_args, final_kwargs) return f(*final_args, **final_kwargs) return new_f
def _vartype_arg(f): argspec = getargspec(f) def _enforce_single_arg(name, args, kwargs): try: vartype = kwargs[name] except KeyError: raise TypeError('vartype argument missing') if isinstance(vartype, Vartype): return try: if isinstance(vartype, str): vartype = Vartype[vartype] else: vartype = Vartype(vartype) except (ValueError, KeyError): raise TypeError(("expected input vartype to be one of: " "Vartype.SPIN, 'SPIN', {-1, 1}, " "Vartype.BINARY, 'BINARY', or {0, 1}.")) kwargs[name] = vartype @wraps(f) def new_f(*args, **kwargs): # bound actual f arguments (including defaults) to f argument names # (note: if call arguments don't match actual function signature, # we'll fail here with the standard `TypeError`) bound_args = inspect.getcallargs(f, *args, **kwargs) # `getcallargs` doesn't merge additional positional/keyword arguments, # so do it manually final_args = list(bound_args.pop(argspec.varargs, ())) final_kwargs = bound_args.pop(argspec.keywords, {}) final_kwargs.update(bound_args) for name in arg_names: _enforce_single_arg(name, final_args, final_kwargs) return f(*final_args, **final_kwargs) return new_f
def _graph_arg(f): argspec = getargspec(f) def _enforce_single_arg(name, args, kwargs): try: G = kwargs[name] except KeyError: raise TypeError('Graph argument missing') if hasattr(G, 'edges') and hasattr(G, 'nodes'): # networkx or perhaps a named tuple kwargs[name] = (list(G.nodes), list(G.edges)) elif _is_integer(G): # an integer, cast to a complete graph kwargs[name] = (list(range(G)), list(itertools.combinations(range(G), 2))) elif isinstance(G, abc.Sequence): if len(G) != 2: # edgelist kwargs[name] = (list(set().union(*G)), G) else: # len(G) == 2 # need to determine if this is a nodes/edges pair or an # edgelist if isinstance(G[0], int): # nodes are an int so definitely nodelist kwargs[name] = (list(range(G[0])), G[1]) elif all( isinstance(e, abc.Sequence) and len(e) == 2 for e in G): # ok, everything is a sequence and everything has length # 2, so probably an edgelist. But we're dealing with # only four objects so might as well check to be sure nodes, edges = G if all( isinstance(e, abc.Sequence) and len(e) == 2 and (v in nodes for v in e) for e in edges): pass # nodes, edges else: # edgelist kwargs[name] = (list(set().union(*G)), G) else: # nodes, edges pass elif allow_None and G is None: # allow None to be passed through kwargs[name] = G else: raise ValueError('Unexpected graph input form') return @wraps(f) def new_f(*args, **kwargs): # bound actual f arguments (including defaults) to f argument names # (note: if call arguments don't match actual function signature, # we'll fail here with the standard `TypeError`) bound_args = inspect.getcallargs(f, *args, **kwargs) # `getcallargs` doesn't merge additional positional/keyword arguments, # so do it manually final_args = list(bound_args.pop(argspec.varargs, ())) final_kwargs = bound_args.pop(argspec.keywords, {}) final_kwargs.update(bound_args) for name in arg_names: _enforce_single_arg(name, final_args, final_kwargs) return f(*final_args, **final_kwargs) return new_f