Created
May 11, 2015 15:21
-
-
Save jwalgran/63fea8966bdfb05b0c33 to your computer and use it in GitHub Desktop.
A management command to generate random surveys for NYC TreesCount testing
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
from __future__ import print_function | |
from __future__ import unicode_literals | |
from __future__ import division | |
from random import SystemRandom | |
from django.core.management.base import BaseCommand | |
from django.db import transaction | |
from apps.core.models import User | |
from apps.survey.models import Survey, Blockface | |
def _create_surveys(user, min_id, max_id, num): | |
r = SystemRandom() | |
created = 0 | |
for x in xrange(num): | |
id = r.randint(min_id, max_id) | |
try: | |
b = Blockface.objects.get(pk=id) | |
Survey.objects.create( | |
user=user, blockface=b, has_trees=False, | |
is_left_side=False, | |
is_mapped_in_blockface_polyline_direction=False) | |
created = created + 1 | |
except Blockface.DoesNotExist: | |
pass | |
return created | |
class Command(BaseCommand): | |
""" | |
Print the id of any Survey that triggers and exception when | |
running the "geometry constructor." | |
Usage: | |
./manage.py find_invalid_surveys | |
""" | |
@transaction.atomic | |
def handle(self, *args, **options): | |
u = User.objects.get(pk=1) | |
print(_create_surveys(u, 100000, 199999, 5000)) | |
print(_create_surveys(u, 200000, 299999, 10000)) | |
print(_create_surveys(u, 300000, 399999, 3000)) | |
print(_create_surveys(u, 400000, 499999, 20000)) | |
print(_create_surveys(u, 500000, 599999, 500)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment