Skip to content

Instantly share code, notes, and snippets.

View zstumgoren's full-sized avatar

Serdar Tumgoren zstumgoren

View GitHub Profile
base_query = Casualty.safe_objects.all()
def build_chart(base_query, field_name, order_by):
'''
This is a function to make building our charts a little DRYer. Beats the pants off of lambdas.
'''
return base_query.values(field_name).annotate(Count(field_name)).order_by(order_by)
extra_context_dict = {
'theater_chart':build_chart(base_query, 'theater__name', '-theater__name__count'),
@zstumgoren
zstumgoren / process_csv.py
Created April 9, 2011 18:02
Sample script on how to use Python's csv DictReader class to process tabular data.
#!/usr/bin/env python
import csv
source_file = open('/path/to/Testfile.txt','rb')
# Use the DictReader class to read in your data. This class allows you to access
# field names by name. It assumes the first line in the file contains the field names
for line in csv.DictReader(source_file, delimiter='\t'):
@zstumgoren
zstumgoren / simple_file_processing.py
Created April 9, 2011 17:24
Read in some data. Skip
#!/usr/bin/env python
"""
A simple script showing how to read data from a specified
file, do some basic processing, and then print out processed
lines to the shell. The input file is hard-coded in the text.
USAGE from command line
python simple_file_processing.py