From 865ff8a91a0244fcbaae8b01d28f526d66794370 Mon Sep 17 00:00:00 2001 From: Plamen Dimitrov Date: Mon, 18 Apr 2022 04:21:27 +0300 Subject: [PATCH] Reorder and complete the string and file serialization methods We were missing to_cnf_string() methods which are usually replaced by the simple `str(Cnf(...))` but it is still better to add them for completeness in code relying on the full mix of serialization methods at least for the sake of syntactic consistency and thus better readability in some particular use cases. --- src/cnfvar/model.py | 75 +++++++++++++++++++++++++++++++++----------------- 1 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/cnfvar/model.py b/src/cnfvar/model.py index d04e8dd..6e46bdd 100644 --- a/src/cnfvar/model.py +++ b/src/cnfvar/model.py @@ -508,6 +508,18 @@ class BaseCnf: class CnfListSerializationMixin(BaseCnfList): """Add serialization support to BaseCnfList.""" + def to_cnf_string(self, renumber=True): + """ + Generate a string representation of this list in the cnfvar format. + + :param bool renumber: whether to fix the lineno of the cnfvars + :returns: the CNF string + :rtype: str + """ + if renumber: + self.renumber() + return str(self) + def to_cnf_file(self, path, renumber=True, encoding=ENCODING): """ Dump a string representation of this list in the cnfvar format to a file. @@ -583,18 +595,6 @@ class CnfListSerializationMixin(BaseCnfList): return CnfList._from_cnf_structure(cnf_obj) @classmethod - def from_json_string(cls, data): - """ - Create a list from a json string. - - :param str data: string to generate the list from - :returns: a list of cnfvars - :rtype: :py:class:`CnfList` - """ - cnf_obj = json.loads(data) - return CnfList._from_cnf_structure(cnf_obj) - - @classmethod def from_cnf_file(cls, path, encoding=ENCODING): """ Create a list from a cnfvar file. @@ -609,6 +609,18 @@ class CnfListSerializationMixin(BaseCnfList): return CnfList.from_cnf_string(fp.read()) @classmethod + def from_json_string(cls, data): + """ + Create a list from a json string. + + :param str data: string to generate the list from + :returns: a list of cnfvars + :rtype: :py:class:`CnfList` + """ + cnf_obj = json.loads(data) + return CnfList._from_cnf_structure(cnf_obj) + + @classmethod def from_json_file(cls, path): """ Create a list from a json file. @@ -624,15 +636,15 @@ class CnfListSerializationMixin(BaseCnfList): class CnfSerializationMixin(BaseCnf): """Add serialization support to BaseCnf.""" - def to_json_string(self, renumber=True): + def to_cnf_string(self, renumber=True): """ - Convert this instance to a JSON string. + Generate a string representation of this list in the cnfvar format. - :param bool renumber: whether to fix the lineno of the cnfvars - :returns: the JSON string + :param bool renumber: whether to fix the lineno of this cnfvar and its children + :returns: the CNF string :rtype: str """ - return CnfList([self]).to_json_string(renumber=renumber) + return CnfList([self]).to_cnf_string(renumber=renumber) def to_cnf_file(self, path, renumber=True, encoding=ENCODING): """ @@ -644,6 +656,16 @@ class CnfSerializationMixin(BaseCnf): """ CnfList([self]).to_cnf_file(path, renumber=renumber, encoding=encoding) + def to_json_string(self, renumber=True): + """ + Convert this instance to a JSON string. + + :param bool renumber: whether to fix the lineno of the cnfvars + :returns: the JSON string + :rtype: str + """ + return CnfList([self]).to_json_string(renumber=renumber) + def to_json_file(self, path, renumber=True): """ Dump a JSON representation of this instance to a file. @@ -683,27 +705,28 @@ class CnfSerializationMixin(BaseCnf): return CnfListSerializationMixin.from_cnf_string(data).single() @classmethod - def from_json_string(cls, data): + def from_cnf_file(cls, path, encoding=ENCODING): """ - Create an instance of this class from a JSON string. + Create an instance of this class from a cnfvar file. - :param str data: JSON string to convert + :param str path: path to the file to read + :param str encoding: encoding to use to read the file :returns: the cnf instance created :rtype: :py:class:`Cnf` """ - return CnfListSerializationMixin.from_json_string(data).single() + return CnfListSerializationMixin.from_cnf_file(path, encoding=encoding).single() @classmethod - def from_cnf_file(cls, path, encoding=ENCODING): + def from_json_string(cls, data): """ - Create an instance of this class from a cnfvar file. + Create an instance of this class from a JSON string. - :param str path: path to the file to read - :param str encoding: encoding to use to read the file + :param str data: JSON string to convert :returns: the cnf instance created :rtype: :py:class:`Cnf` """ - return CnfListSerializationMixin.from_cnf_file(path, encoding=encoding).single() + cnf_obj = json.loads(data) + return CnfList._from_cnf_structure(cnf_obj) @classmethod def from_json_file(cls, path): -- 1.7.1