Skip to content

Instantly share code, notes, and snippets.

View gchiam's full-sized avatar

Gordon Chiam gchiam

View GitHub Profile
$ 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()
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
"""Sequence generator
$ python sequence.py 3
[3]
[2, 1]
[1, 1, 1]
$python sequence.py 6
[6]
@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
@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