def func(n): while True : lmbda = random.uniform(Integer(20),Integer(50)) mu = [random.uniform(Integer(2),Integer(5)),random.uniform(Integer(2),Integer(5))] c = [random.randint(Integer(2),Integer(4)),random.randint(Integer(2),Integer(4))] skip = [random.uniform(RealNumber('0.5'),RealNumber('0.8')),random.uniform(RealNumber('0.5'),RealNumber('0.8'))] ''' lmbda =5 mu = [3,4] c = [2,2] skip = [0.4,0.4] ''' print lmbda print mu print c print skip Object2 = TwoQueueVia.Queue(lmbda,mu,c,skip) ViaOut = Object2.VIA(RealNumber('0.1')) Object1 = StaticHeuristic.n2approx(lmbda,mu,c,skip) HeuristicPolicy = Object1.calculate_D(Integer(20),Integer(20)) Object3 = TwoQueueVia.Queue(lmbda,mu,c,skip,Policy = HeuristicPolicy) HeuristicCost = Object3.VIA(RealNumber('0.1'))[Integer(0)] Object4 = Simm.RoutingSimm(lmbda,mu,c,skip,Integer(500),HeuristicPolicy,Integer(100),[Integer(0),Integer(0)],Policy_type = 'Matrix') Object5 = Simm.RoutingSimm(lmbda,mu,c,skip,Integer(500),ViaOut[Integer(2)],Integer(100),[Integer(0),Integer(0)],Policy_type = 'Matrix') Object6 = StaticHeuristic.independant(lmbda,mu,c,skip) indepPolicy = Object6.calculate_D(Integer(20),Integer(20)) Object7 = TwoQueueVia.Queue(lmbda,mu,c,skip,Policy = indepPolicy) Object8 = Simm.RoutingSimm(lmbda,mu,c,skip,Integer(500),indepPolicy,Integer(100),[Integer(0),Integer(0)],Policy_type = 'Matrix') indepCost = Object7.VIA(RealNumber('0.1'))[Integer(0)] outfile = open('./Static/MDP/Comparisons/out/AllComp.csv','ab') output = csv.writer(outfile) outrow = [] outrow.append(lmbda) outrow.append(mu[Integer(0)]) outrow.append(mu[Integer(1)]) outrow.append(c[Integer(0)]) outrow.append(c[Integer(1)]) outrow.append(skip[Integer(0)]) outrow.append(skip[Integer(1)]) outrow.append(HeuristicCost) outrow.append(Object4[Integer(0)]) outrow.append(HeuristicPolicy.str()) outrow.append(indepCost) outrow.append(Object8[Integer(0)]) outrow.append(indepPolicy.str()) outrow.append(ViaOut[Integer(0)]) outrow.append(Object5[Integer(0)]) outrow.append(ViaOut[Integer(2)].str()) output.writerow(outrow) outfile.close()
def RandomGraph(self, nodes, edges, maxweight = 100.0): """ Generates a graph of random edges. @param nodes: list of nodes or number of nodes in the random graph @param edges: number of edges to generate in the random graph @type edges: integer @param maxweight: maximum weight of each edge. default = 100.0 @type maxweight: float """ import random nodes_size = 0 if type(nodes) == int: adjacency = [range(nodes)] nodes_size = nodes for node in range(nodes): adjacency.append([0 for x in range(nodes)]) elif type(nodes) == list: adjacency = nodes nodes_size = len(nodes) for node in range(nodes_size): adjacency.append([0 for x in range(nodes_size)]) else: raise FunctionParameterTypeError('nodes can only be a list \ or integer') count = 0 while count <= edges: edge = (int(random.uniform(0, nodes_size)) + 1, int(random.uniform(0, nodes_size)), int(random.uniform(0, 1) * maxweight)) if adjacency[edge[0]][edge[1]] == 0: adjacency[edge[0]][edge[1]] = edge[2] count = count + 1 self.makeGraphFromAdjacency(adjacency)
def getPoint(): """ gets a randomly generated point """ x=uniform(0,1) y=uniform(0,1) return (x,y)
def genRandSubIntervals(): intervals = [] for i in range(10): start = random.uniform(0.0, 1.0) end = random.uniform(start, 1.0) intervals.append((start, end)) return intervals
def init_random_generation(items): generation = [] for i in range(items): theta = random.uniform(15, 180) * math.pi / 180 v = random.uniform(2, 20) generation.append((theta, v)) return generation
def generate(): # randomise number of layers numLayers = random.randint(params["layers"][0],params["layers"][1]) layers = [] # start with a convolutional layer inpt = params["inputShape"] layer = generateLayer("convolution") layers.append(layer) # generate and append other layers for i in range(numLayers): k = list(definitions.keys()) k.remove("softmax") typ = random.choice(k) layer = generateLayer(typ) layers.append(layer) # generate and append the output layer layer = generateLayer("softmax") layers.append(layer) # generate network parameters lr = random.uniform(params["learningRate"][0],params["learningRate"][1]) lmbda = random.uniform(params["l2"][0],params["l2"][1]) p = { "learningRate": lr, "l2": lmbda } # return individual return [layers, p]
def splitter(): splitField = ["ra", "dec", "dist", "mag", "absmag", "x", "y", "z", "vx", "vy", "vz"][random.randint(0, 10)] if splitField == "ra": splitValue = random.uniform(1, 23) elif splitField == "dec": splitValue = random.uniform(-87, 87) elif splitField == "dist": splitValue = math.exp(random.gauss(5.5, 1)) elif splitField == "mag": splitValue = random.gauss(8, 1) elif splitField == "absmag": splitValue = random.gauss(2, 2) elif splitField == "x": splitValue = math.exp(random.gauss(5, 1)) * (1 if random.randint(0, 1) == 1 else -1) elif splitField == "y": splitValue = math.exp(random.gauss(5, 1)) * (1 if random.randint(0, 1) == 1 else -1) elif splitField == "z": splitValue = math.exp(random.gauss(5, 1)) * (1 if random.randint(0, 1) == 1 else -1) elif splitField == "vx": splitValue = math.exp(random.gauss(-12, 1)) * (1 if random.randint(0, 1) == 1 else -1) elif splitField == "vy": splitValue = math.exp(random.gauss(-12, 1)) * (1 if random.randint(0, 1) == 1 else -1) elif splitField == "vz": splitValue = math.exp(random.gauss(-12, 1)) * (1 if random.randint(0, 1) == 1 else -1) return splitField, splitValue
def _process_command(self, cmd): "Reaction to each command" try: c = cmd.split() if c[0] == "WHEELS": l,r = int(c[1]), int(c[2]) if (l < -100 or l > 100 or r < -100 or r > 100): return "ERROR" # Introduce up to 10% error in settings ltrue = l*random.uniform(0.9, 1.1) rtrue = r*random.uniform(0.9, 1.1) self.robot.wheels(ltrue, rtrue) return "OK" elif c[0] == "CAM": c = self.robot.camera() if c is None: return "0 0" else: return "%f %f" % c elif c[0] == "GRAB": self.robot.grab() return "OK" elif c[0] == "SHOOT": self.robot.shoot() return "OK" elif c[0] == "BEACON": return "1" if self.robot.beacon() else "0" else: return "ERROR" except: return "ERROR"
def randPayoff(): global M global neighborhood global neighbSize # Randomizing Payoff for x in range(0, len(ch)): playingZeroMax = 0 playingOneMax = 0 playingZeroMin = 0 playingOneMin = 0 # Randomizing Payoff for y in range(0, neighbSize[x]): M[(x, neighborhood[x][y])][0][0] = round(random.uniform(0.0, 1.0), 3) M[(x, neighborhood[x][y])][0][1] = round(random.uniform(0.0, 1.0), 3) playingZeroMax += max(M[(x, neighborhood[x][y])][0][0], M[(x, neighborhood[x][y])][0][1]) playingZeroMin += min(M[(x, neighborhood[x][y])][0][0], M[(x, neighborhood[x][y])][0][1]) M[(x, neighborhood[x][y])][1][0] = round(random.uniform(0.0, 1.0), 3) M[(x, neighborhood[x][y])][1][1] = round(random.uniform(0.0, 1.0), 3) playingOneMax += max(M[(x, neighborhood[x][y])][1][0], M[(x, neighborhood[x][y])][1][1]) playingOneMin += min(M[(x, neighborhood[x][y])][1][0], M[(x, neighborhood[x][y])][1][1]) playingMax = max(playingOneMax, playingZeroMax) playingMin = min(playingOneMin, playingZeroMin) # Normalizing Payoff Matrix for y in range(0, neighbSize[x]): M[(x, neighborhood[x][y])][0][0] = round((M[(x, neighborhood[x][y])][0][0]-playingMin/neighbSize[x])/(playingMax-playingMin/neighbSize[x]), 3) M[(x, neighborhood[x][y])][0][1] = round((M[(x, neighborhood[x][y])][0][1]-playingMin/neighbSize[x])/(playingMax-playingMin/neighbSize[x]), 3) M[(x, neighborhood[x][y])][1][0] = round((M[(x, neighborhood[x][y])][1][0]-playingMin/neighbSize[x])/(playingMax-playingMin/neighbSize[x]), 3) M[(x, neighborhood[x][y])][1][1] = round((M[(x, neighborhood[x][y])][1][1]-playingMin/neighbSize[x])/(playingMax-playingMin/neighbSize[x]), 3)
def AddImplThreadRenderingStats(mock_timer, thread, first_frame, ref_stats=None): """ Adds a random impl thread rendering stats event. thread: The timeline model thread to which the event will be added. first_frame: Is this the first frame within the bounds of an action? ref_stats: A ReferenceRenderingStats object to record expected values. """ # Create randonm data and timestap for impl thread rendering stats. data = {'frame_count': 1, 'visible_content_area': random.uniform(0, 100), 'approximated_visible_content_area': random.uniform(0, 5)} timestamp = mock_timer.AdvanceAndGet() # Add a slice with the event data to the given thread. thread.PushCompleteSlice( 'benchmark', 'BenchmarkInstrumentation::ImplThreadRenderingStats', timestamp, duration=0.0, thread_timestamp=None, thread_duration=None, args={'data': data}) if not ref_stats: return # Add timestamp only if a frame was output if data['frame_count'] == 1: if not first_frame: # Add frame_time if this is not the first frame in within the bounds of an # action. prev_timestamp = ref_stats.frame_timestamps[-1][-1] ref_stats.frame_times[-1].append(round(timestamp - prev_timestamp, 2)) ref_stats.frame_timestamps[-1].append(timestamp) ref_stats.approximated_pixel_percentages[-1].append( round(DivideIfPossibleOrZero(data['approximated_visible_content_area'], data['visible_content_area']) * 100.0, 3))
def direct_pi(N): n_hits = 0 for i in range(N): x, y = random.uniform(-1.0, 1.0), random.uniform(-1.0, 1.0) if x ** 2 + y ** 2 < 1.0: n_hits += 1 return n_hits
def qtstart(): global ctimer, wxtimer, temptimer global manager global objradar1 global objradar2 global objradar3 global objradar4 getallwx() gettemp() r1 = random.uniform(1000,10000) r2 = random.uniform(1000,10000) objradar1.start(1000*5*60+r1) objradar2.start(1000*5*60+r1) objradar3.start(1000*5*60+r2) objradar4.start(1000*5*60+r2) ctimer = QtCore.QTimer() ctimer.timeout.connect(tick) ctimer.start(1000) wxtimer = QtCore.QTimer() wxtimer.timeout.connect(getallwx) wxtimer.start(1000*10*60+random.uniform(1000,10000)) temptimer = QtCore.QTimer() temptimer.timeout.connect(gettemp) temptimer.start(1000*10*60+random.uniform(1000,10000))
def insertHapticSensorsRandom(self): """insert haptic sensors at random locations""" self.sensorGroupName = 'haptic' for _ in range(5): self.insertHapticSensor(dx=random.uniform(-0.65, 0.65), dz=random.uniform(-0.4, 0.2)) ##self.insertHapticSensor(dx=-0.055) return
def __init__(self, height, width): """Constructor will randomize the polygon.""" # Dimensions of src image self.height = height self.width = width # Random constants self.maxSep = (height + width) / self.__class__.MAX_SEP_DENOM # TODO XXX REMOVE # Anchor points control translation self.xAnchor = random.randint(-width, width) self.yAnchor = random.randint(-height, height) # Each polygon point, relative to the anchor points self.xPoints = [] self.yPoints = [] if len(self.xPoints) == 0: self.initPoints() # Don't perform in copying # Other Geometry/LinearAlg operations self.xGrowth = random.uniform(self.__class__.MIN_GROWTH, self.__class__.MAX_GROWTH) self.yGrowth = random.uniform(self.__class__.MIN_GROWTH, self.__class__.MAX_GROWTH) self.rotation = 0 # TODO, probably difficult self.affine = 0 # TODO
def _genRhoWidth(psr): """Calculate the opening angle of pulsar, and the beamwidth. Based on model outlined in Smits et al. 2009""" # cut off period for model perCut = 30.0 # calclate rho randfactor = random.uniform(-.15, .15) if psr.period > perCut: rho = _rhoLaw(psr.period) else: rho = _rhoLaw(perCut) logrho = math.log10(rho) + randfactor rho = 10. ** logrho # generate beta and pulse width beta = random.uniform(-1, 1) * rho width = _sindegree(0.5 * rho) * _sindegree(0.5 * rho) width = width - (_sindegree(0.5 * beta) * _sindegree(0.5 * beta)) width = width / (_sindegree(psr.alpha) * _sindegree(psr.alpha + beta)) if width < 0.0 or width > 1.0: width = 0.0 rho = 0.0 else: width = math.sqrt(width) # convert the width into degrees 0 -> 360 (ie. 90*4) width = math.degrees(math.asin(width))*4.0 return rho, width
def testRotate360( self ) : # Check that a 360 degree rotation gives us the # same image back, regardless of pivot. r = GafferImage.ImageReader() r["fileName"].setValue( self.fileName ) t = GafferImage.ImageTransform() t["in"].setInput( r["out"] ) t["transform"]["rotate"].setValue( 360 ) for i in range( 0, 100 ) : # Check that the ImageTransform isn't being smart and treating # this as a no-op. If the ImageTransform gets smart we'll need # to adjust our test to force it to do an actual resampling of # the image, since that's what we want to test. self.assertNotEqual( r["out"].channelDataHash( "R", IECore.V2i( 0 ) ), t["out"].channelDataHash( "R", IECore.V2i( 0 ) ) ) # Check that the rotated image is basically the same as the input. t["transform"]["pivot"].setValue( IECore.V2f( random.uniform( -100, 100 ), random.uniform( -100, 100 ) ), ) self.assertImagesEqual( r["out"], t["out"], maxDifference = 0.0001, ignoreDataWindow = True )
def draw_stochastic2(s): return draw_generic(RhinoTurtle(), { 'a': lambda t: t.forward(random.uniform(10,20)), 'b': lambda t: t.right(random.uniform(0, 90)), 'c': lambda t: t.left(random.uniform(0, 90)), }, s)
def move(self): self.x=self.x + 0.42*math.cos(self.t*math.pi/180.0) self.y=self.y + 0.42*math.sin(self.t*math.pi/180.0) z=0.33 cx = int(self.x + random.uniform(-z,z)) cy = int(self.y + random.uniform(-z,z)) self.regionColor() pygame.gfxdraw.pixel(self.ss.surface, int(self.x + random.uniform(-z,z)), int(self.y + random.uniform(-z,z)), self.color) if cx >= 0 and cx < self.ss.dimx and cy >= 0 and cy < self.ss.dimy : if self.ss.cgrid[cx, cy] > 10000 or abs(self.ss.cgrid[cx, cy] - self.t) < 5 : self.ss.cgrid[cx, cy] = int(self.t) self.ss.used.append([cx,cy]) else: if abs(self.ss.cgrid[cx, cy] - self.t) > 2 : self.findStart() #self.ss.makeCrack() else: print("???") else: self.findStart()
def checkEvent(self): pass random.seed() event_value = random.uniform(0, 1) encounter_chance = 0.03 + BASE_ENEMY_ENCOUNTER_CHANCE * self.danger if self.player.hiding: h_event_value = random.uniform(0, 1) h_encounter_chance = 0.03 + BASE_HIDE_ENCOUNTER_CHANCE * self.hide_danger if h_event_value <= h_encounter_chance: encounter_chance = 1 else: encounter_chance = -1 if STORY_MODE and self.map.stevendorf and not self.map.boss_fight: self.map.boss_fight = True self.current_enemy = self.enemy_factory.generateEnemy(self.level, boss=self.map.stevendorf or self.encounter_sdorf, dorfweap=14) self.runEvent() elif event_value <= encounter_chance and not self.invuln_turns: # spawn an enemy TODO generator self.current_enemy = self.enemy_factory.get_next_enemy() self.runEvent() self.current_enemy = None if self.invuln_turns: self.invuln_turns -= 1 self.player.hiding = False
def getRandomPosition(self): """ Return a random position inside the room. returns: a Position object. """ return Position(random.uniform(0, self.width), random.uniform(0, self.height))
def tagcloud(worddict, n=10, minsize=25, maxsize=50, minalpha=0.5, maxalpha=1.0): from matplotlib import pyplot as plt import random worddict = wordfreq_to_weightsize(worddict, minsize, maxsize, minalpha, maxalpha) fig = plt.figure() ax = fig.add_subplot(111) ax.set_position([0.0,0.0,1.0,1.0]) plt.xticks([]) plt.yticks([]) words = worddict.keys() alphas = [v[0] for v in worddict.values()] sizes = [v[1] for v in worddict.values()] items = zip(alphas, sizes, words) items.sort(reverse=True) for alpha, size, word in items[:n]: # xpos = random.normalvariate(0.5, 0.3) # ypos = random.normalvariate(0.5, 0.3) xpos = random.uniform(0.0,1.0) ypos = random.uniform(0.0,1.0) ax.text(xpos, ypos, word.lower(), alpha=alpha, fontsize=size) ax.autoscale_view() return ax
def test_serialisation(self): """ Test saving and then reloading a test spectra. """ test_decays = 10 test_spectra = spectra.Spectra("Test", test_decays) for x in range(0, test_decays): energy = random.uniform(0, test_spectra._energy_high) radius = random.uniform(0, test_spectra._radial_high) time = random.uniform(0, test_spectra._time_high) test_spectra.fill(energy, radius, time) store.dump("test.hdf5", test_spectra) loaded_spectra = store.load("test.hdf5") self.assertTrue(loaded_spectra.sum() == test_decays) self.assertTrue(numpy.array_equal(test_spectra._data, loaded_spectra._data)) self.assertTrue(test_spectra._energy_low == loaded_spectra._energy_low) self.assertTrue(test_spectra._energy_high == loaded_spectra._energy_high) self.assertTrue(test_spectra._energy_bins == loaded_spectra._energy_bins) self.assertTrue(test_spectra._energy_width == loaded_spectra._energy_width) self.assertTrue(test_spectra._radial_low == loaded_spectra._radial_low) self.assertTrue(test_spectra._radial_high == loaded_spectra._radial_high) self.assertTrue(test_spectra._radial_bins == loaded_spectra._radial_bins) self.assertTrue(test_spectra._radial_width == loaded_spectra._radial_width) self.assertTrue(test_spectra._time_low == loaded_spectra._time_low) self.assertTrue(test_spectra._time_high == loaded_spectra._time_high) self.assertTrue(test_spectra._time_bins == loaded_spectra._time_bins) self.assertTrue(test_spectra._time_width == loaded_spectra._time_width) self.assertTrue(test_spectra._num_decays == loaded_spectra._num_decays)
def monte_carlo(beta, cubic, quartic): beta = 2.0 N = 2 ** 5 dtau = beta / N delta = 1.0 n_steps = int(10 ** 7) X = np.zeros([n_steps, N]) x = [0.0] * N for step in range(n_steps): k = random.randint(0, N - 1) knext, kprev = (k + 1) % N, (k - 1) % N x_new = x[k] + random.uniform(-delta, delta) old_weight = ( rho_free(x[knext], x[k], dtau) * rho_free(x[k], x[kprev], dtau) * math.exp(-dtau * V(x[k], cubic, quartic)) ) new_weight = ( rho_free(x[knext], x_new, dtau) * rho_free(x_new, x[kprev], dtau) * math.exp(-dtau * V(x_new, cubic, quartic)) ) if random.uniform(0.0, 1.0) < new_weight / old_weight: x[k] = x_new X[step, :] = x if step % 10000 == 0: print("step %d / %d" % (step, n_steps)) return X
def add_exhaust_to_face(bm, face): if not face.is_valid: return # The more square the face is, the more grid divisions it might have num_cuts = randint(1, int(4 - get_aspect_ratio(face))) result = bmesh.ops.subdivide_edges(bm, edges=face.edges[:], cuts=num_cuts, fractal=0.02, use_grid_fill=True) exhaust_length = uniform(0.1, 0.2) scale_outer = 1 / uniform(1.3, 1.6) scale_inner = 1 / uniform(1.05, 1.1) for face in result['geom']: if isinstance(face, bmesh.types.BMFace): if is_rear_face(face): face.material_index = Material.hull_dark face = extrude_face(bm, face, exhaust_length) scale_face(bm, face, scale_outer, scale_outer, scale_outer) extruded_face_list = [] face = extrude_face(bm, face, -exhaust_length * 0.9, extruded_face_list) for extruded_face in extruded_face_list: extruded_face.material_index = Material.exhaust_burn scale_face(bm, face, scale_inner, scale_inner, scale_inner)
def testSolveAndCall( self ) : random.seed( 0 ) for i in range( 0, 100 ) : s = IECore.Splineff() x = 0 for i in range( 0, 40 ) : s[x] = random.uniform( 0, 10 ) x += 1 + random.uniform( 0, 1 ) xv = s.keys() yv = s.values() for i in range( 0, 1000 ) : # select a segment seg = int(random.uniform( 0, int(len(xv) / 4) )) seg -= seg % s.basis.step # evaluate an x,y point on the curve directly # ourselves t = i / 1000.0 c = s.basis.coefficients( t ) x = xv[seg+0] * c[0] + xv[seg+1] * c[1] + xv[seg+2] * c[2] + xv[seg+3] * c[3] y = yv[seg+0] * c[0] + yv[seg+1] * c[1] + yv[seg+2] * c[2] + yv[seg+3] * c[3] # then check that solving for x gives y yy = s( x ) self.assertAlmostEqual( yy, y, 3 )
def __init__(self, pos): self.pos = pos self.life = 10 + int(random.random() * 2) self.move = Vec2D(random.uniform(-2.5, 2.5), random.uniform(-2.5, 0.0)) self.surf = resman.get("game.sparkle_surf") width, height = self.surf.get_size() self.center = Vec2D(width / 2, height / 2)
def generate_asteroid(self, now): # Check if it's time to create a new object if (now > self.next_gen_time): # Create a new object # Select a random item from the list, and # pull it out of the list # Note: It should have been cloned, but there is a problem # with clone() ast_num = random.randint(0,len(self.asteroid_model_list)-2) nobj = self.asteroid_model_list[ast_num] del self.asteroid_model_list[ast_num] # Select an incident angle and speed azimuth = random.uniform(*AZIMUTH_RANGE) incl = random.uniform(*INCLINATION_RANGE) speed = random.uniform(*SPEED_RANGE) # Create the asteroid object ast = Asteroid(nobj, azimuth, incl, speed, now, self.explosion_shader, self.regular_shader) # Calculate the next generation time self.calc_next_gen_time() # Return the new asteroid return ast else: # Do not create anything return None
def test_validate_point_count_called(self): import random with mock.patch("course.page.base.validate_point_count")\ as mock_validate_point_count,\ mock.patch("course.page.base.get_auto_feedback")\ as mock_get_auto_feedback: mock_validate_point_count.side_effect = lambda x: x mock_get_auto_feedback.side_effect = lambda x: x for i in range(10): correctness = random.uniform(0, 15) feedback = "some feedback" AnswerFeedback(correctness, feedback) mock_validate_point_count.assert_called_once_with(correctness) # because feedback is not None self.assertEqual(mock_get_auto_feedback.call_count, 0) mock_validate_point_count.reset_mock() for i in range(10): correctness = random.uniform(0, 15) AnswerFeedback(correctness) # because get_auto_feedback is mocked, the call_count of # mock_validate_point_count is only once mock_validate_point_count.assert_called_once_with(correctness) mock_validate_point_count.reset_mock() # because feedback is None self.assertEqual(mock_get_auto_feedback.call_count, 1) mock_get_auto_feedback.reset_mock() AnswerFeedback(correctness=None) mock_validate_point_count.assert_called_once_with(None)
def test_geopoint_to_native(): geo = GeoPointType(required=True) with pytest.raises(ConversionError): native = geo.to_native((10,)) with pytest.raises(ConversionError): native = geo.to_native({'1':'-20', '2': '18'}) with pytest.raises(ConversionError): native = geo.to_native(['-20', '18']) with pytest.raises(ConversionError): native = geo.to_native('-20, 18') class Point(object): def __len__(self): return 2 with pytest.raises(ConversionError): native = geo.to_native(Point()) native = geo.to_native([89, -12]) assert native == [89, -12] latitude = random.uniform(-90, 90) longitude = random.uniform(-180, 180) point = [latitude, longitude] native = geo.to_native(point) assert native == point
def processAlgorithm(self, parameters, context, feedback): spacing = self.parameterAsDouble(parameters, self.SPACING, context) inset = self.parameterAsDouble(parameters, self.INSET, context) randomize = self.parameterAsBool(parameters, self.RANDOMIZE, context) isSpacing = self.parameterAsBool(parameters, self.IS_SPACING, context) crs = self.parameterAsCrs(parameters, self.CRS, context) extent = self.parameterAsExtent(parameters, self.EXTENT, context, crs) fields = QgsFields() fields.append(QgsField('id', QVariant.Int, '', 10, 0)) (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context, fields, QgsWkbTypes.Point, crs) if sink is None: raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT)) if randomize: seed() area = extent.width() * extent.height() if isSpacing: pSpacing = spacing else: pSpacing = sqrt(area / spacing) f = QgsFeature() f.initAttributes(1) f.setFields(fields) count = 0 total = 100.0 / (area / pSpacing) y = extent.yMaximum() - inset extent_geom = QgsGeometry.fromRect(extent) extent_engine = QgsGeometry.createGeometryEngine(extent_geom.constGet()) extent_engine.prepareGeometry() while y >= extent.yMinimum(): x = extent.xMinimum() + inset while x <= extent.xMaximum(): if feedback.isCanceled(): break if randomize: geom = QgsGeometry().fromPointXY(QgsPointXY( uniform(x - (pSpacing / 2.0), x + (pSpacing / 2.0)), uniform(y - (pSpacing / 2.0), y + (pSpacing / 2.0)))) else: geom = QgsGeometry().fromPointXY(QgsPointXY(x, y)) if extent_engine.intersects(geom.constGet()): f.setAttribute('id', count) f.setGeometry(geom) sink.addFeature(f, QgsFeatureSink.FastInsert) x += pSpacing count += 1 feedback.setProgress(int(count * total)) y = y - pSpacing return {self.OUTPUT: dest_id}
def get_Isc(self): # Will need pixel number as an input value = uniform(0, .0015) return value
def execute(self, write_specs=False, write_profile=False): if not os.path.exists(self.run_dir): try: sleep(random.uniform(0, 1)) if not os.path.exists(self.run_dir): os.makedirs(self.run_dir) except: pass # Write turbulence file if write_specs: self.turbsim_vt.metboundconds.UserFile = os.sep.join( [self.run_dir, self.turbulence_file_name]) turb_specs(V_ref=float(self.wind_speed), L_u=float(self.L_u), L_v=float(self.L_v), L_w=float(self.L_w), sigma_u=float(self.sigma_u), sigma_v=float(self.sigma_v), sigma_w=float(self.sigma_w), filename=self.turbsim_vt.metboundconds.UserFile, template_file=self.turbulence_template_file) self.turbsim_vt.metboundconds.UserFile = os.sep.join( ['..', self.run_dir, self.turbulence_file_name]) # Write profile file if write_profile: self.turbsim_vt.metboundconds.ProfileFile = os.sep.join( [self.run_dir, self.turbsim_vt.metboundconds.ProfileFile]) write_wind(V_ref=float(self.wind_speed), alpha=float(self.shear_exponent), Beta=float(self.veer), Z_hub=float(self.turbsim_vt.tmspecs.HubHt), filename=self.turbsim_vt.metboundconds.ProfileFile, template_file=self.profile_template) self.turbsim_vt.metboundconds.ProfileFile = os.sep.join( ['..', self.turbsim_vt.metboundconds.ProfileFile]) self.turbsim_vt.metboundconds.ProfileFile = os.sep.join([ '..', self.run_dir, self.turbsim_vt.metboundconds.ProfileFile ]) tsinp = open(os.sep.join([self.run_dir, self.tsim_input_file]), 'w') tsinp.write("-----\n") tsinp.write("-----\n") tsinp.write("-----\n") # runtime options tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.echo)) tsinp.write("{}\n".format( int(self.turbsim_vt.runtime_options.RandSeed1))) tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.RandSeed2)) tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.WrBHHTP)) tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.WrFHHTP)) tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.WrADHH)) tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.WrADFF)) tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.WrBLFF)) tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.WrADTWR)) tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.WrFMTFF)) tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.WrACT)) tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.Clockwise)) tsinp.write("{}\n".format(self.turbsim_vt.runtime_options.ScaleIEC)) # Turbine/Model Specifications tsinp.write("\n") tsinp.write("----\n") tsinp.write("{}\n".format(self.turbsim_vt.tmspecs.NumGrid_Z)) tsinp.write("{}\n".format(self.turbsim_vt.tmspecs.NumGrid_Y)) tsinp.write("{}\n".format(self.turbsim_vt.tmspecs.TimeStep)) tsinp.write("{}\n".format(self.turbsim_vt.tmspecs.AnalysisTime)) tsinp.write("{}\n".format(self.turbsim_vt.tmspecs.UsableTime)) tsinp.write("{}\n".format(self.turbsim_vt.tmspecs.HubHt)) tsinp.write("{}\n".format(self.turbsim_vt.tmspecs.GridHeight)) tsinp.write("{}\n".format(self.turbsim_vt.tmspecs.GridWidth)) tsinp.write("{}\n".format(self.turbsim_vt.tmspecs.VFlowAng)) tsinp.write("{}\n".format(self.turbsim_vt.tmspecs.HFlowAng)) # Meteorological Boundary Conditions tsinp.write("\n") tsinp.write("----\n") tsinp.write("{}\n".format(self.turbsim_vt.metboundconds.TurbModel)) tsinp.write('{}\n'.format(self.turbsim_vt.metboundconds.UserFile)) tsinp.write("{}\n".format(self.turbsim_vt.metboundconds.IECstandard)) tsinp.write("{}\n".format(self.turbsim_vt.metboundconds.IECturbc)) tsinp.write("{}\n".format(self.turbsim_vt.metboundconds.IEC_WindType)) tsinp.write("{}\n".format(self.turbsim_vt.metboundconds.ETMc)) tsinp.write("{}\n".format( self.turbsim_vt.metboundconds.WindProfileType)) tsinp.write('{}\n'.format(self.turbsim_vt.metboundconds.ProfileFile)) tsinp.write("{}\n".format(self.turbsim_vt.metboundconds.RefHt)) tsinp.write("{}\n".format(self.turbsim_vt.metboundconds.URef)) tsinp.write("{}\n".format(self.turbsim_vt.metboundconds.ZJetMax)) tsinp.write("{}\n".format(self.turbsim_vt.metboundconds.PLExp)) tsinp.write("{}\n".format(self.turbsim_vt.metboundconds.Z0)) # Non-IEC Meteorological Boundary Conditions tsinp.write("\n") tsinp.write("----\n") tsinp.write("{}\n".format(self.turbsim_vt.noniecboundconds.Latitude)) tsinp.write("{}\n".format(self.turbsim_vt.noniecboundconds.RICH_NO)) tsinp.write("{}\n".format(self.turbsim_vt.noniecboundconds.UStar)) tsinp.write("{}\n".format(self.turbsim_vt.noniecboundconds.ZI)) tsinp.write("{}\n".format(self.turbsim_vt.noniecboundconds.PC_UW)) tsinp.write("{}\n".format(self.turbsim_vt.noniecboundconds.PC_UV)) tsinp.write("{}\n".format(self.turbsim_vt.noniecboundconds.PC_VW)) # Spatial Coherence Parameters tsinp.write("\n") tsinp.write("----\n") tsinp.write("{}\n".format(self.turbsim_vt.spatialcoherance.SCMod1)) tsinp.write("{}\n".format(self.turbsim_vt.spatialcoherance.SCMod2)) tsinp.write("{}\n".format(self.turbsim_vt.spatialcoherance.SCMod3)) tsinp.write('"%f %f"\n' % (float(self.turbsim_vt.spatialcoherance.InCDec1[0]), float(self.turbsim_vt.spatialcoherance.InCDec1[1]))) tsinp.write('"%f %f"\n' % (float(self.turbsim_vt.spatialcoherance.InCDec2[0]), float(self.turbsim_vt.spatialcoherance.InCDec2[1]))) tsinp.write('"%f %f"\n' % (float(self.turbsim_vt.spatialcoherance.InCDec3[0]), float(self.turbsim_vt.spatialcoherance.InCDec3[1]))) tsinp.write("{}\n".format(self.turbsim_vt.spatialcoherance.CohExp)) # Coherent Turbulence Scaling Parameters tsinp.write("\n") tsinp.write("----\n") tsinp.write("{}\n".format( self.turbsim_vt.coherentTurbulence.CTEventPath)) tsinp.write("{}\n".format( self.turbsim_vt.coherentTurbulence.CTEventFile)) tsinp.write("{}\n".format( self.turbsim_vt.coherentTurbulence.Randomize)) tsinp.write("{}\n".format(self.turbsim_vt.coherentTurbulence.DistScl)) tsinp.write("{}\n".format(self.turbsim_vt.coherentTurbulence.CTLy)) tsinp.write("{}\n".format(self.turbsim_vt.coherentTurbulence.CTLz)) tsinp.write("{}\n".format( self.turbsim_vt.coherentTurbulence.CTStartTime))
def GAUSS_PY(SIG): X=(random.uniform(-3.0,3.0))*SIG return (X)
return ret all_categories = ["context"] processed_categories = set() c = crawler.Crawler("") while len(all_categories) > 0: word = all_categories.pop() if word in processed_categories: continue processed_categories.add(word) print word xml = c.download("http://www.citeulike.org/rss/search/all?q=" + word) #fp = open('./words/' + word + '.xml', 'w') #fp.write(xml) #fp.close() users = GetUsers(xml) categories = GetCategories(xml) file_user = open('users.txt','a') for user in users: file_user.write(user + "\n") file_user.close() k = 0 for word,weight in sorted(categories.items(), key=itemgetter(1), reverse=True): if weight < 2 or len(word) < 8 or k > 3 or word in processed_categories: continue all_categories.append(word) k = k + 1 time.sleep(random.uniform(5,10))
def probability(p): """Return true with probability p.""" return p > random.uniform(0.0, 1.0)
def decalTheWorld(mat): base.bspLoader.traceDecal(mat, 2.0, random.uniform(0, 360), camera.getPos(), camera.getPos() + camera.getQuat().getForward() * 10000)
def get_Voc(self): # Will need pixel number as an input value = uniform(0, 1.2) return value
def startMoving(self): self.x_vel = 4 if random.randrange(1, 3) == 1 else -4 self.y_vel = random.uniform(-1, 1) * 4
def __call__(self, img): gs = img.new().resize_as_(img).zero_() alpha = random.uniform(0, self.var) return img.lerp(gs, alpha)
def detectPaddleCollision(self, paddle): if not (self.x > paddle.x + paddle.width or paddle.x > self.x + self.size): if not (self.y > paddle.y + paddle.height or paddle.y > self.y + self.size): self.x_vel = -(self.x_vel + (self.x_vel * 0.03)) self.y_vel = random.uniform(-1, 1) * 4
def __call__(self, img): alpha = random.uniform(1 - self.var, 1 + self.var) return ie.Contrast(img).enhance(alpha)
def __call__(self, img): gs = Grayscale()(img) gs.fill_(gs.mean()) alpha = random.uniform(0, self.var) return img.lerp(gs, alpha)
def _randomly_fill(self, rows: int, columns: int, sparseness: float): for row in range(rows): for column in range(columns): if random.uniform(0, 1.0) < sparseness: self._grid[row][column] = Cell.BLOCKED
def __call__(self, img): alpha = random.uniform(1 - self.var, 1 + self.var) return ie.Sharpness(img).enhance(alpha)
def square_root(n): """ Perform binary search on the interval [n, 1.0] or [1.0, n] (depending on whether n >= 1). The prior interval accounts for input numbers that are less than 1. For example, the square root of 0.25 is 0.5. Time complexity is O(log n) and space complexity is O(1). """ L, R = (n, 1.0) if n < 1 else (1.0, n) while not math.isclose(L, R): M = (L + R) / 2 if M**2 < n: L = M else: R = M return L # -- Testing import math import random if __name__ == '__main__': n = random.uniform(0.0, 100.0) print(f'n: {n}') print(f'Estimated square: {square_root(n)}') print(f'Actual square: {math.sqrt(n)}')
def run_for_dur(self, dur): end_time = self.cur_time + dur for sender in self.senders: sender.reset_obs() while self.cur_time < end_time: event_time, cur_latency, event_type, next_hop, sender, dropped = heapq.heappop( self.q) #print("Got event %s, to link %d, latency %f at time %f" % (event_type, next_hop, cur_latency, event_time)) self.cur_time = event_time new_event_time = event_time new_event_type = event_type new_next_hop = next_hop new_latency = cur_latency new_dropped = dropped push_new_event = False if event_type == EVENT_TYPE_ACK: if next_hop == len(sender.path): if dropped: sender.on_packet_lost() #print("Packet lost at time %f" % self.cur_time) else: sender.on_packet_acked(cur_latency) #print("Packet acked at time %f" % self.cur_time) else: new_next_hop = next_hop + 1 link_latency = sender.path[next_hop].get_cur_latency( self.cur_time) if USE_LATENCY_NOISE: link_latency *= random.uniform(1.0, MAX_LATENCY_NOISE) new_latency += link_latency new_event_time += link_latency push_new_event = True if event_type == EVENT_TYPE_SEND: if next_hop == 0: #print("Packet sent at time %f" % self.cur_time) if sender.can_send_packet(): sender.on_packet_sent() push_new_event = True heapq.heappush(self.q, (self.cur_time + (1.0 / sender.rate), 0.0, EVENT_TYPE_SEND, 0, sender, False)) else: push_new_event = True if next_hop == sender.dest: new_event_type = EVENT_TYPE_ACK new_next_hop = next_hop + 1 link_latency = sender.path[next_hop].get_cur_latency( self.cur_time) if USE_LATENCY_NOISE: link_latency *= random.uniform(1.0, MAX_LATENCY_NOISE) new_latency += link_latency new_event_time += link_latency new_dropped = not sender.path[next_hop].packet_enters_link( self.cur_time) if push_new_event: heapq.heappush(self.q, (new_event_time, new_latency, new_event_type, new_next_hop, sender, new_dropped)) rewards = np.zeros(len(self.senders)) for sndr in range(len(self.senders)): sender_mi = self.senders[sndr].get_run_data() throughput = sender_mi.get("recv rate") latency = sender_mi.get("avg latency") loss = sender_mi.get("loss ratio") bw_cutoff = self.links[0].bw * 0.8 lat_cutoff = 2.0 * self.links[0].dl * 1.5 loss_cutoff = 2.0 * self.links[0].lr * 1.5 #print("thpt %f, bw %f" % (throughput, bw_cutoff)) #reward = 0 if (loss > 0.1 or throughput < bw_cutoff or latency > lat_cutoff or loss > loss_cutoff) else 1 # # Super high throughput #reward = REWARD_SCALE * (20.0 * throughput / RATE_OBS_SCALE - 1e3 * latency / LAT_OBS_SCALE - 2e3 * loss) # Very high thpt # reward = (10.0 * throughput / (8 * BYTES_PER_PACKET) - 1e3 * latency - 2e3 * loss) # High thpt reward = REWARD_SCALE * (5.0 * throughput / (8 * BYTES_PER_PACKET) - 1e3 * latency - 2e3 * loss) # Low latency #reward = REWARD_SCALE * (2.0 * throughput / RATE_OBS_SCALE - 1e3 * latency / LAT_OBS_SCALE - 2e3 * loss) #if reward > 857: #print("Reward = %f, thpt = %f, lat = %f, loss = %f" % (reward, throughput, latency, loss)) #reward = (throughput / RATE_OBS_SCALE) * np.exp(-1 * (LATENCY_PENALTY * latency / LAT_OBS_SCALE + LOSS_PENALTY * loss)) rewards[sndr] = reward return rewards
import shades import random from handy_stuff import palletes canvas = shades.Canvas(2000, 2000) pallete = random.choice(palletes) random.shuffle(pallete) canvas = shades.Canvas(2000, 2000, pallete[0]) tone = shades.BlockColor(warp_noise=shades.noise_fields( scale=[0, random.uniform(0, 0.002)], channels=2), warp_size=random.randint(300, 900)) shift = random.randint(3, 9) for y in range(-tone.warp_size, canvas.height + tone.warp_size, 30): for x in range(-50, canvas.width): for i in range(30): tone.color = pallete[i % len(pallete)] tone.weighted_point(canvas, (x + i * shift, y + i * shift), 2) canvas.show()
def test_kafka_produce_consume(kafka_cluster): instance.query(''' CREATE TABLE test.kafka (key UInt64, value UInt64) ENGINE = Kafka SETTINGS kafka_broker_list = 'kafka1:19092', kafka_topic_list = 'insert2', kafka_group_name = 'insert2', kafka_format = 'TSV', kafka_row_delimiter = '\\n'; ''') messages_num = 10000 def insert(): values = [] for i in range(messages_num): values.append("({i}, {i})".format(i=i)) values = ','.join(values) while True: try: instance.query( "INSERT INTO test.kafka VALUES {}".format(values)) break except QueryRuntimeException as e: if 'Local: Timed out.' in str(e): continue else: raise threads = [] threads_num = 16 for _ in range(threads_num): threads.append(threading.Thread(target=insert)) for thread in threads: time.sleep(random.uniform(0, 1)) thread.start() instance.query(''' DROP TABLE IF EXISTS test.view; DROP TABLE IF EXISTS test.consumer; CREATE TABLE test.view (key UInt64, value UInt64) ENGINE = MergeTree ORDER BY key; CREATE MATERIALIZED VIEW test.consumer TO test.view AS SELECT * FROM test.kafka; ''') while True: result = instance.query('SELECT count() FROM test.view') time.sleep(1) if int(result) == messages_num * threads_num: break instance.query(''' DROP TABLE test.consumer; DROP TABLE test.view; ''') for thread in threads: thread.join() assert int( result ) == messages_num * threads_num, 'ClickHouse lost some messages: {}'.format( result)
def disbdate(): return str(random.randint(1, 29)).zfill(2) + '/' + str(random.randint(1, 12)).zfill(2) + '/' + str(random.randint(1970, 2010)) data = [] for i in xrange(10000000): business_date = str(random.randint(1, 29)).zfill(2) + '/' + str(random.randint(10, 12)).zfill(2) + '/' + '2017' ind = business_date outd = '31/12/2099' data.append( str(100080510+i) + ',' + random.choice(['AL' , 'PL' , 'HL']) + ',' + disbdate() + ',' + str(1000 + i) + ',' + str(random.randint(100000, 1500000)) + ',' + str(random.uniform(8.5, 14.5)) + ',' + str(random.randint(12,240)) + ',' + str(random.randint(1000, 30000)) + ',' + disbdate() + ',' + random.choice(['Active', 'Canceled', 'Closed']) + ',' + ind + ',' + outd ) with open('fact_loan.csv', 'w') as f: f.write('\n'.join(data))
def first_page(self): pre_bitch.Pre_CS_bitch().first_page(self.url) # 导入创建好的目录 html = requests.get(self.url, headers=self.headers).content html = html.decode('gb2312').encode('utf-8') soup_one = BeautifulSoup(html, "html.parser") big_title = soup_one.find_all('p') for i in big_title[:-1]: self.big_title_list.append(i.text) # print self.big_title_list content = re.compile(r'<p class="ywxfl">(.*?)</ul>', re.S) big_content = re.findall(content, html) for j in big_content: t_firstpage = r'(.*?)</p>' title_firstpage = re.findall(t_firstpage, j) # 每本书的每一章节标题 # print title_firstpage[0] con_firstpage = re.compile( r'<li><a href="(.*?)" title=".*?">(.*?)</a></li>', re.S) content_firstpage = re.findall(con_firstpage, j) for item in content_firstpage: # print item[0],item[1] # os.makedirs(r'D:/python_spider/bitch/%s/%s'%(title_firstpage[0].decode('utf-8'),item[1].decode('utf-8'))) try: html_sec = requests.get('%s' % item[0], headers=self.headers).content html_sec = html_sec.decode('gbk').encode('utf-8') kw_dst = re.compile( r'<div id="kw_dst"><ul>.*?<li>(.*?)</ul></div>', re.S) kw_home = re.findall(kw_dst, html_sec) except UnicodeDecodeError: continue for link in kw_home: sub = r'<li><a href="(.*?)" target="_blank"><div class="kkk"><span class="kwml_left">(.*?)</span><span class="kwml_rit">' sub_link = re.findall(sub, link) for mm in sub_link: # print mm[0],mm[1] # 最后的连接和标题 try: sub_html = requests.get( '%s' % mm[0], headers=self.headers).content sub_html = sub_html.decode('gbk').encode('utf-8') # print sub_html mmmm = re.compile( r'<div class="jclj_bt"><h1>(.*?)</h1></div>.*?<div class="jclj_text">(.*?)</p></div>', re.S) mm_title_content = re.findall(mmmm, sub_html) try: mm_title_content_text = mm_title_content[0][1] if mm_title_content_text: cc = r'src="(.*?)"' cc_get = re.findall( cc, mm_title_content_text) for pp in cc_get: try: # print pp url = 'http://www.newxue.com/' + pp # path_root = 'D:/python_spider/bitch/student/picture/' pp = pp.split('/') pp = '%s_%s_%s' % (pp[-3], pp[-2], pp[-1]) # path_add = '%s.jpg'% pp path = 'D:/python_spider/bitch/student/picture/%s.jpg' % pp urllib.urlretrieve(url, path) except IOError: continue #将文章变成正常人看的模式 # mm_title_content_text = mm_title_content[0][1].replace('<p>','\n').replace('<strong>','--') # mm_title_content_text = mm_title_content_text.replace('</p>','').replace('</strong>','--') # p = re.compile(r'<[^>]+>') # mm_title_content_text = re.sub(p,'',mm_title_content_text) print mm_title_content[0][0] except IndexError: continue except UnicodeDecodeError: continue # a = os.makedirs(r'D:/python_spider/bitch/%s/%s/%s.txt'%(title_firstpage[0].decode('utf-8'),item[1].decode('utf-8'),mm_title_content[0][0].decode('utf-8'))) # with open(a,'wb') as f: # f.write(mm_title_content[0][1]) # a = os.getcwd() # f = open('%s/%s'%(os.makedirs(r'D:/python_spider/bitch/%s/%s/'%(title_firstpage[0].decode('utf-8'),item[1].decode('utf-8'))),mm_title_content[0][0].decode('utf-8'))+'.txt','r') # f.write(mm_title_content[0][1]) root = 'D:/python_spider/bitch/chinese/' root_one = '%s' % title_firstpage[0].decode("utf-8") root_two = '%s' % item[1].decode("utf-8") root_three = '%s' % mm_title_content[0][0].decode( "utf-8") + '.txt' root_tot = root + root_one + '/' + root_two + '/' + root_three # print root_tot f = open(root_tot, 'w') f.write('||||%s||||\n' % item[0]) f.write('%s||||\n' % title_firstpage[0].decode("utf-8")) f.write('人教版||||\n') f.write('%s||||\n' % item[1].decode("utf-8")) f.write('%s %s####\n' % (title_firstpage[0].decode("utf-8"), mm_title_content[0][0].decode("utf-8"))) f.write('%s####\n\n\n' % mm[0].decode("utf-8")) f.write('%s\n\n' % mm_title_content_text) f.write('%s,%s,%s\n' % (title_firstpage[0].decode("utf-8"), item[1].decode("utf-8"), mm_title_content[0][0].decode("utf-8"))) # os.makedirs(r'D:/python_spider/bitch/%s/%s/'%(title_firstpage[0].decode('utf-8'),item[1].decode('utf-8'))) # a = os.getcwd() # print a # with open(mm_title_content[0][0].decode('utf-8')+'.txt','w') as f: # f.write(mm_title_content[0][1]) print u'%s完成' % title_firstpage[0].decode("utf-8") time.sleep(random.uniform(2.0, 10.0))
"Pragma": "no-cache", "Cache-Control": "no-cache", } brand = "origene" for i in range(2, 203): url = f"https://www.origene.com.cn/category/assay-kits/elisa-kits?page={i}" with requests.Session() as s: resp = s.get(url=url, headers=headers, timeout=60) lxml = etree.HTML(resp.text) items = lxml.xpath('//article[@class="container"]') for item in items: link = ("https://www.origene.com.cn" + item.xpath('.//a[@class="name"]/@href')[0].strip()) name = item.xpath('.//a[@class="name"]/text()')[0].strip() sku = item.xpath('.//a[@class="sku"]/text()')[0].strip() # print(sku, name, link) new_data = Data(Brand=brand, Catalog_Number=sku, Product_Name=name, Detail_url=link) session.add(new_data) try: session.commit() session.close() except Exception as e: session.rollback() print(e) pass print(i, "done") time.sleep(random.uniform(0.5, 1.0))
def random_mutation(self): for i in range(0, self.dimPop): for j in range(0, len(self.population[i])): if random.uniform(0, 100) > self.pm: self.population[i][j] = random.uniform( self.a[j], self.b[j])
def mutate( self, min_swap_probability=0.2, max_swap_probability=0.8, inverse_probability=0.001, shift_probability=0.01, min_insert_probability=0.3, max_insert_probability=0.8, random_probability=0.2, semi_backtrack_removal_probability=0.1, ): swap_probability = random.uniform(min_swap_probability, max_swap_probability) insert_probability = random.uniform(min_insert_probability, max_insert_probability) for inhabitant in self.generation[1:]: if len(inhabitant.gene) <= 1: inhabitant.gene = self._random_moves() continue if decision(semi_backtrack_removal_probability) and len(inhabitant) > 2: inhabitant.gene = self._semi_backtrack_removal(inhabitant.gene) continue if decision(insert_probability): insert_amount = random.randint(1, 2) if decision(0.4): # remove decision possible_chars = self._random_word(insert_amount) if decision(0.33): inhabitant.gene += possible_chars elif decision(0.5): inhabitant.gene = possible_chars + inhabitant.gene else: insert_index = random.randint(1, len(inhabitant.gene)) inhabitant.gene = ( inhabitant.gene[:insert_index] + possible_chars + inhabitant.gene[insert_index:] ) else: if len(inhabitant) - insert_amount > 1: if decision(0.33): inhabitant.gene = inhabitant.gene[insert_amount:] elif decision(0.33): inhabitant.gene = inhabitant.gene[:-insert_amount] else: for _ in range(insert_amount): remove_index = random.randint(1, len(inhabitant.gene)-insert_amount) inhabitant.gene = ( inhabitant.gene[:remove_index] + inhabitant.gene[remove_index+1:] ) elif decision(random_probability): inhabitant.gene = self._random_moves() else: if decision(shift_probability): shift_range = random.randint(1, 3) for _ in range(shift_range + 1): inhabitant.gene = [inhabitant.gene[-1]] + inhabitant.gene[:-1] if decision(swap_probability): inhabitant.gene = gen_neighbour(inhabitant.gene) if decision(inverse_probability): inhabitant.gene = inhabitant.gene[::-1] inhabitant.gene = self._cancel_backtrack(inhabitant.gene)
def add_e2w(states, ev_profiles, ev, car_registration, car_sales, year, growth, charging, plotting=False): scenarios = get_scenarios() scenario = scenarios[growth] city_projection, two_wheeler_projection = [], [] for city in car_registration.columns.tolist(): city_projection.append(city) car_projections = [] for index, item in scenario.iteritems(): if index == 2020: car_projections.append( car_registration[city]['Two-Wheelers_2015'] * (1 + item)) else: car_projections.append(car_projections[-1] * (1 + item)) car_projections = pd.DataFrame(car_projections, index=scenario.index, columns=[growth]) two_wheeler_projection.append(car_projections[growth][year]) two_wheeler_registration_projection = car_registration.loc[[ 'Two-Wheelers_2014', 'Two-Wheelers_2015', 'Two-Wheelers_2016' ]].T.drop('Total') two_wheeler_registration_projection[ 'Two-Wheelers_' + str(year)] = two_wheeler_projection[:-1] tw_city, two_wheelers_share = [], [] total = two_wheeler_registration_projection[ 'Two-Wheelers_' + str(year)].sum( ) - two_wheeler_registration_projection['Two-Wheelers_2015'].sum() for city in two_wheeler_registration_projection.index.tolist(): two_wheelers_share.append( (two_wheeler_registration_projection['Two-Wheelers_' + str(year)][city] - two_wheeler_registration_projection['Two-Wheelers_2015'][city]) / total) tw_city.append(city) two_wheelers_share = pd.DataFrame(two_wheelers_share, index=tw_city, columns=['Share']) car_sales_2015 = car_sales[2015]['Two Wheelers'] car_sales_projections = [] for index, item in scenario.iteritems(): if index == 2020: car_sales_projections.append(car_sales_2015 * (1 + item)) else: car_sales_projections.append(car_sales_projections[-1] * (1 + item)) car_sales_projections = pd.DataFrame(car_sales_projections, index=scenario.index, columns=[growth]) car_sales_statewise = two_wheelers_share.Share * car_sales_projections[ growth][year] car_sales_statewise = car_sales_statewise.to_frame() car_sales_statewise.columns = ['Sales'] # EV Penetration if growth == 'stable': if year == 2020: ev_sales = car_sales_statewise[ car_sales_statewise['Sales'] > 0] * 0.4 elif year == 2025: ev_sales = car_sales_statewise[ car_sales_statewise['Sales'] > 0] * 0.6 elif year == 2030: ev_sales = car_sales_statewise[ car_sales_statewise['Sales'] > 0] * 0.8 elif year == 2035: ev_sales = car_sales_statewise[car_sales_statewise['Sales'] > 0] else: ev_sales = car_sales_statewise[car_sales_statewise['Sales'] > 0] elif growth == 'slow': if year == 2020: ev_sales = car_sales_statewise[ car_sales_statewise['Sales'] > 0] * 0.25 elif year == 2025: ev_sales = car_sales_statewise[ car_sales_statewise['Sales'] > 0] * 0.5 elif year == 2030: ev_sales = car_sales_statewise[ car_sales_statewise['Sales'] > 0] * 0.75 elif year == 2035: ev_sales = car_sales_statewise[car_sales_statewise['Sales'] > 0] elif year == 2040: ev_sales = car_sales_statewise[car_sales_statewise['Sales'] > 0] else: ev_sales = car_sales_statewise[car_sales_statewise['Sales'] > 0] elif growth == 'rapid': if year == 2020: ev_sales = car_sales_statewise[ car_sales_statewise['Sales'] > 0] * 0.5 elif year == 2025: ev_sales = car_sales_statewise[ car_sales_statewise['Sales'] > 0] * 0.75 elif year == 2030: ev_sales = car_sales_statewise[car_sales_statewise['Sales'] > 0] elif year == 2035: ev_sales = car_sales_statewise[car_sales_statewise['Sales'] > 0] else: ev_sales = car_sales_statewise[car_sales_statewise['Sales'] > 0] poverty = states.Poverty / states.Poverty['India'] poverty_norm = (poverty - poverty.mean()) / (poverty.max() - poverty.min()) ppp = states.PPP_PC / states.PPP_PC['India'] ppp_norm = (ppp - ppp.mean()) / (ppp.max() - ppp.min()) ev_demographics = pd.DataFrame([poverty_norm, ppp_norm]).T sr_e2w, lr_e2w = [], [] # INPUT dominant = 60 dominated = 40 for index, row in ev_demographics.iterrows(): if row.Poverty <= 0 and row.PPP_PC >= 0: lr_e2w.append(dominant) sr_e2w.append(dominated) elif row.Poverty >= 0 and row.PPP_PC <= 0: lr_e2w.append(dominated) sr_e2w.append(dominant) elif row.Poverty >= 0 and row.PPP_PC >= 0 or row.Poverty <= 0 and row.PPP_PC <= 0: if row.PPP_PC >= row.Poverty: lr_e2w.append(dominated) sr_e2w.append(dominant) else: lr_e2w.append(dominant) sr_e2w.append(dominated) ev_e2w = pd.DataFrame(zip(sr_e2w, lr_e2w), index=ev_demographics.index, columns=['Short_Range', 'Long_Range']) hourly_profile = [] for _ in range(0, 52): wd_charging, we_charging = charging_scheme(charging, ev_profiles, ev_sales) week_profile = [] for _ in range(0, 5): wd_profile = pd.DataFrame() for index, row in wd_charging.iterrows(): sr = ev_e2w.loc[index]['Short_Range'] lr = ev_e2w.loc[index]['Long_Range'] entry = (row * (sr / 100) * ev.Power_kW['Short Range E2W'] + row * (lr / 100) * ev.Power_kW['Long Range E2W'] ) * random.uniform(0.8, 1.2) wd_profile = wd_profile.append(entry) week_profile.append(wd_profile) for _ in range(0, 2): we_profile = pd.DataFrame() for index, row in we_charging.iterrows(): sr = ev_e2w.loc[index]['Short_Range'] lr = ev_e2w.loc[index]['Long_Range'] entry = (row * (sr / 100) * ev.Power_kW['Short Range E2W'] + row * (lr / 100) * ev.Power_kW['Long Range E2W'] ) * random.uniform(0.6, 1.4) we_profile = we_profile.append(entry) week_profile.append(we_profile) week = pd.concat(week_profile, axis=1) hourly_profile.append(week) ev_profile = pd.concat(hourly_profile, axis=1) wd_profile = pd.DataFrame() for index, row in wd_charging.iterrows(): sr = ev_e2w.loc[index]['Short_Range'] lr = ev_e2w.loc[index]['Long_Range'] entry = (row * (sr / 100) * ev.Power_kW['Short Range E2W'] + row * (lr / 100) * ev.Power_kW['Long Range E2W']) * random.uniform( 0.8, 1.2) wd_profile = wd_profile.append(entry) ev_profile = pd.concat([ev_profile, wd_profile], axis=1) ev_profile = ev_profile.T ev_profile = ev_profile / 1000 return ev_profile
def update(self,timepassed): if self.emit: self.acctime += timepassed if self.acctime > self.updatetime: self.acctime = 0 # add new particles if self.emit_mode == "stream": if self.direction == "random": angle = random.randint(0,360) vec_x = math.cos((angle * (math.pi/180))) vec_y = math.sin((angle * (math.pi/180))) else: # an angle was provided if self.dir_variance != 0: angle = random.randrange(self.direction-self.dir_variance,self.direction + self.dir_variance) else: angle = self.direction vec_x = math.cos((angle * (math.pi/180))) vec_y = math.sin((angle * (math.pi/180))) calc_strength = random.uniform(max(self.strength-self.str_variance,0),self.strength+self.str_variance) vec_x *= calc_strength vec_y *= calc_strength #fric = random.uniform(self.friction-self.fric_variance,min(self.friction+self.fric_variance,1.0)) fric = random.uniform(self.friction-self.fric_variance,(self.friction+self.fric_variance)) xpos = random.uniform(self.x - self.x_pos_variance,self.x + self.x_pos_variance) ypos = random.uniform(self.y - self.y_pos_variance,self.y + self.y_pos_variance) if self.rand_color: rgb = g_utils.getRandColor() self.color_1 = (rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0) rgb = g_utils.getRandColor() self.color_2 = (rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0) new_particle = self.particle_type(xpos, ypos, self.particle_w, self.particle_h, xvel = vec_x, yvel = vec_y, friction = fric, color_1 = self.color_1, color_2 = self.color_2, lerp_vel = self.lerp_vel, rotate_friction = self.rotate_friction, rotate_vel = self.rotate_vel, scale_velocity = self.scale_velocity, gravity = self.gravity, scale_x_only = self.scale_x_only, scale_y_only = self.scale_y_only, trail_length = self.trail_length, wind_direction = self.wind_direction, wind_strength = self.wind_strength, alpha_decay = self.alpha_decay, alpha = self.alpha ) self.particle_list.append(new_particle) for particle in self.particle_list: particle.update(timepassed) # delete dead particles for particle in self.particle_list: if not particle.alive: self.particle_list.remove(particle)
def randomGaze(): cam = setUpCam() timeSinceStartMovement = time.time() isAbsolute=True while (time.time()-timeSinceStartMovement)<20: #image_container contains iinfo about the image image_container = videoProxy.getImageRemote(cam) #get image width and height width=image_container[0] height=image_container[1] #the 6th element contains the pixel data values = map(ord,list(image_container[6])) image=np.array(values, np.uint8).reshape((height, width,3)) cv2.imwrite("ballimage.png", image) image=cv2.imread("ballimage.png") lower_green = np.array([36,100,100], dtype = np.uint8) upper_green = np.array([86,255,255], dtype=np.uint8) #convert to a hsv colorspace hsvImage=cv2.cvtColor(image,cv2.COLOR_BGR2HSV) #Create a treshold mask color_mask=cv2.inRange(hsvImage,lower_green,upper_green) #apply the mask on the image green_image = cv2.bitwise_and(image,image,mask=color_mask) kernel=np.ones((9,9),np.uint8) #Remove small objects opening =cv2.morphologyEx(color_mask,cv2.MORPH_OPEN,kernel) #Close small openings closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) #Apply a blur to smooth the edges smoothed_mask = cv2.GaussianBlur(closing, (9,9),0) #Apply our (smoothend and denoised) mask #to our original image to get everything that is blue. green_image = cv2.bitwise_and(image,image,mask=smoothed_mask) #Get the grayscale image (last channel of the HSV image gray_image = green_image[:,:,2] #Use a hough transform to find circular objects in the image. # cv2.HOUGH_GRADIENT circles = cv2.HoughCircles(#cv.CV_HOUGH_GRADIENT gray_image, #Input image to perform the transformation on cv2.HOUGH_GRADIENT, #Method of detection 1, #Ignore this one 5, #Min pixel dist between centers of detected circles param1=200, #Ignore this one as well param2=20, #Accumulator threshold: smaller = the more (false) circles minRadius=5, #Minimum circle radius maxRadius=100) #Maximum circle radius if circles is not None: circle = circles[0,:][0] tts.say("I found the ball") print "I found the ball" return time.time()-timeSinceStartMovement else: #TODO: find the exception for not seeing green ball. vertiRand = random.uniform(-0.5,0.5) horiRand = random.uniform(-0.5,0.5) motionProxy.angleInterpolation(headJointsHori, horiRand, [0.5], isAbsolute) motionProxy.angleInterpolation(headJointsVerti, vertiRand, [0.5], isAbsolute) tts.say("I could not find the ball") print "I could not find the ball" return 20
def update(self,timepassed): if self.emit: self.acctime += timepassed if self.acctime > self.updatetime: self.acctime = 0 # add new particles if self.emit_mode == "stream": if self.direction == "random": angle = random.randint(0,360) vec_x = math.cos((angle * (math.pi/180))) vec_y = math.sin((angle * (math.pi/180))) else: # an angle was provided if self.dir_variance != 0: angle = random.randrange(self.direction-self.dir_variance,self.direction + self.dir_variance) else: angle = self.direction vec_x = math.cos((angle * (math.pi/180))) vec_y = math.sin((angle * (math.pi/180))) calc_strength = random.uniform(max(self.strength-self.str_variance,0),self.strength+self.str_variance) vec_x *= calc_strength vec_y *= calc_strength #fric = random.uniform(self.friction-self.fric_variance,min(self.friction+self.fric_variance,1.0)) fric = random.uniform(self.friction-self.fric_variance,(self.friction+self.fric_variance)) xpos = random.uniform(self.x - self.x_pos_variance,self.x + self.x_pos_variance) ypos = random.uniform(self.y - self.y_pos_variance,self.y + self.y_pos_variance) new_particle = self.particle_type( xpos, ypos, self.w, self.h, self.imagepath_list, self.colorkey, friction = fric, lifetime = self.lifetime, xvel = vec_x, yvel = vec_y, rotate_friction = self.rotate_friction, rotate_vel = self.rotate_vel, scale_velocity = self.scale_velocity, gravity = self.gravity, wind_direction = self.wind_direction, wind_strength = self.wind_strength, alpha_decay = self.alpha_decay, trail_length = self.trail_length, alpha_var = self.alpha_var ) self.particle_list.append(new_particle) for particle in self.particle_list: particle.update(timepassed) # delete dead particles for particle in self.particle_list: if not particle.alive: self.particle_list.remove(particle)
def singleNum(): return random.uniform(-1.,1.)
i.all[int(random.random()*CACHE_SIZE)] = x return i def has(i): if i._has == None: lst = sorted(i.all) med,iqr = medianIQR(lst,ordered=True) i._has = o( median = med, iqr = iqr, lo = i.all[0], hi = i.all[-1]) return i._has """ ### Random stuff. """ by = lambda x: random.uniform(0,x) rseed = random.seed any = random.choice rand = random.random def seed(r=None): global The if The is None: The=defaults() if r is None: r = The.seed rseed(r) """ ### List Handling Tricks """
def burst(self,num_particles=15): """Releases a set number of particles in an instant""" for i in range(num_particles): if self.direction == "random": angle = random.randint(0,360) vec_x = math.cos((angle * (math.pi/180))) vec_y = math.sin((angle * (math.pi/180))) else: # an angle was provided if self.dir_variance != 0: angle = random.randrange(self.direction-self.dir_variance,self.direction + self.dir_variance) else: angle = self.direction vec_x = math.cos((angle * (math.pi/180))) vec_y = math.sin((angle * (math.pi/180))) calc_strength = random.uniform(max(self.strength-self.str_variance,0),self.strength+self.str_variance) vec_x *= calc_strength vec_y *= calc_strength #fric = random.uniform(self.friction-self.fric_variance,min(self.friction+self.fric_variance,1.0)) fric = random.uniform(self.friction-self.fric_variance,(self.friction+self.fric_variance)) xpos = random.uniform(self.x - self.x_pos_variance,self.x + self.x_pos_variance) ypos = random.uniform(self.y - self.y_pos_variance,self.y + self.y_pos_variance) if self.rand_color: rgb = g_utils.getRandColor() self.color_1 = (rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0) rgb = g_utils.getRandColor() self.color_2 = (rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0) new_particle = self.particle_type(xpos, ypos, self.particle_w, self.particle_h, xvel = vec_x, yvel = vec_y, friction = fric, color_1 = self.color_1, color_2 = self.color_2, lerp_vel = self.lerp_vel, rotate_friction = self.rotate_friction, rotate_vel = self.rotate_vel, scale_velocity = self.scale_velocity, gravity = self.gravity, scale_x_only = self.scale_x_only, scale_y_only = self.scale_y_only, trail_length = self.trail_length, wind_direction = self.wind_direction, wind_strength = self.wind_strength, alpha_decay = self.alpha_decay, alpha = self.alpha ) self.particle_list.append(new_particle)