Skip to content

Instantly share code, notes, and snippets.

@ehmo
ehmo / gist:1349819
Created November 9, 2011 00:10
Request a new IP address from TOR in Python
def NewTorIP():
s = socket.socket()
s.connect(('localhost', 9051))
s.send("AUTHENTICATE\r\n")
r = s.recv(1024)
if r.startswith('250'):
s.send("signal NEWNYM\r\n")
r = s.recv(1024)
if r.startswith('250'):
return True
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import time
n = 10000000
t0 = time.time()
l = range(n)

Keybase proof

I hereby claim:

  • I am ehmo on github.
  • I am turek (https://keybase.io/turek) on keybase.
  • I have a public key whose fingerprint is 97D9 5F5C 45AF 0D22 AF72 8CA8 6D15 F112 A4FE 9431

To claim this, I am signing this object:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US')
'en_US'
>>> = locale.format("%d", 123456789, grouping=True)
123,456,789
>>> locale.setlocale(locale.LC_ALL, 'sk_SK')
'sk_SK'
>>> locale.format("%d", 123456789, grouping=True)
123 456 789
@ehmo
ehmo / base62.go
Last active August 29, 2015 14:22
Implementation of an URL shortener using base62 encoding in Go
package main
import (
"fmt"
"strings"
)
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Integer power: compute a**b using binary powering algorithm
@ehmo
ehmo / test.py
Created March 30, 2016 15:00
Regex in lxml
script = webpage.xpath(
"//script[re:match(text(), 'params')]",
namespaces={
"re": "http://exslt.org/regular-expressions"
}
)[0].text
@ehmo
ehmo / fibonacci.sql
Created August 11, 2016 01:19
Fast fibonacci function in pure SQL
# Fast fibonacci function in pure SQL
# Tested on Postgresql 9.5
CREATE OR REPLACE FUNCTION _fib (
n integer, a bigint, b bigint
) RETURNS bigint AS $$
BEGIN
IF n < 1 THEN
RETURN b;
END IF;
-- Super fast quasi fibonacci function for postgresql
-- Returns rounded big integer
CREATE OR REPLACE FUNCTION fib (
n integer
) RETURNS bigint AS $$
BEGIN
IF n > 91 THEN
RAISE EXCEPTION 'Bigint overflow past fib(91) [%]', n;
END IF;
// define constants for bytesize
type ByteSize float64
const (
_ = iota
KB ByteSize = 1 << (10 * iota)
MB
GB
TB
CREATE OR REPLACE FUNCTION citus_run_on_3_colocated_placements(table_name1 regclass,
table_name2 regclass,
table_name3 regclass,
command text,
parallel bool default true,
OUT nodename text,
OUT nodeport int,
OUT shardid1 bigint,
OUT shardid2 bigint,
OUT shardid3 bigint,