Skip to content

Commit

Permalink
Document xml_fromstring custom function with an example
Browse files Browse the repository at this point in the history
  • Loading branch information
sanand0 committed Nov 4, 2016
1 parent 8916aad commit aa3f35e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ in the Yahoo method). Override this behaviour using ``xml_fromstring``::
>>> bf_str = BadgerFish(xml_fromstring=repr) # Custom string parser
'{"x": {"$": "\'1\'"}}'

``xml_fromstring`` can be any custom function that takes a string and returns a
value. In the example below, only the integer ``1`` is converted to an integer.
Everything else is retained as a float::

>>> def convert_only_int(val):
... return int(val) if val.isdigit() else val
>>> bf_int = BadgerFish(xml_fromstring=convert_only_int)
>>> dumps(bf_int.data(fromstring('<p><x>1</x><y>2.5</y><z>NaN</z></p>')))
'{"p": {"x": {"$": 1}, "y": {"$": "2.5"}, "z": {"$": "NaN"}}}'


Conventions
-----------
Expand Down
16 changes: 8 additions & 8 deletions xmljson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, xml_fromstring=True, xml_tostring=True, element=None, dict_ty

@staticmethod
def _tostring(value):
'Convert value to XML compatible string'
'''Convert value to XML compatible string'''
if value is True:
value = 'true'
elif value is False:
Expand All @@ -55,7 +55,7 @@ def _tostring(value):

@staticmethod
def _fromstring(value):
'Convert XML string value to None, boolean, int or float'
'''Convert XML string value to None, boolean, int or float'''
if not value:
return None
std_value = value.strip().lower()
Expand All @@ -74,7 +74,7 @@ def _fromstring(value):
return value

def etree(self, data, root=None):
'Convert data structure into a list of etree.Element'
'''Convert data structure into a list of etree.Element'''
result = self.list() if root is None else root
if isinstance(data, (self.dict, dict)):
for key, value in data.items():
Expand Down Expand Up @@ -120,7 +120,7 @@ def etree(self, data, root=None):
return result

def data(self, root):
'Convert etree.Element into a dictionary'
'''Convert etree.Element into a dictionary'''
value = self.dict()
children = [node for node in root if isinstance(node.tag, basestring)]
for attr, attrval in root.attrib.items():
Expand All @@ -144,26 +144,26 @@ def data(self, root):


class BadgerFish(XMLData):
'Converts between XML and data using the BadgerFish convention'
'''Converts between XML and data using the BadgerFish convention'''
def __init__(self, **kwargs):
super(BadgerFish, self).__init__(attr_prefix='@', text_content='$', **kwargs)


class GData(XMLData):
'Converts between XML and data using the GData convention'
'''Converts between XML and data using the GData convention'''
def __init__(self, **kwargs):
super(GData, self).__init__(text_content='$t', **kwargs)


class Yahoo(XMLData):
'Converts between XML and data using the Yahoo convention'
'''Converts between XML and data using the Yahoo convention'''
def __init__(self, **kwargs):
kwargs.setdefault('xml_fromstring', False)
super(Yahoo, self).__init__(text_content='content', simple_text=True, **kwargs)


class Parker(XMLData):
'Converts between XML and data using the Parker convention'
'''Converts between XML and data using the Parker convention'''
def __init__(self, **kwargs):
super(Parker, self).__init__(**kwargs)

Expand Down

0 comments on commit aa3f35e

Please sign in to comment.