How to use find_from_imports method in prospector

Best Python code snippet using prospector_python

autodetect.py

Source:autodetect.py Github

copy

Full Screen

...9# see http://docs.python.org/2/reference/lexical_analysis.html#identifiers10_FROM_IMPORT_REGEX = re.compile(r"^\s*from ([\._a-zA-Z0-9]+) import .*$")11_IMPORT_REGEX = re.compile(r"^\s*import ([\._a-zA-Z0-9]+)$")12_IMPORT_MULTIPLE_REGEX = re.compile(r"^\s*import ([\._a-zA-Z0-9]+(, ){1})+")13def find_from_imports(file_contents):14 names = set()15 for line in file_contents.split("\n"):16 match = _IMPORT_MULTIPLE_REGEX.match(line)17 if match:18 import_names = []19 first = match.group(1)20 import_names.append(first[:-2])21 for name in line.split(first)[1].split(","):22 import_names.append(name.strip())23 else:24 match = _IMPORT_REGEX.match(line) or _FROM_IMPORT_REGEX.match(line)25 if match is None:26 continue27 import_names = match.group(1).split(".")28 for import_name in import_names:29 if import_name in POSSIBLE_LIBRARIES:30 names.add(import_name)31 return names32def find_from_path(path):33 names = set()34 max_possible = len(POSSIBLE_LIBRARIES)35 for item in os.listdir(path):36 item_path = os.path.abspath(os.path.join(path, item))37 if os.path.isdir(item_path):38 if is_virtualenv(item_path):39 continue40 names |= find_from_path(item_path)41 elif not os.path.islink(item_path) and item_path.endswith(".py"):42 try:43 contents = encoding.read_py_file(item_path)44 names |= find_from_imports(contents)45 except encoding.CouldNotHandleEncoding as err:46 # TODO: this output will break output formats such as JSON47 warnings.warn("{0}: {1}".format(err.path, err.cause), ImportWarning)48 if len(names) == max_possible:49 # don't continue on recursing, there's no point!50 break51 return names52def find_from_requirements(path):53 reqs = find_requirements(path)54 names = []55 for requirement in reqs:56 if requirement.name is not None and requirement.name.lower() in POSSIBLE_LIBRARIES:57 names.append(requirement.name.lower())58 return names...

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 prospector 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