Skip to content

Instantly share code, notes, and snippets.

@embayer
embayer / switch_key_values.py
Last active August 29, 2015 14:04
Switch keys and values in a dict
# sitch keys and values in a dict
aDict = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
inverse = dict(zip(aDict.values(), aDict.keys()))
@embayer
embayer / pot.php
Created July 28, 2014 08:52
Resolve the power of two terms of a given sum using the binary system
<?php
// resolve the power of two terms of a given sum
// using the binary system
function resolvePowerOfTwo($sum)
{
$result = array();
// binary representation
$bin = decbin($sum);
@embayer
embayer / germanCities.py
Last active August 29, 2015 14:05
python list of german cities
This file has been truncated, but you can view the full file.
# -*- coding: utf-8 -*-
# data raised by destatis (https://www.destatis.de/)
# "Vorläufige Ergebnisse auf Grundlage des Zensus 2011, Zensusdaten mit dem Stand vom 10.04.2014"
# data as of 2011
# population sum = 80327900
# city count = 11300
@embayer
embayer / germanCities_ca.py
Created August 18, 2014 09:46
python list of german cities (cleared city name additions)
This file has been truncated, but you can view the full file.
# -*- coding: utf-8 -*-
# data raised by destatis
# "Vorläufige Ergebnisse auf Grundlage des Zensus 2011, Zensusdaten mit dem Stand vom 10.04.2014"
# data as of 2011
# population sum = 80327900
# city count = 11300
# cleared additions:
@embayer
embayer / pythonUtf8.md
Last active October 30, 2020 06:21
set python default encoding to utf-8

change default encoding

cd ~/.virtualenvs/myvirtualenv/lib/python2.x/site-packages
echo 'import sys;sys.setdefaultencoding("utf-8")' > sitecustomize.py

set default encoding for each new virtualenv

to automatically create a sitecustomize.py every time you create a virtualenv, edit your

@embayer
embayer / Duration.py
Last active August 29, 2015 14:07
print a duration in human readable format
import datetime
import time
class Duration:
def __init__(self, start_time=None, end_time=None):
self.start_time = start_time
self.end_time = end_time
def start(self):
self.start_time = time.time()
@embayer
embayer / kill_duplicates.py
Created September 30, 2014 18:42
reduce duplicates in a sequence
def kill_duplicates(seq):
seen = set()
seen_add = seen.add
return [ x for x in seq if not (x in seen or seen_add(x))]
@embayer
embayer / color_print.py
Last active December 8, 2015 20:40
Colorful shell output
def color_print(msg, color):
colors = { "clear": "\033[0;m",
"gray": "\033[1;30m",
"red": "\033[1;31m",
"green": "\033[1;32m",
"yellow": "\033[1;33m",
"blue": "\033[1;34m",
"magenta": "\033[1;35m",
"cyan": "\033[1;36m",
"white": "\033[1;37m",

PHP UTF-8

Apache configuration (in httpd.conf or .htaccess)

AddDefaultCharset utf-8

PHP (in php.ini)

default_charset = "utf-8"

@embayer
embayer / sql_collection.sql
Last active August 29, 2015 14:10
collection of useful sql statements
-- find duplicated values
SELECT
address_line1, address_line2, zip, city, country, COUNT(*)
FROM
location
GROUP BY
address_line1, address_line2, zip, city, country
HAVING
COUNT(*) > 1;