/usr/lib/python3/dist-packages/rpy2/robjects/tests/testMethods.py is in python3-rpy2 2.8.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | import unittest
import sys
import six
import rpy2.robjects as robjects
import rpy2.robjects.methods as methods
rinterface = robjects.rinterface
class MethodsTestCase(unittest.TestCase):
def testSet_accessors(self):
robjects.r['setClass']("A", robjects.r('list(foo="numeric")'))
robjects.r['setMethod']("length", signature="A",
definition = robjects.r("function(x) 123"))
class A(methods.RS4):
def __init__(self):
obj = robjects.r['new']('A')
self.__sexp__ = obj.__sexp__
acs = (('length', None, True, None), )
methods.set_accessors(A, "A", None, acs)
a = A()
self.assertEqual(123, a.length[0])
def testRS4_TypeNoAccessors(self):
robjects.r['setClass']("Foo", robjects.r('list(foo="numeric")'))
if sys.version_info[0] == 2:
classdef = """
from rpy2 import robjects
from rpy2.robjects import methods
class Foo(methods.RS4):
__metaclass__ = methods.RS4_Type
def __init__(self):
obj = robjects.r['new']('R_A')
self.__sexp__ = obj.__sexp__
"""
else:
classdef = """
from rpy2 import robjects
from rpy2.robjects import methods
class Foo(methods.RS4, metaclass = methods.RS4_Type):
def __init__(self):
obj = robjects.r['new']('R_A')
self.__sexp__ = obj.__sexp__
"""
code = compile(classdef, '<string>', 'exec')
ns = dict()
exec(code, ns)
f = ns['Foo']()
def testRS4instance_factory(self):
rclassname = 'Foo'
robjects.r['setClass'](rclassname,
robjects.r('list(bar="numeric")'))
obj = robjects.r['new'](rclassname)
f_rs4i = methods.rs4instance_factory(obj)
self.assertEqual(rclassname, type(f_rs4i).__name__)
def testRS4_TypeAccessors(self):
robjects.r['setClass']("R_A", robjects.r('list(foo="numeric")'))
robjects.r['setMethod']("length", signature="R_A",
definition = robjects.r("function(x) 123"))
if sys.version_info[0] == 2:
classdef = """
from rpy2 import robjects
from rpy2.robjects import methods
class R_A(methods.RS4):
__metaclass__ = methods.RS4_Type
__accessors__ = (('length', None,
'get_length', False, 'get the length'),
('length', None,
None, True, 'length'))
def __init__(self):
obj = robjects.r['new']('R_A')
self.__sexp__ = obj.__sexp__
"""
else:
classdef = """
from rpy2 import robjects
from rpy2.robjects import methods
class R_A(methods.RS4, metaclass=methods.RS4_Type):
__accessors__ = (('length', None,
'get_length', False, 'get the length'),
('length', None,
None, True, 'length'))
def __init__(self):
obj = robjects.r['new']('R_A')
self.__sexp__ = obj.__sexp__
"""
code = compile(classdef, '<string>', 'exec')
ns = dict()
exec(code, ns)
R_A = ns['R_A']
class A(R_A):
__rname__ = 'R_A'
ra = R_A()
self.assertEqual(123, ra.get_length()[0])
self.assertEqual(123, ra.length[0])
a = A()
self.assertEqual(123, a.get_length()[0])
self.assertEqual(123, a.length[0])
def testGetclassdef(self):
robjects.r('library(stats4)')
cr = methods.getclassdef('mle', 'stats4')
self.assertFalse(cr.virtual)
def testRS4Auto_Type(self):
robjects.r('library(stats4)')
class MLE(six.with_metaclass(robjects.methods.RS4Auto_Type,
robjects.methods.RS4)):
__rname__ = 'mle'
__rpackagename__ = 'stats4'
def testRS4Auto_Type_nopackname(self):
robjects.r('library(stats4)')
class MLE(six.with_metaclass(robjects.methods.RS4Auto_Type,
robjects.methods.RS4)):
__rname__ = 'mle'
def suite():
suite = unittest.TestLoader().loadTestsFromTestCase(MethodsTestCase)
return suite
if __name__ == '__main__':
unittest.main()
|