def get_reticulate_completions(document, complete_event): word = document.get_word_before_cursor() prefix_length = settings.completion_prefix_length if len(word ) < prefix_length and not complete_event.completion_requested: return [] glo = rcopy(rcall(("reticulate", "py_run_string"), "globals()")) loc = rcopy(rcall(("reticulate", "py_run_string"), "locals()")) try: script = jedi.Interpreter(document.text, column=document.cursor_position_col, line=document.cursor_position_row + 1, path="input-text", namespaces=[glo, loc]) return [ Completion( text_type(c.name_with_symbols), len(text_type(c.complete)) - len(text_type(c.name_with_symbols))) for c in script.completions() ] except Exception: return []
def test_strings(): assert rcall("identical", robject("abc"), rstring("abc"), _convert=True) assert rcall("identical", robject("β"), rstring("β"), _convert=True) assert rcall("identical", robject("你"), rstring("你"), _convert=True) assert rcall("identical", robject(['a', 'b']), reval("c('a', 'b')"), _convert=True)
def installed_packages(): try: return rcall(("base", ".packages"), **{ "all.available": True, "_convert": True }) except Exception: return []
def apply_settings(session, settings): setoption("prompt", settings.prompt) if settings.auto_width: output_width = session.app.output.get_size().columns if output_width: setoption("width", output_width) # necessary on windows setoption("menu.graphics", False) if is_windows() and roption("crayon.enabled", None) is None: setoption("crayon.enabled", True) # enables completion of installed package names if rcopy(rcall(("utils", "rc.settings"), "ipck")) is None: rcall(("utils", "rc.settings"), ipck=True)
def test_functions(): def f(x): return x + 3 fun = robject(f) assert "PyCallable" in rclass(fun) assert rcopy(rcall(fun, 4)) == f(4) assert rcopy(rcall(fun, x=4)) == f(4) fun = robject(lambda x: x + 3, convert=False) assert "PyCallable" in rclass(fun) ret = rcall(fun, 4) assert "PyObject" in rclass(ret) assert rcopy(ret) == f(4) makef = robject(lambda: f, convert=False) ret = rcall(makef) assert "PyCallable" in rclass(ret) assert rcopy(ret) == f
def apply_settings(session, settings): setoption("prompt", settings.prompt) if settings.auto_width: output_width = session.app.output.get_size().columns if output_width: setoption("width", output_width) # necessary on windows setoption("menu.graphics", False) def askpass(message): from prompt_toolkit import prompt return prompt(message, is_password=True) setoption("askpass", askpass) # enables completion of installed package names if rcopy(rcall(("utils", "rc.settings"), "ipck")) is None: rcall(("utils", "rc.settings"), ipck=True)
def get_reticulate_completions(document, complete_event): word = document.get_word_before_cursor() if len(word) < 3 and not complete_event.completion_requested: return [] glo = rcopy(rcall(("reticulate", "py_run_string"), "globals()")) loc = rcopy(rcall(("reticulate", "py_run_string"), "locals()")) try: script = jedi.Interpreter(document.text, column=document.cursor_position_col, line=document.cursor_position_row + 1, path="input-text", namespaces=[glo, loc]) except Exception: script = None if not script: return [] return list(script.completions())
def test_numbers(): assert rcall("identical", robject(1), rint(1), _convert=True) assert rcall("identical", robject(1.0), rdouble(1), _convert=True) assert not rcall("identical", robject(1), rdouble(1), _convert=True) assert not rcall("identical", robject(1.0), rint(1), _convert=True) assert rcall("identical", robject(complex(1, 2)), reval("1 + 2i"), _convert=True) assert rcall("identical", robject([1, 2]), reval("c(1L, 2L)"), _convert=True) assert rcall("identical", robject([1.0, 2.0]), reval("c(1, 2)"), _convert=True) assert rcall("identical", robject([complex(1, 2), complex(2, 1)]), reval("c(1 + 2i, 2 + 1i)"), _convert=True)
def package_is_loaded(pkg): return pkg in rcopy(rcall(("base", "loadedNamespaces")))
def test_rprint(): la = rlang(robject(rprint, asis=True), robject(1)) assert rcall("capture.output", la, _convert=True) == ['[1] 1', 'NULL']
def user_path(*args): return make_path(rcopy(rcall(("base", "path.expand"), "~")), *args)
def source_file(path): rcall(("base", "source"), path, rcall(("base", "new.env")))
def test_rprint(gctorture): la = rlang(robject(rprint, asis=True, invisible=True), robject(1)) assert rcall("capture.output", la, _convert=True) == "[1] 1"
def test_raw(): assert rcall("rawToChar", robject("raw", b"hello"), _convert=True) == "hello"
def reticulate_prompt(*args): rcall(("base", "source"), os.path.join(os.path.dirname(__file__), "data", "register_reticulate.R"), new_env())
def reticulate_hook(*args): rcall(("base", "source"), os.path.join(os.path.dirname(__file__), "data", "patching_reticulate.R"), new_env())
def test_py_tools(): env = rcall(("base", "new.env")) reval("getOption('rchitect.py_tools')$attach()", envir=env) assert "import" in rcall("names", env, _convert=True) reval("os <- import('os')", envir=env) path = reval(""" os$path$join("foo", "bar") """, envir=env) assert "character" in rclass(path) assert rcopy(path) == os.path.join("foo", "bar") path = reval(""" py_call(os$path$join, "foo", "bar") """, envir=env) assert "PyObject" in rclass(path) assert rcopy(path) == os.path.join("foo", "bar") ret = reval(""" bulitins <- import_builtins() len <- bulitins$len len(py_eval("[1, 2, 3]")) """, envir=env) assert rcopy(ret) == 3 ret = reval(""" pyo <- py_object("hello") py_copy(pyo) """, envir=env) assert rcopy(ret) == "hello" ret = reval(""" x <- py_eval("[1, 2, 3]") x[2L] """, envir=env) assert rcopy(ret) == 3 ret = reval(""" x <- py_eval("[1, 2, 3]") x[2L] <- 4L x """, envir=env) assert rcopy(ret) == [1, 2, 4] ret = reval(""" d <- dict(a = 1L, b = 2L) d['b'] """, envir=env) assert rcopy(ret) == 2 ret = reval(""" Foo <- py_eval("type(str('Foo'), (object,), {})") foo <- Foo() foo$x <- 1L foo """, envir=env) assert rcopy(ret).x == 1 assert rcopy(reval("py_unicode('hello')", envir=env)) == "hello" assert rcopy(reval("tuple('a', 3)", envir=env)) == ('a', 3)
def test_booleans(): assert rcall("identical", robject([True, False]), reval("c(TRUE, FALSE)"), _convert=True)
def test_rcall_error(gctorture): with pytest.raises(Exception) as excinfo: rcall("sum", ["a", "b"]) assert "invalid 'type' (character) of argument" in str(excinfo.value)
def test_ordered_list(): d = OrderedDict([("a", 2), ("b", "hello")]) assert rcall("identical", robject(d), reval("list(a = 2L, b = 'hello')"), _convert=True)
def test_none(): assert rcall("identical", robject(None), reval("NULL"), _convert=True)
def execute_key_bindings_script(*args): rcall( ("base", "source"), os.path.join(os.path.dirname(__file__), "R", "key_bindings.R"), rcall("new.env"))
def finalizer(cleanup): rcall(("base", "reg.finalizer"), rcall(("base", "getOption"), "rchitect.py_tools"), cleanup, onexit=True)
def gctorture(): rcall("gctorture", True) yield rcall("gctorture", False)
def reticulate_prompt_hook(*args): rcall( ("base", "source"), os.path.join(os.path.dirname(__file__), "R", "reticulate.R"), rcall("new.env"))
def test_lambda(): rcopy(rcall(robject(lambda x: x + 3), 4)) == 7
def reticulate_message_hook(*args): if not roption("radian.suppress_reticulate_message", False): rcall("packageStartupMessage", RETICULATE_MESSAGE)
def user_path(*args): return os.path.join(rcopy(rcall(("base", "path.expand"), "~")), *args)
def test_r_to_py_rchitect_object(): reval("library(reticulate)") foo = Foo() x = rcall("r_to_py", robject(foo)) assert "python.builtin.object" in rcopy(rcall("class", x))
def greeting(): info = rcopy(rcall(rsym("R.Version"))) return "{} -- \"{}\"\nPlatform: {} ({}-bit)\n".format( info["version.string"], info["nickname"], info["platform"], 8 * struct.calcsize("P"))