This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dict(a=1, b=2, c=3) | |
returns {'a': 1, 'b': 2, 'c': 3} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
def ensure_dir(f): | |
d = os.path.dirname(f) | |
if not os.path.exists(d): | |
os.makedirs(d) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use reader() to create a an object for reading data from a CSV file. The reader can be used as an iterator | |
# to process the rows of the file in order. For example: | |
import csv | |
import sys | |
f = open(sys.argv[1], 'rt') | |
try: | |
reader = csv.reader(f) | |
for row in reader: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Both seem to work for something like: | |
mydict = {'a' : 1, 'b' : 2} | |
for key,val in mydict.items(): | |
print key,val | |
# ==> a 1 b 2 | |
for key,val in mydict.iteritems(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# dicts to lists | |
dictionary = {'a': 1, 'b': 2, 'c': 3} | |
2dict_as_list = dictionary.items() | |
# dict_as_list now contains [('a', 1), ('b', 2), ('c', 3)] | |
# lists to dicts | |
dict_as_list = [['a', 1], ['b', 2], ['c', 3]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def json_response(something): | |
from django.utils import simplejson | |
return HttpResponse(simplejson.dumps(something), | |
content_type='application/json; charset=UTF-8') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Entry( models.Model ): | |
user = models.ForeignKey( User, related_name = 'user_relation' ) | |
category = models.ForeignKey( Category, related_name = 'category_relation' ) | |
title = models.CharField( max_length = 64 ) | |
entry_text = models.TextField() | |
deleted_datetime = models.DateTimeField() | |
kwargs = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models import Q | |
Publisher.objects.filter(Q(state_province="CA") | Q(state_province="AZ")) | |
#or also more programmatically: | |
args = ( Q( title__icontains = 'Foo' ) | Q( title__icontains = 'Bar' ) ) | |
entries = Entry.objects.filter( *args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponseRedirect | |
def myview(request): | |
... | |
return HttpResponseRedirect("/path/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.test.client import Client | |
c = Client() | |
response = c.post(\'/login/\', {\'username\': \'john\', \'password\': \'smith\'}) | |
response.status_code | |
# 200 | |
response = c.get(\'/customer/details/\') | |
response.content | |
# \'<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 ...\' |