def test_event(): """Test Event class.""" # Test __init__ switch_state = [0, 0, 0] event = Event(switch_state, switch_state) assert_equals(event.new_switch_state, switch_state) assert_items_equal(event._effective_switch_vector, switch_state) # Test new_event and state_diff switch_state = [1, 0, 0] event = event.new_event(switch_state) assert_items_equal(event._effective_switch_vector, switch_state) assert_equals(event.state_diff(), 0) # Test _get_effective_switch_vector and state_diff switch_state = [0, 0, 0] event = event.new_event(switch_state) assert_items_equal(event._effective_switch_vector, [1, 0, 0]) assert_equals(event.state_diff(), 0) # Test count_active and get_active_switch switch_state = [1, 0, 0] event = event.new_event(switch_state) switch_state = [1, 0, 1] event = event.new_event(switch_state) assert_equals(event.count_active(), 2) first = event.get_active_switch(1) assert_equals(first, 0) second = event.get_active_switch(2) assert_equals(second, 2)
def set_event(self, event_id, rank, score): event = Event(event_id) event.set_score(score).set_rank(rank) self.events[event_id] = event return self
def _add_event(self, event, events): e = Event(event) if e.start_time().date() == date.today(): if e.artists(): events.append(e) else: print "no artists for for event:", e.name(), "url:", e.url()
def __init__(self, etype, bubbles=True, cancelable=False, relatedObject=None, shiftKey=None, keyCode=0, direction='none'): Event.__init__(self, etype, bubbles, cancelable) self.relatedObject = relatedObject self.keyCode = keyCode self.shiftKey = shiftKey
def post_init(self): shortener = Event("__urls__") shortener.define(msg_definition = "https?://[\S]+") # What to look for to trigger event shortener.subscribe(self) # register ourself to our new custom event self.bot.register_event(shortener, self)
class Swim(): def __init__(self, line): tokens = line.split( "|" ) num_tokens = len( tokens ) # Figure out what version data we have version = 0 if tokens[0].startswith( "V" ): version = int( tokens[0][1:] ) if version == 1: self.asa_number = int( tokens[1] ) self.event = Event( int( tokens[2] ) ) self.date = helpers.ParseDate_dmY( tokens[3] ) self.meet = tokens[4] self.asa_swim_id = int( tokens[5] ) # Splits are in tokens[6] self.is_licensed = True if tokens[7] == 'n': self.is_licensed = False self.race_time = float( tokens[8] ) self.short_course_race_time = self.race_time if self.event.is_long_course(): self.short_course_race_time = self.event.convert_time( self.race_time ) #print( str( self.short_course_race_time ) ) else: raise RuntimeError( "Unhandled swim version" ) # Returns the asa number of the swimmer that this swim is for. def get_asa_swim_id(self): if self.asa_swim_id == -1: return None return self.asa_swim_id
def deploy_event(self, extend_eventtype=None, event=None, **kwargs): eventtype = self._importname if not extend_eventtype else self._importname + extend_eventtype if not event: event = Event(self, eventtype, **kwargs) else: event.eventtype = eventtype self._eventhandler.deploy_event(event)
def get(self): self.response.headers['Content-Type'] = 'text/plain' asa_number_str = self.request.get('asa_number') if asa_number_str == '': self.response.out.write( "Missing asa_number" ) self.response.set_status( 400 ) return stroke_code_str = self.request.get('stroke_code') if stroke_code_str == '': # If there's no stroke code, then we assume that we want the entire swim history swimlist = SwimList.get( int(asa_number_str) ) if swimlist is not None: self.response.out.write( str( swimlist ) ) else: distance_str = self.request.get('distance') if distance_str == '': self.response.out.write( "Missing distance" ) self.response.set_status( 400 ) return asa_number = int(asa_number_str) stroke_code = int(stroke_code_str) distance = int(distance_str) swims = Swim.fetch_all( asa_number, Event.create( stroke_code, distance, "S" ) ) swims.extend( Swim.fetch_all( asa_number, Event.create( stroke_code, distance, "L" ) ) ) swims.extend( UnofficialSwim.fetch_all( asa_number, Event.create( stroke_code, distance, "S" ) ) ) swims.extend( UnofficialSwim.fetch_all( asa_number, Event.create( stroke_code, distance, "L" ) ) ) for swim in swims: self.response.out.write( str( swim ) + "\n" )
def main(): frame_duration = 1.0/60.0 for level in LEVELS: load_level(level) while not Player.main.at_exit(): frame_start_time = time.time() key = Drawable._window.checkKey() if key in KEYMAP: eval(KEYMAP[key]) Event.update() if not Config.hidden_flag: if Gold.all_taken(): HiddenLadder.showAll() Player.main.redraw() for baddie in Baddie.baddies: baddie.redraw() Config.hidden_flag = True # baddies should probably move here frame_time = time.time() - frame_start_time if frame_time < frame_duration: time.sleep(frame_duration - frame_time) Drawable.won()
class World: def __init__(self): self.connections = [] self.connection_added = Event() self.channel_opened = Event() def get_connections(self): ret = [] for c in self.connections: ret.append(c) return ret def remove_connection(self, connection): if connection.is_connected: raise "I don't think so, tim" self.connections.remove(connection) def new_connection(self): conn = Connection() self.connections.append(conn) conn.channel_opened += self.handle_channel_opened self.connection_added.fire(self, conn) return conn def shut_it_down(self): for c in self.connections: c.shut_it_down() def handle_channel_opened(self, channel): self.channel_opened.fire(self, channel)
def _start_sending(self): _logger.debug('%s, _start_sending', str(self)) if self._close_ev is not None: return if not Event.isEventSet(self._wev): _logger.debug('%s, _start_sending::addEvent', str(self)) Event.addEvent(self._wev)
def test_Event_SubscribeAndFireWithDupplicateKeyword_RaisesException(self): mockListener = mock.Mock() event = Event() event.subscribe(mockListener, kword1="value1") with self.assertRaises(DuplicateKeywordArgument): event.fire(kword1="value2")
def post_init(self): b_event = Event("__.bofh__") b_event.define(msg_definition="^\.bofh") b_event.subscribe(self) self.bot.register_event(b_event, self)
def convert_event(self, html_event): event = Event() event.id = self.extract_id(html_event) event.title = self.extract_title(html_event) event.date = self.extract_date(html_event) event.enrollment_date = self.extract_enrollment_date(html_event) return event
def post_init(self): youtube = Event("__.youtubes__") youtube.define(msg_definition="youtube\.com[\S]+") youtube2 = Event("__.youtubeshort__") youtube2.define(msg_definition="(?<!=)youtu\.be[\S]+") youtube.subscribe(self) youtube2.subscribe(self) self.bot.register_event(youtube, self) self.bot.register_event(youtube2, self) self.bot.mem_store['youtube'] = OrderedDict() # for the new v3 google api >:( try: from youtube_credentials import YoutubeCredentials as yc except ImportError: print "Warning: youtube module requires credentials in modules/youtube_credentials.py" class PhonyYc: api_key = "None" yc = PhonyYc() self.api_key = yc.api_key self.api_url = "https://www.googleapis.com/youtube/v3/videos?id="
def notify(self, **attrs): e = Event() e.source = self for k, v in attrs.iteritems(): setattr(e, k, v) for fn in self.callbacks: fn(e)
def __init__(self, bot, raw): Event.__init__(self, bot, raw) if not self.is_a(*UPDATE_EVENTS): self.channel = models.Channel(self.raw["channel"]) else: self.channel = bot.get_channel(self.raw["channel"])
def post_init(self): kanbo = Event("__.custom__") kanbo.define(msg_definition="^\.kanbo") kanbo.subscribe(self) self.help = ".kanbo (kanbo face)" self.bot.register_event(kanbo, self) self.messages = [u'( ͡° ͜ʖ ͡°)', u'( ͡0 ͜ʖ ͡0)', u'|╲/( ͡° ͡° ͜ʖ ͡° ͡°)/\╱\\', u'┬┴┬┴┤( ͡° ͜ʖ├┬┴┬┴']
def findTree(): ei = EventInterface() ei.setDB('citybeat') ei.setCollection('candidate_event_25by25_merged') events = {} fid1 = open('labeled_data_cf/181_positive.txt', 'r') true_events = [] for line in fid1: t = line.split(',') id = str(t[0]) label = int(t[1]) if label == 1: pass else: continue events[id] = label fid1.close() words = ['motor'] for id, label in events.items(): event = ei.getEventByID(id) e = Event(event) if e.containKeywords(words, 1): print id
def post_init(self): version_event = Event("__.version__") version_event.define(msg_definition="^\.version") version_event.subscribe(self) # register ourself to our new custom event self.bot.register_event(version_event, self)
def test_a_listener_is_passed_right_parameters(self): """""" listener = Mock() event = Event() event.connect(listener) event.fire(5, shape="square") listener.assert_called_with(5, shape="square")
def GetCandidates( relay, stroke, swimmers ): candidates = [] shortCourseEvent = Event.create( stroke, relay.distance, "S" ) longCourseEvent = Event.create( stroke, relay.distance, "L" ) for swimmer in swimmers: if swimmer.is_male == relay.is_male: age_on_day = helpers.CalcAge( swimmer.date_of_birth, relay.date ) if (age_on_day >= relay.min_age) and (age_on_day <= relay.max_age): # Get this swimmer's PB scPbSwim = Swim.fetch_pb( swimmer, shortCourseEvent ) lcPbSwim = Swim.fetch_pb( swimmer, longCourseEvent ) pbRaceTime = None if scPbSwim is None: if lcPbSwim is not None: pbRaceTime = longCourseEvent.convertTime( lcPbSwim.race_time ) else: pbRaceTime = scPbSwim.race_time #logging.info( swimmer.full_name() + str( scPbSwim.race_time ) + " " + str( pbRaceTime ) ) if lcPbSwim is not None: lcConvertedRaceTime = longCourseEvent.convertTime( lcPbSwim.race_time ) if lcConvertedRaceTime < pbRaceTime: pbRaceTime = lcConvertedRaceTime #logging.info( swimmer.full_name() + " Age: " + str( age_on_day ) ) if pbRaceTime is not None: #logging.info( swimmer.full_name() + " PB: " + str( age_on_day ) ) swimmer.relay_time = pbRaceTime swimmer.age_on_day = age_on_day candidates.append( swimmer ) candidates.sort( key = lambda swimmer: swimmer.relay_time ) return candidates;
def test_dates(self): e = Event('Test Dates') e.addLog('My Date Event') L = [] for i in range(100): L.append(i) e.save(True)
def init(self): # config options self.config = {} self.loadConfig() self.running = False # initialize the event object used for sleeping self.event = threading.Event() if self.config['persistence'] and os.path.exists(self.config['file_name']): try: Logger.info('loading database from %s' % self.config['file_name']) self.load() except Exception as e: Logger.critical(str(e)) self.setup() # register our 'save' commands Command.register(self.saveCmd, 'db.save', 0, 'db.save') # treat events Event.register('core.reload', self.reloadEvent) Event.register('core.shutdown', self.shutdownEvent)
def test_a_listener_is_notified_when_an_event_is_raised(self): """""" listener = Mock() event = Event() event.connect(listener) event.fire() self.assertTrue(listener.called)
def loadNextWeekData(): # load modified ei = EventInterface() ei.setDB('citybeat') ei.setCollection('next_week_candidate_event_25by25_merged') true_events = [] false_events = [] fid2 = open('labeled_data_cf/label_next_week.txt', 'r') for line in fid2: t = line.split(',') id = str(t[0]) label = int(t[1]) event = ei.getDocument({'_id':ObjectId(id)}) event['label'] = label e = Event(event) if e.getActualValue() < 8 or event['label'] == 0: # print 'bad event ' + id continue if event['label'] == 1: true_events.append(event) else: false_events.append(event) fid2.close() return true_events, false_events
def __init__(self, url, app_key, app_secret, group_name='default', query_message_interval=50, heartbeat_interval=30, sdk_version='top-sdk-java-201403304', *args, **kwargs): super(TmcClient, self).__init__(url, *args, **kwargs) Event.__init__(self) self.on('on_init', lambda: logger.info('WebSocket Start Connect: %s@%s' % (url, group_name))) assert isinstance(url, (str, unicode)) and len(url) > 0 assert isinstance(app_key, (str, unicode)) and len(app_key) > 0 assert isinstance(app_secret, (str, unicode)) and len(app_secret) > 0 assert isinstance(group_name, (str, unicode)) and len(group_name) > 0 assert isinstance(query_message_interval, int) and 0 < query_message_interval < 60 assert isinstance(heartbeat_interval, int) and 0 < heartbeat_interval < 60 self.sdk_version = sdk_version self.url = url self.app_secret = app_secret self.app_key = app_key self.group_name = group_name self.query_message_interval = query_message_interval self.heartbeat_interval = heartbeat_interval self.token = None self.fire('on_init') self.on('on_handshake_success', self._start_query_loop) self.on('on_confirm_message', self._on_confirm_message)
def post_init(self): """ Because of the way this module works we have to make sure to set our event like we normally would with __.tell__, but we cannot define our event with "^\.tell" like we normally would as it will only look for that line to trigger the event and the user being told will never receive his message since the bot is only looking for .tell and not the user in the PRIVMSG We will set the .tell trigger in our handle function "if event.msg.startswith(".tell"):" and set define to PRIVMSG so it searches all lines from users. While simultaneously looking for the .tell trigger from the user. This is because we actually need 2 things for this module to work. 1.) The user needs to be able to leave a message for someone else using ".tell someuser <Insert message here>" 2.) The user who the .tell message is directed towards will be determined by the PRIVMSG definition. This is determined in the "else" block that searches every line not starting with .tell. If the user matches the stored user from the previous tell trigger, the event will be triggered and pybot will spit out text into the proper channel every time the intended user says something in chat until the buffer is out of .tell events. """ tell = Event("__.tell__") tell.define("PRIVMSG") tell.subscribe(self) # register ourself to our new custom event self.bot.register_event(tell, self)
def init(): commands = { 'product.add': {'handler': Product.productAddCmd, 'args':2, 'help':'productAdd sku stock'}, 'product.info': {'handler': Product.productInfoCmd, 'args':1, 'help':'productInfo sku'}, 'reservation.add': {'handler': Product.reservationAddCmd, 'args':3, 'help':'reservationAdd client_id sku qty'}, 'reservation.del': {'handler': Product.reservationDelCmd, 'args':3, 'help':'reservationDel client_id sku qty'}, 'reservation.set': {'handler': Product.reservationSetCmd, 'args':3, 'help':'reservationSet client_id sku qty'}, 'stock.set': {'handler': Product.stockSetCmd, 'args':2, 'help':'stockSet sku stock'}, 'stock.dec': {'handler': Product.stockDecCmd, 'args':2, 'help':'stockDec sku qty'}, 'stock.get': {'handler': Product.stockGetCmd, 'args':1, 'help':'stockGet sku'}, 'product.total': {'handler': Product.totalCmd, 'args':0, 'help':'status'} } for c in commands: Command.register( commands[c]['handler'], c, commands[c]['args'], commands[c]['help'] ) Event.register('db.save', Product.saveDb) Event.register('db.load', Product.loadDb)
def join(self): try: stdout, stderr, returncode = SubProcessTask.join(self) event = Event() event.service = self.name event.ttl = self.ttl * self.ttl_multiplier event.host = self.host event.tags = self.tags event.attributes = self.attributes output, attributes = self.parse_nagios_output(stdout) event.description = "Note: %s\nCommand: %s\nOutput: %s\n" % (self.note, self.raw_command, output) if returncode in self.exitcodes: event.state = self.exitcodes[returncode] else: event.state = 'unknown' if attributes: if 'metric' in self.config and self.config['metric'] in attributes: metric_key = self.config['metric'] else: metric_key = attributes.keys()[0] event.attributes.update(attributes) event.metric = float(NUMERIC_REGEX.match(attributes[metric_key]).group(1)) self.events.append(event) except Exception as e: log.error("Exception joining task '%s':\n%s" % (self.name, str(e)))
def writeRequest(self, pin, params): notify( Event(source=self.name, endpoint='v%d' % pin, data=float(params.pop(0))))
from event import Event from pprint import pprint as pr from selenium import webdriver # def sele_phantom(): # url = "https://tanzu.vmware.com/content/webinars/jun-30-making-k8s-great-improving-the-kubernetes-developer-experience?utm_campaign=Global_BT_Q221_Improving-K8s-Developer-Experience&utm_source=twitter&utm_medium=social" # browser = webdriver.PhantomJS() # browser.get(url) # # iframe = browser.find_element_by_tag_name("iframe") # print(iframe) # browser.switch_to.default_content() # browser.switch_to.frame(iframe) # # iframe_source = browser.page_source # # print(iframe_source) # # print(browser.current_url) if __name__ == "__main__": keywords = "kubernetes" event = Event(keywords) # start crawler data = event.start() pr(data) # sele_phantom()
def __add_to_queue(self, event_type, event_data): self._event_queue.put(Event(event_type, event_data, self._process_id))
def emit(self, event): """ client or event""" msg = Event.event_to_message(event) self._emit_event_socket.send(msg) return
self.transmit(newevent) if self.father == self.id: newevent = Event("Final", self.clock + 1.0, self.father, self.id) self.transmit(newevent) if event.getName() == "FINAL": print("Final del algoritmo") # ---------------------------------------------------------------------------------------- # "main()" # ---------------------------------------------------------------------------------------- # construye una instancia de la clase Simulation recibiendo como parametros el nombre del # archivo que codifica la lista de adyacencias de la grafica y el tiempo max. de simulacion if len(sys.argv) != 2: print("Please supply a file name") raise SystemExit(1) experiment = Simulation(sys.argv[1], 500) # asocia un pareja proceso/modelo con cada nodo de la grafica for i in range(1, len(experiment.graph) + 1): m = AlgorithmDFS() experiment.setModel(m, i) # inserta un evento semilla en la agenda y arranca seed = Event("DESCUBRE", 0.0, 1, 1) experiment.init(seed) experiment.run()
def generate_orders_from_signals(self, portfolio, signals_event): """ Generate random orders, but subject to the same restrictions as ml_strategy. """ date = signals_event.date used_signal_ids = portfolio.broker.blotter.used_signal_ids signals = [] for signal in signals_event.data: if signal.signal_id not in used_signal_ids: signals.append(signal) stocks_with_position_in = set( [trade.ticker for trade in portfolio.broker.blotter.active_trades]) portfolio_value = portfolio.calculate_portfolio_value() available_funds = min( portfolio_value * self.max_percent_to_invest_each_period, portfolio.balance - self.minimum_balance) # short_funds = portfolio_value * self.percentage_short - portfolio.get_dollar_amount_short() # remaining_funds = available_funds - short_funds cur_num_trades = portfolio.get_num_trades() if cur_num_trades < (self.max_positions * 0.5): num_orders = min(self.max_orders_per_period, self.max_positions - cur_num_trades) num_short_orders = 0 else: num_orders = min(self.max_orders_per_period, self.max_positions - cur_num_trades) num_short_orders = max( self.num_short_positions - portfolio.get_num_short_trades(), 0) top_signals = self._get_top_signals(signals, num_short_orders, num_orders, stocks_with_position_in) allocations = self._get_allocation_between_signals(top_signals) orders = [] for allocation, signal in zip(allocations, top_signals): try: prev_ticker_data = portfolio.broker.market_data.prev_for_ticker( signal.ticker) # TODO: implement except: continue # Very bad error handling... ticker_data = portfolio.broker.market_data.current_for_ticker( signal.ticker) # we generate order at the open ticker_price = ticker_data[self.time_of_day] # direction = int(Decimal(signal.direction).quantize(0, ROUND_HALF_UP)) if signal.direction == 1: dollar_amount = min( portfolio_value * self.max_position_size_percentage, available_funds * allocation) elif signal.direction == -1: # Short trades require more money as 150% or order size must be put in a margin account dollar_amount = min( (portfolio_value * self.max_position_size_percentage) / (1 + self.initial_margin_requirement), (available_funds * allocation) / (1 + self.initial_margin_requirement)) number_of_stocks = int(dollar_amount / ticker_price) prev_days_volume = prev_ticker_data["volume"] number_of_stocks = min( int(prev_days_volume * self.volume_limit), int(dollar_amount / ticker_price) ) # TODO: this may free up funds to use for other positions... if number_of_stocks == int(prev_days_volume * self.volume_limit): self.logger.logr.warning( "ML_STRATEGY: Was restricted to order only {} percent of last days volume (vol: {}, price: {}, total: {}) \ for ticker {} at date {} given signal {}.".format( self.volume_limit * 100, prev_days_volume, ticker_price, prev_days_volume * ticker_price, signal.ticker, date, signal.signal_id)) order_dollar_amount = number_of_stocks * ticker_price if order_dollar_amount < self.min_order_size_limit: self.logger.logr.warning( "ML_STRATEGY: Order generation was aborted because order size was too low for ticker {} on date {} given signal {}." .format(signal.ticker, date, signal.signal_id)) continue order = Order( order_id=self.get_order_id(), ticker=signal.ticker, amount=number_of_stocks * signal.direction, date=date, signal=signal, take_profit=signal.ewmstd * self.ptSl[0], stop_loss=signal.ewmstd * self.ptSl[1], # Can be set to stop out early timeout=date + relativedelta(months=1), # signal.timeout ) orders.append(order) # spent += order_dollar_amount if len(orders) == 0: return None else: return Event(event_type="ORDERS", data=orders, date=signals_event.date)
def on_play_down(self): logging.debug("trying to move on") self._pause_thread() ev = Event(EventEnum.TRANSITION_TO_NEXT) self.add_event(ev)
self.padre = event.getSource() self.go() elif(event.getName() == "REG"): self.go() else: #Solo entra cuando el mensaje es AVISO para eliminar la fuente if(event.getSource() in self.sinVisitar): self.sinVisitar.remove(event.getSource()) # ---------------------------------------------------------------------------------------- # "main()" # ---------------------------------------------------------------------------------------- # construye una instancia de la clase Simulation recibiendo como parametros el nombre del # archivo que codifica la lista de adyacencias de la grafica y el tiempo max. de simulacion if len(sys.argv) != 2: print ("Please supply a file name") raise SystemExit(1) experiment = Simulation(sys.argv[1], 100) # asocia un pareja proceso/modelo con cada nodo de la grafica for i in range(1,len(experiment.graph)+1): m = DFSCidol() experiment.setModel(m, i) # inserta un evento semilla en la agenda y arranca seed = Event("DESC", 0.0, 1, 1) experiment.init(seed) #seed2 = Event("DESC", 0.0, 2,2) #experiment.init(seed2) experiment.run()
class Looper(object): """Creates a set of analyzers, and schedules the event processing.""" def __init__(self, name, config, nEvents=None, firstEvent=0, nPrint=0, timeReport=False, quiet=False, memCheckFromEvent=-1, stopFlag=None): """Handles the processing of an event sample. An Analyzer is built for each Config.Analyzer present in sequence. The Looper can then be used to process an event, or a collection of events. Parameters: name : name of the Looper, will be used as the output directory name config : process configuration information, see Config nEvents : number of events to process. Defaults to all. firstEvent : first event to process. Defaults to the first one. nPrint : number of events to print at the beginning stopFlag: it should be a multiprocessing.Value instance, that is set to 1 when this thread, or any other, receives a SIGUSR2 to ask for a graceful job termination. In this case, the looper will also set up a signal handler for SIGUSR2. (if set to None, nothing of all this happens) """ self.config = config self.name = self._prepareOutput(name) self.outDir = self.name self.logger = logging.getLogger(self.name) self.logger.addHandler( logging.FileHandler('/'.join([self.name, 'log.txt']))) self.logger.propagate = False if not quiet: self.logger.addHandler(logging.StreamHandler(sys.stdout)) self.cfg_comp = config.components[0] self.classes = {} # keep track of analyzers in a list for sequential event processing self._analyzers = [] # and in a dict for easy user access self._analyzer_dict = dict() for anacfg in self.config.sequence: anaobj = self._build(anacfg) self._analyzers.append(anaobj) self._analyzer_dict[anacfg.name] = anaobj self.nEvents = nEvents self.firstEvent = firstEvent self.nPrint = int(nPrint) self.timeReport = [{ 'time': 0.0, 'events': 0 } for a in self._analyzers] if timeReport else False self.memReportFirstEvent = memCheckFromEvent self.memLast = 0 self.stopFlag = stopFlag if stopFlag: import signal def doSigUsr2(sig, frame): print 'SIGUSR2 received, signaling graceful stop' self.stopFlag.value = 1 signal.signal(signal.SIGUSR2, doSigUsr2) tree_name = None if (hasattr(self.cfg_comp, 'tree_name')): tree_name = self.cfg_comp.tree_name if len(self.cfg_comp.files) == 0: errmsg = 'please provide at least an input file in the files attribute of this component\n' + str( self.cfg_comp) raise ValueError(errmsg) if hasattr(config, "preprocessor") and config.preprocessor is not None: self.cfg_comp = config.preprocessor.run(self.cfg_comp, self.outDir, firstEvent, nEvents) if hasattr(self.cfg_comp, "options"): print self.cfg_comp.files, self.cfg_comp.options self.events = config.events_class(self.cfg_comp.files, tree_name, options=self.cfg_comp.options) else: self.events = config.events_class(self.cfg_comp.files, tree_name) if hasattr(self.cfg_comp, 'fineSplit'): fineSplitIndex, fineSplitFactor = self.cfg_comp.fineSplit if fineSplitFactor > 1: if len(self.cfg_comp.files) != 1: raise RuntimeError( "Any component with fineSplit > 1 is supposed to have just a single file, while %s has %s" % (self.cfg_comp.name, self.cfg_comp.files)) totevents = min(len(self.events), int(nEvents)) if ( nEvents and int(nEvents) not in [-1, 0]) else len( self.events) self.nEvents = int(ceil(totevents / float(fineSplitFactor))) self.firstEvent = firstEvent + fineSplitIndex * self.nEvents if self.firstEvent + self.nEvents >= totevents: self.nEvents = totevents - self.firstEvent #print "For component %s will process %d events starting from the %d one, ending at %d excluded" % (self.cfg_comp.name, self.nEvents, self.firstEvent, self.nEvents + self.firstEvent) # self.event is set in self.process self.event = None services = dict() for cfg_serv in config.services: service = self._build(cfg_serv) services[cfg_serv.name] = service # would like to provide a copy of the config to the setup, # so that analyzers cannot modify the config of other analyzers. # but cannot copy the autofill config. self.setup = Setup(config, services) def _build(self, cfg): try: theClass = cfg.class_object except AttributeError: errfgmt = 'an object of class {cfg_class}'.format( cfg_class=cfg.__class__) if type(cfg) is type: errfgmt = 'a class named {class_name}'.format( class_name=cfg.__name__) err = ''' The looper is trying to build an analyzer configured by {errfgmt}. Make sure that the configuration object is of class cfg.Analyzer. '''.format(errfgmt=errfgmt) raise ValueError(err) obj = theClass(cfg, self.cfg_comp, self.outDir) return obj #---------------------------------------------------------------------- def analyzer(self, name): """@return: analyzer with this name.""" return self._analyzer_dict[name] def _prepareOutput(self, name): index = 0 tmpname = name while True and index < 2000: try: # print 'mkdir', self.name os.mkdir(tmpname) break except OSError: # failed to create the directory # is it empty? if not os.listdir(tmpname): break # it is, so use it else: # if not we append a number to the directory name index += 1 tmpname = '%s_%d' % (name, index) if index == 2000: raise ValueError( "More than 2000 output folder with same name or 2000 attempts failed, please clean-up, change name or check permissions" ) return tmpname def loop(self): """Loop on a given number of events. At the beginning of the loop, Analyzer.beginLoop is called for each Analyzer. At each event, self.process is called. At the end of the loop, Analyzer.endLoop is called. """ nEvents = self.nEvents firstEvent = self.firstEvent iEv = firstEvent self.nEvProcessed = 0 if nEvents is None or int(nEvents) - firstEvent > len(self.events): nEvents = len(self.events) - firstEvent else: nEvents = int(nEvents) self.logger.info( 'starting loop at event {firstEvent} '\ 'to process {nEvents} events.'.format(firstEvent=firstEvent, nEvents=nEvents)) self.logger.info(str(self.cfg_comp)) for analyzer in self._analyzers: analyzer.beginLoop(self.setup) if hasattr(self.events, '__getitem__'): # events backend supports indexing, e.g. CMS, FCC, bare root for iEv in range(firstEvent, firstEvent + nEvents): if iEv % 100 == 0: if not hasattr(self, 'start_time'): self.logger.info('event {iEv}'.format(iEv=iEv)) self.start_time = timeit.default_timer() self.start_time_event = iEv else: self.logger.warning( 'event %d (%.1f ev/s)' % (iEv, (iEv - self.start_time_event) / float(timeit.default_timer() - self.start_time))) try: self.process(iEv) self.nEvProcessed += 1 if iEv < self.nPrint: self.logger.info(self.event.__str__()) if self.stopFlag and self.stopFlag.value: print 'stopping gracefully at event %d' % (iEv) break except UserStop as err: print 'Stopped loop following a UserStop exception:' print err break else: # events backend does not support indexing, e.g. LCIO iEv = 0 for ii, event in enumerate(self.events): if ii < firstEvent: continue iEv += 1 if iEv % 100 == 0: if not hasattr(self, 'start_time'): self.logger.warning('event {iEv}'.format(iEv=iEv)) self.start_time = timeit.default_timer() self.start_time_event = iEv else: self.logger.info( 'event %d (%.1f ev/s)' % (iEv, (iEv - self.start_time_event) / float(timeit.default_timer() - self.start_time))) try: self.event = Event(iEv, event, self.setup) self.iEvent = iEv self._run_analyzers_on_event() self.nEvProcessed += 1 if iEv < self.nPrint: self.logger.info(self.event.__str__()) if self.stopFlag and self.stopFlag.value: print 'stopping gracefully at event %d' % (iEv) break except UserStop as err: print 'Stopped loop following a UserStop exception:' print err break warning = self.logger.warning warning('') warning(self.cfg_comp) warning('') for analyzer in self._analyzers: analyzer.endLoop(self.setup) if self.timeReport: allev = max([x['events'] for x in self.timeReport]) warning( "\n ---- TimeReport (all times in ms; first evt is skipped) ---- " ) warning("%9s %9s %9s %9s %6s %s" % ("processed", "all evts", "time/proc", " time/all", " [%] ", "analyer")) warning("%9s %9s %9s %9s %6s %s" % ("---------", "--------", "---------", "---------", " -----", "-------------")) sumtime = sum(rep['time'] for rep in self.timeReport) passev = self.timeReport[-1]['events'] for ana, rep in zip(self._analyzers, self.timeReport): timePerProcEv = rep['time'] / (rep['events'] - 1) if rep['events'] > 1 else 0 timePerAllEv = rep['time'] / (allev - 1) if allev > 1 else 0 fracAllEv = rep['time'] / sumtime warning("%9d %9d %10.2f %10.2f %5.1f%% %s" % (rep['events'], allev, 1000 * timePerProcEv, 1000 * timePerAllEv, 100.0 * fracAllEv, ana.name)) totPerProcEv = sumtime / (passev - 1) if passev > 1 else 0 totPerAllEv = sumtime / (allev - 1) if allev > 1 else 0 warning("%9s %9s %9s %9s %s" % ("---------", "--------", "---------", "---------", "-------------")) warning("%9d %9d %10.2f %10.2f %5.1f%% %s" % (passev, allev, 1000 * totPerProcEv, 1000 * totPerAllEv, 100.0, "TOTAL")) warning("") logfile = open('/'.join([self.name, 'log.txt']), 'a') logfile.write('number of events processed: {nEv}\n'.format( nEv=self.nEvProcessed)) logfile.close() def process(self, iEv): """Run event processing for all analyzers in the sequence. This function can be called directly from the python interpreter, to jump to a given event and process it. """ if not hasattr(self.events, '__getitem__'): msg = ''' Your events backend, of type {evclass} does not support indexing. Therefore, you cannot directly access a given event using Loop.process. However, you may still iterate on your events using Loop.loop, possibly skipping a number of events at the beginning. '''.format(evclass=self.events.__class__) raise TypeError(msg) self.event = Event(iEv, self.events[iEv], self.setup) self.iEvent = iEv return self._run_analyzers_on_event() def _run_analyzers_on_event(self): '''Run all analysers on the current event, self.event. Returns a tuple (success?, last_analyzer_name). ''' for i, analyzer in enumerate(self._analyzers): if not analyzer.beginLoopCalled: analyzer.beginLoop(self.setup) start = timeit.default_timer() if self.memReportFirstEvent >= 0 and iEv >= self.memReportFirstEvent: memNow = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss if memNow > self.memLast: print "Mem Jump detected before analyzer %s at event %s. RSS(before,after,difference) %s %s %s " % ( analyzer.name, iEv, self.memLast, memNow, memNow - self.memLast) self.memLast = memNow ret = analyzer.process(self.event) if self.memReportFirstEvent >= 0 and iEv >= self.memReportFirstEvent: memNow = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss if memNow > self.memLast: print "Mem Jump detected in analyzer %s at event %s. RSS(before,after,difference) %s %s %s " % ( analyzer.name, iEv, self.memLast, memNow, memNow - self.memLast) self.memLast = memNow if self.timeReport: self.timeReport[i]['events'] += 1 if self.timeReport[i]['events'] > 0: self.timeReport[i]['time'] += timeit.default_timer( ) - start if ret == False: return (False, analyzer.name) return (True, analyzer.name) def write(self): """Writes all analyzers. See Analyzer.Write for more information. """ for analyzer in self._analyzers: analyzer.write(self.setup) self.setup.close()
def loop(self): """Loop on a given number of events. At the beginning of the loop, Analyzer.beginLoop is called for each Analyzer. At each event, self.process is called. At the end of the loop, Analyzer.endLoop is called. """ nEvents = self.nEvents firstEvent = self.firstEvent iEv = firstEvent self.nEvProcessed = 0 if nEvents is None or int(nEvents) - firstEvent > len(self.events): nEvents = len(self.events) - firstEvent else: nEvents = int(nEvents) self.logger.info( 'starting loop at event {firstEvent} '\ 'to process {nEvents} events.'.format(firstEvent=firstEvent, nEvents=nEvents)) self.logger.info(str(self.cfg_comp)) for analyzer in self._analyzers: analyzer.beginLoop(self.setup) if hasattr(self.events, '__getitem__'): # events backend supports indexing, e.g. CMS, FCC, bare root for iEv in range(firstEvent, firstEvent + nEvents): if iEv % 100 == 0: if not hasattr(self, 'start_time'): self.logger.info('event {iEv}'.format(iEv=iEv)) self.start_time = timeit.default_timer() self.start_time_event = iEv else: self.logger.warning( 'event %d (%.1f ev/s)' % (iEv, (iEv - self.start_time_event) / float(timeit.default_timer() - self.start_time))) try: self.process(iEv) self.nEvProcessed += 1 if iEv < self.nPrint: self.logger.info(self.event.__str__()) if self.stopFlag and self.stopFlag.value: print 'stopping gracefully at event %d' % (iEv) break except UserStop as err: print 'Stopped loop following a UserStop exception:' print err break else: # events backend does not support indexing, e.g. LCIO iEv = 0 for ii, event in enumerate(self.events): if ii < firstEvent: continue iEv += 1 if iEv % 100 == 0: if not hasattr(self, 'start_time'): self.logger.warning('event {iEv}'.format(iEv=iEv)) self.start_time = timeit.default_timer() self.start_time_event = iEv else: self.logger.info( 'event %d (%.1f ev/s)' % (iEv, (iEv - self.start_time_event) / float(timeit.default_timer() - self.start_time))) try: self.event = Event(iEv, event, self.setup) self.iEvent = iEv self._run_analyzers_on_event() self.nEvProcessed += 1 if iEv < self.nPrint: self.logger.info(self.event.__str__()) if self.stopFlag and self.stopFlag.value: print 'stopping gracefully at event %d' % (iEv) break except UserStop as err: print 'Stopped loop following a UserStop exception:' print err break warning = self.logger.warning warning('') warning(self.cfg_comp) warning('') for analyzer in self._analyzers: analyzer.endLoop(self.setup) if self.timeReport: allev = max([x['events'] for x in self.timeReport]) warning( "\n ---- TimeReport (all times in ms; first evt is skipped) ---- " ) warning("%9s %9s %9s %9s %6s %s" % ("processed", "all evts", "time/proc", " time/all", " [%] ", "analyer")) warning("%9s %9s %9s %9s %6s %s" % ("---------", "--------", "---------", "---------", " -----", "-------------")) sumtime = sum(rep['time'] for rep in self.timeReport) passev = self.timeReport[-1]['events'] for ana, rep in zip(self._analyzers, self.timeReport): timePerProcEv = rep['time'] / (rep['events'] - 1) if rep['events'] > 1 else 0 timePerAllEv = rep['time'] / (allev - 1) if allev > 1 else 0 fracAllEv = rep['time'] / sumtime warning("%9d %9d %10.2f %10.2f %5.1f%% %s" % (rep['events'], allev, 1000 * timePerProcEv, 1000 * timePerAllEv, 100.0 * fracAllEv, ana.name)) totPerProcEv = sumtime / (passev - 1) if passev > 1 else 0 totPerAllEv = sumtime / (allev - 1) if allev > 1 else 0 warning("%9s %9s %9s %9s %s" % ("---------", "--------", "---------", "---------", "-------------")) warning("%9d %9d %10.2f %10.2f %5.1f%% %s" % (passev, allev, 1000 * totPerProcEv, 1000 * totPerAllEv, 100.0, "TOTAL")) warning("") logfile = open('/'.join([self.name, 'log.txt']), 'a') logfile.write('number of events processed: {nEv}\n'.format( nEv=self.nEvProcessed)) logfile.close()
def parse_event(event_containers): """ Parse event container to summary, date_time, location, description, url and phone :param event_containers: list of events (with tags) :return: Events list """ csv_file = csv.writer(open('index.csv', 'a')) csv_file.writerow([ 'Summary', 'DateTime', 'Location', 'Description', 'Url', 'Phone', 'Last update' ]) events = [] for e in event_containers: try: summary = e.find('h3', class_="listing-item__title").text.strip() except: summary = None try: datetime_format = parse_date(e) except: datetime_format = None # load the "Read more" link try: link = e.a['href'] except: link = None page = requests.get(link) soup = BeautifulSoup(page.text, 'html.parser') try: info_brand = soup.findAll('span', class_="info-content") except: info_brand = None try: location, phone = info_brand[1].text.strip( ), info_brand[0].text.strip() except: location = phone = None try: description = parse_info(e) except: description = None updated = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") # export to csv file csv_file.writerow([ summary, datetime_format, location, description, link, phone, updated ]) # to Event object and append events.append( Event(summary, location, description, datetime_format, link, updated).to_google_format()) return events
host = socket.gethostname() port = 5538 #########TESTING db = Database(True) from event import Event from zone import Zone z = Zone(1) z2 = Zone(2) z3 = Zone(3) db.add_zone(z) db.add_zone(z2) test_event0 = Event(3.0, 4.0, 'Sunday', 1, 1) test_event1 = Event(1.0, 2.0, 'Sunday', 1, 1) test_event2 = Event(2.0, 3.0, 'Sunday', 1, 1) test_event3 = Event(1.5, 2.5, 'Sunday', 1, 1) test_event4 = Event(1.0, 2.0, 'Sunday', 1, 2) test_event5 = Event(1.0, 2.0, 'Sunday', 1, 3) test_event6 = Event(1.5, 3.0, 'Sunday', 1, 3) test_event7 = Event(1.0, 2.0, 'Sunday', 1, 3) test_event8 = Event(3.0, 4.0, 'Everyday', 2, 1) test_event9 = Event(1.0, 2.0, 'Everyday', 2, 1) test_event10 = Event(2.0, 3.0, 'Everyday', 2, 1) test_event11 = Event(1.5, 2.5, 'Everyday', 2, 1) test_event12 = Event(1.0, 2.0, 'Everyday', 2, 2) test_event13 = Event(1.0, 2.0, 'Everyday', 2, 3) test_event14 = Event(1.5, 3.0, 'Everyday', 2, 3)
for event in events: print(event) start_time = time.time() runtime = cfg.test_time actions = 0 csv_file = cfg.log_dir + "/" + str(start_time) + "-test.csv" with open(csv_file, "w+") as p_f: p_f.write("time,actions,technique,iteration_time,window_name\n") last_event = Event(0, EventType.NONE) while is_running(start_time, runtime, actions, running): iteration_time = time.time() exec_time = time.time() - start_time #os.system('wmctrl -c "firefox"') w_name, w_class, app_x, app_y, app_w, app_h = get_focused_window(cfg.window_name) no_focus = 0 while app_w == 0 or w_class is None:
#!/usr/bin/python import random from event import Event from average import Average e = Event() a = Average() def add_random_sample_delegate(x): a.add_sample(x + 10 * (-.5 + random.random())) def add_sample_delegate(x): a.add_sample(x) e.add_event_listener(add_sample_delegate) e.add_event_listener(add_random_sample_delegate) e.add_event(2) e.add_event(200) print a.calculate()
import matplotlib.pyplot as plt import numpy as np import os from protozfits import SimpleFile from event import Event from pedestal import PedestalSimple plt.rcParams.update({'font.size': 35}) f = SimpleFile(os.path.join("..", "data", "Run021.1.fits.fz")) N = 10000 ped = PedestalSimple() for i in range(1, N): ev = next(f.Events) Ev = Event(ev) Ev.read() ped.fill_pedestal_event(Ev) ped.finalize_pedestal() x = np.linspace(1, 1024, 1024) plt.errorbar(x, ped.mean_value_channel_high_gain[0, :1024], yerr=ped.rms_high_gain[0, :1024], fmt='o') plt.xlabel("DRS 4 kondensator") plt.ylabel("Offset komórkki wraz z RMS") plt.grid(True) plt.show()
def add_event(self, date, max_participants, description): now = datetime.now() if datetime.strptime(date, '%d.%m.%y') >= now: self.events.append(Event(date, max_participants, description)) else: return False
def server_put_post(server_id=None): name = flask.request.json['name'] name = ''.join(x for x in name if x.isalnum() or x in NAME_SAFE_CHARS) network = flask.request.json['network'] interface = flask.request.json['interface'] port = flask.request.json['port'] protocol = flask.request.json['protocol'].lower() local_network = flask.request.json['local_network'] if local_network: local_network = local_network public_address = flask.request.json['public_address'] public_address = ''.join(x for x in public_address if x.isalnum() or x == '.') debug = True if flask.request.json['debug'] else False otp_auth = True if flask.request.json['otp_auth'] else False lzo_compression = True if flask.request.json['lzo_compression'] else False # Network network_split = network.split('/') if len(network_split) != 2: return _network_not_valid() address = network_split[0].split('.') if len(address) != 4: return _network_not_valid() for i, value in enumerate(address): try: address[i] = int(value) except ValueError: return _network_not_valid() if address[0] != 10: return _network_not_valid() if address[1] > 255 or address[1] < 0 or \ address[2] > 255 or address[2] < 0: return _network_not_valid() if address[3] != 0: return _network_not_valid() try: subnet = int(network_split[1]) except ValueError: return _network_not_valid() if subnet < 8 or subnet > 24: return _network_not_valid() # Interface if interface[:3] != 'tun': return _interface_not_valid() try: interface_num = int(interface[3:]) except ValueError: return _interface_not_valid() if interface_num > 64: return _interface_not_valid() interface = interface[:3] + str(interface_num) # Port try: port = int(port) except ValueError: return _port_not_valid() if port < 1 or port > 65535: return _port_not_valid() # Protocol if protocol not in ['udp', 'tcp']: return utils.jsonify( { 'error': PROTOCOL_NOT_VALID, 'error_msg': PROTOCOL_NOT_VALID_MSG, }, 400) # Local network if local_network: local_network_split = local_network.split('/') if len(local_network_split) != 2: return _local_network_not_valid() address = local_network_split[0].split('.') if len(address) != 4: return _local_network_not_valid() for i, value in enumerate(address): try: address[i] = int(value) except ValueError: return _local_network_not_valid() if address[0] > 255 or address[0] < 0 or \ address[1] > 255 or address[1] < 0 or \ address[2] > 255 or address[2] < 0 or \ address[3] > 254 or address[3] < 0: return _local_network_not_valid() try: subnet = int(local_network_split[1]) except ValueError: return _local_network_not_valid() if subnet < 8 or subnet > 30: return _local_network_not_valid() for server in Server.get_servers(): if server.id == server_id: continue elif server.network == network: return utils.jsonify( { 'error': NETWORK_IN_USE, 'error_msg': NETWORK_IN_USE_MSG, }, 400) elif server.interface == interface: return utils.jsonify( { 'error': INTERFACE_IN_USE, 'error_msg': INTERFACE_IN_USE_MSG, }, 400) elif server.port == port and server.protocol == protocol: return utils.jsonify( { 'error': PORT_PROTOCOL_IN_USE, 'error_msg': PORT_PROTOCOL_IN_USE_MSG, }, 400) if not server_id: server = Server( name=name, network=network, interface=interface, port=port, protocol=protocol, local_network=local_network, public_address=public_address, otp_auth=otp_auth, lzo_compression=lzo_compression, debug=debug, ) else: server = Server(id=server_id) if server.status: return utils.jsonify( { 'error': SERVER_NOT_OFFLINE, 'error_msg': SERVER_NOT_OFFLINE_MSG, }, 400) server.name = name server.network = network server.interface = interface server.port = port server.protocol = protocol server.local_network = local_network server.public_address = public_address server.otp_auth = otp_auth server.lzo_compression = lzo_compression server.debug = debug server.commit() Event(type=USERS_UPDATED) return utils.jsonify({})
class StrategyEngineManager(QtWidgets.QWidget): """引擎管理组件""" signal = QtCore.Signal(type(Event())) #---------------------------------------------------------------------- def __init__(self, omEngine, parent=None): """Constructor""" super(StrategyEngineManager, self).__init__(parent) self.engine = omEngine.strategyEngine self.eventEngine = omEngine.eventEngine self.strategyLoaded = False self.initUi() self.registerEvent() # 记录日志 self.engine.writeLog(u'期权策略引擎启动') #---------------------------------------------------------------------- def initUi(self): """初始化界面""" self.setWindowTitle(u'期权策略') # 按钮 loadButton = QtWidgets.QPushButton(u'加载策略') initAllButton = QtWidgets.QPushButton(u'全部初始化') startAllButton = QtWidgets.QPushButton(u'全部启动') stopAllButton = QtWidgets.QPushButton(u'全部停止') loadButton.clicked.connect(self.load) initAllButton.clicked.connect(self.initAll) startAllButton.clicked.connect(self.startAll) stopAllButton.clicked.connect(self.stopAll) # 滚动区域,放置所有的StrategyManager self.scrollArea = QtWidgets.QScrollArea() self.scrollArea.setWidgetResizable(True) # 日志监控 self.ctaLogMonitor = QtWidgets.QTextEdit() self.ctaLogMonitor.setReadOnly(True) self.ctaLogMonitor.setMaximumHeight(200) # 设置布局 hbox2 = QtWidgets.QHBoxLayout() hbox2.addWidget(loadButton) hbox2.addWidget(initAllButton) hbox2.addWidget(startAllButton) hbox2.addWidget(stopAllButton) hbox2.addStretch() vbox = QtWidgets.QVBoxLayout() vbox.addLayout(hbox2) vbox.addWidget(self.scrollArea) vbox.addWidget(self.ctaLogMonitor) self.setLayout(vbox) #---------------------------------------------------------------------- def initStrategyManager(self): """初始化策略管理组件界面""" w = QtWidgets.QWidget() vbox = QtWidgets.QVBoxLayout() for name in self.engine.strategyDict.keys(): strategyManager = StrategyManager(self.engine, self.eventEngine, name) vbox.addWidget(strategyManager) vbox.addStretch() w.setLayout(vbox) self.scrollArea.setWidget(w) #---------------------------------------------------------------------- def initAll(self): """全部初始化""" self.engine.initAll() #---------------------------------------------------------------------- def startAll(self): """全部启动""" self.engine.startAll() #---------------------------------------------------------------------- def stopAll(self): """全部停止""" self.engine.stopAll() #---------------------------------------------------------------------- def load(self): """加载策略""" if not self.strategyLoaded: self.engine.loadSetting() self.initStrategyManager() self.strategyLoaded = True self.engine.writeLog(u'策略加载成功') #---------------------------------------------------------------------- def updateLog(self, event): """更新相关日志""" log = event.dict_['data'] content = '\t'.join([log.logTime, log.logContent]) self.ctaLogMonitor.append(content) #---------------------------------------------------------------------- def registerEvent(self): """注册事件监听""" self.signal.connect(self.updateLog) self.eventEngine.register(EVENT_OM_STRATEGYLOG, self.signal.emit)
Se codifica aparte para que no aparezca en el menú de ayuda. ''' bot.send_message( chat_id=update.message.chat_id, text=emojize( ":confused: Lo siento, no entiendo esa orden", use_aliases=True, ), parse_mode=MARKDOWN, ) if __name__ == '__main__': logging.info('Obteniendo informacion del evento') E = Event(config('EVENT_TAG', default='pydaytf18')) logging.info('OK, evento cargado') updater = Updater(config("TELEGRAM_BOT_TOKEN")) logging.info('PtDayBot starts') dispatcher = updater.dispatcher for name in _kernel: functor = _kernel[name] sig = inspect.signature(functor) if len(sig.parameters) == 2: cmd_handler = CommandHandler(name, functor) else: cmd_handler = CommandHandler(name, functor, pass_args=True) dispatcher.add_handler(cmd_handler) # No hemos capturado ningún comando anes, debe estar mal escrito dispatcher.add_handler(MessageHandler(Filters.command, no_entiendo))
def select_entity(self, current, previous): self.context['selected_entity'] = str(current.text()) new_event = Event('selected_entity', entity=self.context['selected_entity']) EVENT_MANAGER.fire_event(new_event)
def receive(self, event): nombre = event.name() if event.getName() == "Descubre": print("SOY", self.id) if self.visited == False: self.visited = True self.father = event.getSource() aux = event.getSource() print("Recibo Descubre de", aux, "papá", self.father, "en el tiempo", self.clock) if aux != self.father: if aux != self.id: self.sin_visitar.remove(aux) if len(self.sin_visitar) > 0: for i in range(len(self.neighbors)): if self.neighbors[i] != self.father: newevent = Event("AVISO", self.clock + 1.0, self.neighbors[i], self.id) self.transmit(newevent) print("envio AVISO a los nodos vecinos menos padre") if len(self.sin_visitar) > 0: aleat = random.randint(0, len(self.sin_visitar) - 1) newevent = Event("Descubre", self.clock + 1.0, self.sin_visitar[aleat], self.id) del self.sin_visitar[aleat] self.transmit(newevent) else: if self.father != self.id: newevent = Event("Regresa", self.clock + 1.0, self.father, self.id) self.transmit(newevent) if self.hijos == []: for i in range(len(self.neighbors)): if self.neighbors[i] != self.father: newevent = Event("AVISO", self.clock + 1.0, self.neighbors[i], self.id) self.transmit(newevent) print("envio AVISO a los nodos vecinos menos padre") if event.getName() == "AVISO": print( "SOY", self.id, "RECIBO AVISO DE", event.getSource(), "en el tiempo", self.clock, ) aux = event.getSource() self.sin_visitar.remove(aux) print("Lista sin visitar", self.sin_visitar) if event.getName() == "REGRESA": print("SOY", self.id, "Recibo Regresa de", event.getSource(), "En el tiempo", self.clock) self.hijos.append(event.getSource()) print("Mis hijos", self.hijos) if len(self.sin_visitar) > 0: aleat = random.randint(0, len(self.sin_visitar) - 1) newevent = Event("Descubre", self.clock + 1.0, self.sin_visitar[aleat], self.id) del self.sin_visitar[aleat] self.transmit(newevent) else: if self.father != self.id: newevent = Event("Regresa", self.clock + 1.0, self.father, self.id) self.transmit(newevent) if self.father == self.id: newevent = Event("Final", self.clock + 1.0, self.father, self.id) self.transmit(newevent) if event.getName() == "FINAL": print("Final del algoritmo")
self._broadcast_event_socket.send(strevent) logger.debug("[server] broadcast message: %s" % strevent) if __name__ == '__main__': import time, datetime, sys def simpletest(event): print str(datetime.datetime.now()), event ee = ZMQEventEngine() ee.register(Event.TIMER, simpletest) timer = Timer(ee) ee.start() timer.start() event = Event(route=Event.TIMER) timer.stop() time.sleep(2) timer.resume() time.sleep(2) timer.stop() client = ZMQEventEngine() event = Event(route=Event.TIMER, args={'data': 'from client'}) client.start() client.emit(event) try: while True: time.sleep(1) except KeyboardInterrupt: ee.stop()
class StrategyManager(QtWidgets.QGroupBox): """策略管理组件""" signal = QtCore.Signal(type(Event())) #---------------------------------------------------------------------- def __init__(self, engine, eventEngine, name, parent=None): """Constructor""" super(StrategyManager, self).__init__(parent) self.engine = engine self.eventEngine = eventEngine self.name = name self.initUi() self.updateMonitor() self.registerEvent() #---------------------------------------------------------------------- def initUi(self): """初始化界面""" self.setTitle(self.name) self.paramMonitor = ValueMonitor(True, self) self.varMonitor = ValueMonitor(False, self) height = 65 self.paramMonitor.setFixedHeight(height) self.varMonitor.setFixedHeight(height) buttonInit = QtWidgets.QPushButton(u'初始化') buttonStart = QtWidgets.QPushButton(u'启动') buttonStop = QtWidgets.QPushButton(u'停止') buttonInit.clicked.connect(self.init) buttonStart.clicked.connect(self.start) buttonStop.clicked.connect(self.stop) hbox1 = QtWidgets.QHBoxLayout() hbox1.addWidget(buttonInit) hbox1.addWidget(buttonStart) hbox1.addWidget(buttonStop) hbox1.addStretch() hbox2 = QtWidgets.QHBoxLayout() hbox2.addWidget(self.paramMonitor) hbox3 = QtWidgets.QHBoxLayout() hbox3.addWidget(self.varMonitor) vbox = QtWidgets.QVBoxLayout() vbox.addLayout(hbox1) vbox.addLayout(hbox2) vbox.addLayout(hbox3) self.setLayout(vbox) self.paramMonitor.itemChanged.connect(self.setParam) #---------------------------------------------------------------------- def updateMonitor(self, event=None): """显示策略最新状态""" paramDict = self.engine.getStrategyParam(self.name) if paramDict: self.paramMonitor.updateData(paramDict) varDict = self.engine.getStrategyVar(self.name) if varDict: self.varMonitor.updateData(varDict) #---------------------------------------------------------------------- def registerEvent(self): """注册事件监听""" self.signal.connect(self.updateMonitor) self.eventEngine.register(EVENT_OM_STRATEGY + self.name, self.signal.emit) #---------------------------------------------------------------------- def init(self): """初始化策略""" self.engine.initStrategy(self.name) #---------------------------------------------------------------------- def start(self): """启动策略""" self.engine.startStrategy(self.name) #---------------------------------------------------------------------- def stop(self): """停止策略""" self.engine.stopStrategy(self.name) #---------------------------------------------------------------------- def setParam(self, cell): """更新参数""" text = unicode(cell.text()) key = cell.key if text.isdigit(): if '.' in text: value = float(text) else: value = int(text) else: value = text self.engine.setStrategyParam(self.name, key, value)
def owner_exited(self, context, who: str, when: float) -> None: ev = Event(EventEnum.EXIT_OWNER) ev.data = {"when": when, "who": who} context.add_event(ev)
def create_enter_sleep_event(self, context): state = SilenceState(context.app_state, {}) event = Event(EventEnum.TRANSITION_TO_GIVEN) event.target_state = state context.add_event(event)
def schedule(self, target, type, time): ev = Event(self, target, type, time) self.scheduler.insert(ev)
def main(): N = input("Number of wireless hosts: ") arrivalRate = input("Arrival rate: ") # create N hosts for i in range(N): host = Host() hosts.append(host) # schedule transmission event & add to gel eventTime = negExpDist(arrivalRate) #first arrival time firstTransmission = Event(eventTime,"ready") gel.insert(firstTransmission) channelBusyGlobal = 0 for i in range(100000): if gel.isEmpty() == False: event = gel.delete() if event.event_category == "ready": dataSize = negExpDist(arrivalRate) transmissionTime = ((dataSize * 8)/(11 * math.pow(10,6)))/1000 sendIndex = random.choice(range(1, N)) # randomly select one sender host and one receiver host receiveIndex = random.choice(range(1, sendIndex) + range(sendIndex+1, N)) sender = hosts[sendIndex] receiver = hosts[receiveIndex] #nextEventTime = time + negExpDist(arrivalRate) # schedule next transmission event & add to gel #nextTransmission = Event(nextEventTime, "ready") #gel.insert(nextTransmission) sender.buffer.append(time) sender.packetReady(dataSize, time, transmissionTime, event, sendIndex, receiveIndex, channelBusyGlobal) #print("ready event") elif event.event_category == "wait_frame": sender.waitDIFS(dataSize, time, transmissionTime, event, sendIndex, receiveIndex, channelBusyGlobal) #print("waiting before transmitting (DIFS)") elif event.event_category == "wait_ack": receiver.waitSIFS(dataSize, time, transmissionTime, event, sendIndex, receiveIndex, channelBusyGlobal) #print("waiting before sending ACK (SIFS)") elif event.event_category == "wait_timeout": sender.waitACKTimeout(dataSize, time, transmissionTime, event, sendIndex, receiveIndex, channelBusyGlobal) #print("ACK countdown timer") elif event.event_category == "transmit_frame": sender.transmitFrame(dataSize, time, transmissionTime, event, sendIndex, receiveIndex, channelBusyGlobal) #print("transmission of frame complete") elif event.event_category == "transmit_ack": receiver.transmitAck(dataSize, time, transmissionTime, event, sendIndex, receiveIndex, channelBusyGlobal) #print("transmission of ack complete") elif event.event_category == "collision_check": receiver.checkCollision(sendIndex, receiveIndex, sender, N, event) #print("checked for collision") elif event.event_category == "queue_ack": receiver.queueACK(dataSize, time, transmissionTime, event, sendIndex, receiveIndex, channelBusyGlobal) #print("queued ACK") elif event.event_category == "timeout": sender.waitDIFS(dataSize, time, transmissionTime, event, sendIndex, receiveIndex, channelBusyGlobal) #print("timeout, retransmit") if gel.isEmpty(): print("The throughput is: ") print(numBytes/time) print("Average network delay is: ") print(totalDelay*1000/(numBytes/time)) exit()
class Looper(object): """Creates a set of analyzers, and schedules the event processing.""" def __init__( self, name, config, nEvents=None, firstEvent=0, nPrint=0, timeReport=False, quiet=False, memCheckFromEvent=-1): """Handles the processing of an event sample. An Analyzer is built for each Config.Analyzer present in sequence. The Looper can then be used to process an event, or a collection of events. Parameters: name : name of the Looper, will be used as the output directory name config : process configuration information, see Config nEvents : number of events to process. Defaults to all. firstEvent : first event to process. Defaults to the first one. nPrint : number of events to print at the beginning """ self.config = config self.name = self._prepareOutput(name) self.outDir = self.name self.logger = logging.getLogger( self.name ) self.logger.addHandler(logging.FileHandler('/'.join([self.name, 'log.txt']))) self.logger.propagate = False if not quiet: self.logger.addHandler( logging.StreamHandler(sys.stdout) ) self.cfg_comp = config.components[0] self.classes = {} self.analyzers = map( self._build, config.sequence ) self.nEvents = nEvents self.firstEvent = firstEvent self.nPrint = int(nPrint) self.timeReport = [ {'time':0.0,'events':0} for a in self.analyzers ] if timeReport else False self.memReportFirstEvent = memCheckFromEvent self.memLast=0 tree_name = None if( hasattr(self.cfg_comp, 'tree_name') ): tree_name = self.cfg_comp.tree_name if len(self.cfg_comp.files)==0: errmsg = 'please provide at least an input file in the files attribute of this component\n' + str(self.cfg_comp) raise ValueError( errmsg ) if hasattr(config,"preprocessor") and config.preprocessor is not None : self.cfg_comp = config.preprocessor.run(self.cfg_comp,self.outDir,firstEvent,nEvents) if hasattr(self.cfg_comp,"options"): print self.cfg_comp.files,self.cfg_comp.options self.events = config.events_class(self.cfg_comp.files, tree_name,options=self.cfg_comp.options) else : self.events = config.events_class(self.cfg_comp.files, tree_name) if hasattr(self.cfg_comp, 'fineSplit'): fineSplitIndex, fineSplitFactor = self.cfg_comp.fineSplit if fineSplitFactor > 1: if len(self.cfg_comp.files) != 1: raise RuntimeError("Any component with fineSplit > 1 is supposed to have just a single file, while %s has %s" % (self.cfg_comp.name, self.cfg_comp.files)) totevents = min(len(self.events),int(nEvents)) if (nEvents and int(nEvents) not in [-1,0]) else len(self.events) self.nEvents = int(ceil(totevents/float(fineSplitFactor))) self.firstEvent = firstEvent + fineSplitIndex * self.nEvents if self.firstEvent + self.nEvents >= totevents: self.nEvents = totevents - self.firstEvent #print "For component %s will process %d events starting from the %d one, ending at %d excluded" % (self.cfg_comp.name, self.nEvents, self.firstEvent, self.nEvents + self.firstEvent) # self.event is set in self.process self.event = None services = dict() for cfg_serv in config.services: service = self._build(cfg_serv) services[cfg_serv.name] = service # would like to provide a copy of the config to the setup, # so that analyzers cannot modify the config of other analyzers. # but cannot copy the autofill config. self.setup = Setup(config, services) def _build(self, cfg): theClass = cfg.class_object obj = theClass( cfg, self.cfg_comp, self.outDir ) return obj def _prepareOutput(self, name): index = 0 tmpname = name while True and index < 2000: try: # print 'mkdir', self.name os.mkdir( tmpname ) break except OSError: index += 1 tmpname = '%s_%d' % (name, index) if index == 2000: raise ValueError( "More than 2000 output folder with same name or 2000 attempts failed, please clean-up, change name or check permissions") return tmpname def loop(self): """Loop on a given number of events. At the beginning of the loop, Analyzer.beginLoop is called for each Analyzer. At each event, self.process is called. At the end of the loop, Analyzer.endLoop is called. """ nEvents = self.nEvents firstEvent = self.firstEvent iEv = firstEvent if nEvents is None or int(nEvents) > len(self.events) : nEvents = len(self.events) else: nEvents = int(nEvents) eventSize = nEvents self.logger.info( 'starting loop at event {firstEvent} '\ 'to process {eventSize} events.'.format(firstEvent=firstEvent, eventSize=eventSize)) self.logger.info( str( self.cfg_comp ) ) for analyzer in self.analyzers: analyzer.beginLoop(self.setup) try: for iEv in range(firstEvent, firstEvent+eventSize): # if iEv == nEvents: # break if iEv%100 ==0: # print 'event', iEv if not hasattr(self,'start_time'): print 'event', iEv self.start_time = timeit.default_timer() self.start_time_event = iEv else: print 'event %d (%.1f ev/s)' % (iEv, (iEv-self.start_time_event)/float(timeit.default_timer() - self.start_time)) self.process( iEv ) if iEv<self.nPrint: print self.event except UserWarning: print 'Stopped loop following a UserWarning exception' info = self.logger.info warning = self.logger.warning warning('number of events processed: {nEv}'.format(nEv=iEv+1)) warning('') info( self.cfg_comp ) info('') for analyzer in self.analyzers: analyzer.endLoop(self.setup) if self.timeReport: allev = max([x['events'] for x in self.timeReport]) warning("\n ---- TimeReport (all times in ms; first evt is skipped) ---- ") warning("%9s %9s %9s %9s %6s %s" % ("processed","all evts","time/proc", " time/all", " [%] ", "analyer")) warning("%9s %9s %9s %9s %6s %s" % ("---------","--------","---------", "---------", " -----", "-------------")) sumtime = sum(rep['time'] for rep in self.timeReport) passev = self.timeReport[-1]['events'] for ana,rep in zip(self.analyzers,self.timeReport): timePerProcEv = rep['time']/(rep['events']-1) if rep['events'] > 1 else 0 timePerAllEv = rep['time']/(allev-1) if allev > 1 else 0 fracAllEv = rep['time']/sumtime warning( "%9d %9d %10.2f %10.2f %5.1f%% %s" % ( rep['events'], allev, 1000*timePerProcEv, 1000*timePerAllEv, 100.0*fracAllEv, ana.name)) totPerProcEv = sumtime/(passev-1) if passev > 1 else 0 totPerAllEv = sumtime/(allev-1) if allev > 1 else 0 warning("%9s %9s %9s %9s %s" % ("---------","--------","---------", "---------", "-------------")) warning("%9d %9d %10.2f %10.2f %5.1f%% %s" % ( passev, allev, 1000*totPerProcEv, 1000*totPerAllEv, 100.0, "TOTAL")) warning("") if hasattr(self.events, 'endLoop'): self.events.endLoop() if hasattr(self.config,"preprocessor") and self.config.preprocessor is not None: if hasattr(self.config.preprocessor,"endLoop"): self.config.preprocessor.endLoop(self.cfg_comp) def process(self, iEv ): """Run event processing for all analyzers in the sequence. This function is called by self.loop, but can also be called directly from the python interpreter, to jump to a given event. """ self.event = Event(iEv, self.events[iEv], self.setup) self.iEvent = iEv for i,analyzer in enumerate(self.analyzers): if not analyzer.beginLoopCalled: analyzer.beginLoop(self.setup) start = timeit.default_timer() if self.memReportFirstEvent >=0 and iEv >= self.memReportFirstEvent: memNow=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss if memNow > self.memLast : print "Mem Jump detected before analyzer %s at event %s. RSS(before,after,difference) %s %s %s "%( analyzer.name, iEv, self.memLast, memNow, memNow-self.memLast) self.memLast=memNow ret = analyzer.process( self.event ) if self.memReportFirstEvent >=0 and iEv >= self.memReportFirstEvent: memNow=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss if memNow > self.memLast : print "Mem Jump detected in analyzer %s at event %s. RSS(before,after,difference) %s %s %s "%( analyzer.name, iEv, self.memLast, memNow, memNow-self.memLast) self.memLast=memNow if self.timeReport: self.timeReport[i]['events'] += 1 if self.timeReport[i]['events'] > 0: self.timeReport[i]['time'] += timeit.default_timer() - start if ret == False: return (False, analyzer.name) if iEv<self.nPrint: self.logger.info( self.event.__str__() ) return (True, analyzer.name) def write(self): """Writes all analyzers. See Analyzer.Write for more information. """ for analyzer in self.analyzers: analyzer.write(self.setup) self.setup.close()
def _run_client(self): message = self._client_recv_event_socket.recv() event = Event.message_to_event(message) logger.debug("[client] receive message: %s" % event) self._queue_engine.emit(event)
from color import Color from mixer import Mixer from time import Time import util import mask import font import sprite from locals import * time = Time() display = Display() image = Image() draw = Draw() transform = Transform() surfarray = Surfarray() mixer = Mixer() event = Event() mouse = Mouse() key = Key() init = lambda: None def quit(): canvas = display.get_canvas() canvas.stop() class error(RuntimeError): pass