Skip to content

Instantly share code, notes, and snippets.

"""
Provides an example on how to create a server script for blueberry.
"""
import os
import sys
from signal import SIGTERM
from optparse import OptionParser
from routes.middleware import RoutesMiddleware
import urllib2
from urllib import urlencode
class APIRequest(urllib2.Request):
"""
Hack urllib2.Request so we can use custom HTTP requests like DELETE and PUT
"""
def set_method(self, method):
self.method = method.upper()
;; this function is defined in vector_of_pairs.c and used in this file
(define vector_of_pairs (foreign-safe-lambda void "vector_of_pairs" int))
;; define the scheme callback -- note that this is extern'd in vector_of_pairs.c
(define-external (scm_cb (scheme-object x)) void
(print x))
;; call C function to generate vector of pairs, passing N into it
(vector_of_pairs 10)
#include <sys/epoll.h>
#include <fcntl.h>
#include <string.h>
#include "chicken.h"
#define MAX_EVENTS 24
extern void SCM_epoll_wait_cb(C_word vec);
(define (write-handler fd)
;; epoll tells us to write to socket
(let ((buf (hash-table-ref fd-write-table fd)))
(##net#write fd buf (string-length buf)))
;; write prompt to client after sending buf
(##net#write fd "> " 2)
;; clear out write buffer
(hash-table-set! fd-write-table fd "")
;; Is this a good way to "remove" elements from a list? It just builds a new
;; list after filtering out the element I want removed.
(define (filter-out-fd fd)
;; builds a new list after filtering out fd
(let loop ((li fd-list) (newlist '()))
(if (null? li)
newlist
(if (eq? fd (car li))
(loop (cdr li) newlist)
@davidreynolds
davidreynolds / gist:1084019
Created July 15, 2011 03:42
cg_annotate output for hashtree loading
24,522,286 1 1 16,348,190 0 0 0 0 0 while (i < node_count) {
16,348,188 0 0 8,174,094 0 0 8,174,094 0 0 z = (struct rbt_node_s *)pmap;
24,522,282 0 0 8,174,094 0 0 8,174,094 0 0 pmap += sizeof(struct rbt_node_s);
. . . . . . . . .
24,522,282 0 0 16,348,188 0 0 8,174,094 3,064,773 3,064,773 z->strkey = pmap;
57,218,658 1 1 24,522,282 510,781 510,781 8,174,094 0 0 pmap += z->vlen+1;
. . . . . . . . .
24,522,282 0 0 16,348,188 6,962,690 6,962,690 8,174,094 0 0 poff = *(long *)pmap;
24,522,282 0 0 8,174,094 0 0 8,174,094 0 0 pmap += sizeof(long);
24,522,282 1 1 16,348,188 1,021,156 1,021,156
@davidreynolds
davidreynolds / level_order.c
Created July 15, 2011 05:12
print binary tree in level order proof-of-concept
/*
* XXX: This code might not work as-is. I'm cutting and pasting the relevant
* parts from a working implementation in my hash tree project that may or may
* not get pushed to github later. That's why I'm putting it here for reference.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>
@davidreynolds
davidreynolds / simple_snapshot.c
Created August 13, 2011 05:54
Exploring copy-on-write snapshotting of a binary search tree
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#define RWRR S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
#define MAP_RW PROT_READ | PROT_WRITE
@davidreynolds
davidreynolds / boggle.c
Created June 4, 2012 00:00
Boggle Backtracking Algorithm
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define BIN_SEARCH(dict, size, needle, res, prefix) \
do { \
assert((dict) != NULL); \
assert((size) > 0); \
int l = 0, r = (size)-1; \