Skip to content

Commit

Permalink
Release 0.1.3
Browse files Browse the repository at this point in the history
- Fixed #73 again : Ordered Nested Dict in place of NestedDict
  • Loading branch information
PonteIneptique committed Apr 4, 2016
1 parent 886a223 commit e5fe59d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion MyCapytain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"""

__name__ = "MyCapytain"
__version__ = "0.1.2"
__version__ = "0.1.3"
__all__ = ["common", "endpoints", "resources"]
17 changes: 15 additions & 2 deletions MyCapytain/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from __future__ import unicode_literals
from functools import reduce

from collections import defaultdict
from collections import OrderedDict
from lxml import etree
from io import IOBase, StringIO
from past.builtins import basestring
Expand Down Expand Up @@ -253,7 +253,20 @@ def passageLoop(parent, new_tree, xpath1, xpath2=None, preceding_siblings=False,
return new_tree


nested_dictionary = lambda: defaultdict(nested_dictionary)
class OrderedDefaultDict(OrderedDict):
def __init__(self, default_factory=None, *args, **kwargs):
super(OrderedDefaultDict, self).__init__(*args, **kwargs)
self.default_factory = default_factory

def __missing__(self, key):
if self.default_factory is None:
raise KeyError(key)
val = self[key] = self.default_factory()
return val


def nested_ordered_dictionary():
return OrderedDefaultDict(nested_ordered_dictionary)


def nested_get(dictionary, keys):
Expand Down
4 changes: 2 additions & 2 deletions MyCapytain/resources/texts/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from MyCapytain.errors import DuplicateReference, RefsDeclError
from MyCapytain.common.utils import xmlparser, NS, copyNode, passageLoop, normalizeXpath, normalize, \
nested_set, nested_get, nested_dictionary
nested_set, nested_ordered_dictionary
from MyCapytain.common.reference import URN, Citation, Reference
from MyCapytain.resources.proto import text
from MyCapytain.errors import InvalidSiblingRequest
Expand Down Expand Up @@ -116,7 +116,7 @@ def nested_dict(self, exclude=None):
:returns: Dictionary
"""
reffs = self.getValidReff(level=len(self.citation))
text = nested_dictionary()
text = nested_ordered_dictionary()
for reff in reffs:
_r = reff.split(".")
nested_set(text, _r, self.getPassage(_r, hypercontext=False).text(exclude=exclude))
Expand Down
4 changes: 4 additions & 0 deletions tests/resources/texts/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ def test_nested_dict(self):
"Check that different fist level works as well")
self.assertEqual(nested["1"]["3"]["8"], "Ibis ab excusso missus in astra sago. ",
"Check that notes are removed ")
self.assertEqual(
[list(nested.keys()), list(nested["1"].keys())[:3], list(nested["2"]["pr"].keys())[:3]],
[["1", "2"], ["pr", "1", "2"], ["sa", "1", "2"]],
"Ensure that text keeps its order")

def test_warning(self):
with open("tests/testing_data/texts/duplicate_references.xml") as xml:
Expand Down

0 comments on commit e5fe59d

Please sign in to comment.