Skip to content

Instantly share code, notes, and snippets.

@bogdandm
bogdandm / s3_cache.py
Last active December 9, 2022 13:58
Update cache-control for S3 files
import mimetypes
from itertools import chain
import boto3
from django.conf import settings
from tqdm import tqdm
sts = boto3.client(
service_name='sts',
aws_access_key_id=settings.AWS_STS_ACCESS_KEY_ID,
class StringIteratorIO(io.TextIOBase):
"""
Creates file-like object from any string iterator (read-only, unicode only, no buffer)
"""
def __init__(self, iter: t.Iterator[str]):
self._iter = iter
self._left = ''
def readable(self):
@bogdandm
bogdandm / stdout.py
Last active October 11, 2022 18:33
Collect and annotate log from processes list (pool)
processes = {}
pipe_r, pipe_w = os.pipe()
proc = subprocess.Popen(
[...],
stdout=pipe_w,
stderr=pipe_w
)
processes[proc] = pipe_r, pipe_w
def handle_output(processes: Dict[subprocess.Popen, Tuple[int, int]], timeout: float = 0): # int here - result from
#!python
# pylint: skip-file
import datetime
import json
import os
import pathlib
import threading
import signal
import sys
from collections import defaultdict
@bogdandm
bogdandm / axis_style.py
Last active April 13, 2021 09:08
Jupyter notebooks copy-paste
def ax_format(ax):
ax.grid(which='minor', axis='x', dashes=(5, 5), linewidth=.5)
ax.grid(which='minor', axis='y', dashes=(5, 5), linewidth=.5)
ax.tick_params(axis='both', which='major', labelsize=14)
ax.tick_params(axis='both', which='minor', labelsize=8)
ax.xaxis.set_minor_formatter(tk.FormatStrFormatter("%.0f"))
ax.minorticks_on()
ax.legend()
@bogdandm
bogdandm / 1.py
Last active October 9, 2020 15:44
TS code generation example
@OpenAPI(
methods=['POST'],
request_model=SignupRequest,
request_mime_type=RequestBodyFormat.form_data,
responses={
200: t.Union[SignupSuccessResponse, FormErrorResponse]
},
error_response_callback=signup_error_response
)
def signup(request: HttpRequest, data: SignupRequest) -> HttpResponse:
@bogdandm
bogdandm / Redis.ipynb
Last active March 5, 2020 07:20
Redis monitor dump analyzer
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import time
from django.conf import settings
from django.db.backends import utils
truncate = 1000
try:
import sqlparse
sqlparse_format_kwargs_defaults = dict(
reindent_aligned=True,
truncate_strings=500,
@bogdandm
bogdandm / context_required.py
Created September 16, 2019 09:18
Validate usage of serializer.context
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from rest_framework import serializers as s
class ContextRequired(s.Field):
"""
Specify required data in serializer context
@bogdandm
bogdandm / validation_middleware.py
Last active September 16, 2019 09:16
Validate drf-yasg OpenAPI Schema
from __future__ import absolute_import, unicode_literals, division, print_function
import logging
import os
import sentry_sdk
import six
import ujson as json
import jsonschema