Skip to content

Instantly share code, notes, and snippets.

@zed
zed / amit.c
Created October 15, 2011 02:32
remove duplicates from an array without preserving the order of elements in C
/**
gcc -std=c99 -Wall -g *.c && ./a.out
*/
#include <assert.h>
#include <iso646.h> // and, or
#include <stdio.h> // putchar
#include <stdlib.h> // size_t
#include <string.h> // strcspn
#include <stddef.h> // ptrdiff_t
@zed
zed / coroutine_example.py
Created October 15, 2011 23:06
Translation of "Coroutines in C" in Python
#!/usr/bin/env python
"""Translation of "Coroutines in C" in Python
http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
"""
EOF = b''
def decompressor(getchar):
for c in iter(getchar, EOF):
@zed
zed / daikini.py
Created October 16, 2011 16:09
Find coords that are in a +/- step in sqlite
#!/usr/bin/env python
import random
import sys
import sqlite3
from pprint import pprint
try:
import numpy as np
from itertools import izip
@zed
zed / allessandro.py
Created October 21, 2011 15:53
store multiple images in a single file using sqlite3 in Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import glob
import os
import sqlite3
# support both Python 2.x and 3.x
try: buffer = buffer
except NameError:
buffer = lambda x: x # on Python 3.x 'rb' mode already returns what we need
CFLAGS = -O2 -g \
-D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 \
-std=c99 -Wall -Wextra -pedantic \
-Wunused -Wimplicit -Wshadow -Wformat=2 \
-Wmissing-declarations -Wno-missing-prototypes -Wwrite-strings \
-Wbad-function-cast -Wnested-externs -Wcomment -Winline \
-Wchar-subscripts -Wcast-align \
-Wsequence-point \
LDFLAGS =
@zed
zed / .gitignore
Created November 5, 2011 23:45
returning functions in c++
/a.out
@zed
zed / urllib2_ssl.py
Created November 8, 2011 05:09
SSL client/server certificates verification for `urllib2`. :python:ssl:
"""SSL client/server certificates verification for `urllib2`.
It works on Python 2.6, 2.7, 3.1, 3.2
It also works on Python 2.4, 2.5 if `ssl` is installed (``pip install ssl``)
Example::
>>> import urllib2, urllib2_ssl
>>> opener = urllib2.build_opener(urllib2_ssl.HTTPSHandler(
... key_file='clientkey.pem',
@zed
zed / anonymous-struct.cc
Created November 9, 2011 23:20
anonymous struct as a return type in c++11
int main()
{
int mx = [] () -> struct { int x, y ; } { return { 99, 101 } ; } ().x ;
}
// gcc -std=c99 -Wall -pedantic -Wextra *.c -lreadline && ./a.out
#include <limits.h> // INT_MAX
#include <stdio.h>
#include <stdlib.h> // strtol
#include <readline/readline.h>
// space = \s+
// skip one or more ' ' characters
// return a pointer to the first non-' ' character or NULL on error
@zed
zed / test.sh
Created November 13, 2011 09:37
getuid() vs. geteuid() in Python
# checkout pyintaller
##svn co http://svn.pyinstaller.org/trunk pyinstaller && cd pyinstaller
# make Python script that prints uid, euid
printf '#!/usr/bin/env python\nimport os\nprint(os.getuid(),os.geteuid())\n' >print-uid.py
# create stand-alone executable
python pyinstaller.py -F print-uid.py
# change ownership to root