Skip to content

Commit

Permalink
bugfix (thanks @ArnaudBelcour)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aluriak committed Nov 4, 2019
1 parent f92248e commit f2c65e8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clyngor/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ def _parse_answer(self, answer_set:str) -> iter:
yield from self.__finish_parsing(naive_parsing_of_answer_set(answer_set, discard_quotes=self._discard_quotes and not self._collapse_atoms, parse_int=self._parse_int, parse_args=True or self._collapse_args or self._first_arg_only))
elif isinstance(answer_set, (set, tuple)) and all(isinstance(atom, (str, int, tuple)) for atom in answer_set): # already parsed
# print('FROM SET OR TUPLE')
if not self._parse_int:
answer_set = utils.integers_to_string_atoms(answer_set)
yield from self.__finish_parsing(answer_set)
else: # unknown format
raise ValueError(f"unknow answer set format: {type(answer_set)}, {answer_set}")
Expand Down
10 changes: 10 additions & 0 deletions clyngor/test/test_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,13 @@ def test_optimization_access(optimized_answers):
answers.with_answer_number
assert next(answers) == ({'edge(4,"s…lp.")', 'r_e_l(1,2)'}, 4, False, 5)
assert next(answers, None) is None

def test_int_not_parsed():
"Ensures that .int_not_parsed modifier behave correctly"
asp = 'a(1). #show a/1.'
for model in next(clyngor.ASP(asp)):
t = type(model[1][0])
assert t is int, t
for model in next(clyngor.ASP(asp).int_not_parsed):
t = type(model[1][0])
assert t is str, t
13 changes: 13 additions & 0 deletions clyngor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ def python_value_to_asp(val:str or int or list or tuple, *, args_of_predicate:bo
# python_value_to_asp.in_predicate = lambda x: python_value_to_asp(x, args_of_predicate=True)


def integers_to_string_atoms(model:iter) -> object:
"""Return an identical structure of (frozen)set, tuple and list, but with integer values as string"""
if isinstance(model, (list, tuple, frozenset, set)):
return type(model)(map(integers_to_string_atoms, model))
elif isinstance(model, dict):
return {integers_to_string_atoms(k): integers_to_string_atoms(v)
for k, v in model.items()}
elif isinstance(model, int):
return str(model)
else:
return model


def answer_set_to_str(answer_set:iter, atom_end:str='', atom_sep:str=' ') -> str:
"""Returns the string representation of given answer set.
Expand Down

0 comments on commit f2c65e8

Please sign in to comment.