Skip to content

Instantly share code, notes, and snippets.

View snorfalorpagus's full-sized avatar

Joshua Arnott snorfalorpagus

View GitHub Profile
@snorfalorpagus
snorfalorpagus / upload.py
Created February 5, 2017 12:14
Flask upload fiona
import os
from flask import Flask, request, redirect, url_for, flash
from werkzeug.utils import secure_filename
import uuid
UPLOAD_FOLDER = os.path.join(os.path.dirname(__file__), "uploads")
app = Flask(__name__)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
@snorfalorpagus
snorfalorpagus / pywr_concurrent.py
Created January 20, 2017 18:12
Pywr + concurrent.futures
import concurrent.futures
from pywr.model import Model
from pywr.nodes import Input, Output
def run_model(demand):
model = Model()
i = Input(model, "input", max_flow=15)
o = Output(model, "output", max_flow=demand, cost=-1)
i.connect(o)
@snorfalorpagus
snorfalorpagus / python35_mingw64.md
Last active January 17, 2017 15:02
Building Python 3.5 (32-bit) modules with Anaconda and MINGW64 (without MSVC 2015)

Building Python 3.5 (32-bit) modules with Anaconda and MINGW64 (without MSVC 2015)

To create 32-bit anaconda environment on 64-bit install:

set CONDA_FORCE_32BIT=1
conda create -n py35_32 python=3.5

To install mingw64, don't use mingw package (deprecated). Intsead:

import fiona
from shapely.geometry import shape, mapping, LineString, MultiLineString
from copy import deepcopy
def split_segments(geometry, length):
"""Split a LineString into segments
Parameters
----------
geometry : shapely geometry
"""
This example converts all of the MapInfo layers in a folder into ESRI Shapefiles.
This is complicated a little by the fact that MapInfo geometries are not
homogenous, i.e. you can have different kinds of geometry (LineString, Polygon, ...)
in the same file. This approach assumes that all of the geometries have the same
type as the first feature in the layer.
"""
import os
import fiona
@snorfalorpagus
snorfalorpagus / sqlalchemy_nested_example.py
Created April 6, 2016 16:44
SQLAlchemy nested transactions
"""
This script demonstrates the use of nested transactions in SQLAlchemy, including
a workaround for an issue with the SQLite backend.
References:
http://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#using-savepoint
http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html#serializable-isolation-savepoints-transactional-ddl
"""
from sqlalchemy import Column, String, Integer
@snorfalorpagus
snorfalorpagus / example.c
Created March 8, 2016 17:50
GLPK minimum flow problem
/*
Simple example of minimum cost flow problem in GLPK
To build and run:
$ gcc -o example -lglpk example.c
$ ./example
ret = 0; sol = 25
flow = 5
*/
@snorfalorpagus
snorfalorpagus / example.json
Created February 19, 2016 22:47
JSON pywr
{
"pywr": {
"metadata": {
"title": "JSON example",
"description": "An example of how models could be stored in JSON."
},
"scenarios": {
"demand_scenario": "demand_scenario"
},
"structure": {
Date Flow
01/01/1990 26.42
02/01/1990 30.94
03/01/1990 40.23
04/01/1990 41.95
05/01/1990 37.42
06/01/1990 33.85
07/01/1990 31.49
08/01/1990 37.31
09/01/1990 49.15
@snorfalorpagus
snorfalorpagus / hello.pyx
Last active February 10, 2016 21:37
Cython hello world
def say_hello_to(name):
print("Hello %s!" % name)