Skip to content

Instantly share code, notes, and snippets.

serve_file(Response, File) ->
case file:open(File, [raw, binary]) of
{ok, IoDevice} ->
%% Set ChunkSize to an optimal value
ChunkSize = 1024,
Stream = iodevice_stream(IoDevice, ChunkSize),
Response1 = setelement(4, Response, Stream),
file:close(IoDevice),
Response1;
_ ->
# a heavy int is one where the average of the digits is greater than 7
# eg: 8678 is heavy because (8 + 6 + 7 + 8) / 4 = 7.25
# 8677 is not heavy because ( 8 + 6 + 7 + 7) / 4 = 7
def is_heavy(my_number, heaviness=7):
# map each digit to a float and divide by the length
digits = str(my_number)
return sum(map(float, digits)) / len(digits) > heaviness
import time
class TimedOperation(object):
"Describes a single operation with timing information"
__slots__ = ('description', '_cpustart', '_cpuend', '_wallstart', '_wallend')
def __init__(self, description):
self.description = description
self._cpustart = time.clock()
self._cpuend = None
@hntrmrrs
hntrmrrs / longdrama.py
Created January 31, 2011 12:27
Unexpected behaviour when subclassing long (Python 2.6.6)
import sys
class Foo(object):
def __init__(self, value):
self._value = value
def __long__(self):
return long(self._value)
@hntrmrrs
hntrmrrs / unicode_error.py
Created May 6, 2011 16:04
pika issue #34
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
# Fix import paths
base_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(base_dir)
import pika
@hntrmrrs
hntrmrrs / blocking_close.py
Created May 9, 2011 17:56
Closing a channel with BlockingConnection blocks forever
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
# Fix import paths
base_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(base_dir)
import pika
@hntrmrrs
hntrmrrs / bindingload.py
Created May 13, 2011 14:56
Load-testing for exchange to exchange bindings in RabbitMQ
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Note: Requires https://github.com/skarab/pika/tree/rabbitmq-ext
# for RabbitMQ exchange binding extensions
import csv
import getopt
import os
import random
import sys
import time
% Entry point from Misultin
ws_loop(Ws) ->
WsPid = self(),
{ok, Pid, Uuid} = erlsio_session_sup:start_child(?MODULE, [WsPid]),
% This is OK because the child session process will time out if we
% die before this link.
true = link(Pid),
% First step is to send session ID
Ws:send(erlsio_protocol:encode({text, Uuid})),
?MODULE:ws_loop_1(#loop_state{ws = Ws, pid = Pid, uuid = Uuid}).
@hntrmrrs
hntrmrrs / gist:1022828
Created June 13, 2011 14:04
Basic usage of BF_cfb64_encrypt
#include <stdio.h>
#include <string.h>
#include <openssl/blowfish.h>
#include <openssl/evp.h>
int main(int argv, const char **argc)
{
const unsigned char keytext[] = "key";
const unsigned char plaintext[] = "this is plaintext";
const unsigned char ivec[] = "12345678";
open_connection_link(Params) ->
process_flag(trap_exit, true),
{ok, Conn} = amqp_connection:start(network, Params),
link(Conn),
receive
{'EXIT', Conn, Reason} ->
exit(Reason);
{'EXIT', _Pid, Reason} ->
ok = amqp_connection:close(Conn),
exit(Reason)