本文整理汇总了Python中utility.flatten函数的典型用法代码示例。如果您正苦于以下问题:Python flatten函数的具体用法?Python flatten怎么用?Python flatten使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了flatten函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _Procedure
def _Procedure(self, ast):
con = self.context
# Create a new type variable for the return type of the procedure
restype = con.fresh_typevar()
restype.source = None
# Get the names and type variables for the formal parameters
argnames = [arg.id for arg in flatten(ast.formals())]
argtypes = [arg.type for arg in flatten(ast.formals())]
# Make the definition of this function visible within its body
# to allow for recursive calls
con.typings[ast.name().id] = ast.name().type
con.begin_scope()
# Make the formals visible
con.typings.update(dict(zip(argnames, argtypes)))
prior_monomorphs = con.monomorphs
con.monomorphs = prior_monomorphs | set(argtypes)
argtypeit = iter(argtypes)
# Construct the formals types
def make_type(formal):
if hasattr(formal, '__iter__'):
return T.Tuple(*[make_type(x) for x in iter(formal)])
else:
return argtypeit.next()
formals_types = make_type(ast.formals())
# XXX This makes the restriction that recursive invocations of F
# in the body of F must have the same type signature. Probably
# not strictly necessary, but allowing the more general case
# might prove problematic, especially in tail recursive cases.
# For now, we will forbid this.
con.monomorphs.add(ast.name().type)
# Produce all constraints for arguments
# Tuple arguments, for example, will produce constraints
for a in ast.formals():
for c in self.visit(a): yield c
# Produce all the constraints for the body
for c in self.visit_block(ast, restype, ast.body()): yield c
con.monomorphs = prior_monomorphs
M = set(self.context.monomorphs)
yield Generalizing(ast.name().type,
T.Fn(formals_types, restype),
M,
ast)
con.end_scope()
开发者ID:99plus2,项目名称:copperhead,代码行数:59,代码来源:typeinference.py
示例2: _Bind
def _Bind(self, bind):
destination = bind.binder()
self.box = False
self.scalar = False
self.rewrite_children(bind)
if self.box:
self.box_results.update((str(x) for x in flatten(destination)))
if self.scalar:
for dest in flatten(destination):
self.scalar_results[str(dest)] = bind.value()
return bind
开发者ID:WuCPMark,项目名称:TFJ,代码行数:11,代码来源:midrewrites.py
示例3: _Procedure
def _Procedure(self, proc):
self.rewrite_children(proc)
proc.parameters = list(flatten(proc.parameters))
procedureName = proc.variables[0].id
self.procedures[procedureName] = proc
return proc
开发者ID:zahangircse,项目名称:copperhead,代码行数:7,代码来源:rewrites.py
示例4: _Procedure
def _Procedure(self, proc):
self.currentProcedureName = proc.variables[0].id
self.body = []
self.currentBin = self.body
self.tailRecursive = False
self.rewrite_children(proc)
if not self.tailRecursive:
return proc
def convertRecursion(stmt):
if not isinstance(stmt, S.Bind):
return stmt
if not isinstance(stmt.id, S.Name):
return stmt
if stmt.id.id == C.anonymousReturnValue.id:
arguments = proc.variables[1:]
apply = stmt.parameters[0]
assert apply.parameters[0].id == self.currentProcedureName
newArguments = apply.parameters[1:]
return [S.Bind(x, y) for x, y in zip(arguments, newArguments)]
else:
return stmt
recursiveBranch = list(flatten([convertRecursion(x) for x in self.recursiveBranch]))
recursiveBranch = filter(lambda x: isinstance(x, S.Bind), recursiveBranch)
whileLoop = M.While(self.condition, [recursiveBranch + self.body])
cleanup = self.nonrecursiveBranch
proc.parameters = self.body + [whileLoop] + cleanup
return proc
开发者ID:WuCPMark,项目名称:TFJ,代码行数:27,代码来源:midrewrites.py
示例5: _Return
def _Return(self, stmt):
e = self.rewrite(stmt.value())
if isinstance(e, S.Name):
# If we're returning one of the procedure formals unchanged,
# we need to copy its value into a return variable.
# Here is where we check:
if e.id not in self.formals:
#No need to copy value into a return variable
stmt.parameters = [e]
self.emit(stmt)
return
if isinstance(e, S.Tuple):
# If we're returning any of the procedure formals unchanged,
# we need to copy their value into a return variable
# Here is where we check:
formals = reduce(lambda a, b: a or b, \
(ei.id in self.formals for ei in flatten(e)))
if not formals:
stmt.parameters = [e]
self.emit(stmt)
return
ret = S.Name(anonymousReturnValue.id)
self.emit(S.Bind(ret, e))
stmt.parameters = [ret]
self.emit(stmt)
开发者ID:WuCPMark,项目名称:TFJ,代码行数:25,代码来源:rewrites.py
示例6: gather
def gather(self, suite):
self.sources = []
self.gathered = set()
for stmt in suite:
self.clean.append(self.rewrite(stmt))
while self.sources:
stmt = self.sources.pop(0)
self.clean.insert(0, self.rewrite(stmt))
return list(flatten(self.clean))
开发者ID:zahangircse,项目名称:copperhead,代码行数:9,代码来源:rewrites.py
示例7: _Bind
def _Bind(self, bind):
destination = bind.binder()
if isinstance(destination, S.Tuple):
for dest in flatten(destination):
self.env[dest.id] = dest.id
else:
id = destination.id
self.env[id] = id
self.rewrite_children(bind)
return bind
开发者ID:zahangircse,项目名称:copperhead,代码行数:10,代码来源:rewrites.py
示例8: _deliver
def _deliver(self, server, msg, tracker):
from_addr, to_addrs = self.__extract_addrs(msg)
flattened_msg = flatten(msg)
failures = {}
try:
tracker.update(_('Delivering %s' % msg['Subject']))
failures = server.sendmail(from_addr, to_addrs, flattened_msg)
except:
tracker.error_delivering(msg)
return None
return failures
开发者ID:FOSSRIT,项目名称:sweetermail,代码行数:11,代码来源:smtp.py
示例9: _Procedure
def _Procedure(self, proc):
self.locals.begin_scope()
for x in proc.formals():
#Tuples may be arguments to procedures
#Mark all ids found in each formal argument
for y in flatten(x):
self.locals[y.id] = True
self.rewrite_children(proc)
self.locals.end_scope()
proc.variables = map(S.mark_user, proc.variables)
return proc
开发者ID:adityaatluri,项目名称:copperhead,代码行数:11,代码来源:rewrites.py
示例10: _Bind
def _Bind(self, stmt):
var = stmt.binder()
varNames = [x.id for x in flatten(var)]
operation = S.substituted_expression(stmt.value(), self.env)
for name in varNames:
if name not in self.exceptions:
rename = '%s_%s' % (name, self.serial.next())
else:
rename = name
self.env[name] = S.Name(rename)
result = S.Bind(S.substituted_expression(var, self.env), operation)
return result
开发者ID:adityaatluri,项目名称:copperhead,代码行数:13,代码来源:rewrites.py
示例11: _Apply
def _Apply(self, apply):
for arg in apply.arguments():
if isinstance(arg, S.Name):
if arg.id not in self.declarations:
self.declarations[arg.id] = PT.none
def name_filter(x):
if isinstance(x, S.Name):
return self.declarations[x.id]
else:
return PT.total
completions = [name_filter(x) for x in apply.arguments()]
fn_name = apply.function().id
fn_phase = apply.function().cu_phase
input_phases, output_completion = \
fn_phase(*completions)
for name in flatten(self.destination):
self.declarations[name.id] = output_completion
sync = []
if hasattr(apply.function(), 'box'):
self.box = True
for x in apply.arguments():
# XXX Special case for dealing with zip -
# The unzip transform pushes tuple args around the AST
# This needs to be rethought
# Right now it will only work for zip as argument to Box
# functions.
# The right thing to do is generate host code for this
# But we need to transition the entire entry-point procedure
# to C++ on the host in order to do this.
if hasattr(x, '__iter__'):
for xi in x:
sync.append(xi.id)
else:
sync.append(x.id)
else:
for x, y in zip(apply.arguments(), input_phases):
if isinstance(x, S.Name):
x_phase = self.declarations[x.id]
if x_phase is PT.unknown:
self.declarations[x.id] = y
elif x_phase < y:
sync.append(x.id)
if sync:
self.sync = M.PhaseBoundary(sync)
return apply
开发者ID:WuCPMark,项目名称:TFJ,代码行数:51,代码来源:midrewrites.py
示例12: collect_local_typings
def collect_local_typings(suite, M):
"""
This compiler pass may be used to annotate every Procedure node with
a list of typings for its locally declared variables. This pass
implicitly assumes that the program has already been run through all
single assignment, typing, and flattening phases of the frontend
compiler.
"""
def select(A, kind):
return ifilter(lambda x: isinstance(x, kind), A)
M.fn_types = dict()
for fn in select(suite, AST.Procedure):
fntype = fn.name().type
fnname = str(fn.name())
if fnname in M.inputTypes:
M.fn_types[fnname] = fntype
if isinstance(fntype, T.Polytype):
fntype = fntype.monotype()
input_type_tuple = fntype.parameters[0]
input_types = input_type_tuple.parameters
localtypes = dict(zip((x.id for x in flatten(fn.formals())), flatten(input_types)))
def record_bindings(body):
for binding in select(body, AST.Bind):
for x in select(AST.walk(binding.binder()), AST.Name):
localtypes[x.id] = x.type
for cond in select(body, AST.Cond):
for cbody in cond.parameters[1:]:
record_bindings(cbody)
record_bindings(fn.body())
fn.typings = localtypes
return suite
开发者ID:dsheffie,项目名称:TFJ,代码行数:37,代码来源:typeinference.py
示例13: _Bind
def _Bind(self, stmt):
var = stmt.binder()
varNames = [x.id for x in flatten(var)]
operation = S.substituted_expression(stmt.value(), self.env)
for name in varNames:
if self.freeze:
if name in self.env:
rename = self.env[name]
elif name not in self.exceptions:
rename = markGenerated('%s_%s' % (name, SingleAssignmentRewrite.serial.next()))
else:
rename = name
elif name not in self.exceptions:
rename = markGenerated('%s_%s' % (name, SingleAssignmentRewrite.serial.next()))
else:
rename = name
self.env[name] = S.Name(rename)
result = S.Bind(S.substituted_expression(var, self.env), operation)
return result
开发者ID:WuCPMark,项目名称:TFJ,代码行数:20,代码来源:rewrites.py
示例14: _Lambda
def _Lambda(self, e):
_ClosureRecursion._Lambda(self, e)
formals = [v.id for v in flatten(e.formals())]
# Take the free variable list, stick it in a set to make sure we don't
# duplicate a variable, and then put it back in a list to make sure
# it's got a defined ordering, which sets don't have
free = list(set([v for v in S.free_variables(e.body(), formals)
if v in self.env]))
if free:
bound = [S.Name("_K%d" % i) for i in range(len(free))]
body = S.substituted_expression(e.body(), dict(zip(free, bound)))
e.parameters = [body]
e.variables = e.variables + bound
return S.Closure([S.Name(x) for x in free], e)
else:
return e
开发者ID:zahangircse,项目名称:copperhead,代码行数:20,代码来源:rewrites.py
示例15: _Bind
def _Bind(self, x):
names = U.flatten(x.binder())
result = list(self.visit(x.value()))
for name in names:
self.env[name.id] = name.id
return result
开发者ID:zahangircse,项目名称:copperhead,代码行数:6,代码来源:coresyntax.py
示例16: _Apply
def _Apply(self, appl):
fn = appl.function()
if isinstance(fn, S.Closure):
fn_name = fn.body().id
else:
fn_name = fn.id
if fn_name in self.procedures:
instantiatedFunction = self.procedures[fn_name]
functionArguments = instantiatedFunction.variables[1:]
instantiatedArguments = appl.parameters[1:]
if isinstance(fn, S.Closure):
instantiatedArguments.extend(fn.variables)
env = pltools.Environment()
for (internal, external) in zip(functionArguments, instantiatedArguments):
env[internal.id] = external
return_finder = ReturnFinder()
return_finder.visit(instantiatedFunction)
#XXX HACK. Need to do conditional statement->expression conversion
# In order to make inlining possible
if return_finder.return_in_conditional:
return appl
env[return_finder.return_value.id] = self.activeBinding
statements = filter(lambda x: not isinstance(x, S.Return),
instantiatedFunction.body())
statements = [S.substituted_expression(x, env) for x in \
statements]
singleAssignmentInstantiation = single_assignment_conversion(statements, exceptions=set((x.id for x in flatten(self.activeBinding))), M=self.M)
self.statements = singleAssignmentInstantiation
return None
return appl
开发者ID:adityaatluri,项目名称:copperhead,代码行数:30,代码来源:rewrites.py
示例17: inline
def inline(s):
inliner = FunctionInliner()
return list(flatten(inliner.rewrite(s)))
开发者ID:zahangircse,项目名称:copperhead,代码行数:3,代码来源:rewrites.py
示例18: _Cond
def _Cond(self, cond):
body = list(flatten(self.rewrite(cond.body())))
orelse = list(flatten(self.rewrite(cond.orelse())))
return S.Cond(cond.test(), body, orelse)
开发者ID:adityaatluri,项目名称:copperhead,代码行数:4,代码来源:rewrites.py
示例19: _Apply
def _Apply(self, apply):
functionName = apply.parameters[0].id
if functionName in self.procedures:
instantiatedFunction = self.procedures[functionName]
functionArguments = instantiatedFunction.variables[1:]
instantiatedArguments = apply.parameters[1:]
env = pltools.Environment()
for (internal, external) in zip(functionArguments, instantiatedArguments):
env[internal.id] = external
return_finder = ReturnFinder(self.activeBinding, env)
return_finder.visit(instantiatedFunction)
statements = [S.substituted_expression(x, env) for x in \
instantiatedFunction.body() \
if not isinstance(x, S.Return)]
singleAssignmentInstantiation = single_assignment_conversion(statements, exceptions=set((x.id for x in flatten(self.activeBinding))))
self.statements = singleAssignmentInstantiation
return None
return apply
开发者ID:zahangircse,项目名称:copperhead,代码行数:19,代码来源:rewrites.py
示例20: _Return
def _Return(self, node):
val = list(flatten(node.value()))
assert(len(val) == len(self.binding))
for b, v in zip(self.binding, val):
self.env[v.id] = b
开发者ID:zahangircse,项目名称:copperhead,代码行数:5,代码来源:rewrites.py
注:本文中的utility.flatten函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论