Skip to content

Instantly share code, notes, and snippets.

View kkroesch's full-sized avatar

Karsten Kroesch kkroesch

View GitHub Profile
@kkroesch
kkroesch / logstash.conf.j2
Created February 3, 2022 13:06
Logstash configuration template for polling weather api and store observations into Elasticsearch.
input {
http_poller {
urls => {
wunderground => "https://api.weather.com/v2/pws/observations/current?stationId=IDULLI1&format=json&units=m&apiKey={{ api_key }}"
}
request_timeout => 60
schedule => { cron => "0,15,30,45 * * * * UTC"}
codec => "json"
# A hash of request metadata info (timing, response headers, etc.) will be sent here
metadata_target => "http_poller_metadata"
@kkroesch
kkroesch / datamanager.py
Last active June 4, 2022 21:57
Persistent YAML with easy access.
import yaml
from dotmap import DotMap as DD
class DataManager:
""" Manages YAML data representation where members are accessible with dot notation.
"""
data_map = DD()
@kkroesch
kkroesch / qs.py
Created January 25, 2022 14:12
Calculate digit sum
def qs(k : int) -> int:
s = 0
while k:
s = s + k % 10
k = k // 10
return s
@kkroesch
kkroesch / phi.py
Last active January 21, 2022 12:25
Eulers Phi Function
from math import gcd
def φ(z):
return sum([gcd(z,k)==1 for k in range(1, z+1)])
@kkroesch
kkroesch / ast.py
Last active January 12, 2022 08:13
Model algebraic expressions
from dataclasses import dataclass
from numbers import Number
from unittest import TestCase
class Expression:
def derive(self, variable):
return self
@kkroesch
kkroesch / kibana-curl.sh
Created November 18, 2021 19:49 — forked from arpat/kibana-curl.sh
CURL the kibana endpoint to query Elasticsearch
#!/bin/bash
#
## CURL the kibana endpoint
## Use the kibana dev console to validate query json
curl -i --insecure --user User:Password -XPOST \
--header "kbn-version: 5.6.0" \
'https://kibana.yourdomain/api/console/proxy?path=_search&method=POST' \
--data '{"query":{"match_all":{}}}'
@kkroesch
kkroesch / Vagrantfile
Last active August 19, 2021 13:53
Lightweight Kubernetes Playground with K3S
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.define "controlplane", primary: true do |controlplane|
config.vm.box = "centos/7"
config.vm.hostname = "haithabu"
end
@kkroesch
kkroesch / numbers.py
Last active October 29, 2021 10:54
Readable large numbers in Python
""" Underscores in number literals are ignored: """
>>> ten_billion = 10_000_000_000
>>> ten_billion
10000000000
""" In output formatting, you can insert thousands separators: """
>>> f"{ten_billion:,}"
'10,000,000,000'
@kkroesch
kkroesch / plugin.py
Last active June 4, 2022 21:57
Plugin registry implementation
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Registry(metaclass=Singleton):
"""Management for plugins as a singleton.
@kkroesch
kkroesch / test_tracer.py
Last active July 18, 2021 07:53
Tracer Ammunition for Python code. See test case how to use it.
import sys, logging
from unittest import TestCase
from tracer import trace, profile, log, slow_down, timer, debug
class TestTrace(TestCase):
def setUp(self):
root = logging.getLogger()
root.setLevel(logging.DEBUG)