functions.py :  » Language-Interface » RPy » rpy2-2.1.3 » rpy » robjects » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » RPy 
RPy » rpy2 2.1.3 » rpy » robjects » functions.py
from rpy2.robjects.robject import RObjectMixin,RObject
import rpy2.rinterface as rinterface
#import rpy2.robjects.conversion as conversion
import conversion

baseenv_ri = rinterface.baseenv

#needed to avoid circular imports
_parse = rinterface.baseenv['parse']
_reval = rinterface.baseenv['eval']
NULL = _reval(_parse(text = rinterface.StrSexpVector(("NULL", ))))


class Function(RObjectMixin, rinterface.SexpClosure):
    """ Python representation of an R function.
    """

    __formals = baseenv_ri.get('formals')
    __local = baseenv_ri.get('local')
    __call = baseenv_ri.get('call')
    __assymbol = baseenv_ri.get('as.symbol')
    __newenv = baseenv_ri.get('new.env')

    _local_env = None

    def __init__(self, *args, **kwargs):
        super(Function, self).__init__(*args, **kwargs)
        self._local_env = self.__newenv(hash=rinterface.BoolSexpVector((True, )))

    def __call__(self, *args, **kwargs):
        new_args = [conversion.py2ri(a) for a in args]
        new_kwargs = {}
        for k, v in kwargs.iteritems():
            new_kwargs[k] = conversion.py2ri(v)
        res = super(Function, self).__call__(*new_args, **new_kwargs)
        res = conversion.ri2py(res)
        return res

    def formals(self):
        """ Return the signature of the underlying R function 
        (as the R function 'formals()' would).
        """
        res = self.__formals(self)
        res = conversion.ri2py(res)
        return res

    def rcall(self, *args):
        """ Wrapper around the parent method rpy2.rinterface.SexpClosure.rcall(). """
        res = super(Function, self).rcall(*args)
        res = conversion.ri2py(res)
        return res

class SignatureTranslatedFunction(Function):
    """ Python representation of an R function such as 
    the character '.' is replaced with '_' whenever present in the R argument name. """
    _prm_translate = None

    def __init__(self, sexp, init_prm_translate = None):
        super(SignatureTranslatedFunction, self).__init__(sexp)
        if init_prm_translate is None:
            prm_translate = {}
        else:
            assert isinstance(init_prm_translate, dict)
            prm_translate = init_prm_translate
        if not self.formals().rsame(NULL):
            for r_param in self.formals().names:
                py_param = r_param.replace('.', '_')
                if py_param in prm_translate:
                    raise ValueError("Error: '%s' already in the transalation table" %r_param)
                if py_param != r_param:
                    prm_translate[py_param] = r_param
        self._prm_translate = prm_translate
        if hasattr(sexp, '__rname__'):
            self.__rname__ = sexp.__rname__

    def __call__(self, *args, **kwargs):
        prm_translate = self._prm_translate
        for k in tuple(kwargs.keys()):
            r_k = prm_translate.get(k, None)
            if r_k is not None:
                v = kwargs.pop(k)
                kwargs[r_k] = v
        return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.