bronx.fancies.dump¶
Data dumper… The (challenging) idea is to be able to dump any object to many different formats.
It is mostly used in objects’ docstring within the footprints package.
- note:
Dumper objects are managed using
bronx.patterns.getbytag; consequently, they are associated with atagand can be re-used.
Example:
>>> class Foo(object):
... a = 1
...
... def __str__(self):
... return str(self.a)
>>> somelist = [dict(akey=Foo(),bkey=['item1', 'item2'],
... ckey=dict(other=1, sutff=2)), 'a_string',
... ['another', 'list', tuple([1, 2, 3])]]
# A Txt Dumper object can be created directly
>>> tdumper = TxtDumper()
>>> print(tdumper.tag)
default
# Or using get(). Since getbytag is used, the same objet is dumped
>>> tdumper_bis = get()
>>> tdumper_bis is tdumper
True
>>> print(tdumper.cleandump(somelist))
[dict(
akey = ...Foo::1,
bkey = ['item1', 'item2'],
ckey = dict(
other = 1,
sutff = 2,
),
), 'a_string', ['another', 'list', (1, 2, 3)]]
# The Jsonable Dumper will produce something that can safely be dumped to
# a JSON File
>>> jdumper = JsonableDumper(tag='testdumper')
>>> jdumper.cleandump(somelist)
[{'akey': ...'...Foo::1',
'bkey': [...'item1', ...'item2'],
'ckey': {'sutff': 2, 'other': 1}}, ...
'a_string', [...'another', ...'list', [1, 2, 3]]]
# The XML Dumper returns a xml.dom's Document object
>>> xdumper = XmlDomDumper()
>>> xd = xdumper.cleandump(somelist, 'testxml')
>>> xd
<xml.dom.minidom.Document instance at 0x...>
>>> print(xd.toprettyxml(indent=' ', encoding='utf-8'))
<?xml version="1.0" encoding="utf-8"?>
<testxml>
<generic_item>
<akey>
<generic_object>
<overview>1</overview>
<type>...Foo</type>
</generic_object>
</akey>
<bkey>item1</bkey>
<bkey>item2</bkey>
<ckey>
<other>1</other>
<sutff>2</sutff>
</ckey>
</generic_item>
<generic_item>a_string</generic_item>
<generic_item>
<generic_item>another</generic_item>
<generic_item>list</generic_item>
<generic_item>
<generic_item>1</generic_item>
<generic_item>2</generic_item>
<generic_item>3</generic_item>
</generic_item>
</generic_item>
</testxml>
# Interface functions can be used to obtain quickly a text dump
>>> print(fulldump(somelist))
[dict(
akey = ...Foo::1,
bkey = ['item1', 'item2'],
ckey = dict(
other = 1,
sutff = 2,
),
), 'a_string', ['another', 'list', (1, 2, 3)]]
Functions¶
- bronx.fancies.dump.fulldump(obj, startpos=6, reset=True)[source]¶
Entry point: Return a text dump of the provided
obj.- Parameters:
obj – The object that will be dumped
startpos (int) – Number of blank characters that will be added to the first line of the text dump
reset (bool) – Reset the TxtDumper object’s cache before dumping
obj
- bronx.fancies.dump.is_an_instance(val)[source]¶
Detect if a given object is an instance (as opposed to being a class).
- Parameters:
val – The object to analyse
Classes¶
- class bronx.fancies.dump.JsonableDumper(*args, **kw)[source]¶
Bases:
_AbstractDumperReturn a dump consisting of a pure mix of dictionaries and lists.
The resulting dump can be serialised using the standard pickle or json module.
No arguments.
- class bronx.fancies.dump.OneLineTxtDumper(*args, **kw)[source]¶
Bases:
TxtDumperDump single-line text representation of almost any object…
No arguments.
- class bronx.fancies.dump.TxtDumper(*args, **kw)[source]¶
Bases:
_AbstractDumperDump a text representation of almost any object…
No arguments.
- class bronx.fancies.dump.XmlDomDumper(*args, **kw)[source]¶
Bases:
JsonableDumperReturn a dump as an XML DOM object (instance of
xml.minidom.Document).- Parameters:
named_nodes (tuple) – List of XML nodes that support a name attribute. For such nodes, a dictionary will be converted as follows :
attr=dict(toto="BlaBla",titi="BlaBla")becomes<attr name="toto">BlaBla</attr><attr name="titi">BlaBla</attr>
- cleandump(obj, root, rootattr=None, level=0, nextline=True)¶
Call this method to dump
obj(or at least try to…).- Parameters:
obj – The object that will be dumped
root (str) – Name of the XML root node
rootattr (dict) – dictionary of attributes that will be added to the XML root element.
level (int) – For internal use only.
nextline (bool) – For internal use only.
- dump(obj, root, rootattr=None, level=0, nextline=True)[source]¶
Call this method to dump
obj(or at least try to…).- Parameters:
obj – The object that will be dumped
root (str) – Name of the XML root node
rootattr (dict) – dictionary of attributes that will be added to the XML root element.
level (int) – For internal use only.
nextline (bool) – For internal use only.