import schedules, but lots of errors

This commit is contained in:
Sanj 2012-02-25 00:10:25 +05:30
parent 8dc7f32ca4
commit 33e912c2de
4 changed files with 109 additions and 2 deletions

View File

@ -101,6 +101,7 @@ INSTALLED_APPS = (
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.humanize', 'django.contrib.humanize',
'django.contrib.gis', 'django.contrib.gis',
'django_extensions',
'trains', 'trains',
) )

View File

@ -4,6 +4,9 @@ import json
from os.path import join from os.path import join
import re import re
import datetime import datetime
from decimal import Decimal
from django.contrib.gis.geos import Point
import csv2kml
def import_stations(): def import_stations():
stations = json.load(open(join(DATA_DIR, "stations.json"))) stations = json.load(open(join(DATA_DIR, "stations.json")))
@ -162,8 +165,110 @@ def getDate(s):
date = int(matches[0][1]) date = int(matches[0][1])
return "%02d-%02d" % (monthInt, date,) return "%02d-%02d" % (monthInt, date,)
def getMinutes(s):
if s is None:
return None
s = s.replace('m', '').strip()
if s == '':
return None
else:
return s
def getBool(s): def getBool(s):
if s == None or int(s) == 0: if s == None or int(s) == 0:
return False return False
else: else:
return True return True
def import_schedule():
schedules = json.load(open(join(DATA_DIR, "schedule.json")))
errors = []
for s in schedules:
schedule = Schedule()
schedule.data_id = s['id']
schedule.arrival = getTime(s['arrival'])
schedule.departure = getTime(s['departure'])
schedule.halt = getMinutes(s['halt'])
try:
schedule.stop_number = Decimal(s['stop_number'])
except:
errors.append({
'typ': 'Invalid Stop Number',
'data_id': s['id'],
'stop_number': s['stop_number']
})
schedule.stop_number = Decimal('0')
try:
schedule.station = Station.objects.get(code=s['station_code'])
except:
schedule.station = Station.objects.filter(code=s['station_code'])[0]
errors.append({
'typ': 'Invalid or non-unique station code (NOT IMPORTED)',
'data_id': s['id'],
'code': s['station_code']
})
continue
try:
schedule.train = Train.objects.get(number=s['train_number'])
except:
schedule.train = Train.objects.filter(number=s['train_number'])[0]
errors.append({
'typ': 'Invalid or non-unique train code (NOT IMPORTED)',
'data_id': s['id'],
'train_number': s['train_number']
})
continue
try:
schedule.day = int(s['day'])
except:
errors.append({
'typ': 'Day Error',
'data_id': s['id'],
'day': s['day']
})
schedule.day = 0
try:
schedule.distance_travelled = int(s['distance_travelled'])
except:
errors.append({
'typ': 'Day Error',
'data_id': s['id'],
'distance': s['distance_travelled']
})
schedule.distance_travelled = 0
try:
schedule.save()
except:
errors.append({
'typ': 'Something foobarred on Save :(',
'data_id': s['id']
})
errorsFile = open(join(DATA_DIR, "scheduleErrors.json"), "w")
json.dump(errors, errorsFile, indent=2)
errorsFile.close()
def geolocate_stations():
errors = []
for s in Station.objects.filter(point=None)[0:10]:
coords = csv2kml.get_coordinates(s.address)
if coords is not None:
pt = Point(coords[0], coords[1])
s.point = pt
print s.name
s.save()
else:
errors.append({
'typ': 'Failed to geolocate',
'data_id': s.data_id,
'address': s.address
})
errorsFile = open(join(DATA_DIR, "geolocateErrors.json"), "w")
json.dump(errors, errorsFile, indent=2)
errorsFile.close()
def import_all():
import_stations()
import_trains()
import_schedules()

View File

@ -61,8 +61,8 @@ class Train(models.Model):
class Schedule(models.Model): class Schedule(models.Model):
data_id = models.IntegerField() data_id = models.IntegerField()
arrival = models.TimeField() # if blank, get departure time from train models arrival = models.TimeField(null=True) # if blank, get departure time from train models
departure = models.TimeField() departure = models.TimeField(null=True)
halt = models.IntegerField(null=True) halt = models.IntegerField(null=True)
stop_number = models.DecimalField(max_digits=3, decimal_places=2) stop_number = models.DecimalField(max_digits=3, decimal_places=2)
station = models.ForeignKey(Station) station = models.ForeignKey(Station)

View File

@ -1,2 +1,3 @@
django==1.3 django==1.3
-e bzr+http://code.0x2620.org/python-ox/#egg=python-ox -e bzr+http://code.0x2620.org/python-ox/#egg=python-ox
-e git://github.com/bit/django-extensions.git#egg=django_extensions