Skip to content

Instantly share code, notes, and snippets.

View remram44's full-sized avatar

Remi Rampin remram44

View GitHub Profile
@remram44
remram44 / matrix_to_quaternion.c
Created March 28, 2013 04:20
matrix to quaternion
/* quaternion from matrix */
/* from Id Software, From Quaternion to Matrix and Back, 2005 */
float ReciprocalSqrt(float x)
{
long i;
float y, r;
y = x * 0.5f;
i = *(long*)&x;
i = 0x5f3759df - (i >> 1);
@remram44
remram44 / README.md
Created April 4, 2013 14:11
Multiprocessing example

Introduction

This is an example that shows the differences in multiprocessing execution between POSIX systems (where fork() is available) and Windows (where multiprocessing does some magic to recreate the Python state in spawned interpreters, namely, reimporting the main module as well as the one where the function lives). It also shows that environment variable are inherited.

Results

On Windows (no fork())

main running as __main__
func running
# S -> aSa | aa
def S(s, pos, depth):
orig = pos
# Try aSa
print "%strying aSa @%d" % (' '*depth, pos)
pos = a(s, pos, depth+1)
if pos is not None:
pos = S(s, pos, depth+1)
if pos is not None:
pos = a(s, pos, depth+1)
@remram44
remram44 / debugmt.py
Last active December 18, 2015 00:49
Debug metaclass listing calls to methods
import contextlib
pad = ''
@contextlib.contextmanager
def call():
global pad
pad += " "
yield
@remram44
remram44 / instancemethod.py
Last active December 18, 2015 03:29
Implicit self
import dis
import struct
import types
class instancemethod(object):
def __init__(self, func):
code = func.func_code
bytecode = code.co_code
@remram44
remram44 / pickleable_staticmethods.py
Created June 12, 2013 22:21
Allows pickling staticmethods through a metaclass
# From http://stackoverflow.com/a/1914798/711380
class _PickleableStaticMethod(object):
def __init__(self, fn, cls=None):
self.cls = cls
self.fn = fn
def __call__(self, *args, **kwargs):
return self.fn(*args, **kwargs)
def __get__(self, obj, cls):
return _PickleableStaticMethod(self.fn, cls)
def __getstate__(self):
@remram44
remram44 / sparks.py
Last active December 18, 2015 14:49 — forked from stefanv/sparks.py
#!/usr/bin/python
# coding=utf-8
# Python version of Zach Holman's "spark"
# https://github.com/holman/spark
# by Stefan van der Walt <[email protected]>
"""
USAGE:
@remram44
remram44 / cplusplus.cpp
Last active September 22, 2024 02:26
Python's versus Lua's closures
#include <functional>
#include <iostream>
#include <vector>
int main()
{
std::vector<std::function<int (int)>> l;
for(int i = 0; i < 10; ++i)
l.push_back([=](int x) { return i + x; });
std::cout << l[2](3) << std::endl; // 5
#include <functional>
std::function<int (int)> foo(int n)
{
return [=](int x) mutable { return n += x; };
}
@remram44
remram44 / cplusplus.cpp
Last active December 18, 2015 19:39
Closures (2)
#include <functional>
#include <iostream>
#include <vector>
int main()
{
std::vector<std::function<int (int)>> l;
for(int i = 0; i < 10; ++i)
l.push_back([=](int x) mutable { return i += x; });
std::cout << l[2](3) << ", "; // 5