Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python's repr fixed #828

Merged
merged 14 commits into from
Jan 14, 2025
3 changes: 2 additions & 1 deletion python/hyperon/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ def __repr__(self):
# Overwrite Python default representation of a string to use
# double quotes instead of single quotes.
if isinstance(self.content, str):
return f'"{self.content}"'
newstr = self.content.translate(str.maketrans({'"' : r'\"'}))
vsbogd marked this conversation as resolved.
Show resolved Hide resolved
return f'"{newstr}"'

# Use default representation for everything else
return repr(self.content) if self.id is None else self.id
Expand Down
2 changes: 1 addition & 1 deletion python/hyperon/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def text_ops(run_context):

"""

reprAtom = OperationAtom('repr', lambda a: [ValueAtom(repr(a))],
reprAtom = OperationAtom('repr', lambda a: [ValueAtom(repr(a), 'String')],
['Atom', 'String'], unwrap=False)
parseAtom = OperationAtom('parse', lambda s: [SExprParser(str(s)[1:-1]).parse(run_context.tokenizer())],
['String', 'Atom'], unwrap=False)
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ def test_char_vs_string(self):
self.assertEqualMettaRunnerResults(metta.run("!(get-type 'A')"), [[S('Char')]])
self.assertEqualMettaRunnerResults(metta.run('!(get-type "A")'), [[S('String')]])


class SomeObject():

def __init__(self):
Expand Down
4 changes: 4 additions & 0 deletions python/tests/test_grounded_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def test_higher_func(self):
self.assertEqual(metta.run("!((curry_num plus 1) 2)"),
metta.run("! 3"))

def test_string_repr(self):
metta = MeTTa(env_builder=Environment.test_env())
self.assertEqual(metta.run('!(repr "A")')[0][0].get_object(), ValueObject("\"A\""))

def test_meta_types(self):
metta = MeTTa(env_builder=Environment.test_env())
### Basic functional types
Expand Down
4 changes: 2 additions & 2 deletions python/tests/test_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def test_text_ops(self):
#self.assertEqualMettaRunnerResults(metta.run('!(parse "$X")'),
# [[(V("X"))]])

self.assertEqualMettaRunnerResults(metta.run('!(parse "\\"A\\"")'),
[[(ValueAtom("A"))]])
self.assertEqualMettaRunnerResults(repr(metta.run('!(parse "\\"A\\"")')),
'[[\\"A\\"]]')
vsbogd marked this conversation as resolved.
Show resolved Hide resolved

#self.assertEqualMettaRunnerResults(metta.run('!(parse "(func (Cons $x (Cons $xs $xss))) ")'),
# [[E(S("func"), E(S("Cons"), V("x"), E(S("Cons"), V("xs"), V("xss"))))]])
Expand Down
Loading