Skip to content

Instantly share code, notes, and snippets.

View TheBigRoomXXL's full-sized avatar
⚗️
experimenting

TheBigRoomXXL TheBigRoomXXL

⚗️
experimenting
View GitHub Profile
@TheBigRoomXXL
TheBigRoomXXL / sqlachemy_patch.py
Last active April 7, 2023 07:05
Flask-SQLAlchemy patch method
from flask_sqlalchemy import SQLAlchemy
from flask_sqlalchemy.model import Model
class MyDbModel(Model):
"""MyDbModel is used as the "model_class" of db (aka db.Model).
Extend the SQLAlchemy functionalities throught this class."""
def patch(self, update_dictionary: dict) -> None:
"""Partial update of an object with a simple and clear syntax"""
@TheBigRoomXXL
TheBigRoomXXL / SQLPagination.py
Last active April 7, 2023 07:07
SQL Cursor for Flask-SQLAlchemy and Flask-Smorest
from flask_smorest import Api, Blueprint
from flask_sqlalchemy.query import Query
class SQLCursor():
"""Automatically paginate a Query object return by a view"""
def __init__(self, query:Query, page_params):
self.page_params = page_params
self.result = query.paginate(
page=page_params.page,
@TheBigRoomXXL
TheBigRoomXXL / traced.py
Last active April 7, 2023 07:13
Tracing decorator for OpenTelemetry
# Inspired by https://stackoverflow.com/a/309000
# With some help for typing from https://rednafi.github.io/reflections/static-typing-python-decorators.html
from functools import wraps
from collections.abc import Callable
from typing import ParamSpec, TypeVar
from opentelemetry import trace
P = ParamSpec("P")
@TheBigRoomXXL
TheBigRoomXXL / no_expire_on_commit.py
Last active April 12, 2023 07:28
Flask-SQLAchemy Session without expire_on_commit
""" Usefull before 2.0 when serializing object after commit, now you should use *returning* """
from contextlib import contextmanager
# More details here: https://stackoverflow.com/a/51452451
@contextmanager
def no_expire():
"""Provide db.session with the expire_on_commit set to False only this time."""
s = db.session()
s.expire_on_commit = False
@TheBigRoomXXL
TheBigRoomXXL / flask_post_processing.py
Last active May 12, 2023 14:35
Post Processing Decorator for Flask Smorest - Execute a function after Flask returns response
class PostProcessingTask:
def __init__(self, func: Callable[..., None], *args: Any, **kwargs: Any):
self.func = func
self.args = args
self.kwargs = kwargs
def execute(self) -> None:
self.func(*self.args, **self.kwargs)
""" Extend SQLAlchemy
This is an experiment to put in common some logique beetween sqla model class so that
most queries are done through a shared interface.
Some calls are specific to flask_sqlavhemy or smorest but can be easily adapted.
"""
from typing import TYPE_CHECKING
@TheBigRoomXXL
TheBigRoomXXL / .git_shortcut.sh
Last active September 14, 2023 12:11
Git shortcut
#!/bin/bash
`
add () {
git add .
}
commit () {
git commit -m "$1"
}
@TheBigRoomXXL
TheBigRoomXXL / img-zoom.html
Created December 15, 2023 09:07
A zoomable image with just a bit of css and js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Loading Optimisation</title>
<style>
figure.zoom {
background-position: 50% 50%;
@TheBigRoomXXL
TheBigRoomXXL / node-image-renderer.ts
Last active January 24, 2024 08:47
ImageNode with crossorigin
/**
* Sigma.js WebGL Renderer Node Program
* =====================================
*
* Program rendering nodes using GL_POINTS, but that draws an image on top of
* the classic colored disc.
* @module
*/
// import { Coordinates, Dimensions, NodeDisplayData } from "../../../types";
// import { floatColor } from "../../../utils";
// Insert using sql.DB with transaction
func InsertPage(db *sql.DB, url string, content string) error {
// Open transaction
tx, err := db.Begin()
if err != nil {
return err
}