File: test-python-class.R

package info (click to toggle)
r-cran-reticulate 1.41.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,088 kB
  • sloc: cpp: 5,154; python: 620; sh: 13; makefile: 2
file content (243 lines) | stat: -rw-r--r-- 4,930 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
context("PyClass")

test_that("Can create a Python class and call methods", {
  skip_if_no_python()

  Hi <- PyClass("Hi", list(
    name = NULL,
    `__init__` = function(self, name) {
      self$name <- name
      NULL
    },
    say_hi = function(self) {
      paste0("Hi ", self$name)
    }
  ))

  a <- Hi("World")
  b <- Hi("R")

  expect_equal(a$say_hi(), "Hi World")
  expect_equal(b$say_hi(), "Hi R")

})

test_that("Can inherit from another class created in R", {
  skip_if_no_python()

  Hi <- PyClass("Hi", list(
    name = NULL,
    `__init__` = function(self, name) {
      self$name <- name
      NULL
    },
    say_hi = function(self) {
      paste0("Hi ", self$name)
    }
  ))

  Hello <- PyClass("Hello", inherit = Hi, list(
    say_hello = function(self) {
      paste0("Hello and ", super()$say_hi())
    }
  ))

  a <- Hello("World")
  expect_equal(a$say_hello(), "Hello and Hi World")
})

test_that("Can inherit from a Python class", {
  skip_if_no_python()

  py <- reticulate::py_run_string("
class Person(object):
  def __init__ (self, name):
    self.name = name
")

  Person2 <- PyClass("Person2", inherit = py$Person, list(
    `__init__` = function(self, name) {
      super()$`__init__`(name)
      self$test <- name
      NULL
    }
  ))


  a <- Person2("World")
  expect_equal(a$name, "World")
  expect_equal(a$test, "World")
})

test_that("Can inherit from multiple Python classes", {
  skip_if_no_python()

  py <- reticulate::py_run_string("
class Clock(object):
  def __init__ (self, time):
    self.time = time

class Calendar(object):
  def __init__ (self, date):
    self.date = date
")

  ClockCalendar <- PyClass("ClockCalendar", inherit = list(py$Clock, py$Calendar), list(
    `__init__` = function(self, time, date) {
      py$Clock$`__init__`(self, time)
      py$Calendar$`__init__`(self, date)
    }
  ))

  a <- ClockCalendar("15:54", "2019-11-05")

  expect_equal(a$date, "2019-11-05")
  expect_equal(a$time, "15:54")
})

test_that("Can define and instantiate empty classes", {
  skip_if_no_python()

  Hi <- PyClass("Hi")
  x <- Hi()
  x$name <- "Hi"

  expect_s3_class(x, "Hi")
  expect_equal(x$name, "Hi")
})

test_that("Methods can access enclosed env", {
  skip_if_no_python()

  Hi <- PyClass("Hi", list(
    say_a = function(self) {
      a
    }
  ))

  x <- Hi()

  a <- 1
  expect_equal(x$say_a(), 1)
  a <- 2
  expect_equal(x$say_a(), 2)
})

test_that("Can directly access variables from a class", {

  skip_if_no_python()

  bt <- import_builtins()

  Hi <- PyClass("Hi", list(
    `__init__` = function(self, a) {
      self$a <- a
      self$x <- bt$isinstance # Have a python that can't be
                              # converted to an R type
      NULL
    },
    add_2 = function(self) {
      self$a + 2
    },
    get_class = function(self) {
      class(self$a)
    },
    add_n = function(self, n) {
      self$a + n
    }
  ))

  x <- Hi(a = 2)
  expect_equal(x$add_2(), 4)
  expect_equal(x$a, 2)
  expect_equal(x$get_class(), "numeric")
  expect_equal(x$add_n(10), 12)
})

test_that("Properties are automatically converted in inherited classes", {
  skip_if_no_python()

  bt <- import_builtins(convert = FALSE)
  p <- py_run_string("
class Base(object):
  def __init__ (self, x):
    self.x = 1
", convert = FALSE)

  Hi <- PyClass("Hi", inherit = p$Base, list(
    `__init__` = function(self, a, ...) {
      self$a <- a
      self$k <- bt$isinstance # Have a python that can't be
                              # converted to an R type
      super()$`__init__`(...)
    },
    add_2 = function(self) {
      self$a + 2
    },
    get_class = function(self) {
      class(self$a)
    },
    add_n = function(self, n) {
      self$a + n
    }
  ))

  x <- Hi(a = 2, x = 1)
  expect_equal(x$add_2(), 4)
  expect_equal(x$a, 2)
  expect_equal(x$get_class(), "numeric")
  expect_equal(x$add_n(10), 12)
})

test_that("self is not converted when there's a py_to_r method for it", {
  skip_if_no_python()

  bt <- import_builtins(convert = FALSE)
  Base <- PyClass("Base")

  Hi <- PyClass("Hi", inherit = Base, list(
    `__init__` = function(self, a) {
      self$a <- a
      NULL
    },
    add_2 = function(self) {
      self$a + 2
    }
  ))

  assign("py_to_r.Base", value = function(x) {
    "base"
  }, envir = .GlobalEnv)

  x <- Hi(2)
  expect_equal(x, "base")

  rm(py_to_r.Base, envir = .GlobalEnv)
})

test_that("can call super from an inherited class", {

  Base1 <- PyClass("Base1", list(`__init__` = function(self, a) {
    self$a <- a
  }))

  Base2 <- PyClass("Base2", list(`__init__` = function(self, a, b) {
    self$b <- b
    super()$`__init__`(a)
  }), inherit = Base1)

  Inhe <- PyClass("Inhe", inherit = Base2, list(
    `__init__` = function(self, a, b, c) {
      self$c <- c
      super()$`__init__`(a, b)
      NULL
    }
  ))

  x <- Inhe(10, 20, 30)


  expect_equal(x$a, 10)
  expect_equal(x$b, 20)
  expect_equal(x$c, 30)
})