How to use stop_execution method in localstack

Best Python code snippet using localstack_python

metrics.py

Source:metrics.py Github

copy

Full Screen

1# metrics.py 2# script holding function to analyse unipartite and bipartite networkx3# last modified : 16/11/214# author : jonas-mika senghaas5import os6import sys7import time8from datetime import date9from collections import Counter10import numpy as np11import pandas as pd12import networkx as nx13from tqdm import tqdm14local_path = os.path.dirname(os.path.realpath(__file__))15if local_path not in sys.path:16 sys.path.append(local_path)17from handle_timeout import * 18#--- GLOBAL SETUP 19STOP_EXECUTION = 60 20np.set_printoptions(suppress=True)21#--- HELPER FUNCTIONS22def five_num_summary(arr):23 """24 Function to compute a five-number-summary of any given 1-dimensional data, 25 that is passed in as an iterable. Uses numpy's percentile() function.26 Function Arguments 27 ------------------28 arr : iterable | 1D Data29 Returns30 -------31 np.array(5) | Five-Number Summary of Data32 """33 return np.percentile(arr, [0, 25, 50, 75, 100])34@break_after(STOP_EXECUTION)35def get_degrees(G):36 """37 Function to extract an (unsorted) iterable of all degrees from a38 nx.Graph object. Uses nx.Graph degree() method.39 Function Arguments 40 ------------------41 G : nx.Graph | Graph 42 Returns43 -------44 degrees : iterable | Unsorted Degrees of Graph 45 """46 return [degree for _, degree in G.degree()]47@break_after(10)48def get_lccs(G):49 """50 Function to extract an (unsorted) iterable of all local clustering51 coefficients from a nx.Graph object. Uses nx.clustering function.52 Note: Computationally expensive, may abort on huge graphs.53 Function Arguments 54 ------------------55 G : nx.Graph | Graph 56 Returns57 -------58 lccs : iterable | Unsorted LCC's of Graph 59 """60 return list(nx.clustering(G).values())61@break_after(STOP_EXECUTION)62def get_ccs(G):63 """64 Function to extract an iterable of all connected components65 (disconnected) nx.Graph object. Uses nx.connected_components() function.66 Function Arguments 67 ------------------68 G : nx.Graph | Graph 69 Returns70 -------71 ccs : iterable(nx.Graph) | Unsorted LCC's of Graph 72 """73 return [G.subgraph(cc) for cc in nx.connected_components(G)]74@break_after(STOP_EXECUTION)75def get_edge_weights(G):76 """77 Function to extract an iterable of all edge_weights from a 78 nx.Graph object. 79 Function Arguments 80 ------------------81 G : nx.Graph | Graph 82 Returns83 -------84 edge_weights : iterable(float) | Iterable of edge weights 85 """86 if not nx.is_weighted(G):87 return None88 return [edge[-1]['weight'] for edge in G.edges(data=True)] 89#--- SINGLE NETWORK METRICS90# basic network statistics91@break_after(STOP_EXECUTION)92def number_of_nodes(G):93 """94 Returns number of nodes in graph. Uses number_of_nodes() 95 method on nx.Graph object.96 Function Arguments 97 ------------------98 G : nx.Graph | Graph 99 Returns100 -------101 node_count : int | Number of Nodes102 """103 return G.number_of_nodes()104@break_after(STOP_EXECUTION)105def number_of_edges(G):106 """107 Returns number of edges in graph. Uses number_of_edges() 108 method on nx.Graph object.109 Function Arguments 110 ------------------111 G : nx.Graph | Graph 112 Returns113 -------114 edge_count : int | Number of Edges115 """116 return G.number_of_edges()117@break_after(STOP_EXECUTION)118def global_density(G):119 """120 Returns global density of graph, which is the proportion of121 the number of existing edges of from all edges that can122 possibly exist, st. for an undirected graph:123 density = |E| / N(N-1)124 Function Arguments 125 ------------------126 G : nx.Graph | Graph 127 Returns128 -------129 density : float[0,1] | Global Density Measure 130 """131 return nx.density(G)132@break_after(STOP_EXECUTION)133def global_diameter(G):134 """135 Returns global diameter, which corresponds to the 136 longest shortest path in the network. Can only be computed137 for fully connected graphs. Otherwise, callable on CCs (ie.138 largest CC)139 Function Arguments 140 ------------------141 G : nx.Graph | Graph 142 Returns143 -------144 diameter : int | Global Diameter Measure 145 """146 if nx.is_connected(G):147 return nx.diameter(G)148 else: return 'No Global Diameter (Unconnected Graph)'149# edge weights statistics150@break_after(STOP_EXECUTION)151def average_edge_weight(edge_weights):152 return np.mean(edge_weights) if edge_weights else None153@break_after(STOP_EXECUTION)154def summarise_edge_weights(edge_weights):155 return five_num_summary(edge_weights) if edge_weights else None156@break_after(STOP_EXECUTION)157def variance_edge_weights(edge_weights):158 return np.var(edge_weights) if edge_weights else None159@break_after(STOP_EXECUTION)160def most_frequent_edge_weight(edge_weights):161 return Counter(edge_weights).most_common()[0][0] if edge_weights else None162# degree statistics163@break_after(STOP_EXECUTION)164def average_degree(degrees):165 """166 Returns average degree from iterable of degrees.167 Function Arguments 168 ------------------169 degrees : iterable(int) | Iterable of Degrees 170 Returns171 -------172 average_degree : float | Average Degree 173 """174 return np.mean(degrees) if degrees else None175@break_after(STOP_EXECUTION)176def summarise_degrees(degrees):177 """178 Returns Five-Number-Summary of Degrees from iterable179 of degrees.180 Function Arguments 181 ------------------182 degrees : iterable(int) | Iterable of Degrees 183 Returns184 -------185 summary : np.array(5) | Five-Num-Summary of Degrees 186 """187 return five_num_summary(degrees) if degrees else None188# local clustering coefficient189@break_after(STOP_EXECUTION)190def average_lcc(lccs):191 """192 Returns average local clustering coefficient from 193 iterable of local clustering coefficients.194 Function Arguments 195 ------------------196 lccs : iterable(float) | Iterable of LCCs 197 Returns198 -------199 average_lcc : float | Average LCC 200 """201 return np.mean(lccs) if lccs else None202@break_after(STOP_EXECUTION)203def summarise_lcc(lccs):204 """205 Returns Five-Number-Summary of local clustering 206 coefficients from iterable of local clustering 207 coefficients.208 Function Arguments 209 ------------------210 lccs : iterable(float) | Iterable of LCCs 211 Returns212 -------213 summary : np.array(5) | Five-Num-Summary of LCCs214 """215 return five_num_summary(lccs) if lccs else None216# connected components (size)217@break_after(STOP_EXECUTION)218def number_of_ccs(ccs):219 """220 Returns number of connected components from iterable221 of connected subgraphs. 222 Function Arguments 223 ------------------224 ccs : iterable(nx.Graph) | Iterable of connected subgraphs 225 Returns226 -------227 #ccs : int | Number of CCs 228 """229 return len(ccs) if ccs else None230@break_after(STOP_EXECUTION)231def average_cc_size(ccs):232 """233 Returns Average Size of connected components from iterable234 of connected components as nx.Subgraphs().235 Function Arguments 236 ------------------237 ccs : iterable(nx.Graph) | Iterable of connected subgraphs 238 Returns239 -------240 average_size : float | Average Size of CCs241 """242 return np.mean([cc.number_of_nodes() for cc in ccs]) if ccs else None243@break_after(STOP_EXECUTION)244def summarise_cc_size(ccs):245 """246 Returns Five-Number-Summary of sizes of connected components 247 from iterableof connected components as nx.Subgraphs().248 Function Arguments 249 ------------------250 ccs : iterable(nx.Graph) | Iterable of connected subgraphs 251 Returns252 -------253 summary : np.array(5) | Five-Num-Summary of CC Sizes254 """255 return five_num_summary([cc.number_of_nodes() for cc in ccs]) if ccs else None256@break_after(STOP_EXECUTION)257def average_cc_density(ccs):258 """259 Returns the average density in all connected components.260 Function Arguments 261 ------------------262 ccs : iterable(nx.Graph) | Iterable of connected subgraphs 263 Returns264 -------265 average_density : float | Average Density among all CCs266 """267 return np.mean([nx.density(cc) for cc in ccs]) if ccs else None268@break_after(STOP_EXECUTION)269def summarise_cc_density(ccs):270 """271 Returns Five-Number-Summary of densities in all connected components.272 Function Arguments 273 ------------------274 ccs : iterable(nx.Graph) | Iterable of connected subgraphs 275 Returns276 -------277 summary : np.array(5) | Five-Num-Summary of Densities among all CCs278 """279 return five_num_summary([nx.density(cc) for cc in ccs]) if ccs else None280@break_after(STOP_EXECUTION)281def average_diameter(ccs):282 """283 Returns average diameter of all connected components.284 Function Arguments 285 ------------------286 ccs : iterable(nx.Graph) | Iterable of connected subgraphs 287 Returns288 -------289 average_diameter : float | Average Diameter among all CCs290 """291 return np.mean([nx.diameter(cc) for cc in ccs]) if ccs else None292@break_after(STOP_EXECUTION)293def summarise_diameter(ccs):294 """295 Returns Five-Number-Summary of diameter in all connected components.296 Function Arguments 297 ------------------298 ccs : iterable(nx.Graph) | Iterable of connected subgraphs 299 Returns300 -------301 summary : np.array(5) | Five-Num-Summary of diameters among all CCs302 """303 return five_num_summary([nx.diameter(cc) for cc in ccs]) if ccs else None304# centrality measures305@break_after(STOP_EXECUTION)306def degree_centrality(G, n):307 """308 Returns the n most degree central nodes from a nx.Graph.309 Function Arguments 310 ------------------311 G : nx.Graph | Graph 312 n : int | Number of nodes to return313 Returns314 -------315 nodes : iterable(node) | N most degree central nodes 316 """317 return [x[0] for x in sorted([item for item in nx.algorithms.centrality.degree_centrality(G).items()], 318 key=lambda item: item[1], reverse=True)[:n]]319@break_after(10)320def betweenness_centrality(G, n):321 """322 Returns the n nodes with highest betweenness centrality 323 from a nx.Graph.324 Function Arguments 325 ------------------326 G : nx.Graph | Graph 327 n : int | Number of nodes to return328 Returns329 -------330 nodes : iterable(node) | N most between nodes331 """332 return [x[0] for x in sorted([item for item in nx.algorithms.centrality.betweenness_centrality(G).items()], 333 key=lambda item: item[1], reverse=True)[:n]]334#--- NETWORK STATISTIC BUNDLER335#--- Combines functions in similar fields into dictionary to conveniently call together (also faster in execution)336def export_metrics(G):337 degrees = get_degrees(G)338 lccs = get_lccs(G)339 try: 340 ccs = get_ccs(G)341 except: 342 ccs = None343 edge_weights = get_edge_weights(G)344 try: gd = global_diameter(G)345 except: gd = None346 return {'Basic Statistics': 347 {348 'Number of Nodes': number_of_nodes(G), 349 'Number of Edges': number_of_edges(G), 350 'Global Density' : global_density(G),351 'Global Diameter': gd352 #'Average Diameter': f'{round(average_diameter(G), 2)}', 'Five-Number-Summary Diameter': summarise_diameter(G)}353 },354 'Degree Statistics': 355 {356 'Average Degree': average_degree(degrees),357 'Five-Number-Summary Degrees': summarise_degrees(degrees)358 },359 'Edge Weight Statistics':360 {361 'Average Edge Weight': average_edge_weight(edge_weights),362 'Five-Number-Summary Edge Weights': summarise_edge_weights(edge_weights),363 'Variance of Edge Weights': variance_edge_weights(edge_weights),364 'Most Frequent Edge Weight': most_frequent_edge_weight(edge_weights)365 },366 'Clustering Statistics':367 {368 'Average LCC': average_lcc(lccs),369 'Five-Number-Summary LCC': summarise_lcc(lccs)370 },371 'Connected Components Statistics': 372 {373 'Number of CC': number_of_ccs(ccs), 374 'Average CC Size': average_cc_size(ccs), 375 'Five-Number-Summary of CC Sizes': summarise_cc_size(ccs),376 'Average CC Density': average_cc_density(ccs),377 'Five-Number-Summary of CC Densities': summarise_cc_density(ccs)378 },379 'Centrality Statistics':380 {381 'Degree Centrality': degree_centrality(G, 10),382 'Betweenness Centrality': betweenness_centrality(G, 10)383 }384 }385if __name__ == '__main__':386 start = time.time()387 G = nx.read_gpickle('../data/projections/pickle_format/simple_weight.pickle')388 #G = nx.karate_club_graph()389 end = time.time()390 print(f'Finished Reading in {end-start}s')...

