Python builtins.len() Examples
The following are 30
code examples of builtins.len().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
builtins
, or try the search function
.
Example #1
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 6 votes |
def _get_conditional_content(self, fname, spans, conditions, contents): out = [] ieval = [] peval = [] multiline = (spans[0][0] != spans[-1][1]) for condition, content, span in zip(conditions, contents, spans): try: cond = bool(self._evaluate(condition, fname, span[0])) except Exception as exc: msg = "exception occurred when evaluating '{0}'"\ .format(condition) raise FyppFatalError(msg, fname, span, exc) if cond: if self._linenums and not self._diverted and multiline: out.append(linenumdir(span[1], fname)) outcont, ievalcont, pevalcont = self._render(content) ieval += _shiftinds(ievalcont, len(out)) peval += pevalcont out += outcont break if self._linenums and not self._diverted and multiline: out.append(linenumdir(spans[-1][1], fname)) return out, ieval, peval
Example #2
Source File: test_dynamic.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_cannot_change_globals_or_builtins_with_exec(self): def foo(): return len([1, 2, 3]) self.configure_func(foo) globals_dict = {"foo": foo} exec("x = foo()", globals_dict) self.assertEqual(globals_dict["x"], 3) # Note that this *doesn't* change the definition of len() seen by foo(). builtins_dict = {"len": lambda x: 7} globals_dict = {"foo": foo, "__builtins__": builtins_dict, "len": lambda x: 8} exec("x = foo()", globals_dict) self.assertEqual(globals_dict["x"], 3)
Example #3
Source File: test_dynamic.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_modify_builtins_from_leaf_function(self): # Verify that modifications made by leaf functions percolate up the # callstack. with swap_attr(builtins, "len", len): def bar(): builtins.len = lambda x: 4 def foo(modifier): l = [] l.append(len(range(7))) modifier() l.append(len(range(7))) return l self.configure_func(foo, lambda: None) self.assertEqual(foo(bar), [7, 4])
Example #4
Source File: shell.py From unipacker with GNU General Public License v2.0 | 6 votes |
def print_mem(self, base, num_elements, t="int", base_alias=""): if not base_alias: base_alias = f"0x{base:02x}" string = None if t == "str": string = get_string(base, self.engine.uc) t = "byte" num_elements = len(string) types = { "byte": ("B", 1), "int": ("<I", 4) } fmt, size = types[t] for i in range(num_elements): item, = struct.unpack(fmt, self.engine.uc.mem_read(base + i * size, size)) print(f"{base_alias}+{i * 4} = 0x{item:02x}") if string is not None: print(f"String @0x{base:02x}: {string}")
Example #5
Source File: core.py From pythonflow with Apache License 2.0 | 6 votes |
def control_dependencies(dependencies, graph=None): """ Ensure that all `dependencies` are executed before any operations in this scope. Parameters ---------- dependencies : list Sequence of operations to be evaluted before evaluating any operations defined in this scope. """ # Add dependencies to the graph graph = Graph.get_active_graph(graph) graph.dependencies.extend(dependencies) yield # Remove dependencies from the graph del graph.dependencies[-len(dependencies):] # pylint: disable=C0103
Example #6
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 6 votes |
def _get_callable_argspec_py2(func): argspec = inspect.getargspec(func) varpos = argspec.varargs varkw = argspec.keywords args = argspec.args tuplearg = False for elem in args: tuplearg = tuplearg or isinstance(elem, list) if tuplearg: msg = 'tuple argument(s) found' raise FyppFatalError(msg) defaults = {} if argspec.defaults is not None: for ind, default in enumerate(argspec.defaults): iarg = len(args) - len(argspec.defaults) + ind defaults[args[iarg]] = default return args, defaults, varpos, varkw
Example #7
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 6 votes |
def run_fypp(): '''Run the Fypp command line tool.''' options = FyppOptions() optparser = get_option_parser() opts, leftover = optparser.parse_args(values=options) infile = leftover[0] if len(leftover) > 0 else '-' outfile = leftover[1] if len(leftover) > 1 else '-' try: tool = Fypp(opts) tool.process_file(infile, outfile) except FyppStopRequest as exc: sys.stderr.write(_formatted_exception(exc)) sys.exit(USER_ERROR_EXIT_CODE) except FyppFatalError as exc: sys.stderr.write(_formatted_exception(exc)) sys.exit(ERROR_EXIT_CODE)
Example #8
Source File: shell.py From unipacker with GNU General Public License v2.0 | 6 votes |
def do_aaa(self, args): """Analyze absolutely all: Show a collection of stats about the current sample""" print(f"{Fore.LIGHTRED_EX}File analysis:{Fore.RESET}") print_cols([ ("YARA:", ", ".join(map(str, self.sample.yara_matches))), ("Chosen unpacker:", self.sample.unpacker.__class__.__name__), ("Allowed sections:", ', '.join(self.sample.unpacker.allowed_sections)), ("End of unpacking stub:", f"0x{self.sample.unpacker.endaddr:02x}" if self.sample.unpacker.endaddr != sys.maxsize else "unknown"), ("Section hopping detection:", "active" if self.sample.unpacker.section_hopping_control else "inactive"), ("Write+Exec detection:", "active" if self.sample.unpacker.write_execute_control else "inactive") ]) print(f"\n{Fore.LIGHTRED_EX}PE stats:{Fore.RESET}") print_cols([ ("Declared virtual memory size:", f"0x{self.sample.virtualmemorysize:02x}", "", ""), ("Actual loaded image size:", f"0x{len(self.sample.loaded_image):02x}", "", ""), ("Image base address:", f"0x{self.sample.BASE_ADDR:02x}", "", ""), ("Mapped stack space:", f"0x{self.engine.STACK_ADDR:02x}", "-", f"0x{self.engine.STACK_ADDR + self.engine.STACK_SIZE:02x}"), ("Mapped hook space:", f"0x{self.engine.HOOK_ADDR:02x}", "-", f"0x{self.engine.HOOK_ADDR + 0x1000:02x}") ]) self.do_i("i") print(f"\n{Fore.LIGHTRED_EX}Register status:{Fore.RESET}") self.do_i("r")
Example #9
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 6 votes |
def __init__(self, maxlen=132, indent=4, method='smart', prefix='&', suffix='&'): # Line length should be long enough that contintuation lines can host at # east one character apart of indentation and two continuation signs minmaxlen = indent + len(prefix) + len(suffix) + 1 if maxlen < minmaxlen: msg = 'Maximal line length less than {0} when using an indentation'\ ' of {1}'.format(minmaxlen, indent) raise FyppFatalError(msg) self._maxlen = maxlen self._indent = indent self._prefix = ' ' * self._indent + prefix self._suffix = suffix if method not in ['brute', 'smart', 'simple']: raise FyppFatalError('invalid folding type') if method == 'brute': self._inherit_indent = False self._fold_position_finder = self._get_maximal_fold_pos elif method == 'simple': self._inherit_indent = True self._fold_position_finder = self._get_maximal_fold_pos elif method == 'smart': self._inherit_indent = True self._fold_position_finder = self._get_smart_fold_pos
Example #10
Source File: test_dynamic.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_modify_builtins_from_leaf_function(self): # Verify that modifications made by leaf functions percolate up the # callstack. with swap_attr(builtins, "len", len): def bar(): builtins.len = lambda x: 4 def foo(modifier): l = [] l.append(len(range(7))) modifier() l.append(len(range(7))) return l self.configure_func(foo, lambda: None) self.assertEqual(foo(bar), [7, 4])
Example #11
Source File: stats.py From Turing with MIT License | 6 votes |
def listfunc(func): def wrapper(*args): if builtins.len(args) == 1: if isinstance(args[0], list): try: return func(args[0]) except: pass try: return func(list(iter(args[0]))) except: pass return func(list(args)) setattr(wrapper, "listfunc", True) return wrapper
Example #12
Source File: sanity.py From reframe with BSD 3-Clause "New" or "Revised" License | 6 votes |
def count(iterable): '''Return the element count of ``iterable``. This is similar to the built-in :func:`len() <python:len>`, except that it can also handle any argument that supports iteration, including generators. ''' try: return builtins.len(iterable) except TypeError: # Try to determine length by iterating over the iterable ret = 0 for ret, _ in builtins.enumerate(iterable, start=1): pass return ret
Example #13
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def _argsplit_fortran(argtxt): txt = _INLINE_EVAL_REGION_REGEXP.sub(_blank_match, argtxt) splitpos = [-1] quote = None closing_brace_stack = [] closing_brace = None for ind, char in enumerate(txt): if quote: if char == quote: quote = None continue if char in _QUOTES_FORTRAN: quote = char continue if char in _OPENING_BRACKETS_FORTRAN: closing_brace_stack.append(closing_brace) ind = _OPENING_BRACKETS_FORTRAN.index(char) closing_brace = _CLOSING_BRACKETS_FORTRAN[ind] continue if char in _CLOSING_BRACKETS_FORTRAN: if char == closing_brace: closing_brace = closing_brace_stack.pop(-1) continue else: msg = "unexpected closing delimiter '{0}' in expression '{1}' "\ "at position {2}".format(char, argtxt, ind + 1) raise FyppFatalError(msg) if not closing_brace and char == _ARGUMENT_SPLIT_CHAR_FORTRAN: splitpos.append(ind) if quote or closing_brace: msg = "open quotes or brackets in expression '{0}'".format(argtxt) raise FyppFatalError(msg) splitpos.append(len(txt)) fragments = [argtxt[start + 1 : end] for start, end in zip(splitpos, splitpos[1:])] return fragments
Example #14
Source File: test_functools.py From android_universal with MIT License | 5 votes |
def test_lru_reentrancy_with_len(self): # Test to make sure the LRU cache code isn't thrown-off by # caching the built-in len() function. Since len() can be # cached, we shouldn't use it inside the lru code itself. old_len = builtins.len try: builtins.len = self.module.lru_cache(4)(len) for i in [0, 0, 1, 2, 3, 3, 4, 5, 6, 1, 7, 2, 1]: self.assertEqual(len('abcdefghijklmn'[:i]), i) finally: builtins.len = old_len
Example #15
Source File: test_typing_annotations.py From typecheck-decorator with BSD 2-Clause "Simplified" License | 5 votes |
def test_Callable_OK(): # TODO: What's going wrong here? assert callable(foo_Callable) # Not even one of the following works: foo_Callable(lambda: foo_Callable) foo_Callable(lambda x: 2*x) foo_Callable(builtins.callable) foo_Callable(builtins.dict) foo_Callable(builtins.len) foo_Callable(foo_Callable) ############################################################################ # _Protocol # is handled by TypeChecker without special code, so we do not test them all
Example #16
Source File: test_typing_annotations.py From typecheck-decorator with BSD 2-Clause "Simplified" License | 5 votes |
def foo_KeysView_to_Sequence(v: tg.KeysView[Tc]) -> tg.Sequence[Tc]: result = [item for item in v] result.sort() assert len([item for item in v]) == len(result) # iterable not exhausted return result
Example #17
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def handle_endcall(self, span, name, blockcall): '''Should be called to signalize an endcall directive. Args: span (tuple of int): Start and end line of the directive. name (str): Name of the endcall statement. Could be None, if endcall was specified without name. blockcall (bool): Whether the alternative "block / contains / endblock" calling directive has been used. ''' self._check_for_open_block(span, 'endcall') block = self._open_blocks.pop(-1) directive, fname, spans = block[0:3] callname, callargexpr, args, argnames = block[3:7] if blockcall: opened, current = 'block', 'endblock' else: opened, current = 'call', 'endcall' self._check_if_matches_last(directive, opened, spans[0], span, current) if name is not None and name != callname: msg = "wrong name in {0} directive "\ "(expected '{1}', got '{2}')".format(current, callname, name) raise FyppFatalError(msg, fname, span) args.append(self._curnode) # If nextarg or endcall immediately followed call, then first argument # is empty and should be removed (to allow for calls without arguments # and named first argument in calls) if args and not args[0]: if len(argnames) == len(args): del argnames[0] del args[0] del spans[1] spans.append(span) block = (directive, fname, spans, callname, callargexpr, args, argnames) self._curnode = self._path.pop(-1) self._curnode.append(block)
Example #18
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def _split_line(line, maxlen, prefix, suffix, fold_position_finder): # length of continuation lines with 1 or two continuation chars. maxlen1 = maxlen - len(prefix) maxlen2 = maxlen1 - len(suffix) start = 0 end = fold_position_finder(line, start, maxlen - len(suffix)) result = [line[start:end] + suffix] while end < len(line) - maxlen1: start = end end = fold_position_finder(line, start, start + maxlen2) result.append(prefix + line[start:end] + suffix) result.append(prefix + line[end:]) return result
Example #19
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def _apply_definitions(defines, evaluator): for define in defines: words = define.split('=', 2) name = words[0] value = None if len(words) > 1: try: value = evaluator.evaluate(words[1]) except Exception as exc: msg = "exception at evaluating '{0}' in definition for " \ "'{1}'".format(words[1], name) raise FyppFatalError(msg, cause=exc) evaluator.define(name, value)
Example #20
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def _find_next_eol(output, ind): 'Find last newline before current position.' # find first eol after expr. evaluation inext = ind + 1 while inext < len(output): eolnext = output[inext].find('\n') if eolnext != -1: break inext += 1 else: inext = len(output) - 1 eolnext = len(output[-1]) - 1 return inext, eolnext
Example #21
Source File: test_functools.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_lru_reentrancy_with_len(self): # Test to make sure the LRU cache code isn't thrown-off by # caching the built-in len() function. Since len() can be # cached, we shouldn't use it inside the lru code itself. old_len = builtins.len try: builtins.len = self.module.lru_cache(4)(len) for i in [0, 0, 1, 2, 3, 3, 4, 5, 6, 1, 7, 2, 1]: self.assertEqual(len('abcdefghijklmn'[:i]), i) finally: builtins.len = old_len
Example #22
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def _process_arguments(self, args, keywords): kwdict = dict(keywords) argdict = {} nargs = min(len(args), len(self._argnames)) for iarg in range(nargs): argdict[self._argnames[iarg]] = args[iarg] if nargs < len(args): if self._varpos is None: msg = "macro '{0}' called with too many positional arguments "\ "(expected: {1}, received: {2})"\ .format(self._name, len(self._argnames), len(args)) raise FyppFatalError(msg, self._fname, self._spans[0]) else: argdict[self._varpos] = list(args[nargs:]) elif self._varpos is not None: argdict[self._varpos] = [] for argname in self._argnames[:nargs]: if argname in kwdict: msg = "got multiple values for argument '{0}'".format(argname) raise FyppFatalError(msg, self._fname, self._spans[0]) if nargs < len(self._argnames): for argname in self._argnames[nargs:]: if argname in kwdict: argdict[argname] = kwdict.pop(argname) elif argname in self._defaults: argdict[argname] = self._defaults[argname] else: msg = "macro '{0}' called without mandatory positional "\ "argument '{1}'".format(self._name, argname) raise FyppFatalError(msg, self._fname, self._spans[0]) if kwdict and self._varkw is None: kwstr = "', '".join(kwdict.keys()) msg = "macro '{0}' called with unknown keyword argument(s) '{1}'"\ .format(self._name, kwstr) raise FyppFatalError(msg, self._fname, self._spans[0]) if self._varkw is not None: argdict[self._varkw] = kwdict return argdict
Example #23
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def _check_for_open_block(self, span, directive): if len(self._open_blocks) <= self._nr_prev_blocks[-1]: msg = 'unexpected {0} directive'.format(directive) raise FyppFatalError(msg, self._curfile, span)
Example #24
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def _get_call_arguments(self, fname, spans, argexpr, contents, argnames): if argexpr is None: posargs = [] kwargs = {} else: # Parse and evaluate arguments passed in call header self._evaluator.openscope() try: posargs, kwargs = self._evaluate( '__getargvalues(' + argexpr + ')', fname, spans[0][0]) except Exception as exc: msg = "unable to parse argument expression '{0}'"\ .format(argexpr) raise FyppFatalError(msg, fname, spans[0], exc) self._evaluator.closescope() # Render arguments passed in call body args = [] for content in contents: self._evaluator.openscope() rendered = self.render(content, divert=True) self._evaluator.closescope() if rendered.endswith('\n'): rendered = rendered[:-1] args.append(rendered) # Separate arguments in call body into positional and keyword ones: if argnames: posargs += args[:len(args) - len(argnames)] offset = len(args) - len(argnames) for iargname, argname in enumerate(argnames): ind = offset + iargname if argname in kwargs: msg = "keyword argument '{0}' already defined"\ .format(argname) raise FyppFatalError(msg, fname, spans[ind + 1]) kwargs[argname] = args[ind] else: posargs += args return posargs, kwargs
Example #25
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def handle_endinclude(self, span, fname): '''Should be called when processing of a file finished. Args: span (tuple of int): Start and end line of the include directive or None if called the first time for the main input. fname (str): Name of the file which has been included. ''' nprev_blocks = self._nr_prev_blocks.pop(-1) if len(self._open_blocks) > nprev_blocks: directive, fname, spans = self._open_blocks[-1][0:3] msg = '{0} directive still unclosed when reaching end of file'\ .format(directive) raise FyppFatalError(msg, self._curfile, spans[0]) block = self._open_blocks.pop(-1) directive, blockfname, spans = block[0:3] if directive != 'include': msg = 'internal error: last open block is not \'include\' when '\ 'closing file \'{0}\''.format(fname) raise FyppFatalError(msg) if span != spans[0]: msg = 'internal error: span for include and endinclude differ ('\ '{0} vs {1}'.format(span, spans[0]) raise FyppFatalError(msg) oldfname, _ = block[3:5] if fname != oldfname: msg = 'internal error: mismatching file name in close_file event'\ " (expected: '{0}', got: '{1}')".format(oldfname, fname) raise FyppFatalError(msg, fname) block = directive, blockfname, spans, fname, self._curnode self._curnode = self._path.pop(-1) self._curnode.append(block) self._curfile = blockfname
Example #26
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def handle_include(self, span, fname): '''Should be called to signalize change to new file. Args: span (tuple of int): Start and end line of the include directive or None if called the first time for the main input. fname (str): Name of the file to be included. ''' self._path.append(self._curnode) self._curnode = [] self._open_blocks.append( ('include', self._curfile, [span], fname, None)) self._curfile = fname self._nr_prev_blocks.append(len(self._open_blocks))
Example #27
Source File: test_dynamic.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_eval_gives_lambda_custom_globals(self): globals_dict = {"len": lambda x: 7} foo = eval("lambda: len([])", globals_dict) self.configure_func(foo) self.assertEqual(foo(), 7)
Example #28
Source File: test_dynamic.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_cannot_replace_builtins_dict_between_calls(self): def foo(): return len([1, 2, 3]) self.configure_func(foo) self.assertEqual(foo(), 3) with swap_item(globals(), "__builtins__", {"len": lambda x: 7}): self.assertEqual(foo(), 3)
Example #29
Source File: test_dynamic.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_cannot_replace_builtins_dict_while_active(self): def foo(): x = range(3) yield len(x) yield len(x) self.configure_func(foo) g = foo() self.assertEqual(next(g), 3) with swap_item(globals(), "__builtins__", {"len": lambda x: 7}): self.assertEqual(next(g), 3)
Example #30
Source File: test_dynamic.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_cannot_change_globals_or_builtins_with_eval(self): def foo(): return len([1, 2, 3]) self.configure_func(foo) # Note that this *doesn't* change the definition of len() seen by foo(). builtins_dict = {"len": lambda x: 7} globals_dict = {"foo": foo, "__builtins__": builtins_dict, "len": lambda x: 8} self.assertEqual(eval("foo()", globals_dict), 3) self.assertEqual(eval("foo()", {"foo": foo}), 3)