def test_iteritems(): dict_ = dict(foo="bar") assert isinstance(iteritems(dict_), Iterable) for k, v in iteritems(dict_): assert dict_[k] == v
def get_function_arguments(fn): # https://stackoverflow.com/a/2677263 params = dict() success = False if _compat.PY2: # pragma: no cover argspec_getter = inspect.getargspec success = True if _compat.PYTHON_VERSION >= (3, 0) and _compat.PYTHON_VERSION < ( 3, 5): # pragma: no cover argspec_getter = inspect.getfullargspec success = True if success: # pragma: no cover argspec = argspec_getter(fn) params = dict_from_list(argspec.args, argspec.defaults or []) if _compat.PYTHON_VERSION >= (3, 5): signature = inspect.signature(fn) parameters = signature.parameters params = {k: v.default for k, v in _compat.iteritems(parameters)} success = True if not success: # pragma: no cover raise ValueError( "Unknown Python Version {} for fetching functional arguments.". format(sys.version)) return params
def foobar(*args, **kwargs): args = get_args() params = merge_dict(args, override) for k, v in iteritems(values): assert params[k] == v if initial: for k in iterkeys(initial): assert initial[k] == args[k]
def popen(*args, **kwargs): output = kwargs.get("output", False) quiet = kwargs.get("quiet", False) directory = kwargs.get("cwd") environment = kwargs.get("env") shell = kwargs.get("shell", True) raise_err = kwargs.get("raise_err", True) environ = os.environ.copy() if environment: environ.update(environment) for k, v in iteritems(environ): environ[k] = str(v) command = " ".join([str(arg) for arg in args]) logger.info("Executing command: %s" % command) if quiet: output = True proc = sp.Popen(command, bufsize=-1, stdin=sp.PIPE if output else kwargs.get("stdin"), stdout=sp.PIPE if output else None, stderr=sp.PIPE if output else None, env=environ, cwd=directory, shell=shell) code = proc.wait() if code and raise_err: raise PopenError(code, command) if output: output, error = proc.communicate() if output: output = safe_decode(output) output = strip(output) if error: error = safe_decode(error) error = strip(error) if quiet: return code else: return code, output, error else: return code
def _init(self): for k, v in iteritems(Settings._DEFAULTS): self.set(k, v)