-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbasic_example.erl
More file actions
67 lines (48 loc) · 2.17 KB
/
basic_example.erl
File metadata and controls
67 lines (48 loc) · 2.17 KB
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
#!/usr/bin/env escript
%%% @doc Basic example of using erlang_python.
%%%
%%% Prerequisites: rebar3 compile
%%% Run from project root: escript examples/basic_example.erl
-mode(compile).
main(_) ->
%% Add the compiled beam files to the code path
ScriptDir = filename:dirname(escript:script_name()),
ProjectRoot = filename:dirname(ScriptDir),
EbinDir = filename:join([ProjectRoot, "_build", "default", "lib", "erlang_python", "ebin"]),
true = code:add_pathz(EbinDir),
%% Start the application
{ok, _} = application:ensure_all_started(erlang_python),
io:format("~n=== Basic Python Calls ===~n~n"),
%% Simple math
{ok, Result1} = py:call(math, sqrt, [16]),
io:format("math.sqrt(16) = ~p~n", [Result1]),
%% String operations (using eval since str methods are on objects, not a module)
{ok, Result2} = py:eval(<<"'hello world'.upper()">>),
io:format("'hello world'.upper() = ~s~n", [Result2]),
%% JSON encoding
{ok, Json} = py:call(json, dumps, [#{name => <<"Alice">>, age => 30}]),
io:format("json.dumps({name: 'Alice', age: 30}) = ~s~n", [Json]),
%% JSON decoding
{ok, Decoded} = py:call(json, loads, [<<"{\"foo\": [1, 2, 3]}">>]),
io:format("json.loads(...) = ~p~n", [Decoded]),
io:format("~n=== Python Eval ===~n~n"),
%% Evaluate expressions
{ok, Sum} = py:eval(<<"sum(range(10))">>),
io:format("sum(range(10)) = ~p~n", [Sum]),
%% Eval with locals
{ok, Computed} = py:eval(<<"x * 2 + y">>, #{x => 10, y => 5}),
io:format("x * 2 + y (x=10, y=5) = ~p~n", [Computed]),
io:format("~n=== List Operations ===~n~n"),
%% List comprehension
{ok, Squares} = py:eval(<<"[x**2 for x in range(5)]">>),
io:format("[x**2 for x in range(5)] = ~p~n", [Squares]),
io:format("~n=== Async Calls ===~n~n"),
%% Async call with spawn_call/await
Ref1 = py:spawn_call(math, factorial, [10]),
Ref2 = py:spawn_call(math, factorial, [20]),
{ok, Fact10} = py:await(Ref1),
{ok, Fact20} = py:await(Ref2),
io:format("math.factorial(10) = ~p~n", [Fact10]),
io:format("math.factorial(20) = ~p~n", [Fact20]),
io:format("~n=== Done ===~n~n"),
ok = application:stop(erlang_python).