Peter wrote: > On Wed, Sep 30, 2009 at 4:59 PM, Laurent Gautier <lgaut...@gmail.com> wrote: >> Peter wrote: >>> Can you do something like check if example_name is a valid argument >>> for the function at run time, and if not, check example.name instead? >>> Would this impose too high a performance cost? >>> >>> I agree this won't cover the special case of a function which has >>> two arguments which differ only in underscore vs dot, but that is >>> very rare corner case (surely)? >> I welcome all proposal, but I'd like to keep users on the safe side by >> default as much as possible (starting with "it should not happen... most of >> the time" does not seem like the right way to start). > > I see your point, but it should be balanced against the desire to make > rpy2 easy to use, especially for those of us used to the "." / "_" mapping > from rpy1. > > Ideally I'd like rpy2 to map "arg_name" (in Python code) to either > "arg_name" or "arg.name" (when talking to R) as appropriate for > the function concerned, but raise an error if both possibilities are > defined. That way I continue to use the simple substitution when > writing R calls, and only have to worry about these very rare corner > cases when due to ambiguity the naive way should raise an exception. > I don't know enough about R's introspection to say if this idea is > realistic or not. > > Peter
rpy2.robjects.packages.importr() (in rpy2-2.1-dev) is trying to address something similar with object names by performing a check at import time (and binding R object Python symbols, saving on the cost from translating "." to "_" each time). Having a somewhat similar approach with function signatures is on the (unwritten but ever growing) to-do list. The method robjects.Function.formals() can help making it possible and look like follows (this is a simple example; it would be nice(r) to have the function returned an instance of a subclass of rpy2.robjects.Function): def param_translate(rfunc): """ Take an R rpy2.robjects.Function and return the same function with its argument names with the character '.' replaced with '_' whenever present """ prm_translate = {} for r_param in myrfunc.formals().names: py_param = r_param.replace('.', '_') if py_param in prm_translate: raise ValueError("Error '%s' already in the transalation table" %r_param) prm_translate[py_param] = r_param def f(*args, **kwargs): for k in tuple(kwargs.keys()): v = kwargs.pop(k) kwargs[prm_translate[k]] = v return rfunc(*args, **kwargs) return f # example with the method "unlist" >>> unlist = param_translate(robjects.baseenv['unlist']) # Inspect the translation dict >>> unlist.__closure__[1].cell_contents {'recursive': 'recursive', 'use_names': 'use.names', 'x': 'x'} L. ------------------------------------------------------------------------------ Come build with us! The BlackBerry(R) Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9 - 12, 2009. Register now! http://p.sf.net/sfu/devconference _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list