Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# coding=utf-8
from uuid import uuid4
from eventsourcing.domain.model.entity import EventSourcedEntity, mutableproperty
from eventsourcing.domain.model.events import publish
class SuffixTree(EventSourcedEntity):
"""A suffix tree for string matching. Uses Ukkonen's algorithm
for construction.
"""
class Created(EventSourcedEntity.Created):
pass
class AttributeChanged(EventSourcedEntity.AttributeChanged):
pass
class Discarded(EventSourcedEntity.Discarded):
pass
def __init__(self, string, case_insensitive=False, **kwargs):
"""
string
the string for which to construct a suffix tree
"""
super(SuffixTree, self).__init__(**kwargs)
self._string = string
self._case_insensitive = case_insensitive
self._N = len(string) - 1
node = register_new_node()
self._nodes = {
return -1
i += edge.length + 1
curr_node_id = edge.dest_node_id
return edge.first_char_index - len(substring) + ln
def has_substring(self, substring):
return self.find_substring(substring) != -1
class Node(EventSourcedEntity):
"""A node in the suffix tree.
"""
class Created(EventSourcedEntity.Created): pass
class AttributeChanged(EventSourcedEntity.AttributeChanged): pass
class Discarded(EventSourcedEntity.Discarded): pass
def __init__(self, *args, **kwargs):
super(Node, self).__init__(*args, **kwargs)
self._suffix_node_id = None
@mutableproperty
def suffix_node_id(self):
"""The id of a node with a matching suffix, representing a suffix link.
None indicates this node has no suffix link.
"""
return self._suffix_node_id
def __repr__(self):
None indicates this node has no suffix link.
"""
return self._suffix_node_id
def __repr__(self):
return "Node(suffix link: %d)" % self.suffix_node_id
class Edge(EventSourcedEntity):
"""An edge in the suffix tree.
"""
class Created(EventSourcedEntity.Created): pass
class AttributeChanged(EventSourcedEntity.AttributeChanged): pass
class Discarded(EventSourcedEntity.Discarded): pass
def __init__(self, label, first_char_index, last_char_index, source_node_id, dest_node_id, **kwargs):
super(Edge, self).__init__(**kwargs)
self._label = label
self._first_char_index = first_char_index
self._last_char_index = last_char_index
self._source_node_id = source_node_id
self._dest_node_id = dest_node_id
@mutableproperty
def label(self):
"""String part represented by this edge.
"""
return self._label
if not suffix.explicit():
edge_id = make_edge_id(suffix.source_node_id, self.string[suffix.first_char_index])
e = self.edges[edge_id]
if e.length <= suffix.length:
suffix.first_char_index += e.length + 1
suffix.source_node_id = e.dest_node_id
self._canonize_suffix(suffix)
class Node(EventSourcedEntity):
"""A node in the suffix tree.
"""
class Created(EventSourcedEntity.Created): pass
class AttributeChanged(EventSourcedEntity.AttributeChanged): pass
class Discarded(EventSourcedEntity.Discarded): pass
def __init__(self, suffix_node_id=None, *args, **kwargs):
super(Node, self).__init__(*args, **kwargs)
self._suffix_node_id = suffix_node_id
@mutableproperty
def suffix_node_id(self):
"""The id of a node with a matching suffix, representing a suffix link.
None indicates this node has no suffix link.
"""
return self._suffix_node_id
def __repr__(self):
import six
from eventsourcing.domain.model.entity import EventSourcedEntity, mutableproperty, EntityRepository
from eventsourcing.domain.model.events import publish
class SuffixTree(EventSourcedEntity):
"""A suffix tree for string matching. Uses Ukkonen's algorithm
for construction.
"""
class Created(EventSourcedEntity.Created):
pass
class AttributeChanged(EventSourcedEntity.AttributeChanged):
pass
class Discarded(EventSourcedEntity.Discarded):
pass
def __init__(self, root_node_id, case_insensitive=False, **kwargs):
"""
string
the string for which to construct a suffix tree
"""
super(SuffixTree, self).__init__(**kwargs)
self._root_node_id = root_node_id
self._case_insensitive = case_insensitive
self._nodes = {}
self._edges = {}
self._N = None
from eventsourcing.domain.model.entity import EventSourcedEntity, mutableproperty, EntityRepository, entity_mutator
from eventsourcing.domain.model.events import publish, DomainEvent
# Use a private code point to terminate the string IDs.
STRING_ID_END = '\uEFFF'
class GeneralizedSuffixTree(EventSourcedEntity):
"""A suffix tree for string matching. Uses Ukkonen's algorithm
for construction.
"""
class Created(EventSourcedEntity.Created):
pass
class AttributeChanged(EventSourcedEntity.AttributeChanged):
pass
class Discarded(EventSourcedEntity.Discarded):
pass
def __init__(self, root_node_id, case_insensitive=False, **kwargs):
super(GeneralizedSuffixTree, self).__init__(**kwargs)
self._root_node_id = root_node_id
self._case_insensitive = case_insensitive
self._nodes = {}
self._edges = {}
self._string = None
self._suffix = None
self._node_repo = None
self._edge_repo = None
"""
An example event sourced domain model entity.
"""
# Needed to get an event history longer than 10000 in Cassandra.
__page_size__ = 1000
# Make sure events that are applied to the entity have originated
# from the entity at the version the instance it is current at.
# - this assumes _validate_originator() is called in mutators.
__always_validate_originator_version__ = True
class Created(EventSourcedEntity.Created):
pass
class AttributeChanged(EventSourcedEntity.AttributeChanged):
pass
class Discarded(EventSourcedEntity.Discarded):
pass
class Heartbeat(DomainEvent):
pass
def __init__(self, a, b, **kwargs):
super(Example, self).__init__(**kwargs)
self._a = a
self._b = b
self._count_heartbeats = 0
@mutableproperty
def a(self):
raise AssertionError
edge_id = make_edge_id(suffix.source_node_id, string[suffix.first_char_index])
e = self.get_edge(edge_id)
if e.length <= suffix.length:
suffix.first_char_index += e.length + 1
suffix.source_node_id = e.dest_node_id
self._canonize_suffix(suffix, string)
class SuffixTreeNode(EventSourcedEntity):
"""A node in the suffix tree.
"""
class Created(EventSourcedEntity.Created): pass
class AttributeChanged(EventSourcedEntity.AttributeChanged): pass
class Discarded(EventSourcedEntity.Discarded): pass
class ChildNodeAdded(DomainEvent): pass
class ChildNodeRemoved(DomainEvent): pass
def __init__(self, suffix_node_id=None, string_id=None, *args, **kwargs):
super(SuffixTreeNode, self).__init__(*args, **kwargs)
self._suffix_node_id = suffix_node_id
self._string_id = string_id
self._child_node_ids = OrderedDict()
@mutableproperty
def suffix_node_id(self):
"""The id of a node with a matching suffix, representing a suffix link.
try:
del(self._child_node_ids[event.child_node_id])
except KeyError:
pass
self._increment_version()
return self
class SuffixTreeEdge(EventSourcedEntity):
"""An edge in the suffix tree.
"""
class Created(EventSourcedEntity.Created):
pass
class AttributeChanged(EventSourcedEntity.AttributeChanged):
pass
class Discarded(EventSourcedEntity.Discarded):
pass
def __init__(self, label, source_node_id, dest_node_id, **kwargs):
super(SuffixTreeEdge, self).__init__(**kwargs)
self._label = label
self._source_node_id = source_node_id
self._dest_node_id = dest_node_id
@mutableproperty
def label(self):
"""Index of start of string part represented by this edge.
"""
return self._label
None indicates this node has no suffix link.
"""
return self._suffix_node_id
def __repr__(self):
return "Node(suffix link: %d)" % self.suffix_node_id
class Edge(EventSourcedEntity):
"""An edge in the suffix tree.
"""
class Created(EventSourcedEntity.Created): pass
class AttributeChanged(EventSourcedEntity.AttributeChanged): pass
class Discarded(EventSourcedEntity.Discarded): pass
def __init__(self, first_char_index, last_char_index, source_node_id, dest_node_id, **kwargs):
super(Edge, self).__init__(**kwargs)
self._first_char_index = first_char_index
self._last_char_index = last_char_index
self._source_node_id = source_node_id
self._dest_node_id = dest_node_id
@mutableproperty
def first_char_index(self):
"""Index of start of string part represented by this edge.
"""
return self._first_char_index