Skip to content

Instantly share code, notes, and snippets.

View neutrinoceros's full-sized avatar
🎯
Focusing

Clément Robert neutrinoceros

🎯
Focusing
View GitHub Profile
@neutrinoceros
neutrinoceros / repl2script.py
Last active January 21, 2024 15:59
A script I use to cleanup Python "scripts" provided in form of copy-pasted REPL sessions
# Python 3.8 is required, but this was only tested manually with 3.12
from __future__ import annotations
import argparse
import re
import sys
from dataclasses import dataclass
from functools import cached_property
from pathlib import Path
@neutrinoceros
neutrinoceros / test_shearingbox_adv.py
Created June 1, 2023 13:43
shearing box point advection benchmark
"""
Implementation examples and tests for the shearing-box boundary advection scheme of point-like particles.
"""
import numpy as np
import pytest
# half box size (best avoid integer, or "round-looking" values)
dy2 = 0.55
@neutrinoceros
neutrinoceros / write_vtk.py
Last active June 13, 2023 09:33
Write vtk files in pure Python + numpy
"""
A simple example for writing a valid VTK file in pure Python + numpy (no extra requirements)
"""
import numpy as np
def main(x, y, z, fields, filename, subheader=""):
nx, ny, nz = shape = (len(_) for _ in (x, y, z))
ntot = np.product([_-1 for _ in shape]) # number of cells
with open(filename, 'wb') as fh:
# two lines of header

This is a personal note on the migration process to port a yt frontend into its own extension package so that it can be developed outside the main code base.

Steps

A working repo structure

The following structure isn't the only possible one, but it's designed to make migration to the core yt codebase easiest once the frontend is mature and battle tested.

I'll use dummy as a fake frontend name here, just replace it with whatever fits yours. Note that the source directory has to be named yt_<something> to comply with yt's extensions importing rules.

@neutrinoceros
neutrinoceros / function_signature_changes.py
Last active October 23, 2021 11:27
Python function decorators to progressively evolve function signatures over a deprecation cycle
import warnings
from functools import wraps
from types import FunctionType
from typing import Dict
def future_keyword_only(positions2names:Dict[int, str]):
"""Warn users when using a future keyword-only argument as positional.
@neutrinoceros
neutrinoceros / typing_changes.py
Last active September 19, 2021 14:34
Simple Python typing changes from Python 3.7
# in Python 3.6
from typing import Dict, List, Tuple
def eggs(a: int, b: List[int]) -> Dict[str, Tuple[int, ...]]:
...
# in Python 3.7+, we can use builtin types instead of their typing equivalents
# this becomes the default in Python 3.9
# see https://www.python.org/dev/peps/pep-0585/
from __future__ import annotations
def eggs(a: int, b: list[int]) -> dict[str, tuple[int, ...]]: