Skip to content

Commit

Permalink
Add a matches dict into Matches.to_dict return value
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Oct 7, 2015
1 parent 8de859c commit 93c29d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ It has the following additional methods and properties on it.
Convert to a standard dict, with ``Match.name`` as key and ``Match.value`` as value.
If several distinct values are found for the same name, value will be a list.

It's a subclass of dict, that contains an additional ``matches`` property which is a dict with ``Match.name`` as key
and list of ``Match`` objects as value.

- ``markers``

A custom ``Matches`` sequences specialized for ``markers`` matches (see below)
Expand Down
12 changes: 11 additions & 1 deletion rebulk/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
from .loose import ensure_list, filter_index


class MatchesDict(dict):
"""
A custom dict with matches property.
"""
def __init__(self):
super(MatchesDict, self).__init__()
self.matches = defaultdict(list)


class _BaseMatches(MutableSequence):
"""
A custom list[Match] that automatically maintains name, tag, start and end lookup structures.
Expand Down Expand Up @@ -167,9 +176,10 @@ def to_dict(self, details=False):
:return:
:rtype: dict
"""
ret = {}
ret = MatchesDict()
for match in self:
value = match if details else match.value
ret.matches[match.name].append(match)
if match.name in ret.keys():
if not isinstance(ret[match.name], list):
if ret[match.name] == value:
Expand Down

0 comments on commit 93c29d0

Please sign in to comment.