Skip to content

Instantly share code, notes, and snippets.

@jhidding
jhidding / block2.md
Last active January 22, 2019 18:06
entangled blog #2
``` {.py file=hello.py}
print("Hello, World!")
```
@jhidding
jhidding / block1.md
Last active January 22, 2019 17:27
block1.md
```py
print("Hello, Amsterdam!")
```
@jhidding
jhidding / mesh_to_obj.cc
Created September 15, 2018 15:50
Write a CGAL mesh to wavefront OBJ
/* This takes a C2t3 from example 3.1 in CGAL manual on meshes.
The `vertices_begin()` method returns non-const iterator, so
we pass non-const reference. This should be fixed somehow.
*/
void write_to_obj(std::ostream &out, C2t3 &mesh)
{
// create a map from Vertex_handle to the vertex index as it is
// written into the OBJ file. Remember that OBJ indices start from 1.
std::map<C2t3::Vertex_handle, unsigned> vertex_map;
unsigned count = 1;
@jhidding
jhidding / downsample.sh
Created May 9, 2018 08:26
downsample a PDF file using ghostscript
#!/bin/bash
# Copyright 2018 Johan Hidding
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
@jhidding
jhidding / solution.scm
Last active March 21, 2018 19:49
bridge-zombie-problem
#| Problem statement
| =================
|
| You, a lab assistant in a remote mountain facility, accidentally release a
| horde of zombies. You, the lab technician, professor and student intern need
| to flee the laboratory across a rope bridge. The bridge carries a maximum of
| two people, it's dark and you only managed to grab one lantern.
|
| Each of you takes a different time to cross the bridge:
|
@jhidding
jhidding / etu1.py
Last active March 6, 2018 10:11
expecting-the-unexpected
def something_dangerous(x):
print("computing reciprocal of", x)
return 1 / x
try:
for x in [2, 1, 0, -1]:
print("1/{} = {}".format(x, something_dangerous(x)))
except ArithmeticError as error:
print("Something went terribly wrong:", error)
@jhidding
jhidding / nlesc-logo.py
Created December 15, 2017 11:50
This Python snippets prints the logo of the Netherlands eScience center in full colour UTF-8
print("\033[47;30m Netherlands\033[48;2;0;174;239;37m▌\033[38;2;255;255;255me\u20d2Science\u20d2\033[37m▐\033[47;30mcenter \033[m")
@jhidding
jhidding / workon.sh
Created July 19, 2017 15:20
A bash command for creating and activating Python virtual envs
export WORKON_PATH="${HOME}/.local/share/workon"
function workon() {
source ${WORKON_PATH}/${1}/bin/activate
}
function create-python3-env() {
py3-virtualenv ${WORKON_PATH}/${1}
}
@jhidding
jhidding / easy-docker-example.py
Last active February 15, 2017 12:26
Example of easy-docker decoding a secret message
sed_program = """# usage: sed -f rot13.sed <filename>
y/abcdefghijklmnopqrstuvwxyz/nopqrstuvwxyzabcdefghijklm/
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/NOPQRSTUVWXYZABCDEFGHIJKLM/
"""
message = "Vf gurer nalobql BHG gurer?"
with DockerContainer('busybox') as c:
c.put_archive(
Archive('w')
@jhidding
jhidding / easy-docker.py
Created February 3, 2017 10:03
An easier interface to the Docker API
class DockerContainer(object):
"""Easy interface to Docker API.
This class encapsulates a part of the Docker API, with the goal
of making it easier to start a container, add some files, run a
few scripts, read the output, and then remove the container
again.
The object is a context manager for the created Docker container,
in that it starts the container upon entry and exterminates the