Best Python code snippet using lisa_python
statically.py
Source:statically.py  
...15    if hasattr(main, 'get_ipython'):16        return True17    # standard REPL doesn't have __file__18    return hasattr(main, '__file__')19def _get_source_code(obj):20    if inspect.isclass(obj):21        lines = inspect.getsourcelines(obj)[0]22        extra_spaces = lines[0].find("class")23        return "".join(l[extra_spaces:] for l in lines)24    elif callable(obj):25        lines = inspect.getsourcelines(obj)[0]26        extra_spaces = lines[0].find("@")27        return "".join(l[extra_spaces:] for l in lines[1:])28    else:29        message = "Function or class expected, got {}.".format(type(obj).__name__)30        raise TypeError(message)31def _get_non_local_scopes(frame):32    while frame:33        yield frame.f_locals34        frame = frame.f_back35def _get_outer_variables(obj):36    frame = inspect.currentframe().f_back37    non_local_scopes = _get_non_local_scopes(frame)38    non_local_variables = list(obj.__code__.co_freevars)39    variables = {}40    for scope in non_local_scopes:41        for name in non_local_variables:42            if name in scope:43                variables[name] = scope[name]44                non_local_variables.remove(name)45        if not non_local_variables:46            break47    return variables48def typed(obj):49    """Compiles a function or class with cython.50    Use annotations for static type declarations. Example:51        import statically52        @statically.typed53        def add_two(x: int) -> int:54            two: int = 255            return x + two56    """57    if not _can_cython_inline():58        return obj59    elif has_async_gen_fun and inspect.isasyncgenfunction(obj):60        raise TypeError("Async generator funcions are not supported.")61    source = _get_source_code(obj)62    frame = inspect.currentframe().f_back63    if inspect.isclass(obj):64        locals_ = frame.f_locals65    else:66        locals_ = _get_outer_variables(obj)67    with warnings.catch_warnings():68        warnings.simplefilter("ignore")69        compiled = cython.inline(source, locals=locals_,70                                 globals=frame.f_globals, quiet=True)71        return compiled[obj.__name__]72if not _can_cython_inline():73    warnings.warn(74        "Current code isn't launched from a file so statically.typed isn't able to cythonize stuff."75        "Falling back to normal Python code.",...base.py
Source:base.py  
...16    @abstractmethod17    def _get_imports(self) -> Optional[List[str]]:18        pass19    @abstractmethod20    def _get_source_code(self) -> List[str]:21        pass22    @abstractmethod23    def _is_package(self) -> bool:24        pass25    def make(self) -> None:26        package_dir = 'packages' if self._is_package() else ''27        with open(os.path.join(os.getcwd(), self.__directory, package_dir, f'{self._get_file_name()}.py'), mode='w') as f:28            f.write(self.__program())29    def __get_lines(self) -> List[str]:30        imports = self._get_imports()31        if imports is None:32            return self._get_source_code()33        return imports + ['', ''] + self._get_source_code()34    def __program(self) -> str:35        indent = 036        closed = False37        results = []38        for line in self.__get_lines():39            line = line.strip()40            new_line = ''41            skip = len(line) > 0 and ('}' == line[0] or '{' == line[0])42            if len(line) > 0 and '}' == line[0]:43                indent -= 144                closed = True45            else:46                if closed and line != 'else:':47                    new_line = '\n'...download_contract.py
Source:download_contract.py  
...3from util import ContractNotFoundException, InvalidEtherscanKeyException4def download_contract(address):5    try:6        eth = Etherscan(os.getenv('KEY'))7        filename, source = _get_source_code(eth, address)8    except AssertionError as e:9        raise InvalidEtherscanKeyException(e)10    _save_file(filename, source)11    return filename[:-4]12def _get_source_code(eth, address):13    result = eth.get_contract_source_code(address)[0]14    sourcecode = result['SourceCode']15    if sourcecode:16        filename = f"{result['ContractName']}__{address[-5:]}.sol"17        return filename, sourcecode18    else:19        raise ContractNotFoundException(address)20def _save_file(filename, source):21    with open(os.getenv('FILE_LOCATION') + filename, 'w') as f:...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!!
