jdna

jdna.linked_list

Linked list model to represent linear or circular sequences.

Classes

DoubleLinkedList([data, first, cyclic])

A generic double linked list class.

EmptyNode()

Node constructor.

LinkedListMatch(template_bounds, …)

A match object.

Node(data)

A node in a linked list.

Exceptions

LinkedListException

Generic linked list exception.

LinkedListIndexError

Indices were out of bounds for LinkedList.

class jdna.linked_list.DoubleLinkedList(data: Sequence[Any] = None, first: jdna.linked_list.Node = None, cyclic: bool = False)[source]

Bases: object

A generic double linked list class.

linked list construction.

Parameters
  • data (iterable) – iterable data

  • first (Node) – first node

class Direction[source]

Bases: enum.IntFlag

An enumeration.

NODE_CLASS

alias of Node

property circular

Alias for cyclic.

static collect_nodes(nodes)[source]

Return all visisted nodes and return an unordered set of nodes.

Returns

all visited nodes

Return type

set

compare(other: jdna.linked_list.DoubleLinkedList) → bool[source]

Compares two linked lists. If both are cyclic, will attempt to reindex.

Parameters

other (DoubleLinkedList) – other linked list

Returns

whether sequence data is equivalent

Return type

bool

copy_slice(start: jdna.linked_list.Node, end: jdna.linked_list.Node) → jdna.linked_list.DoubleLinkedList[source]

Return a copy of the sequence between ‘start’ and ‘end’ nodes.

If start is None, return the slice copy from head to end. If end is None, return copy from start to tail. If both start and end are None return None.

static find_ends(nodes)[source]

Efficiently finds the head and tails from a group of nodes.

find_iter(query: jdna.linked_list.DoubleLinkedList, min_query_length: int = None, direction: int = <Direction.FORWARD: 1>, protocol: Callable = None, depth: int = None)[source]

Iteratively finds positions that match the query.

Parameters
  • query (DoubleLinkedList) – query list to find

  • min_query_length (inst) – the minimum number of matches to return. If None, find_iter will only return complete matches

  • direction (int or tuple) – If Direction.FORWARD (+1), find iter will search from the query head and search forward, potentially leaving a ‘tail’ overhang on the query. If Direction.REVERSE (-1), find iter will search from the query tail and search reverse, potentially leaving a ‘head’ overhang on the query. If a tuple, the template_direction and query_direction are set respectively.

  • protocol (callable) – the callable taking two parameters (as Node) to compare during find. If None, defaults to ‘equivalent’

Returns

list of LinkedListMatches

Return type

list

inclusive_range(i: int, j: int) → Generator[jdna.linked_list.Node, None, None][source]

Return generator for inclusive nodes between index i and j.

If i is None, assume i is the head node.

classmethod new_slice(start: jdna.linked_list.Node, end: jdna.linked_list.Node) → jdna.linked_list.DoubleLinkedList[source]

Return a copy of the sequence between ‘start’ and ‘end’ nodes)

node_range(start: jdna.linked_list.Node, end: jdna.linked_list.Node) → Generator[jdna.linked_list.Node, None, None][source]

Iterate between ‘start’ to ‘end’ nodes (inclusive)

range(i: int, j: int) → Generator[jdna.linked_list.Node, None, None][source]

Returns an iterator from node at ‘i’ to node at ‘j-1’.

class jdna.linked_list.EmptyNode[source]

Bases: jdna.linked_list.Node

Node constructor. Stores a single piece of data.

Parameters

data (any) – data

_break_connections()

Break connections in this node.

Returns

Return type

_complete_match(node: jdna.linked_list.Node, next_method: Callable) → bool

Return whether the longest match between two nodes is equivalent.

Parameters
  • node (Node) – the node to compare

  • next_method (callable) – how to obtain the next node

Returns

whether the longest match between two nodes is equivalent

Return type

bool

add_next(data)

Create a new node and add to next.

Parameters

data (any) – any data

Returns

the new node

Return type

Node

add_prev(data)

Create a new node and add to previous.

Parameters

data (any) – any data

Returns

the new node

Return type

Node

cut_next()

Cut the next node, return the cut node.

Returns

the cut (next) node

Return type

Node

cut_prev()

Cut the previous node, return the cut node.

Returns

the cut (previous) node

Return type

Node

equivalent(other: jdna.linked_list.Node) → bool

Evaluates whether two nodes hold the same data.

find_first() → jdna.linked_list.Node

Find the head node.

Returns

Return type

find_last() → jdna.linked_list.Node

Find the tail node.

Returns

Return type

fwd(stop_node: Optional[jdna.linked_list.Node] = None, stop_criteria: Callable = None) → Generator

Propogates forwards until stop node is visited or stop criteria is reached.

Parameters
  • stop_node

  • stop_criteria

Returns

Return type

longest_match(node: jdna.linked_list.Node, next_method: Callable = None) → Tuple[jdna.linked_list.Node, jdna.linked_list.Node]

Find the longest match between two linked_lists.

Parameters
  • node (Node) – the node to compare

  • next_method (callable) – how to obtain the next node

