Skip to content

Instantly share code, notes, and snippets.

@jhidding
jhidding / cosmology.py
Created October 25, 2023 14:32
Compute growing mode solution
from scipy.integrate import quad
class Cosmology:
"""This object stores all relevant information wrt the background
cosmology, parametrized by OmegaM, OmegaL and H0."""
def __init__(self, H0, OmegaM, OmegaL):
self.H0 = H0
self.OmegaM = OmegaM
self.OmegaL = OmegaL
self.OmegaK = 1 - OmegaM - OmegaL
@jhidding
jhidding / make.jl
Last active December 4, 2022 09:41
push!(LOAD_PATH,"../src/")
using Documenter, ...
function noweb_label_pass(src, target_path)
mkpath(joinpath(target_path, dirname(src)))
script = joinpath(@__DIR__, "noweb_label_pass.awk")
run(pipeline(src, `awk -f $script`, joinpath(target_path, basename(src))))
end
@jhidding
jhidding / json_to_dataclass.py
Last active February 14, 2022 09:54
Convert a nested structure of dict and list to dataclasses
import typing
from dataclasses import is_dataclass
def isgeneric(annot):
return hasattr(annot, "__origin__") \
and hasattr(annot, "__args__")
def construct(annot, json):
@jhidding
jhidding / material_dsl.py
Last active June 17, 2020 23:33
DSL for making Blender materials
"""
Create Blender materials from Python:
from material_dsl import (BsdfPrincipled, OutputMaterial, Value, VertexColor, make_material)
color_input = VertexColor(location=(-400, 300), layer_name="my colormap")
shader = BsdfPrincipled(location=(0, 300), base_color=color_input.color)
output_material = OutputMaterial(location=(400, 300), surface=shader.BSDF)
make_material("colored mesh", output_material)
@jhidding
jhidding / blender_object_creation.py
Created June 16, 2020 10:16
Create objects in Blender using Metaclasses
import bpy
import numpy as np
from numpy import (sin, cos, pi)
def remove_from(data, name):
if name in data:
data.remove(data[name])
class BObjectMeta(type):
def __new__(cls, name, bases, dct):
@jhidding
jhidding / pi.py
Created January 16, 2020 11:54
Compute Pi using Numba and Dask
# requirements: python3, numba, dask
import random
import numba
import dask
@dask.delayed
@numba.jit(nopython=True, nogil=True)
def calc_pi(N):
@jhidding
jhidding / maybe.py
Last active January 13, 2020 13:41
A function decorator for failing computations
from functools import (wraps)
from itertools import (chain)
class Failure:
"""Signifies a failure in a computation that was wrapped by a `@maybe`
decorator."""
def __init__(self, func, fails=None, exception=None):
self.name = func.__name__
self.fails = fails
@jhidding
jhidding / flower.gnuplot
Last active May 7, 2019 20:40
Flowery patterns using polar plots in Gnuplot
# Create nice flower patterns in Gnuplot
set term svg
set output "flower.svg"
unset border
unset raxis
unset tics
unset key
set parametric
#include <stdlib.h>
#include <stdio.h>
int main() {
int i;
for (i = 0; i <= 0; ++i) {}
printf("i = %u\n", i);
return EXIT_SUCCESS;
}
@jhidding
jhidding / block3.md
Created January 22, 2019 18:08
entangled blog #3
``` {.py file=hello2.py}
if __name__ == "__main__":
    <<main-body>>
```