Skip to content

Commit db64ee6

Browse files
committed
python python/hyperlog.py soup
1 parent d505f13 commit db64ee6

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

python/hyperlog.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,48 @@
11
import os
22
import atexit
3-
import tempfile
4-
import collections.abc
5-
from hyperon import *
6-
from hyperon.ext import register_atoms
7-
from hyperon.atoms import OperationAtom
8-
import janus_swi as janus
93

4+
import janus_swi as janus
105

11-
PROLOG_BOOTSTRAP = r"""
6+
def _initialize_prolog_driver():
7+
janus.consult("hyperlog",
8+
r"""
129
1310
:- use_module(library(debug)).
1411
:- nodebug(hyperlog).
1512
1613
ensure_mettalog_modules :-
17-
( current_predicate(eval_args/3) -> true ; ensure_loaded(library(metta_rt)) ).
14+
ensure_loaded(library(metta_rt)).
1815
1916
:- ensure_mettalog_modules.
2017
21-
"""
22-
23-
def _write_temp_prolog_file(code: str):
24-
tmp = tempfile.NamedTemporaryFile("w+", suffix=".pl", delete=False)
25-
tmp.write(code)
26-
tmp.flush()
27-
return tmp.name
28-
29-
30-
def _initialize_prolog_driver():
31-
temp_file = _write_temp_prolog_file(PROLOG_BOOTSTRAP)
32-
janus.consult(temp_file)
18+
""")
3319

3420

3521
# ✅ Prolog bootstrap happens at module load (similar to __init__)
3622
_initialize_prolog_driver()
3723

3824

3925
def pretty_print_result(result, prefix=" => "):
40-
if isinstance(result, collections.abc.Iterator) and not isinstance(result, (str, bytes, list, tuple, dict, set)):
26+
if not isinstance(result, (str, bytes, list, tuple, set)):
4127
try:
28+
# Attempt indexed iteration (like for generators or custom iterables)
4229
for i, item in enumerate(result):
4330
print(f"{prefix}[{i}] {item}")
44-
except Exception as e:
45-
print(f"Error iterating result: {e}")
46-
47-
print(f"{prefix}{result}")
48-
elif isinstance(result, (list, tuple, set)):
49-
for i, item in enumerate(result):
50-
print(f"{prefix}[{i}] {item}")
51-
else:
52-
print(f"{prefix}{result}")
31+
return
32+
except Exception: pass
33+
34+
try:
35+
# Attempt flat iteration (like for sets, dict keys, etc.)
36+
for item in result:
37+
print(f"{prefix}{item}")
38+
return
39+
40+
except Exception: pass
41+
42+
# Final fallback: just print the object
43+
print(f"{prefix}{result}")
44+
45+
5346

5447

5548
class MeTTaLogImpl:

0 commit comments

Comments
 (0)