Skip to content

Instantly share code, notes, and snippets.

View ksindi's full-sized avatar
🚀
Shipping

Kamil Sindi ksindi

🚀
Shipping
View GitHub Profile
@ksindi
ksindi / mysql.py
Created October 23, 2015 19:26 — forked from adamv/mysql.py
Simple wrapper around MySQLdb
import collections
import MySQLdb as dbapi
__all__ = ['MySql']
class MySql(object):
def __init__(self, **kwargs):
self.connection = dbapi.connect(**kwargs)
self.host_name = kwargs.get('host')
@ksindi
ksindi / mysqldb_namedtuple.py
Created October 23, 2015 20:44
Return rows as namedtuples
names = " ".join(d[0] for d in cursor.description)
cls = collections.namedtuple('Results', names)
rows = map(cls._make, cursor.fetchall())
@ksindi
ksindi / bobp-python.md
Created November 27, 2015 02:55 — forked from sloria/bobp-python.md
A "Best of the Best Practices" (BOBP) guide to developing in Python.

The Best of the Best Practices (BOBP) Guide for Python

A "Best of the Best Practices" (BOBP) guide to developing in Python.

In General

Values

  • "Build tools for others that you want to be built for you." - Kenneth Reitz
  • "Simplicity is alway better than functionality." - Pieter Hintjens
@ksindi
ksindi / search4.py
Last active November 30, 2015 15:40 — forked from thomasst/search4.py
from pyparsing import *
import unittest
class Node(list):
def __eq__(self, other):
return list.__eq__(self, other) and self.__class__ == other.__class__
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, list.__repr__(self))
@ksindi
ksindi / search6.py
Created December 3, 2015 15:27 — forked from thomasst/search6.py
from pyelasticsearch import ElasticSearch, ElasticHttpNotFoundError
from pyparsing import *
import unittest
ELASTICSEARCH_INDEX = 'myindex'
ELASTICSEARCH_URL = 'http://localhost:9200/'
es = ElasticSearch(ELASTICSEARCH_URL)
@ksindi
ksindi / tmux-cheatsheet.markdown
Created December 10, 2015 03:25 — forked from MohamedAlaa/tmux-cheatsheet.markdown
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@ksindi
ksindi / namedtupledict
Created December 22, 2015 16:23
access namedstuples as dictionary
# from http://pythondoeswhat.blogspot.com/2011/09/namedtupledict.html
def namedtupledict(*a, **kw):
namedtuple = collections.namedtuple(*a, **kw)
def getitem(self, key):
if type(key) == str:
return getattr(self, key)
return tuple.__getitem__(self, key)
namedtuple.__getitem__ = getitem
return namedtuple
@ksindi
ksindi / reactor.py
Created December 26, 2015 20:46 — forked from jpanganiban/reactor.py
A Very Simple Reactor Pattern Implementation in Python (with Gevent)
#!/usr/bin/env python
from gevent import monkey
monkey.patch_all() # Patch everything
import gevent
import time
class Hub(object):
"""A simple reactor hub... In async!"""
@ksindi
ksindi / cumulative_return_ranking.py
Created December 28, 2015 19:36
Show cumulative return and rank
import pandas as pd
df = pd.DataFrame([['2012', 'A', 1], ['2012', 'B', 4], ['2011', 'A', 5], ['2011', 'B', 4]],
columns=['year', 'manager', 'return_pct'])
df['total_return'] = (df
.groupby('manager')['return_pct']
.transform(lambda group: (1 + group / 100.).cumprod().iat[-1])) - 1
df['ranking'] = df.total_return.rank(ascending=False, method='dense')
@ksindi
ksindi / mysqldb_query_generator.py
Created February 20, 2016 17:56 — forked from robcowie/mysqldb_query_generator.py
Memory-efficient, streaming query generator with MySQLdb
from MySQLdb.cursors import SSDictCursor
def iterate_query(query, connection, arraysize=1):
c = connection.cursor(cursorclass=SSDictCursor)
c.execute(query)
while True:
nextrows = c.fetchmany(arraysize)
if not nextrows:
break