Jan 30, 2012

Logging

import logging
import logging.handlers

# Sets up a basic textfile log, with formatting
logging.basicConfig(level=logging.DEBUG,
   format='%(asctime)s %(levelname)-8s %(message)s',
   datefmt='%m/%d/%y %H:%M:%S',
   filename=r'C:\temp\mylog.log',
   filemode='a')

# Log a few different events
logging.info('Just testing the water.')
logging.warning('Hmm, something is not right here')
logging.error("Oh no, now you're in for it")
The resulting text log:
02/14/08 22:19:03 INFO     Just testing the water.
02/14/08 22:19:03 WARNING  Hmm, something is not right here
02/14/08 22:19:03 ERROR    Oh no, now you're in for it
Add a few more lines and it sends you an email for any logs that are level "ERROR" or above:
email = logging.handlers.SMTPHandler('smtp.foo.com',
   'script@foo.com',('techart@bar.com'),'Error Report')
email.setLevel(logging.ERROR)

logging.getLogger('').addHandler(email)

Python String Templates

import string
template = string.Template("The $speed $color $thing1")
template.substitute(speed='quick', color='brown', thing1='fox')
# 'The quick brown fox'

Jan 20, 2012

How to override the verbose name of a superclass model field in django

class SuperFoo(models.Model):
    name = models.CharField('name of SuperFoo instance', max_length=50)
    ...

class Foo(SuperFoo):
    ... # do something that changes verbose_name of name field of SuperFoo
    Foo._meta.get_field('name').verbose_name = 'Whatever'

Django Imagekit processing the original image

from django.db import models
from imagekit.models import ImageSpec
from imagekit.processors import resize, Adjust

class Photo(models.Model):
    original_image = models.ImageField(upload_to='photos')
    thumbnail = ImageSpec([resize.Fit(50, 50)], image_field='original_image',
            format='JPEG', options={'quality': 90})

Mocking

Mock basics

mock = Mock(name='bar', return_value='fish')
mock(1, 2, spam = 99)
# 'fish'
mock.assert_called_once_with(1, 2, spam = 99)
mock.called
# True
mock.call_count
# 1
mock.call_args
# ((1, 2), {'spam': 99})
From: http://blip.tv/pycon-us-videos-2009-2010-2011/pycon-2011-testing-with-mock-4899484

Magic Mock basics

mock = MagicMock()
mock[5]
# <mock.Mock object at 0x2f90690>
mock.__str__.return_value = 'bzzzz'
str(mock)
# 'bzzzz'

Mock exceptions handling test

mock = Mock(side_effect=KeyError('boom'))
mock()
# Traceback (most recent call last)
# ....
# KeyError: 'boom'

Mock file write

>>> open_name = '%s.open' % __name__
>>> with patch(open_name, create=True) as mock_open:
...     mock_open.return_value = MagicMock(spec=file)
...
...     with open('/some/path', 'w') as f:
...         f.write('something')
...

>>> file_handle = mock_open.return_value.__enter__.return_value
>>> file_handle.write.assert_called_with('something')
http://stackoverflow.com/questions/1289894/how-do-i-mock-an-open-used-in-a-with-statement-using-the-mock-framework-in-pyth
http://www.voidspace.org.uk/python/mock/magicmock.html

Mock file and raw_post_data

from mock import Mock, MagicMock

class SomeTestCase(TestCase):

    def testRawPostData(self):
        ...

        request = Mock(spec=request)
        request.raw_post_data = 'myrawdata'
        print request.raw_post_data  # prints 'myrawdata'

        file_mock = MagicMock(spec=file)
        file_mock.read.return_value = 'myfiledata'
        request.FILES = {'myfile': file_mock}    
        print request.FILES['myfile'].read()  # prints 'myfiledata'

Mocking stdout

outstream = StringIO()
with patch('sys.stdout', new=outstream) as out:
    ...
    actual_out = out.getvalue()

Video: 
A Gentle Introduction to Mock for Python
Why Use Mock?
Mock and Django

Jan 19, 2012

Python Standard Library: Collections

Counter

Dict subclass for counting hashable items. Sometimes called a bag or multiset. Elements are stored as dictionary keys and their counts are stored as dictionary values.
Counter(['a', 'b', 'c', 'a'])
# Counter({'a': 2, 'c': 1, 'b': 1})

namedtuple

Returns a new subclass of tuple with named fields.
Point = namedtuple('Point', 'x y z')
Point.__doc__
# 'Point(x, y, z)'
p = Point(x=1, y=2, z=3)
print p.x, p.y, p.z
# 1 2 3

Python Standard Library: Functools

partial

New function with partial application of the given arguments and keywords
from functools import partial
def f(a, b=2):
    print a, b

f1 = partial(f, 'fixed_a')
f1(b=777)
# fixed_a 777

f2 = partial(f, b='fixed_b')
f2(777)
# 777 fixed_b

Backbone, Underscore

Tutorials
Backbone.js по-русски
Getting to Know Backbone.js

Example applications
backbone-boilerplate - A set of best practices and utilities for building Backbone.js applications.
vertebrate-django - A small project illustrating Django and Backbone.js integration
django_backbone_demo - the classic todo example, but with server side persistance
django-backbone-example - Sample application to show how to use Django and Backbone.js together
scrumboard - application with backbone.js, django and tastypie

