Skip to content

Commit

Permalink
Release 0.1.2
Browse files Browse the repository at this point in the history
Release 0.1.2
- Fixed #73 :  Add a nested_dict method to local.Text
  • Loading branch information
PonteIneptique committed Apr 4, 2016
1 parent 8523684 commit 886a223
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 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.1"
__version__ = "0.1.2"
__all__ = ["common", "endpoints", "resources"]
27 changes: 26 additions & 1 deletion MyCapytain/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"""
from __future__ import unicode_literals
from functools import reduce


from collections import defaultdict
from lxml import etree
from io import IOBase, StringIO
from past.builtins import basestring
Expand Down Expand Up @@ -250,3 +251,27 @@ def passageLoop(parent, new_tree, xpath1, xpath2=None, preceding_siblings=False,
passageLoop(result_2, child_2, queue_2, None, preceding_siblings=True)

return new_tree


nested_dictionary = lambda: defaultdict(nested_dictionary)


def nested_get(dictionary, keys):
""" Get value in dictionary for dictionary[keys[0]][keys[1]][keys[..n]]
:param dictionary: An input dictionary
:param keys: Keys where to store data
:return:
"""
return reduce(lambda d, k: d[k], keys, dictionary)


def nested_set(dictionary, keys, value):
""" Set value in dictionary for dictionary[keys[0]][keys[1]][keys[..n]]
:param dictionary: An input dictionary
:param keys: Keys where to store data
:param value: Value to set at keys** target
:return: None
"""
nested_get(dictionary, keys[:-1])[keys[-1]] = value
18 changes: 17 additions & 1 deletion MyCapytain/resources/texts/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import warnings

from MyCapytain.errors import DuplicateReference, RefsDeclError
from MyCapytain.common.utils import xmlparser, NS, copyNode, passageLoop, normalizeXpath, normalize
from MyCapytain.common.utils import xmlparser, NS, copyNode, passageLoop, normalizeXpath, normalize, \
nested_set, nested_get, nested_dictionary
from MyCapytain.common.reference import URN, Citation, Reference
from MyCapytain.resources.proto import text
from MyCapytain.errors import InvalidSiblingRequest
Expand Down Expand Up @@ -106,6 +107,21 @@ def citation(self, value):
child=value.child
)

def nested_dict(self, exclude=None):
""" Nested Dict Representation of the text passages
:param exclude: Remove some nodes from text according to `MyCapytain.resources.texts.tei.Passage.text`
:type exclude: List
:rtype: dict
:returns: Dictionary
"""
reffs = self.getValidReff(level=len(self.citation))
text = nested_dictionary()
for reff in reffs:
_r = reff.split(".")
nested_set(text, _r, self.getPassage(_r, hypercontext=False).text(exclude=exclude))
return text

def getPassage(self, reference, hypercontext=True):
""" Finds a passage in the current text
Expand Down
12 changes: 12 additions & 0 deletions tests/resources/texts/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ def testValidReffs(self):
self.assertEqual(
self.TEI.getValidReff(reference=MyCapytain.common.reference.Reference("2.hellno"), level=3), [])

def test_nested_dict(self):
""" Check the nested dict export of a local.Text object """
nested = self.TEI.nested_dict(exclude=["tei:note"])
self.assertEqual(nested["1"]["pr"]["1"], "Spero me secutum in libellis meis tale temperamen-",
"Check that dictionary path is well done")
self.assertEqual(nested["1"]["12"]["1"], "Itur ad Herculeas gelidi qua Tiburis arces ",
"Check that dictionary path works on more than one passage")
self.assertEqual(nested["2"]["pr"]["1"], "'Quid nobis' inquis 'cum epistula? parum enim tibi ",
"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 ")

def test_warning(self):
with open("tests/testing_data/texts/duplicate_references.xml") as xml:
text = MyCapytain.resources.texts.local.Text(resource=xml)
Expand Down

0 comments on commit 886a223

Please sign in to comment.