Skip to content

Instantly share code, notes, and snippets.

View atemate's full-sized avatar
🐒

Artem Yushkovskiy atemate

🐒
View GitHub Profile
@atemate
atemate / fspath.md
Last active June 4, 2024 16:41
Python OOP
>>> from os import PathLike
>>> class MyPath(PathLike):
...     def __init__(self, prefix, path):
...         self._prefix = prefix
...         self._path = path
...
...     def __fspath__(self):
...         return self._prefix + self._path
...
@atemate
atemate / __init__.py
Created June 4, 2024 12:31
Python code to check if extras is installed
import importlib
# require installation of 'gcp' extras
try:
importlib.util.find_spec("google.cloud")
except ImportError:
raise ValueError("Please install package[gcp]")
@atemate
atemate / README.md
Last active May 28, 2024 21:46
PubSub request/response with dynamically created subscriptions with filters
@atemate
atemate / docker-compose.yaml
Created May 22, 2024 10:21
Docker-compose config for SNS running in a Localstack image
version: "0.1"
services:
sns:
image: localstack/localstack
environment:
# LocalStack configuration: https://docs.localstack.cloud/references/configuration/
- DEBUG=${DEBUG:-0}
- SERVICES=sns
ports:
- "4566:4566"
@atemate
atemate / iter_dict_in_chunks.py
Created April 26, 2024 10:50
Function to iterate over a dict of lists and yield key:values
from typing import Any, Dict, List
def iter_dict_in_chunks(input_dict: Dict[Any, List], chunk_size: int, total_size: int):
"""Iterates a dict of lists and yields key:values pairs where len of values
is at most `chunk_size`, until `total_size` is exhausted.
Examples:
# Realistic cases:
>>> d = {'a': [1, 2], 'b': [3, 4, 5, 6, 7, 8, 9]}
>>> list(iter_dict_in_chunks(d, 3, 5))
@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