Skip to content

Instantly share code, notes, and snippets.

@lambdamusic
lambdamusic / Snipplr-34614.py
Created February 7, 2013 21:25
Python: Creating Dictionaries
dict(a=1, b=2, c=3)
returns {'a': 1, 'b': 2, 'c': 3}
@lambdamusic
lambdamusic / Snipplr-43554.py
Created February 7, 2013 21:25
Python: Creating directories programmatically
import sys
import os
def ensure_dir(f):
d = os.path.dirname(f)
if not os.path.exists(d):
os.makedirs(d)
@lambdamusic
lambdamusic / Snipplr-50966.py
Created February 7, 2013 21:25
Python: CSV reading in python
# 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:
@lambdamusic
lambdamusic / Snipplr-34617.py
Created February 7, 2013 21:25
Python: dict.items vs dict.iteritems
# 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():
@lambdamusic
lambdamusic / Snipplr-34615.py
Created February 7, 2013 21:25
Python: Dicts to Lists / Lists to Dicts
# 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]]
@lambdamusic
lambdamusic / Snipplr-25237.py
Created February 7, 2013 21:25
Django: Django and JSON
def json_response(something):
from django.utils import simplejson
return HttpResponse(simplejson.dumps(something),
content_type='application/json; charset=UTF-8')
@lambdamusic
lambdamusic / Snipplr-25266.py
Created February 7, 2013 21:25
Django: Django kwargs
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 = {
@lambdamusic
lambdamusic / Snipplr-25267.py
Created February 7, 2013 21:25
Django: Django Q objects
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)
@lambdamusic
lambdamusic / Snipplr-42051.py
Created February 7, 2013 21:25
Django: Django simple url redirect
from django.http import HttpResponseRedirect
def myview(request):
  ...
  return HttpResponseRedirect("/path/")
@lambdamusic
lambdamusic / Snipplr-34626.py
Created February 7, 2013 21:25
Django: Django | Testing Django applications
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 ...\'