Skip to content

Instantly share code, notes, and snippets.

View adewes's full-sized avatar

Andreas Dewes adewes

View GitHub Profile
@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 / README.md
Last active March 8, 2024 21:58
Borg Backup - Shell script to create regular backups (can e.g. be called hourly by cron).

Borg Backup Script

This is a little helper script to regulary back up your data using the Borg backup tool. I wrote this as I was frustrated with Deja-Dup, which does not allow me to exclude directories by pattern, hence I ended up with backups that were very large and took a really long time to create because they contained many directories with non-essential files (e.g. node modules or Python virtual environments). Borg backup is a simple tool that offers everything that Deja-Dup does and is easier to customize.

  • Put the script in your home folder and make it executable.
  • Call it regularly using e.g. cron (put e.g. "0 * * * * [your username] /home/[your username]/make_backup.sh" in your /etc/crontab - this will call the script every hour). Please note that the script will perform its own checking to see
@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 / 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 / contracts.py
Created July 12, 2017 14:55
Contracts in Python
import functools
import inspect
class Validated(type):
"""
Base meta-class for our validation classes.
"""
def __getitem__(self, type):
@adewes
adewes / track_temperature.sh
Last active February 28, 2024 16:33
Temperature Tracking Script
#!/bin/bash
# logs the temperature every minute
# sends a message if it is too high or low or if it can't be measured
# also sends a message at a given time every day
#sensor ID (must be a DS18B20 sensor)
SENSOR_ID="28-011453d372aa"
# hour when to send the message
REF_HOUR="17:00"
# maximum temperature
@adewes
adewes / eight_queens.sql
Last active February 18, 2024 03:07
Eight Queens Problem Solved using Common Table Expressions
WITH RECURSIVE
positions(i) as (
VALUES(0)
UNION SELECT ALL
i+1 FROM positions WHERE i < 63
),
solutions(board, n_queens) AS (
SELECT '----------------------------------------------------------------', cast(0 AS bigint)
FROM positions
UNION
@adewes
adewes / README.md
Last active February 13, 2024 16:39
Ebay Ads - Bot. Because who wants to write messages by hand...

To use this bot:

  • Download ads_bot.py and requirements.txt.
  • Type pip install -r requirements.txt to install the requirements.
  • Fill out the required information in the Python file.
  • Ideally, create a (free) Slack account and set up a web hook to receive notifications from the bot.
  • Run the script :)
  • Relax and be ready to answer incoming calls :D
@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 / 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):