Skip to content

Instantly share code, notes, and snippets.

View dutc's full-sized avatar

James Powell dutc

View GitHub Profile
@dutc
dutc / resource_limits.py
Last active August 29, 2015 14:04
runtime resource limits (for your IPython notebook)
# do this first:
! sudo apt-get install cpulimit
from os import getpid
from resource import setrlimit, RLIMIT_RSS, RLIM_INFINITY, getrusage, RUSAGE_SELF
# limit CPU: use only 1% of 1 CPU
pid = getpid()
! cpulimit -b -p $pid -c 1 -l 1
@dutc
dutc / __ipynb__.py
Last active August 29, 2015 14:04
import IPython notebooks as modules
#!/usr/bin/env python
from imp import new_module
class IPythonModule(type(new_module(''))):
from json import loads
from imp import new_module
loads = staticmethod(loads)
new_module = staticmethod(new_module)
def __init__(self, name, path):
@dutc
dutc / newton.py
Last active August 29, 2015 14:05
Netwon's method (or any other iterative solving technique) with numpy
from numpy.random import randint
from numpy import sqrt
# Newton's method:
# `x' is input, `y' is iteratively refined answer
# solve for square root of x: y**2 = x
# f (y) = y^2 - x
# f'(y) = 2y
# y[1] = y[0] - f(y[0]) / f'(y[0])
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dutc
dutc / let.py
Last active May 27, 2019 03:29
Let-Statement in Python
# proposal 1: re-use `class`-statement and just hook into return value in build_class
let result:
x, y = 10, 20
return x * y
assert result == 30
# proposal 2: not sure the exact steps, but shouldn't be much more difficult than above
result = foo.x * foo.y where foo:
x, y = 10, 20
@dutc
dutc / py₃.so₂.py
Last active April 3, 2020 01:51
py₃.so₂ example
#!/usr/bin/env python3
from cffi import FFI
from textwrap import dedent
from os import getpid
from sys import version_info
if __name__ == '__main__':
from sys import argv
print('Host is: %s (%s)' % (version_info, getpid()))
@dutc
dutc / Makefile
Last active August 29, 2015 14:08
testing dlmopen
CC=gcc -std=c99 -Wall
dlmopen-test: dlmopen-test.c
${CC} -o $@ $^ -ldl
@dutc
dutc / dlopen.c
Created October 22, 2014 17:45
replace dlopen with dlmopen
#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
# define LM_ID_BASE 0 /* Initial namespace. */
# define LM_ID_NEWLM -1 /* For dlmopen: request new namespace. */
void __attribute__ ((constructor)) init(void);
@dutc
dutc / cffi-test.py
Last active September 2, 2021 23:41
cffi version of dlmopen CPython embedding
#!/usr/bin/python
from cffi import FFI
from textwrap import dedent
from sys import exit
if __name__ == '__main__':
ffi = FFI()
ffi.cdef('''
void *dlmopen (long int __nsid, const char*, int);
void *dlopen (const char*, int);
@dutc
dutc / dedent.py
Created October 23, 2014 00:12
dedenting helpers for use with vim
#!/usr/bin/env python
from textwrap import dedent
if __name__ == '__main__':
from sys import stdin
exec dedent(''.join(stdin)) in locals(), globals()