Skip to content

Commit

Permalink
Code enhancement + UnitTest
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Dec 7, 2016
1 parent 1128bf3 commit 523d4e1
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 91 deletions.
52 changes: 26 additions & 26 deletions MyCapytain/common/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Metadatum(object):
:type children: List
:Example:
>>> a = Metadatum(name="label", [("lat", "Amores"), ("fre", "Les Amours")])
>>> a = Metadatum("label", [("lat", "Amores"), ("fre", "Les Amours")])
>>> print(a["lat"]) # == "Amores"
.. automethod:: __getitem__
Expand Down Expand Up @@ -56,7 +56,7 @@ def __getitem__(self, key):
:raises: KeyError if key is unknown (when using Int based key or when default is not set)
:Example:
>>> a = Metadatum(name="label", [("lat", "Amores"), ("fre", "Les Amours")])
>>> a = Metadatum("label", [("lat", "Amores"), ("fre", "Les Amours")])
>>> print(a["lat"]) # Amores
>>> print(a[("lat", "fre")]) # Amores, Les Amours
>>> print(a[0]) # Amores
Expand Down Expand Up @@ -129,7 +129,7 @@ def setDefault(self, key):
:raises: `ValueError` If key is not registered
:Example:
>>> a = Metadatum(name="label", [("lat", "Amores"), ("fre", "Les Amours")])
>>> a = Metadatum("label", [("lat", "Amores"), ("fre", "Les Amours")])
>>> a.setDefault("fre")
>>> print(a["eng"]) # == "Les Amours"
Expand All @@ -144,7 +144,7 @@ def __iter__(self):
""" Iter method of Metadatum
:Example:
>>> a = Metadata(name="label", [("lat", "Amores"), ("fre", "Les Amours")])
>>> a = Metadata("label", [("lat", "Amores"), ("fre", "Les Amours")])
>>> for key, value in a:
>>> print(key, value) # Print ("lat", "Amores") and then ("fre", "Les Amours")
"""
Expand All @@ -160,7 +160,7 @@ def __len__(self):
:rtype: int
:Example:
>>> a = Metadata(name="label", [("lat", "Amores"), ("fre", "Les Amours")])
>>> a = Metadata("label", [("lat", "Amores"), ("fre", "Les Amours")])
>>> len(a) == 2
"""
return len(self.children)
Expand All @@ -178,8 +178,8 @@ def __getstate__(self):

def __setstate__(self, dic):
""" Unpickling method
:param value:
:return:
:param dic: Dictionary to use to set up the object
:return: New generated object
"""
self.name = dic["name"]
self.children = OrderedDict(dic["langs"])
Expand All @@ -191,8 +191,8 @@ class Metadata(object):
"""
A metadatum aggregation object provided to centralize metadata
:param key: A metadata field name
:type key: List.<text_type>
:param keys: A metadata field names list
:type keys: [text_type]
:ivar metadata: Dictionary of metadatum
Expand All @@ -206,7 +206,7 @@ def __init__(self, keys=None):
""" Initiate the object
"""
self.metadata = defaultdict(Metadatum)
self.__keys = []
self.__keys__ = []

