How to use expand_range method in localstack

Best Python code snippet using localstack_python

utils.py

Source:utils.py Github

copy

Full Screen

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]))...

Full Screen

Full Screen

btctools-ini-generator.py

Source:btctools-ini-generator.py Github

copy

Full Screen

...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:...

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