Skip to content

Instantly share code, notes, and snippets.

View gjcourt's full-sized avatar

George Courtsunis gjcourt

View GitHub Profile
@gjcourt
gjcourt / prettify.py
Created October 11, 2011 21:52
prettify number
def prettify(value):
decimal, integer = math.modf(value)
decimal = '%0.2f' % decimal
decimal = decimal[decimal.index('.'):]
sign = '' if integer >= 0 else '-'
integer = abs(int(integer))
result = (',' if integer > 1000 else '') + str(integer % 1000)
while integer / 1000 > 1:
integer /= 1000
result = (',' if integer > 1000 else '') + str(integer % 1000) + result
/**
* PROMPT
* You are given an array named data that represents posts in proper sorted
* order. Each element in the array represent post data, and is itself an
* array. The first element for the post array represents the post id, the
* second element is the parent post id, and the third is the message. If
* the parent post id is 0, it's a top level comment. Construct a simple
* HTML representation of our comment thread using <ul> and <li> elements
* from this array of posts making sure to keep track of proper depth.
*/
for site, sns in _map.iteritems():
counts_total = [0] * 5
for sn in sns:
counts = [[int(c) for c in l.split('\t')[1:]] for l in website_map[sn]]
for count in counts:
counts_total = [sum(c) for c in zip(counts_total, count)]
print '%s\t%s' % (site.strip(), '\t'.join([str(c) for c in counts_total]))
@gjcourt
gjcourt / tail.py
Created February 6, 2012 22:37
Read last line of a file
def read_last_line(fd):
"""
Efficiently read the last line of a file by seeking to the
end of the file and looping backwards until the first linefeed
character is encountered. Ignores trailing newlines at the EOF
"""
# seek to the end of the file
fd.seek(0, 2)
size = fd.tell()
if size == 0:
@gjcourt
gjcourt / rebalance.sh
Created April 27, 2012 16:15
Cassandra node rebalancing script
#!/bin/sh
# This will allow you to rebalance the Cassandra ring
# Accepts hosts from stdin and automatically rebalances
# tokens in your ring.
#
# $ echo "one two three" | ./rebalance.sh
RING_SIZE=$(echo "2^127" | bc)
@gjcourt
gjcourt / latency.txt
Created May 31, 2012 18:50 — forked from jboner/latency.txt
Latency numbers every programmer should know
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns
Mutex lock/unlock 25 ns
Main memory reference 100 ns
Compress 1K bytes with Zippy 3,000 ns
Send 2K bytes over 1 Gbps network 20,000 ns
Read 1 MB sequentially from memory 250,000 ns
Round trip within same datacenter 500,000 ns
Disk seek 10,000,000 ns
@gjcourt
gjcourt / rolling_mean.py
Created July 26, 2012 00:24
Rolling mean
class RollingMean(object):
count = 0
average = 0
@property
def val(self):
return self.average
def add(self, value):
self.average = (value + self.count * self.average) / (self.count + 1)
self.count += 1
return self.average
(ns example.storm.clj.spout.kafka-spout
(:import ; [example.storm.spout UnreliableKafkaSpout]
[storm.kafka HostPort KafkaSpout SpoutConfig StringScheme]))
(def ^:dynamic *kafka-hosts* ["kafka-1.example.net"
"kafka-2.example.net"
"kafka-3.example.net"])
(def ^:dynamic *kafka-ports* [9093
9094
(defbolt split-category ["category" "event"]
[tuple collector]
(let [event (.getString tuple 0)
category "testing_category2"]
(comment
(emit-bolt! collector [category event] :anchor tuple)
)
(emit-bolt! collector [category event] :anchor tuple :stream "3")
(ack! collector tuple)))
get foo
set foo foo
get bar, via property
set bar bar