if keys is not None:
for key in keys:
Expand All @@ -222,8 +222,8 @@ def __getitem__(self, key):
:Example:
>>> a = Metadata()
>>> m1 = Metadatum(name="title", [("lat", "Amores"), ("fre", "Les Amours")])
>>> m2 = Metadatum(name="author", [("lat", "Ovidius"), ("fre", "Ovide")])
>>> m1 = Metadatum("title", [("lat", "Amores"), ("fre", "Les Amours")])
>>> m2 = Metadatum("author", [("lat", "Ovidius"), ("fre", "Ovide")])
>>> a[("title", "author")] = (m1, m2)
>>> a["title"] == m1
Expand All @@ -233,10 +233,10 @@ def __getitem__(self, key):
"""
if isinstance(key, int):
if key + 1 > len(self.__keys):
if key + 1 > len(self.__keys__):
raise KeyError()
else:
key = self.__keys[key]
key = self.__keys__[key]
elif isinstance(key, tuple):
return tuple([self[k] for k in key])

Expand All @@ -260,12 +260,12 @@ def __setitem__(self, key, value):
:Example:
>>> a = Metadata()
>>> a["title"] = Metadatum(name="title", [("lat", "Amores"), ("fre", "Les Amours")])
>>> a["title"] = Metadatum("title", [("lat", "Amores"), ("fre", "Les Amours")])
>>> print(a["title"]["lat"]) # Amores
>>> a[("title", "author")] = (
>>> Metadatum(name="title", [("lat", "Amores"), ("fre", "Les Amours")]),
>>> Metadatum(name="author", [("lat", "Ovidius"), ("fre", "Ovide")])
>>> Metadatum("title", [("lat", "Amores"), ("fre", "Les Amours")]),
>>> Metadatum("author", [("lat", "Ovidius"), ("fre", "Ovide")])
>>> )
>>> print(a["title"]["lat"], a["author"]["fre"]) # Amores, Ovide
Expand All @@ -284,8 +284,8 @@ def __setitem__(self, key, value):
elif isinstance(value, Metadatum):
self.metadata[key] = value

if key in self.metadata and key not in self.__keys:
self.__keys.append(key)
if key in self.metadata and key not in self.__keys__:
self.__keys__.append(key)

def __iter__(self):
""" Iter method of Metadata
Expand All @@ -296,7 +296,7 @@ def __iter__(self):
>>> print(key, value) # Print ("title", "<Metadatum object>") then ("desc", "<Metadatum object>")...
"""
i = 0
for key in self.__keys:
for key in self.__keys__:
yield (key, self.metadata[key])
i += 1

Expand All @@ -316,7 +316,7 @@ def __add__(self, other):
from copy import deepcopy
result = deepcopy(self)
for metadata_key, metadatum in other:
if metadata_key in self.__keys:
if metadata_key in self.__keys__:
for key, value in metadatum:
result[metadata_key][key] = value
else:
Expand All @@ -336,7 +336,7 @@ def __len__(self):
return len(
[
k
for k in self.__keys
for k in self.__keys__
if isinstance(self.metadata[k], Metadatum)
]
)
Expand All @@ -357,9 +357,9 @@ def __setstate__(self, dic):
"""
self.metadata = defaultdict(Metadatum)

self.__keys = []
self.__keys__ = []
for key, value in dic.items():
self.__keys.append(key)
self.__keys__.append(key)
self.metadata[key] = getattr(Metadatum(name=value["name"]), "__setstate__")(value)
return self

Expand All @@ -368,7 +368,7 @@ def keys(self):
:return: List of metadatum keys
"""
return self.__keys
return self.__keys__

def export(self, mime=Mimetypes.JSON.Std):
if mime == Mimetypes.JSON.Std:
Expand All @@ -390,7 +390,7 @@ def export(self, mime=Mimetypes.JSON.Std):
ns = RDF_PREFIX[ns]

for lang, value in metadatum:
if not lang in descs:
if lang not in descs:
descs[lang] = {"@language": lang}
descs[lang][ns+k] = value
return [value for value in descs.values()]
Expand Down
86 changes: 42 additions & 44 deletions MyCapytain/common/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
from lxml.etree import _Element
from MyCapytain.common.utils import NS

REFSDECL_SPLITTER = re.compile("/+[\*()|\sa-zA-Z0-9:\[\]@=\\\{\$'\"\.\s]+")
REFSDECL_REPLACER = re.compile("\$[0-9]+")
SUBREFERENCE = re.compile("(\w*)\[{0,1}([0-9]*)\]{0,1}", re.UNICODE)
REFERENCE_REPLACER = re.compile("(@[a-zA-Z0-9:]+){1}(=){1}([\\\$'\"?0-9]{3,6})")
REFSDECL_SPLITTER = re.compile(r"/+[*()|\sa-zA-Z0-9:\[\]@=\\{$'\".\s]+")
REFSDECL_REPLACER = re.compile(r"\$[0-9]+")
SUBREFERENCE = re.compile(r"(\w*)\[?([0-9]*)\]?", re.UNICODE)
REFERENCE_REPLACER = re.compile(r"(@[a-zA-Z0-9:]+)(=)([\\$'\"?0-9]{3,6})")


def __childOrNone__(liste):
""" Used to parse resources in Citation
:param liste:
:return:
:param liste: List of item
:return: If there is > 1 element in the list, return the last one
"""
if len(liste) > 0:
return liste[-1]
Expand All @@ -32,22 +32,10 @@ def __childOrNone__(liste):


class Reference(object):
""" A reference object giving informations
""" A reference object giving information
:param reference: Passage Reference part of a Urn
:type reference: basestring
:ivar parent: Parent Reference
:type parent: Reference
:ivar highest: List representation of the range member which is the highest in the hierarchy (If equal, start is returned)
:type highest: Reference
:ivar start: First part of the range
:type start: Reference
:ivar end: Second part of the range
:type end: Reference
:ivar list: List representation of the range. Not available for range
:type list: list
:ivar subreference: Word and Word counter ("Achiles", 1) representing the subreference. Not available for range
:type subreference: (str, int)
:Example:
>>> a = Reference(reference="1.1@Achiles[1]-1.2@Zeus[1]")
Expand All @@ -62,7 +50,8 @@ class Reference(object):
>>> b == Reference("1.1") && b != a
.. note::
While Reference(...).subreference and .list are not available for range, Reference(..).start.subreference and Reference(..).end.subreference as well as .list are available
While Reference(...).subreference and .list are not available for range, Reference(..).start.subreference \
and Reference(..).end.subreference as well as .list are available
"""

