-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path04_functions.ring
More file actions
32 lines (26 loc) · 835 Bytes
/
04_functions.ring
File metadata and controls
32 lines (26 loc) · 835 Bytes
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
/*
04 - Calling Python Functions
Use py_call() to invoke any Python function by dotted path.
*/
load "python.ring"
py_init()
py_exec("import math")
py_exec("import os.path")
# Built-in functions
? "len('hello') = " + py_call("len", ["hello"])
? "abs(-42) = " + py_call("abs", [-42])
? "max(3,7,1) = " + py_call("max", [[3, 7, 1]])
# Module functions
? ""
? "math.sqrt(144) = " + py_call("math.sqrt", [144])
? "math.pow(2, 10) = " + py_call("math.pow", [2, 10])
? "math.factorial(8) = " + py_call("math.factorial", [8])
# os.path
? ""
? "os.path.join: " + py_call("os.path.join", ["/home", "user", "docs"])
# With keyword arguments (py_call with 3 params: func, args, kwargs)
py_exec("import json")
result = py_call("json.dumps", [[["x", 1], ["y", 2]]], [["indent", 2]])
? ""
? "json.dumps with indent:"
? result