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


Python Address.proto方法代码示例

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


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

示例1: Instruction

# 需要导入模块: from address import Address [as 别名]
# 或者: from address.Address import proto [as 别名]
class Instruction(ModelBase):
    """ General class for all instructions """

    def __init__(self, value):
        """ Value is a list or Instruction. """
        if isinstance(value, Instruction):
            self.fromother(value)
        else:
            # Process protocol instruction
            self.fromproto(value)

    def fromproto(self, value):
        """ Sets properties from list. """
        # TODO: type checking
        value = list(value)
        self.code = value[0]
        self.address = Address(value[1])
        self.additional = value[2:]

    def fromother(self, other):
        """ Sets properties from other instruction. """
        self.fromproto(other.proto())

    def apply(self, tree, checkfirst=False):
        """ Apply this instruction to a tree. Operation is responsible for calling sanitycheck before applying. """
        # Resolve tree in question
        tree = self.address.resolve(tree)

        # Sanity check
        if checkfirst:
            self.sanitycheck(tree)

            # Branch based on insertion or deletion
        if self.isinsert:
            self._apply_insert(tree)
        else:
            self._apply_delete(tree)

    def _apply_delete(self, tree):
        for d in self.deletions:
            for i in range(d[0], d[1] + 1):
                tree.delete(i)

    def _apply_insert(self, tree):
        vn = self.value_node
        try:
            tree.get(self.position, vn.key)
        except:
            tree.put(self.position, vn)

    def sanitycheck(self, tree):
        """ Check a tree to make sure this instruction can be applied. """
        if self.isinsert:
            validpos(tree, self.position)
        else:
            for i in self.deletions:
                validpos(tree, i[0])
                validpos(tree, i[1])
                if i[0] > i[1]:
                    raise ValueError("Backwards deletion range [%d, %d]" % i)

    def proto(self):
        """ Protocol representation """
        return [self.code, self.address.proto()] + self.additional

    @property
    def isinsert(self):
        return self.code != 0

    @property
    def position(self):
        return self.additional[0]

    @property
    def value(self):
        return self.additional[1]

    @property
    def value_node(self):
        """ Generates a node for self.value on the fly. """
        if self.code == 1:
            return node.StringNode(self.value)
        elif self.code == 2:
            return node.MapNode(self.value)
        elif self.code == 3:
            return node.ListNode(self.value)
        elif self.code == 4:
            return node.NumberNode(self.value, self.additional[2])
        elif self.code == 5:
            return node.SingleNode()
        elif self.code == 6:
            return node.TrinaryNode(self.value)
        else:
            raise TypeError("Unknown instruction code, or does not have a value node")

    @property
    def deletions(self):
        result = []
        for i in self.additional:
            if type(i) == int:
#.........这里部分代码省略.........
开发者ID:aral,项目名称:ConcurrenTree,代码行数:103,代码来源:instruction.py


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