How to use is_path_added method in avocado

Best Python code snippet using avocado_python Github


Full Screen

1import cfscrape2import time3import os4import slugify5import json6ROOT = ""7file_path = [os.path.abspath(os.curdir), 'data']8scraper = cfscrape.create_scraper()9def get_data(url, sleep_time=1):10 res = scraper.get(url)11 data = res.json()12 path_name = data.get('name')13 if path_name is not None:14 path_name = slugify.slugify(path_name)15 time.sleep(sleep_time)16 return data, path_name17def get_name(data):18 return data.get('name', 'No name...')19def save_data(data, file_path):20 path = os.path.join(*file_path)21 if not os.path.isdir(path):22 os.makedirs(path)23 fp = os.path.join(path, file_path[-1] + '.json')24 with open(fp, 'w') as fl:25 json.dump(data, fl)26def get_url(path):27 return ROOT + path28def get_subregions(data):29 return sorted(data.get('subRegions').keys())30def get_custom_code(data):31 return data.get('customCode')32def skip_tally(regional_json, subregional_json, municipality_json):33 # Use this to ignore already downloaded data34 skip_region = None35 skip_subregion = None36 skip_municipality = None37 if all([skip_region, regional_json]):38 if (get_custom_code(regional_json) < skip_region):39 return True40 if all([skip_region, skip_subregion, regional_json, subregional_json]):41 if (get_custom_code(regional_json) == skip_region) and (get_name(subregional_json) < skip_subregion):42 return True43 if all([skip_region, skip_subregion, skip_municipality, regional_json, subregional_json, municipality_json]):44 if (45 (get_custom_code(regional_json) == skip_region) and46 (get_name(subregional_json) == skip_subregion) and47 (get_name(municipality_json) < skip_municipality)48 ):49 return True50 return False51def process_data(parent_json, child_key, level=0, out_type='subRegions'):52 global file_path53 is_path_added = False54 child_url = get_url(parent_json.get('subRegions')[child_key].get('url'))55 child_json, path_name = get_data(child_url)56 print '\t' * level, get_name(child_json)57 if path_name is not None:58 file_path.append(path_name)59 is_path_added = True60 save_data(child_json, file_path)61 if out_type == 'subRegions':62 grandchildren = get_subregions(child_json)63 elif out_type == 'contests':64 grandchildren = child_json.get(out_type)65 else:66 raise ValueError('Use either `subRegions` or `contests` as out_type value.')67 return child_json, grandchildren, is_path_added68def process_contests(contests_list):69 global file_path70 if contests_list is None:71 return72 # Get actual poll results73 for contest in contests_list:74 contest_url = get_url(contest['url'])75 try:76 contest_json, _ = get_data(contest_url, sleep_time=0.1)77 except Exception as e:78 print e.message79 continue80 path_name = contest['url']81 file_path.append(path_name)82 fname = os.path.join(*file_path)83 if not os.path.exists(os.path.dirname(fname)):84 os.makedirs(os.path.dirname(fname))85 with open(fname, 'w') as fl:86 json.dump(contest_json, fl)87 file_path.pop()88class Depth(object):89 subregional = 090 municipality = 191# This can be used to indicate whether the scraper should scrape upto the municipality level or not.92MAX_DEPTH = Depth.municipality93if __name__ == '__main__':94 country_url = get_url("data/regions/0.json")95 country_json, path_name = get_data(country_url)96 print get_name(country_json)97 if path_name is not None:98 file_path.append(path_name)99 save_data(country_json, file_path)100 regions = get_subregions(country_json)101 for region in regions:102 print '\t', region103 regional_json, subregions, is_path_added = process_data(country_json, region, level=1)104 if skip_tally(regional_json, None, None):105 if is_path_added:106 file_path.pop()107 continue108 for subregion in subregions:109 subregional_json, municipalities, is_path_added = process_data(regional_json, subregion, level=2)110 if skip_tally(regional_json, subregional_json, None):111 if is_path_added:112 file_path.pop()113 continue114 subregional_contests = subregional_json.get('contests')115 process_contests(subregional_contests)116 if MAX_DEPTH != Depth.municipality:117 file_path.pop()118 continue119 for municipality in municipalities:120 municipality_json, municipal_contests, is_path_added = (121 process_data(122 subregional_json,123 municipality,124 level=3,125 out_type='contests'126 )127 )128 if skip_tally(regional_json, subregional_json, municipality_json):129 if is_path_added:130 file_path.pop()131 continue132 process_contests(municipal_contests)133 file_path.pop()134 file_path.pop()135 file_path.pop()...

Full Screen

Full Screen Github


Full Screen

1import os2import re3if __name__ == '__main__':4 current_abs_path = os.path.abspath(os.getcwd()) # gets the current abs path to this py file5 sep = os.path.sep # get os specific path seperator (either '\' for Windows or '/' for linux)6 pattern = f'(.+)\{sep}Unittests'7 # grabs everything from current_abs_path except '/UnitTests' string8 m = re.match(pattern, current_abs_path)9 src_abs_path = f'{m.groups()[0]}{sep}Src{sep}' # forms src_abs_path from the grabbed string10 unittests_common_abs_path = f'{current_abs_path}{sep}Common{sep}'11 paths = [src_abs_path, unittests_common_abs_path]12 result = ''13 if 'PYTHONPATH' in os.environ:14 is_path_added = False15 for p in paths:16 if p in os.environ['PYTHONPATH']:17 # if p already is included in PYTHONPATH, do nothing18 pass19 else:20 # if p is not included in PYTHONPATH, then result will contain p21 if result:22 result = f'{os.path.pathsep}'.join([p,result])23 else:24 result = p25 is_path_added = True26 if is_path_added:27 result = os.environ['PYTHONPATH'] + os.path.pathsep + result28 else:29 result = f'{os.path.pathsep}'.join(paths)30 file = open('pythonpath.txt', 'w+') # re-formats file contents, under the same folder as run command31 file.write(result)...

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:


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

Run avocado 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?