Skip to content

Commit

Permalink
move EnactmentIndex.insert_by_name to Mentionod
Browse files Browse the repository at this point in the history
  • Loading branch information
mscarey committed Oct 13, 2021
1 parent edb8b91 commit ec90ef6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
26 changes: 1 addition & 25 deletions authorityspoke/io/enactment_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,6 @@ def add_anchor_for_enactment(
anchors_for_selected_element.append(anchor)
self[enactment_name]["anchors"] = anchors_for_selected_element

def index_enactment(self, obj: RawEnactment) -> Union[str, RawEnactment]:
r"""
Update index of mentioned Factors with 'obj', if obj is named.
If there is already an entry in the mentioned index with the same name
as obj, the old entry won't be replaced. But if any additional text
anchors are present in the new obj, the anchors will be added.
If obj has a name, it will be collapsed to a name reference.
:param obj:
data from JSON to be loaded as a :class:`.Enactment`
"""
if obj.get("name"):
if obj["name"] in self:
if obj.get("anchors"):
for anchor in obj["anchors"]:
self.add_anchor_for_enactment(
enactment_name=obj["name"], anchor=anchor
)
else:
self.insert_by_name(obj)
obj = obj["name"]
return obj


def create_name_for_enactment(obj: RawEnactment) -> str:
"""Create unique name for unloaded Enactment data, for indexing."""
Expand Down Expand Up @@ -115,6 +91,6 @@ def collect_enactments(

if new_dict.get("enactment") or (new_dict.get("name") in mentioned.keys()):
new_dict = ensure_enactment_has_name(new_dict)
new_dict = mentioned.index_enactment(new_dict)
new_dict = mentioned.update_anchors_or_insert(new_dict)
obj = new_dict
return obj, mentioned
29 changes: 28 additions & 1 deletion authorityspoke/io/name_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from re import findall
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union

from legislice.enactments import RawEnactment
from nettlesome.predicates import StatementTemplate
from authorityspoke.facts import Exhibit, RawPredicate, RawFactor
from authorityspoke.holdings import RawHolding
Expand Down Expand Up @@ -63,6 +64,32 @@ def sorted_by_length(self) -> Mentioned:
def __repr__(self):
return f"{self.__class__.__name__}({repr(dict(self))})"

def update_anchors_or_insert(
self, obj: Union[str, RawEnactment, RawFactor]
) -> Union[str, RawEnactment, RawFactor]:
r"""
Update index of mentioned Factors with 'obj', if obj is named.
If there is already an entry in the mentioned index with the same name
as obj, the old entry won't be replaced. But if any additional text
anchors are present in the new obj, the anchors will be added.
If obj has a name, it will be collapsed to a name reference.
:param obj:
data from JSON to be loaded as a :class:`.Enactment`
"""
if obj.get("name"):
if obj["name"] in self:
if obj.get("anchors"):
for anchor in obj["anchors"]:
self.add_anchor_for_enactment(
enactment_name=obj["name"], anchor=anchor
)
else:
self.insert_by_name(obj)
obj = obj["name"]
return obj


def assign_name_from_content(obj: Dict) -> str:
r"""
Expand Down Expand Up @@ -204,7 +231,7 @@ def update_name_index_from_terms(terms: List[RawFactor], mentioned: Mentioned):
else:
factor_name = factor.get("name")
if factor_name and factor_name not in mentioned:
mentioned.insert_by_name(factor)
mentioned.update_anchors_or_insert(factor)
return mentioned.sorted_by_length()


Expand Down
4 changes: 2 additions & 2 deletions tests/io/test_enactment_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class TestIndexEnactments:
def test_index_section_with_name(self, section6d):
mentioned = EnactmentIndex()
section6d["name"] = "section6d"
mentioned.index_enactment(section6d)
mentioned.update_anchors_or_insert(section6d)
assert mentioned["section6d"]["start_date"] == "1935-04-01"
assert "EnactmentIndex({'section6d" in str(mentioned)

def test_index_key_error(self, section6d):
mentioned = EnactmentIndex()
section6d["name"] = "section6d"
mentioned.index_enactment(section6d)
mentioned.update_anchors_or_insert(section6d)
with pytest.raises(KeyError):
mentioned.get_by_name("not in index")

Expand Down

0 comments on commit ec90ef6

Please sign in to comment.