print_cnf_raw(*argv, **kw_argv)
-def output_cnf(root, out, renumber=False, bytes=True):
+def output_cnf(root, out, renumber=False):
+ """
+ Dump a textual representation of given CNF VAR structure to given stream.
+
+ Runs :py:func:`format_cnf_vars` on the input (`root`) and then writes that
+ to the given file-like object (out).
+
+ :param root: a CNF_VAR structure
+ :type root: dict or list or anything that :py:func:`cnf_root` accepts
+ :param out: file-like object or something with a `write(str)` function
+ :param bool renumber: Whether to renumber cnfvars first
+ """
cnf = cnf_root(root)
if renumber is True:
_count = renumber_vars(root)
if is_cnf(cnf) is True:
(_, lines) = functools.reduce(format_cnf_vars, cnf, (0, []))
- if bytes is False:
- out.write ("\n".join (map (from_latin1, lines)))
- out.write ("\n")
- else:
+ if isinstance(out, (io.RawIOBase, io.BufferedIOBase)):
out.write (b"\n".join (lines))
out.write (b"\n")
+ else: # either subclass of io.TextIOBase or unknown
+ out.write ("\n".join (map (from_latin1, lines)))
+ out.write ("\n")
def dump_cnf_bytes (root, renumber=False):
"""
cnf = cnf_root(root)
out = io.BytesIO()
- output_cnf(root, out, renumber=renumber, bytes=True)
+ output_cnf(root, out, renumber=renumber)
res = out.getvalue()
out.close()
return res
"""
cnf = cnf_root(root)
out = io.BytesIO()
- output_cnf(root, out, renumber=renumber, bytes=False)
+ output_cnf(root, out, renumber=renumber)
res = out.getvalue()
out.close()
return res
def print_cnf(root, out=None, renumber=False):
if root is not None:
- output_cnf(root, out or sys.stdout, renumber=renumber, bytes=False)
+ output_cnf(root, out or sys.stdout, renumber=renumber)
def write_cnf(*argv, **kw_argv):