Returns

list of tuples containing matching nodes

Return type

list

next()

Return the next node.

Returns

the next node

Return type

Node

prev()

Return the previous node.

Returns

the previous node

Return type

Node

remove()

Remove node from linked list, connecting the previous and next nodes together.

Returns

None

Return type

None

rev(stop_node: Optional[jdna.linked_list.Node] = None, stop_criteria: Callable = None) → Generator[jdna.linked_list.Node, None, None]

Propogates backwards until stop node is visited or stop criteria is reached.

Parameters
  • stop_node

  • stop_criteria

Returns

Return type

set_next(node: jdna.linked_list.Node)

Set the next node.

Parameters

node

Returns

Return type

set_prev(node: jdna.linked_list.Node)

Set the previous node.

Parameters

node

Returns

Return type

swap()

Swap the previous and next nodes.

Returns

None

Return type

None

exception jdna.linked_list.LinkedListException[source]

Bases: Exception

Generic linked list exception.

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception jdna.linked_list.LinkedListIndexError[source]

Bases: jdna.linked_list.LinkedListException, IndexError

Indices were out of bounds for LinkedList.

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class jdna.linked_list.LinkedListMatch(template_bounds: Tuple[jdna.linked_list.Node, jdna.linked_list.Node], query_bounds: Tuple[jdna.linked_list.Node, jdna.linked_list.Node], template: jdna.linked_list.DoubleLinkedList, query: jdna.linked_list.DoubleLinkedList)[source]

Bases: object

A match object.

classmethod batch_create(template_bounds_list: List[Tuple[jdna.linked_list.Node, jdna.linked_list.Node]], query_bounds_list: List[Tuple[jdna.linked_list.Node, jdna.linked_list.Node]], template: jdna.linked_list.DoubleLinkedList, query: jdna.linked_list.DoubleLinkedList) → List[jdna.linked_list.LinkedListMatch][source]

Efficiently create several LinkedListMatches from lists of template starts/ends and query starts/ends.

Parameters
  • template_bounds_list (template DoubleLinkedList) – list of 2 len tuples containing starts and ends from a template

  • query_bounds_list (query DoubleLinkedList) – list of 2 len tuples containing starts and ends from a query

  • template (DoubleLinkedList) – the template

  • query (DoubleLinkedList) – the query

Returns

matchese

Return type

list of LinkedListMatch

class jdna.linked_list.Node(data)[source]

Bases: object

A node in a linked list.

Node constructor. Stores a single piece of data.

Parameters

data (any) – data

_break_connections()[source]

Break connections in this node.

Returns

Return type

_complete_match(node: jdna.linked_list.Node, next_method: Callable) → bool[source]

Return whether the longest match between two nodes is equivalent.

Parameters
  • node (Node) – the node to compare

  • next_method (callable) – how to obtain the next node

Returns

whether the longest match between two nodes is equivalent

Return type

bool

add_next(data)[source]

Create a new node and add to next.

Parameters

data (any) – any data

Returns

the new node

Return type

Node

add_prev(data)[source]

Create a new node and add to previous.

Parameters

data (any) – any data

Returns

the new node

Return type

Node

cut_next()[source]

Cut the next node, return the cut node.

Returns

the cut (next) node

Return type

Node

cut_prev()[source]

Cut the previous node, return the cut node.

Returns

the cut (previous) node

Return type

Node

equivalent(other: jdna.linked_list.Node) → bool[source]

Evaluates whether two nodes hold the same data.

find_first() → jdna.linked_list.Node[source]

Find the head node.

Returns

Return type

find_last() → jdna.linked_list.Node[source]

Find the tail node.

Returns

Return type

fwd(stop_node: Optional[jdna.linked_list.Node] = None, stop_criteria: Callable = None) → Generator[source]

Propogates forwards until stop node is visited or stop criteria is reached.

Parameters
  • stop_node

  • stop_criteria

Returns

Return type

longest_match(node: jdna.linked_list.Node, next_method: Callable = None) → Tuple[jdna.linked_list.Node, jdna.linked_list.Node][source]

Find the longest match between two linked_lists.

Parameters
  • node (Node) – the node to compare

  • next_method (callable) – how to obtain the next node

Returns

list of tuples containing matching nodes

Return type

list

next()[source]

Return the next node.

Returns

the next node

Return type

Node

prev()[source]

Return the previous node.

Returns

the previous node

Return type

Node

remove()[source]

Remove node from linked list, connecting the previous and next nodes together.

Returns

None

Return type

None

rev(stop_node: Optional[jdna.linked_list.Node] = None, stop_criteria: Callable = None) → Generator[jdna.linked_list.Node, None, None][source]

Propogates backwards until stop node is visited or stop criteria is reached.

Parameters
  • stop_node

  • stop_criteria

Returns

Return type

set_next(node: jdna.linked_list.Node)[source]

Set the next node.

Parameters

node

Returns

Return type

set_prev(node: jdna.linked_list.Node)[source]

Set the previous node.

Parameters

node

Returns

Return type

swap()[source]

Swap the previous and next nodes.

Returns

None

Return type

None