Skip to content

Instantly share code, notes, and snippets.

def datechunk(start, end, days):
"""
Create a generator of (start_date, end_date) tuples
where the difference between start_date and end_date
is the number of days given.
>>> list(datechunk(datetime.date(2010, 01, 01), datetime.date(2010, 08, 20), 30))
[(datetime.date(2010, 1, 1), datetime.date(2010, 1, 31)),
(datetime.date(2010, 2, 1), datetime.date(2010, 3, 3)),
(datetime.date(2010, 3, 4), datetime.date(2010, 4, 3)),
@bycoffe
bycoffe / ncaa_basketball_scores.py
Created March 19, 2010 17:14
Script to allow viewing of scores of in-progress college basketball games on the command line
#!/usr/bin/env python
"""
Python script to allow viewing of scores of in-progress college basketball games
on the command line. Scores come from Yahoo Sports.
Requirements:
BeautifulSoup
"""
import re
import urllib2
# http://xkcd.com/710/
# An iterator for showing the Collatz Conjecture
"""
>>> [x for x in Collatz(6)]
[3, 10, 5, 16, 8, 4, 2, 1]
>>> [x for x in Collatz(11)]
[34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
>>> len([x for x in Collatz(27)])
# Testing goat. Via http://valkyrie.kwister.com/home/ascii/mygoat.html
______._
/ / \ \
/ / / /
\ \
\ \____.
/ / \\
/ @ \ / \,
/ \/ \___ _______
\____/ \ `--------' `-,
import Image
import sys
def make_image(color, filename, dimensions=(1,1)):
"""Create a solid-color image."""
im = Image.new('RGB', dimensions, color)
im.save(filename)
if __name__ == '__main__':
Copyright 2010 Aaron Bycoffe. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
from django.template import Library
register = Library()
@register.filter_function
def html5datetime(date):
"""Format a date or a date and time according to the HTML 5 specifications,
so it may be used as the value for a datetime attribute of a time tag.
Most of the date formatting could be done in the template with the built-in
@bycoffe
bycoffe / csvtosqlite.py
Created October 23, 2009 16:52
Creates a sqlite database and table from a CSV file for easier querying
"""
Copyright (c) 2009, Aaron Bycoffe
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
@bycoffe
bycoffe / testcsv.csv
Created October 23, 2009 16:49
Test CSV file for csvtosqlite
City Latitude Longitude
Autauga, AL 32.5791817 -86.4996546
Baldwin, AL 30.6010744 -87.7763333
Barbour, AL 31.8172896 -85.354965
Bibb, AL 32.9562798 -87.1422895
Blount, AL 34.0145152 -86.4996546
Bullock, AL 32.0573536 -85.7256372
Butler, AL 32.091673 -88.221233
Calhoun, AL 33.7701576 -85.80766
Chambers, AL 32.9028048 -85.354965
#!/usr/bin/env python
"""
Copyright (c) 2009, Aaron Bycoffe
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.