Skip to content

Instantly share code, notes, and snippets.

View adewes's full-sized avatar

Andreas Dewes adewes

View GitHub Profile
@adewes
adewes / natural_numbers.sql
Created August 23, 2013 11:14
Sometimes you just need an SQL table filled with some natural numbers. Here's a simple & brute-force solution that will work on ANY SQL dialect. It returns a table with an arbitrary number of natural numbers, provided this number is equal to 10.000... I used this on ExaSOL, where most Oracle / MSSQL magic does not work.
create table natural_numbers (n int);
insert into natural_numbers values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12), (13), (14), (15), (16), (17), (18), (19), (20), (21), (22), (23), (24), (25), (26), (27), (28), (29), (30), (31), (32), (33), (34), (35), (36), (37), (38), (39), (40), (41), (42), (43), (44), (45), (46), (47), (48), (49), (50), (51), (52), (53), (54), (55), (56), (57), (58), (59), (60), (61), (62), (63), (64), (65), (66), (67), (68), (69), (70), (71), (72), (73), (74), (75), (76), (77), (78), (79), (80), (81), (82), (83), (84), (85), (86), (87), (88), (89), (90), (91), (92), (93), (94), (95), (96), (97), (98), (99), (100), (101), (102), (103), (104), (105), (106), (107), (108), (109), (110), (111), (112), (113), (114), (115), (116), (117), (118), (119), (120), (121), (122), (123), (124), (125), (126), (127), (128), (129), (130), (131), (132), (133), (134), (135), (136), (137), (138), (139), (140), (141), (142), (143), (144), (145), (146), (147), (148), (149), (150), (151), (1
@adewes
adewes / workload_and_failures.py
Created July 29, 2013 15:07
Simple functions to calculate expected number of failures and workload in systems that can process a given number of subscribers in parallel, using a binomial distribution model to simulate subscriber behavior. Applicable e.g. to web servers & cellphone base stations.
from scipy.stats import binom
def workload(n_subscribers,n_capacity,p_call):
"""
Calculates the estimated workload (in percent) of a system to which n_subscribers connect at any given time with probability *p_call* and that can process *n_capacity* clients in parallel.
Arguments:
n_subscribers : The number of subscribers that might use the system at the given time.
n_capacity : The maximum number of subscribers that the system can process in parallel.
@adewes
adewes / redis_lock.py
Last active December 20, 2015 08:49
A Redis-based distributed lock class, based on the solution proposed by Chris Lamb (https://chris-lamb.co.uk/posts/distributing-locking-python-and-redis).
import redis
import time
class LockTimeout(BaseException):
pass
class Lock(object):
"""
Implements a distributed lock using Redis.
@adewes
adewes / data.js
Last active December 12, 2015 03:58
Hierarchical Pie Chart
code_hierarchy_data = [
"",
[
416598,
11581
],
{
"docs": [
"docs",
[
@adewes
adewes / Makefile
Created November 12, 2015 10:28
The build watcher: Automatically rebuild Pelican content as files change.
# Changes gather, and now my watch begins.
# It shall not end until my death.
# I shall live and die at my post.
# I am the process in the darkness.
# I am the watcher on the files.
# I am the shield that guards the realms of the build dir.
watch:
@which inotifywait || (echo "Please install inotifywait";exit 2)
@while true ; do \
@adewes
adewes / find_circular_dependencies.py
Last active September 16, 2015 16:48
Finding circular dependencies in your SQLAlchemy data scheme
from collections import defaultdict
dependencies = defaultdict(set)
#use your metadata object here
for t in metadata.sorted_tables:
for fkey in t.foreign_key_constraints:
dependencies[t.name].add(fkey.referred_table.name)
### Keybase proof
I hereby claim:
* I am adewes on github.
* I am dewes (https://keybase.io/dewes) on keybase.
* I have a public key whose fingerprint is 3BCA 20E5 94D4 910B 070E 25D6 8E2F D9A8 CBA7 BA57
To claim this, I am signing this object:
@adewes
adewes / export.py
Created April 9, 2014 17:11
Python: Dictionary Value Exporter
def export(d,key):
"""
Exports a set of values from a nested dict.
Example usage:
d = {
'name' : 'test',
'remote_servers' :
@adewes
adewes / hacker_school_effect.ipynb
Created March 11, 2014 04:34
The Hacker School Effect
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@adewes
adewes / eight_queens.py
Last active August 29, 2015 13:56
Acht Königinnen
board_size = 8
def get_free_states(queens):
"""
Get free board states for a set of queens
"""
free_states = [[True]*board_size for i in range(0,board_size)]
for queen in queens:
for i in range(0,board_size):
for signs in [(1,1),(-1,-1),(1,-1),(-1,1),(1,0),(-1,0),(0,1),(0,-1)]: