Skip to content

Instantly share code, notes, and snippets.

@msecret
Created August 6, 2015 17:20
Show Gist options
  • Save msecret/a96ebbb55c94c47de4bd to your computer and use it in GitHub Desktop.
Save msecret/a96ebbb55c94c47de4bd to your computer and use it in GitHub Desktop.
A py code review
from django import db
from django.db import transaction
from django.conf import settings
from apidata.models import Cfda
import csv
from dateutil.parser import *
import traceback
@transaction.atomic
def load_data:
def clean_flag(s):
if s.strip().lower() == 'yes':
return True
else:
return False
update_count = 0
insert_count = 0
data_reader = csv.reader(open('a-csv-file.csv''))
for i, row in enumerate(data_reader):
if i == 0:
header_row = row
else:
try:
program_number = row[1]
record = Cfda.objects.get(program_number=program_number)
update_count = update_count + 1
except:
record = Cfda()
record.program_number=program_number
insert_count = insert_count + 1
record.program_title = row[0]
record.popular_name = row[2]
record.recovery_flag = clean_flag(row[35])
try:
record.save()
db.reset_queries()
except:
traceback.print_exc()
for j,col in enumerate(row):
print '%s: %s %s' % (header_row[j], len(col), col.decode("windows-1252"))
return
print 'CFDA loaded: %s records inserted, %s updated' % (insert_count, update_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment