Browse Source

Make xmlast tostring easier to debug

Update samples to work with and without lxml (hopefully)
master
Jason Morgan 2 weeks ago
parent
commit
2be9a3e519
3 changed files with 5 additions and 6 deletions
  1. +3
    -2
      pypeg2/xmlast.py
  2. +2
    -2
      samples/sample1.py
  3. +0
    -2
      samples/sample2.py

+ 3
- 2
pypeg2/xmlast.py View File

@ -120,14 +120,15 @@ def thing2xml(thing, pretty=False, object_names=False):
tree = create_tree(thing, None, object_names)
try:
if lxml:
return etree.tostring(tree, pretty_print=pretty)
txt = etree.tostring(tree, pretty_print=pretty)
except NameError:
if __debug__:
if pretty:
warnings.warn("lxml is needed for pretty printing",
ImportWarning)
etree.indent(tree)
return etree.tostring(tree)
txt = etree.tostring(tree)
return txt
def create_thing(element, symbol_table):


+ 2
- 2
samples/sample1.py View File

@ -58,8 +58,9 @@ pyPEG contains an XML backend, too:
>>> del f[2]
>>> from pypeg2.xmlast import thing2xml
>>> import sys
>>> xml = thing2xml(f, pretty=True)
>>> print(xml.decode()) # doctest: +ELLIPSIS
>>> print(xml.decode(), end="") # doctest: +ELLIPSIS
<Function typing="int" name="f">
<Parameters>
<Parameter typing="int" name="a".../>
@ -68,7 +69,6 @@ pyPEG contains an XML backend, too:
<Instruction>do_this</Instruction>
<Instruction>do_that</Instruction>
</Function>
<BLANKLINE>
The XML backend can read XML text and create things:


+ 0
- 2
samples/sample2.py View File

@ -65,7 +65,6 @@ pyPEG contains an XML backend, too:
</Section>
<Section name="Number 3".../>
</IniFile>
<BLANKLINE>
In this sample the tree contains named objects only. Then we can output object
names as tag names. Spaces in names will be translated into underscores.
@ -82,7 +81,6 @@ names as tag names. Spaces in names will be translated into underscores.
</Number_2>
<Number_3.../>
</IniFile>
<BLANKLINE>
"""
from __future__ import unicode_literals, print_function


Loading…
Cancel
Save