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): def is_empty(x): if x is None: return True try: return len(x) == 0 except TypeError: return False ofn = list(prm.keys()) nfn = args[0::2] nfv = args[1::2] unused = [False]*len(nfv) # same length as nfv for n in range(len(nfn)): # Find indices in ofn where the field name matches nfn[n], case-insensitive ix = [i for i, s in enumerate(ofn) if s.lower() == nfn[n].lower()] if len(ix) > 1: # Case insensitive match hits more than one field name. # Defer to case sensitive matching as arbiter/disambiguator. ix = [i for i, s in enumerate(ofn) if s == nfn[n]] if ix: index = ix[0] # If the value in prm is a list, and the new value is not, make it a list if isinstance(prm[ofn[index]], list) and not isinstance(nfv[n], list): nfv[n] = [nfv[n]] oclass = type(prm[ofn[index]]) empty = is_empty(prm[ofn[index]]) or is_empty(nfv[n]) if empty or isinstance(nfv[n], oclass): prm[ofn[index]] = nfv[n] else: nclass = type(nfv[n]) raise TypeError(f"{caller}:Option:ValueWrongClass: Option '{nfn[n]}' has value of unsupported type.\nExpected '{oclass.__name__}', but got '{nclass.__name__}' in its place.") else: unused[n] = True # Collect unused options into extra extra = [] for n in range(len(unused)): if unused[n]: extra.append(nfn[n]) extra.append(nfv[n]) 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.