From 4b4634994c1b6a7ee08f355face1ac11d6696d96 Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Thu, 7 Apr 2022 16:41:15 +0200 Subject: [PATCH] master > master: src-py - init --- code/python/src/__init__.py | 0 code/python/src/graphs/__init__.py | 0 code/python/src/graphs/graph.py | 46 +++++++++++++++++++++ code/python/src/local/__init__.py | 0 code/python/src/local/config.py | 24 +++++++++++ code/python/src/local/io.py | 20 ++++++++++ code/python/src/local/maths.py | 18 +++++++++ code/python/src/local/misc.py | 18 +++++++++ code/python/src/local/system.py | 23 +++++++++++ code/python/src/local/typing.py | 37 +++++++++++++++++ code/python/src/stacks/__init__.py | 0 code/python/src/stacks/stack.py | 64 ++++++++++++++++++++++++++++++ 12 files changed, 250 insertions(+) create mode 100644 code/python/src/__init__.py create mode 100644 code/python/src/graphs/__init__.py create mode 100644 code/python/src/graphs/graph.py create mode 100644 code/python/src/local/__init__.py create mode 100644 code/python/src/local/config.py create mode 100644 code/python/src/local/io.py create mode 100644 code/python/src/local/maths.py create mode 100644 code/python/src/local/misc.py create mode 100644 code/python/src/local/system.py create mode 100644 code/python/src/local/typing.py create mode 100644 code/python/src/stacks/__init__.py create mode 100644 code/python/src/stacks/stack.py diff --git a/code/python/src/__init__.py b/code/python/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/python/src/graphs/__init__.py b/code/python/src/graphs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/python/src/graphs/graph.py b/code/python/src/graphs/graph.py new file mode 100644 index 0000000..5d4c0de --- /dev/null +++ b/code/python/src/graphs/graph.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# EXPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +__all__ = [ + 'Graph', +]; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# CLASS Graph +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +class Graph(object): + ''' + a data structure for graphs + ''' + nodes: list[str]; + edges: list[tuple[str,str]] + + def __init__(self, nodes: list[str], edges: list[tuple[str,str]]): + self.nodes = nodes; + self.edges = edges; + return; + + def successor(self, u: str): + ''' + @returns + list of successor nodes + ''' + return [ v for (u_, v) in self.edges if u == u_ ]; + + def predecessor(self, v: str): + ''' + @returns + list of predecessor nodes + ''' + return [ u for (u, v_) in self.edges if v == v_ ]; diff --git a/code/python/src/local/__init__.py b/code/python/src/local/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/python/src/local/config.py b/code/python/src/local/config.py new file mode 100644 index 0000000..93f1cc5 --- /dev/null +++ b/code/python/src/local/config.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +import json; +from yaml import add_constructor; +from yaml import load; +from yaml import Loader; +from yaml import FullLoader; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# EXPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +__all__ = [ + 'json', + 'add_constructor', + 'load', + 'Loader', + 'FullLoader', +]; diff --git a/code/python/src/local/io.py b/code/python/src/local/io.py new file mode 100644 index 0000000..cf32d8e --- /dev/null +++ b/code/python/src/local/io.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +import io; +import getpass; +import argparse; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# EXPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +__all__ = [ + 'io', + 'getpass', + 'argparse', +]; diff --git a/code/python/src/local/maths.py b/code/python/src/local/maths.py new file mode 100644 index 0000000..7bfe9ad --- /dev/null +++ b/code/python/src/local/maths.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +import math; +import random; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# EXPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +__all__ = [ + 'math', + 'random', +]; diff --git a/code/python/src/local/misc.py b/code/python/src/local/misc.py new file mode 100644 index 0000000..b485ace --- /dev/null +++ b/code/python/src/local/misc.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +import re; +from textwrap import dedent; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# EXPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +__all__ = [ + 're', + 'dedent', +]; diff --git a/code/python/src/local/system.py b/code/python/src/local/system.py new file mode 100644 index 0000000..3157ffb --- /dev/null +++ b/code/python/src/local/system.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +import os; +import sys; + +import platform; +import shutil; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# EXPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +__all__ = [ + 'os', + 'sys', + 'platform', + 'shutil', +]; diff --git a/code/python/src/local/typing.py b/code/python/src/local/typing.py new file mode 100644 index 0000000..618353a --- /dev/null +++ b/code/python/src/local/typing.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +from types import TracebackType; + +from typing import Any; +from typing import Callable; +from typing import Dict; +from typing import Generator; +from typing import Generic; +from typing import List; +from typing import Tuple; +from typing import Type; +from typing import TypeVar; +from typing import Union; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# EXPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +__all__ = [ + 'TracebackType', + 'Any', + 'Callable', + 'Dict', + 'Generator', + 'Generic', + 'List', + 'Tuple', + 'Type', + 'TypeVar', + 'Union', +]; diff --git a/code/python/src/stacks/__init__.py b/code/python/src/stacks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/python/src/stacks/stack.py b/code/python/src/stacks/stack.py new file mode 100644 index 0000000..85cc025 --- /dev/null +++ b/code/python/src/stacks/stack.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +from src.local.typing import *; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# EXPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +__all__ = [ + 'Stack', +]; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# CLASS Stack +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +class Stack: + ''' + A data structure for stacks + ''' + values: list[Any]; + + def __init__(self): + self.values = []; + return; + + def __len__(self): + ''' + @returns + number of elements in stack + ''' + return len(self.values); + + def __contains__(self, value: Any) -> bool: + return value in self.values; + + def push(self, value: Any): + ''' + add element to stack + ''' + self.values.append(value); + + def pop(self) -> Any: + ''' + @returns + top element from stack and removes it + ''' + value = self.top(); + self.values = self.values[:-1]; + return value; + + def top(self) -> Any: + ''' + @returns + top element from stack without removal + ''' + if len(self.values) == 0: + raise Exception('Stack is empty!'); + return self.values[-1];