Articles
http://paltman.com/2012/04/30/integration-backbonejs-tastypie/
http://joshbohde.com/blog/backbonejs-and-django

Extensions
backbone-ui
backbone-aura - A decoupled, event-driven architecture on top of Backbone.js for developing widget-based applications

gedit-markdown

http://www.jpfleury.net/en/software/gedit-markdown.php

Jan 18, 2012

Python Standard Library: Itertools

chain

Return a chain object whose .next() method returns elements from the first iterable until it is exhausted, then elements from the next iterable, until all of the iterables are exhausted.
[x for x in chain([1, 2],[3, 4])]
# [1, 2, 3, 4]

izip

Works like the zip() function but consumes less memory by returning an iterator instead of a list.
[x for x in izip([1, 2],[3, 4])]
# [(1, 3), (2, 4)]

[x for x in izip([1, 2, 4],[3, 4])]
# [(1, 3), (2, 4)]

islice

Works like the slice() function but consumes less memory by returning an iterator instead of a list.
[x for x in islice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 4)] # first n-elements
# [0, 1, 2, 3]

[x for x in islice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 2, 5)]
# [2, 3, 4]

[x for x in islice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 0, 10, 3)]
# [0, 3, 6, 9]

#same with count()
from itertools import islice, count
[x for x in islice(count(), 0, 10, 3)]
# [0, 3, 6, 9]

imap

Like map() except that it returns an iterator instead of a list and that it stops when the shortest iterable is exhausted instead of filling in None for shorter iterables.
[x for x in imap(lambda x: 2*x, [1,2,3])]
# [2, 4, 6]

combination

Return successive r-length combinations of elements in the iterable.
[x for x in combinations([1, 2, 3], 1)]
# [(1,), (2,), (3,)]

[x for x in combinations([1, 2, 3], 2)]
# [(1, 2), (1, 3), (2, 3)]

[x for x in combinations([1, 2, 3], 3)]
# [(1, 2, 3)]

[x for x in combinations([1, 2, 3], 4)]
# []

cycle

Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.
for x in cycle(['one', 'two', 'three']):
    print x
  
one
two
three
one
two
three
one
two
......

repeat

Create an iterator which returns the element for the specified number of times. If not specified, returns the element endlessly.
for x in repeat(['one', 'two', 'three'], 2):
    print x
['one', 'two', 'three']
['one', 'two', 'three']
Much faster then
for x in ['one', 'two', 'three'] * 2:
    print x

Jan 11, 2012

How do you reload a Django model module using the interactive interpreter

from django.db.models.loading import AppCache
cache = AppCache()

for app in cache.get_apps():
    __import__(app.__name__)
    reload(app)

from django.utils.datastructures import SortedDict
cache.app_store = SortedDict()
cache.app_models = SortedDict()
cache.app_errors = {}
cache.handled = {}
cache.loaded = False

Jan 8, 2012

Social apps

django-allauth - Integrated set of Django applications addressing authentication, registration, account management as well as 3rd party (social) account authentication. Visit http://jug.gl for a live demo
django-netauth - django auth backend (RU)

Jan 6, 2012

Django runserver and stunnel for testing HTTPS

To get a simple tunnel setup, we typically want to follow this route:

browser ---> https://localhost:8443 ---> http://localhost:8000 ---> runserver

Create ssl sertificate

That is, the routing of all requests on localhost port 8443 to localhost port 8000, which is where our Django runserver instance is serving up our web application and static content (if any). To setup this routing, I’ve created a simple stunnel configuration file, which also provides a few other configuration niceties, like outputting all messages to stdout rather than running silently in the background. The configuration file (fake_https) is represented below:
pid=

cert = path/to/your/stunnel.pem
sslVersion = SSLv3
foreground=yes
#debug = 7
#output = /path/to/stunnel.log

[https]
accept=8443
connect=8000
TIMEOUTclose=1
Be sure to note the use of the TIMEOUTclose option. Without this set to a low timeout value, you will notice a severe lag before your browser receives a close message. To run stunnel with this configuration, simply execute the following from the command line:
stunnel fake_https
Finally, you must tell Django’s runserver to modify all incoming HTTP requests to behave as if they were over HTTPS. This tells Django to set all request objects to return True for calls to request.is_secure(). This may be accomplished by simply setting the HTTPS environment variable to a non-zero value (i.e. True) prior to executing runserver. For example:
HTTPS=1 python manage.py runserver
You may now visit https://localhost:8443 in your web browser, and you should see activity in your stunnel terminal window and in your Django runserver terminal window, indicating a successful tunneling of all local SSL traffic to your basic Django runserver.

This simple method is a great way to test your web apps locally to ensure they behave correctly under secure and unsecure scenarios, including server-side handling of secure cookies.

http://stackoverflow.com/questions/8023126/how-can-i-test-https-connections-with-django-as-easily-as-i-can-non-https-connec

How to generate a self-signed ssl certificate

openssl genrsa 1024 > stunnel.key
openssl req -new -x509 -nodes -sha1 -days 365 -key stunnel.key > stunnel.cert
cat stunnel.key stunnel.cert > stunnel.pem
dd if=/dev/urandom count=2 | openssl dhparam -rand - 512 > stunnel.pem
This generates a self-signed certificate which should be sufficient for testing purposes.
http://code.google.com/p/django-weave/wiki/HTTPSDevelopment