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 occured 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
python类len()的实例源码
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
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)
def _get_callable_argspec_py2(func):
argspec = inspect.getargspec(func)
if argspec.keywords is not None:
msg = "variable length keyword argument '{0}' found"\
.format(argspec.keywords)
raise FyppFatalError(msg)
vararg = argspec.varargs
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, vararg
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
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
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))
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
def handle_endcall(self, span, name):
'''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.
'''
self._check_for_open_block(span, 'endcall')
block = self._open_blocks.pop(-1)
directive, fname, spans = block[0:3]
self._check_if_matches_last(directive, 'call', spans[0], span,
'endcall')
callname, callargexpr, args, argnames = block[3:7]
if name is not None and name != callname:
msg = "wrong name in endcall directive "\
"(expected '{0}', got '{1}')".format(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)
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)
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 occured 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
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
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
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
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
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])
def _process_arguments(self, args, 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._varargs 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._varargs] = tuple(args[nargs:])
elif self._varargs is not None:
argdict[self._varargs] = ()
for argname in self._argnames[:nargs]:
if argname in keywords:
msg = "got multiple values for argument '{0}'".format(argname)
raise FyppFatalError(msg, self._fname, self._spans[0])
if self._varargs is not None and self._varargs in keywords:
msg = "got unexpected keyword argument '{0}'".format(self._varargs)
raise FyppFatalError(msg, self._fname, self._spans[0])
argdict.update(keywords)
if nargs < len(self._argnames):
for argname in self._argnames[nargs:]:
if argname in argdict:
pass
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])
return argdict
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)
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
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
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
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
def hex_to_rgb(value):
"""Return (red, green, blue) for the color given as #rrggbb."""
lv = bt.len(value)
out = tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
out = tuple([x/256.0 for x in out])
return out
def compute_confusion_matrix(num_clusters,clustered_points_algo, sorted_indices_algo):
"""
computes a confusion matrix and returns it
"""
seg_len = 400
true_confusion_matrix = np.zeros([num_clusters,num_clusters])
for point in xrange(bt.len(clustered_points_algo)):
cluster = clustered_points_algo[point]
num = (int(sorted_indices_algo[point]/seg_len) %num_clusters)
true_confusion_matrix[int(num),int(cluster)] += 1
return true_confusion_matrix
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
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
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
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
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
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