def get_position(self): if self.symbol != '': info = self.broker.binance_http.get_position_info() if isinstance(info, list): for item in info: symbol = item["symbol"] if self.symbol == symbol: event = Event(EVENT_POS, { "symbol": symbol, "pos": item }) self.broker.event_engine.put(event)
def get_kline_data(self): data = self.broker.binance_http.get_kline(symbol=self.symbol, interval=Interval.MINUTE_5, limit=100) if len(data): kline_time = data[-1][0] if kline_time != self.kline_time: event = Event(EVENT_KLINE, { 'symbol': self.symbol, "data": data, '_flag': '' }) self.broker.event_engine.put(event) self.kline_time = kline_time
def events(self): file_list = glob.glob(self.args.root + "*.json") random.seed(100) random.shuffle(file_list) with open(opt.label) as f: d = {} for line in f: s = line.split(" ") idx = s[0].split(":")[1] label = s[1].split(':')[1] d[idx] = label if self.mode == "train": data_set = file_list[:int(0.675 * len(file_list))] elif self.mode == "test": data_set = file_list[int(0.675 * len(file_list)):] # val_set = file_list[int(0.7*len(file_list)):int(0.8*len(file_list))] for i in range(len(data_set))[::self.args.batch_size]: file = data_set[i] label = d[file.split('.')[0].split('/')[-1]] yield Event(self.args, file, int(label), self.vocab)
def __init__(self): self.on_key_down = Event() self.on_key_up = Event()
def __init__( self, url_chain, name=None, debug=0, stdout=None, stderr=None, session_log=None, log=None ): # Event emitted when new command output received self.command_output_received = Event() # Event emitted when device changed the name self.name_changed = Event() # Event emitted when device wants to log the message self.log_event = Event() self.log_event.listen(self._log) self.node_chain = [] self.stdby_nodes = [] self.debug = debug self.url_chain = url_chain for url in iter(to_list(url_chain)): if "," in url: # It has standby alt_address = url.split(',') if ('.' in alt_address[-1]): #Alternate mgmt IP given for standby connections for alt_ip in alt_address[1:] : self.stdby_nodes.append('@'.join(url.split("@")[:-1]) + "@" + alt_ip) else : #Alternate ports given for standby connections for alt_port in alt_address[1:] : self.stdby_nodes.append(':'.join(url.split(":")[:-1]) + ":" + alt_port) url = url.split(",")[0] node_info = NodeInfo(url) if not node_info.is_valid(): self._dbg( 2, "For {} protocol {} is not supported or " "{} is invalid URL".format( node_info.hostname, node_info.scheme, node_info.url) ) continue self.node_chain.append(node_info) if "," in self.url_chain[-1] : self.url_chain[-1] = self.url_chain[-1].split(",")[0] if self.node_chain: self.node_chain[0].verify_reachability = is_reachable self.session = None self.session_log = session_log self.connected = False self.last_command_succeed = False self.pending_connection = False self.command_execution_pending = Lock() self.connecting_lock = Lock() self.output_store_dir = "." if name: self.name = name #self.name = self.node_chain[-1].node_name else: self.name = self.node_chain[-1].node_name \ if self.node_chain else "Unknown" if stdout is None: self.stdout = open(os.devnull, 'w') else: self.stdout = stdout if stderr is None: self.stderr = sys.stderr else: self.stderr = stderr self.info = {} self.packages = DevicePackages() self.log_event.disconnect(self._log) self.error_code = None
class Device(object): def __init__( self, url_chain, name=None, debug=0, stdout=None, stderr=None, session_log=None, log=None ): # Event emitted when new command output received self.command_output_received = Event() # Event emitted when device changed the name self.name_changed = Event() # Event emitted when device wants to log the message self.log_event = Event() self.log_event.listen(self._log) self.node_chain = [] self.stdby_nodes = [] self.debug = debug self.url_chain = url_chain for url in iter(to_list(url_chain)): if "," in url: # It has standby alt_address = url.split(',') if ('.' in alt_address[-1]): #Alternate mgmt IP given for standby connections for alt_ip in alt_address[1:] : self.stdby_nodes.append('@'.join(url.split("@")[:-1]) + "@" + alt_ip) else : #Alternate ports given for standby connections for alt_port in alt_address[1:] : self.stdby_nodes.append(':'.join(url.split(":")[:-1]) + ":" + alt_port) url = url.split(",")[0] node_info = NodeInfo(url) if not node_info.is_valid(): self._dbg( 2, "For {} protocol {} is not supported or " "{} is invalid URL".format( node_info.hostname, node_info.scheme, node_info.url) ) continue self.node_chain.append(node_info) if "," in self.url_chain[-1] : self.url_chain[-1] = self.url_chain[-1].split(",")[0] if self.node_chain: self.node_chain[0].verify_reachability = is_reachable self.session = None self.session_log = session_log self.connected = False self.last_command_succeed = False self.pending_connection = False self.command_execution_pending = Lock() self.connecting_lock = Lock() self.output_store_dir = "." if name: self.name = name #self.name = self.node_chain[-1].node_name else: self.name = self.node_chain[-1].node_name \ if self.node_chain else "Unknown" if stdout is None: self.stdout = open(os.devnull, 'w') else: self.stdout = stdout if stderr is None: self.stderr = sys.stderr else: self.stderr = stderr self.info = {} self.packages = DevicePackages() self.log_event.disconnect(self._log) self.error_code = None def store_property(self, key, value): self._dbg(4, "Store '{}' <- '{}'".format(key, value)) self.info[key] = value def get_property(self, key): return self.info.get(key, None) def __repr__(self): name = "" for node in self.node_chain: name += "->{}".format(node) return name[2:] def execute_command(self,command,timeout=60): return self.session.connected, self.session.send(command,timeout) def disconnect(self): return self.session.disconnect() def connect(self): status = False self.session = condor.make_connection_from_urls( "None", self.url_chain) try : print "Connecting to device" status = self.session.connect(self.session_log) except Exception as e: print "Failed to connect device" print e if not status and self.stdby_nodes : for node in self.stdby_nodes : new_url_chain = self.url_chain new_url_chain[-1] = node print "Trying to connect to: ", new_url_chain[-1] self.session = condor.make_connection_from_urls( "None", new_url_chain) try : status = self.session.connect(self.session_log) except : print "Failed to connect to : ",node return status def reconnect(self): """ Wait for system to come up with max timeout as 10 Minutes """ status = False timeout = 900 poll_time = 30 time_waited = 0 print "System going for reload., please wait!!" self.session.disconnect() time.sleep(60) try : self.session.connect(self.session_log) except : pass while 1: time_waited += poll_time if time_waited >= timeout: break else: time.sleep(poll_time) print "\nRetry count :%s @ %s"%(time_waited/poll_time,time.strftime("%H:%M:%S", time.localtime())) try: status = self.session.connect(self.session_log) except: continue if status : return True return status def _dbg(self, level, msg): if self.debug <= level: return self.stderr.write("{} [{}]: {}\n".format( datetime.datetime.now(), self.name, msg) ) def _log(self, msg): message = "{}: {}".format(self.name, msg) self._dbg(4, msg) self.stdout.write(message + '\n') def log(self, msg): self.log_event(msg)
clients_arrival_interval = 5 maxrow = 8 EVENTS = [] ROW = [] WORKERS = [Worker() for _ in range(num_of_workers)] time = start_hour * 60 ncl = 0 N = 0 Nserved = 0 Nwaited = 0 Twaited = 0 EVENTS = [] ev = Event(time, 1, (1, )) EVENTS = fill_events(EVENTS, ev) ev = Event(last_minute, 8, (1, )) EVENTS = fill_events(EVENTS, ev) t = time + int(round(pr.exponential(clients_arrival_interval))) ev = Event(t, 2, (0, 0)) EVENTS = fill_events(EVENTS, ev) while EVENTS: ev = EVENTS.pop(0) time = ev.tm if ev.tp in [1, 8]: print(ev)
def rsvp_events(sess, token, group): """ Used to RSVP the events of a particular group Parameters: sess (requests.Session object): A Session object of the logged in session group (Group object) : A object of Group class, denoting selected group. Returns: None Writes: logs the RSVP-ed events into a 'rsvped_events.json' file. """ url = get_upcoming_events_url(group) rsvped_events_string = get_rsvped_events_string() res = sess.get(url) soup = BeautifulSoup(res.text, 'html.parser') all_events = soup.find_all('a', {'id': 'attendButton'}) rsvped_events = [] for event in all_events: sess.headers = base_headers.RSVP_GET id = event.get('href').split('/')[-2] rsvp_endpoint = urls.BASE_URL + \ event.get('href')+'action=rsvp&response=yes' if id in rsvped_events_string: continue if not args.dry_run: params = ( ('action', 'rsvp'), ('response', 'yes'), ) res = sess.get(urls.BASE_URL + event.get('href'), params=params) sess.headers = base_headers.RSVP_POST sess.headers['x-mwp-csrf'] = sess.cookies['x-mwp-csrf-header'] sess.headers['referer'] = rsvp_endpoint query = '(endpoint:' + group.group_url + "/events/" + id + "/rsvps" + ',meta:(method:post),' + 'params:(eventId:' + id + \ ',fields:rsvp_counts,' + 'response:yes,' + 'urlname:' + group.group_url + \ ')' + ',ref:rsvpAction' + "_" + group.group_url + '_' + id + ')' data = { 'queries': query } res = sess.post( 'https://www.meetup.com/mu_api/urlname/events/eventId', data=data) if res.status_code == 200: rsvp_event = Event(group.group_name, id, True) rsvped_events.append(rsvp_event) old_rsvped_events = [Event.fromJson(x) for x in json.loads(rsvped_events_string)] rsvped_events += old_rsvped_events jsonStr = json.dumps([x.__dict__ for x in rsvped_events]) with open('rsvped_events.json', "w") as f: f.seek(0) f.write(jsonStr) f.truncate()
session.headers = base_headers.RSVP_POST session.headers['x-mwp-csrf'] = session.cookies['x-mwp-csrf-header'] session.headers['referer'] = rsvp_endpoint query = '(endpoint:' + group.group_url + "/events/" + id + "/rsvps" + ',meta:(method:post),params:(eventId:' + id + \ ',fields:rsvp_counts,response:yes,urlname:' + group.group_url + \ '),ref:rsvpAction' + "_" + group.group_url + '_' + id + ')' data = { 'queries': query } res = session.post( 'https://www.meetup.com/mu_api/urlname/events/eventId', data=data) if res.status_code == 200: rsvp_event = Event(group.group_name, id, True) rsvped_events.append(rsvp_event) return rsvped_events #used to write RSVPed events into 'rsvped_events.json' file. def rsvp_events(session, token, group): rsvped_events_string = get_rsvped_events_string() rsvped_events = get_event_rsvped(rsvped_events_string, session, group) old_rsvped_events = [Event.fromJson(x) for x in json.loads(rsvped_events_string)] rsvped_events += old_rsvped_events rsvpList = json.dumps([x.__dict__ for x in rsvped_events])
class Device(object): def __init__( self, url_chain, name=None, debug=0, stdout=None, stderr=None, session_log=None, log=None ): # Event emitted when new command output received self.command_output_received = Event() # Event emitted when device changed the name self.name_changed = Event() # Event emitted when device wants to log the message self.log_event = Event() self.log_event.listen(self._log) self.node_chain = [] self.stdby_nodes = [] self.debug = debug self.url_chain = url_chain for url in iter(to_list(url_chain)): if "," in url: # It has standby alt_address = url.split(',') if ('.' in alt_address[-1]): #Alternate mgmt IP given for standby connections for alt_ip in alt_address[1:] : self.stdby_nodes.append('@'.join(url.split("@")[:-1]) + "@" + alt_ip) else : #Alternate ports given for standby connections for alt_port in alt_address[1:] : self.stdby_nodes.append(':'.join(url.split(":")[:-1]) + ":" + alt_port) url = url.split(",")[0] node_info = NodeInfo(url) if not node_info.is_valid(): self._dbg( 2, "For {} protocol {} is not supported or " "{} is invalid URL".format( node_info.hostname, node_info.scheme, node_info.url) ) continue self.node_chain.append(node_info) if "," in self.url_chain[-1] : self.url_chain[-1] = self.url_chain[-1].split(",")[0] if self.node_chain: self.node_chain[0].verify_reachability = is_reachable self.session = None self.session_log = session_log self.connected = False self.last_command_succeed = False self.pending_connection = False self.command_execution_pending = Lock() self.connecting_lock = Lock() self.output_store_dir = "." if name: self.name = name #self.name = self.node_chain[-1].node_name else: self.name = self.node_chain[-1].node_name \ if self.node_chain else "Unknown" if stdout is None: self.stdout = open(os.devnull, 'w') else: self.stdout = stdout if stderr is None: self.stderr = sys.stderr else: self.stderr = stderr self.info = {} self.packages = DevicePackages() self.log_event.disconnect(self._log) self.error_code = None def store_property(self, key, value): self._dbg(4, "Store '{}' <- '{}'".format(key, value)) self.info[key] = value def get_property(self, key): return self.info.get(key, None) def __repr__(self): name = "" for node in self.node_chain: name += "->{}".format(node) return name[2:] def execute_command(self,command): return self.session.connected, self.session.send(command) def disconnect(self): status = self.session.disconnect() return status def connect(self): status = False self.session = condor.make_connection_from_urls( "None", self.url_chain) try : print "Connecting to device" status = self.session.connect(self.session_log) except Exception as e: print "Failed to connect device" print e if not status and self.stdby_nodes : for node in self.stdby_nodes : new_url_chain = self.url_chain new_url_chain[-1] = node print "Trying to connect to: ", new_url_chain[-1] self.session = condor.make_connection_from_urls( "None", new_url_chain) try : status = self.session.connect(self.session_log) except : print "Failed to connect to : ",node return status def reconnect(self): """ Wait for system to come up with max timeout as 10 Minutes """ status = False timeout = 900 poll_time = 30 time_waited = 0 print "System going for reload., please wait!!" time.sleep(60) self.session.disconnect() while 1: time_waited += poll_time if time_waited >= timeout: break else: time.sleep(poll_time) print "\nRetry count :%s @ %s"%(time_waited/poll_time,time.strftime("%H:%M:%S", time.localtime())) try: status = self.session.connect() except: continue if status : return True return status def _dbg(self, level, msg): if self.debug <= level: return self.stderr.write("{} [{}]: {}\n".format( datetime.datetime.now(), self.name, msg) ) def _log(self, msg): message = "{}: {}".format(self.name, msg) self._dbg(4, msg) self.stdout.write(message + '\n') def log(self, msg): self.log_event(msg)