-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path05_modules.ring
More file actions
44 lines (36 loc) · 921 Bytes
/
05_modules.ring
File metadata and controls
44 lines (36 loc) · 921 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
33
34
35
36
37
38
39
40
41
42
43
44
/*
05 - Importing Modules
Import Python modules and access their attributes.
*/
load "python.ring"
py_init()
# Import modules using py_import() — returns a pointer
math = py_import("math")
json = py_import("json")
os = py_import("os")
sys = py_import("sys")
# Access module attributes with py_getattr()
? "math.pi = " + py_getattr(math, "pi")
? "math.e = " + py_getattr(math, "e")
# Check if an attribute exists
? ""
? "math has 'sqrt': " + py_hasattr(math, "sqrt")
? "math has 'hello': " + py_hasattr(math, "hello")
# Get the current working directory
? ""
? "CWD: " + py_call("os.getcwd", [])
# List sys.path entries
? ""
? "Python sys.path:"
py_exec("import sys; _paths = sys.path[:5]")
paths = py_get("_paths")
for p in paths
? " " + p
next
# Get module dir listing (first 10 items)
? ""
? "First 10 items in dir(math):"
dirlist = py_dir(math)
for i = 1 to 10
? " " + dirlist[i]
next