Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions graphtage/bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

class Infinity:
"""A class for representing infinite values. This is primarily used for unbounded ranges."""

__slots__ = ('_positive',)

def __init__(self, positive=True):
self._positive = positive

Expand Down Expand Up @@ -113,6 +116,8 @@ def __str__(self):

class Range:
"""An integer range."""
__slots__ = ('lower_bound', 'upper_bound')

def __init__(self, lower_bound: RangeValue = NEGATIVE_INFINITY, upper_bound: RangeValue = POSITIVE_INFINITY):
"""Constructs a range.

Expand Down Expand Up @@ -264,6 +269,9 @@ def wrapper(self: Bounded, *args, **kwargs):

class ConstantBound(Bounded):
"""An object with constant bounds."""

__slots__ = ('_range',)

def __init__(self, value: RangeValue):
"""Initializes the constant bounded object.

Expand All @@ -289,6 +297,9 @@ class BoundedComparator:
definitive or sufficiently distinct to differentiate them from another object to which it is being compared.

"""

__slots__ = ('bounded',)

def __init__(self, bounded: Bounded):
"""Initializes this bounded comparator.

Expand Down
24 changes: 24 additions & 0 deletions graphtage/edits.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
class AbstractEdit(Debuggable, Edit, ABC):
"""Abstract base class for the :class:`Edit` protocol."""

__slots__ = (
'from_node', 'to_node', '_constant_cost', '_cost_upper_bound',
'_valid', 'initial_bounds', '_checking_bounds'
)

def __init__(self,
from_node: TreeNode,
to_node: TreeNode = None,
Expand Down Expand Up @@ -141,6 +146,9 @@ def bounds(self) -> Range:

class ConstantCostEdit(AbstractEdit, ABC):
"""An edit whose definitive cost is known at the time of construction."""

__slots__ = ()

def __init__(
self,
from_node: TreeNode,
Expand Down Expand Up @@ -170,6 +178,8 @@ def tighten_bounds(self) -> bool:
class AbstractCompoundEdit(AbstractEdit, CompoundEdit, ABC):
"""Abstract base class implementing the :class:`CompoundEdit` protocol."""

__slots__ = ()

@abstractmethod
def edits(self) -> Iterator[Edit]:
raise NotImplementedError()
Expand Down Expand Up @@ -207,6 +217,8 @@ class PossibleEdits(AbstractCompoundEdit):

"""

__slots__ = ('_search',)

def __init__(
self,
from_node: TreeNode,
Expand Down Expand Up @@ -270,6 +282,8 @@ def bounds(self) -> Range:
class Match(ConstantCostEdit):
"""A constant cost edit specifying that one node should be matched to another."""

__slots__ = ()

def __init__(self, match_from: TreeNode, match_to: TreeNode, cost: int):
super().__init__(
from_node=match_from,
Expand Down Expand Up @@ -302,6 +316,8 @@ def __repr__(self):
class Replace(ConstantCostEdit):
"""A constant cost edit specifying that one node should be replaced with another."""

__slots__ = ()

def __init__(self, to_replace: TreeNode, replace_with: TreeNode):
cost = max(to_replace.total_size, replace_with.total_size) + 1
super().__init__(
Expand All @@ -328,6 +344,8 @@ def __repr__(self):
class Remove(ConstantCostEdit):
"""A constant cost edit specifying that a node should be removed from a container."""

__slots__ = ()

REMOVE_STRING: str = '~~'
"""The string used to denote a removal if ANSI color is disabled."""

Expand Down Expand Up @@ -361,6 +379,8 @@ def __repr__(self):
class Insert(ConstantCostEdit):
"""A constant cost edit specifying that a node should be added to a container."""

__slots__ = ()

INSERT_STRING: str = '++'
"""The string used to denote an insertion if ANSI color is disabled."""

Expand Down Expand Up @@ -403,6 +423,8 @@ def __repr__(self):
class EditCollection(AbstractCompoundEdit, Generic[C]):
"""An edit comprised of one or more sub-edits."""

__slots__ = ('_edit_iter', '_sub_edits', '_cost', 'explode_edits', '_add')

def __init__(
self,
from_node: TreeNode,
Expand Down Expand Up @@ -531,6 +553,8 @@ def __repr__(self):
class EditSequence(EditCollection[List]):
"""An :class:`EditCollection` using a :class:`list` as the underlying container."""

__slots__ = ()

def __init__(
self,
from_node: TreeNode,
Expand Down
7 changes: 7 additions & 0 deletions graphtage/graphtage.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def __str__(self):
class KeyValuePairEdit(AbstractCompoundEdit):
"""An edit type for two key/value pairs"""

__slots__ = ('key_edit', 'value_edit')

def __init__(
self,
from_kvp: 'KeyValuePairNode',
Expand Down Expand Up @@ -535,6 +537,9 @@ def __iter__(self) -> Iterator[KeyValuePairNode]:

class FixedKeyDictNodeEdit(SequenceEdit, EditCollection[List]):
"""The edit type returned by :class:`FixedKeyDictNode`."""

__slots__ = ()

def __init__(
self,
from_node: 'FixedKeyDictNode',
Expand Down Expand Up @@ -645,6 +650,8 @@ def __iter__(self) -> Iterator[KeyValuePairNode]:
class StringEdit(AbstractEdit):
"""An edit returned from a :class:`StringNode`"""

__slots__ = ('edit_distance',)

def __init__(
self,
from_node: 'StringNode',
Expand Down
7 changes: 7 additions & 0 deletions graphtage/levenshtein.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ class EditDistance(SequenceEdit):
bound.

"""

__slots__ = (
'penalty', 'shared_prefix', 'reversed_shared_suffix', 'from_seq', 'to_seq',
'edit_matrix', 'path_costs', 'costs', '_fringe_row', '_fringe_col',
'_last_fringe', '_EditDistance__edits'
)

def __init__(
self,
from_node: TreeNode,
Expand Down
3 changes: 3 additions & 0 deletions graphtage/multiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class MultiSetEdit(SequenceEdit):
the elements of one collection to the elements of the other.

"""

__slots__ = ('_matched_kvp_edits', 'to_insert', 'to_remove', '_edits', '_matcher')

def __init__(
self,
from_node: SequenceNode,
Expand Down
6 changes: 6 additions & 0 deletions graphtage/sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

class SequenceEdit(AbstractCompoundEdit, ABC):
"""An edit type for sequence nodes."""

__slots__ = ()

def __init__(
self,
from_node: 'SequenceNode',
Expand Down Expand Up @@ -60,6 +63,9 @@ def print(self, formatter: GraphtageFormatter, printer: Printer):

class FixedLengthSequenceEdit(SequenceEdit):
"""An edit for sequences that does not consider interleaving."""

__slots__ = ('_sub_edits', 'to_remove', 'to_insert')

def __init__(
self,
from_node: 'SequenceNode',
Expand Down
Loading