-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd63783
commit f0424a6
Showing
6 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Kaspar Johannes Schneider | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
__all__ = ['math_tools', 'debug_tools', 'web_tools'] | ||
|
||
from .debug_tools import * | ||
from .math_tools import * | ||
from .web_tools import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
__all__ = ['debug', 'timer', 'run_fct_get_stdout'] | ||
|
||
import inspect | ||
import sys | ||
import time | ||
from io import StringIO | ||
from typing import Callable | ||
|
||
|
||
def debug(f) -> Callable: | ||
"""Wrapper that prints function call, arguments, return value and the time elapsed. | ||
How to use: @wrappers.debug | ||
:param f: The function to be wrapped | ||
:return: The wrapped function | ||
""" | ||
|
||
def wrapper(*args): | ||
t_start = time.time() | ||
val = f(*args) | ||
t_end = time.time() | ||
print('') | ||
print('--debug--debug--debug--debug--debug--debug--debug--debug--debug--debug--') | ||
print('-- Function: ' + _get_func_str(f)) | ||
print('-- Arguments: ' + str(args)) | ||
print('-- Returned: ' + str(val)) | ||
print('-- Time elapsed [ms]: ' + str((t_end - t_start) * 1000)) | ||
print('--debug--debug--debug--debug--debug--debug--debug--debug--debug--debug--') | ||
print('') | ||
return val | ||
|
||
return wrapper | ||
|
||
|
||
def timer(f) -> Callable: | ||
"""Wrapper that times a function. | ||
How to use: @wrappers.timer | ||
:param f: The function to be wrapped | ||
:return: The wrapped function | ||
""" | ||
|
||
def wrapper(*args): | ||
t_start = time.time() | ||
val = f(*args) | ||
t_end = time.time() | ||
print('') | ||
print('--timer--timer--timer--timer--timer--timer--timer--timer--timer--timer--') | ||
print('-- Function: ' + _get_func_str(f)) | ||
print('-- Time elapsed [ms]: ' + str((t_end - t_start) * 1000)) | ||
print('--timer--timer--timer--timer--timer--timer--timer--timer--timer--timer--') | ||
print('') | ||
return val | ||
|
||
return wrapper | ||
|
||
|
||
def _get_func_str(f: Callable) -> str: | ||
"""Returns the name of a function including the argument names. | ||
:param f: The function of which the name should be returned | ||
:return: The name of the function f | ||
""" | ||
f_name = str(f).split(' ')[1] | ||
f_args = str(inspect.getfullargspec(f).args) \ | ||
.replace('[', '(') \ | ||
.replace(']', ')') \ | ||
.replace('\'', '') | ||
return f_name + f_args | ||
|
||
|
||
def run_fct_get_stdout(fct: callable, *args) -> str: | ||
"""Runs a function and collects stdout | ||
:param fct: function to be run | ||
:param args: arguments for the function | ||
:return: collected stdout | ||
""" | ||
# redirect stdout | ||
stdout_old = sys.stdout | ||
stdout_read = StringIO() | ||
sys.stdout = stdout_read | ||
|
||
# run the function | ||
fct(*args) | ||
|
||
# Read stdout and restore it | ||
sys.stdout = stdout_old | ||
return stdout_read.getvalue() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
__all__ = ['sum_1_n', 'ltm', 'is_triangular'] | ||
|
||
import math | ||
import scipy.special | ||
|
||
|
||
def sum_1_n(n: int) -> int: | ||
"""Calculates the sum from 1 to n. | ||
see https://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%E2%8B%AF | ||
:rtype: int | ||
:param n: the integer to sum up to | ||
:return: sum from 1 to n | ||
""" | ||
assert n >= 0, 'n must not be negative!' | ||
return int(n * (n + 1) / 2) | ||
|
||
|
||
def ltm(n: int) -> int: | ||
"""Returns the next lower triangular number to n or n if n is triangular. | ||
:param n: the number to get the next lower triangular number too | ||
:return: n if triangular else the next lower triangular number | ||
:rtype: int | ||
""" | ||
assert n > 0, 'n must be greater than zero!' | ||
n += 1 | ||
x1 = math.ceil((math.sqrt(8 * n + 1) - 1) / | ||
2) | ||
return int(scipy.special.binom(x1, 2)) | ||
|
||
|
||
def is_triangular(n: int) -> bool: | ||
"""Checks if a positive integer is triangular. | ||
:param n: the number to check | ||
:return: True if n is triangular else False | ||
:rtype: bool | ||
""" | ||
if n <= 0: | ||
return False | ||
return n == ltm(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
__all__ = ['is_page_up'] | ||
|
||
import requests.exceptions | ||
from requests import request | ||
|
||
|
||
def is_page_up(url: str) -> bool: | ||
"""Tests if a webpage is up (returns 200) | ||
:param url: url to be tested | ||
:return: True if url is reachable (returns 200) | ||
""" | ||
try: | ||
return request('GET', url).status_code == 200 | ||
except requests.exceptions.ConnectionError: | ||
return False |