Skip to content

Instantly share code, notes, and snippets.

View atemate's full-sized avatar
🐒

Artem Yushkovskiy atemate

🐒
View GitHub Profile
@atemate
atemate / _tenacity_retry_with_asyncio_semaphore.py
Last active March 13, 2024 12:48
Minimal example testing behaviour of tenacity.retry() with asyncio.Semaphore()
import asyncio
import random
import logging
import httpx
import tenacity
logging.basicConfig(level=logging.INFO, format="%(relativeCreated)dms %(message)s")
N_SEMAPHORE = 2
@atemate
atemate / configure_logging_to_stderr_only.py
Created August 10, 2023 09:16
Configure python logging to stderr only
import logging
import os
import sys
from distutils.util import strtobool
from typing import Optional
def configure_logging_to_stderr_only(filename: Optional[str] = None):
"""Configures a basic config depending on 'DEBUG' env variable,
and also re-configures all existing handlers to output to stderr
@atemate
atemate / match_dict.py
Last active August 9, 2023 16:28
Finds a dict in a list of dicts that matches specific filter
class WildcardDict(dict):
def __init__(self, *args, enable_wildcards: bool = False, **kwargs) -> None:
self._enable_wildcards = enable_wildcards
return super().__init__(*args, **kwargs)
def __getitem__(self, key):
if not self._enable_wildcards:
return super().__getitem__(key)
for k, v in self.items():
@atemate
atemate / camel_to_snake.py
Created July 27, 2023 11:37
Converts camel case to snake case
# Kudos to: https://stackoverflow.com/a/12867228
def camel_to_snake(value: str) -> str:
"""
Converts value in camel case to snake case:
>>> camel_to_snake("camelCase")
'camel_case'
>>> camel_to_snake("PascalCase")
'pascal_case'
>>> camel_to_snake("one1Two2Three")
@atemate
atemate / action.yml
Last active July 24, 2023 13:47
GitHub Action to install a python package from a private GCP repostiroty (artifact registry)
name: Setup private pypi - GCP Artifact Registry
inputs:
central_project:
type: string
required: true
description: GCP project where the Artifact Registry is located
central_repo:
type: string
required: true
@atemate
atemate / .pre-commit-config.yaml
Created July 21, 2023 13:27
My .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
hooks:
- id: check-added-large-files
- id: end-of-file-fixer
- id: trailing-whitespace
- id: detect-private-key
- id: no-commit-to-branch
- id: check-toml
@atemate
atemate / export_payload_var.sh
Last active July 19, 2023 16:42
export a variable from a json payload
set -eu
# shortcut for getting payload values
function export_payload_var() {
name="$1"
pattern="$2"
value=$(jq -r "$pattern" "${PAYLOAD}")
if [[ "$value" == "null" ]]; then
echo "Could not find pattern '$pattern' in payload '${PAYLOAD}'"
exit 1
@atemate
atemate / replace-first-entry.sh
Created June 30, 2023 10:57
Replace first entry in a file (with sed)
echo "tag=$tag"
echo "FILE_PATH=$FILE_PATH"
export LINE_MARK="tag:"
sed -i'' -e "0,/$LINE_MARK .*/s//$LINE_MARK $tag/" "$FILE_PATH" # replace first entry
@atemate
atemate / light_data_frame.py
Created May 23, 2023 18:41
Lightweigh pd.DataFrame without pandas
# TODO: use np.array and dtypes there
class _MyIndexer:
def __init__(self, obj) -> None:
self._obj = obj
def __getitem__(self, idx):
print('__getitem__', idx, type(idx), isinstance(idx, (list, tuple)))
if isinstance(idx, str):
@atemate
atemate / jq_get_unique_dirs.sh
Created May 17, 2023 11:58
jq_get_unique_dirs.sh
modified_dirs='["a", "a/b", "b/c/d", "c/d/e.txt"]'
modified_dirs=$(echo $modified_files | jq -r '[ .[] | split("/") | .[0] ] | unique')