def new_f(*args, **kwargs): try: vartype = args[arg_idx] except IndexError: vartype = kwargs['vartype'] if isinstance(vartype, Vartype): # we don't need to do anything return f(*args, **kwargs) 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}.")) new_args = list(args) new_args[arg_idx] = vartype return f(*new_args, **kwargs)
def __init__(self, info=None, vartype=Vartype.UNDEFINED): # each sample is stored as a dict in _sample_data self.datalist = [] self._sorted_datalist = [] if info is None: self.info = {} elif not isinstance(info, dict): raise TypeError("expected 'info' input to be a dict") else: self.info = info # set the vartype if vartype in Vartype: self.vartype = vartype elif isinstance(vartype, str): self.vartype = Vartype[vartype] else: self.vartype = Vartype(vartype)
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