Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 23:37 (UTC +03:00)
View GitHub Profile
@horodchukanton
horodchukanton / simple_tasks_queue.py
Last active September 24, 2022 11:13
FastAPI Background tasks queue
import logging
from concurrent.futures import ThreadPoolExecutor
from datetime import timedelta, datetime
from typing import Any, Dict
class SimpleStorageEntry:
_counter: int = 0
id: int
@spookylukey
spookylukey / after_fetch_queryset_mixin.py
Created September 2, 2021 20:31
AfterFetchQuerySetMixin for Django
class AfterFetchQuerySetMixin:
"""
QuerySet mixin to enable functions to run immediately
after records have been fetched from the DB.
"""
# This is most useful for registering 'prefetch_related' like operations
# or complex aggregations that need to be run after fetching, but while
# still allowing chaining of other QuerySet methods.
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@adamchainz
adamchainz / double_checked_lock_iterator.py
Created February 28, 2020 17:18
double_checked_lock_iterator.py
# refactor of https://lukeplant.me.uk/blog/posts/double-checked-locking-with-django-orm/
# untested
def double_checked_lock_iterator(queryset):
for item_pk in queryset.values_list("pk", flat=True):
with transaction.atomic():
try:
yield queryset.select_for_update(skip_locked=True).get(id=item_pk)
except queryset.model.DoesNotExist:
pass
# HANDLER
from django.utils.translation import ugettext_lazy as _
from rest_framework.exceptions import ErrorDetail
from rest_framework.views import exception_handler
non_field_errors = 'non_field_errors'
def set_custome_errors(errors):
@omarryhan
omarryhan / Sanic-Gino-Alembic migrations Setup
Last active May 30, 2020 15:49
Sanic/Gino/Alembic migrations
# Do the first 6 steps only once.
1. pip install --user alembic
2. bash: ``cd {{my_project}} && alembic init alembic``
3. bash: ``text_editor {{my_project}}/alembic.ini``
4. Change: "sqlalchemy.url = postgres://{{username}}:{{password}}@{{address}}/{{db_name}}"
5. bash: ``text_editor {{my_project}}/alembic/env.py``
6. Now, import your metadata/db object from your app.:
# {{my_project}}/{{my_project_dir}}/app.py
@dedsm
dedsm / timescale--db--backend--base.py
Last active April 24, 2024 14:15
WeRiot Django Timescale integration
import logging
from django.contrib.gis.db.backends.postgis.base import \
DatabaseWrapper as PostgisDBWrapper
from django.db import ProgrammingError
from .schema import TimescaleSchemaEditor
logger = logging.getLogger(__name__)
@hakib
hakib / custom_django_checks.py
Last active April 10, 2024 13:01
Custom django checks using Django check framework, inspect and ast.
"""
Custom django checks.
H001: Field has no verbose name.
H002: Verbose name should use gettext.
H003: Words in verbose name must be all upper case or all lower case.
H004: Help text should use gettext.
H005: Model must define class Meta.
H006: Model has no verbose name.
H007: Model has no verbose name plural.
@althonos
althonos / setup.cfg
Last active March 4, 2024 18:08
A `setup.cfg` template for my Python projects
# https://gist.github.com/althonos/6914b896789d3f2078d1e6237642c35c
[metadata]
name = {name}
version = file: {name}/_version.txt
author = Martin Larralde
author_email = martin.larralde@embl.de
url = https://github.com/althonos/{name}
description = {description}
long_description = file: README.md
@kai101
kai101 / wkhtmltopdf.md
Last active August 24, 2023 09:10
How to Install Wkhtmltopdf 12.4 with patched Qt?

How to Install Wkhtmltopdf 12.4 with patched Qt?

For earlier version whos suffering from versionlock syndrome. Please follow the instructions to rollback to older SSL interface. Do not alarm by the fallback ssl interface, they received security patches for vulnerability discovered.

Current version of apt-get wkhtmltopdf does not come with Qt patch. There are some issue going with both Qt patched and non-patched. Most common use case is compatible with the patched Qt.

@gauravvjn
gauravvjn / create_sha256_signature.py
Last active February 7, 2022 23:58
Create HMAC SHA256 signature/encryption/encode
"""
https://gist.github.com/Azadehkhojandi/50eaae4cf20b21faef186f2c8ee97873
"""
import hmac
import hashlib
import binascii
def create_sha256_signature(key, message):