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


Python EventManager.record方法代码示例

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


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

示例1: TwitterStreamer

# 需要导入模块: from event_manager import EventManager [as 别名]
# 或者: from event_manager.EventManager import record [as 别名]
class TwitterStreamer(StreamListener):

    def __init__(self, node, southwest, northeast, extras):

        logging.info("Beginning TwitterStreamer init")

        StreamListener.__init__(self)

        # Lock and target location
        self.lock = threading.Lock()
        self.buckets = {}
        self.deltas = {}
        self.uploaders = {}
        self.location = [southwest[LONG],southwest[LAT],northeast[LONG],northeast[LAT]]
        self.event_manager = EventManager()


        # Upload handler
        self.node = node

        logging.info("TwitterStreamer init successful")

    #
    #   Threading Functiosn
    #

    # Start node
    def start(self):

        logging.info("Starting TwitterStreamer")

        # This handles Twitter authetification and the connection to Twitter Streaming API
        auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

        # 2nd parameter self serves as the StreamListener object
        self.stream = Stream(auth, self)

        logging.debug("Created Stream object")

        # Start streaming with right parameters
        #self.stream.filter(locations=self.location, async=True)
        t = threading.Thread(target=self.stream.filter, kwargs={'locations':self.location, 'async':False})
        t.daemon = True
        t.start()

        logging.info("TwitterStreamer started successfully")

        return t


    def stop(self):

        logging.info("Stopping TwitterStreamer")

        # Disconnect the stream
        self.stream.disconnect()

        # Cancel our uploading threads
        for t in self.uploaders:
            self.uploaders[t].cancel()

        logging.info("TwitterStreamer stopped")


    #
    #   Data Management
    #

    def find_location(self, json):
        # Primary check
        coordinates = json['place']['bounding_box']['coordinates'][0]
        return coordinates

    def record_event(self,long,lat):

        key = str(long) + "," + str(lat)

        # If this is the first time recording for bucket, launch updater
        if key not in self.buckets:
            self.buckets[key] = self.buckets.get(key,0) + 1
            self.deltas[key] = datetime.datetime.now()
            self.post_bucket(key)
        else:
            self.buckets[key] += 1


    def post_bucket(self, key):

        #now = datetime.datetime.now()
        #period = (now - self.deltas[key]).total_seconds()
        #self.deltas[key] = now
        period = 10

        velocity = float(self.buckets[key]) / float(period)

        accel, torque = self.event_manager.record(velocity)

        x, y = [int(i) for i in key.split(',')]

#.........这里部分代码省略.........
开发者ID:andrewsosa001,项目名称:TweetEngine,代码行数:103,代码来源:twitter_streamer.py


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