def __init__(self, reference=""):
Expand Down Expand Up @@ -119,18 +108,21 @@ def highest(self):
return self.end
elif len(self.start):
return self.start
return self

@property
def start(self):
""" Quick access property for start list
:rtype: Reference
"""
if self.parsed[0][0] and len(self.parsed[0][0]):
return Reference(self.parsed[0][0])

@property
def end(self):
""" Quick access property for reference end list
:rtype: Reference
"""
if self.parsed[1][0] and len(self.parsed[1][0]):
return Reference(self.parsed[1][0])
Expand Down Expand Up @@ -282,18 +274,6 @@ class URN(object):
:param urn: A CTS URN
:type urn: str
:ivar urn_namespace: Namespace of the URN
:type urn_namespace: str
:ivar namespace: CTS Namespace
:type namespace: str
:ivar textgroup: CTS Textgroup
:type textgroup: str
:ivar work: CTS Work
:type work: str
:ivar version: CTS Version
:type version: str
:ivar reference: CTS Reference
:type reference: Reference
:cvar NAMESPACE: Constant representing the URN until its namespace
:cvar TEXTGROUP: Constant representing the URN until its textgroup
:cvar WORK: Constant representing the URN until its work
Expand All @@ -317,7 +297,6 @@ class URN(object):
>>> len(a) == 5 # Reference is not counted to not induce count equivalencies with the optional version
>>> len(b) == 4
.. exclude-members:: all
.. automethod:: upTo
"""

Expand Down Expand Up @@ -814,7 +793,7 @@ def __iter__(self):

def __getitem__(self, item):
if not isinstance(item, int) or item > len(self)-1:
return KeyError
raise KeyError("Citation index is too big")
return [x for x in self][item]

def __len__(self):
Expand Down Expand Up @@ -852,9 +831,8 @@ def fill(self, passage=None, xpath=None):
if xpath is True: # Then passage is a string or None
xpath = self.xpath

if passage is None:
replacement = r"\1"
elif isinstance(passage, text_type):
replacement = r"\1"
if isinstance(passage, text_type):
replacement = r"\1\2'" + passage + "'"

return REFERENCE_REPLACER.sub(replacement, xpath)
Expand Down Expand Up @@ -981,29 +959,49 @@ class NodeId(object):
:param depth: Depth of the node in the global hierarchy of the text tree
:type depth: int
"""
def __init__(self, identifier=None, children=None, parent=None, siblings=(None, None), depth=1):
def __init__(self, identifier=None, children=None, parent=None, siblings=(None, None), depth=None):
self.__children__ = children or []
self.__parent__ = parent
self.__prev__, self.__next__ = siblings
self.__prev__, self.__nextId__ = siblings
self.__identifier__ = identifier
self.__depth__ = depth

@property
def depth(self):
""" Depth of the node in the global hierarchy of the text tree
:rtype: [str]
:rtype: int
"""
return self.__depth__

@property
def childIds(self):
""" Siblings Node
""" Children Node
:rtype: [str]
"""
return self.__children__

@property
def firstId(self):
""" First child Node
:rtype: str
"""
if len(self.__children__) == 0:
return None
return self.__children__[0]

@property
def lastId(self):
""" Last child Node
:rtype: str
"""
if len(self.__children__) == 0:
return None
return self.__children__[-1]

@property
def parentId(self):
""" Parent Node
Expand All @@ -1018,7 +1016,7 @@ def siblingsId(self):
:rtype: (str, str)
"""
return self.__prev__, self.__next__
return self.__prev__, self.__nextId__

@property
def prevId(self):
Expand All @@ -1034,7 +1032,7 @@ def nextId(self):
:rtype: str
"""
return self.__next__
return self.__nextId__

@property
def id(self):
Expand Down

0 comments on commit 523d4e1

Please sign in to comment.