Skip to content

Instantly share code, notes, and snippets.

View phalt's full-sized avatar
🐍

Paul Hallett phalt

🐍
View GitHub Profile
@phalt
phalt / fib.py
Created June 6, 2013 19:33
Python Fibonacci sequence, be careful running this
a,b = 1,0
while True:
a, b = b, a+b
print b
@phalt
phalt / minimum.py
Last active December 18, 2015 07:39
Simple minimum function that can be used recursively
def minimum(first, second):
# Returns the minimum
if first > second:
return sec
return first
#Test this
# print 5
@phalt
phalt / birthday.py
Last active August 2, 2016 11:12
Script to sing happy birthday to someone
def happy_birthday(name):
for i in range(0,4):
if i == 2:
print('happy birthday dear {}'.format(name))
else:
print('happy birthday to you')
# example:
@phalt
phalt / fibgen.py
Created November 13, 2013 10:56
Fibonacci sequence with a generator
def fib(n):
a, b = 1, 0
while a < n:
yield a
a, b = b, a+b
# example:
'''
@phalt
phalt / 2014.py
Created January 2, 2014 12:37
Prints '2014' without actually using any numbers. Attributes - http://codegolf.stackexchange.com/a/17103/12689
print sum(ord(c) for c in 'Happy new year to you!')
@phalt
phalt / cool.py
Last active January 3, 2016 03:29
Cool python things
# List comprehensions
# Generate a list of integers from 1 to 10 in one line
l = [i for i in range(1, 11)]
# Lambdas
# Anonymous functions assigned to variables
f = lambda x: x % 2 == 0
for i in l:
@phalt
phalt / decorator.py
Last active January 3, 2016 08:09
Useful decorator around the django.contrib.sites package for view functions.
from django.conf import settings
from functools import wraps
from django.contrib.sites.models import get_current_site
from django.http import HttpResponseNotFound
def main_site_only(function):
"Use this decorator on views that should only be visible on your main site"
@wraps(function)
def decorator(request, *args, **kwargs):
@phalt
phalt / escape.py
Created July 13, 2014 08:18
Escape html and other common characters
def escape_html(text):
html_escape_table = {
"&": "&amp;",
'"': "&quot;",
"'": "&apos;",
">": "&gt;",
"<": "&lt;",
}
return "".join(html_escape_table.get(c,c) for c in text)
@phalt
phalt / example.py
Last active August 29, 2015 14:04
Get Pokemon description based on name using Pykemon
import pykemon
name = 'charizard' # Example
p = pykemon.get(pokemon=name)
# Figure out the number of descriptions we have for this Pokemon
des = p.descriptions
l = str(len(des))
r = Response()
r.message(pokemon_name + ',' + description)