def display(self, iterations=500): x_vals = np.empty(iterations, dtype="d") y_vals = np.empty(iterations, dtype="d") self.network.reset() # we ignore first 100 iterations since those are transients for _ in xrange(0, 100): self.network.test_one() for idx in xrange(0, iterations): self.network.test_one() x_vals[idx] = self.network.total_activation() self.network.test_one() y_vals[idx] = self.network.total_activation() color_map = np.arctan2(y_vals, x_vals) plt.scatter(x_vals, y_vals, s=42, c=color_map, lw=0) edge_r = (max(x_vals) - min(x_vals)) / 100 edge_v = (max(y_vals) - min(y_vals)) / 100 plt.xlim(min(x_vals) - edge_r, max(x_vals) + edge_r) plt.ylim(min(y_vals) - edge_v, max(y_vals) + edge_v) plt.show()
def choice_flashcard_template(headings, prompt, answer): deck = FlashcardDeck() # set up headings for heading in headings: new_heading = deck.headings.new_item() new_heading.text = heading deck.headings.array.append(new_heading) # set up a default interaction ci = ChoiceInteraction() ci.prompt = str(prompt) ci.answer = str(answer) deck.interactions.array.append(deck.interactions.new_item()) deck.interactions.array[0].store(ci, False) # create four empty rows (for now, at least) for i in xrange(4): card = Flashcard() for j in xrange(len(headings)): card.sides.array.append(card.sides.new_item()) new_card = deck.cards.new_item() new_card.store(card, False) deck.cards.array.append(new_card) return deck
def test_buffer(self): "Testing buffer()." for bg in self.geometries.buffer_geoms: g = fromstr(bg.wkt) # The buffer we expect exp_buf = fromstr(bg.buffer_wkt) quadsegs = bg.quadsegs width = bg.width # Can't use a floating-point for the number of quadsegs. self.assertRaises(ctypes.ArgumentError, g.buffer, width, float(quadsegs)) # Constructing our buffer buf = g.buffer(width, quadsegs) self.assertEqual(exp_buf.num_coords, buf.num_coords) self.assertEqual(len(exp_buf), len(buf)) # Now assuring that each point in the buffer is almost equal for j in xrange(len(exp_buf)): exp_ring = exp_buf[j] buf_ring = buf[j] self.assertEqual(len(exp_ring), len(buf_ring)) for k in xrange(len(exp_ring)): # Asserting the X, Y of each point are almost equal (due to floating point imprecision) self.assertAlmostEqual(exp_ring[k][0], buf_ring[k][0], 9) self.assertAlmostEqual(exp_ring[k][1], buf_ring[k][1], 9)
def display_weights(self): logger.info(" {0}➞{1}:".format(self.from_module.name, self.to_module.name)) for j in xrange(0, len(self.from_module.r)): s = " " for i in xrange(0, len(self.to_module.r)): s += "{:10.6f}".format(self.weights[i, j]) logger.info(s)
def init_v_weights(self): """Initialize connections to R nodes from V with values dependent on distance between R and V node pair.""" if self.size % 2 == 0: mdl_size = self.size else: mdl_size = self.size + 1 self.v_to_r = np.empty((self.size, self.size), dtype="d") middle = int(floor(mdl_size / 2.0)) n = np.float64(mdl_size) # calculate optimal sigma sigma = (-4.0 / n) * np.log((0.01 + np.exp(-0.25 * n)) / (n + 1.0)) v_weights = [] for i in range(0, middle + 1): v_weights.append((n + 1.0) * np.exp(0.0 - (sigma * i * i) / n) - n - 1.0 + self.parameters["DOWN"]) for i in xrange(0, self.size): for j in xrange(0, self.size): dist = abs(i - j) # distance between R and V node if dist > middle: dist = mdl_size - dist # correct for size # SIGMA depends on module size. A module size up to 20 has # experimentally been defined to have an optimal sigma around 0.06. # With more nodes, this sigma slightly increases. # With 64 nodes, sigma should be picked around 0.15 # see https://www.dropbox.com/s/8d0u9o71sn4sbrh/gaussian.pdf self.v_to_r[i, j] = v_weights[dist] with printoptions(formatter={"float": "{: 0.2f}".format}, suppress=True): logger.info("Sigma: {0}\nV to R weights: {1}".format(sigma, self.v_to_r))
def test_render_stars(self): max_test_stars = 50 for max_stars in xrange(1, max_test_stars + 1): for num in xrange(max_stars + 1): stars = render_stars(num, max_stars, self.star_set) self.assertEqual(len(stars), max_stars) self.assertEqual(stars.count(self.star_set['star']), num) self.assertEqual( stars.count(self.star_set['unlit']), max_stars - num)
def test_mutable_geometries(self): "Testing the mutability of Polygons and Geometry Collections." ### Testing the mutability of Polygons ### for p in self.geometries.polygons: poly = fromstr(p.wkt) # Should only be able to use __setitem__ with LinearRing geometries. self.assertRaises(TypeError, poly.__setitem__, 0, LineString((1, 1), (2, 2))) # Constructing the new shell by adding 500 to every point in the old shell. shell_tup = poly.shell.tuple new_coords = [] for point in shell_tup: new_coords.append((point[0] + 500., point[1] + 500.)) new_shell = LinearRing(*tuple(new_coords)) # Assigning polygon's exterior ring w/the new shell poly.exterior_ring = new_shell str(new_shell) # new shell is still accessible self.assertEqual(poly.exterior_ring, new_shell) self.assertEqual(poly[0], new_shell) ### Testing the mutability of Geometry Collections for tg in self.geometries.multipoints: mp = fromstr(tg.wkt) for i in range(len(mp)): # Creating a random point. pnt = mp[i] new = Point(random.randint(21, 100), random.randint(21, 100)) # Testing the assignment mp[i] = new str(new) # what was used for the assignment is still accessible self.assertEqual(mp[i], new) self.assertEqual(mp[i].wkt, new.wkt) self.assertNotEqual(pnt, mp[i]) # MultiPolygons involve much more memory management because each # Polygon w/in the collection has its own rings. for tg in self.geometries.multipolygons: mpoly = fromstr(tg.wkt) for i in xrange(len(mpoly)): poly = mpoly[i] old_poly = mpoly[i] # Offsetting the each ring in the polygon by 500. for j in xrange(len(poly)): r = poly[j] for k in xrange(len(r)): r[k] = (r[k][0] + 500., r[k][1] + 500.) poly[j] = r self.assertNotEqual(mpoly[i], poly) # Testing the assignment mpoly[i] = poly str(poly) # Still accessible self.assertEqual(mpoly[i], poly) self.assertNotEqual(mpoly[i], old_poly)
def __init__(self, *args, **kwargs): """ Initializes on the given sequence -- may take lists, tuples, NumPy arrays of X,Y pairs, or Point objects. If Point objects are used, ownership is _not_ transferred to the LineString object. Examples: ls = LineString((1, 1), (2, 2)) ls = LineString([(1, 1), (2, 2)]) ls = LineString(array([(1, 1), (2, 2)])) ls = LineString(Point(1, 1), Point(2, 2)) """ # If only one argument provided, set the coords array appropriately if len(args) == 1: coords = args[0] else: coords = args if isinstance(coords, (tuple, list)): # Getting the number of coords and the number of dimensions -- which # must stay the same, e.g., no LineString((1, 2), (1, 2, 3)). ncoords = len(coords) if coords: ndim = len(coords[0]) else: raise TypeError('Cannot initialize on empty sequence.') self._checkdim(ndim) # Incrementing through each of the coordinates and verifying for i in xrange(1, ncoords): if not isinstance(coords[i], (tuple, list, Point)): raise TypeError('each coordinate should be a sequence (list or tuple)') if len(coords[i]) != ndim: raise TypeError('Dimension mismatch.') numpy_coords = False elif numpy and isinstance(coords, numpy.ndarray): shape = coords.shape # Using numpy's shape. if len(shape) != 2: raise TypeError('Too many dimensions.') self._checkdim(shape[1]) ncoords = shape[0] ndim = shape[1] numpy_coords = True else: raise TypeError('Invalid initialization input for LineStrings.') # Creating a coordinate sequence object because it is easier to # set the points using GEOSCoordSeq.__setitem__(). cs = GEOSCoordSeq(capi.create_cs(ncoords, ndim), z=bool(ndim==3)) for i in xrange(ncoords): if numpy_coords: cs[i] = coords[i,:] elif isinstance(coords[i], Point): cs[i] = coords[i].tuple else: cs[i] = coords[i] # If SRID was passed in with the keyword arguments srid = kwargs.get('srid', None) # Calling the base geometry initialization with the returned pointer # from the function. super(LineString, self).__init__(self._init_func(cs.ptr), srid=srid)
def display(self, input_mdl, input_idx, width=10, step=0.001, iterations=500): step = np.float64(step) cur_val = input_mdl.r[input_idx] start = cur_val - (width * step) if start < 0.0: logger.error("Range will be out of bounds!") return end = cur_val + (width * step) if end > 1.0: logger.error("Range will be out of bounds!") return x_vals = np.empty(iterations * (2 * width + 1), dtype="d") y_vals = np.empty(iterations * (2 * width + 1), dtype="d") values = np.arange(start, end + step, step) idx = 0 for x in values: input_mdl.r[input_idx] = x self.network.reset() # we ignore first 100 iterations since those are transients for _ in xrange(0, 100): self.network.test_one() for _ in xrange(0, iterations): self.network.test_one() try: x_vals[idx] = self.network.total_activation() except IndexError: break self.network.test_one() try: y_vals[idx] = self.network.total_activation() except IndexError: break print("{0}: {1}".format(x_vals[idx], y_vals[idx])) idx += 1 else: continue break color_map = np.arctan2(y_vals, x_vals) plt.scatter(x_vals, y_vals, s=42, c=color_map, lw=0) edge_r = (max(x_vals) - min(x_vals)) / 10 edge_v = (max(y_vals) - min(y_vals)) / 10 plt.xlim(min(x_vals) - edge_r, max(x_vals) + edge_r) plt.ylim(min(y_vals) - edge_v, max(y_vals) + edge_v) plt.show()
def test_column_search_formatted_column(self): '''Should filter on a formatted column''' for _ in xrange(3): BrowserFactory() for _ in xrange(2): BrowserFactory(name='test') response = self.get_response('formatted-browsers', self.build_query(sSearch_1='tes')) data = json.loads(response.content.decode()) self.assertEqual(len(data['aaData']), 2) for row in data['aaData']: self.assertTrue(self.value(row, NAME).startswith('test'))
def test_column_search_regex(self): '''Should filter on a single column with regex''' for _ in xrange(3): BrowserFactory() for _ in xrange(2): BrowserFactory(name='test') response = self.get_response('browsers', self.build_query(sSearch_1='[tes]{4}', bRegex_1=True)) data = json.loads(response.content.decode()) self.assertEqual(len(data['aaData']), 2) for row in data['aaData']: self.assertEqual(self.value(row, NAME), 'test')
def __init__(self, *args, **kwargs): super(DatatablesForm, self).__init__(*args, **kwargs) for idx in xrange(int(self.data['iColumns'])): self.fields['mDataProp_%s' % idx] = forms.CharField(required=False) self.fields['sSearch_%s' % idx] = forms.CharField(required=False) self.fields['bRegex_%s' % idx] = forms.BooleanField(required=False) self.fields['bSortable_%s' % idx] = forms.BooleanField(required=False) self.fields['bSearchable_%s' % idx] = forms.BooleanField(required=False) for idx in xrange(int(self.data['iSortingCols'])): self.fields['iSortCol_%s' % idx] = forms.IntegerField(required=True) self.fields['sSortDir_%s' % idx] = forms.ChoiceField(required=True, choices=SORT_DIRS)
def _generate_code(self): qs = self.__class__.objects for attempt in xrange(500): # 500 attempts really REALLY should be enough. n_digits = randint(CODE_MIN_N_DIGITS, CODE_MAX_N_DIGITS + 1) code = ("".join(choice(digits) for x in xrange(n_digits))) if not CODE_ALLOW_LEADING_ZEROES: code = code.lstrip("0") # Leading zeroes could have dropped digits off the code, so recheck that. if CODE_MIN_N_DIGITS <= len(code) <= CODE_MAX_N_DIGITS: if not qs.filter(code=code).exists(): return code raise ValueError("Unable to find an unused code! Is the keyspace exhausted?")
def test_render_string_numbers(self): """ String representations of integers are rendered in the usual manner """ max_test_stars = 50 for max_stars in xrange(1, max_test_stars + 1): for num in xrange(max_stars + 1): num = str(num) max_stars = str(max_stars) stars = render_stars(num, max_stars, self.star_set) self.assertEqual(len(stars), int(max_stars)) self.assertEqual(stars.count(self.star_set['star']), int(num)) self.assertEqual( stars.count(self.star_set['unlit']), int(max_stars) - int(num))
def test_column_search_many_columns(self): '''Should filter on many columns''' for _ in xrange(2): BrowserFactory(name='test') for _ in xrange(3): BrowserFactory(engine__name='engine') for _ in xrange(4): BrowserFactory(name='test', engine__name='engine') response = self.get_response('browsers', self.build_query(sSearch_0='eng', sSearch_1='tes')) data = json.loads(response.content.decode()) self.assertEqual(len(data['aaData']), 4) for row in data['aaData']: self.assertEqual(self.value(row, ENGINE_NAME), 'engine') self.assertEqual(self.value(row, NAME), 'test')
def test_is_cyclic(self): sut = RoundRobinRoutingStrategy(settings.DATABASES) expected_cycled_shards = ['app_shard_001_replica_001', 'app_shard_001_replica_002', 'app_shard_001'] expected_cycled_shards.sort() resulting_shards = [sut.pick_read_db('app_shard_001') for i in xrange(150)] self.assertEqual(len(set([resulting_shards[i] for i in xrange(0, 150, 3)])), 1) self.assertEqual(len(set([resulting_shards[i] for i in xrange(1, 150, 3)])), 1) self.assertEqual(len(set([resulting_shards[i] for i in xrange(2, 150, 3)])), 1) resulting_cycled_shard = resulting_shards[:3] resulting_cycled_shard.sort() self.assertEqual(expected_cycled_shards, resulting_cycled_shard)
def disconnect(self, receiver=None, sender=None, weak=True, dispatch_uid=None): """ Disconnect receiver from sender for signal. If weak references are used, disconnect need not be called. The receiver will be remove from dispatch automatically. Arguments: receiver The registered receiver to disconnect. May be none if dispatch_uid is specified. sender The registered sender to disconnect weak The weakref state to disconnect dispatch_uid the unique identifier of the receiver to disconnect """ if dispatch_uid: lookup_key = (dispatch_uid, _make_id(sender)) else: lookup_key = (_make_id(receiver), _make_id(sender)) with self.lock: self._clear_dead_receivers() for index in xrange(len(self.receivers)): (r_key, _) = self.receivers[index] if r_key == lookup_key: del self.receivers[index] break self.sender_receivers_cache.clear()
def __init__(self, *args, **kwargs): """ A class for generating sets of Google Maps that will be shown on the same page together. Example: gmapset = GoogleMapSet( GoogleMap( ... ), GoogleMap( ... ) ) gmapset = GoogleMapSet( [ gmap1, gmap2] ) """ # The `google-multi.js` template is used instead of `google-single.js` # by default. template = kwargs.pop('template', 'gis/google/google-multi.js') # This is the template used to generate the GMap load JavaScript for # each map in the set. self.map_template = kwargs.pop('map_template', 'gis/google/google-single.js') # Running GoogleMap.__init__(), and resetting the template # value with default obtained above. super(GoogleMapSet, self).__init__(**kwargs) self.template = template # If a tuple/list passed in as first element of args, then assume if isinstance(args[0], (tuple, list)): self.maps = args[0] else: self.maps = args # Generating DOM ids for each of the maps in the set. self.dom_ids = ['map%d' % i for i in xrange(len(self.maps))]
def __getitem__(self, index): "Get the item(s) at the specified index/slice." if isinstance(index, slice): return [self._get_single_external(i) for i in xrange(*index.indices(len(self)))] else: index = self._checkindex(index) return self._get_single_external(index)
def __init__(self, num_zoom=19, tilesize=256): "Initializes the Google Zoom object." # Google's tilesize is 256x256, square tiles are assumed. self._tilesize = tilesize # The number of zoom levels self._nzoom = num_zoom # Initializing arrays to hold the parameters for each one of the # zoom levels. self._degpp = [] # Degrees per pixel self._radpp = [] # Radians per pixel self._npix = [] # 1/2 the number of pixels for a tile at the given zoom level # Incrementing through the zoom levels and populating the parameter arrays. z = tilesize # The number of pixels per zoom level. for i in xrange(num_zoom): # Getting the degrees and radians per pixel, and the 1/2 the number of # for every zoom level. self._degpp.append(z / 360.) # degrees per pixel self._radpp.append(z / (2 * pi)) # radians per pixel self._npix.append(z / 2) # number of pixels to center of tile # Multiplying `z` by 2 for the next iteration. z *= 2
def test_coord_seq(self): "Testing Coordinate Sequence objects." for p in self.geometries.polygons: if p.ext_ring_cs: # Constructing the polygon and getting the coordinate sequence poly = fromstr(p.wkt) cs = poly.exterior_ring.coord_seq self.assertEqual(p.ext_ring_cs, cs.tuple) # done in the Polygon test too. self.assertEqual(len(p.ext_ring_cs), len(cs)) # Making sure __len__ works # Checks __getitem__ and __setitem__ for i in xrange(len(p.ext_ring_cs)): c1 = p.ext_ring_cs[i] # Expected value c2 = cs[i] # Value from coordseq self.assertEqual(c1, c2) # Constructing the test value to set the coordinate sequence with if len(c1) == 2: tset = (5, 23) else: tset = (5, 23, 8) cs[i] = tset # Making sure every set point matches what we expect for j in range(len(tset)): cs[i] = tset self.assertEqual(tset[j], cs[i][j])
def forms(self): """ Instantiate forms at first property access. """ # DoS protection is included in total_form_count() forms = [self._construct_form(i) for i in xrange(self.total_form_count())] return forms
def check_dates(self, valid_dates, title, one_year=True): """ Checks the dates in the given valid_dates dict. Also check that the event title appears on the page when it's supposed to. """ for year, dates in valid_dates.items(): for month, days in dates.items(): response = self.client.get(reverse( 'calendar:list', kwargs={'year': year, 'month': month} )) self.clean_whitespace(response) if days: self.assertContains(response, title) else: self.assertNotContains(response, title) for day in days: # days given should appear on the calendar [self.assertContains(response, self.cal_str(day)) for day in days] # days not given should not appear on the calendar [self.assertNotContains(response, self.cal_str(day)) for day in xrange(1, monthrange(2014, int(month))[1] + 1) if day not in days] if one_year: response = self.client.get(reverse( 'calendar:list', kwargs={ 'year': str(int(year) + 1), 'month': month } )) self.clean_whitespace(response) self.assertNotContains(response, title)
def get_zoom(self, geom): "Returns the optimal Zoom level for the given geometry." # Checking the input type. if not isinstance(geom, GEOSGeometry) or geom.srid != 4326: raise TypeError('get_zoom() expects a GEOS Geometry with an SRID of 4326.') # Getting the envelope for the geometry, and its associated width, height # and centroid. env = geom.envelope env_w, env_h = self.get_width_height(env.extent) center = env.centroid for z in xrange(self._nzoom): # Getting the tile at the zoom level. tile_w, tile_h = self.get_width_height(self.tile(center, z).extent) # When we span more than one tile, this is an approximately good # zoom level. if (env_w > tile_w) or (env_h > tile_h): if z == 0: raise GoogleMapException('Geometry width and height should not exceed that of the Earth.') return z-1 # Otherwise, we've zoomed in to the max. return self._nzoom-1
def performance_check(self, iterations=100): """Tests the network on each pattern and reports activations and winners.""" for pat in self.patterns: with printoptions(formatter={"float": "{: 0.2f}".format}, suppress=True): for (k, v) in six.iteritems(pat): logger.info("Pattern: {0}: {1}".format(k, v)) # reset activations for mdl in self.modules: mdl.reset() # set pattern for (k, v) in six.iteritems(pat): self.inputs[k].r = v # activation flow for _ in xrange(0, iterations): # update activations for mdl in self.modules: mdl.activate() for mdl in self.modules: mdl.swap_activations() self.check_convergence() with printoptions(formatter={"float": "{: 0.2f}".format}, suppress=True): for mdl in self.modules: logger.info("Module {0}: {1} ➞ {2}".format(mdl.name, mdl.r, mdl.winner))
def test_list_view_with_daily_repeat(self): event = create_event( start_date=(2014, 4, 1), end_date=(2014, 4, 1), created_by=self.user, title="Casey", description="I repeat every day with no end in sight.", repeat='DAILY', ) m = ['04', '05', '06'] y = ['2014', '2015', '2016'] valid_dates = dict.fromkeys(y, m) for year, months in valid_dates.items(): for month in months: response = self.client.get(reverse( 'calendar:list', kwargs={'year': year, 'month': month} )) self.clean_whitespace(response) self.assertContains(response, event.title) [self.assertContains(response, self.cal_str(day)) for day in xrange(1, monthrange(2014, int(month) + 1)[1])] # The month before start_date shouldn't have any events response = self.client.get(reverse( 'calendar:list', kwargs={'year': '2014', 'month': '03'} )) self.clean_whitespace(response) self.assertNotContains(response, event.title) self.assertNotContains(response, self.event_div)
def test_picking_is_cyclic(self): class FakeUser(): username = None expected_cycle = ['app_shard_001', 'app_shard_002'] sut = RoundRobinBucketingStrategy(shard_group='default', databases=settings.DATABASES) resulting_shards = [sut.pick_shard(FakeUser()) for i in xrange(100)] self.assertEqual(len(set([resulting_shards[i] for i in xrange(0, 100, 2)])), 1) self.assertEqual(len(set([resulting_shards[i] for i in xrange(1, 100, 2)])), 1) resulting_cycled_shard = resulting_shards[:2] resulting_cycled_shard.sort() self.assertEqual(resulting_cycled_shard, expected_cycle)
def as_sql(self, qn, connection): max_in_list_size = connection.ops.max_in_list_size() if self.rhs_is_direct_value() and (max_in_list_size and len(self.rhs) > max_in_list_size): # This is a special case for Oracle which limits the number of elements # which can appear in an 'IN' clause. lhs, lhs_params = self.process_lhs(qn, connection) rhs, rhs_params = self.batch_process_rhs(qn, connection) in_clause_elements = ['('] params = [] for offset in xrange(0, len(rhs_params), max_in_list_size): if offset > 0: in_clause_elements.append(' OR ') in_clause_elements.append('%s IN (' % lhs) params.extend(lhs_params) sqls = rhs[offset: offset + max_in_list_size] sqls_params = rhs_params[offset: offset + max_in_list_size] param_group = ', '.join(sqls) in_clause_elements.append(param_group) in_clause_elements.append(')') params.extend(sqls_params) in_clause_elements.append(')') return ''.join(in_clause_elements), params else: return super(In, self).as_sql(qn, connection)
def clean_year_month_day(year, month, day, net): error = False error_msg = "The date given was invalid." # check that the month is within 1-12 if month not in xrange(1, 13): month = now.month error = error_msg # check that the day is within range for the month if day not in xrange(1, monthrange(year, month)[1] + 1): day = 1 error = error_msg # if no error yet, increment the day by net then check the year if not error: year, month, day = _inc_day(year, month, day, net) year, month, error = _check_year(year, month, error, error_msg) return year, month, day, error
def clean_year_month(year, month, month_orig): """ If 'month_orig', which is the month given in the url BEFORE any next/prev query strings have been applied, is out of range, sets month to the current month and returns an error message. Also Returns an error message if the year given is +/- 50 years from now. If 'month', which is the month given in the url AFTER any next/prev query strings have been applied, is out of range, adjusts it to be in range (by also adjusting the year). """ error = False error_msg = "The date given was invalid." if month_orig not in xrange(1, 13) and month_orig is not None: month = now.month error = error_msg # This takes care of 'next' query strings making month > 12 while month > 12: month -= 12 year += 1 # This takes care of 'prev' query strings making month < 1 while month < 1: month += 12 year -= 1 year, month, error = _check_year(year, month, error, error_msg) return year, month, error
def test_get_pk_value_on_save_calls_strategy_get_next_id(self): for i in xrange(100): instance = TestModel.objects.using('app_shard_001').create(user_pk=1) self.assertEqual(ShardedTestModelIDs.objects.using('app_shard_001').latest('pk').pk, instance.pk)
def test_get_prep_value_strings(self): item = LikertField() for value in xrange(1, 50 + 1): self.assertIsInstance(item.get_prep_value(str(value)), int)
def tuple(self): "Gets the tuple for each ring in this Polygon." return tuple(self[i].tuple for i in xrange(len(self)))
def __iter__(self): "Allows for iteration over the layers in a data source." for i in xrange(self.layer_count): yield self[i]
def __iter__(self): "Iterates over each point in the LineString." for i in xrange(self.point_count): yield self[i]
def tuple(self): "Returns a tuple representation of this Geometry Collection." return tuple(self[i].tuple for i in xrange(self.geom_count))
class GZipMiddlewareTest(TestCase): """ Tests the GZip middleware. """ short_string = "This string is too short to be worth compressing." compressible_string = 'a' * 500 uncompressible_string = ''.join( chr(random.randint(0, 255)) for _ in xrange(500)) def setUp(self): self.req = HttpRequest() self.req.META = { 'SERVER_NAME': 'testserver', 'SERVER_PORT': 80, } self.req.path = self.req.path_info = "/" self.req.META['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate' self.req.META[ 'HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1' self.resp = HttpResponse() self.resp.status_code = 200 self.resp.content = self.compressible_string self.resp['Content-Type'] = 'text/html; charset=UTF-8' @staticmethod def decompress(gzipped_string): return gzip.GzipFile(mode='rb', fileobj=StringIO.StringIO(gzipped_string)).read() def test_compress_response(self): """ Tests that compression is performed on responses with compressible content. """ r = GZipMiddleware().process_response(self.req, self.resp) self.assertEqual(self.decompress(r.content), self.compressible_string) self.assertEqual(r.get('Content-Encoding'), 'gzip') self.assertEqual(r.get('Content-Length'), str(len(r.content))) def test_compress_non_200_response(self): """ Tests that compression is performed on responses with a status other than 200. See #10762. """ self.resp.status_code = 404 r = GZipMiddleware().process_response(self.req, self.resp) self.assertEqual(self.decompress(r.content), self.compressible_string) self.assertEqual(r.get('Content-Encoding'), 'gzip') def test_no_compress_short_response(self): """ Tests that compression isn't performed on responses with short content. """ self.resp.content = self.short_string r = GZipMiddleware().process_response(self.req, self.resp) self.assertEqual(r.content, self.short_string) self.assertEqual(r.get('Content-Encoding'), None) def test_no_compress_compressed_response(self): """ Tests that compression isn't performed on responses that are already compressed. """ self.resp['Content-Encoding'] = 'deflate' r = GZipMiddleware().process_response(self.req, self.resp) self.assertEqual(r.content, self.compressible_string) self.assertEqual(r.get('Content-Encoding'), 'deflate') def test_no_compress_ie_js_requests(self): """ Tests that compression isn't performed on JavaScript requests from Internet Explorer. """ self.req.META[ 'HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)' self.resp['Content-Type'] = 'application/javascript; charset=UTF-8' r = GZipMiddleware().process_response(self.req, self.resp) self.assertEqual(r.content, self.compressible_string) self.assertEqual(r.get('Content-Encoding'), None) def test_no_compress_uncompressible_response(self): """ Tests that compression isn't performed on responses with uncompressible content. """ self.resp.content = self.uncompressible_string r = GZipMiddleware().process_response(self.req, self.resp) self.assertEqual(r.content, self.uncompressible_string) self.assertEqual(r.get('Content-Encoding'), None)
def __iter__(self): "Iterate over the items in the list" for i in xrange(len(self)): yield self[i]
def U(): u = salt + struct.pack(b'>I', i) for j in xrange(int(iterations)): u = _fast_hmac(password, u, digest).digest() yield _bin_to_long(u)
def index(self, val): "Standard list index method" for i in xrange(0, len(self)): if self[i] == val: return i raise ValueError('%s not found in object' % str(val))
def _construct_forms(self): # instantiate all the forms and put them in self.forms self.forms = [] for i in xrange(self.total_form_count()): self.forms.append(self._construct_form(i))
def __iter__(self): "Iterates over each Geometry." for i in xrange(self.geom_count): yield self[i]
def tuple(self): "Returns a tuple of LinearRing coordinate tuples." return tuple(self[i].tuple for i in xrange(self.geom_count))
def point_count(self): "The number of Points in this Geometry Collection." # Summing up the number of points in each geometry in this collection return sum(self[i].point_count for i in xrange(self.geom_count))
class GZipMiddlewareTest(TestCase): """ Tests the GZip middleware. """ short_string = b"This string is too short to be worth compressing." compressible_string = b'a' * 500 uncompressible_string = b''.join( six.int2byte(random.randint(0, 255)) for _ in xrange(500)) sequence = [b'a' * 500, b'b' * 200, b'a' * 300] def setUp(self): self.req = HttpRequest() self.req.META = { 'SERVER_NAME': 'testserver', 'SERVER_PORT': 80, } self.req.path = self.req.path_info = "/" self.req.META['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate' self.req.META[ 'HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1' self.resp = HttpResponse() self.resp.status_code = 200 self.resp.content = self.compressible_string self.resp['Content-Type'] = 'text/html; charset=UTF-8' self.stream_resp = StreamingHttpResponse(self.sequence) self.stream_resp['Content-Type'] = 'text/html; charset=UTF-8' @staticmethod def decompress(gzipped_string): return gzip.GzipFile(mode='rb', fileobj=BytesIO(gzipped_string)).read() def test_compress_response(self): """ Tests that compression is performed on responses with compressible content. """ r = GZipMiddleware().process_response(self.req, self.resp) self.assertEqual(self.decompress(r.content), self.compressible_string) self.assertEqual(r.get('Content-Encoding'), 'gzip') self.assertEqual(r.get('Content-Length'), str(len(r.content))) def test_compress_streaming_response(self): """ Tests that compression is performed on responses with streaming content. """ r = GZipMiddleware().process_response(self.req, self.stream_resp) self.assertEqual(self.decompress(b''.join(r)), b''.join(self.sequence)) self.assertEqual(r.get('Content-Encoding'), 'gzip') self.assertFalse(r.has_header('Content-Length')) def test_compress_non_200_response(self): """ Tests that compression is performed on responses with a status other than 200. See #10762. """ self.resp.status_code = 404 r = GZipMiddleware().process_response(self.req, self.resp) self.assertEqual(self.decompress(r.content), self.compressible_string) self.assertEqual(r.get('Content-Encoding'), 'gzip') def test_no_compress_short_response(self): """ Tests that compression isn't performed on responses with short content. """ self.resp.content = self.short_string r = GZipMiddleware().process_response(self.req, self.resp) self.assertEqual(r.content, self.short_string) self.assertEqual(r.get('Content-Encoding'), None) def test_no_compress_compressed_response(self): """ Tests that compression isn't performed on responses that are already compressed. """ self.resp['Content-Encoding'] = 'deflate' r = GZipMiddleware().process_response(self.req, self.resp) self.assertEqual(r.content, self.compressible_string) self.assertEqual(r.get('Content-Encoding'), 'deflate') def test_no_compress_uncompressible_response(self): """ Tests that compression isn't performed on responses with uncompressible content. """ self.resp.content = self.uncompressible_string r = GZipMiddleware().process_response(self.req, self.resp) self.assertEqual(r.content, self.uncompressible_string) self.assertEqual(r.get('Content-Encoding'), None)
def point_count(self): "The number of Points in this Polygon." # Summing up the number of points in each ring of the Polygon. return sum(self[i].point_count for i in xrange(self.geom_count))
def _listarr(self, func): """ Internal routine that returns a sequence (list) corresponding with the given function. """ return [func(self.ptr, i) for i in xrange(len(self))]
def __iter__(self): "Iterates through each ring in the Polygon." for i in xrange(self.geom_count): yield self[i]
def __iter__(self): "Iterates over each Geometry in the Collection." for i in xrange(len(self)): yield self[i]
def tuple(self): "Returns the tuple representation of this LineString." return tuple(self[i] for i in xrange(len(self)))
def __iter__(self): "Allows iteration over coordinates of this Point." for i in xrange(len(self)): yield self[i]
def __iter__(self): "Iterates over each point in the coordinate sequence." for i in xrange(self.size): yield self[i]
def fields(self): "Returns a list of fields in the Feature." return [ capi.get_field_name(capi.get_field_defn(self._layer._ldefn, i)) for i in xrange(self.num_fields) ]
def __iter__(self): "Iterates over each ring in the polygon." for i in xrange(len(self)): yield self[i]
def test_baseconv(self): nums = [-10**10, 10**10] + list(xrange(-100, 100)) for converter in [base2, base16, base36, base56, base62, base64]: for i in nums: self.assertEqual(i, converter.decode(converter.encode(i)))
def __iter__(self): "Iterates over each field in the Feature." for i in xrange(self.num_fields): yield self[i]
def test_no_exception_raised(self): sut = RoundRobinRoutingStrategy(settings.DATABASES) [sut.pick_read_db('app_shard_001') for i in xrange(150)]
import random try: random = random.SystemRandom() using_sysrandom = True except NotImplementedError: import warnings warnings.warn('A secure pseudo-random number generator is not available ' 'on your system. Falling back to Mersenne Twister.') using_sysrandom = False from django.conf import settings from django.utils.encoding import smart_bytes from django.utils import six from django.utils.six.moves import xrange _trans_5c = bytearray([(x ^ 0x5C) for x in xrange(256)]) _trans_36 = bytearray([(x ^ 0x36) for x in xrange(256)]) def salted_hmac(key_salt, value, secret=None): """ Returns the HMAC-SHA1 of 'value', using a key generated from key_salt and a secret (which defaults to settings.SECRET_KEY). A different key_salt should be passed in for every application of HMAC. """ if secret is None: secret = settings.SECRET_KEY # We need to generate a derived key from our base key. We can do this by # passing the key_salt and our base key through a pseudo-random function and
def make_atom(self, child, qn, connection): """ Turn a tuple (Constraint(table_alias, column_name, db_type), lookup_type, value_annotation, params) into valid SQL. The first item of the tuple may also be an Aggregate. Returns the string for the SQL fragment and the parameters to use for it. """ warnings.warn( "The make_atom() method will be removed in Django 1.9. Use Lookup class instead.", PendingDeprecationWarning) lvalue, lookup_type, value_annotation, params_or_value = child field_internal_type = lvalue.field.get_internal_type() if lvalue.field else None if isinstance(lvalue, Constraint): try: lvalue, params = lvalue.process(lookup_type, params_or_value, connection) except EmptyShortCircuit: raise EmptyResultSet elif isinstance(lvalue, Aggregate): params = lvalue.field.get_db_prep_lookup(lookup_type, params_or_value, connection) else: raise TypeError("'make_atom' expects a Constraint or an Aggregate " "as the first item of its 'child' argument.") if isinstance(lvalue, tuple): # A direct database column lookup. field_sql, field_params = self.sql_for_columns(lvalue, qn, connection, field_internal_type), [] else: # A smart object with an as_sql() method. field_sql, field_params = qn.compile(lvalue) is_datetime_field = value_annotation is datetime.datetime cast_sql = connection.ops.datetime_cast_sql() if is_datetime_field else '%s' if hasattr(params, 'as_sql'): extra, params = qn.compile(params) cast_sql = '' else: extra = '' params = field_params + params if (len(params) == 1 and params[0] == '' and lookup_type == 'exact' and connection.features.interprets_empty_strings_as_nulls): lookup_type = 'isnull' value_annotation = True if lookup_type in connection.operators: format = "%s %%s %%s" % (connection.ops.lookup_cast(lookup_type),) return (format % (field_sql, connection.operators[lookup_type] % cast_sql, extra), params) if lookup_type == 'in': if not value_annotation: raise EmptyResultSet if extra: return ('%s IN %s' % (field_sql, extra), params) max_in_list_size = connection.ops.max_in_list_size() if max_in_list_size and len(params) > max_in_list_size: # Break up the params list into an OR of manageable chunks. in_clause_elements = ['('] for offset in xrange(0, len(params), max_in_list_size): if offset > 0: in_clause_elements.append(' OR ') in_clause_elements.append('%s IN (' % field_sql) group_size = min(len(params) - offset, max_in_list_size) param_group = ', '.join(repeat('%s', group_size)) in_clause_elements.append(param_group) in_clause_elements.append(')') in_clause_elements.append(')') return ''.join(in_clause_elements), params else: return ('%s IN (%s)' % (field_sql, ', '.join(repeat('%s', len(params)))), params) elif lookup_type in ('range', 'year'): return ('%s BETWEEN %%s and %%s' % field_sql, params) elif is_datetime_field and lookup_type in ('month', 'day', 'week_day', 'hour', 'minute', 'second'): tzname = timezone.get_current_timezone_name() if settings.USE_TZ else None sql, tz_params = connection.ops.datetime_extract_sql(lookup_type, field_sql, tzname) return ('%s = %%s' % sql, tz_params + params) elif lookup_type in ('month', 'day', 'week_day'): return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, field_sql), params) elif lookup_type == 'isnull': assert value_annotation in (True, False), "Invalid value_annotation for isnull" return ('%s IS %sNULL' % (field_sql, ('' if value_annotation else 'NOT ')), ()) elif lookup_type == 'search': return (connection.ops.fulltext_search_sql(field_sql), params) elif lookup_type in ('regex', 'iregex'): return connection.ops.regex_lookup(lookup_type) % (field_sql, cast_sql), params raise TypeError('Invalid lookup_type: %r' % lookup_type)
def newItems(): for i in xrange(newLen): if i in newVals: yield newVals[i] else: yield self._get_single_internal(i)