当前位置: 首页>>代码示例>>Python>>正文


Python ChainMap.update方法代码示例

本文整理汇总了Python中collections.ChainMap.update方法的典型用法代码示例。如果您正苦于以下问题:Python ChainMap.update方法的具体用法?Python ChainMap.update怎么用?Python ChainMap.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在collections.ChainMap的用法示例。


在下文中一共展示了ChainMap.update方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: vars

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import update [as 别名]
import os
import argparse
from collections import ChainMap

defaults = {'SHELL': '/bin/bash'}

parser = argparse.ArgumentParser()
parser.add_argument('-s', '--shell')
namespace = parser.parse_args([])
command_line_args = {k:v for k,v in vars(namespace).items() if v}


d = ChainMap(command_line_args, os.environ, defaults)
d.update(command_line_args)
开发者ID:alexandre-k,项目名称:python-programs,代码行数:16,代码来源:updating_env_var.py

示例2: Interpreter

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import update [as 别名]
class Interpreter(object):
    def __init__(self, functions, name="module"):
        self.vars = ChainMap()
        self.vars.update({fn.instructions[0][1]: fn for fn in functions})

        # List of Python modules to search for external decls
        external_libs = [ 'math', 'os' ]
        self.external_libs = [ __import__(name) for name in external_libs]

    def run(self, function, *args):
        '''
        Run intermediate code in the interpreter.  ircode is a list
        of instruction tuples.  Each instruction (opcode, *args) is
        dispatched to a method self.run_opcode(*args)
        '''
        # Create local scope and add args to it
        self.vars = self.vars.new_child()
        if args:
            argnames = function.instructions[0][3::2]
            for i, argname in enumerate(argnames):
                self.vars[argname] = self.vars[args[i]]

        # Run the body
        retval = self.run_block(function.body)
        self.vars = self.vars.parents # pop off scope
        return retval

    def run_block(self, block):
        kind = block.__class__.__name__
        runner = getattr(self, 'run_{}'.format(kind), None)
        if runner:
            return runner(block)
        else:
            print("Warning: No run_{}() method".format(kind))

    def run_BasicBlock(self, block):
        retval = self.run_instructions(block.instructions)
        if retval is not None:
            return retval
        elif block.next_block:
            return self.run_block(block.next_block)

    def run_IfBlock(self, block):
        # Run the test then it's a simple if/else here
        self.run_instructions(block.instructions)
        retval = None
        if self.vars[block.gen_location]:
            retval = self.run_BasicBlock(block.if_branch)
        elif block.else_branch:
            retval = self.run_BasicBlock(block.else_branch)

        if retval is not None:
            return retval
        if block.next_block:
            return self.run_block(block.next_block)

    def run_WhileBlock(self, block):
        def run_test():
            self.run_instructions(block.instructions)
            return self.vars[block.gen_location]
        while run_test():
            retval = self.run_BasicBlock(block.while_body)
            if retval is not None:
                return retval
        if block.next_block:
            return self.run_block(block.next_block)

    def run_instructions(self, instructions):
        """Run a set of instructions in the interpreter"""
        for op in instructions:
            opcode = op[0]
            if hasattr(self, "run_"+opcode):
                retval = getattr(self, "run_"+opcode)(*op[1:])
                if retval is not None:
                    return retval
            else:
                # match a bit fuzzier
                parts = opcode.split('_')
                if hasattr(self, "run_"+parts[0]):
                    retval = getattr(self, "run_"+parts[0])(*op[1:])
                    if retval is not None:
                        return retval
                else:
                    print("Warning: No run_"+opcode+"() method")


    def run_add(self, left, right, target):
        self.vars[target] = self.vars[left] + self.vars[right]

    def run_sub(self, left, right, target):
        self.vars[target] = self.vars[left] - self.vars[right]

    def run_mul(self, left, right, target):
        self.vars[target] = self.vars[left] * self.vars[right]

    def run_div(self, left, right, target):
        self.vars[target] = self.vars[left] / self.vars[right]

    def run_uadd(self, source, target):
        self.vars[target] = +self.vars[source]
#.........这里部分代码省略.........
开发者ID:geraldstanje,项目名称:gone,代码行数:103,代码来源:goneinterp.py


注:本文中的collections.ChainMap.update方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。