Skip to content

Instantly share code, notes, and snippets.

View harunyasar's full-sized avatar

Harun Yasar harunyasar

  • Hyperware LTD
  • Oxford
  • 17:53 (UTC)
View GitHub Profile
@harunyasar
harunyasar / config
Created January 30, 2018 13:59
Multiple SSH keys
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
Host bitbucket.org
HostName bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_bitbucket
@harunyasar
harunyasar / install.sh
Created December 30, 2017 18:52
virtualenwrapper
sudo easy_install pip
sudo pip install virtualenv
sudo pip install virtualenvwrapper
export WORKON_HOME=~/env
mkdir -p $WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh
@harunyasar
harunyasar / example.py
Last active January 17, 2018 16:46
Function-based decorators
def add2(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs) + 2
return wrapper
@add2
def foo(number):
return number * 2
@harunyasar
harunyasar / attrget.py
Last active December 12, 2017 16:30
attrget
from operator import attrgetter
from collections import namedtuple
# Example has been given from Aaron Maxwell's The Python Next Level source
class Student:
def __init__(self, name, major, gpa):
self.name = name
self.major = major
self.gpa = gpa
@harunyasar
harunyasar / itemgetter.py
Created December 12, 2017 11:40
itemgetter
from operator import itemgetter
from functools import reduce
leagues = [
{
'country': 'Switzerland',
'teams': ['Young Boys', 'Basel', 'Zürich', 'Grasshoppers', 'St. Gallen', 'Lausanne Sports', 'Thun', 'Luzerns',
'Lugano', 'Sion']
},
{
public class Garson implements Runnable {
public Kantin k;
public int id, bekleme;
Garson(int id, int bekleme, Kantin k) {
this.id = id;
this.k = k;
this.bekleme = bekleme;
}
@harunyasar
harunyasar / quicksort.py
Created April 26, 2017 06:06
Quicksort with Python
def quicksort(a, p, r):
if p < r:
q = partition(a, p, r)
quicksort(a, p, q - 1)
quicksort(a, q + 1, r)
def partition(a, p, r):
x = a[r]
i = p - 1
@harunyasar
harunyasar / function_decorator_in_a_class.py
Created March 20, 2017 11:53
How to use Python function decorators in a class?
class Lightning:
lights_on = 'ON'
lights_off = 'OFF'
def button_action(action_type):
def action_decorator(func):
def action_wrapper(self):
if action_type == 'on':
print(self.lights_on)
func(self)
@harunyasar
harunyasar / function_composition.py
Created March 10, 2017 12:07
Example of function composition with Python
def addition(x):
return x + 5
def subtraction(y):
return y - 1
def compose(*funcs):
return lambda x: reduce(lambda v, f: f(v), reversed(funcs), x)
c = compose(addition, subtraction)
@harunyasar
harunyasar / kill.sh
Created February 23, 2017 08:29
Kill all PostgreSQL connections
#!/usr/bin/env bash
# kill all connections to the postgres server
if [ -n "$1" ] ; then
where="where pg_stat_activity.datname = '$1'"
echo "killing all connections to database '$1'"
else
where="where pg_stat_activity.datname in (select datname from pg_database where datname != 'postgres')"
echo "killing all connections to database"
fi