Skip to content

Instantly share code, notes, and snippets.

View adewes's full-sized avatar

Andreas Dewes adewes

View GitHub Profile
@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)]:
@adewes
adewes / mapreduce.py
Last active March 29, 2018 13:06
A map-reduce class in Python, with the typical "hello, world!" word-counting example. You can download ulysses.txt file used in the example here: http://www.gutenberg.org/ebooks/4300
from collections import defaultdict
import abc
class MapReducer(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def map(self,items):
return []
@adewes
adewes / get_active_window.py
Last active February 28, 2024 23:26
Get the active window in Gtk using the wnck library.
import wnck
import gtk
import time
if __name__ == '__main__':
screen = wnck.screen_get_default()
screen.force_update()
while True:
while gtk.events_pending():
gtk.main_iteration()
@adewes
adewes / jsonp.py
Created September 17, 2013 13:22
A Flask decorator that takes a response with JSON content and converts it to a well-formatted JSONP response.
def jsonp():
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if not 'callback' in request.args:
abort(404)
response = f(*args, **kwargs)
if isinstance(response,str):
@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 / get_all_github_users.py
Created July 26, 2013 09:47
Scripts for getting the full list of Github users (get_all_github_users.py) and for retrieving the details of these users (get_github_user_details.py) using the Github API.
import requests
import json
import datetime
import time
import sys
import math
ACCESS_TOKEN = '[put your API key here]'
usage = """Retrieves a list of all Github users using the Github API.
@adewes
adewes / generate_random_color.py
Last active March 26, 2024 08:18
A small Python script to generate random color sequences, e.g. for use in plotting. Just call the "generate_new_color(existing_colors,pastel_factor)" function to generate a random color that is (statistically) maximally different from all colors in "existing_colors". The "pastel_factor" parameter can be used to specify the "pasteliness"(?) of th…
import random
def get_random_color(pastel_factor = 0.5):
return [(x+pastel_factor)/(1.0+pastel_factor) for x in [random.uniform(0,1.0) for i in [1,2,3]]]
def color_distance(c1,c2):
return sum([abs(x[0]-x[1]) for x in zip(c1,c2)])
def generate_new_color(existing_colors,pastel_factor = 0.5):
max_distance = None
@adewes
adewes / data.js
Last active December 12, 2015 03:58
Hierarchical Pie Chart
code_hierarchy_data = [
"",
[
416598,
11581
],
{
"docs": [
"docs",
[