Best Python code snippet using localstack_python
utils.py
Source:utils.py
1# -*- coding: utf-8 -*-2import functools3from typing import List, Optional, Tuple4import jieba5from langml.log import warn6def deprecated_warning(msg='this function is deprecated! it might be removed in a future version.'):7 def decorator(func):8 @functools.wraps(func)9 def wrapper(*args, **kwargs):10 warn(msg)11 return func(*args, **kwargs)12 return wrapper13 return decorator14def modify_boundary(target: str, content: str, expand_range: Optional[int] = 3) -> str:15 """16 åè¯ä¿®æ£ç®æ å符串çè¾¹ç17 Args:18 target: ç®æ å符串19 content: ç®æ å符串æå¨çææ¬20 expand_range: ç®æ å符串å¨ææ¬æå¨ä½ç½®ï¼åååæ©å±çåæ°ï¼æ é太大ï¼å 为ä¸ä¸ªä¸æè¯è¯å¤§çº¦1å°4å21 Returns: 使ç¨åè¯ä¿®æ£ç®æ å符串边çåçå符串22 """23 begin = content.find(target)24 # å¨contentä¸æ æ³æ¾å°target25 if begin == -1:26 return target27 # ç®æ å符串å¨ææ¬æå¨ä½ç½®ï¼åååæ©å±çä¸ä¸æ28 context = content[begin - expand_range if begin >= expand_range else 0: begin + len(target) + expand_range]29 # ç®æ å符串å¨ä¸ä¸æçèµ·å§ç»æ¢ä½ç½®30 context_start = expand_range if begin >= expand_range else begin31 context_end = context_start + len(target)32 seg_list = list(jieba.cut(context, cut_all=False))33 # æ¾åºtarget头尾å¨åè¯å表çä½ç½®34 seg_sum = 035 seg_start, seg_end = None, None36 for seg_id, seg in enumerate(seg_list):37 seg_range = [x + seg_sum for x in range(len(seg))]38 seg_sum += len(seg)39 if context_start in seg_range:40 seg_start = seg_id41 if context_end in seg_range:42 # å¦æåå§ç»å°¾å¨åè¯çåºé´ç¬¬ä¸ä¸ªä½ç½®, é£ä¹è¿ä¸ªåè¯åºé´ä¸éè¦ï¼# å¦åéè¦è¿ä¸ªåºé´43 if context_end == seg_range[0]:44 seg_end = seg_id45 else:46 seg_end = seg_id + 147 if seg_start and seg_end:48 new_target = "".join(seg_list[seg_start: seg_end])49 else:50 new_target = target51 return new_target52@deprecated_warning(msg='`rematch` is deprecated, it might be removed in a future version! '53 'please turn to `Tokenizer.tokens_mapping`.')54def rematch(offsets: List) -> List:55 mapping = []56 for offset in offsets:57 if offset[0] == 0 and offset[1] == 0:58 mapping.append([])59 else:60 mapping.append([i for i in range(offset[0], offset[1])])61 return mapping62def bio_decode(tags: List[str]) -> List[Tuple[int, int, str]]:63 """ Decode BIO tags64 Examples:65 >>> bio_decode(['B-PER', 'I-PER', 'O', 'B-ORG', 'I-ORG', 'I-ORG'])66 >>> [(0, 1, 'PER'), (3, 5, 'ORG')]67 """68 entities = []69 start_tag = None70 for i, tag in enumerate(tags):71 tag_capital = tag.split('-')[0]72 tag_name = tag.split('-')[1] if tag != 'O' else ''73 if tag_capital in ['B', 'O']:74 if start_tag is not None:75 entities.append((start_tag[0], i - 1, start_tag[1]))76 start_tag = None77 if tag_capital == 'B':78 start_tag = (i, tag_name)79 elif tag_capital == 'I' and start_tag is not None and start_tag[1] != tag_name:80 entities.append((start_tag[0], i, start_tag[1]))81 start_tag = None82 if start_tag is not None:83 entities.append((start_tag[0], i, start_tag[1]))...
btctools-ini-generator.py
Source:btctools-ini-generator.py
...7 # Example8 "Pod 1": "192.168.1.1-80",9 "Container 1": "10.10.1.1-200"10}11def expand_range(num_range: str):12 if '-' in num_range:13 lower, higher = list(map(int, num_range.split('-')))14 individual_numbers = []15 # Add +1 because iteration stops on the upper number16 for x in range(lower, higher + 1):17 individual_numbers.append(x)18 return individual_numbers19 else:20 return [num_range]21def alter_btctools_ini(new_ranges_dict: dict):22 formatted_text = []23 for name, ip_range in new_ranges_dict.items():24 first_expanded = second_expanded = third_expanded = fourth_expanded = None25 first_octet = ip_range.split(".")[0]26 first_expanded = expand_range(first_octet)27 second_octet = ip_range.split(".")[1]28 second_expanded = expand_range(second_octet)29 third_octet = ip_range.split(".")[2]30 third_expanded = expand_range(third_octet)31 fourth_octet = ip_range.split(".")[3]32 fourth_expanded = expand_range(fourth_octet)33 generated_entry = []34 for o1 in first_expanded:35 for o2 in second_expanded:36 for o3 in third_expanded:37 generated_entry.append("{0}.{1}.{2}.{3}-{0}.{1}.{2}.{4}"38 .format(o1, o2, o3, fourth_expanded[0], fourth_expanded[-1]))39 formatted_text.append("#{}:".format(name) + ','.join(generated_entry))40 formatted_text = ';'.join(formatted_text)41 # Alters only ipRangeGroups42 config = configparser.ConfigParser()43 config.read(btc_ini_template)44 previous_value = config.get('ui', 'ipRangeGroups')45 # If alteration was already made, exit the function46 if formatted_text in previous_value:...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!