def select_detail(details): """Pick the preferred type of detail from a list of details. (If the argument isn't a list, treat it as a list of one.)""" types = {"text/html": 30, "application/xhtml+xml": 20, "text/plain": 10} if details is None: return None if type(details) is not list: details = [details] ds = [] for detail in details: ctype = detail.get("type", None) if ctype is None: continue if types.has_key(ctype): score = types[ctype] else: score = 0 if detail["value"] != "": ds.append((score, detail)) ds.sort() if len(ds) == 0: return None else: return ds[-1][1]
def new_f(*args, **kwds): argument_types = ", ".join( "%s=%s" %(v,t) for v,t in types.items() ) for i,v in enumerate(args): if types.has_key(f.func_code.co_varnames[i]) and \ not isinstance(v, types[f.func_code.co_varnames[i]]): raise exception("Function argument '%s' with a " "value of %r does not match the allowed " "types %s. \n The arguments " "of this function have allowed types of %s" % \ (f.func_code.co_varnames[i],v, types[f.func_code.co_varnames[i]],argument_types)) del types[f.func_code.co_varnames[i]] for k,v in kwds.iteritems(): if types.has_key(k) and not isinstance(v, types[k]): raise exception("Function argument '%s' with a " "value of %r does not match one of the allowed " "types %s. \n The arguments " "of this function have allowed types of %s" % \ (k,v,types[k],argument_types)) return f(*args, **kwds)
def prototype(cls, func, types={}): '''Generate a prototype for an instance of a function.''' args, defaults, (star, starstar) = cls.ex_args(func) argsiter = (("{:s}={:s}".format( n, "{:s}".format('|'.join( t.__name__ for t in types[n])) if not isinstance(types[n], type) and hasattr(types[n], '__iter__') else types[n].__name__) if types.has_key(n) else n) for n in args) res = (argsiter, ("*{:s}".format(star), ) if star else (), ("**{:s}".format(starstar), ) if starstar else ()) return "{:s}({:s})".format(func.func_name, ', '.join(itertools.chain(*res)))
def new_f(*args, **kwds): argument_types = ", ".join("%s=%s" % (v, t) for v, t in types.items()) for i, v in enumerate(args): if types.has_key(f.func_code.co_varnames[i]) and \ not isinstance(v, types[f.func_code.co_varnames[i]]): raise exception("Function argument '%s' with a " "value of %r does not match the allowed " "types %s. \n The arguments " "of this function have allowed types of %s" % \ (f.func_code.co_varnames[i],v, types[f.func_code.co_varnames[i]],argument_types)) del types[f.func_code.co_varnames[i]] for k, v in kwds.iteritems(): if types.has_key(k) and not isinstance(v, types[k]): raise exception("Function argument '%s' with a " "value of %r does not match one of the allowed " "types %s. \n The arguments " "of this function have allowed types of %s" % \ (k,v,types[k],argument_types)) return f(*args, **kwds)
def new_f(*args, **kwds): argument_types = ", ".join( "%s=%s" %(v,t) for v,t in types.items() ) for i,v in enumerate(args): # no need to check self argument if i == 0 : continue if types.has_key(f.func_code.co_varnames[i]) and \ not isinstance(v, types[f.func_code.co_varnames[i]]): raise exception("Method argument '%s' with a " "value of %r does not match the " "allowed types %s. \n The " "arguments of this method have " "allowed types of %s" % \ (f.func_code.co_varnames[i],v,types[f.func_code.co_varnames[i]],argument_types)) del types[f.func_code.co_varnames[i]] for k,v in kwds.iteritems(): if types.has_key(k) and not isinstance(v, types[k]): raise exception("Method argument '%s' with a " "value of %r does not match one " "of the allowed types %s. \n The " "arguments of this method have allowed types of %s" % \ (k,v,types[k],argument_types)) return f(*args, **kwds)
def new_f(*args, **kwds): argument_types = ", ".join("%s=%s" % (v, t) for v, t in types.items()) for i, v in enumerate(args): # no need to check self argument if i == 0: continue if types.has_key(f.func_code.co_varnames[i]) and \ not isinstance(v, types[f.func_code.co_varnames[i]]): raise exception("Method argument '%s' with a " "value of %r does not match the " "allowed types %s. \n The " "arguments of this method have " "allowed types of %s" % \ (f.func_code.co_varnames[i],v,types[f.func_code.co_varnames[i]],argument_types)) del types[f.func_code.co_varnames[i]] for k, v in kwds.iteritems(): if types.has_key(k) and not isinstance(v, types[k]): raise exception("Method argument '%s' with a " "value of %r does not match one " "of the allowed types %s. \n The " "arguments of this method have allowed types of %s" % \ (k,v,types[k],argument_types)) return f(*args, **kwds)
def show_settings(self, settings): tsp_list = settings['type-spectrum-phase-list'] if tsp_list: types = {} for type, spectrum, phase in tsp_list: if sparky.object_exists(spectrum): self.type_to_spectrum_menu[type].set(spectrum.name) self.type_to_phase[type].set(phase) types[type] = 1 for type in self.type_to_spectrum_menu.keys(): if not types.has_key(type): self.type_to_spectrum_menu[type].set('') ttable = settings['tolerance-table'] for e in self.tolerances: atom_name = e.label['text'] if ttable.has_key(atom_name): new_tol = ttable[atom_name] current_tol = pyutil.string_to_float(e.variable.get(), 0) if new_tol != current_tol: e.variable.set('%.3g' % new_tol) self.temp_directory.set(settings['temp-directory'])