Skip to content

Instantly share code, notes, and snippets.

View laurent-laporte-pro's full-sized avatar

Laurent LAPORTE laurent-laporte-pro

View GitHub Profile
@laurent-laporte-pro
laurent-laporte-pro / big_matrix_benchmark.py
Last active August 30, 2023 07:58
2D-NumPy Matrix Benchmark: Measuring Read/Write Times and Memory Usage
import json
import multiprocessing
import sys
import time
import typing as t
import typing_extensions as te
from pathlib import Path
import numpy as np
import pandas as pd
@laurent-laporte-pro
laurent-laporte-pro / id_generator.py
Last active July 6, 2023 08:56
Transform a name into an identifier by replacing consecutive invalid characters by a single white space, and then whitespaces are striped from both ends.
import re
# Invalid chars was taken from Antares Simulator (C++).
_sub_invalid_chars = re.compile(r"[^a-zA-Z0-9_(),& -]+").sub
def transform_name_to_id(name: str, lower: bool = True) -> str:
"""
Transform a name into an identifier by replacing consecutive
invalid characters by a single white space, and then whitespaces
@laurent-laporte-pro
laurent-laporte-pro / product_model_optionals.py
Last active May 24, 2023 13:03
Pydantic model with optional fields
from enum import Enum
from typing import Optional
import pydantic.main
from pydantic import Field, BaseModel
class AllOptionalMetaclass(pydantic.main.ModelMetaclass):
....
@laurent-laporte-pro
laurent-laporte-pro / all_optional_metaclass.py
Last active October 13, 2023 19:22
Metaclass that makes all fields of a Pydantic model optional.
from typing import Any, Dict, Optional, Tuple, Type
import pydantic.main
class AllOptionalMetaclass(pydantic.main.ModelMetaclass):
"""
Metaclass that makes all fields of a Pydantic model optional.
This metaclass modifies the class's annotations to make all fields
@laurent-laporte-pro
laurent-laporte-pro / product_model.py
Created May 24, 2023 11:32
Advanced Pydantic model -- example of "product" with INI parameters
from enum import Enum
from pydantic import Field
from antarest.study.business.utils import FormFieldsBaseModel
class SizeEnum(str, Enum):
SMALL = "Small"
MEDIUM = "Medium"
@laurent-laporte-pro
laurent-laporte-pro / find_non_std_imports.py
Created January 22, 2023 15:48
Find imports that are not part of the standard Python library (for `install_requires`)
import ast
import os
from typing import Set
STD_LIB = {
# fmt :off
'__future__', '__main__', '_abc', '_ast', '_asyncio', '_bisect', '_blake2', '_bootlocale', '_bz2', '_codecs',
'_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections',
'_collections_abc', '_compat_pickle', '_compression', '_contextvars', '_crypt', '_csv', '_ctypes',
'_ctypes_test', '_curses', '_curses_panel', '_datetime', '_dbm', '_decimal', '_dummy_thread', '_elementtree',
@laurent-laporte-pro
laurent-laporte-pro / color_converter.py
Last active November 5, 2019 20:47
Parse/Format CSS color values (like #ff7f7f or #00ff00a5).
# coding: utf-8
import re
_match_hex8 = re.compile(
r"^#(?P<r>[0-9a-f]{2})(?P<g>[0-9a-f]{2})(?P<b>[0-9a-f]{2})(?P<a>[0-9a-f]{2})?$", flags=re.I
).match
def parse_hex8(text, scale=255):
mo = _match_hex8(text)
@laurent-laporte-pro
laurent-laporte-pro / dict_cc.BG.EN.tsv
Created August 28, 2019 20:15
BG-EN vocabulary database compiled by dict.cc
We can make this file beautiful and searchable if this error is corrected: It looks like row 8 should actually have 2 columns, instead of 4. in line 7.
# BG-EN vocabulary database compiled by dict.cc
# Date and time 2019-08-28 22:04
# License THIS WORK IS PROTECTED BY INTERNATIONAL COPYRIGHT LAWS!
# License Private use is allowed as long as the data, or parts of it, are not published or given away.
# License By using this file, you agree to be bound to the Terms of Use published at the following URL:
# License https://www.dict.cc/translation_file_request.php
# Brought to you by Paul Hemetsberger and the users of https://www.dict.cc/, 2002 - 2019
(а/пък) и (and) then conj
(главен) готвач {м} chef noun [gastr.] [jobs]
@laurent-laporte-pro
laurent-laporte-pro / dict_cc.DE.FR.tsv
Created August 28, 2019 20:15
DE-FR vocabulary database compiled by dict.cc
We can't make this file beautiful and searchable because it's too large.
# DE-FR vocabulary database compiled by dict.cc
# Date and time 2019-08-28 21:50
# License THIS WORK IS PROTECTED BY INTERNATIONAL COPYRIGHT LAWS!
# License Private use is allowed as long as the data, or parts of it, are not published or given away.
# License By using this file, you agree to be bound to the Terms of Use published at the following URL:
# License https://www.dict.cc/translation_file_request.php
# Brought to you by Paul Hemetsberger and the users of https://www.dict.cc/, 2002 - 2019
(absolutes) Minimum {n} [zum Leben, Rente, Lohn] portion {f} congrue [rente, salaire] noun
(akute) Leberbeschwerden {f} crise {f} de foie noun [med.]
@laurent-laporte-pro
laurent-laporte-pro / dict_cc.DE.EN.tsv
Created August 28, 2019 20:14
DE-EN vocabulary database compiled by dict.cc
We can make this file beautiful and searchable if this error is corrected: It looks like row 9 should actually have 2 columns, instead of 4. in line 8.
# DE-EN vocabulary database compiled by dict.cc
# Date and time 2019-08-28 21:44
# License THIS WORK IS PROTECTED BY INTERNATIONAL COPYRIGHT LAWS!
# License Private use is allowed as long as the data, or parts of it, are not published or given away.
# License By using this file, you agree to be bound to the Terms of Use published at the following URL:
# License https://www.dict.cc/translation_file_request.php
# Contains data from http://dict.tu-chemnitz.de/ with friendly permission by Frank Richter, TU Chemnitz
# Brought to you by Paul Hemetsberger and the users of https://www.dict.cc/, 2002 - 2019
&#945;-Keratin {n} &#945;-keratin noun [biochem.]