def open(self): os.system("clear") print blue("Welcome to %s" % green(self.name)) print blue("========================") while True: print red("How may I help you?") print yellow("1. Buy a ticket\n" " 2. Book a ticket by phone\n" " 3. Book a ticket\n" " 4. Cancel booking\n" " 5. See how many money do you have\n" " 6. Goodbye!\n") option = raw_input(green("I want to: ")) if option == "1": self.buy_ticket() if option == "2": self.book_ticket_by_phone() if option == "3": self.book_ticket() if option == "4": self.cancel_booking() if option == "5": self.money() if option == "6": break
async def get_download_link(session: ClientSession, ep_name: str, link: str, quality: str, pb) -> DownloadLink: """Download an episode""" # Try each server in this order: nova>rapidvideo>mp4upload for server, method in DOWNLOAD_METHODS.items(): # Redirects=False cause kissanime will redirect to the "default" server if the server doesn't exist # Or to the "Are you human" captcha page page, status = await retryable_get_request(session, link + f"&s={server}", 5, 5, allow_redirects=False) if status == 302: continue video_link = re.findall(VIDEO_LINK_PATTERN, page)[0][:-1] pb.update(1) try: return DownloadLink(ep_name, await method(session, video_link, quality)) except NoQualityFound: pb.write(red(f"Couldn't find {ep_name} in {quality} ❌")) raise pb.write(red(f"No valid server found for {ep_name} ❌")) raise NoServerFound
def show_diff(old, new): for k, v in new.iteritems(): if k in old.keys() and v == old[k]: continue if k in old.keys() and v != old[k]: red(" - ['{}'] = {}".format(k, old[k])) green(" + ['{}'] = {}".format(k, v))
def _request(self, url_path, query_params, json, expected_codes, json_response): # Construct the URL using url_path, query_params and the api key url = url_path if FLAGS.api_key: if query_params: url = '%s?key=%s&%s' % (url_path, FLAGS.api_key, query_params) else: url = '%s?key=%s' % (url_path, FLAGS.api_key) elif query_params: url = '%s?%s' % (url_path, query_params) # Prepare Headers headers = {'Content-Type': 'application/json'} if FLAGS.auth_token: headers['Authorization'] = 'Bearer ' + FLAGS.auth_token self._conn.request('POST', url, json, headers) response = utils.Response(self._conn.getresponse()) if not response.status_code in expected_codes: print(utils.red( "Invalid status code {}:\n\turl={},\n\trequest_header={},\n\trequest_body={},\n\tresponse={}\n\n").format( response.status_code, url, headers, json, response)) self._unexpected_errors += 1 if json_response and not ( response.status_code != 200 and response.content_type == "text/plain") \ and not response.is_json(): print(utils.red( "response is not json {}:\n\turl={},\n\trequest_header={},\n\trequest_body={},\n\tresponse={}\n\n").format( response.content_type, url, headers, json, response)) self._unexpected_errors += 1 self._total_requests += 1
def _status(self, final=False): if not final: new = (green(len(self._completed)), white(len(self._running)), yellow(len(self._queued)), green('finished'), white('running'), yellow('queued')) if hasattr(self, 'last_status') and new == self.last_status: return self.last_status = (green(len(self._completed)), white(len(self._running)), yellow(len(self._queued)), green('finished'), white('running'), yellow('queued')) print WHIPE, "[%s/%s/%s] %s, %s, %s" % new else: print "\n[ %s OK / %s ERROR ] in %s seconds" % ( green(self._num_of_jobs - self._errors, True), red(self._errors), time.time() - self._time_start) if self._errors: print red("Failures:", True) for job in self._completed: if job.exitcode != 0: print red(job.name) sys.stdout.flush()
def start(self): os.system("clear") while True: print green("How can I help you, %s?" % self.user) print yellow("1. Transfer some money\n" " 2. Pay\n" " 3. Retrieve\n" " 4. Show balance\n" " 5. Logout\n") option = int(raw_input(blue("I want to: "))) if option == 1 or option == 2: to = raw_input(red("to: ")) money = raw_input(red("sum: ")) try: print self.bank.transfer_from(self.user, to, money) except ValueError as ve: print red(ve) elif option == 3: money = raw_input(red("sum: ")) try: if self.bank.transfer_from(self.user, self.name, money): print green("Operation finished with success!\n") else: print red("Something went wrong...try again later\n") except ValueError as ve: print red(ve) elif option == 4: print self.bank.show_balance(self.user) elif option == 5: self.user = None return
def run(self): if self.is_negative: if self._from != 10: self.log.error(red("I dont know how to convert negative numbers if" " there are not in decimal base")) return False else: number = self.transform_negative() else: number = self.transform_positive() print green("Your number is %s" % red(number))
def login(self): print "\n\n" username = raw_input(green("username: "******"password: "******"db/clients.txt", "r") as f: clients = f.read() credentials = "%s:%s" % (username, sha1(password).hexdigest()) if credentials in clients: print green("Welcome!") self.user = username self.start() else: print red("Wrong credentials!")
def process_tick_top(self, tick, last_row): volok = (last_row['V'] < last_row['VMA20']) priceok = (last_row['H'] > last_row['BBU']) self.log('%s(%.2f < %.2f) %s(%s > %s)' % (green('volok') if volok else red('volko'), last_row['V'], last_row['VMA20'], green('priceok') if priceok else red('priceko'), last_row['H'], last_row['BBU'])) if priceok: if volok: self.sell(tick) else: self.set_stop(tick, last_row['BBM'])
def path_exists(self, path, host=None, negate=False): containers = [self.containers[host]] if host else self.containers.itervalues() for container in containers: if negate: if not path_exists(path, container): continue print(utils.red("Found path <{}> on host <{}>".format(path, self.host_from_container(container)))) else: if path_exists(path, container): continue print(utils.red("Path <{}> not found on host <{}>".format(path, self.host_from_container(container)))) return False return True
async def download_episode(session: ClientSession, name: str, link: str, path: str, total_bar: tqdm, pool: asyncio.Queue): """Download the episode to local storage""" file_target = f'{path}/{name}.mp4' try: async with get_connection( pool): # Limit ourself to max concurrent downloads if os.path.isfile(file_target): raise FileExistsError(f'{name} already exists in the folder') req_method = session.post if Servers.MP4UPLOAD in link else session.get async with req_method(link) as resp: if resp.status != 200: raise RuntimeError( f'Got a bad response from the server for {name}: {resp.status}' ) file_size = int(resp.headers.get('content-length')) with tqdm(desc=name, total=file_size, unit='B', unit_scale=True, unit_divisor=1024, leave=False, ncols=BAR_WIDTH) as progress_bar: async with aiofiles.open(f'{path}/{name}', mode='wb') as file: async for chunk, _ in ChunkTupleAsyncStreamIterator( resp.content): await file.write(chunk) progress_bar.update(len(chunk)) # Mark success and wait a bit before removing the bar progress_bar.set_postfix_str(green('✔️')) await asyncio.sleep(5) except Exception as e: tqdm.write(red(f'Failed to download {name} : {e} ❌')) if DEBUG: tqdm.write( red(''.join( traceback.format_exception(None, e, e.__traceback__)))) try: os.remove(f'{path}/{name}') except FileNotFoundError: pass return False finally: total_bar.update(1) return True
def process_tick_middle(self, tick, last_row): volok = (last_row['V'] > last_row['VMA20']) priceok = (tick['H'] > last_row['BBM']) self.log('%s(%.2f > %.2f) %s(%s > %s)' % (green('volok') if volok else red('volko'), last_row['V'], last_row['VMA20'], green('priceok') if priceok else red('priceko'), tick['H'], last_row['BBM'])) if priceok: if volok: self.status = 'top' self.set_stop(tick, self.entry) else: self.sell(tick)
def show_unencrypted_diff(diff_part, password_file=None): intense(get_head(diff_part).strip()) old, new = decrypt_diff(diff_part, password_file) diff = difflib.unified_diff(old.split('\n'), new.split('\n'), lineterm='') # ... we'll take the git filenames from git's diff output rather than # ... difflib for line in islice(diff, 2, None): if line.startswith('-'): red(line) elif line.startswith('+'): green(line) elif line.startswith('@@'): cyan(line) else: print line
def handle(socket, address): print green("Client connected") while True: data = socket.recv(2048) if not data: print red("Client disconnected") return message = parser(data) if message is None: return if message[0] == "transfer": transfer(message[1], socket)
def execute_program(raw_program: str, initial_register_zero=0): program = raw_program.splitlines() ip_idx = int(program.pop(0)[4]) register = [0] * 6 register[0] = initial_register_zero ip = register[ip_idx] init_time = time() for i in count(1): if ip == 26: print(register[1]) row = program[ip] instruction = row[:4] a, b, c = map(int, row[5:].split()) if instruction == "seti": # stores value A into register C register[c] = a elif instruction == "addi": # stores into register C the result of adding register A and value B register[c] = register[a] + b elif instruction == "addr": # stores into register C the result of adding register A and register B. register[c] = register[a] + register[b] elif instruction == "setr": # copies the contents of register A into register C register[c] = register[a] elif instruction == "mulr": # stores into register C the result of multiplying register A and register B register[c] = register[a] * register[b] elif instruction == "muli": # stores into register C the result of multiplying register A and value B register[c] = register[a] * b elif instruction == "eqrr": # sets register C to 1 if register A is equal to register B. Otherwise, register C is set to 0 register[c] = int(register[a] == register[b]) elif instruction == "gtrr": # sets register C to 1 if register A is greater than register B. Otherwise, register C is set to 0 register[c] = int(register[a] > register[b]) else: u.red(f"unknown instruction {instruction}") break new_ip = register[ip_idx] + 1 if new_ip >= len(program): break register[ip_idx] = new_ip ip = new_ip print( f"program took {time() - init_time:.2f} seconds / {i} iterations to execute" ) return register
def run_all_tests(self): for _ in range(FLAGS.runs): self._run_fuzz_tests() if self._unexpected_errors > 0: sys.exit(utils.red('Fuzz test failed.')) else: print utils.green('Fuzz test passed.')
def _get_status(self): self._status_conn.request('GET', '/ready') status = utils.Response(self._status_conn.getresponse()) if status.status_code != 200: sys.exit(utils.red( 'ESPv2 crash')) return status.text.strip()
def _check_for_crash(self): status = self._get_status() if status != "LIVE": print(status) sys.exit(utils.red('ESPv2 crash')) return print utils.green('No crashes detected.')
def run(self): pid = 0 try: if self.silent: self.proc = subprocess.Popen( [self.path] + self.args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: self.proc = subprocess.Popen([self.path] + self.args) pid = self.proc.pid self.started = True except Exception as e: print(utils.red("Process start failed:") + " %s" % self.name) print(str(e)) sys.exit(1) try: while self.proc.poll() is None: self.started = True time.sleep(0.1) self.started = True self.retcode = -1 if self.proc is None else self.proc.poll() self.proc = None except Exception as e: return
def main(args = None): opts = docopt.docopt( doc = __doc__, argv = args) try: if opts["--no-color"]: utils.disable_colors() if opts["--verbose"]: utils.enable_tracing() for name in opts["NAMES"]: dist = get_distribution(name) for name in required(dist): raise Error(dist.project_name, "required by", name) for req in dist.requires(): names = filter(lambda name: name != dist.project_name, required(req)) if not names: uninstall(req.project_name, dryrun = opts["--dry-run"]) else: utils.trace(req.project_name, "not uninstalled, used by", names) uninstall(dist.project_name, dryrun = opts["--dry-run"]) # else: # for dist in pip.get_installed_distributions(): # print utils.magenta(dist.project_name), dist.location except utils.Error as exc: raise SystemExit(utils.red(exc))
def _dump_data(name, data): try: with open(name, 'wb') as fh: fh.write(data) print "Wrote: %s" % (red(name)) except Exception, e: print "Error: could not write (%s), (%s)." % (name, str(e))
def open(self): os.system("clear") print blue("Welcome to ") + yellow(self.bank.name) print blue("========================\n\n") while True: print green("Please choose one of the action:") print red("1. Register\n" " 2. Login\n\n") option = int(raw_input(yellow("I want to: "))) if option == 1: self.register() elif option == 2: self.login() else: print red("I dont't understand you! Please repeat")
def run_all_tests(self): shelf1 = {'name': 'shelves/1', 'theme': 'Fiction'} shelf2 = {'name': 'shelves/2', 'theme': 'Fantasy'} book13 = { 'name': 'shelves/1/books/3', 'author': 'Neal Stephenson', 'title': 'REAMDE' } book24 = { 'name': 'shelves/2/books/4', 'author': 'George R.R. Martin', 'title': 'A Game of Thrones' } self.verify_shelf(shelf1) self.verify_shelf(shelf2) self.verify_list_shelves([shelf1, shelf2]) self.verify_list_books(shelf1['name'], [book13]) self.verify_list_books(shelf2['name'], [book24]) self.verify_book(book13) self.verify_book(book24) self.verify_jwt_locations() self.verify_allow_cors_passthrough() if self._failed_tests: sys.exit( utils.red('%d tests passed, %d tests failed.' % (self._passed_tests, self._failed_tests))) else: print utils.green('All %d tests passed' % self._passed_tests)
def eval_network(network, env_creator, num_episodes, greedy=False, verbose=True): len_int = [10, 10] emulator = SequentialBatchEmulator(env_creator, num_episodes, False) try: num_steps, rewards, final_res = evaluate.stats_eval(network, emulator, greedy=greedy) finally: emulator.close() set_exit_handler() mean_steps = np.mean(num_steps) min_r, max_r = np.min(rewards), np.max(rewards) mean_r, std_r = np.mean(rewards), np.std(rewards) stats = TrainingStats(mean_r, max_r, min_r, std_r, mean_steps, final_res) if verbose: lines = [ 'Perfromed {0} tests:'.format(len(num_steps)), 'Mean number of steps: {0:.3f}'.format(mean_steps), 'Mean R: {0:.2f} | Std of R: {1:.3f}'.format(mean_r, std_r), 'Success percentage: {} '.format(final_res) ] logging.info(utils.red('\n'.join(lines))) return stats
def job_status(job): status = blue("STOPPED") if not job.is_enabled(): status = red("DISABLED") if job.is_running(): status = green("RUNNING") return status
def run(self): pid = 0 try: if self.silent: self.proc = subprocess.Popen( [self.path] + self.args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: self.proc = subprocess.Popen([self.path] + self.args) pid = self.proc.pid self.started = True except Exception as e: print(utils.red("Process start failed:") + " %s" % self.name) print(str(e)) sys.exit(1) try: while self.proc.poll() is None: self.started = True time.sleep(self.interval) self.started = True self.retcode = -1 if self.proc is None else self.proc.poll() self.proc = None except Exception as e: return
def run_all_tests(self): self.verify_key_restriction(); if self._failed_tests: sys.exit(utils.red('%d tests passed, %d tests failed.' % ( self._passed_tests, self._failed_tests))) else: print utils.green('All %d tests passed' % self._passed_tests)
def process_tick_rsi(self, tick, last_row): rsiok = (last_row['RSI'] > 70) self.log('%s(%.1f > %f)' % (green('rsiok') if rsiok else red('rsiko'), last_row['RSI'], 70)) if rsiok: self.sell(tick) else: self.set_stop(last_row['ATR_STP'])
def register(self): print "\n\n" username = raw_input(green("username: "******"password: "******"db/clients.txt", "ab+") as f: clients = f.read() if "%s:" % username in clients: print red("Username already taken") return f.write("%s:%s\n" % (username, sha1(password).hexdigest())) initial_deposit = raw_input(green("Initial deposit: ")) with open("clients/%s" % sha1(username).hexdigest(), "w") as g: g.write(initial_deposit) print green("Succesfully register!") self.user = username self.start()
def run_all_tests(self): self.verify_quota_control() if self._failed_tests: sys.exit( utils.red('%d tests passed, %d tests failed.' % (self._passed_tests, self._failed_tests))) else: print utils.green('All %d tests passed' % self._passed_tests)
def delete(self, predicate, bindings): """Delete a fact from the working memory, if it's present.""" ground_expression = predicate.ground(bindings=bindings) for fact in list(self.facts): if fact == ground_expression: self.facts.remove(fact) self._facts_by_first_character[fact[0]].remove(fact) if config.VERBOSITY >= 2: print(utils.red(f" {fact}")) return
def __init__(self, raw_description): tile_data = raw_description.splitlines() tile_title = tile_data.pop(0) self.id = int(tile_title[5:9]) # all tiles have 4-digits numbers self.borders = [ tile_data[0], # top "".join(tile_row[-1] for tile_row in tile_data), # right tile_data[-1], # bottom "".join(tile_row[0] for tile_row in tile_data), # left ] for border in self.borders: if border == border[::-1]: u.red(f"{self.id} will be a problem") self.inner = [row[1:-1] for row in tile_data[1:-1]] # self.inner = tile_data # for debug self.size = len(self.inner[0]) self.neighbors = {} self.locked = False self.first = False
def process_tick_searching(self, tick, last_row): volok = (last_row['V'] < last_row['VMA20']) priceok = (tick['L'] < (last_row['BBL'] * self.selling_pressure(last_row))) bbok = (last_row['BBW'] > self.percent) self.log('%s(%.2f < %.2f) %s(%s < %s) %s(%.2f > %.2f)' % (green('volok') if volok else red('volko'), last_row['V'], last_row['VMA20'], green('priceok') if priceok else red('priceko'), btc2str(tick['L']), btc2str(last_row['BBL']), green('bbok') if bbok else red('bbko'), last_row['BBW'], self.percent)) if volok and priceok and bbok: self.status = 'buying' self.entry = last_row['L'] self.quantity = self.amount / self.entry self.send_order(self.exch.buy_limit, self.pair, self.quantity, self.entry) self.log('buying %f @ %s' % (self.quantity, btc2str(self.entry)))
def send_to_lava(self): try: dev = self.get_device_status() if dev["status"] == "offline": print(red("Device seems offline, not sending the job")) return except Exception as e: print(red(repr(e))) print(red("Not sending the job")) return print("Sending to LAVA") job_str = self.job_template.render(self.job) ret = utils.get_connection(**self.options).scheduler.submit_job(job_str) try: for r in ret: print(green("Job send (id: %s)" % r)) print("Potential working URL: ", "%s/scheduler/job/%s" % (self.options['web_ui_address'], r)) except: print(green("Job send (id: %s)" % ret)) print("Potential working URL: ", "%s/scheduler/job/%s" % (self.options['web_ui_address'], ret))
def print_diff(dbdict, data, removes=True): "Print a (hopefully) human readable list of changes." # TODO: needs work, especially on multiline properties, # empty properties (should probably never be allowed but still) # and probably more corner cases. Also the output format could # use some tweaking. try: from collections import defaultdict import jsonpatch from jsonpointer import resolve_pointer, JsonPointerException ops = defaultdict(int) diff = jsonpatch.make_patch(dbdict, data) for d in diff: try: ptr = " > ".join(decode_pointer(d["path"])) if d["op"] == "replace": print yellow("REPLACE:") print yellow(ptr) db_value = resolve_pointer(dbdict, d["path"]) print red(dump_value(db_value)) print green(dump_value(d["value"])) ops["replace"] += 1 if d["op"] == "add": print green("ADD:") print green(ptr) if d["value"]: print green(dump_value(d["value"])) ops["add"] += 1 if removes and d["op"] == "remove": print red("REMOVE:") print red(ptr) value = resolve_pointer(dbdict, d["path"]) if value: print red(dump_value(value)) ops["remove"] += 1 except JsonPointerException as e: print " - Error parsing diff - report this!: %s" % e # # The following output is a bit misleading, removing for now # print "Total: %d operations (%d replace, %d add, %d remove)" % ( # sum(ops.values()), ops["replace"], ops["add"], ops["remove"]) return diff except ImportError: print >> sys.stderr, ("'jsonpatch' module not available - " "no diff printouts for you! (Try -d instead.)")
def get_str(self, models_by_name=None, print_name=True): s = '' if print_name: s = '%s : %s \t<%s>' % (red(self.name.ljust(20)), green(self.superclass.ljust(15)), blue(self.filename)) for field in self.fields: s += '\n%s' % field.__repr__() if self.superclass != 'MTLModel' and models_by_name: # Also print the fields of the superclass s += '\n%s' % models_by_name[self.superclass].get_str(models_by_name=models_by_name, print_name=False) return s
def on_post_item(self, response, **kwargs): input_file = kwargs.get('input_file', '') source = kwargs.get('source', '') filename = self.format_filename(input_file.replace(source, '')) if response.success: self._optimized += 1 self._input_bytes += response.input_size self._output_bytes += response.output_size print("%s %16s %37s" % (filename, green("OK"), response.output_ratio)) else: self._failed += 1 print("%s %18s %30s" % (filename, red("FAIL"), "-"))
def get_str(self, models_by_name=None, print_name=True): s = '' if print_name: s = '%s : %s \t<%s>' % (red( self.name.ljust(20)), green( self.superclass.ljust(15)), blue(self.filename)) for field in self.fields: s += '\n%s' % field.__repr__() if self.superclass != 'MTLModel' and models_by_name: # Also print the fields of the superclass s += '\n%s' % models_by_name[self.superclass].get_str( models_by_name=models_by_name, print_name=False) return s
def process_tick_searching(self, tick, last_row): RSI_THRESHOLD = 30 CORPSE_THRESHOLD = 0.2 rsiok = (last_row['RSI'] <= RSI_THRESHOLD) priceok = (tick['L'] < last_row['BBL']) bbok = (last_row['BBW'] >= self.percent) volok = (last_row['V'] < last_row['VMA20']) if last_row['H'] == last_row['C']: candle_corpse = 1 else: candle_corpse = ((last_row['H'] - min(last_row['C'], last_row['O'])) / (last_row['H'] - last_row['L'])) candleok = (candle_corpse <= CORPSE_THRESHOLD) or volok self.log('%s(%s <= %s) %s(%.1f < %f) %s(%.2f >= %.2f) ' '%s(%.2f <= %.2f or %s %.2f < %.2f)' % (green('priceok') if priceok else red('priceko'), btc2str(tick['L']), btc2str(last_row['BBL']), green('rsiok') if rsiok else red('rsiko'), last_row['RSI'], RSI_THRESHOLD, green('bbok') if bbok else red('bbko'), last_row['BBW'], self.percent, green('candleok') if candleok else red('candleko'), candle_corpse, CORPSE_THRESHOLD, green('volok') if volok else red('volko'), last_row['V'], last_row['VMA20'])) if rsiok and priceok and bbok and candleok: self.status = 'buying' self.entry = last_row['L'] self.quantity = self.amount / self.entry self.send_order(self.exch.buy_limit, self.pair, self.quantity, self.entry) self.log('buying %f @ %s' % (self.quantity, btc2str(self.entry))) self.middle = last_row['BBM'] self.top = last_row['BBU']
def run(self): try: if self.silent: self.proc = subprocess.Popen([self.path] + self.args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: self.proc = subprocess.Popen([self.path] + self.args) except Exception as e: print(utils.red("Process start failed:") + " %s" % self.name) print(str(e)) sys.exit(1) try: while self.proc.poll() is None: time.sleep(self.interval) self.proc = None except: return print("Process %s ended" % self.name)
def run(self): try: if self.silent: self.proc = subprocess.Popen([self.path] + self.args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: self.proc = subprocess.Popen([self.path] + self.args) except Exception as e: print (utils.red("Process start failed:") + " %s" % self.name) print (str(e)) sys.exit(1) try: while self.proc.poll() is None: time.sleep(self.interval) self.proc = None except: return print ("Process %s ended" % self.name)
def _triggers(self, rule, bindings): """Return whether the given rule triggers.""" for precondition in rule.preconditions: if precondition.optional_roles: continue if config.VERBOSITY >= 3: print(f" Evaluating precondition: {precondition}") if not self.working_memory.holds(condition=precondition, bindings=bindings): if config.VERBOSITY >= 3: print( utils.red( f" Doesn't hold: {precondition.ground(bindings=bindings)}" )) return False if config.VERBOSITY >= 3: print(f" Holds: {precondition.ground(bindings=bindings)}") return True
def process_tick_nine(self, tick): if tick['T'].hour == 9: if tick['C'] >= self.midnight_price * 1.05: self.status = 'buying' self.entry = tick['C'] self.quantity = self.amount / self.entry self.send_order(self.exch.buy_limit, self.pair, self.quantity, self.entry) self.log('%s %f @ %s' % (green('buying'), self.quantity, btc2str(self.entry))) else: self.log('%s: %s >= %s. retrying tomorrow. %.3f' % (red('no up trend'), btc2str(tick['C']), btc2str(self.midnight_price * 1.05), self.amount)) self.status = 'midnight'
def evaluate(self, verbose=True): num_steps, rewards = self.eval_func(*self.eval_args, **self.eval_kwargs) mean_steps = np.mean(num_steps) min_r, max_r = np.min(rewards), np.max(rewards) mean_r, std_r = np.mean(rewards), np.std(rewards) stats = TrainingStats(mean_r, max_r, min_r, std_r, mean_steps) if verbose: lines = [ 'Perfromed {0} tests:'.format(len(num_steps)), 'Mean number of steps: {0:.3f}'.format(mean_steps), 'Mean R: {0:.2f} | Std of R: {1:.3f}'.format(mean_r, std_r) ] logging.info(red('\n'.join(lines))) return stats
def profile_leaks(shell, queries, count=1, rounds=1, supp_file=None): report = {} for name, query in queries.iteritems(): print("Analyzing leaks in query: %s" % query) # Apply count (optionally run the query several times). summary = check_leaks(shell, query, count, supp_file) display = [] for key in summary: output = summary[key] if output is not None and output[0] != "0": # Add some fun colored output if leaking. if key == "definitely": output = utils.red(output) report[name] = "LEAKING" if key == "indirectly": output = utils.yellow(output) report[name] = "WARNING" else: report[name] = "SAFE" display.append("%s: %s" % (key, output)) print(" %s" % "; ".join(display)) return report
def images_delete(self, uproot=False): func = image_delete_and_containers if uproot else image_delete for image in self.get_real_images(): print(utils.red("Delete image {}".format(image))) func(image) return self
def run(self, platform, precision=None, devices=None, use_dispersion_correction=True): """ Runs the test on the given platform with the given precision model. Parameters ---------- platform : str Name of the OpenMM platform to use (CPU, Reference, CUDA, or OpenCL) precision : str Precision model to use for CUDA or OpenCL (single, double, or mixed) devices : int or tuple of ints Which GPUs to run on (a tuple will run in parallel) """ if not platform in ('CPU', 'Reference', 'CUDA', 'OpenCL'): raise ValueError('Platform %s not recognized.' % platform) # Define our platform and our platform properties plat = mm.Platform.getPlatformByName(platform) properties = None if platform == 'CUDA': if not precision in ('mixed', 'double', 'single'): raise ValueError('You must set the precision to single, ' 'double, or mixed for the CUDA platform.') properties = dict(CudaPrecision=precision) if devices is not None: properties['CudaDeviceIndex'] = str(devices) elif platform == 'OpenCL': if not precision in ('mixed', 'double', 'single'): raise ValueError('You must set the precision to single, ' 'double, or mixed for the CUDA platform.') properties = dict(OpenCLPrecision=precision) if devices is not None: properties['OpenCLDeviceIndex'] = str(devices) # Create a new system with no charges so we can compare vdW and EEL # energies to Amber parmcopy = copy(self.parm) for i in range(len(parmcopy.parm_data['CHARGE'])): parmcopy.parm_data['CHARGE'][i] = 0.0 system = parmcopy.createSystem(nonbondedCutoff=8.0*u.angstroms, nonbondedMethod=app.PME) system.setDefaultPeriodicBoxVectors(*self.parm.box_vectors) # Test serialization xmlsys = mm.XmlSerializer.deserialize( mm.XmlSerializer.serialize(self.system) ) # Loop through all systems and turn on or off the dispersion correction print 'Trying to set PME parameters...', succeeded = None for sysmod in (self.system, self.systemapp, system, xmlsys): for force in sysmod.getForces(): if isinstance(force, mm.NonbondedForce): force.setUseDispersionCorrection(use_dispersion_correction) # See if we can set the PME parameters try: force.setPMEParameters(3.285326106/u.nanometers, 60, 64, 60) succeeded = True except AttributeError: # This version of OpenMM does not support setting PME # parameters succeeded = False if succeeded: print 'Changed.' elif succeeded is None: print 'No NonbondedForce detected.' else: print 'OpenMM is too old. Could not change PME parameters.' # Define some integrators dummyint1 = mm.VerletIntegrator(1.0e-6*u.picoseconds) dummyint2 = mm.VerletIntegrator(1.0e-6*u.picoseconds) dummyint3 = mm.VerletIntegrator(1.0e-6*u.picoseconds) dummyint4 = mm.VerletIntegrator(1.0e-6*u.picoseconds) # Define the contexts if properties is None: context1 = mm.Context(self.system, dummyint1, plat) context2 = mm.Context(system, dummyint2, plat) context3 = mm.Context(self.systemapp, dummyint3, plat) context4 = mm.Context(xmlsys, dummyint4, plat) else: context1 = mm.Context(self.system, dummyint1, plat, properties) context2 = mm.Context(system, dummyint2, plat, properties) context3 = mm.Context(self.systemapp, dummyint3, plat, properties) context4 = mm.Context(xmlsys, dummyint4, plat, properties) # Set the context positions context1.setPositions(self.parm.positions) context2.setPositions(self.parm.positions) context3.setPositions(self.crdapp.getPositions()) context4.setPositions(self.parm.positions) # Get the energies eunit = u.kilocalories_per_mole state = context1.getState(getEnergy=True, getForces=True, enforcePeriodicBox=True) funit = eunit / u.angstrom forces = state.getForces().value_in_unit(funit) tote = state.getPotentialEnergy().value_in_unit(eunit) state = context3.getState(getEnergy=True, enforcePeriodicBox=True) toteapp = state.getPotentialEnergy().value_in_unit(eunit) state = context4.getState(getEnergy=True, enforcePeriodicBox=True) xmltote = state.getPotentialEnergy().value_in_unit(eunit) # Now get the decomposed energies from both the system and the # deserialized system to check that serialization and deserialization # behave as expected with these force objects and force groups state = context1.getState(getEnergy=True, enforcePeriodicBox=True, groups=2**self.parm.BOND_FORCE_GROUP) bonde = state.getPotentialEnergy().value_in_unit(eunit) state = context4.getState(getEnergy=True, enforcePeriodicBox=True, groups=2**self.parm.BOND_FORCE_GROUP) xmlbonde = state.getPotentialEnergy().value_in_unit(eunit) state = context1.getState(getEnergy=True, enforcePeriodicBox=True, groups=2**self.parm.ANGLE_FORCE_GROUP) anglee = state.getPotentialEnergy().value_in_unit(eunit) state = context4.getState(getEnergy=True, enforcePeriodicBox=True, groups=2**self.parm.ANGLE_FORCE_GROUP) xmlanglee = state.getPotentialEnergy().value_in_unit(eunit) state = context1.getState(getEnergy=True, enforcePeriodicBox=True, groups=2**self.parm.DIHEDRAL_FORCE_GROUP) dihede = state.getPotentialEnergy().value_in_unit(eunit) state = context4.getState(getEnergy=True, enforcePeriodicBox=True, groups=2**self.parm.DIHEDRAL_FORCE_GROUP) xmldihede = state.getPotentialEnergy().value_in_unit(eunit) state = context1.getState(getEnergy=True, enforcePeriodicBox=True, groups=2**self.parm.NONBONDED_FORCE_GROUP) nonbe = state.getPotentialEnergy().value_in_unit(eunit) state = context4.getState(getEnergy=True, enforcePeriodicBox=True, groups=2**self.parm.NONBONDED_FORCE_GROUP) xmlnonbe = state.getPotentialEnergy().value_in_unit(eunit) state = context2.getState(getEnergy=True, enforcePeriodicBox=True, groups=2**self.parm.NONBONDED_FORCE_GROUP) vdwe = state.getPotentialEnergy().value_in_unit(eunit) eele = nonbe - vdwe # Now get the sander forces and compare them traj = netcdf_file(get_fn('sander_pme.nc'), 'r') sander_forces = traj.variables['forces'][0] maxdif = [abs(ofrc-sfrc) for ofrc, sfrc in zip(forces[0], sander_forces[0])] maxrel = [abs(ofrc-sfrc)/sfrc for ofrc, sfrc in zip(forces[0], sander_forces[0])] avgdif = [0, 0, 0] avgrel = [0, 0, 0] n = 0 for ofrcs, sfrcs in zip(forces, sander_forces): for i, sfrc in enumerate(sfrcs): ofrc = ofrcs[i] dif = abs(ofrc-sfrc) rel = dif/sfrc maxdif[i] = max(maxdif[i], dif) maxrel[i] = max(maxrel[i], rel) avgdif[i] += dif avgrel[i] += rel n += 1 avgdif = [x/n for x in avgdif] avgrel = [x/n for x in avgrel] # The sander energies are: # Etot = -69285.4160 EKtot = 0.0000 EPtot = -69285.4160 # BOND = 404.9439 ANGLE = 1003.4499 DIHED = 2231.7367 # 1-4 NB = 440.7084 1-4 EEL = 3818.2959 VDWAALS = 8271.5191 # EELEC = -85456.0701 EHBOND = 0.0000 RESTRAINT = 0.0000 sander = dict(bond=404.9439, angle=1003.4499, dihedral=2231.7367, vdw=8271.5191+440.7084, eel=-85456.0701+3818.2959, total=-69285.4160) if not use_dispersion_correction: # Without the long-range dispersion correction, VDWAALS = 8943.8420 sander['total'] -= sander['vdw'] sander['vdw'] = 8943.8420 + 440.7084 sander['total'] += sander['vdw'] bonddif = bonde - sander['bond'] angledif = anglee - sander['angle'] diheddif = dihede - sander['dihedral'] vdwdif = vdwe - sander['vdw'] # includes 1-4 also eeldif = eele - sander['eel'] # Includes 1-4 also totaldif = tote - sander['total'] appdif = tote - toteapp print 'Energy differences compared to sander/Amber (kcal/mol)' print ' Absolute Relative sander' print '------------------------------------------------------' print 'Bond =', colorize_error(bonddif), \ colorize_error(bonddif/sander['bond'], 1e-6), \ '%12.4f'%sander['bond'] print 'Angle =', colorize_error(angledif), \ colorize_error(angledif/sander['angle'], 1e-6), \ '%12.4f'%sander['angle'] print 'Dihedral =', colorize_error(diheddif), \ colorize_error(diheddif/sander['dihedral'], 1e-6), \ '%12.4f'%sander['dihedral'] if use_dispersion_correction: # The dispersion correction in Amber neglects the repulsive part of # the correction, but OpenMM does not. Therefore, when we are using # the dispersion correction we should allow for a slightly larger # energy difference. print 'vdWaals =', colorize_error(vdwdif, 1.0), \ colorize_error(vdwdif/sander['vdw'], 1e-4), \ '%12.4f'%sander['vdw'] else: print 'vdWaals =', colorize_error(vdwdif, 1e-2), \ colorize_error(vdwdif/sander['vdw'], 1e-6), \ '%12.4f'%sander['vdw'] print 'Elec =', colorize_error(eeldif, 4e0), \ colorize_error(eeldif/sander['eel'], 1e-3), '%12.4f'%sander['eel'] print 'Total =', colorize_error(totaldif, 4e0), \ colorize_error(totaldif/sander['total'], 1e-3), \ '%12.4f'%sander['total'] print '' print 'Difference b/w ParmEd and OpenMM App layer' print '------------------------------------------' print 'Total =', colorize_error(appdif, tolerance=5e-3) print '' print 'Difference b/w sander and OpenMM forces' print '---------------------------------------' print 'Maximum deviation = [%12s, %12s, %12s]' % colorize_list(maxdif,2e0) print 'Maximum rel. dev. = [%12s, %12s, %12s]' % colorize_list(maxrel,2e0) print 'Average deviation = [%12s, %12s, %12s]' % colorize_list(avgdif,1e-1) print 'Average rel. dev. = [%12s, %12s, %12s]' % colorize_list(avgrel,5e-1) # Now test serialization CUTOFF = 1e-5 CUTOFFNB = 1e-2 print '' print 'Serialization tests' print '-------------------' print 'Bond........', if abs(xmlbonde - bonde) < CUTOFF: print green('OK') else: dif = xmlbonde - bonde print red('off by %.4e (%f%%)' % (dif, 100*dif/(bonde or xmlbonde))) print 'Angle.......', if abs(xmlanglee - anglee) < CUTOFF: print green('OK') else: dif = xmlanglee - anglee print red('off by %.4e (%f%%)' % (dif,100*dif/(anglee or xmlanglee))) print 'Dihedral....', if abs(xmldihede - dihede) < CUTOFF: print green('OK') else: dif = xmldihede - dihede print red('off by %.4e (%f%%)' % (dif,100*dif/(dihede or xmldihede))) print 'Nonbonded...', if abs(xmlnonbe - nonbe) < CUTOFFNB: print green('OK') else: dif = xmlnonbe - nonbe print red('off by %.4e (%f%%)' % (dif,100*dif/(nonbe or xmlnonbe)))
for match in call[crawler].crawl_full(): with matchfile(match) as fw: fw.write(str(match) + '\n') if 0 < match.tostart < 3600 and not tbd in match.teams: pool[match.webpage] = match except KeyboardInterrupt: sys.exit(0) except Exception as e: cooldown[crawler] = time.time() + cd print('{0} cooldowned for {1}s for exception'.format(crawler, cd)) print(e) cd += 0 with open('httpalias', 'a') as fw: G = networkx.Graph() print('{0} matches start <1h'.format(len(G))) for s1, s2 in itertools.combinations(pool.values(), 2): if domain(s1) == domain(s2) or not s1 == s2: continue G.add_edge(s1.webpage, s2.webpage) for idx, c in enumerate(networkx.connected_components(G)): fw.write(' '.join(c) + '\n') print('###MATCHED### {0}: '.format(idx) + ' '.join(c)) profit = (max([pool[w].returns[0] for w in c]) - 1) * (max([pool[w].returns[1] for w in c]) - 1) - 1 if profit < profit_trs: continue print(red(' PROFIT {0:.3}'.format(profit))) for w in c: s = pool[w] print(red(' {0} {1} ({2}): {3}/{4} {5} {6}'.format(s.teams[0], s.teams[1], s.series, s.returns, s.poolsize, domain(s), s.tostart))) time.sleep(5)