Skip to content

Commit

Permalink
read Enactment groups as tuple, not list
Browse files Browse the repository at this point in the history
  • Loading branch information
mscarey committed Aug 10, 2019
1 parent 1005784 commit 621e3d6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
18 changes: 8 additions & 10 deletions authorityspoke/io/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def read_enactments(
mentioned: Optional[TextLinkDict] = None,
regime: Optional[Regime] = None,
report_mentioned: bool = False,
) -> Union[List[Enactment], Tuple[List[Enactment], TextLinkDict]]:
) -> Union[Tuple[Enactment, ...], Tuple[Tuple[Enactment, ...], TextLinkDict]]:
r"""
Create a new :class:`Enactment` object using imported JSON data.
Expand Down Expand Up @@ -136,7 +136,7 @@ def read_enactments(
)
created_list.append(created)
mentioned = mentioned or defaultdict(list)
return (created_list, mentioned) if report_mentioned else created_list
return (tuple(created_list), mentioned) if report_mentioned else tuple(created_list)


def read_fact(
Expand Down Expand Up @@ -189,18 +189,16 @@ def add_content_references(
sorted_mentioned = sorted(
mentioned.keys(), key=lambda x: len(x.name) if x.name else 0, reverse=True
)
context_with_indices: List[List[Union[Factor, int]]] = []
context_with_indices: Dict[Factor, int] = {}
for factor in sorted_mentioned:
if factor.name and factor.name in content and factor.name != content:
factor_index = content.find(factor.name)
for pair in context_with_indices:
if pair[1] > factor_index:
pair[1] -= len(factor.name) - len(placeholder)
context_with_indices.append([factor, factor_index])
for named_factor in context_with_indices:
if context_with_indices[named_factor] > factor_index:
context_with_indices[named_factor] -= len(factor.name) - len(placeholder)
context_with_indices[factor] = factor_index
content = content.replace(factor.name, placeholder)
context_factors = [
k[0] for k in sorted(context_with_indices, key=lambda k: k[1])
]
context_factors = sorted(context_with_indices, key=context_with_indices.get)
return content, context_factors

# TODO: inherit the later part of this function from Factor
Expand Down
6 changes: 3 additions & 3 deletions authorityspoke/procedures.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def factors_all(self) -> List[Factor]:
return [*self.outputs, *inputs, *despite]

@property
def generic_factors(self) -> List[Factor]:
def generic_factors(self) -> Tuple[Factor]:
r"""
:class:`.Factor`\s that can be replaced without changing ``self``\s meaning.
Expand All @@ -203,8 +203,8 @@ def generic_factors(self) -> List[Factor]:
with :meth:`.Factor.means` or :meth:`.Factor.__ge__`.
"""
if self.generic:
return [self]
return list(
return (self)
return tuple(
{
generic: None
for factor in self.factors_all
Expand Down

0 comments on commit 621e3d6

Please sign in to comment.