Skip to content

Commit

Permalink
fix #213, remove obsolete OrderedDict reference
Browse files Browse the repository at this point in the history
  • Loading branch information
c0fec0de committed Oct 11, 2023
1 parent 0d9ac33 commit 351aab5
Showing 1 changed file with 1 addition and 16 deletions.
17 changes: 1 addition & 16 deletions anytree/exporter/dictexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,12 @@ class DictExporter:
>>> s1 = AnyNode(a="sub1", parent=root)
>>> exporter = DictExporter()
>>> pprint(exporter.export(root)) # order within dictionary might vary!
>>> pprint(exporter.export(root))
{'a': 'root',
'children': [{'a': 'sub0',
'children': [{'a': 'sub0A', 'b': 'foo'}, {'a': 'sub0B'}]},
{'a': 'sub1'}]}
Pythons dictionary `dict` does not preserve order.
:any:`collections.OrderedDict` does.
In this case attributes can be ordered via `attriter`.
>>> from collections import OrderedDict
>>> exporter = DictExporter(dictcls=OrderedDict, attriter=sorted)
>>> pprint(exporter.export(root))
OrderedDict([('a', 'root'),
('children',
[OrderedDict([('a', 'sub0'),
('children',
[OrderedDict([('a', 'sub0A'), ('b', 'foo')]),
OrderedDict([('a', 'sub0B')])])]),
OrderedDict([('a', 'sub1')])])])
The attribute iterator `attriter` may be used for filtering too.
For example, just dump attributes named `a`:
Expand Down

0 comments on commit 351aab5

Please sign in to comment.