Skip to content

Instantly share code, notes, and snippets.

View gchiam's full-sized avatar

Gordon Chiam gchiam

View GitHub Profile
@gchiam
gchiam / fibonacci.py
Created July 11, 2014 02:27
Python yield example - Fibonacci
"""Print Fibonacci series using generator"""
import argparse
import textwrap
def fibonacci(number):
"""Fibonacci series generator"""
previous, current = 0, 1
counter = 0
@gchiam
gchiam / django_custom_model_manager_chaining.py
Created July 11, 2014 08:05
Django custom model manager chaining
"""Copied from http://hunterford.me/django-custom-model-manager-chaining/
Reference: http://www.dabapps.com/blog/higher-level-query-api-django-orm/
usage:
Post.objects.published()
Post.objects.by_author(user=request.user).published()
"""
from django.db import models
"""Sequence generator
$ python sequence.py 3
[3]
[2, 1]
[1, 1, 1]
$python sequence.py 6
[6]
a = [1, 2, 3]
b = [4, 5, 6]
zipped = zip(a,b)
# zipped = [(1, 4), (2, 5), (3, 6)]
unzipped = zip(*zippped)
# unzipped = [(1, 2, 3), (4, 5, 6)]
# get back a, b
$ python -c "print('True' if None < 0 else False)"
True
$ python3 -c "print('True' if None < 0 else False)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: unorderable types: NoneType() &lt; int()
@gchiam
gchiam / dashing.py
Created September 9, 2015 00:02
Python module to send data to Dashing widget
"""Dashing module"""
import logging
import urllib2
import simplejson as json
DEFAULT_DASHING_AUTH_TOKEN = 'YOUR_AUTH_TOKEN'
@gchiam
gchiam / simpleworkflow.py
Last active September 30, 2015 01:47
Simple Workflow Engine
import functools
import logging
class SimpleWorkflowEngine(object):
"""A simple workflow engine
Usage:
from functools import partial
def initialize(number):
@gchiam
gchiam / gruvbox.yml
Created October 6, 2015 23:58
Color scheme for base16-builder based on gruvbox color scheme
# Color scheme for base16-builder based on gruvbox color scheme
# (https://github.com/morhetz/gruvbox).
# [1] gruvbox.yml
scheme: "Gruvbox"
author: "Gordon Chiam (https://github.com/gchiam)"
base00: "282828"
base01: "504945"
base02: "7c6f54"
base03: "ebdbb2"
base04: "bdae93"
@gchiam
gchiam / tmux-theme-gruvbox.conf
Created October 7, 2015 00:34
gruvbox color scheme for tmux
# This tmux statusbar config was created based on gruvbox colorscheme
set -g status "on"
set -g status-justify "left"
set -g status-left-length "100"
set -g status-right-length "100"
set -g status-right-attr "none"
set -g status-attr "none"
set -g status-utf8 "on"
set -g status-left-attr "none"
@gchiam
gchiam / spam1.py
Last active October 15, 2015 03:50
fallback import (python3 only)
# spam.py
try:
import foo
except ImportError:
import simplefoo as foo