Full Screen

Full Screen

hooks.py

Source:hooks.py Github

copy

Full Screen

...23 emu.mem_write(address, "\xdd\xdd\xdd\xdd")24 skip = True25 for op in emu.arch.calls:26 if opcode.startswith(op):27 emu.stop_execution()28 skip = True29 if not skip:30 emu.stop_execution()31 if access == UC_MEM_WRITE:32 if emu.verbosity_level > 1:33 print "Instruction 0x{:x} writes value 0x{:x} with 0x{:x} bytes into 0x{:x}".format(current_address, value,34 size, address)35 value_hex = int_to_hex(value, size)36 prev_value = addr_to_int(emu.mem_read(address, size)) if address in emu.memory else 037 emu.add_to_emulator_mem(address, value_hex)38 else:39 # memory read index replacement40 if emu.mem_read_index_map:41 if emu.mem_read_index_counter in emu.mem_read_index_map:42 value = emu.mem_read_index_map[emu.mem_read_index_counter]43 emu.mem_write(address, value)44 emu.mem_read_index_counter += 145 if emu.stop_next_instruction:46 opcode = str(emu.mem_read(current_address, size)).encode("hex")47 if opcode.startswith("c3") or opcode.startswith("cb"):48 value = addr_to_int("\xdd\xdd\xdd\xdd")49 emu.mem_write(address, "\xdd\xdd\xdd\xdd")50 SP = emu.reg_read(emu.arch.SP)51 emu.reg_write(emu.arch.SP, SP + 4)52 if opcode.startswith("c2") or opcode.startswith("ca"):53 value = addr_to_int("\xdd\xdd\xdd\xdd")54 emu.mem_write(address, "\xdd\xdd\xdd\xdd")55 SP = emu.reg_read(emu.arch.SP)56 v = addr_to_int(opcode[1:])57 emu.reg_write(emu.arch.SP, SP + 4 + v)58 if opcode.startswith("e8") or opcode.startswith("9a") or opcode.startswith("ff"):59 emu.stop_execution()60 value = addr_to_int(emu.mem_read(address, size))61 if emu.no_zero_mem and value == 0:62 value = 163 emu.mem_write(address, "\x01")64 prev_value = value65 if emu.verbosity_level > 1:66 print "Instruction 0x{:x} reads value 0x{:x} with 0x{:x} bytes from 0x{:x}".format(current_address, value,67 size, address)68 if emu.memory_trace:69 emu.memory_tracer.add_trace(current_address, access, address, prev_value, value, size)70 return True71def hook_code(uc, address, size, emu):72 opcode = str(emu.mem_read(address, size))73 if emu.stop_next_instruction:74 emu.stop_execution()75 if address == emu.final_instruction:76 emu.stop_next_instruction = True77 for op in emu.arch.jumps.union(emu.arch.conditional_jumps):78 if opcode.encode("hex").startswith(op):79 emu.stop_execution()80 return False81 if emu.verbosity_level > 1:82 print "0x{:x};{}".format(address, opcode.encode("hex"))83 # handle breakpoint84 if emu.instruction_breakpoints_enabled and address in emu.instruction_breakpoints:85 cb = emu.instruction_breakpoints[address][0]86 args = emu.instruction_breakpoints[address][1]87 call = cb(emu, *args)88 # bp handler returns False89 if not call:90 emu.stop_execution()91 return False92 if emu.instruction_trace:93 emu.code_tracer.add_instruction_trace(address, opcode, size)94 if emu.force_path:95 if not emu.enforced_path:96 emu.stop_execution()97 return False98 path_addr, path_instr_size = emu.enforced_path.popleft()99 if path_addr != address:100 emu.stop_execution()101 emu.enforced_path.appendleft((path_addr, size))102 return False103 return True104def hook_block(uc, address, size, emu):105 opcodes = str(emu.mem_read(address, size))106 # handle breakpoint107 if emu.basic_block_breakpoints_enabled and address in emu.basic_block_breakpoints:108 cb = emu.basic_block_breakpoints[address][0]109 args = emu.basic_block_breakpoints[address][1]110 call = cb(emu, *args)111 # bp handler returns False112 if not call:113 emu.stop_execution()114 if emu.verbosity_level > 1:115 print "Basic block at 0x{:x}".format(address)116 if emu.basic_block_trace:117 emu.code_tracer.add_basic_block_trace(address, opcodes, size)...

Full Screen

Full Screen

timer_utils.py

Source:timer_utils.py Github

copy

Full Screen

1import time2class Timer(object):3 def __init__(self):4 self.start_execution = 05 self.stop_execution = 06 def reset_timer(self):7 self.start_execution = 08 self.stop_execution = 09 def start_timer(self):10 self.start_execution = time.time()11 def stop_timer(self):12 self.stop_execution = time.time()13 self.print_time()14 def print_time(self):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful