Skip to content

Instantly share code, notes, and snippets.

View DiTo97's full-sized avatar

Federico Minutoli DiTo97

View GitHub Profile
@DiTo97
DiTo97 / !database.py
Last active March 13, 2025 02:16
A feature-rich database model for use with SQLAlchemy's ORM abstraction
import typing
from uuid import UUID, uuid4
from datetime import datetime, timezone
from functools import partial
from typing import Any
from pydantic import BaseModel
from sqlalchemy import select, func, sql
from sqlalchemy.dialects.mysql import insert as mysql_insert
from sqlalchemy.dialects.postgresql import insert as postgres_insert
@DiTo97
DiTo97 / !spawning.py
Last active February 21, 2025 12:41
A collection of safe process and system utilities
import logging
import typing
import psutil
T = typing.TypeVar("T")
def safe_execute(function: typing.Callable[..., T], *args, **kwargs, timeout: int = 60) -> T:
@DiTo97
DiTo97 / !savings-simulation.py
Last active December 30, 2024 16:10
savings simulation on poste.it options
import argparse
import bisect
from typing import Any
from gekko import GEKKO
cumulative_deposit_config = {
"grossinterest": 2.6,
"deduction": 3.5 + 1.5,
@DiTo97
DiTo97 / !importing.py
Last active February 16, 2024 02:28
A minimal collection of import utility functions
import sys
import typing
if typing.TYPE_CHECKING:
import types
def srcfile_import(modpath: str, modname: str) -> "types.ModuleType":
"""It imports a python module from its srcfile
@DiTo97
DiTo97 / !primefinding.py
Last active August 25, 2024 01:06
A collection of prime finding algorithms implemented in pure python
import math
def sieve_of_atkin(n: int) -> list[int]:
"""The Sieve of Atkin prime finding algorithm, [1]_
References
----------
.. [1] A. O. L. Atkin, D. J. Bernstein, 2003
"""
@DiTo97
DiTo97 / index.html
Last active January 10, 2024 05:34
A simple python-to-JS HTTP web server from a specific webroot
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
const X = "I am X";
function helloworld() {
return "Hello world";
}
@DiTo97
DiTo97 / rotation-vs-bound-rotation.ipynb
Created January 10, 2024 04:42
A comparison between rotation and bound rotation
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@DiTo97
DiTo97 / !logging.py
Last active May 5, 2024 00:37
A centralized logging system for any library
"""A centralized logging system for any library
source code inspired by https://github.com/huggingface/transformers/blob/main/src/transformers/utils/logging.py
"""
import logging
import os
import sys
import threading
from functools import lru_cache
@DiTo97
DiTo97 / deepfrozenset.py
Created September 7, 2023 15:48
A frozen set for nested containers
from typing import Any
# TODO: type hinting
def deepfrozenset(obj: Any) -> Any:
if isinstance(obj, dict):
return frozenset([(key, deepfrozenset(val)) for key, val in obj.items()])
@DiTo97
DiTo97 / graphcoloring.py
Last active January 10, 2024 04:10
A collection of graph coloring algorithms
"""A collection of graph coloring algorithms
The problem of assigning colors to the vertices of a graph such that no two adjacent vertices have the same color.
"""
import random
import typing
from collections import defaultdict
import networkx