def eat(self, pll: pool.Pool): """ Docstring """ victims = pll.get_victim(self.get_pos()) if victims: self._is_not_hungry += 3 for victim in victims: pll.kill(victim)
class Evaluator: def __init__(self, gameClass, opponents, rW, nWorkers=1, nSeeds=None): self._pool = Pool([EvalWorker(gameClass, opponents, rW) for _ in range(nWorkers)]) self._pool.start() if nSeeds is not None: self._seeds = np.random.randint(np.iinfo(np.int32).max, size=(nSeeds,)) else: self._seeds = [None] self._nSeeds = len(self._seeds) def setNRounds(self, nRounds): self._nRounds = nRounds @property def nRounds(self): return self._nRounds def evaluate(self, players): evs = [Evaluation() for _ in range(len(players))] tasks = [] for iPlayer, player in enumerate(players): for iRound in range(self._nRounds): tasks.append( (iPlayer, self._seeds[iRound % len(self._seeds)], player) ) for i, r in enumerate(self._pool.runTasks(tasks)): (iPlayer, w, W, D, L) = r evs[iPlayer].update(1, w, W, D, L) for ev in evs: ev.done() return evs def stop(self): self._pool.stop()
def calc_fitness(individual): fitness = 0 json_file = os.path.dirname(os.path.realpath(__file__)) + '/test.json' with open(json_file, 'w') as fp: json.dump(individual, fp, ensure_ascii=False) bot1 = os.path.dirname(os.path.realpath(__file__)) + '/xouba.py' bot2 = os.path.dirname(os.path.realpath(__file__)) + '/../stockfishbot.py' # bot3 = os.path.dirname(os.path.realpath(__file__)) + '/../randombot.py' p1 = Pool(bot1=[bot1, '-j', json_file], bot2=[bot2], debug=True) p2 = Pool(bot1=[bot1, '-j', json_file], bot2=[bot2, '-l', '2'], debug=True) # p2 = Pool(bot1=[bot1, '-j', json_file], bot2=bot2, debug=True) # p3 = Pool(bot1=[bot1, '-j', json_file], bot2=bot3, debug=True) matches = [p1, p2] for p in matches: for _ in range(15): p.match() if p.last_result == p.RESULT_WIN_WHITE: fitness += 5 elif p.last_result == p.RESULT_DRAW: fitness += 1 p.reset_chess_board() del p return fitness
def born(self, p_ppp: pool.Pool): """ Z name main Nain jjk. """ if random.randint(1, 10) < self._born_rate: p_ppp.fill(self.__class__, self._born_num)
def bestchallenge_comics(): logger = logging.getLogger(__name__ + '.bestchallenge_comics') url_format = BESTCHALLENGE_LIST_URL + '?page={0}' last_url = url_format.format(999999) with urlfetch.fetch(last_url, cache, 120) as f: html = lxml.html.parse(f) logger.info(last_url) last = html.xpath('//*[@id="content"]//*[contains(concat(" ", @class,' '" "), " paginate ")]//strong[@class="page"]/em/text()') last_html = html last = int(last[0]) def get_html(page): if page == last: return last_html url = url_format.format(page) with urlfetch.fetch(url, cache, 120) as f: logger.info(url) html = lxml.html.parse(f) return html pool = Pool(POOL_SIZE) htmls = pool.map(get_html, xrange(1, last + 1)) for html in htmls: links = html.xpath('//*[@id="content"]//table[@class="challengeList"]' '//td/*[@class="fl"]/a') for a in links: href = a.attrib['href'] query = href[href.index('?') + 1:] title_id = int(werkzeug.urls.url_decode(query)['titleId']) yield title_id, a.xpath('./img/@title')[0]
def eat(self, sw_pool: pool.Pool): """This is method docstring""" victims = sw_pool.get_victim(self.get_pos()) if victims: self._is_not_hungry += 3 for victim in victims: sw_pool.kill(victim)
def runStrategy(in_prices): global prices, mas, emas, smas, lwmas, vmas, vemas, vsmas, vlwmas log.debug('beginning vma strategy ...') prices = in_prices vols = [p['vol'] for p in prices] vmalength = const.VMA_MAX + 1 vmas = [0] * vmalength vemas = [0] * vmalength vsmas = [0] * vmalength vlwmas = [0] * vmalength for period in range(1, vmalength): vmas[period] = ma.calc_ma(vols, period) vemas[period] = ma.calc_ema(vols, period) vsmas[period] = ma.calc_sma(vols, period) vlwmas[period] = ma.calc_lwma(vols, period) ps = [p['close'] for p in prices] malength = const.MA_MAX + 1 mas = [0] * malength emas = [0] * malength smas = [0] * malength lwmas = [0] * malength for period in range(1, malength): mas[period] = ma.calc_ma(ps, period) emas[period] = ma.calc_ema(ps, period) smas[period] = ma.calc_sma(ps, period) lwmas[period] = ma.calc_lwma(ps, period) log.debug('running ma strategy ...') starttime = datetime.datetime.now() pool = Pool(const.POOL_SIZE) for vft, vf in [(matype, period) for matype in const.VMA_TYPES for period in const.VMA_FAST]: for vst, vs in [(matype, period) for matype in const.VMA_TYPES for period in const.VMA_SLOW]: if vs != 0 and vs <= vf: continue poola = Pool(const.POOL_SIZE) poolb = Pool(const.POOL_SIZE) for ft, f in [(matype, period) for matype in const.MA_TYPES for period in const.MA_FAST]: for s1t, s1 in [(matype, period) for matype in const.MA_TYPES for period in const.MA_SLOW1]: if s1 != 0 and s1 <= f: continue elapsed = (datetime.datetime.now() - starttime).seconds log.debug('== ' + str(elapsed) + ',' + vft + '_' + str(vf) + ',' + vst + '_' + str(vs) + ',' + ft + '_' + str(f) + ',' + s1t + '_' + str(s1) + ' ==') doTrade(poola, vft, vf, vst, vs, ft, f, s1t, s1, '', 0, '', 0) doTrade(poolb, vft, vf, vst, vs, '', 0, '', 0, ft, f, s1t, s1) for ia in range(len(poola.strategies)): for ib in range(len(poolb.strategies)): sa = poola.strategies[ia] sb = poolb.strategies[ib] if sa[0] == 0 or sb[0] == 0: continue t = doTrade(pool, vft, vf, vst, vs, sa[0].args[0], sa[0].args[1], sa[0].args[2], sa[0].args[3], sb[0].args[4], sb[0].args[5], sb[0].args[6], sb[0].args[7]) pool.showStrategies() return pool.strategies[0][0]
def eat(self, swimming_pool: pool.Pool): """def""" victims = swimming_pool.get_victim(self.get_pos()) if victims: self._is_not_hungry += 3 for victim in victims: swimming_pool.kill(victim)
def eat(self, p: pool.Pool): """ """ victims = p.get_victim(self.get_pos()) if victims: self._is_not_hungry += 3 for victim in victims: p.kill(victim)
def __insertPools(self): teamCount = 1 interval = self.__getTeamLst(0) for i in range(self.__pools): tmpPool = Pool() tmpPool.createSchedule(self.__games, interval[i], teamCount) self.poolBracket.append(tmpPool) teamCount += interval[i]
def search(keywords): pool = Pool() while pool.pool: for name, pos in pool.items(): if matchKey(getSoup(name, pos).text, keywords): print(getRootUrl(name, post)) next_pos = getNextPos(name, pos) pool.update(name, next_pos)
def __init__(self, gameClass, opponents, rW, nWorkers=1, nSeeds=None): self._pool = Pool([EvalWorker(gameClass, opponents, rW) for _ in range(nWorkers)]) self._pool.start() if nSeeds is not None: self._seeds = np.random.randint(np.iinfo(np.int32).max, size=(nSeeds,)) else: self._seeds = [None] self._nSeeds = len(self._seeds)
def born(self, pas: pool.Pool): """ :param pas: :return: """ if random.randint(1, 10) < self._born_rate: pas.fill(self.__class__, self._born_num)
def born(self, basin: pool.Pool): """ :param basin: :return: """ if random.randint(1, 10) < self._born_rate: basin.fill(self.__class__, self._born_num)
def eat(self, pool_var: pool.Pool) -> None: """ Eat method description """ victims = pool_var.get_victim(self.get_pos()) if victims: self._is_not_hungry += 3 for victim in victims: pool_var.kill(victim)
def place_in_bounds(self, pool_var: pool.Pool): """Fish method description""" try: self._pos[X] = min(max(self._pos[X], 0), pool_var.get_size()[X] - 1) self._pos[Y] = min(max(self._pos[Y], 0), pool_var.get_size()[Y] - 1) except ValueError: print("Oooops!")
def testException(self): results = set() pool = Pool(3) for result in pool.imap_unordered(Run, [[x] for x in range(0, 12)]): # Item 10 will not appear in results due to an internal exception. results.add(result.value) expect = set(range(0, 12)) expect.remove(10) self.assertEquals(expect, results)
def testAdd(self): results = set() pool = Pool(3) for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]): results.add(result.value) if result.value < 30: pool.add([result.value + 20]) self.assertEquals(set(range(0, 10) + range(20, 30) + range(40, 50)), results)
def testException(self): results = set() pool = Pool(3) for result in pool.imap_unordered(Run, [[x] for x in range(0, 12)]): # Item 10 will not appear in results due to an internal exception. results.add(result) expect = set(range(0, 12)) expect.remove(10) self.assertEquals(expect, results)
def fullPool(spec): """Creates a pool and fills it with the world drawn from the provided specification, before returning it.""" pool = Pool() data = sampleWorld(spec) for pair in data: pool.store(pair[0], pair[1]) return pool
def place_in_bounds(self, swimming_pool: pool.Pool): '''def''' try: self._pos[X] = min(max(self._pos[X], 0), swimming_pool.get_size()[X] - 1) self._pos[Y] = min(max(self._pos[Y], 0), swimming_pool.get_size()[Y] - 1) except ValueError: print("Oooops!")
def eat(self, p_ppp: pool.Pool) -> int: """ Fgfghs ms """ victims = p_ppp.get_victim(self.get_pos()) if victims: self._is_not_hungry += 3 for victim in victims: p_ppp.kill(victim) return victims
def __init__( self, core, numHilos, callback_empieza_trabajo, callback_termina_trabajo, callback_nuevo_trabajo, callback_empieza_trabajo_anadir, callback_termina_trabajo_anadir, callback_empieza_progreso, callback_termina_progreso, callback_empieza_trabajo_extraer, callback_termina_trabajo_extraer, callback_empieza_trabajo_copiar, callback_termina_trabajo_copiar, callback_termina_trabajo_descargar_caratula, callback_termina_trabajo_descargar_disco, callback_spinner, callback_nuevo_juego, callback_nuevo_descripcion, callback_nuevo_genero, callback_nuevo_online_feature, callback_nuevo_accesorio, callback_nuevo_companie, callback_error_importando, callback_empieza_descarga, callback_empieza_descomprimir, callback_empieza_importar, callback_termina_importar, callback_empieza_progreso_indefinido, callback_termina_progreso_indefinido): Pool.__init__(self, numHilos) Thread.__init__(self) self.core = core self.numHilos = int(numHilos) self.callback_empieza_trabajo = callback_empieza_trabajo self.callback_termina_trabajo = callback_termina_trabajo self.callback_nuevo_trabajo = callback_nuevo_trabajo self.callback_empieza_trabajo_anadir = callback_empieza_trabajo_anadir self.callback_termina_trabajo_anadir = callback_termina_trabajo_anadir self.callback_empieza_progreso = callback_empieza_progreso self.callback_termina_progreso = callback_termina_progreso self.callback_empieza_trabajo_extraer = callback_empieza_trabajo_extraer self.callback_termina_trabajo_extraer = callback_termina_trabajo_extraer self.callback_empieza_trabajo_copiar = callback_empieza_trabajo_copiar self.callback_termina_trabajo_copiar = callback_termina_trabajo_copiar self.callback_termina_trabajo_descargar_caratula = callback_termina_trabajo_descargar_caratula self.callback_termina_trabajo_descargar_disco = callback_termina_trabajo_descargar_disco self.callback_spinner = callback_spinner self.callback_nuevo_juego = callback_nuevo_juego self.callback_nuevo_descripcion = callback_nuevo_descripcion self.callback_nuevo_genero = callback_nuevo_genero self.callback_nuevo_online_feature = callback_nuevo_online_feature self.callback_nuevo_accesorio = callback_nuevo_accesorio self.callback_nuevo_companie = callback_nuevo_companie self.callback_error_importando = callback_error_importando self.callback_empieza_descarga = callback_empieza_descarga self.callback_empieza_descomprimir = callback_empieza_descomprimir self.callback_empieza_importar = callback_empieza_importar self.callback_termina_importar = callback_termina_importar self.callback_empieza_progreso_indefinido = callback_empieza_progreso_indefinido self.callback_termina_progreso_indefinido = callback_termina_progreso_indefinido self.actualizarPreferencias() # sesion http con wiitdb self.sesion_wiitdb = SesionWiiTDB() # pool ayudante self.poolBash = None
def eat(self, basin: pool.Pool): """ :param pool: :return: """ victims = basin.get_victim(self.get_pos()) if victims: self._is_not_hungry += 3 for victim in victims: basin.kill(victim)
def get_html(url): pool = Pool() proxies = pool.pool() headers={'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'} while True: try: req=requests.get(url,headers=headers, proxies = proxies, timeout=5) req.encoding='UTF-8' return html_str_util.filter_tags(req.text) except requests.exceptions.RequestException: pass
def __init__(self, app, **kwargs): self.app = app or self.app super(Guard, self).init(**kwargs) self.rpc_server = PyroServer(GUARD_PORT) self.rpc_client = PyroClient(self.app.conf['HUB_IP'], HUB_PORT) self.pool = Pool(self.app) self.machine = Machine(healthy_mock=app.conf['HEALTHY_MOCK'], labels=self.labels) self.__shutdown_guard = threading.Event() self.LOOP_INTERVAL = 10 # second self.alive = False
def eat(self, pas: pool.Pool): """ :param pas: :return: """ victims = pas.get_victim(self.get_pos()) if victims: self._is_not_hungry += 3 for victim in victims: pas.kill(victim)
def __init__(self, sub, loo): self.sub = sub self.loo = loo self.data_file = './sub{}/pca_loo_tr{}'.format(self.sub, self.loo) self.data = pd.read_pickle(self.data_file) self.data_val = pd.read_pickle(self.data_file) self.costs = pd.Series(np.ones(32)) self.pool = Pool(POOL_SIZE) # 1.000.000 self.env = Environment(self.data, self.costs, FEATURE_FACTOR) self.brain = Brain(self.pool) self.agent = Agent(self.env, self.pool, self.brain) self.log = Log(self.data_val, self.costs, FEATURE_FACTOR, self.brain)
def run(IP, PORT): # Simulated "event queue" eventQueue = deque() # Process queue used for async rpc system. processQ = Queue() sessionManager = MasterSessionManager(IP, PORT, processQ) rpcManager = RPCManager(sessionManager, processQ) containerAllocator = RMContainerAllocator(eventQueue, sessionManager) committerEventHandler = CommitterEventHandler(eventQueue) printed = False; serverList = [] assignedServers = [] serverAssignments = defaultdict(list) pool = Pool() job = Job(work, pool, rpcManager, eventQueue) # Simulate Delayed Job init and start. eventQueue.append(("JOB_INIT", job)) eventQueue.append(("JOB_START", job)) while True: # Simulate "event delivery" containerAllocator.pushNewEvents(eventQueue) committerEventHandler.pushNewEvents(eventQueue) pool.pushNewEvents(eventQueue) eventQueue.clear() # Simulate async mechanisums sessionManager.poll() rpcManager.poll() containerAllocator.heartbeat() committerEventHandler.heartbeat() # For server failure for locator in serverList: if (locator not in sessionManager.serverList()): eventQueue.append(("JOB_UPDATED_NODES", locator)) if serverList != sessionManager.serverList(): print "serverList change" serverList = sessionManager.serverList() # Run tasks pool.poll() if job.getStatus() == "SUCCEEDED" and not printed: print "Job Complete" print job printed = True
def __init__(self, host, user, password, database, port=3306, connect_timeout=5, init_command=None, pool_max_active=0, conn_idle_timeout=600, db_timeout=3): self.pool = Pool(host, user, password, database, port, connect_timeout, init_command, pool_max_active, conn_idle_timeout) self.db_timeout = db_timeout
def runStrategy(in_prices): global mas, emas, smas, lwmas, std, prices log.debug('beginning ma strategy ...') prices = in_prices ps = [p['close'] for p in prices] std = [0] * 51 l = len(prices) for period in range(2, 51): std[period] = [0] * l for i in range(period - 1, l): std[period][i] = np.std(ps[i - period + 1:i + 1], dtype=np.float64, ddof=0) malength = const.MA_MAX + 1 mas = [0] * malength emas = [0] * malength smas = [0] * malength lwmas = [0] * malength for period in range(1, malength): mas[period] = ma.calc_ma(ps, period) emas[period] = ma.calc_ema(ps, period) smas[period] = ma.calc_sma(ps, period) lwmas[period] = ma.calc_lwma(ps, period) log.debug('running ma strategy ...') starttime = datetime.datetime.now() matypes = ['MA', 'EMA', 'SMA', 'LWMA'] pool = Pool(const.POOL_SIZE) for ft, f in [(matype, period) for matype in matypes for period in const.MA_FAST]: for s1t, s1 in [(matype, period) for matype in matypes for period in const.MA_SLOW1]: if s1 != 0 and s1 <= f: continue elapsed = (datetime.datetime.now() - starttime).seconds log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ',' + s1t + '_' + str(s1) + ' ==') for s2t, s2 in [(matype, period) for matype in matypes for period in const.MA_SLOW2]: if s2 != 0 and s2 <= s1: continue #run doTrade(pool, ft, f, s1t, s1, s2t, s2) pool.showStrategies() return pool.strategies[0][0]
def __init__(self, peers, host, port): self.host = host self.port = port self.peer_id = '{}:{}'.format(host, port) self._logger = logging.getLogger(__name__) self._loop = asyncio.get_event_loop() self._pool = Pool(self, peers) # heartbeat constants and bookkeeping variables self._heartbeat_interval = 1000 # ms self._last_interval = None self._min_heartbeat_timeout = 2000 # ms self._max_heartbeat_timeout = 4000 # ms self._heartbeat_timeout = None self._last_heartbeat = None self.reset_heartbeat() self.reset_timeout() self._log = Log(Machine()) self.state = State.FOLLOWER self.term = 0 self.voted = None self.votes = set() self._pending_clients = {} self.handlers = {'append_entries_req': self.handle_append_entries_req, 'append_entries_resp': self.handle_append_entries_resp, 'request_vote_req': self.handle_request_vote_req, 'request_vote_resp': self.handle_request_vote_resp}
def __init__(self, job, updated_job_file=None, client=None, config=None, pool=None, batch_size=None, updated_job_spec=None, roll_back_on_failure=None, max_instance_attempts=None, max_failure_instances=None, start_paused=None, ): self.config = config or IntegrationTestConfig() self.client = client or Client() self.pool = pool or Pool(self.config) if updated_job_spec is None: job_config_dump = load_test_config(updated_job_file) updated_job_spec = JobSpec() json_format.ParseDict(job_config_dump, updated_job_spec) self.updated_job_spec = updated_job_spec self.batch_size = batch_size or 0 self.roll_back_on_failure = roll_back_on_failure or False self.max_instance_attempts = max_instance_attempts or 0 self.max_failure_instances = max_failure_instances or 0 self.start_paused = start_paused or False self.job = job
def go(filename, max_size, use_donor_weight): def edge_score_fun(e): return 1# + (e.patient.weight - e.donor.weight)/999.0 sys.stdout.write(filename + "\t") sys.stdout.write("1\t" if use_donor_weight else "0\t") pool = Pool.from_file(filename) pool.create_donor_patient_arcs(use_donor_weight) sys.stdout.write(str(len(pool.patients)) + "\t") unrestricted_obj_val, optimal_exchanges = pool.solve_uef(edge_score_fun) sys.stdout.write(str(unrestricted_obj_val) + "\t") obj_val = None for i in range(2, max_size+1): if obj_val==unrestricted_obj_val: pass elif i < 6: obj_val, optimal_exchanges = pool.solve_cycle_formulation(i, edge_score_fun) else: obj_val, optimal_exchanges = pool.solve_eef(i, edge_score_fun) sys.stdout.write(str(obj_val) + "\t") sys.stdout.flush() sys.stdout.write(str(pool.n_patients_with_2_compat_donors()) + "\n")
def __init__( self, job_file="test_stateless_job_spec.yaml", client=None, config=None, pool=None, job_config=None, job_id=None, ): self.config = config or IntegrationTestConfig( pool_file='test_stateless_respool.yaml') self.client = client or Client() self.pool = pool or Pool(self.config, client=self.client) self.job_id = job_id self.entity_version = None self.job_spec = None if job_config is not None: self.job_spec = job_config if job_id is not None: self.job_spec = self.get_spec() if self.job_spec is None: job_spec_dump = load_test_config(job_file) job_spec = stateless.JobSpec() json_format.ParseDict(job_spec_dump, job_spec) self.job_spec = job_spec
class TestPoolFunctions(unittest.TestCase): def setUp(self): self.pool = Pool(MsgPackPoolableFactory(), **config) def tearDown(self): unittest.TestCase.tearDown(self) self.pool.destroy() def test_ping(self): try: client = self.pool.borrowObject() self.assertIsNotNone(client, "client is None") rs = client.call("ping") self.assertEqual(rs, "pong", "is pinged") print rs finally: self.pool.returnObject(client)
def run_neuron_experiment(N, input_rates, weights, threshold, T=None, nspikes=None, neuron_model=RegularNeuron): """Run an experiment with a single neuron """ spike_rates = set_list_var(input_rates, N) weights = set_list_var(weights, N) neurons = [neuron_model(spike_rate, weight) for spike_rate, weight in zip(spike_rates, weights)] pool = Pool(neurons=neurons, threshold=threshold) spks_in = pool.gen_nrn_spikes(T=T, nspikes=nspikes) merged_spks_in = pool.merge_spikes(spks_in) spks_out, acc_state = pool.gen_acc_spikes(merged_spks_in) return spks_in, acc_state, spks_out
def runStrategy(in_prices): global mas, emas, smas, lwmas, std, prices log.debug('beginning ma strategy ...') prices = in_prices ps = [p['close'] for p in prices] std = [0] * 51 l = len(prices) for period in range(2, 51): std[period] = [0] * l for i in range(period - 1, l): std[period][i] = np.std(ps[i-period+1 : i+1], dtype=np.float64, ddof=0) malength = const.MA_MAX + 1 mas = [0] * malength emas = [0] * malength smas = [0] * malength lwmas = [0] * malength for period in range(1, malength): mas[period] = ma.calc_ma(ps, period) emas[period] = ma.calc_ema(ps, period) smas[period] = ma.calc_sma(ps, period) lwmas[period] = ma.calc_lwma(ps, period) log.debug('running ma strategy ...') starttime = datetime.datetime.now() matypes = ['MA', 'EMA', 'SMA', 'LWMA'] pool = Pool(const.POOL_SIZE) for ft, f in [(matype, period) for matype in matypes for period in const.MA_FAST]: for s1t, s1 in [(matype, period) for matype in matypes for period in const.MA_SLOW1]: if s1 != 0 and s1 <= f: continue elapsed = (datetime.datetime.now() - starttime).seconds log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ',' + s1t + '_' + str(s1) + ' ==') for s2t, s2 in [(matype, period) for matype in matypes for period in const.MA_SLOW2]: if s2 != 0 and s2 <= s1: continue #run doTrade(pool, ft, f, s1t, s1, s2t, s2) pool.showStrategies() return pool.strategies[0][0]
def cli_entrypoint(): parser = argparse.ArgumentParser() parser.add_argument('-c', help='config file', default='./config.ini') args = parser.parse_args() DemoConfig.config_file = args.c config = DemoConfig() # create table session = DemoData.get_session(config) session.close() logging.basicConfig(level=config.log_level) vacuum = Vacuum() pool = Pool() try: vacuum.start() pool.start() server = ThreadedHTTPServer(('0.0.0.0', config.http_port), Handler) server.serve_forever() except (KeyboardInterrupt, SystemExit): logging.info("Exit signal catched") vacuum.stop = True
def runStrategy(in_prices): global prices, ps log.debug('beginning pattern strategy ...') prices = in_prices ps = [p['close'] for p in prices] starttime = datetime.datetime.now() pool = Pool(const.POOL_SIZE) for i in range(20, 81)[::5]: for j in range(1, 6)[::1]: if i < j: continue for f in range(5, 16)[::2]: elapsed = (datetime.datetime.now() - starttime).seconds log.debug('== ' + str(elapsed) + ', ' + str(i) + ',' + str(j) + ',' + str(f) + ' ==') for sl in range(15, 31)[::3]: for si in range(3, 16)[::2]: doTrade(pool, i, j, f, sl, si) pool.showStrategies() return pool.strategies[0][0]
class CPServer(object): def __init__(self,statefile=None,load=False): self.pool=Pool() self.statefile=statefile if self.statefile and load: self.pool.load_state(self.statefile) def dispatch_request(self,request): #if not request.json: # return BadRequest('No JSON found!') result=self.pool.doMultipleCommandsAsync(request.json) return Response(dumps(result),content_type='text/json') def wsgi_app(self, environ, start_response): request = Request(environ) response = self.dispatch_request(request) return response(environ, start_response) def __call__(self, environ, start_response): return self.wsgi_app(environ,start_response) def close(self): if self.statefile: self.pool.save_state('state.json')
class CPServer(object): def __init__(self): self.pool=Pool() def dispatch_request(self,request): #if not request.json: # return BadRequest('No JSON found!') result=self.pool.doMultipleCommandsAsync(request.json) return Response(dumps(result),content_type='text/json') def wsgi_app(self, environ, start_response): request = Request(environ) response = self.dispatch_request(request) return response(environ, start_response) def __call__(self, environ, start_response): return self.wsgi_app(environ,start_response)
def start(filename, max_size): pool = Pool.from_file(filename) pool.create_donor_patient_arcs(True) pool.show() exchanges = pool.find_exchanges(max_size) for exch in exchanges: exch.show() print "Number of exchanges: ", len(exchanges) # Find out how many exchanges there are if two exchanges with the same # set of patients are considered equal unique_exch = set() for exch in exchanges: unique_exch.add(frozenset(e.patient for e in exch.edges)) print "....", len(unique_exch) def edge_score_fun(e): return 1 # + (e.patient.weight - e.donor.weight)/999.0 print "Optimal exchanges with restricted cycle size (cycle formulation):" obj_val, optimal_exchanges = pool.solve_cycle_formulation(max_size, edge_score_fun) print obj_val for exch in optimal_exchanges: exch.show() print "Optimal exchanges with restricted cycle size:" obj_val, optimal_exchanges = pool.solve_eef(max_size, edge_score_fun) print obj_val for exch in optimal_exchanges: exch.show() print "Optimal exchanges with unrestricted cycle size:" obj_val, optimal_exchanges = pool.solve_uef(edge_score_fun) print obj_val for exch in optimal_exchanges: exch.show() print "Number of patients with 2 compatible paired donors:" print pool.n_patients_with_2_compat_donors() # TODO: put this in a unit test print pool.solve_eef(1, lambda e: 1)[0] / 2
def main(argv): setup_loggers() ctx = parse_args(argv) settings.initialize(ctx) logger.debug("Settings.general:\n %s", pprint.pformat(settings.general).replace("\n", "\n ")) pool = Pool() pool.print_counts() for i in xrange(5, 20): print "" print "setting osds to: %s" % i pool.set_osds(i) pool.print_counts()
def __init__(self, node, timeout = None, checkInterval = DEFAULT_CHECK_INTERVAL, errorCallback = None): """Create a new EtcdClient Parameters: node The initialized etcd node, could be the following values: - A host - A list of host - A tuple (host, port) - A list of tuple (host, port) - The pool.Node object - A list of pool.Node object checkInterval The pool node checking interval in ms, None will disable the node checking """ # Parse the node into a list of pool.Node object if not node: raise ValueError('Invalid node [%s]' % node) nodes = [] if isinstance(node, list): # A node list for n in node: if isinstance(n, Node): nodes.append(n) else: nodes.append(Node.load(n)) else: # A single node if isinstance(node, Node): nodes.append(Node) else: nodes.append(Node.load(node)) # Set attributes self.timeout = timeout self.errorCallback = errorCallback # Create the pool self._pool = Pool(self, nodes, timeout = timeout or DEFAULT_TIMEOUT) # Create the channel self._channel = self.__createchannel__() # Create the dispatcher self._dispatcher = self.__createdispatcher__() # Dispatch background jobs self._dispatcher.timer(self.__backgroundpoolchecking__, checkInterval) # Create APIs self.cluster = ClusterAPI(self) self.keys = KeysAPI(self)
def init(self, connect_config): "set up connection pools" # allow for legacy config # - check for space and comma delimited strings if type(connect_config) == str: if ',' in connect_config: delim = ',' else: delim = ' ' connect_config = connect_config.split(delim) print ('Deprecated: database connection should be a tuple not a string') host, user, pw = connect_config self.conn_pool = Pool(Constructor(self.connect , host, user, pw , charset='utf8'), poolsize=8) self.async_pool = adbapi.ConnectionPool('MySQLdb' , host, user, pw , charset='utf8' , cursorclass=DictCursor)
class PoolTest(AsyncTestCase): def setUp(self): super(PoolTest, self).setUp() self.pool = Pool(['127.0.0.1:11211', '127.0.0.1:11212'], debug=True) @gen_test def test_set_get_multi(self): res = yield self.pool.set_multi({'key1': 'test1', 'key2': 'test2', 'key3': 'test3', 1: '3', 2: 2}, 10, key_prefix="multi_") print res assert res == [] res = yield self.pool.get_multi(['key1', 'key2', 'key3', 1, 2], key_prefix='multi_') print res assert res == {'key1': 'test1', 'key2': 'test2', 'key3': 'test3', 1: '3', 2: 2} yield self.pool.delete("multi_key1") res = yield self.pool.get_multi(['key1', 'key2', 'key3'], key_prefix='multi_') print res assert res == {'key2': 'test2', 'key3': 'test3'} print "--------------" yield self.pool.set_multi({"list": ['a', 'b', 2], "dict": {1: 'a', 'c': 'that is fun', 'info': {1: 3}}}, 10, key_prefix="multi_") res = yield self.pool.get_multi(['list', 'dict'], key_prefix="multi_") print res print type(res)
class Transaction(): """ 基本用法1: ta = Transaction(dbconfig, poolConfig) @ta def test(param1,param2,db): return db.get("select now() as time") test(param1,param2) 注意db参数在函数的 第一个 基本用法1: ta=Transaction(dbconfig, poolConfig) def fun(): //do something ta.execute(fun) example1: dbconfig = { "host":"127.0.0.1:4000", "user":"******", "password":"", "database":"test" } poolConfig = {} ta = Transaction(dbconfig, poolConfig) @ta def test(db): return db.get("select now() as time") @ta def insert(name, birthday, is_relative,db): return db.insert("insert into person(name,birthday,is_relative) values(%s,%s,%s)", name, birthday, is_relative) class Test3(): @staticmethod @ta def test(db): return db.get("select now() as time") print test() from datetime import date print insert("x1", date.today(), 1) print Test3.test() example2: def test(db): return db.get("select now() as time") def test2(db): return db.query("select now() as time") class Test3(): @staticmethod def test(db): return db.get("select now() as time") class Test4(): def test(self,db): return db.get("select now() as time") t=Transaction(dbconfig, poolConfig) rs = t.execute(test) print rs.time rses = t.execute(test2) print [str(rs.time) for rs in rses] rs = t.execute(Test3.test) print rs.time rs = t.execute(Test4().test) print rs.time rs= t.execute(lambda db:db.get("select now() as time")) print rs.time t.destory() """ def __init__(self, dbConfig=None, poolConfig=None, pool=None): '''可给定dbConfig 和poolConfig 或者pool''' if pool is None: self._pool = Pool(Torndb2PoolableFactory(**dbConfig), **poolConfig) else: self._pool = pool def destory(self): self._pool.destroy() def __call__(self, func): def execute(*args,**kwargs): try: db = self._pool.borrowObject() a = [] a.append(db) for arg in args: a.append(arg) newArgs = tuple(a) # print newArgs return func(*newArgs,**kwargs) except Exception, e: db.rollback() raise e finally:
def __init__(self, dbConfig=None, poolConfig=None, pool=None): '''可给定dbConfig 和poolConfig 或者pool''' if pool is None: self._pool = Pool(Torndb2PoolableFactory(**dbConfig), **poolConfig) else: self._pool = pool