def __init__(self, username='', password=''): self.api = vkapi.vk_api(username, password, ignored_errors=ignored_errors) self.api.initLongpoll() self.users = user_cache(self.api, 'sex,crop_photo,blacklisted,blacklisted_by_me') self.initSelf() self.guid = int(time.time() * 5) self.last_viewed_comment = 0 self.good_conf = {} self.tm = thread_manager() self.last_message = {} # peer_id: (id, time, text) self.last_message_id = 0 self.whitelist = None self.bad_conf_title = lambda s: False self.admin = None
def __init__(self, username, password, captcha_handler=None): self.api = vkapi.vk_api(username, password, 4) self.api.captcha_handler = captcha_handler self.api.getToken() self.banned_messages = set() self.guid = int(time.time() * 5) self.ensureLoggedIn() self.self_id = str(self.api.users.get()[0]['id']) self.last_viewed_comment = 0 self.name_cache = {} self.good_conf = set() self.tm = thread_manager() self.last_message = {} self.left_confs = set()
def sentiment_handler(query): try: print("Initializing...", flush=True) global count """ This method will parse the tweets from past seven days and get their sentiments using subroutines """ date = getDate() cur_date = date threadManager = thread_manager() apis, threads, location, sentiments = [], [], {}, [None] * 7 # Making api's object for various token keys of twitter for key in range(1, 8): apis.append(auth_Handler(str(key))) print("Collecting Data...", flush=True) # Making threads for each of the api created, for parallel running for num, api in enumerate(apis): prev_date = prev_date_getter(cur_date) thread = threading.Thread(target=tweets_handler, args=(api, query, cur_date, prev_date, sentiments, location)) cur_date = prev_date thread.setName(str(num + 1)) thread.setDaemon(True) threads.append(thread) threadManager.add_thread(thread) thread.start() # Waiting for threads to finish, for maintaining synchronization for thread in threads: threadManager.remove_thread(thread) thread.join() count += 1 if count == 7: print("Sentiments Calculated", flush=True) print("Preparing Results", flush=True) maps_data_filler(location) spine_chart(sentiments) piechart(sentiments) multi_graph(sentiments) print("Process Complete", flush=True) print("Here you go", flush=True) return True except: print("error", flush=True)
def __init__(self, username='', password=''): self.api = vkapi.vk_api(username, password, ignored_errors=ignored_errors) self.api.initLongpoll() self.users = user_cache( self.api, 'sex,crop_photo,blacklisted,blacklisted_by_me') self.initSelf() self.guid = int(time.time() * 5) self.last_viewed_comment = 0 self.good_conf = {} self.tm = thread_manager() self.last_message = {} # peer_id: (id, time, text) self.last_message_id = 0 self.whitelist = None self.bad_conf_title = lambda s: False self.admin = None
class serial_operator(object): thread_mag = thread_manager() def __init__(self, motestring): self.mif = MoteIF.MoteIF() self.tos_source = self.mif.addSource(motestring) self.mif.addListener(self, sensor_msg.sensor_msg) self.mif.addListener(self, reply_msg.reply_msg) def receive(self, src, msg): if msg.get_amType() == sensor_msg.AM_TYPE: insert_data = t_insert_data() insert_data.node_id = int(msg.get_node_id()) insert_data.sensor_id = int(msg.get_sensor_type()) insert_data.data = int(msg.get_sensor_value()) if insert_data.sensor_id == d_sensor_set["THERMISTOR"]: insert_data.data = float("%.2f" % Celsius(insert_data.data)) #print msg #if insert_data.sensor_id == 4: #print "###### GET ONE SENSE MESSAGE #######" #print "# node_id = %d" % insert_data.node_id #print "# sensor_id = %d [%s]" % (insert_data.sensor_id, d_sensor_num_to_name[str(insert_data.sensor_id)]) #print "# data = %f [ADC = %d] " % (insert_data.data, msg.get_sensor_value()) #print "####################################" #print "###### GET ONE SENSE MESSAGE #######" #print "# node_id = %d" % insert_data.node_id #print "# sensor_id = %d [%s]" % (insert_data.sensor_id, d_sensor_num_to_name[str(insert_data.sensor_id)]) #print "# data = %d" % insert_data.data #print "####################################" #try: if 1: db_op = db_operator() db_op.db_connect() db_op.db_insert_data(insert_data) db_op.db_close() #print "DB <<<<<<<<< ONE DATA INSERTED" #except sqlite3.OperationalError, e: #print e elif msg.get_amType() == reply_msg.AM_TYPE: #print msg task_finished = t_update_task() task_finished.transaction_number = int(msg.get_transaction_number()) task_finished.status = int(msg.get_status()) task_finished.node_id = int(msg.get_node_id()) task_finished.sensor_id = int(msg.get_request_device()) task_finished.operation_code = int(msg.get_request_code()) operation_data = int(msg.get_request_data()) if task_finished.sensor_id == d_sensor_set["THERMISTOR"] and \ task_finished.operation_code == d_request_code["SET_READING_THRESHOLD_REQUEST"]: operation_data = Celsius(operation_data) task_finished.operation_data = operation_data print "****** GET ON REPLY MESSAGE ********" print "* transaction_number = %d" % task_finished.transaction_number print "* node_id = %d" % task_finished.node_id print "* sensor_id " , task_finished.sensor_id print "* sensor_id = %d [%s]" % (task_finished.sensor_id, d_sensor_num_to_name[str(task_finished.sensor_id)]) print "* request_code ", task_finished.operation_code print "* request_code = %d [%s]" % (task_finished.operation_code, d_request_code_to_name[str(task_finished.operation_code)]) print "* request_data = %d [ADC = %d]" % (task_finished.operation_data, msg.get_request_data()) print "* status = %d" % task_finished.status print "************************************" self.thread_mag.upword(task_finished) sys.stdout.flush() def send(self, task_to_commit): smsg = request_msg.request_msg() smsg.set_transaction_number(int(task_to_commit["transaction_number"])) smsg.set_node_id(int(task_to_commit["node_id"])) smsg.set_request_device(int(task_to_commit["sensor_id"])) smsg.set_request_code(int(task_to_commit["operation_code"])) smsg.set_request_data(int(task_to_commit["operation_data"])) self.mif.sendMsg(self.tos_source, 0xFFFF, smsg.get_amType(), 0, smsg) print "SENT ONE REQUEST >>>>>>>> HARDWARE" def main_loop(self): self.thread_mag.downword(self.send)
_sleep = 2 return thread_num if __name__ == '__main__': config = get_config() if not config: print 'config error' sys.exit() _sleep = 2 thread_num = get_thread_num(get_queue_size()) # 防止KeyboardInterrupt时报错,程序会在下一步捕获KeyboardInterrupt try: thread_manager(thread_num, main) except: pass # 所有线程执行完退出后 循环检查 try: while True: time.sleep(_sleep) print 'all threads exit. loop checking...' queue_size = get_queue_size(); if queue_size > 0: thread_manager(get_thread_num(queue_size), main) except KeyboardInterrupt: print "\nthread manager stoped"
thread_num = 2 _sleep = 2 return thread_num if __name__ == '__main__': config = get_config() if not config: print 'config error' sys.exit() _sleep = 2 thread_num = get_thread_num(get_queue_size()) # 防止KeyboardInterrupt时报错,程序会在下一步捕获KeyboardInterrupt try: thread_manager(thread_num, main) except: pass # 所有线程执行完退出后 循环检查 try: while True: time.sleep(_sleep) print 'all threads exit. loop checking...' queue_size = get_queue_size() if queue_size > 0: thread_manager(get_thread_num(queue_size), main) except KeyboardInterrupt: print "\nthread manager stoped"