Skip to content

Instantly share code, notes, and snippets.

View obeattie's full-sized avatar

Oliver Beattie obeattie

View GitHub Profile
@obeattie
obeattie / datetime_range.py
Created October 19, 2009 12:31
A datetime range class for Python, for (obviously) dealing with ranges of datetimes.
"""Provides a `DateTimeRange` class, which is used for managing ranges of datetimes."""
import datetime
class DateTimeRange(object):
"""Represents a range of datetimes, with a start and (optionally) an end.
Basically implements most of the methods on a standard sequence data type to provide
some lovely syntactic sugar. Specifically, you can iterate on this, index it, slice it,
use the in operator, reverse it, and use it in a boolean context to see if there is any
time in between the start and end."""
@obeattie
obeattie / db_utils.py
Created October 14, 2009 12:51
Exposes SQLAlchemy's sessions and transactions as context managers (so they will be managed automatically inside blocks), and also provides a transaction decorator, which wraps an entire function in a transaction
"""Utilities for managing database sessions."""
from __future__ import with_statement
import contextlib
import functools
@contextlib.contextmanager
def temp_session(session_cls, **kwargs):
"""Quick and dirty context manager that provides a temporary Session object
to the nested block. The session is always closed at the end of the block.
from cStringIO import StringIO
from decimal import Decimal
from PIL import Image
from urllib2 import urlopen
from django.db import transaction
from django.core.files.base import ContentFile
from ilovephotos.blacktip import models
from ilovephotos.blacktip.util.parse_help import verify_args, get_list_ids