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: 1 addition & 0 deletions python/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ def test_char_vs_string(self):
self.assertEqual(repr(metta.run('!("A")')), '[[("A")]]')
self.assertEqualMettaRunnerResults(metta.run("!(get-type 'A')"), [[S('Char')]])
self.assertEqualMettaRunnerResults(metta.run('!(get-type "A")'), [[S('String')]])
self.assertEqual(metta.run('!(repr "A")')[0][0].get_object(), ValueObject("\"A\""))
vsbogd marked this conversation as resolved.
Show resolved Hide resolved


class SomeObject():
Expand Down
Loading