Skip to content

Commit

Permalink
fixed import-py!
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSPoon committed Aug 26, 2024
1 parent bdbba01 commit e5b69e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/canary/metta_python.pl
Original file line number Diff line number Diff line change
Expand Up @@ -806,18 +806,29 @@
(nonvar(File)-> Use=File ; Use=Module),
pybug('extend-py!'(Use)),
%py_call(mettalog:use_mettalog()),
(Use==mettalog->true;(py_call(mettalog:load_functions(Use),R),pybug(R))),
(Use==mettalog->true;py_load_modfile(Use)),
%listing(ensure_rust_metta/1),
%ensure_mettalog_py,
nb_setval('$py_ready','true'),
%working_directory(PWD,PWD), py_add_lib_dir(PWD),
%replace_in_string(["/"="."],Module,ToPython),
%py_call(mettalog:import_module_to_rust(ToPython)),
%sformat(S,'!(import! &self ~w)',[ToPython]),rust_metta_run(S),
%py_mcall(mettalog:import_module_to_rust(ToPython)),
%sformat(S,'!(import! &self ~w)',[Use]),rust_metta_run(S,R),
R = [],
%py_module_exists(Module),
%py_call(MeTTa:load_py_module(ToPython),Result),
true)),!.

py_load_modfile(Use):- py_mcall(mettalog:load_functions(Use),R),!,pybug(R).
py_load_modfile(Use):- exists_directory(Use),!,directory_file_path(Use,'_init_.py',File),py_load_modfile(File).
py_load_modfile(Use):- file_to_modname(Use,Mod),read_file_to_string(Use,Src),!,py_module(Mod,Src).

file_to_modname(Filename,ModName):- symbol_concat('../',Name,Filename),!,file_to_modname(Name,ModName).
file_to_modname(Filename,ModName):- symbol_concat('./',Name,Filename),!,file_to_modname(Name,ModName).
file_to_modname(Filename,ModName):- symbol_concat(Name,'/_init_.py',Filename),!,file_to_modname(Name,ModName).
file_to_modname(Filename,ModName):- symbol_concat(Name,'.py',Filename),!,file_to_modname(Name,ModName).
file_to_modname(Filename,ModName):- replace_in_string(["/"="."],Filename,ModName).

%import_module_to_rust(ToPython):- sformat(S,'!(import! &self ~w)',[ToPython]),rust_metta_run(S).
rust_metta_run(S,Run):- var(S),!,freeze(S,rust_metta_run(S,Run)).
rust_metta_run(exec(S),Run):- \+ callable(S), string_concat('!',S,SS),!,rust_metta_run(SS,Run).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[(include! &corelb stdlib_minimal)]
[(include! &corelib stdlib_minimal)]
[(assertEqualNoOrder (Error (get-type A space) match expects a space as the first argument) (B C))]
[(assertEqualNoOrder (Error (get-type (f 42) space) match expects a space as the first argument) (String))]
[]
Expand All @@ -8,5 +8,5 @@
Expected: [()]
Got: []
Missed result: ())]
0.84user 0.01system 0:00.85elapsed 99%CPU (0avgtext+0avgdata 35076maxresident)k
0inputs+0outputs (0major+3959minor)pagefaults 0swaps
0.01user 0.00system 0:00.03elapsed 90%CPU (0avgtext+0avgdata 25044maxresident)k
0inputs+0outputs (0major+3023minor)pagefaults 0swaps

0 comments on commit e5b69e9

Please sign in to comment.