def _convert_output_ports(self, ports): if isinstance(ports, list) or isinstance(ports, tuple): return [self._convert_sw_port(p) for p in ports] elif isinstance(ports, str): return [self._convert_sw_port(ports)] else: error('error port format')
def execute(self, sql): try: self.cursor.execute(sql) except Exception as e: self.close() utils.error("hit error when execute sql:\n{}".format(sql)) raise e
def request(req, session, payload=None): try: if 'GET' in req: return session.get( TARGET, verify=False, allow_redirects=False) elif 'POST' in req: return session.post( TARGET, data=payload, verify=False, allow_redirects=False) except exceptions.HTTPError as eh: print(u.error(), 'HTTPError:', eh) quit() except exceptions.ConnectionError as ec: print(u.error(), 'ConnectionError:', ec) quit() except exceptions.Timeout as et: print(u.warn(), 'Timeout:', et) except exceptions.RequestException as er: print(u.warn(), 'RequestException:', er)
def read_passwords(): try: with open(PASSWORDS, 'r') as passwords: return map(str.strip, passwords.readlines()) except Exception as e: print(u.error(), 'COULD NOT READ:', PASSWORDS) quit()
def _convert_act(self, act): type = act[0] ret = [] if type=='vlan_output': sw_port, isfinal = act[1] if isfinal: ret.append('pop_vlan') output = ','.join(str(p) for p in self._convert_output_ports(sw_port)) ret.append('output:' + output) elif type=='set_vlan': ret.append('push_vlan:0x8100,mod_vlan_vid:%s'%str(act[1])) elif type=='output': output=','.join(str(p) for p in self._convert_output_ports(act[1])) ret.append('output:' + output) else: error('unknown action') return ret
def print_exit(success, attempts, t0): if not success: print(u.error(), "FAILED TO FIND CREDENTIALS") t = time.time() - t0 hours, rem = divmod(t, 3600) minutes, seconds = divmod(rem, 60) print("%s TIME: %dh%dm%ds" % (u.info(), hours, minutes, seconds)) print(u.info(), round(attempts / t, 2), "pw/s")
def select_drop_down_list(self, drop_down_label_name, value): """ select the value for drop down list """ utils.log("select value:[{}] for Drop Down Box:[{}]".format( value, drop_down_label_name)) self.slb.click( report_schedule.drop_down_trigger.format(drop_down_label_name)) li_list = self.slb.get_elements(report_schedule.drop_ul_li) for li in li_list: self.slb.sleep(0.5) if self.slb.get_element_text(li) == value: li.click() self.slb.sleep(0.5) return utils.error( "failed to select! Not found item [{}] in Drop Down List:[{}]. Please check!" .format(value, drop_down_label_name))
def tearDown(self): result = self.defaultTestResult( ) # these 2 methods have no side effects self._feedErrorsToResult(result, self._outcome.errors) error = self.list2reason(result.errors) failure = self.list2reason(result.failures) ok = not error and not failure if not ok: utils.error("run fail or error\n{}".format(error)) utils.log( "******************************************************************" ) utils.log( "**************** TEST END ************" ) utils.log( "******************************************************************\n\n" )
def _convert_act(self, act): type = act[0] ret = [] if type == 'vlan_output': sw_port, isfinal = act[1] if isfinal: ret.append(('pop_vlan', )) output = ','.join( str(p) for p in self._convert_output_ports(sw_port)) ret.append(('output', output)) elif type == 'set_vlan': ret.append(('set_vlan', str(act[1]))) elif type == 'output': output = ','.join( str(p) for p in self._convert_output_ports(act[1])) ret.append(('output', output)) else: error('unknown action') return ret
def __action(self, url, headers, method='Get', **kwargs): if not self.silence: utils.log(".....................................................") utils.log("sending request...") utils.log("request parameters:") utils.log("url: {}".format(url)) utils.log("method: {}".format(method)) utils.log("headers: {}".format(headers)) if kwargs: for k, v in kwargs.items(): utils.log("{}: {}".format(k, v)) else: utils.log("request parameters is none.") try: if method == 'Post': resp = self.session.post(url, headers=headers, timeout=self.timeout, verify=self.verify_cert, **kwargs) elif method == 'Put': resp = self.session.put(url, headers=headers, timeout=self.timeout, verify=self.verify_cert, **kwargs) elif method == 'Delete': resp = self.session.delete(url, headers=headers, timeout=self.timeout, verify=self.verify_cert, **kwargs) else: resp = self.session.get(url, headers=headers, timeout=self.timeout, verify=self.verify_cert, **kwargs) resp.raise_for_status() except Exception as e: utils.error("HTTP请求异常,异常信息:{}".format(e)) return resp
def tearDown(self): result = self.defaultTestResult() # these 2 methods have no side effects self._feedErrorsToResult(result, self._outcome.errors) error = self.list2reason(result.errors) failure = self.list2reason(result.failures) ok = not error and not failure if not ok: self.slb = seleniumLib.SeleniumLib(self.driver) if not os.path.isdir(config.SCREENSHOT_DIR): os.mkdir(config.SCREENSHOT_DIR) file_path = config.SCREENSHOT_DIR + "/{}{}.png".format(self.id(), time.strftime("%H%M%S")) # utils.error("run fail or error, take snapshot at:\n{}".format(file_path)) utils.error("run fail or error, take snapshot at:\n{}\n{}".format(file_path, error)) self.slb.get_screenshot(file_path) bi_logger.info("******************************************************************") bi_logger.info("**************** TEST END ************") bi_logger.info("******************************************************************\n\n")
def parse_command_line_arguments(): argv = sys.argv[1:] try: opts, rest = getopt.getopt(argv, "", ["save=", "tag=", "show="]) except getopt.GetoptError as err: utils.error( "parse_command_line_arguments()", "The following error was thrown whilst parsing command-line arguments" ) utils.fatal("parse_command_line_arguments()", err) if len(rest) is not 1: raise ValueError( f"parse_command_line_arguments(): expected 1 unlabelled argument where {len(argv)} provided" ).with_traceback(sys.exc_info()[2]) cfg_name = rest[0] save_fname, do_show, tag = None, True, None if not utils.is_file(cfg_name): raise RuntimeError( f"parse_command_line_arguments(): config file {cfg_name} not found" ).with_traceback(sys.exc_info()[2]) for option, value in opts: if option in ["--tag"]: tag = str(value) utils.info("parse_command_line_arguments()", f"Labelling temporary files using the tag: {tag}") if option in ["--save"]: save_fname = str(value) utils.info("parse_command_line_arguments()", f"Opening plots file {save_fname}") utils.open_plots_pdf(save_fname) if option in ["--show"]: do_show = utils.string_to_object(value) if type(do_show) != bool: raise ValueError( f"parse_command_line_arguments(): --show value \"{value}\" could not be cast to a bool" ) return cfg_name, do_show, save_fname, tag
def _convert(self, flow, next_table): matches = {} actions = [] for field, value in flow.matches.items(): assert isinstance(field, str) if value == '*': continue if field == 'inport_label' or field=='inport': matches['in_port'] = self._convert_sw_port(value) elif field == 'vlan': matches['dl_vlan'] = value elif field == 'pkt.eth.src': matches['eth_src'] = value elif field == 'pkt.eth.dst': matches['eth_dst'] = value else: reg = self._convert_reg(field) if reg: matches[reg] = value else: error("unknown field", field) isterminal = False for act in flow.actions: if isinstance(act, GlobalAction): if act.type == deployer.dataplane.NON_TERMINAL_ACTION: reg = self._convert_reg(act.key) if not reg: error("unknown field", act.key) actions.append("set_field:%s->%s"%(act.value,reg)) elif act.action == 'toController': actions.append("controller:65535") isterminal = True else: error("unknown action", act, act.action) elif isinstance(act, list): for act2 in act: actions.extend(self._convert_act(act2)) isterminal = True # TODO elif isinstance(act, tuple): actions.extend(self._convert_act(act)) isterminal = True # TODO else: error("unknown action type") if not isterminal: actions.append("goto_table:"+str(next_table)) return flow.priority, matches, actions
def _convert_flow(self, flow): matches = {} actions = [] for field, value in flow.matches.items(): assert isinstance(field, str) if value == '*': continue if field == 'inport_label' or field == 'inport': matches['standard_metadata.ingress_port'] = ( self._convert_sw_port(value), 0x1ff, 9) elif field == 'vlan': matches['hdr.tag.tag'] = (int(value), 0xffff, 16) elif field == 'pkt.eth.src': matches['hdr.ethernet.src'] = parse_mac(value) elif field == 'pkt.eth.dst': matches['hdr.ethernet.dst'] = parse_mac(value) else: reg = self._convert_reg(field) if reg: matches['meta.' + reg] = (int(value), 0x3, 2) else: error('unknown field', field) isterminal = False for act in flow.actions: if isinstance(act, GlobalAction): if act.type == deployer.dataplane.NON_TERMINAL_ACTION: reg = self._convert_reg(act.key) if not reg: error('unknown field', act.key) actions.append(('set_field', act.value, reg)) elif act.action == 'toController': actions.append(('controller', )) isterminal = True else: error('unknown action', act, act.action) elif isinstance(act, list): for act2 in act: actions.extend(self._convert_act(act2)) isterminal = True # TODO elif isinstance(act, tuple): actions.extend(self._convert_act(act)) isterminal = True # TODO else: error('unknown action type') return flow.priority, matches, actions
def run(self): while True: try: data = self.queue.popleft() attempt = request("POST", self.session, data) if attempt.headers['Location'] == SUCCESS: print_success( self.num_of_passwords - len(self.queue), data[USERNAME_FORM], data[PASSWORD_FORM]) os._exit(0) except IndexError: print(u.error(), 'EMPTY DEQUE') return
def error_curve_experiment(): ns = 10**np.arange(1, 6) max_epsilons = [] for n in ns: print(f'n = {n}') h = 1 / (n + 1) x = np.linspace(0, 1, n) b = h**2 * f(x) v = gaussian_elimination_special_case(b) epsilon = error(v[1:-1], u(x[1:-1])) max_error = np.max(epsilon) # print(f'max error = {max_error}') # plt.plot(x, u(x), label='u(x)') # plt.plot(x, v, label='v(x)') # plt.title(f'u and v for n = {n}') # plt.legend() # plt.grid() # plt.show() # # plt.plot(x[1:-1], epsilon, label='error') # plt.title(f'Error(u,v) for n = {n}') # plt.legend() # plt.grid() # plt.show() max_epsilons.append(max_error) plt.figure(5, figsize=(9, 6)) plt.plot(1 / (ns + 1), max_epsilons) plt.title('Relative error by step length `h`.') plt.xscale("log") plt.yscale('log') plt.ylabel('max(epsilon)') plt.xlabel('log(h)') plt.savefig('results/fig-1d.png')
def _parse_action(self, actions): if len(actions) == 1: a = actions[0] if a[0] == 'output': return 'output', {'port': int(a[1])} elif a[0] == 'set_field': return 'set_field' + a[2], {'v': int(a[1])} elif a[0] == 'controller': return 'controller', {} else: error('unhandled actions', actions) elif len(actions) == 2: a1, a2 = actions if a1[0] == 'pop_vlan' and a2[0] == 'output': return 'pop_tag_output', {'port': int(a2[1])} elif a1[0] == 'set_vlan' and a2[0] == 'output': return 'push_tag_output', { 'tag': int(a1[1]), 'port': int(a2[1]) } else: error('unhandled actions', actions) else: error('unhandled actions', actions)
def _convert_sw_port(self, port): try: sw, port = port.split(':') except ValueError: error(port) return int(port)
def run_batch(root, start, end, idx=None): """ Run this script in batch mode. Download reading data whose timestamps lie within start and end dates. The date must follow the following format (Note the T between date and time): YYYY-MM-DDTHH:MM:SS where 24 hour time is used. If idx is a non-negative integer, instead download the meter at that index. idx is zero-indexed. If idx is greater than the number of meters, nothing happens; no files are downloaded. Default behavior is to download data for all meters. Params: root string start string end string idx integer """ s_date = get_date(start) e_date = get_date(end) if not s_date or not e_date: raise ValueError('Invalid/missing dates') elif start > end: raise ValueError('Start date must come before end date') elif not utils.exists_dir(root): raise ValueError('Root directory not found') elif idx is not None and not is_valid_index(idx): raise ValueError('Index must be non-negative integer') creds_file = defaults.creds(root) cnxn_str = utils.get_cnxn_str(creds_file) output_dir = defaults.downloads(root) meter_file = defaults.meter_file(root) utils.print_time('GETTER START') with Cursor.Cursor(cnxn_str) as cursor: dq = get_reading_from_name_query_str() meters = utils.read_meter_file(meter_file) for i, m in enumerate(meters): if idx is not None and idx != i: continue ion_name = utils.get_ion_name(m) qid = utils.get_ion_qid(m) try: cursor.execute(dq, ion_name, qid, str(s_date), str(e_date)) except pyodbc.Error: utils.error( 'Problem with query to get data for meter %s qid %d' % (ion_name, qid)) continue if cursor.rowcount == 0: utils.warn('No data found for meter %s qid %d' % (ion_name, qid)) continue meterId, meterName = utils.get_lucid_id_and_name(m) s_date_str = utils.make_lucid_ts(str(s_date)) e_date_str = utils.make_lucid_ts(str(e_date)) dl_fname = "%sT%sT%s.csv" % (meterId, s_date_str, e_date_str) path = os.path.join(output_dir, dl_fname) print('Writing data for meter %s qid %d to file: %s ...' % (ion_name, qid, path)), with open(path, 'wb') as data_file: writer = csv.writer(data_file) writer.writerow([meterId, meterName]) for row in cursor: ts = row.TimestampUTC val = row.Value data_row = [utils.make_lucid_ts(ts), val] writer.writerow(data_row) print('done') utils.print_time('GETTER END')
new_energy = energi(I, new_omega) energy.append(new_energy) if initial_energy == 0: continue if np.abs(new_energy - initial_energy) > max_energy_difference: print("\n") return euler(X_0, interval, n * 2, L, I, initial_energy) return W, t, energy, [0] if __name__ == "__main__": X_0 = np.identity(3, dtype=np.double) h = get_h() n = 50000 interval = [0.0, 50.0] L = np.array([1, 0, 0], dtype=np.double) I = np.identity(3, dtype=np.double) W, t, _, _ = euler(X_0, interval, n, L, I) print("Løsning med eulers metode:") print(W[n], end="\n\n") exact_sol = exactSolution(t) print("Eksakt løsning:") print(exact_sol[-1]) error(W, exact_sol, plot=True, title="Euler")
def within_tolerance(e_i, w_i): return (e_i / np.abs(np.sqrt(np.trace(np.dot(w_i.T, w_i))))) < T def get_new_h(h, w_i, e_i): return (0.8 * (((T * np.abs(np.sqrt(np.trace(np.dot(w_i.T, w_i)))) / (e_i))**(1 / 5))) * h) if __name__ == "__main__": X_0 = np.identity(3, dtype=np.double) h = get_h() interval = [0, 50] n = 50000 L = np.array([1, 0, 0], dtype=np.double) I = np.identity(3, dtype=np.double) W_r, t, _, E = RK45(X_0, interval, n, L, I) print("Approksimert løsning: ") print(W_r[-1]) print("Eksakt løsning: ") print(exactSolution([interval[1]])) exact_sol = exactSolution(t) error(W_r, exact_sol, plot=True, title="RK45") # for i in range(len(exact_sol)): # print(error(exact_sol[i], W_r[i])) #
def run_batch(root, start, end, idx=None): """ Run this script in batch mode. Download reading data whose timestamps lie within start and end dates. The date must follow the following format (Note the T between date and time): YYYY-MM-DDTHH:MM:SS where 24 hour time is used. If idx is a non-negative integer, instead download the meter at that index. idx is zero-indexed. If idx is greater than the number of meters, nothing happens; no files are downloaded. Default behavior is to download data for all meters. Params: root string start string end string idx integer """ s_date = get_date(start) e_date = get_date(end) if not s_date or not e_date: raise ValueError('Invalid/missing dates') elif start > end: raise ValueError('Start date must come before end date') elif not utils.exists_dir(root): raise ValueError('Root directory not found') elif idx is not None and not is_valid_index(idx): raise ValueError('Index must be non-negative integer') creds_file = defaults.creds(root) cnxn_str = utils.get_cnxn_str(creds_file) output_dir = defaults.downloads(root) meter_file = defaults.meter_file(root) utils.print_time('GETTER START') with Cursor.Cursor(cnxn_str) as cursor: dq = get_reading_from_name_query_str() meters = utils.read_meter_file(meter_file) for i, m in enumerate(meters): if idx is not None and idx != i: continue ion_name = utils.get_ion_name(m) qid = utils.get_ion_qid(m) try: cursor.execute(dq, ion_name, qid, str(s_date), str(e_date)) except pyodbc.Error: utils.error('Problem with query to get data for meter %s qid %d' % (ion_name, qid)) continue if cursor.rowcount == 0: utils.warn('No data found for meter %s qid %d' % (ion_name, qid)) continue meterId, meterName = utils.get_lucid_id_and_name(m) s_date_str = utils.make_lucid_ts(str(s_date)) e_date_str = utils.make_lucid_ts(str(e_date)) dl_fname = "%sT%sT%s.csv" % (meterId, s_date_str, e_date_str) path = os.path.join(output_dir, dl_fname) print('Writing data for meter %s qid %d to file: %s ...' % (ion_name, qid, path)), with open(path, 'wb') as data_file: writer = csv.writer(data_file) writer.writerow([meterId, meterName]) for row in cursor: ts = row.TimestampUTC val = row.Value data_row = [utils.make_lucid_ts(ts), val] writer.writerow(data_row) print('done') utils.print_time('GETTER END')
def _parse_table(self, table): TERNARY = 'ternary' EXACT = 'exact' table_headers = [] regs = [] for field in table.matches: if field == 'pkt.eth.src': table_headers.append({ 'name': 'hdr.ethernet.src', 'type': TERNARY }) elif field == 'pkt.eth.dst': table_headers.append({ 'name': 'hdr.ethernet.dst', 'type': TERNARY }) elif field == 'pkt.eth.dst': table_headers.append({ 'name': 'hdr.ethernet.dst', 'type': TERNARY }) elif field == 'inport_label' or field == 'inport': table_headers.append({ 'name': 'standard_metadata.ingress_port', 'type': TERNARY }) elif field == 'vlan': table_headers.append({'name': 'hdr.tag.tag', 'type': TERNARY}) elif field == 'pkt': pass else: reg = self._convert_reg(field) if reg: regs.append(reg) table_headers.append({ 'name': 'meta.' + reg, 'type': TERNARY }) else: error('unknown field', field) action_parm_str = '' action_str = '' if table.flowRules: # type: FlowRule _, _, actions = self._convert_flow(table.flowRules[0]) ret = self._parse_action(actions) str_map = { 'output': ('bit<9> port', 'output(port);set_terminal();'), 'controller': ('', 'controller();set_terminal();'), 'pop_tag_output': ('bit<9> port', 'pop_tag_output(port);set_terminal();'), 'push_tag_output': ('bit<16> tag, bit<9> port', 'push_tag_output(tag, port);set_terminal();') } if ret[0] in str_map: action_parm_str, action_str = str_map[ret[0]] elif ret[0].startswith('set_field'): action_parm_str = 'bit<2> v' action_str = 'meta.%s = v;' % ret[0][9:] else: error('action error', ret) flows = [] for f in table.flowRules: priority, matches, actions = self._convert_flow(f) ret = self._parse_action(actions) flows.append({ 'priority': priority, 'matches': matches, 'action_name': 't%d_action' % (table.tableId + 1), 'action_params': ret[1] }) return table_headers, regs, action_parm_str, action_str, flows
def print_failure(attempts): print(u.error(), 'FAILED TO FIND CREDENTIALS') print_exit(attempts)