master > master: code py - refactoring
This commit is contained in:
0
code/python/src/models/__init__.py
Normal file
0
code/python/src/models/__init__.py
Normal file
16
code/python/src/models/graphs/__init__.py
Normal file
16
code/python/src/models/graphs/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.models.graphs.graph import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'Graph',
|
||||
];
|
||||
60
code/python/src/models/graphs/graph.py
Normal file
60
code/python/src/models/graphs/graph.py
Normal file
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from __future__ import annotations;
|
||||
|
||||
from src.thirdparty.types import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'Graph',
|
||||
];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# CLASS Graph
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
class Graph(object):
|
||||
'''
|
||||
a data structure for graphs
|
||||
'''
|
||||
nodes: list[Any];
|
||||
edges: list[tuple[Any,Any]]
|
||||
|
||||
def __init__(self, nodes: list[Any], edges: list[tuple[Any,Any]]):
|
||||
self.nodes = nodes;
|
||||
self.edges = edges;
|
||||
return;
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.nodes);
|
||||
|
||||
def subgraph(self, nodes: list[Any]) -> Graph:
|
||||
'''
|
||||
@returns graph induced by subset of nodes
|
||||
'''
|
||||
return Graph(
|
||||
nodes = [ u for u in self.nodes if u in nodes ],
|
||||
edges = [ (u, v) for u, v in self.edges if u in nodes and v in nodes ],
|
||||
);
|
||||
|
||||
def successors(self, u: str):
|
||||
'''
|
||||
@returns
|
||||
list of successor nodes
|
||||
'''
|
||||
return [ v for (u_, v) in self.edges if u == u_ ];
|
||||
|
||||
def predecessors(self, v: str):
|
||||
'''
|
||||
@returns
|
||||
list of predecessor nodes
|
||||
'''
|
||||
return [ u for (u, v_) in self.edges if v == v_ ];
|
||||
23
code/python/src/models/hirschberg/__init__.py
Normal file
23
code/python/src/models/hirschberg/__init__.py
Normal file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.models.hirschberg.alignment import *;
|
||||
from src.models.hirschberg.paths import *;
|
||||
from src.models.hirschberg.penalties import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'Alignment',
|
||||
'AlignmentBasic',
|
||||
'AlignmentPair',
|
||||
'Directions',
|
||||
'gap_penalty',
|
||||
'missmatch_penalty',
|
||||
];
|
||||
112
code/python/src/models/hirschberg/alignment.py
Normal file
112
code/python/src/models/hirschberg/alignment.py
Normal file
@@ -0,0 +1,112 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from __future__ import annotations;
|
||||
|
||||
from src.thirdparty.types import *;
|
||||
from src.thirdparty.maths import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'Alignment',
|
||||
'AlignmentBasic',
|
||||
'AlignmentPair',
|
||||
];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# Class Alignments
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
class Alignment():
|
||||
@property
|
||||
def parts1(self) -> List[str]:
|
||||
if isinstance(self, AlignmentBasic):
|
||||
return [self.word1];
|
||||
elif isinstance(self, AlignmentPair):
|
||||
return self.left.parts1 + self.right.parts1;
|
||||
return [];
|
||||
|
||||
@property
|
||||
def parts2(self) -> List[str]:
|
||||
if isinstance(self, AlignmentBasic):
|
||||
return [self.word2];
|
||||
elif isinstance(self, AlignmentPair):
|
||||
return self.left.parts2 + self.right.parts2;
|
||||
return [];
|
||||
|
||||
def astree(
|
||||
self,
|
||||
indent: str = ' ',
|
||||
prefix: str = '',
|
||||
braces: bool = False,
|
||||
branch: str = ' └──── ',
|
||||
) -> str:
|
||||
return '\n'.join(list(self._astree_recursion(indent=indent, prefix=prefix, braces=braces, branch=branch)));
|
||||
|
||||
def _astree_recursion(
|
||||
self,
|
||||
depth: int = 0,
|
||||
indent: str = ' ',
|
||||
prefix: str = '',
|
||||
braces: bool = False,
|
||||
branch: str = ' └──── ',
|
||||
branch_atom: str = '˚└──── ',
|
||||
) -> Generator[str, None, None]:
|
||||
word1 = self.as_string1(braces=braces);
|
||||
word2 = self.as_string2(braces=braces);
|
||||
if isinstance(self, AlignmentBasic):
|
||||
u = prefix + branch_atom if depth > 0 else prefix;
|
||||
yield f'{u}{word2}';
|
||||
if depth == 0:
|
||||
yield f'{" "*len(u)}{"-"*len(word1)}';
|
||||
yield f'{" "*len(u)}{word1}';
|
||||
elif isinstance(self, AlignmentPair):
|
||||
u = prefix + branch if depth > 0 else prefix;
|
||||
yield f'{u}{word2}';
|
||||
if depth == 0:
|
||||
yield f'{" "*len(u)}{"-"*len(word1)}';
|
||||
yield f'{" "*len(u)}{word1}';
|
||||
yield f'{indent}{prefix} │';
|
||||
yield from self.left._astree_recursion(
|
||||
depth = depth + 1,
|
||||
indent = indent,
|
||||
prefix = indent + prefix,
|
||||
braces = braces,
|
||||
branch = branch,
|
||||
);
|
||||
yield f'{indent}{prefix} │';
|
||||
yield from self.right._astree_recursion(
|
||||
depth = depth + 1,
|
||||
indent = indent,
|
||||
prefix = indent + prefix,
|
||||
braces = braces,
|
||||
branch = branch,
|
||||
);
|
||||
return;
|
||||
|
||||
def as_string1(self, braces: bool = False) -> Tuple[str, str]:
|
||||
if braces:
|
||||
return f'({")(".join(self.parts1)})';
|
||||
return ''.join(self.parts1);
|
||||
|
||||
def as_string2(self, braces: bool = False,) -> Tuple[str, str]:
|
||||
if braces:
|
||||
return f'({")(".join(self.parts2)})';
|
||||
return ''.join(self.parts2);
|
||||
|
||||
@dataclass
|
||||
class AlignmentBasic(Alignment):
|
||||
word1: str = field();
|
||||
word2: str = field();
|
||||
|
||||
@dataclass
|
||||
class AlignmentPair(Alignment):
|
||||
left: Alignment = field();
|
||||
right: Alignment = field();
|
||||
28
code/python/src/models/hirschberg/paths.py
Normal file
28
code/python/src/models/hirschberg/paths.py
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.thirdparty.types import *;
|
||||
from src.setup.config import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'Directions',
|
||||
];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# ENUMS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
class Directions(Enum):
|
||||
UNSET = -1;
|
||||
# Prioritäten hier setzen
|
||||
DIAGONAL = 0;
|
||||
HORIZONTAL = 1;
|
||||
VERTICAL = 2;
|
||||
28
code/python/src/models/hirschberg/penalties.py
Normal file
28
code/python/src/models/hirschberg/penalties.py
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.thirdparty.types import *;
|
||||
from src.setup.config import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'gap_penalty',
|
||||
'missmatch_penalty',
|
||||
];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# PENALTIES
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def gap_penalty(x: str):
|
||||
return OPTIONS.hirschberg.penality_gap;
|
||||
|
||||
def missmatch_penalty(x: str, y: str):
|
||||
return 0 if x == y else OPTIONS.hirschberg.penality_missmatch;
|
||||
16
code/python/src/models/stacks/__init__.py
Normal file
16
code/python/src/models/stacks/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.models.stacks.stack import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'Stack',
|
||||
];
|
||||
70
code/python/src/models/stacks/stack.py
Normal file
70
code/python/src/models/stacks/stack.py
Normal file
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.thirdparty.types import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'Stack',
|
||||
];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# CLASS Stack
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
class Stack:
|
||||
'''
|
||||
A data structure for stacks
|
||||
'''
|
||||
elements: list[Any];
|
||||
|
||||
def __init__(self):
|
||||
self.elements = [];
|
||||
return;
|
||||
|
||||
def __len__(self):
|
||||
'''
|
||||
@returns
|
||||
number of elements in stack
|
||||
'''
|
||||
return len(self.elements);
|
||||
|
||||
def __contains__(self, value: Any) -> bool:
|
||||
return value in self.elements;
|
||||
|
||||
def push(self, value: Any):
|
||||
'''
|
||||
add element to stack
|
||||
'''
|
||||
self.elements.append(value);
|
||||
|
||||
def top(self) -> Any:
|
||||
'''
|
||||
@returns
|
||||
top element from stack without removal
|
||||
'''
|
||||
if len(self.elements) == 0:
|
||||
raise Exception('Stack is empty!');
|
||||
return self.elements[-1];
|
||||
|
||||
def pop(self) -> Any:
|
||||
'''
|
||||
@returns
|
||||
top element from stack and removes it
|
||||
'''
|
||||
value = self.top();
|
||||
self.elements = self.elements[:-1];
|
||||
return value;
|
||||
|
||||
def contains(self, element: Any) -> bool:
|
||||
'''
|
||||
checks if element in stack:
|
||||
'''
|
||||
return element in self.elements;
|
||||
Reference in New Issue
Block a user