From eeb12f95da108d580e7fe7ca1d834345bc9e0fe1 Mon Sep 17 00:00:00 2001 From: ssardina Date: Wed, 20 Mar 2024 10:02:10 +1100 Subject: [PATCH] fix: address formatting and style issues --- pddl/core.py | 25 ++++++++++++------------- pddl/formatter.py | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/pddl/core.py b/pddl/core.py index 1ce13eb..8c27509 100644 --- a/pddl/core.py +++ b/pddl/core.py @@ -14,6 +14,7 @@ It contains the class definitions to build and modify PDDL domains or problems. """ +from textwrap import indent from typing import AbstractSet, Collection, Dict, Optional, Tuple, cast from pddl._validation import ( @@ -26,6 +27,14 @@ from pddl.action import Action from pddl.custom_types import name as name_type from pddl.custom_types import namelike, parse_name, to_names, to_types # noqa: F401 +from pddl.formatter import ( + print_constants, + print_function_skeleton, + print_predicates_with_types, + print_types_or_functions_with_parents, + remove_empty_lines, + sort_and_print_collection, +) from pddl.helpers.base import assert_, check, ensure, ensure_set from pddl.logic.base import And, Formula, is_literal from pddl.logic.functions import FunctionExpression, Metric, NumericFunction @@ -33,16 +42,6 @@ from pddl.logic.terms import Constant from pddl.requirements import Requirements -from pddl.formatter import ( - sort_and_print_collection, - print_types_or_functions_with_parents, - print_predicates_with_types, - remove_empty_lines, - print_constants, - print_function_skeleton, - indent, -) - class Domain: """A class for a PDDL domain file.""" @@ -220,9 +219,9 @@ def __init__( self._domain, self._domain_name = self._parse_domain_and_domain_name( domain, domain_name ) - self._requirements: Optional[ - AbstractSet[Requirements] - ] = self._parse_requirements(domain, requirements) + self._requirements: Optional[AbstractSet[Requirements]] = ( + self._parse_requirements(domain, requirements) + ) self._objects: AbstractSet[Constant] = ensure_set(objects) self._init: AbstractSet[Formula] = ensure_set(init) self._goal: Formula = ensure(goal, And()) diff --git a/pddl/formatter.py b/pddl/formatter.py index 483778f..3655417 100644 --- a/pddl/formatter.py +++ b/pddl/formatter.py @@ -11,7 +11,6 @@ # """Formatting utilities for PDDL domains and problems.""" -from textwrap import indent from typing import Callable, Collection, Dict, List, Optional, TypeVar from pddl.custom_types import name @@ -33,6 +32,20 @@ def sort_and_print_collection( to_string: Callable = str, is_mandatory: bool = False, ): + """Given a collection (requirements, actions, objects, etc.) produced its sorted string version with prefix and postfix strings. + Prefix is used to start with strings like "(:requirements" or "(:actions" + Postfix is used to close the structure, usually with ")\n" + + Args: + prefix (str): start of the string + collection (Collection): the collection of entities to report as a string + postfix (str): the end of the string + to_string (Callable, optional): the function to use to convert to string. Defaults to str. + is_mandatory (bool, optional): if the string is mandatory even if the collection is empty. Defaults to False. + + Returns: + str: a string with + """ if len(collection) > 0: return prefix + " ".join(sorted(map(to_string, collection))) + postfix elif is_mandatory: