Python __builtin__.len() Examples
The following are 29
code examples of __builtin__.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
__builtin__
, or try the search function
.
Example #1
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 #2
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 #3
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 #4
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 #5
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def _get_iterated_content(self, fname, spans, loopvars, loopiter, content): out = [] ieval = [] peval = [] try: iterobj = iter(self._evaluate(loopiter, fname, spans[0][0])) except Exception as exc: msg = "exception occurred when evaluating '{0}'"\ .format(loopiter) raise FyppFatalError(msg, fname, spans[0], exc) multiline = (spans[0][0] != spans[-1][1]) for var in iterobj: if len(loopvars) == 1: self._define(loopvars[0], var) else: for varname, value in zip(loopvars, var): self._define(varname, value) if self._linenums and not self._diverted and multiline: out.append(linenumdir(spans[0][1], fname)) outcont, ievalcont, pevalcont = self._render(content) ieval += _shiftinds(ievalcont, len(out)) peval += pevalcont out += outcont if self._linenums and not self._diverted and multiline: out.append(linenumdir(spans[1][1], fname)) return out, ieval, peval
Example #6
Source File: test_iterlen.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def len(obj): try: return _len(obj) except TypeError: try: # note: this is an internal undocumented API, # don't rely on it in your own programs return obj.__length_hint__() except AttributeError: raise TypeError
Example #7
Source File: test_iterlen.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def len(obj): try: return _len(obj) except TypeError: try: # note: this is an internal undocumented API, # don't rely on it in your own programs return obj.__length_hint__() except AttributeError: raise TypeError
Example #8
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 #9
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 #10
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 #11
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 #12
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def _func_setvar(self, *namesvalues): if len(namesvalues) % 2: msg = 'setvar function needs an even number of arguments' raise FyppFatalError(msg) for ind in range(0, len(namesvalues), 2): self.define(namesvalues[ind], namesvalues[ind + 1])
Example #13
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def define(self, name, value): '''Define a Python entity. Args: name (str): Name of the entity. value (Python object): Value of the entity. Raises: FyppFatalError: If name starts with the reserved prefix or if it is a reserved name. ''' varnames = self._get_variable_names(name) if len(varnames) == 1: value = (value,) elif len(varnames) != len(value): msg = 'value for tuple assignment has incompatible length' raise FyppFatalError(msg) for varname, varvalue in zip(varnames, value): self._check_variable_name(varname) if self._locals is None: self._globals[varname] = varvalue else: if varname in self._globalrefs: self._globals[varname] = varvalue else: self._locals[varname] = varvalue self._scope[varname] = varvalue
Example #14
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 #15
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 5 votes |
def _get_included_content(self, fname, spans, includefname, content): includefile = spans[0] is not None out = [] if self._linenums and not self._diverted: if includefile or self._linenum_gfortran5: out += linenumdir(0, includefname, _LINENUM_NEW_FILE) else: out += linenumdir(0, includefname) outcont, ieval, peval = self._render(content) ieval = _shiftinds(ieval, len(out)) out += outcont if self._linenums and not self._diverted and includefile: out += linenumdir(spans[0][1], fname, _LINENUM_RETURN_TO_FILE) return out, ieval, peval
Example #16
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 #17
Source File: test_iterlen.py From ironpython2 with Apache License 2.0 | 5 votes |
def len(obj): try: return _len(obj) except TypeError: try: # note: this is an internal undocumented API, # don't rely on it in your own programs return obj.__length_hint__() except AttributeError: raise TypeError
Example #18
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 #19
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 #20
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 #21
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 #22
Source File: test_iterlen.py From medicare-demo with Apache License 2.0 | 5 votes |
def len(obj): try: return _len(obj) except TypeError: try: # note: this is an internal undocumented API, # don't rely on it in your own programs return obj.__length_hint__() except AttributeError: raise TypeError
Example #23
Source File: test_iterlen.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def len(obj): try: return _len(obj) except TypeError: try: # note: this is an internal undocumented API, # don't rely on it in your own programs return obj.__length_hint__() except AttributeError: raise TypeError
Example #24
Source File: solveCrossTime.py From TICC with BSD 2-Clause "Simplified" License | 5 votes |
def ADMM_x(entry): global rho variables = entry[X_VARS] #-----------------------Proximal operator --------------------------- x_update = [] # proximal update for the variable x if(__builtin__.len(entry[1].args) > 1 ): # print 'we are in logdet + trace node' cvxpyMat = entry[1].args[1].args[0].args[0] numpymat = cvxpyMat.value mat_shape = ( int( numpymat.shape[1] * ( numpymat.shape[1]+1 )/2.0 ) ,) a = numpy.zeros(mat_shape) for i in xrange(entry[X_DEG]): z_index = X_NEIGHBORS + (2 * i) u_index = z_index + 1 zi = entry[z_index] ui = entry[u_index] for (varID, varName, var, offset) in variables: z = getValue(edge_z_vals, zi + offset, var.size[0]) u = getValue(edge_u_vals, ui + offset, var.size[0]) a += (z-u) A = upper2Full(a) A = A/entry[X_DEG] eta = 1/float(rho) x_update = Prox_logdet(numpymat, A, eta) solution = numpy.array(x_update).T.reshape(-1) writeValue(node_vals, entry[X_IND] + variables[0][3], solution, variables[0][2].size[0]) else: x_update = [] # no variable to update for dummy node return None # z-update for ADMM for one edge
Example #25
Source File: solveCrossTime.py From TICC with BSD 2-Clause "Simplified" License | 5 votes |
def __UpdateAllVariables(self, NId, Objective): if NId in self.node_objectives: # First, remove the Variables from the old Objective. old_obj = self.node_objectives[NId] self.all_variables = self.all_variables - set(old_obj.variables()) # Check that the Variables of the new Objective are not currently # in other Objectives. new_variables = set(Objective.variables()) if __builtin__.len(self.all_variables.intersection(new_variables)) != 0: raise Exception('Objective at NId %d shares a variable.' % NId) self.all_variables = self.all_variables | new_variables # Helper method to get CVXPY Variables out of a CVXPY Objective
Example #26
Source File: test_iterlen.py From oss-ftp with MIT License | 5 votes |
def len(obj): try: return _len(obj) except TypeError: try: # note: this is an internal undocumented API, # don't rely on it in your own programs return obj.__length_hint__() except AttributeError: raise TypeError
Example #27
Source File: test_iterlen.py From BinderFilter with MIT License | 5 votes |
def len(obj): try: return _len(obj) except TypeError: try: # note: this is an internal undocumented API, # don't rely on it in your own programs return obj.__length_hint__() except AttributeError: raise TypeError
Example #28
Source File: fypp.py From fypp with BSD 2-Clause "Simplified" License | 4 votes |
def _parse(self, txt, linenr=0, directcall=False): pos = 0 for match in _ALL_DIRECTIVES_REGEXP.finditer(txt): start, end = match.span() if start > pos: endlinenr = linenr + txt.count('\n', pos, start) self._process_text(txt[pos:start], (linenr, endlinenr)) linenr = endlinenr endlinenr = linenr + txt.count('\n', start, end) span = (linenr, endlinenr) ldirtype, ldir, idirtype, idir = match.groups() if directcall and (idirtype is None or idirtype != '$'): msg = 'only inline eval directives allowed in direct calls' raise FyppFatalError(msg, self._curfile, span) elif idirtype is not None: if idir is None: msg = 'missing inline directive content' raise FyppFatalError(msg, self._curfile, span) dirtype = idirtype content = idir elif ldirtype is not None: if ldir is None: msg = 'missing line directive content' raise FyppFatalError(msg, self._curfile, span) dirtype = ldirtype content = _CONTLINE_REGEXP.sub('', ldir) else: # Comment directive dirtype = None if dirtype == '$': self.handle_eval(span, content) elif dirtype == '#': self._process_control_dir(content, span) elif dirtype == '@': self._process_direct_call(content, span) else: self.handle_comment(span) pos = end linenr = endlinenr if pos < len(txt): endlinenr = linenr + txt.count('\n', pos) self._process_text(txt[pos:], (linenr, endlinenr))
Example #29
Source File: solveCrossTime.py From TICC with BSD 2-Clause "Simplified" License | 4 votes |
def Solve(self, M=Minimize, UseADMM=True, NumProcessors=0, Rho=1.0, MaxIters=250, EpsAbs=0.01, EpsRel=0.01, Verbose=False, UseClustering = False, ClusterSize = 1000 ): global m_func m_func = M # Use ADMM if the appropriate parameter is specified and if there # are edges in the graph. #if __builtin__.len(SuperNodes) > 0: if UseClustering and ClusterSize > 0: SuperNodes = self.__ClusterGraph(ClusterSize) self.__SolveClusterADMM(M,UseADMM,SuperNodes, NumProcessors, Rho, MaxIters,\ EpsAbs, EpsRel, Verbose) return if UseADMM and self.GetEdges() != 0: self.__SolveADMM(NumProcessors, Rho, MaxIters, EpsAbs, EpsRel, Verbose) return if Verbose: print 'Serial ADMM' objective = 0 constraints = [] # Add all node objectives and constraints for ni in self.Nodes(): nid = ni.GetId() objective += self.node_objectives[nid] constraints += self.node_constraints[nid] # Add all edge objectives and constraints for ei in self.Edges(): etup = self.__GetEdgeTup(ei.GetSrcNId(), ei.GetDstNId()) objective += self.edge_objectives[etup] constraints += self.edge_constraints[etup] # Solve CVXPY Problem objective = m_func(objective) problem = Problem(objective, constraints) try: problem.solve() except SolverError: problem.solve(solver=SCS) if problem.status in [INFEASIBLE_INACCURATE, UNBOUNDED_INACCURATE]: problem.solve(solver=SCS) # Set TGraphVX status and value to match CVXPY self.status = problem.status self.value = problem.value # Insert into hash to support ADMM structures and GetNodeValue() for ni in self.Nodes(): nid = ni.GetId() variables = self.node_variables[nid] value = None for (varID, varName, var, offset) in variables: if var.size[0] == 1: val = numpy.array([var.value]) else: val = numpy.array(var.value).reshape(-1,) if value is None: value = val else: value = numpy.concatenate((value, val)) self.node_values[nid] = value