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
def transform_positive(self): self.log.info("Start transforming %s from base %s to base %s" % (self.number, self._from, self.to)) self.number = self.number.split('.') decimal = self.number[0] floating = self.number[1] if len(self.number) > 1 else '' if self._from != 10: self.log.warn(yellow("Initial base is not decimal!")) now = time.time() self.number = str(self.to_decimal(decimal, self._from)) if floating: self.number += ".%s" % self.to_decimal(floating, self._from) self.log.debug(blue("Transforming the number from base %s into decimal" " took %f seconds" % (self._from, round(time.time() - now, 10)))) self.log.info("Decimal representation of the number is %s" % self.number) if self.to != 10: now = time.time() self.number = str(self.to_base(decimal, self.to)) if floating: self.number += ".%s" % self._get_floating(floating, self.to) self.log.debug(blue("Transforming the number from decimal into base %s" " took %f seconds" % (self.to, round(time.time() - now, 10)))) return self.number
def INIT(): '''Init repo `fabsetup_custom` with custom tasks and config.''' # decorator @needs_repo_fabsetup_custom makes the job print(green('Initialization finished\n')) print('List available tasks: ' + blue('fab -l')) print(' '.join([ 'Show details of a task: `fab -d <task>`, eg.:', blue('fab -d setup_webserver'),]))
def job_list(self): print(blue('Fetching job list for %s...' % self.url)) table_data = [['Name', 'Status', 'Url']] for job_name, job_instance in self.server.get_jobs(): table_data.append([ job_instance.name, green('RUNNING') if job_instance.is_running() else blue('STOPPED'), job_instance.url ]) format_data(table_data) print("Jobs found: ", len(self.server.get_jobs_list()))
def transform_negative(self): self.log.info("Start transforming %s from base %s to base %s" % (self.number, self._from, self.to)) now = time.time() number = self.number[1:] # TODO: don't use builtin functions complement = str(bin(~int(number)))[3:] self.log.debug(blue("2's complement of %s is %s" % (self.number, complement))) new_number = self.to_base(self.to_decimal(complement, 2), self.to) self.log.debug(blue("Transforming the number from decimal into base %s" " took %f seconds" % (self.to, round(time.time() - now, 10)))) return new_number
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 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 checkup_git_repo_legacy(url, name=None, base_dir='~/repos', verbose=False, prefix='', postfix=''): '''Checkout or update a git repo.''' if not name: match = re.match(r'.*/(.+)\.git', url) assert match, flo("Unable to extract repo name from '{url}'") name = match.group(1) assert name is not None, flo('Cannot extract repo name from repo: {url}') assert name != '', flo('Cannot extract repo name from repo: {url} (empty)') if verbose: name_blue = blue(name) print_msg(flo('{prefix}Checkout or update {name_blue}{postfix}')) if not exists(base_dir): run(flo('mkdir -p {base_dir}')) if not exists(flo('{base_dir}/{name}/.git')): run(flo(' && '.join(['cd {base_dir}', 'git clone {url} {name}'])), msg='clone repo') else: if verbose: print_msg('update: pull from origin') run(flo('cd {base_dir}/{name} && git pull')) return name
def checkup_git_repo(url, name=None, base_dir=package.downloads_basedir, verbose=True, prefix='', postfix=''): '''Checkout or update a git repo.''' if not name: # eg. url = 'https://github.com/my-repo-name.git' # => name = 'my-repo-name' match = re.match(r'.*/(.+)\.git', url) assert match, flo("Unable to extract repo name from '{url}'") name = match.group(1) assert name is not None, flo( 'Cannot extract repo name from repo: {url}') assert name != '', flo( 'Cannot extract repo name from repo: {url} (empty)') if verbose: name_blue = blue(name) print_msg(flo('{prefix}Checkout or update {name_blue}{postfix}')) filename_readme = '~/.fabsetup-downloads/README.md' if not exists(filename_readme): install_file_legacy(filename_readme) if not exists(base_dir): run(flo('mkdir -p {base_dir}')) if not exists(flo('{base_dir}/{name}/.git')): run(flo(' && '.join( ['cd {base_dir}', 'git clone {url} {name}'])), msg='clone repo') else: if verbose: print_msg('update: pull from origin') run(flo('cd {base_dir}/{name} && git pull')) return flo('{base_dir}/{name}')
def do_list(self, *args): """List all available sessions.""" # List with all active hosts. hosts = [] # Length of the bigggest text string[card]. biggest = None for index, connection in enumerate(self.__sessions): # Append host card to list. hosts.append('{} - {} - {}'.format(index, *connection[1:])) if index == 0: # The first bigger card. biggest = len(hosts[0]) else: if len(hosts[index]) > biggest: # Set the new biggest card. biggest = len(hosts[index]) else: if self.server_started: # Any socket has connected the server. hosts.append('Any available connections!') else: # The server has not been started. hosts.append('Server still not started. Use "listen" to start a remote server.') biggest = len(hosts[0]) # Print the top stick. print(utils.yellow('{}'.format('-' * (biggest + 4)))) # Print each host. for host in hosts: print(utils.blue('| {} |'.format(host + ' ' * (biggest - len(host))))) # Print the bottom stick. print(utils.yellow('{}'.format('-' * (biggest + 4))))
def checkup_git_repo(url, name=None, base_dir=package.downloads_basedir, verbose=True, prefix='', postfix=''): '''Checkout or update a git repo.''' if not name: # eg. url = 'https://github.com/my-repo-name.git' # => name = 'my-repo-name' match = re.match(r'.*/(.+)\.git', url) assert match, flo("Unable to extract repo name from '{url}'") name = match.group(1) assert name is not None, flo( 'Cannot extract repo name from repo: {url}') assert name != '', flo( 'Cannot extract repo name from repo: {url} (empty)') if verbose: name_blue = blue(name) print_msg(flo('{prefix}Checkout or update {name_blue}{postfix}')) filename_readme = '~/.fabsetup-downloads/README.md' if not exists(filename_readme): install_file_legacy(filename_readme) if not exists(base_dir): run(flo('mkdir -p {base_dir}')) if not exists(flo('{base_dir}/{name}/.git')): run(flo(' && '.join([ 'cd {base_dir}', 'git clone {url} {name}'])), msg='clone repo') else: if verbose: print_msg('update: pull from origin') run(flo('cd {base_dir}/{name} && git pull')) return flo('{base_dir}/{name}')
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 save_figure_and_close(files, param_string, filename): path = files['results_dir'] + param_string if not os.path.exists(path): os.makedirs(path) filepath = path + '/' + param_string + '_' + filename plt.savefig(filepath, bbox_inches='tight', facecolor='white') print('Write figure to [%s]' % utils.blue(filepath)) plt.close()
def job_details(self, job_name): print( blue('Getting details for job %s on server %s ...' % (job_name, self.url))) table_data = [['Name', 'Status', 'Url']] try: job = self.server.get_job(job_name) table_data.append([job.name, self.job_status(job), job.url]) format_data(table_data) except custom_exceptions.UnknownJob: print('No job found : %s' % job_name)
def write_models_to_html(image_metrics, hp, files): image_metrics.sort_values(['ImageID', 'cH'], ascending=[True, False], inplace=True) add_eval_set = True param_string_eval_set = utils.create_param_string(hp, add_eval_set) out_filename = ( '%s/%s/%s.html' % (files['results_dir'], param_string_eval_set, param_string_eval_set)) # Map metric to its display name. metrics, metrics_display = [], dict() metrics_style, fs, line_width, marker_size = get_models_style() for k in metrics_style.keys(): metrics_display[metrics_style[k]['name']] = metrics_style[k]['legend'] metrics.append(metrics_style[k]['name']) metrics_display['y_true'] = 'GT' # Read URLs id_url = utils.image_to_url(files['images_path']) images = list(set(image_metrics.ImageID.values.tolist())) f = open(out_filename, 'w') f.write('<html><body><table border ="2">') for c, im in enumerate(images): url = id_url[im] f.write('<tr><td>' + str(c) + '</td>') f.write('<td><img src ="' + str(url) + '" width=200></td><td>') im_df = image_metrics.loc[(image_metrics.ImageID == im), :] f.write( im_df.to_html(border=4, index=False, justify='center', col_space=60).encode('utf-8', errors='ignore')) f.write('</td><td>') for metric in metrics: metric_name = metrics_display[metric] # Return the label that was ranked first by the metric. l_first = im_df.sort_values(by=metric, ascending=False)['DisplayName'].iat[0] l_true = im_df.sort_values(by='y_true', ascending=False)['DisplayName'].iat[0] f.write('<b>' + metric_name + '</b> : ' + l_first) # Print a red cross if conf. H is correct. if metric == 'cH' and l_first == l_true: f.write('<font color="red"><b> + </b></font>') f.write('<br>') f.write('</td>') f.write('</td></tr>') f.write('</table></body></html>') f.close() print('Finished writing html to [%s]' % utils.blue(out_filename))
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 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 to_decimal(self, number, _from, floating=False): if not floating: number = number[::-1] new_number = 0 digits = self.digits(_from) for index in xrange(len(number)): value = digits.index(number[index]) if not floating: digit = int(value) * (_from ** index) self.log.debug(blue("%s * (%s ** %s) = %s + %s = %s" % (value, _from, index, digit, new_number, new_number + digit))) else: digit = float(value) / (_from ** (index + 1)) self.log.debug(blue("%s / (%s ** %s) = %s + %s = %s" % (value, _from, index, digit, new_number, new_number + digit))) new_number += digit return new_number
def to_base(self, number, to): number = int(number) new_number = '' digits = self.digits(to) while number >= to: self.log.debug(blue("%s / %s = %s %s" % (number, to, number / to, number % to))) new_number += digits[number % to] number /= to if number != 0: new_number += str(number) return new_number[::-1]
def job_list_active(self): print(blue('Fetching job list for %s...' % self.url)) table_data = [['Name', 'Status', 'Url']] count = 0 for job_name, job_instance in self.server.get_jobs(): if not job_instance.is_enabled(): continue count += 1 if count >= 10: break table_data.append([ job_instance.name, self.job_status(job_instance), job_instance.url ]) format_data(table_data) print("Jobs found: ", len(self.server.get_jobs_list()))
def to_decimal(self, number, _from): number = number[::-1] new_number = 0 digits = self.digits(_from) for index in xrange(len(number)): value = digits.index(number[index]) digit = int(value) * (_from ** index) self.log.debug(blue("%s * (%s ** %s) = %s + %s = %s" % (value, _from, index, digit, new_number, new_number + digit))) new_number += digit return new_number
def checkup_git_repo_legacy(url, name=None, base_dir='~/repos', verbose=False, prefix='', postfix=''): '''Checkout or update a git repo.''' if not name: match = re.match(r'.*/(.+)\.git', url) assert match, flo("Unable to extract repo name from '{url}'") name = match.group(1) assert name is not None, flo('Cannot extract repo name from repo: {url}') assert name != '', flo('Cannot extract repo name from repo: {url} (empty)') if verbose: name_blue = blue(name) print_msg(flo('{prefix}Checkout or update {name_blue}{postfix}')) if not exists(base_dir): run(flo('mkdir -p {base_dir}')) if not exists(flo('{base_dir}/{name}/.git')): run(flo(' && '.join([ 'cd {base_dir}', 'git clone {url} {name}'])), msg='clone repo') else: if verbose: print_msg('update: pull from origin') run(flo('cd {base_dir}/{name} && git pull')) return name
def __str__(self): name = self.basename.decode("utf-8", errors="ignore") return utils.blue("%s/") % name if self.is_directory() else name
val_loss, val_count = 0, 0 net.eval() for batch in val_loader: imgs = batch['image'] true_masks = batch['mask'] imgs = imgs.to(device=device, dtype=torch.float32) true_masks = true_masks.to(device=device, dtype=torch.long) pred_masks = net(imgs) loss = criterion(pred_masks, true_masks.squeeze(1)) val_loss += loss.item() val_count += 1 writer.add_scalar('Loss/validate', val_loss / val_count, epoch + 1) # writer.add_scalar('Acc/test', acc, validate_step) logging.info( blue( f'[Epoch {epoch + 1}/{opt.nepoch}] Valid loss:{(val_loss/val_count):f}' )) scheduler.step(val_loss / val_count) writer.add_scalar('learning_rate', optimizer.param_groups[0]['lr'], epoch + 1) # writer.add_images('images', imgs, epoch+1) if opt.save_cp: try: os.mkdir(opt.dir_checkpoint) logging.info('Created checkpoint directory') except OSError: pass torch.save(net.state_dict(), opt.dir_checkpoint + f'CP_epoch{epoch + 1}.pth') # logging.info(f'Checkpoint {epoch + 1} saved !')
def __repr__(self): return '\t%s : %s %s' % (blue(self.name.ljust(15)), cyan(self.ftype.ljust(30)), decamel(self.name))
def __repr__(self): return '\t%s : %s %s' % (blue( self.name.ljust(15)), cyan(self.ftype.ljust(30)), decamel( self.name))
destination -= 1 if destination < mini: destination = maxi destination_clockwise = cups[destination] cups[destination] = picked[0] cups[picked[2]] = destination_clockwise current = cups[current] return cups example_chained_list = build_chained_list("389125467", 9) new_chained_list = crab_moves_cups_but_faster(example_chained_list, 3, 10) u.assert_equals(cups_to_str(new_chained_list, 8), "837419265") u.blue("now try with bigger lists...") example_BIG_list = build_chained_list("389125467", 1000000) BIG_list_after_10M_moves = crab_moves_cups_but_faster(example_BIG_list, 3, 10000000) u.assert_equals(BIG_list_after_10M_moves[1], 934001) u.assert_equals(BIG_list_after_10M_moves[934001], 159792) my_BIG_list = build_chained_list(my_labeling, 1000000) my_list_after_10M_moves = crab_moves_cups_but_faster( my_BIG_list, int(my_labeling[0]), 10000000 ) clockwise_1st_position = my_list_after_10M_moves[1] clockwise_2nd_position = my_list_after_10M_moves[clockwise_1st_position] u.answer_part_2(clockwise_1st_position * clockwise_2nd_position)
def main(): if main_repo_url is None or sub_repos is None: config_error() old_wd = os.getcwd() print utils.blue('Initialising merging dir '+merger_dir) utils.remove_dir(merger_dir) utils.ensure_dir(merger_dir) os.chdir(merger_dir) main_repo_name = utils.get_repo_name(main_repo_url) main_repo_dir = os.path.join(merger_dir, main_repo_name) print utils.blue('\nCloning main repo ' + main_repo_name) call(['git', 'clone', main_repo_url]) print '' for sub_repo in sub_repos: sub_repo_name = utils.get_repo_name(sub_repo['url']) sub_repo_dir = os.path.join(merger_dir, sub_repo_name) print utils.blue('Merging sub-repo ' + sub_repo_name + ' into main repo ' + main_repo_name) print utils.blue('>Cloning sub-repo ' + sub_repo_name) call(['git', 'clone', sub_repo['url']]) os.chdir(sub_repo_dir) print utils.blue('>Looking for files to delete') if files_to_delete is not None and files_to_delete: files = os.listdir(sub_repo_dir) for f in files: for ftd in files_to_delete: if (ftd.startswith('*') and f.endswith(ftd.split('*')[1])) or (not ftd.startswith('*') and f == ftd): file_path = os.path.join(sub_repo_dir, f) utils.remove_file(file_path) print utils.blue('>>File ' + file_path + ' deleted') files = os.listdir(sub_repo_dir) files.remove('.git') destination_dir = os.path.join(sub_repo_dir, sub_repo['dir']) print utils.blue('>Directory ' + destination_dir + ' created') utils.ensure_dir(destination_dir) for f in files: call(['git', 'mv', f, sub_repo['dir']]) print utils.blue('>>File/dir ' + f + ' moved into ' + sub_repo['dir']) call(['git', 'add', '-A']) call(['git', 'commit', '-m', 'Merging '+sub_repo_name+' into '+main_repo_name]) print utils.blue('>Changes committed in sub-repo') os.chdir(main_repo_dir) print utils.blue('>Adding remote '+sub_repo_name) call(['git', 'remote', 'add', sub_repo_name, sub_repo_dir]) print utils.blue('>Fetching remote '+sub_repo_name) call(['git', 'fetch', sub_repo_name]) print utils.blue('>Merging '+sub_repo_name+' into '+main_repo_name) call(['git', 'merge', '--allow-unrelated-histories', '--no-edit', sub_repo_name+'/master']) print utils.blue('>Removing remote '+sub_repo_name) call(['git', 'remote', 'remove', sub_repo_name]) print utils.blue('>Sub-repo '+sub_repo_name+' merged into main repo '+main_repo_name) os.chdir(merger_dir) print '' os.chdir(main_repo_dir) push = None while push is None: push = utils.yes_no_input('Do you want to push the merged main repo '+main_repo_name+' to remote', 'y') if push: call(['git', 'push']) remote_delete = None while remote_delete is None: remote_delete = utils.yes_no_input('Do you want to delete the remote sub-repos', 'n') if remote_delete: for sub_repo in sub_repos: sub_repo_name = utils.get_repo_name(sub_repo['url']) print '\tTODO: delete here remote repo '+sub_repo_name+'. Not yet implemented, does nothing for now.' # TODO: see https://developer.github.com/v3/repos/#delete-a-repository clean = None while clean is None: clean = utils.yes_no_input('Do you want to delete the merging dir '+merger_dir+' and all its content', 'y') if clean: utils.remove_dir(merger_dir) os.chdir(old_wd)