Best Python code snippet using responses
__init__.py
Source:__init__.py
...36 return cache.get('allowed') or {}37 @property38 def _get_denied(self):39 return cache.get('denied') or {}40 def _set_registry(self, key, value):41 cache.set(key, value)42 setattr(self, '_{0}'.format(key),43 getattr(self, '_get_{0}'.format(key)))44 def init_permissions(self):45 resources = {}46 for res in Resource.all():47 resources[res.name] = set()48 roles = {}49 allowed = {}50 denied = {}51 for r in Role.all():52 roles[r.rolename] = set()53 for perm, res, allow in r.perms():54 roles[r.rolename, perm, res] = None55 if allow:56 allowed[r.rolename, perm, res] = None57 else:58 denied[r.rolename, perm, res] = None59 self._set_registry('roles', roles)60 self._set_registry('resources', resources)61 self._set_registry('allowed', allowed)62 self._set_registry('denied', denied)63acl = Registry()64class check_permission(object):65 """Improved version of check_permission.66 Original function raises assertion error if resource was not found,67 but this one will throw warning and deny access.68 Also, this version uses exception inherited from APIError.69 As an original method, this one could be used as a decorator, a context70 manager, a boolean-like value or directly by calling method check().71 """72 def __init__(self, operation, resource, user=None, **exception_kwargs):73 self.operation = operation74 self.resource = resource75 self.user = user76 self.exception_kwargs = exception_kwargs...
core.py
Source:core.py
...14_RegistryType = dict[str, _F]15_registry_context: ContextVar[_RegistryType] = ContextVar("registry", default={})16def _get_registry() -> _RegistryType:17 return _registry_context.get().copy()18def _set_registry(registry: _RegistryType) -> None:19 _registry_context.set(registry)20@contextmanager21def registry_context() -> Iterator[None]:22 registry = _get_registry()23 yield24 _set_registry(registry)25@overload26def register(func: _F) -> _F:27 ...28@overload29def register(*, name: str) -> Callable[[_F], _F]:30 ...31def register(func: Optional[_F] = None, *, name: Optional[str] = None) -> Callable[[_F], _F]:32 wrapper = _register_inner(name)33 if func is not None:34 return wrapper(func)35 return wrapper36def _register_inner(name: Optional[str]) -> Callable[[_F], _F]:37 def inner(f: _F) -> _F:38 nonlocal name39 if name is None:40 name = f.__name__41 registry = _get_registry()42 registry[name] = f43 _set_registry(registry)44 return f45 return inner46def run(f: Callable[..., _RType], **kwargs: Any) -> _RType:47 result, teardowns = _run_inner(f, kwargs)48 for teardown in reversed(teardowns):49 with suppress(StopIteration):50 next(teardown)51 return result52def _run_inner(f: Callable[..., _RType], kwargs: dict[str, Any]) -> tuple[_RType, list]:53 teardowns = []54 function_signature = signature(f)55 parameters = {}56 for parameter, v in function_signature.parameters.items():57 logger.debug(f"For {f.__name__}, resolving {parameter}")...
register.py
Source:register.py
...11 return {}12 with open(file, "r") as f:13 r = json.load(f)14 return r15def _set_registry(r, file=RESOURCE_FILE):16 with open(file, "w") as f:17 json.dump(r, f, indent=4)18def get_graph_registry(file=RESOURCE_FILE):19 """20 Read the graph registry. By default, it is located in "./resources.json" with the "graph" key.21 :param file: file containing the registry. It should be a JSON file with a "graph" key.22 :return: a dictionary mapping graph names to their location on the disk.23 """24 if not os.path.exists(file):25 return {}26 with open(file, "r") as f:27 r = json.load(f)28 return r.get("graph", {})29def registry_contains(graph_name):30 return graph_name in get_graph_registry()31def get_registered(graph_name):32 return get_graph_registry().get(graph_name, graph_name)33def register(graph_name, graph_dir, force_override=False):34 r = _get_registry()35 if "graph" not in r:36 r["graph"] = {}37 if graph_name in r["graph"] and not force_override:38 if input(f"A graph named '{graph_name}' is already registered, with path '{r['graph'][graph_name]}'. Do you"39 " want to override it? y/[n]") != "y":40 return41 r["graph"][graph_name] = graph_dir42 _set_registry(r)43def deregister(graph_name):44 r = _get_registry()45 if "graph" not in r or graph_name not in r["graph"]:46 return47 del r["graph"][graph_name]...
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!!