o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
function [prm, extra] = process_options(prm, caller, varargin) ofn = fieldnames(prm);…
- User
function [prm, extra] = process_options(prm, caller, varargin) ofn = fieldnames(prm); nfn = varargin(1 : 2 : end); nfv = varargin(2 : 2 : end);
unused = false(size(nfv));
for n = 1 : numel(nfn) ix = find(strcmpi(nfn{n}, ofn));
if numel(ix) > 1 % Case insensitive match hits more than one field name. Defer to % case sensitive matching as arbiter/disambiguator. If we then % don't match *any* option, proceed to next field name. Note: Due % to guarantees implied by ofn = FIELDNAMES(prm) we either match % zero or one field name here. ix = find(strcmp(nfn{n}, ofn)); end if ~isempty(ix) if iscell(prm.(ofn{ix})) && ~iscell(nfv{n}) % Original is CELL -> accept anything by turning "new" % into CELL too. nfv{n} = nfv(n); end oclass = class(prm.(ofn{ix})); empty = isempty(prm.(ofn{ix})) || isempty(nfv{n}); if empty || isa(nfv{n}, oclass) prm.(ofn{ix}) = nfv{n}; else nclass = class(nfv{n}); error([caller, ':Option:ValueWrongClass'], ... ['Option ''', nfn{n}, ''' has value of ', ... 'unsupported type.\nExpected ''', oclass, ... ''', but got ''', nclass, ''' in its place.']); end else unused(n) = true; endend
extra = reshape([ nfn(unused) ; nfv(unused) ], 1, []); end 请将以上matlab代码,用python重写
- Assistant
def process_options(prm, caller, *args): if len(args) % 2 != 0: raise ValueError(f"{caller}:Option:ArgumentsMustBePairs", "Arguments must occur in name-value pairs.") nfn = args[0::2] nfv = args[1::2] ofn = list(prm.keys()) unused = [False] * len(nfv) for n in range(len(nfn)): name = nfn[n] value = nfv[n] # Case-insensitive matching matches = [i for i, field in enumerate(ofn) if field.lower() == name.lower()] if len(matches) > 1: # Case-sensitive matching matches = [i for i, field in enumerate(ofn) if field == name] if matches: index = matches[0] param_name = ofn[index] if isinstance(prm[param_name], list) and not isinstance(value, list): value = [value] def is_empty(val): if val is None: return True elif isinstance(val, (list, dict, str, tuple)): return len(val) == 0 else: return False empty = is_empty(prm[param_name]) or is_empty(value) if empty or isinstance(value, type(prm[param_name])): prm[param_name] = value else: oclass = type(prm[param_name]).__name__ nclass = type(value).__name__ raise TypeError(f"{caller}:Option:ValueWrongClass", f"Option '{name}' has value of unsupported type.\n" f"Expected '{oclass}', but got '{nclass}' in its place.") else: unused[n] = True # Prepare extra as interleaved list of unused names and values extra = [] for i in range(len(unused)): if unused[i]: extra.extend([nfn[i], nfv[i]]) return prm, extra
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.