This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
` | |
add () { | |
git add . | |
} | |
commit () { | |
git commit -m "$1" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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%; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
OlderNewer