Skip to content

Instantly share code, notes, and snippets.

View neuton's full-sized avatar
🇺🇦

Oleksii neuton

🇺🇦
View GitHub Profile
@neuton
neuton / push_host_status.sh
Last active September 4, 2023 13:17
push to telegram when host is up or down
#!/bin/bash
HOST=example.com
FLAG=/tmp/host_is_up
SLEEP=60
RETRIES=3
RETRY_SLEEP=10
MSG_PREFIX="example.com is"
TG_TOKEN=
TG_CHAT_ID=
@neuton
neuton / mtools.py
Last active October 25, 2022 18:22
early draft dump of some tools for class method decorators (+ combination of decorators with "&" etc.)
"""some decorators for methods of classes
>>> class A:
... "test class"
... @(cached & dynamic(int))
... def square(self, x=1):
... "docstring"
... print('computed!')
... return x**2
... @dynamic_array
@neuton
neuton / tg_send.sh
Last active April 24, 2022 18:50
send stdin lines to a telegram chat (via bot)
#!/bin/sh
[ -z "$TG_TOKEN" ] && { echo "TG_TOKEN not set"; exit 1; }
[ -z "$TG_CHAT_ID" ] && { echo "TG_CHAT_ID not set"; exit 1; }
while IFS= read -r stdin; do
# printf '%s\n' "$stdin"
curl -s -d chat_id="$TG_CHAT_ID" --data-urlencode text="$stdin" "https://api.telegram.org/bot$TG_TOKEN/sendMessage"
done
@neuton
neuton / sparkasse.py
Last active June 11, 2023 03:59
hacky sparkasse transactions push notifications via telegram bot
#!/usr/bin/env python3
# coding: utf8
import time, glob, csv, os, logging, random, sys, requests, functools, io
from datetime import datetime, timedelta
from sqlitedict import SqliteDict
from bs4 import BeautifulSoup
prefix = 'https://www.rheinhessen-sparkasse.de'
target = '/en/home/onlinebanking/umsaetze/umsaetze.html'
@neuton
neuton / he_hartree.py
Created February 13, 2022 02:27
Helium atom ground state with Hartree method
#!/usr/bin/env python3
import numpy as np
from scipy.sparse import diags, linalg
import sympy as sp
from scipy import integrate
import matplotlib.pyplot as plt
# nucleus charge:
Z = 2
@neuton
neuton / solve_schrodinger.py
Last active February 9, 2022 00:12
finite-diffs sparse matrix Schroedinger equation solver sample
#!/usr/bin/env python3
import numpy as np
from scipy.sparse import diags, linalg
import matplotlib.pyplot as plt
# solving for function values excluding boundaries:
x = np.linspace(-.5, .5, 42)[1:-1]
# imposing psi=0 boundary conditions, aka infinite walls:
laplace = diags([1, -2, 1], [-1, 0, 1], shape=[len(x)]*2) / (x[1] - x[0])**2
@neuton
neuton / wave_vs_schrodinger.py
Last active October 16, 2021 05:55
Wave equation vs Schrödinger equation
#!/usr/bin/env python3
import numpy as np
from numpy import cos, sin, pi, exp
from functools import lru_cache
nt = 500 # number of timesteps of the animation per (default) period of the oscillation
nx = 200 # number of points to evaluate the function at for each frame
@neuton
neuton / parallel.py
Created September 22, 2021 17:05
parallel CPU execution function decorator using "ray"
"""
parallel execution function decorator (with quite large overhead)
>>> @parallel(reduction=sum)
... def f(array):
... return sum(array)
>>> f(range(1, 9))
36
>>> @parallel
@neuton
neuton / dynval.py
Created July 5, 2021 04:12
a hack for dynamically defined values in Python
"""Dynamic values (hack)
Dynamic float value:
>>> f = ffloat(lambda x=1.0, y=2.0: x**y)
>>> f
1.0
>>> f(x=2, y=3)
8.0
>>> f
1.0
@neuton
neuton / classtools.py
Last active May 4, 2021 22:27
some drafty lazy black magic for Python methods inside classes
import numpy as np
class ClassWraps(type): # metaclass
"""kinda like functools.wraps decorator, but a metaclass and specifically for descriptor-decorator classes,
just to retain the documentation of the decorated methods inside a class"""
def __new__(cls, clsname, parents, attrs):
def copy_attr(attr): # closure
return property(lambda self: getattr(self._method, attr))
for attr in ['__name__', '__doc__', '__module__', '__annotations__']: # `__qualname__` won't work like this...
attrs[attr] = copy_attr(attr)