Skip to content

Instantly share code, notes, and snippets.

View Menziess's full-sized avatar

Stefan Schenk Menziess

View GitHub Profile
from datetime import date
from datetime import datetime as dt
from typing import Union, cast
from dateutil.tz import tzlocal, tzutc
DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
def int_to_timestamp(
from time import sleep
from typing import Any
def retry(f, *args, _retries: int = 3, _sleep: float = 0.0, **kwargs) -> Any:
"""Retry function call until it succeeds."""
if _retries < 0:
raise ValueError('_retries cannot be negative.')
tries = _retries + 1
for attempt in range(tries):
try:
import sys
import logging
logger = logging.getLogger(__file__)
def handle_exception(exc_type, exc_value, exc_traceback):
"""Can be assigned to sys.excepthook to log uncaught exceptions."""
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
name: $(BuildID)
trigger:
- master
- feature/*
- refs/tags/*
variables:
${{ if startsWith(variables['Build.SourceBranch'], 'refs/tags/') }}:
group: prd
/bin/kafka-topics --bootstrap-server localhost:9091 --list
/bin/kafka-topics --bootstrap-server localhost:9091 --create --topic lol
/bin/kafka-topics --bootstrap-server localhost:9091 --delete --topic lol
/bin/kafka-console-consumer --bootstrap-server localhost:9091 --topic lol --from-beginning
/bin/kafka-console-producer --bootstrap-server localhost:9091 --topic lol
@Menziess
Menziess / flatten_structs.py
Created March 19, 2024 12:13
Spark flatten nested structures
from pyspark.sql.functions import *
def flatten_structs(df):
"""Omits lists, and flattens structs into regular columns.
>>> flatten_structs(test_df).show() # doctest: +NORMALIZE_WHITESPACE
Omitted column rootstructype.nestedstructtype
Omitted column arraytype
+---+--------+---------+------------------+------------------+------------------+
@Menziess
Menziess / row_exists_in_group.py
Last active March 19, 2024 12:18
Spark check row exists in group using windowing
from pyspark.sql.functions import *
from pyspark.sql.window import Window
from pyspark.sql import Column
from functools import reduce
w = Window.partitionBy('group_id')
filter_expression = reduce(Column.__and__, (
@Menziess
Menziess / camelcase.py
Created October 18, 2023 12:16
Convert input string to snake-, camel-, kebab- or pascalcase.
from re import findall
from toolz.curried import curry, map, pipe
def to_something_case(text: str, case='snake') -> str:
"""Convert input string to snake-, camel-, kebab- or pascalcase."""
if not text:
return text
first_char, sep, other_first_chars = {
'snake': (str.lower, '_', str.lower),
'camel': (str.lower, '', str.title),
@Menziess
Menziess / rocksdb.py
Last active November 24, 2023 11:22
Rocksdb using the rocksdict python package, cleaning/pruning old data via TTL or FIFO compaction
import os
import random
import string
from os import cpu_count
from pprint import pprint
from time import sleep
from rocksdict import (AccessType, DBCompactionStyle, DBCompressionType,
FifoCompactOptions, Options, Rdict, ReadOptions)
def timer(start):
_start = start
def _(end):
nonlocal _start
it_took = end - _start
_start = end
return it_took
return _