Best Python code snippet using localstack_python
test_spatial.py
Source:test_spatial.py
1# encoding: utf-82from __future__ import absolute_import, division, print_function, unicode_literals3from django.contrib.gis.geos import GEOSGeometry4from django.test import TestCase5from haystack import connections6from haystack.exceptions import SpatialError7from haystack.query import SearchQuerySet8from haystack.utils.geo import (D, ensure_distance, ensure_geometry, ensure_point, ensure_wgs84,9 generate_bounding_box, Point)10from .models import Checkin11class SpatialUtilitiesTestCase(TestCase):12 def test_ensure_geometry(self):13 self.assertRaises(SpatialError, ensure_geometry, [38.97127105172941, -95.23592948913574])14 ensure_geometry(GEOSGeometry('POLYGON((-95 38, -96 40, -97 42, -95 38))'))15 ensure_geometry(GEOSGeometry('POINT(-95.23592948913574 38.97127105172941)'))16 ensure_geometry(Point(-95.23592948913574, 38.97127105172941))17 def test_ensure_point(self):18 self.assertRaises(SpatialError, ensure_point, [38.97127105172941, -95.23592948913574])19 self.assertRaises(SpatialError, ensure_point, GEOSGeometry('POLYGON((-95 38, -96 40, -97 42, -95 38))'))20 ensure_point(Point(-95.23592948913574, 38.97127105172941))21 def test_ensure_wgs84(self):22 self.assertRaises(SpatialError, ensure_wgs84, GEOSGeometry('POLYGON((-95 38, -96 40, -97 42, -95 38))'))23 orig_pnt = Point(-95.23592948913574, 38.97127105172941)24 std_pnt = ensure_wgs84(orig_pnt)25 self.assertEqual(orig_pnt.srid, None)26 self.assertEqual(std_pnt.srid, 4326)27 self.assertEqual(std_pnt.x, -95.23592948913574)28 self.assertEqual(std_pnt.y, 38.97127105172941)29 orig_pnt = Point(-95.23592948913574, 38.97127105172941)30 orig_pnt.set_srid(2805)31 std_pnt = ensure_wgs84(orig_pnt)32 self.assertEqual(orig_pnt.srid, 2805)33 self.assertEqual(std_pnt.srid, 4326)34 # These should be different, since it got transformed.35 self.assertNotEqual(std_pnt.x, -95.23592948913574)36 self.assertNotEqual(std_pnt.y, 38.97127105172941)37 def test_ensure_distance(self):38 self.assertRaises(SpatialError, ensure_distance, [38.97127105172941, -95.23592948913574])39 ensure_distance(D(mi=5))40 def test_generate_bounding_box(self):41 downtown_bottom_left = Point(-95.23947, 38.9637903)42 downtown_top_right = Point(-95.23362278938293, 38.973081081164715)43 ((min_lat, min_lng), (max_lat, max_lng)) = generate_bounding_box(downtown_bottom_left, downtown_top_right)44 self.assertEqual(min_lat, 38.9637903)45 self.assertEqual(min_lng, -95.23947)46 self.assertEqual(max_lat, 38.973081081164715)47 self.assertEqual(max_lng, -95.23362278938293)48 def test_generate_bounding_box_crossing_line_date(self):49 downtown_bottom_left = Point(95.23947, 38.9637903)50 downtown_top_right = Point(-95.23362278938293, 38.973081081164715)51 ((south, west), (north, east)) = generate_bounding_box(downtown_bottom_left, downtown_top_right)52 self.assertEqual(south, 38.9637903)53 self.assertEqual(west, 95.23947)54 self.assertEqual(north, 38.973081081164715)55 self.assertEqual(east, -95.23362278938293)56class SpatialSolrTestCase(TestCase):57 fixtures = ['sample_spatial_data.json']58 using = 'solr'59 def setUp(self):60 super(SpatialSolrTestCase, self).setUp()61 self.ui = connections[self.using].get_unified_index()62 self.checkindex = self.ui.get_index(Checkin)63 self.checkindex.reindex(using=self.using)64 self.sqs = SearchQuerySet().using(self.using)65 self.downtown_pnt = Point(-95.23592948913574, 38.97127105172941)66 self.downtown_bottom_left = Point(-95.23947, 38.9637903)67 self.downtown_top_right = Point(-95.23362278938293, 38.973081081164715)68 self.lawrence_bottom_left = Point(-95.345535, 39.002643)69 self.lawrence_top_right = Point(-95.202713, 38.923626)70 def tearDown(self):71 self.checkindex.clear(using=self.using)72 super(SpatialSolrTestCase, self).setUp()73 def test_indexing(self):74 # Make sure the indexed data looks correct.75 first = Checkin.objects.get(pk=1)76 sqs = self.sqs.models(Checkin).filter(django_id=first.pk)77 self.assertEqual(sqs.count(), 1)78 self.assertEqual(sqs[0].username, first.username)79 # Make sure we've got a proper ``Point`` object.80 self.assertAlmostEqual(sqs[0].location.get_coords()[0], first.longitude)81 self.assertAlmostEqual(sqs[0].location.get_coords()[1], first.latitude)82 # Double-check, to make sure there was nothing accidentally copied83 # between instances.84 second = Checkin.objects.get(pk=2)85 self.assertNotEqual(second.latitude, first.latitude)86 sqs = self.sqs.models(Checkin).filter(django_id=second.pk)87 self.assertEqual(sqs.count(), 1)88 self.assertEqual(sqs[0].username, second.username)89 self.assertAlmostEqual(sqs[0].location.get_coords()[0], second.longitude)90 self.assertAlmostEqual(sqs[0].location.get_coords()[1], second.latitude)91 def test_within(self):92 self.assertEqual(self.sqs.all().count(), 10)93 sqs = self.sqs.within('location', self.downtown_bottom_left, self.downtown_top_right)94 self.assertEqual(sqs.count(), 7)95 sqs = self.sqs.within('location', self.lawrence_bottom_left, self.lawrence_top_right)96 self.assertEqual(sqs.count(), 9)97 def test_dwithin(self):98 self.assertEqual(self.sqs.all().count(), 10)99 sqs = self.sqs.dwithin('location', self.downtown_pnt, D(mi=0.1))100 self.assertEqual(sqs.count(), 5)101 sqs = self.sqs.dwithin('location', self.downtown_pnt, D(mi=0.5))102 self.assertEqual(sqs.count(), 7)103 sqs = self.sqs.dwithin('location', self.downtown_pnt, D(mi=100))104 self.assertEqual(sqs.count(), 10)105 def test_distance_added(self):106 sqs = self.sqs.within('location', self.downtown_bottom_left, self.downtown_top_right).distance('location', self.downtown_pnt)107 self.assertEqual(sqs.count(), 7)108 self.assertAlmostEqual(sqs[0].distance.mi, 0.01985226)109 self.assertAlmostEqual(sqs[1].distance.mi, 0.03385863)110 self.assertAlmostEqual(sqs[2].distance.mi, 0.04539100)111 self.assertAlmostEqual(sqs[3].distance.mi, 0.04831436)112 self.assertAlmostEqual(sqs[4].distance.mi, 0.41116546)113 self.assertAlmostEqual(sqs[5].distance.mi, 0.25098114)114 self.assertAlmostEqual(sqs[6].distance.mi, 0.04831436)115 sqs = self.sqs.dwithin('location', self.downtown_pnt, D(mi=0.1)).distance('location', self.downtown_pnt)116 self.assertEqual(sqs.count(), 5)117 self.assertAlmostEqual(sqs[0].distance.mi, 0.01985226)118 self.assertAlmostEqual(sqs[1].distance.mi, 0.03385863)119 self.assertAlmostEqual(sqs[2].distance.mi, 0.04539100)120 self.assertAlmostEqual(sqs[3].distance.mi, 0.04831436)121 self.assertAlmostEqual(sqs[4].distance.mi, 0.04831436)122 def test_order_by_distance(self):123 sqs = self.sqs.within('location', self.downtown_bottom_left, self.downtown_top_right).distance('location', self.downtown_pnt).order_by('distance')124 self.assertEqual(sqs.count(), 7)125 self.assertEqual([result.pk for result in sqs], ['8', '9', '6', '3', '1', '2', '5'])126 self.assertEqual(["%0.04f" % result.distance.mi for result in sqs], ['0.0199', '0.0339', '0.0454', '0.0483', '0.0483', '0.2510', '0.4112'])127 sqs = self.sqs.dwithin('location', self.downtown_pnt, D(mi=0.1)).distance('location', self.downtown_pnt).order_by('distance')128 self.assertEqual(sqs.count(), 5)129 self.assertEqual([result.pk for result in sqs], ['8', '9', '6', '3', '1'])130 self.assertEqual(["%0.04f" % result.distance.mi for result in sqs], ['0.0199', '0.0339', '0.0454', '0.0483', '0.0483'])131 sqs = self.sqs.dwithin('location', self.downtown_pnt, D(mi=0.1)).distance('location', self.downtown_pnt).order_by('-distance')132 self.assertEqual(sqs.count(), 5)133 self.assertEqual([result.pk for result in sqs], ['3', '1', '6', '9', '8'])134 self.assertEqual(["%0.04f" % result.distance.mi for result in sqs], ['0.0483', '0.0483', '0.0454', '0.0339', '0.0199'])135 def test_complex(self):136 sqs = self.sqs.auto_query('coffee').within('location', self.downtown_bottom_left, self.downtown_top_right).distance('location', self.downtown_pnt).order_by('distance')137 self.assertEqual(sqs.count(), 5)138 self.assertEqual([result.pk for result in sqs], ['8', '6', '3', '1', '2'])139 self.assertEqual(["%0.04f" % result.distance.mi for result in sqs], ['0.0199', '0.0454', '0.0483', '0.0483', '0.2510'])140 sqs = self.sqs.auto_query('coffee').dwithin('location', self.downtown_pnt, D(mi=0.1)).distance('location', self.downtown_pnt).order_by('distance')141 self.assertEqual(sqs.count(), 4)142 self.assertEqual([result.pk for result in sqs], ['8', '6', '3', '1'])143 self.assertEqual(["%0.04f" % result.distance.mi for result in sqs], ['0.0199', '0.0454', '0.0483', '0.0483'])144 sqs = self.sqs.auto_query('coffee').dwithin('location', self.downtown_pnt, D(mi=0.1)).distance('location', self.downtown_pnt).order_by('-distance')145 self.assertEqual(sqs.count(), 4)146 self.assertEqual([result.pk for result in sqs], ['3', '1', '6', '8'])147 self.assertEqual(["%0.04f" % result.distance.mi for result in sqs], ['0.0483', '0.0483', '0.0454', '0.0199'])148 sqs = self.sqs.auto_query('coffee').within('location', self.downtown_bottom_left, self.downtown_top_right).distance('location', self.downtown_pnt).order_by('-created')149 self.assertEqual(sqs.count(), 5)150 self.assertEqual([result.pk for result in sqs], ['8', '6', '3', '2', '1'])151 sqs = self.sqs.auto_query('coffee').dwithin('location', self.downtown_pnt, D(mi=0.1)).distance('location', self.downtown_pnt).order_by('-created')152 self.assertEqual(sqs.count(), 4)...
views.py
Source:views.py
1from django.views.generic import ListView2from django.shortcuts import render, get_object_or_4043from django.http import HttpResponse4from django.template import loader, Context5from haystack.forms import FacetedSearchForm6from haystack.query import SearchQuerySet7from haystack.views import FacetedSearchView8from haystack.inputs import Raw9from templatetags.display import as_xml10from models import *11# Create your views here.12class XMLDocumentList(ListView):13 model = XMLDocument14 context_object_name = "xmldocument_list"15 template_name = "xmldocument_list.html"16 queryset = XMLDocument.objects.order_by('filename')17#def index(request):18# return HttpResponse('Hello, %d documents indexed' % XMLDocument.objects.count())19sqs = SearchQuerySet()20sqs = sqs.facet('contributor')21sqs = sqs.facet('coverage')22sqs = sqs.facet('creator')23sqs = sqs.facet('application')24sqs = sqs.facet('date')25sqs = sqs.facet('format')26sqs = sqs.facet('languages')27sqs = sqs.facet('publisher')28sqs = sqs.facet('countries')29sqs = sqs.facet('themes')30sqs = sqs.facet('firsttag')31sqs = sqs.facet('length')32sqs = sqs.facet('schema')33sqs = sqs.facet('places')34sqs = sqs.facet('events')35sqs = sqs.facet('persons')36sqs = sqs.facet('tags')37sqs = sqs.facet('organizations')38sqs = sqs.facet('artifacts')39sqs = sqs.facet('references')40#sqs = sqs.facet('location', precision=3)41#sqs = sqs.filter(location=Raw('{"aggregations": { "lg": { "geohash_grid": { "field": "location", "precision": 3}}}}'))42class XMLFacetedSearchForm(FacetedSearchForm):43 def no_query_found(self):44 return self.searchqueryset.all()45class XMLFacetedSearchView(FacetedSearchView):46 searchqueryset=sqs47 def __init__(self, *args, **kwargs):48 if kwargs.get('searchqueryset') is None:49 kwargs['searchqueryset'] = sqs50 if kwargs.get('form_class') is None:51 kwargs['form_class'] = XMLFacetedSearchForm52 super(XMLFacetedSearchView, self).__init__(*args, **kwargs)53 def create_response(self):54 if not self.request.META.get('QUERY_STRING', ''):55 print "Patching QUERY_STRING"56 self.request.META['QUERY_STRING']="q="57 return super(XMLFacetedSearchView, self).create_response()58 def extra_context_(self):59 extra = super(XMLFacetedSearchView, self).extra_context()60 print self.results61 if self.query == '':62 extra['facets'] = sqs.facet_counts()63 return extra64def document(request, document_id):65 xmldocument = get_object_or_404(XMLDocument, pk=document_id)66 return render(request, 'main/xmldocument.html', {'xmldocument': xmldocument})67def download(request, document_id):68 xmldocument = get_object_or_404(XMLDocument, pk=document_id)69 filename = xmldocument.filename.rsplit("/")[-1]70 response = HttpResponse(content_type='text/xml')71 response['Content-Disposition'] = 'attachment; filename="%s"' % filename72 response.write(as_xml(xmldocument.contents))...
infinite_sqs.py
Source:infinite_sqs.py
1def all_sqs():2 n = 03 while True:4 yield n*n5 n += 16sqs_infinite = all_sqs()7print(next(sqs_infinite))8print(next(sqs_infinite))9print(next(sqs_infinite))10print(next(sqs_infinite))11print(next(sqs_infinite))12print(next(sqs_infinite))13print(next(sqs_infinite))14print(next(sqs_infinite))15print(next(sqs_infinite))16print(next(sqs_infinite))17print(next(sqs_infinite))18print(next(sqs_infinite))19print(next(sqs_infinite))20print(next(sqs_infinite))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!