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


Python Timer.cancel方法代码示例

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


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

示例1: World

# 需要导入模块: from utils import Timer [as 别名]
# 或者: from utils.Timer import cancel [as 别名]
class World(object):
    worlds = []

    @staticmethod
    def clear_all():
        for world in World.worlds:
            world.stop()
        World.worlds = []

    def __init__(self, cols, rows):
        self.cols = cols
        self.rows = rows
        self._objects = []
        self._field = []
        self._timer = None
        self.queue = []
        self.reproductions = 0
        self.plants = 0
        self.herbivores = 0
        self.predators = 0
        self.turns = 0
        self.deaths = 0
        for x in xrange(cols):
            self._field.append([ None for y in xrange(rows)])

        World.worlds.append(self)

    def check_queue(self):
        newqueue = []
        for turns, obj in self.queue:
            if turns == 0:
                self.add_creature(obj)
            else:
                newqueue.append((turns - 1, obj))
        return newqueue

    def loop(self):
        try:
            self.queue = self.check_queue()

            for obj in self._objects:
                obj.turn(self)
            deleted = filter(lambda obj: obj.is_nothing, self._objects)
            #todo: need lock
            for obj in deleted:
                logging.debug("Dead: %r, turns: %s, history: %s" % (obj, obj.turns, repr(obj.history.read())))
                self._field[obj.x][obj.y] = None
                self._objects.remove(obj)
                self.info_update_del(obj)
                self.deaths += 1
            self.stabilize()

            self.turns += 1
        except:
            import traceback
            logging.debug(traceback.format_exc())
            self._timer.cancel()

    def start(self, speed):
        if self._timer:
            raise Exception()
        self.init_count_objects = len(self._objects) + len(self.queue)
        self._timer = Timer(speed, self.loop)
        self._timer.start()

    def stop(self):
        self._timer.cancel()

    def begin_force(self):
        self._timer.is_force = True

    def end_force(self):
        self._timer.is_force = False

    def check_position(self, x, y):
        return 0 <= x < self.cols and 0 <= y < self.rows

    def add_creature(self, creature):
        if not self.check_position(creature.x, creature.y):
            return False
        if self._field[creature.x][creature.y] is not None:
            return False
        self._objects.append(creature)
        self._field[creature.x][creature.y] = creature
        self.info_update_add(creature)
        return True

    def info_update_add(self, creature):
        name = creature.__class__.__name__
        if name == 'Predator':
            self.predators += 1
        elif name == 'Herbivore':
            self.herbivores += 1
        elif name == 'Plant':
            self.plants += 1

    def info_update_del(self, creature):
        name = creature.__class__.__name__
        if name == 'Predator':
            self.predators -= 1
#.........这里部分代码省略.........
开发者ID:NElias,项目名称:swottc,代码行数:103,代码来源:world.py


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