Skip to content

Instantly share code, notes, and snippets.

View gnn's full-sized avatar

Stephan Günther gnn

View GitHub Profile
@gnn
gnn / caching.py
Created November 25, 2022 15:31
Explore the different caching possibilities in Python
"""Illustrate some strategies to cache potentially expensive results.
Sometimes results are obtained via code which has side effects and/or is
potentially expensive so you don't want to run it more often than
necessary. This script demonstrates a few different ways of caching
those results at the use site, so they only need to be generated once
and can be read from whatever is used as a cache afterwards.
"""
# First we need a function having an observable side effect, so we can
@gnn
gnn / filter.py
Last active February 23, 2021 11:39
Filter CSV lines using a shapefile
#! /usr/bin/env python
from pathlib import Path
import csv
import json
from shapely.geometry import Point, shape
from shapely.prepared import prep
from egon.data import subprocess
@gnn
gnn / rasterize.py
Last active January 19, 2021 12:18
Read in CSV data and rasterize it
from functools import partial
import json
from geocube.api.core import make_geocube
from geocube.rasterize import rasterize_image
from rasterio.enums import MergeAlg
from shapely.wkt import loads
from shapely.geometry import box, mapping, MultiPoint
import pandas as pd
import geopandas as gpd
@gnn
gnn / extract-zipfile.py
Last active December 1, 2020 13:34
Using zipped files with Python
import os
import zipfile
with zipfile.ZipFile(PATH_TO_ZIPFILE) as zf:
for filename in zf.namelist():
zf.extract(filename)
# Do stuff with `filename`.
os.remove(filename)
@gnn
gnn / results.md
Last active November 17, 2020 15:49
The results of the model development coordination poll.

Topic Partitioning Votes

These are the most significant polling results:

Partitioning Votes
2, 3 5
7, 9 5
5, 6 4
10 4
@gnn
gnn / grid.geojson
Created April 2, 2020 11:49
The grid used by open_FRED
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@gnn
gnn / grid.geojson
Last active April 2, 2020 11:31
Geojson files generated with open_FRED's grid
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@gnn
gnn / tests.rst
Last active January 13, 2021 09:45
Try out various reStructuredText features and whether and how they are rendered on GitHub

Math

Here's a math block. Let's see how it's rendered.

$$\sum_{n=1}^{\infty}$$

Apparently not at all. So what about the same thing inlined:

@gnn
gnn / sorting.dol
Last active August 18, 2016 17:03
A proposed fix for the "attempt to hide ... from the local environment" error
spec Sorting =
TotalOrder and List
then {
preds is_ordered : List;
permutation : List * List
vars x,y:Elem; L,L1,L2:List
. is_ordered([])
. is_ordered(x::[])
. is_ordered(x::y::L) <=> x<=y /\ is_ordered(y::L)
. permutation(L1,L2) <=> (forall x:Elem . count(x,L1) = count(x,L2))
@gnn
gnn / invest_test.py
Last active May 11, 2016 23:02 — forked from uvchik/invest_test.py
test the invest
from oemof.network import Source
class Investment:
""" Components (Nodes?) grouped under `Investment` objects are optimized
for investment.
"""
def __init__(self, maximum=float('+inf')):
self.maximum = maximum