def t_STRING_LIT(t): r"\"[^\"]*\"" # if r"\s*\*/": # print_error("ERROR: Wrong Multiline Comment") # return if "\n" in t.value: print_lexer_error("string cannot contain line breaks") lineno = t.lexer.lineno pos = find_column(t) splits = list(t.value.split("\n")) for i, line_ in enumerate(splits): print_line(lineno) line_actual = lines[lineno - 1] if i == 0: print_marker(pos - 1, len(line_actual) - pos + 1) elif i == len(splits) - 1: print_marker(0, line_actual.find('"') + 1) else: print_marker(0, len(line_actual)) lineno += 1 t.lexer.lineno += t.value.count("\n") return t.value = ("string", t.value) t.lexer.begin("InsertSemi") return t
def add_type(self, name: str, lineno, col_num, storage, eltype=None, check=True): """Add a new type definition with the details""" if check and self.is_defined(name): print_error() print(f"Re-declaration of type '{name}' at line {lineno}") print_line(lineno) pos = col_num - 1 width = len(name) print_marker(pos, width) other_type = self.get_type(name) print(f"{name} previously declared at line {other_type.lineno}") print_line(other_type.lineno) pos = other_type.col_num - 1 print_marker(pos, width) new_type = TypeInfo(name, lineno, col_num, storage, eltype) self.type_map[name] = new_type
def check_unused(self): func_type = self.type_table.get_type("FUNCTION") for symbol in self.symbols: if (symbol.uses == [] and symbol.scope_id != "1" and symbol.type_ != func_type): print_error("Unused variable", kind="ERROR") print( f"Variable {symbol.name} is defined at line {symbol.lineno} " "but never used.") print_line(symbol.lineno) pos = symbol.col_num - 1 width = len(symbol.name) print_marker(pos, width) if utils.package_name == "main": main_fn = self.get_symbol("main") if main_fn is None: print_error("main is undeclared in package main", kind="ERROR") print( "main function is not declared in a file with 'main' package" )
def init_optimizer(self): beta1, beta2 = 0.5, 0.99 self.vae_optimizer = optim.Adam(itertools.chain( self.E.parameters(), self.G.parameters()), lr=self.lr, betas=(beta1, beta2)) utils.print_line() logging.debug('Use ADAM optimizers for E and G.')
def p_error(p: lex.LexToken): print(f"{Fore.RED}SYNTAX ERROR:{Style.RESET_ALL}") if p is not None: col = find_column(p) print(f"at line {p.lineno}, column {col}") print_line(p.lineno) # print(" " * 10, " \t", " " * (col - 1), "^", sep="") print_marker(col - 1, len(p.value)) else: print("Unexpected end of file")
def usage(): util.print_empty_line() print util.prog_name + ' ' + '<options>' util.print_line() print 'options:' print '\t-h,--help\t\t - print help' print '\t-v,--verbose\t\t - print verbose logging' print '\t-m \t\t - monitor /proc/meminfo' print '\t-i \t\t - monitor /d/kernel/ion' print '\t--version\t\t - print version' util.print_empty_line()
def load_dataset(dataset, batch_size=64, transform='tanh'): """ Load dataset loader >>> trainloader = load_dataset("MNIST", 24) >>> trainloader.batch_size 24 >>> l = load_dataset('mnist', 5, 'tanh'); l.dataset Dataset MNIST Number of datapoints: 60000 Split: train Root Location: ./data/mnist Transforms (if any): Compose( ToTensor() Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) ) Target Transforms (if any): None """ dataset = dataset.lower() dataset_mapping = { 'small-mnist': load_dataset_MNIST, 'mnist': load_dataset_MNIST, 'fashion_mnist': load_dataset_FashionMNIST, 'cifar10': load_dataset_CIFAR10, } assert dataset in dataset_mapping.keys() if dataset == 'small': trainloader, _ = load_dataset_Small(batch_size, for_tanh=(transform == 'tanh')) elif dataset == 'cifar10': trainloader, _ = load_dataset_CIFAR10(batch_size, transform=transform_mapping( transform, False, 64)) else: trainloader, _ = dataset_mapping[dataset](batch_size, transform=transform_mapping( transform, False)) utils.print_line() dict_transform_str = { 'tanh': 'in [-1, 1].', '01': 'in [0,1].', 'pretrained_model': 'for pretrained models.', 'inception': 'for inception.' } logging.info(dataset.upper() + ' Data loaded in normalized range ' + dict_transform_str[transform]) return trainloader
def process_message_3(payload, server_kas, server_Ns, connection): utils.print_trancsation_message("Server processing message 3") utils.print_dict("Message 3 request: ", payload) ser_final_nonce = encrypt.decrypt_nonce( server_kas, payload['client_encrypted_final_nonce']) ser_dec_ns = encrypt.get_ns_from_final_nonce(ser_final_nonce) ser_side_na = encrypt.get_na_from_final_nonce(ser_final_nonce) print "payload['client_encrypted_final_nonce']: ", payload[ 'client_encrypted_final_nonce'] print "ser_final_nonce: ", ser_final_nonce print "ser_dec_ns : ", ser_dec_ns print "server_Ns : ", server_Ns print "ser_side_na: ", ser_side_na try: i_ser_dec_ns = int(ser_dec_ns) except: print "sfasf" message_4_payload['success'] = False message_4_payload['server_encrypted_Na'] = "naren" connection.sendall(str(message_4_payload)) return if (server_Ns == i_ser_dec_ns): print "Client successfully authenticated to server" utils.print_line() authorized_users[payload['username']] = True message_4_payload['success'] = True else: print "Hack Alert" message_4_payload['success'] = False message_4_payload['server_encrypted_Na'] = "naren" connection.sendall(str(message_4_payload)) return # Server sending message 4 utils.print_trancsation_message("Server sending message 4") server_enc_na = encrypt.encrypt_nonce(server_kas, ser_side_na) print "server_enc_na: ", server_enc_na message_4_payload['server_encrypted_Na'] = server_enc_na connection.sendall(str(message_4_payload)) utils.print_dict("message_4_payload", message_4_payload) # Server sending message 5 utils.print_trancsation_message("Server sending message 4")
def run(command): stdout = '' try: process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Poll process for new output until finished while True: nextline = process.stdout.readline().decode('utf-8') # store output to log file if nextline == '' and process.poll() is not None: break print(nextline, end='') stdout += nextline sys.stdout.flush() exitCode = process.returncode if (exitCode == 0): return stdout else: utils.print_line() print('Something went wrong with the command below: ') print(command) utils.print_line() return None except: utils.print_line() print('Something went wrong with the command below: ') print(command) utils.print_line() return None
def init_optimizer(self): """ initialize optimizer """ beta1, beta2 = 0.5, 0.99 self.G_optimizer = optim.Adam(self.G.parameters(), lr=self.lr, betas=(beta1, beta2), weight_decay=0) self.D_optimizer = optim.Adam(self.D.parameters(), lr=self.lr, betas=(beta1, beta2), weight_decay=0) self.info_optimizer = optim.Adam(itertools.chain( self.G.parameters(), self.D.parameters()), lr=self.lr, betas=(beta1, beta2)) utils.print_line() logging.debug('Use ADAM optimizers for D and G.')
def p_OperandName(p): """OperandName : IDENTIFIER %prec '=' | QualifiedIdent """ if not isinstance(p[1], syntree.QualifiedIdent): ident: Tuple = p[1] sym = symtab.get_symbol(ident[1]) lineno = p.lineno(1) if not symtab.is_declared(ident[1]): print_error() print(f"Undeclared symbol '{ident[1]}' at line {lineno}") print_line(lineno) line: str = utils.lines[lineno - 1] # TODO: get correct position of token rather than searching pos = ident[2] - 1 width = len(ident[1]) print_marker(pos, width) else: sym.uses.append(lineno) p[0] = p[1]
def declare_new_variable( self, symbol: str, lineno: int, col_num: int, type_=None, const=False, value=None, ): """Helper function to add symbol to the Symbol Table with declaration set to given line number. Prints an error if the symbol is already declared at current depth. """ if self.is_declared_in_cur_symtab(symbol): print_error() print(f"Re-declaration of symbol '{symbol}' at line {lineno}") print_line(lineno) pos = col_num - 1 width = len(symbol) print_marker(pos, width) other_sym = self.get_symbol(symbol) print(f"{symbol} previously declared at line {other_sym.lineno}") print_line(other_sym.lineno) pos = other_sym.col_num - 1 print_marker(pos, width) else: self.update_info(symbol, lineno, col_num=col_num, type_=type_, const=const, value=value)
def link_tables(self, table_dir, dest_dir, max_mention_cnt=100, test_cnt=None, verbose=0, alpha=0.5, beta=0.5): if os.path.exists(dest_dir): print('clear', dest_dir) shutil.rmtree(dest_dir) create_dir(dest_dir) table_names = os.listdir(table_dir) if test_cnt is None: test_cnt = len(table_names) cnt = 0 for i, name in enumerate(table_names): if cnt >= test_cnt: break status = self.link_table(os.path.join(table_dir, name), os.path.join(dest_dir, name), verbose=verbose, max_mention_cnt=max_mention_cnt, alpha=alpha, beta=beta) if status: cnt += 1 print_line(s='%d/%d' % (i + 1, len(table_names))) print('****', 'alpha', alpha, 'beta', beta) check_result(dest_dir)
def __init__(self): print_line() print('Bus created')
def __init__(self): print_line() print('Car created')
def train(self, **kwargs): # test covered ''' The main training process of the given model. ''' save_interval = 1 visualize_interval = 1 utils.print_line() logging.debug('Training begins.') total_step = len(self.trainloader) # epoch loop for epoch in tnrange(self.num_epoch, desc='epoch loop'): # for each training step for step, (batch_x, _) in tqdm_notebook(enumerate(self.trainloader, 0), desc=f'{total_step} step loop', leave=False): # preprocess batch_x = batch_x.float() # step train loss_dict = self.model.stepTraining(batch_x) # chk nan utils.check_nan(loss_dict) # write loss in tbx if ((step + 1) % 50) == 1: epoch_step = epoch * total_step + step for key, val in loss_dict.items(): self.tbx_writer.add_scalar(key, val, epoch_step) # and in record dict self.record_values(loss_dict) # print loss values if ((step + 1) % 100) == 1: logging.debug( f"Epoch: [{epoch+1}] Step: [{step+1}/{total_step}]") logging.debug(str(loss_dict)) # end step loop # print epoch and get images in the TBX writer self.tbx_writer.add_text('Epoch', str(epoch), epoch) self.tbx_writer.add_image( 'Generated_images_random', self._make_grid(self.model.sampleN(self.num_visual_samples)), epoch) self.tbx_writer.add_image( 'Generated_images_fixed', self._make_grid(self.model.sample_fixed()), epoch) self.tbx_writer.add_image('Real_images_for_comparison', self._make_grid(batch_x), epoch) # if this is the AE model, show the reconstructed images if hasattr(self.model, 'E'): self.tbx_writer.add_image( 'Reconstructed_images', self._make_grid(self.model.reconstruct(batch_x)), epoch) self.tbx_writer.add_image( 'images_residual', (self._make_grid(batch_x) - self._make_grid(self.model.reconstruct(batch_x))), epoch) # save the net model if save_interval > 0 and (epoch + 1) % save_interval == 0: self.save() # visualize with matplotlib if visualize_interval > 0 and (epoch + 1) % visualize_interval == 0: self.visualize_results(epoch + 1) # end epoch loop # savings self.save() logging.info("Models saved. Training finished!") self.tbx_writer.add_text('Epoch', "Training finished!", self.num_epoch + 1) # gc torch.cuda.empty_cache() gc.collect() return self
def test(): secret_key = 223 generator = 3 prime_num = 197221152031991558322935568090317202983 password = "******" random_number = 197221152031991558322935568090317202983 dh_key = generate_dh_key(secret_key, generator, prime_num) pwd_digest = generate_SHA1(password) eke = get_eke(pwd_digest, secret_key, generator, prime_num) print "secret_key: ", secret_key print "eke: ", eke print "pwd_digest: ", pwd_digest print "decrypt: ", decrypt_eke(eke, pwd_digest) print "dh_key: ", dh_key kas = generate_kas(dh_key, secret_key, prime_num) print "kas: ", kas print "enc kas: ", encrypt_kas(kas, random_number) kas = 25494605730065883870637428293146839535 client_dh_key = dh_key ######################## # server encrypting nonce and sending to client """message 2""" utils.print_line() server_kas = kas server_Xs = 23 server_eke = get_eke(pwd_digest, server_Xs, generator, prime_num) server_Ns = get_random_number() server_enc_nonce = encrypt_nonce(server_kas, server_Ns) print "server_eke: ", server_eke print "server_Ns: ", server_Ns print "server_enc_nonce: ", server_enc_nonce # Client decryptin server nonce client_Ns = get_random_number() client_kas = kas c_server_nonce = int(decrypt_nonce(client_kas, server_enc_nonce)) print "client side c_server_nonce: ", c_server_nonce # Client encrypting server and client nonce, creating final nonce final_nonce_c = concat(c_server_nonce, client_Ns) final_nonce_x = xor(c_server_nonce, client_Ns) # client encypting final nonce with kas enc_final_nonce_c = encrypt_nonce(client_kas, final_nonce_c) enc_final_nonce_x = encrypt_nonce(client_kas, final_nonce_x) print "final_nonce_c: ", final_nonce_c print "final_nonce_x: ", final_nonce_x print "enc_final_nonce_c: ", enc_final_nonce_c print "enc_final_nonce_x: ", enc_final_nonce_x # Server side decryptin mesage 3 ser_final_nonce = decrypt_nonce(server_kas, enc_final_nonce_c) ser_dec_ns = get_ns_from_final_nonce(ser_final_nonce) ser_side_na = get_na_from_final_nonce(ser_final_nonce) print "ser_dec_ns : ", ser_dec_ns print "server_Ns : ", server_Ns print "ser_side_na: ", ser_side_na # Server sending message 4 server_enc_na = encrypt_nonce(server_kas, ser_side_na) print "server_enc_na: ", server_enc_na utils.print_line() ######################## print "\n# Sever sending enc server nonce to client" server_nonce = get_random_number() enc = aes_cbc_encrypt(kas, server_nonce) print server_nonce, "->", enc print "\n# Client decrypting the enc_nonce sent from server" client_dec_server_nonce = aes_cbc_decrypt(kas, enc) print "client_dec_server_nonce", client_dec_server_nonce print "\n# Client generating client nonce" client_nonce = get_random_number() enc = aes_cbc_encrypt(kas, client_nonce) print client_nonce, "->", enc print "\n# Client encrypting c_nonce and s_nonce" enc_c_s_nonce = encrypt_client_and_server_nonce( int(client_dec_server_nonce), client_nonce) encrypted_nonce_wid_kas = encrypt_nonce(kas, enc_c_s_nonce) print "encrypted_nonce_wid_kas", encrypted_nonce_wid_kas print "\n# Send encrypted_nonce_wid_kas and user name to server" dec = aes_cbc_decrypt(kas, enc) print dec
def main(): ''' The main application. Purpose is to cycle around the functions used to interract with the API in a forever-loop. Exits loop and terminates application, when variables href, method and schema are set as None. No return value. Exceptions. All API calls are done over a requests session. If any exceptions specific to the session are caught, then the application restarts from entry point. Any API specific exceptions are caught within the method handling the app continues from a state before the API call, caused the exception. ConnectionError In case of a connection error of the TCP/IP stack. requests.Timeout If a timeout occured with the HTTP request. requests.TooManyRedirects In case the request experiences too many redirects. APIError In case the API replies with any != 2xx status code ''' breakout = False while True and not breakout: with requests.Session() as s: SERVER_URL, href, method = api_entry(s) SERVER_URL = SERVER_URL.strip("/api/") # print("DEBUG MAIN:\t\tFull URL: ", SERVER_URL + href, "\r\n") try: body = None while True: if body is not None: # Print UI print_line() print("\r\nCurrent route: {}".format(href)) href, method, schema = process_body(body) # print("DEBUG MAIN:\t\thref after process is: ", href) # print("DEBUG MAIN:\t\tmethod after" # "process is: ", method) if method == "get": # print("DEBUG MAIN:\t\thref for GET is: ", href) # print("DEBUG MAIN:\t\tGETting: ", SERVER_URL + href) try: # Resfresh UI get_body = get_resource(s, SERVER_URL + href) except APIError as err: print("\n", err) input("Press Enter to continue...") except JSONDecodeError: print("Server response was not a " "valid JSON document.") input("Press Enter to continue...") else: body = get_body elif method == "post": # print("DEBUG MAIN:\t\thref for POST is: ", href) # print("DEBUG MAIN:\t\tPOSTing: ", SERVER_URL + href) try: # Post new resource resp = post_resource(s, SERVER_URL + href, schema) if resp.status_code == 201: print( "\r\nResource created: ", resp.headers["Location"].strip( SERVER_URL) # noqa: E501 ) # Ask user to get data on newly created # resource, or stay at current resource-state while True: str_in = input("\r\nExamine newly created " "resource? (y/n): ") \ .strip().lower() if str_in == "y": # Get newly created resource data # and return body body = get_resource( s, resp.headers["Location"] ) # noqa: E501 break elif str_in == "n": # Resfresh UI body = get_resource( s, SERVER_URL + href) # noqa: E501 break else: print("Select \"y\" for yes or " "\"n\n for no, please.") else: raise APIError(resp.status_code, resp.content) except APIError as err: print("\n", err) input("Press Enter to continue...") elif method == "put": # print("DEBUG MAIN:\t\thref for PUT is: ", href) # print("DEBUG MAIN:\t\tPUTing: ", SERVER_URL + href) try: # Make changes resp = put_resource(s, SERVER_URL + href, schema) if resp.status_code == 204: print("\r\nResource modified: ", href) # Resfresh UI print("Refreshing UI...") body = get_resource(s, SERVER_URL + href) else: raise APIError(resp.status_code, resp.content) except APIError as err: print("\n", err) input("Press Enter to continue...") elif method == "delete": # print("DEBUG MAIN:\t\thref for DELETE is: ", href) # print("DEBUG MAIN:\t\tDELETing: ", SERVER_URL + href) try: # Delete resource resp = delete_resource(s, SERVER_URL + href) if resp.status_code == 204: print("\r\nResource deleted: ", href) # If successfull, go back one level in href href = extract_prev_href(href) print("Falling back to parent resource...") body = get_resource(s, SERVER_URL + href) else: raise APIError(resp.status_code, resp.content) except APIError as err: print("\n", err) input("Press Enter to continue...") # Terminates program elif href is None and method is None and schema is None: breakout = True break except ConnectionError: print( "Get request to {} experienced a connection error.".format( SERVER_URL + href)) input("Press Enter to continue...") except requests.Timeout: print("Get request to {} timed out.".format(SERVER_URL + href)) input("Press Enter to continue...") except requests.TooManyRedirects: print( "Get request to {} experienced too many redirects.".format( SERVER_URL + href)) input("Press Enter to continue...")
def __init__(self, pool_size): print_line() self._socket_pool = [CustomSocket() for _ in range(pool_size)] print_line()
def update_info(self, symbol: str, lineno, col_num=None, type_=None, const=None, value=None): sym = self.get_symbol(symbol) sym.lineno = lineno sym.type_ = None sym.col_num = col_num # TODO: infer type from value if not given typename = None composite_type = False eltype = None if type_ is not None: # sym.storage = self.storage[type_.data] valid_type = True typename = "" # type_ can sometimes be syntree.Type if hasattr(type_, "data") and hasattr(type_, "name"): if type_.name == "BasicType": typename = type_.data elif type_.name == "ARRAY" or type_.name == "SLICE": typename = type_.typename composite_type = True eltype = type_.eltype else: print(f"Unknown node {type_}. Could not determine type") valid_type = False elif isinstance(type_, str): typename = type_ else: print( f"Could not determine type, issue in code. Found {type_}") valid_type = False if valid_type: if not self.type_table.is_defined(typename): print_error() print(f"Type '{typename}' is not defined at line {lineno}") print_line(lineno) line = utils.lines[lineno] pos = line.find(typename) width = len(typename) print_marker(pos, width) else: sym.type_ = self.type_table.get_type(typename) # elif composite_type: # self.type_table.add_type(f"{typename}_{eltype}") if value is not None: sym.value = value if const is not None: sym.const = const
def __init__(self): print_line() print('Motorcycle created')
PICKY, WARY, RANDOM) from utils import print_line, print_header if __name__ == '__main__': results = [] for x in range(0, 300): b = Board(20) result = b.play(1000) results.append(result) print(Colors.clear) # Limpar terminal # Quantas partidas terminam por time out (1000 rodadas) print_header('🎲 Quantas partidas terminam por time out (1000 rodadas)') timeout_count = [x for x in results if x[1] == 1] print_line('Total', len(timeout_count)) # Quantos turnos em média demora uma partida shifts = [x[2] for x in results] mean = statistics.mean(shifts) # or sum(shifts) / len(shifts) print_header('⏳ Quantos turnos em média demora uma partida') print_line('Média', f'{mean:.2f}') print_line('Máximo', f'{max(shifts):.2f}') # Qual a porcentagem de vitórias por comportamento dos jogadores print_header('➗ Qual a porcentagem de vitórias por comportamento dos jogadores') impulsive_count = len([x for x in results if x[0] == IMPULSIVE]) picky_count = len([x for x in results if x[0] == PICKY]) wary_count = len([x for x in results if x[0] == WARY]) random_count = len([x for x in results if x[0] == RANDOM]) inners_sort = sorted({
def check_socket_availability(self): print_line() print('Sockets availability') for _socket in self._socket_pool: print('Socket {_socket} - Mem. Addr. {addr}'.format(_socket=_socket, addr=id(_socket))) print_line()