Skip to content

Instantly share code, notes, and snippets.

@machinaut
machinaut / divided_icosahedron.py
Last active August 19, 2023 19:27
Subdivide Icosahedrons to make nice spheres
#!/usr/bin/env python
from itertools import combinations, chain
import numpy as np
from pyhull.convex_hull import ConvexHull
from stl import Mode
from stl.mesh import Mesh
def subdivide(shape):
''' Take a triangulated sphere and subdivide each face. '''
@machinaut
machinaut / maybe.py
Created February 22, 2017 19:51
Haskell's "Maybe" types in python
#!/usr/bin/env python
# Useful when combined with: http://pythoncentral.io/validate-python-function-parameters-and-return-types-with-decorators/
class Maybe(type):
''' Metaclass to match types with optionally none. Use maybe() instead '''
maybe_type = type(None) # Overridden in derived classes
def __instancecheck__(self, instance):
return isinstance(instance, self.maybe_type) or instance is None
@machinaut
machinaut / rotate_euler.py
Last active April 1, 2019 13:56
Simple Euler Angles Rotation in python
import numpy as np
from scipy.linalg import expm
def rot_euler(v, xyz):
''' Rotate vector v (or array of vectors) by the euler angles xyz '''
# https://stackoverflow.com/questions/6802577/python-rotation-of-3d-vector
for theta, axis in zip(xyz, np.eye(3)):
v = np.dot(np.array(v), expm(np.cross(np.eye(3), axis*-theta)))
return v
@machinaut
machinaut / atest.py
Created January 24, 2017 09:57
Passing cython function pointers through python
import pyximport
import numpy as np
pyximport.install()
cmap = pyximport.load_module('cmap', 'cmap.pyx')
cadd = pyximport.load_module('cadd', 'cadd.pyx')
a = np.arange(6, dtype='i')
print('start', a)
cmap.pymap(a, cadd.pyadd_one)
print('add_one', a)
@machinaut
machinaut / logger.c
Created September 28, 2016 16:07
printf() style output through python's logger in python c extension
// logger.c - hook into python's logging module
#include <Python.h>
#include <stdarg.h>
#include "logger.h"
static PyObject *logger;
// logger = logging.getLogger('libvncdriver')
int logger_init(void) {
@machinaut
machinaut / util.py
Created August 16, 2016 20:13
Various utilities for solving project euler problems
# Useful stuff
from operator import mul
def prod(x):
""" Product reducer """
return reduce(mul, x, 1)
@machinaut
machinaut / rand.py
Created May 10, 2016 21:06
Random agent (run with environment name as argument) for OpenAI gym.
#!/usr/bin/env python
import gym
import sys
env = gym.make(sys.argv[1])
num_episodes = 200
max_timestep = 1000
env.monitor.start(sys.argv[1])
for _ in xrange(num_episodes):
@machinaut
machinaut / nchain-custom.py
Created May 6, 2016 18:35
Run a custom-parameterized openai/gym environment. (using 'nchain' environment from Pull Request #61)
#!/usr/bin/env python
import gym
import gym.envs
import numpy as np
gym.envs.register(id='NChainCustom-v0',
entry_point='gym.envs.toy_text:NChainEnv',
kwargs={'large':100},
timestep_limit=200)
@machinaut
machinaut / fm_example.grc
Created August 12, 2014 21:15
FM Receiver gnuradio example for the HackRF One SDR
<?xml version='1.0' encoding='ASCII'?>
<flow_graph>
<timestamp>Tue Aug 12 14:13:51 2014</timestamp>
<block>
<key>options</key>
<param>
<key>id</key>
<value>fm_receiver</value>
</param>
<param>
void printx(char *s, uint8_t *x, int n) {
char buf[PS];
for (int i = 0; i < PS; i++){
if(*s == '%'){
for(int j = n-1; j >= 0; j--){
buf[i++%128] = ((*(x+j)>>4)&0xf)[
"0123456789ABCDEF"
];
buf[i++%128] = (*(x+j)&0xf)[