Skip to content

Instantly share code, notes, and snippets.

@cessor
cessor / flatten_list.py
Last active November 3, 2022 08:50
Flatten a list of lists
def reduce(data):
return functools.reduce(lambda v,head: v+head, data)
# The following is even faster
data = image.getdata()
data = [color for pixel in data for color in pixel]
@cessor
cessor / test_secret_number.py
Created June 5, 2018 08:38
Secret Number Game, 10 Minutes, With TDD
from nose.tools import *
from random import random
def eval(secretNumber,guessedNumber):
return cmp(guessedNumber, secretNumber)
messages = {
-1 : "Guess bigger!",
0 : "You won!",
@cessor
cessor / steamroller.py
Created June 5, 2018 08:37
Steamroller - Flatten a JSON Doc and prevent key collisions
def identity(name):
return name
def prefix_with(parent, collisions):
def prefix_if_colliding(name):
if name in collisions:
return '_'.join((parent, name))
return name
return prefix_if_colliding
@cessor
cessor / flattten_dict.py
Created June 5, 2018 08:36
Flatten a nested dictionary to one level, avoiding key collisions
from nose.tools import *
# UNSAUBER?!
def flatten(dictionary, prefix = lambda n: n):
new_dictionary = {}
for key,value in dictionary.iteritems():
if isinstance(value, dict):
mangle = lambda k: '_'.join((key,k))
flattened_value = flatten(value, mangle)
new_dictionary.update(flattened_value)
@cessor
cessor / sum.py
Created June 5, 2018 08:35
How to sum numbers - More than one way to do it
#coding: utf-8
reaction_times = [512,122,928,650,9999,612]
def summe_berechnen():
sum = 0
for value in reaction_times:
sum += value
return sum
def summe_berechnen_2():
@cessor
cessor / blockhost.py
Created May 19, 2018 11:11
blockhosts.py
import os
from pathlib import Path
from urllib.parse import urlparse
'''
Blocks a couple of hosts by adding an entry to Windows' etc/hosts file
that point to 127.0.0.1. I use this script to stop me from procrastinating.
Todo:
[ ] The lines-Abstraction made sense along the way but it broke. Could it
@cessor
cessor / flatten.py
Created April 9, 2018 08:49
Flatten a Directory by moving all files up one level.
import os
import shutil
ROOT = '.'
for dirpath, dirnames, filenames in os.walk(ROOT):
if dirpath == ROOT:
continue
for file in filenames:
source = os.path.join(dirpath, file)
destination = os.path.join(ROOT, file)
@cessor
cessor / theme.html
Created March 10, 2018 12:59
Weyland Yutani UI Theme - GunMetalGray
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Weyland Yutani UI Theme - GunMetalGray</title>
<link rel="stylesheet" type="text/css" href="wy.css">
</head>
<body>
<h1>Hello</h1>
@cessor
cessor / .gitconfig
Last active June 24, 2018 06:34
.gitconfig
# git config --global alias.<alias> <command>
# https://github.com/cessor/howtolinux/blob/master/git.md
# https://git-scm.com/docs/pretty-formats
[alias]
st = status -s
addrem = add . -A
summary = log --oneline
sum = log --oneline
tl = log --oneline --all --graph --pretty=format:'%C(yellow)%h%Creset - %C(cyan)%cr%Creset [%C(green)%an%Creset] - %Cred%d%Creset %s' --abbrev-commit --date=relative
tree = !git tl
@cessor
cessor / django_tree.py
Created March 8, 2018 00:40
Closure Table Tree (Django)
from django.db import models
class Page(models.Model):
path = models.CharField(max_length=100, blank=False, primary_key=True)
title = models.CharField(max_length=255, blank=False)
rank = models.IntegerField(default=0)
def __str__(self):
return str(self.title)