class Job(object): # A job stores the representation of the function that is to be executed. def __init__(self, id, func, args, kwargs): self.id = id self.func = Func(func, args, kwargs, self) def start(self, worker, is_parent_process=True): # This function is called by the worker who dequeued it from the queue. self.worker = worker logging.debug('Started Job #' + repr(self.id) + ' - ' + repr(self.func)) self.start_time = time.time() # Execute the function self.func.execute() def finished_successfully(self): # Log that this job successfuly finished and tell the worker that this # job completed logging.debug('Finished Job #' + repr(self.id) + ' - ' + repr(self.func) + '; took ' + repr(time.time() - self.start_time) + ' seconds') self.worker.job_finished(self.id) def failed(self, error_str): # Log the error and tell the worker that this job completed logging.debug('>>>>> Job #' + repr(self.id) + ' failed;\n' + str(error_str)) self.worker.job_finished(self.id)
def raceinfo_page(): racs = Raceinfos(app.config['dsn']) method = Func(app.config['dsn']) tr_list = method.get_tracks() nat_list = method.get_nations() yr_list = method.get_years() dr_list = method.get_drivers() if request.method == 'GET': now = datetime.datetime.now() racinflist = racs.get_raceinfolist() return render_template('raceinfos.html', RaceList = racinflist, current_time = now.ctime(), TrackList = tr_list, NationList = nat_list, YearList = yr_list, DriverList = dr_list) elif 'raceinfos_to_delete' in request.form: raceinfos = request.form.getlist('raceinfos_to_delete') for raceinfo in raceinfos: racs.delete_raceinfo(raceinfo) return redirect(url_for('raceinfo_page')) elif 'raceinfos_to_add' in request.form: racs.add_raceinfo(request.form) return redirect(url_for('raceinfo_page')) elif 'raceinfos_to_update' in request.form: racs.update_raceinfo(request.form) return redirect(url_for('raceinfo_page')) elif 'raceinfos_to_searchwinner' in request.form: now = datetime.datetime.now() racinflist = racs.search_raceinfolist('winner', request.form) return render_template('raceinfos.html', RaceList = racinflist, current_time = now.ctime(), TrackList = tr_list, NationList = nat_list, YearList = yr_list, DriverList = dr_list) elif 'raceinfos_to_searchtrack' in request.form: now = datetime.datetime.now() racinflist = racs.search_raceinfolist('track', request.form) return render_template('raceinfos.html', RaceList = racinflist, current_time = now.ctime(), TrackList = tr_list, NationList = nat_list, YearList = yr_list, DriverList = dr_list)
def start(self, port): self = State(port) # bind socket try: ip = self.ip s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((ip, port)) s.listen() self.sock = s print('Listening on {}:{}'.format(ip, port)) except Exception as e: print(e) # print the usage of this system print('-----------------------') print("usage : \n ping: ping the current node \n create_ring: create a new ring \n join <ip> <port>: join an existing ring \n exit: exit the system ") print('-----------------------') # keep the state of this node up-to-date. Func([None,None], self, True).start() #keep accepting new command while True: peer = s.accept() print ("after accept") Func(peer, self, False).start() print ("after func") s.close()
def a_year_page(year_title): now = datetime.datetime.now() fn = Func(app.config['dsn']) rc = Raceinfos(app.config['dsn']) year_id = fn.get_id("years", year_title) if year_id is None: return render_template('404.html', current_time = now.ctime()) rclist = rc.get_raceinfolist(year_title = year_title) return render_template('a_year.html', YearTitle = year_title, RaceList = rclist, current_time = now.ctime())
class SpiderOperator(threading.Thread): def __init__(self): super(SpiderOperator,self).__init__() self.operator = Func() def run(self): global q while True: sid,password = q.get() self.operator.check(sid,password) q.task_done()
def a_nation_page(nat_title): now = datetime.datetime.now() fn = Func(app.config['dsn']) nt = Nations(app.config['dsn']) rc = Raceinfos(app.config['dsn']) nat_id = fn.get_id("nations", nat_title) #will be null if unknown title entered nat = nt.get_a_nation(nat_id) if nat is None: return render_template('404.html', current_time = now.ctime()) rclist = rc.get_raceinfolist(nat_id = nat_id) trlist = nt.get_trackfornation(nat_id) return render_template('a_nation.html', Nation = nat, RaceList = rclist, TrackInfoList = trlist, current_time = now.ctime())
def p_functionblock(p): """ functionblock : functionblockname LFBRACK functionlines returnstmtforfunc SEMICOLON RFBRACK | functionblockname1 LFBRACK functionlines RETURN SEMICOLON RFBRACK | functionblockname1 LFBRACK functionlines RFBRACK """ # type, name, pointerdepth, paramList global FunctionNodes if p[4] == '}': temp = Func(p[1][1],p[1][0],p[1][2],p[1][3],p[3],[]) elif p[4] == 'return': temp = Func(p[1][1],p[1][0],p[1][2],p[1][3],p[3],[]) else: temp = Func(p[1][1],p[1][0],p[1][2],p[1][3],p[3],[p[4]]) FunctionNodes.append(temp)
def get_function_class(clause, nlp, run_selenium): """ Read clause and return the Function class with assign the function type. Input: clause(str): one clause for function nlp(StanfordCoreNLP parser) Output: func: 'func' class with assignd function type """ assert isinstance(clause, str) if args.use_corenlp: pos_tag_clause = nlp.pos_tag(clause) else: pos_tag_clause = nlp.pos_tag(nlp.word_tokenize(clause)) func = Func(clause, pos_tag_clause, run_selenium) func.assign_func_name() return func
def p_mainblock(p): """ mainblock : VOID mainfunctionname LFBRACK functionlines RFBRACK """ global FunctionNodes temp = Func('main','void',0,[],p[4],None) FunctionNodes.append(temp) p[0] = temp
def add_func( self, cpp_name, returns=None, params=[], opt_params=[], kl_name=None, promotion_prolog=None, dfg_preset_omit=False, ): cpp_local_name = cpp_name try: cpp_global_name = "::".join(self.nested_cpp_names + [cpp_local_name]) kl_local_name = kl_name if not kl_local_name: kl_local_name = cpp_local_name kl_global_name = "_".join(self.nested_kl_names + [kl_local_name]) returns = massage_returns(returns) params = massage_params(params) opt_params = massage_params(opt_params) result = None for i in range(0, len(opt_params)+1): func = Func( self, cpp_global_name, kl_global_name, returns, params + opt_params[0:i], promotion_prolog=promotion_prolog, dfg_preset_omit=dfg_preset_omit, ) self.ext.add_decl(func) promotion_sig, promotion_cost = func.get_promotion_data() if not promotion_sig in self.ext.func_promotions \ or self.ext.func_promotions[promotion_sig][1] > promotion_cost: self.ext.func_promotions[promotion_sig] = (func, promotion_cost) if not result: result = func return result except Exception as e: self.warning("Ignoring func %s: %s" % (cpp_local_name, e)) return EmptyCommentContainer()
def inject(self, **kwargs): internal = kwargs.get('internal', False) is_asm = kwargs.get('is_asm', False) mark_func = kwargs.get('mark_func', False) return_size = kwargs.get('size', False) target = kwargs.get('target', 'patch') desc = kwargs.get('desc', '') if desc: desc = ' | "%s"' % desc addr = self.binary.next_alloc(target) c = kwargs.get('c') if c: asm = compiler.compile(c, self.binary.linker) raw = self.asm(asm, addr=addr, att_syntax=True) typ = 'c' is_asm = True else: raw, typ = self._compile(addr, **kwargs) self._lint(addr, raw, typ, is_asm=kwargs.get('is_asm')) if typ == 'asm': ret = self.asm(self.arch.ret()) if raw[-len(ret):] != ret and not internal: self.warn('Injected asm does not return!') self.info( pfcol('INJECT') + '@0x%x-0x%x%s' % (addr, addr + len(raw), desc)) if not kwargs.get('silent'): if typ == 'asm' or is_asm: self.debug(dis=self.arch.dis(raw, addr=addr)) else: self.debug(binascii.hexlify(raw)) addr = self.binary.alloc(len(raw), target=target) if mark_func: self.marked_funcs.append(Func(self, addr, len(raw))) self.elf.write(addr, raw) if return_size: return addr, len(raw) else: return addr
def parse_script(self, file_object): func_counter = -1 line_counter = -1 func = Func('') for line in file_object.readlines(): line_counter += 1 splitted = line.split(' ') if splitted[0] == 'func': func_name = self.get_func_name(splitted[1]) func = Func(func_name) func.add_content(line) self.functions.append(func) func_counter = line_counter elif re.match(r'\s', splitted[0]) and func_counter != -1: func.add_content(line) else: self.general_content.append(line) func_counter = -1
def funcs(self, marked=False): addrs = [] self.func_printed = None try: funcs = self.relopen('funcs') except IOError: return for line in funcs: s, e = line.strip().split(' ') start, end = int(s, 16), int(e, 16) func = Func(self, start, end - start) self.current_func = func yield func self.current_func = None if marked: tmp = self.marked_funcs[:] for func in tmp: yield func
def gaInfo(self): try: function = Func(self.f_entry.get()) optimizer = OptimizerGA(function) optimizer.startGA( chromosomes_number=int(self.chromosomes_number_entry.get()), generations_number=int(self.generations_number_entry.get()), mutation=self.mutation.get(), mutation_range=self.mutation_range.get(), optimizer=self.optimizer_entry.get(), statistics=self.statistics.get(), save=self.save.get(), plot=self.plot.get()) if self.statistics.get(): with open('results/GA-statistics.txt', 'r') as f: mytext = f.read() root = Tk() root.title("STATISTICS") root.geometry('700x500+100+100') frame = tk.Frame(master=root, bg='grey') frame.pack(fill='both', expand='yes') text = tkst.ScrolledText(master=frame, wrap=tk.WORD, width=700, height=500) text.pack() text.insert(1.0, mytext) root.mainloop() except Exception as err: print('Ошибка!\n', type(err)) print(err) self.errorGUI()
def add_func( self, cpp_name, returns=None, params=[], opt_params=[], kl_name=None, ): cpp_local_name = cpp_name try: cpp_global_name = "::".join(self.nested_cpp_names + [cpp_local_name]) kl_local_name = kl_name if not kl_local_name: kl_local_name = cpp_local_name kl_global_name = "_".join(self.nested_kl_names + [kl_local_name]) returns = massage_returns(returns) params = massage_params(params) opt_params = massage_params(opt_params) result = None for i in range(0, len(opt_params) + 1): func = Func( self, cpp_global_name, kl_global_name, returns, params + opt_params[0:i], ) self.ext.add_decl(func) if not result: result = func return result except Exception as e: self.warning("Ignoring func %s: %s" % (cpp_local_name, e)) return EmptyCommentContainer()
def a_track_page(track_title): now = datetime.datetime.now() fn = Func(app.config['dsn']) nt = Nations(app.config['dsn']) rc = Raceinfos(app.config['dsn']) nat_id = fn.get_country(track_title) tid=fn.get_trackid(track_title) yr=fn.get_firstrace(track_title) nat_name=fn.get_countryname(nat_id) nat = nt.get_a_nation(nat_id) if nat is None: return render_template('404.html', current_time = now.ctime()) rclist = fn.get_trackinfo(tid) return render_template('a_track.html', yr=yr,rally=track_title,Nation = nat,nat_name=nat_name, RaceList = rclist, current_time = now.ctime())
def __init__(self): super(SpiderOperator,self).__init__() self.operator = Func()
from func import Func url = 'http://www.insoonia.com' metodo = 'utf-8' texto = None cont = 0 site = Func(url,metodo,texto,cont) site.conectSite() print(site)
#------------------------------------------------------------ # Create a list of specialization functions for fx16 #------------------------------------------------------------ #conv2d_chw_str1 can only be used with stride==1 and it is using vmac #conv2d_chw can be used for any stride and is using dmac #conv2d_chw_nopad_k1x1_str1 is optimized for 1x1 kernelsize and stride==1 #construct the different specializtions for stride 1 and kernel 1x1 corefunc = "conv2d_chw_nopad_k1x1_str1" stride = 1 k = 1 channel_range = [0, 1, 3, 4] f_list.extend([ Func(fbase, k, k, ch, stride, stride, corefunc, "nopad") for ch in channel_range ]) #stride = 1, any kernel size, any channel size corefunc = "conv2d_chw_str1" stride = 1 kernel_range = [ 2, 3, 4, 5, 6, 7 ] #above 8x8 there is less than 20% benefit of using specialized version channel_range = [ 0, 1 ] #for larger number of channels the generic channel case has similar performance. maybe 3 channels is still useful f_list.extend([ Func(fbase, k, k, ch, stride, stride, corefunc, "krnpad") for k in kernel_range for ch in channel_range
def gt(num): def inner(args): return len(args) > num inner.__doc__ = "Number of arguments must be greater than %s" % num return inner def between(lower, upper): """Lower and upper are inclusive""" def inner(args): return lower <= len(args) <= upper inner.__doc__ = "Number of arguments must be " \ "between (inclusive) %s and %s" % (lower, upper) return inner IF = Func('IF', args_cmp_func=eq(3), doc="IF function. Param 1: Conditional. " "Param 2: True Part. Param 3: False Part.") IFS = Func('IFS', args_cmp_func=between(2, 127 * 2)) IFERROR = Func('IFERROR', args_cmp_func=eq(2)) SUMIFS = Func('SUMIFS', args_cmp_func=between(2, 127 * 2)) SUM = Func('SUM', args_cmp_func=between(1, 255)) DATE = Func('DATE', args_cmp_func=eq(3)) YEAR = Func('YEAR', args_cmp_func=eq(1)) MONTH = Func('MONTH', args_cmp_func=eq(1)) DAY = Func('DAY', args_cmp_func=eq(1))
include_list = ["mli_krn_avepool_chw.h"] define_list = [] # commandline arguments can be used to generate a specific output 'fx16' | 'fx8' | 'header'] # if no arguments are given, all files are generated. no_args = len(sys.argv) == 1 #------------------------------------------------------------ # Create a list of specialization functions #------------------------------------------------------------ #construct the different specializtions for stride 1 and kernel 2x2 corefunc = "avepool_chw_nopad_k2x2" stride = 1 k = 2 ch = 0 f_list.extend([Func(fbase, k, k, ch, stride, stride, corefunc, "nopad")]) corefunc = "avepool_chw_k4x4_str1_nopad" stride = 1 k = 4 ch = 0 f_list.extend([Func(fbase, k, k, ch, stride, stride, corefunc, "nopad")]) corefunc = "avepool_chw_krnpad" stride = 0 kernel_range = range(3, 11, 2) ch = 0 f_list.extend([Func(fbase, k, k, ch, stride, stride, corefunc, "krnpad") for k in kernel_range]) corefunc = "avepool_chw_krnpad_k4_Nx2_N_even" stride = 0
def test_twosum(self): fc = Func() self.assertEqual(fc.two_sum(2, 3), 5) self.assertEqual(fc.two_sum(2, 4), 6) self.assertEqual(fc.two_sum(2, 5), 7) self.assertEqual(fc.two_sum(2, 6), 8)
def test_twomul(self): fc = Func() self.assertEqual(fc.two_mul(2, 3), 6)
def __init__(self, cp): self.cp = cp self.fn = Func(cp) return
class Raceinfos: def __init__(self, cp): self.cp = cp self.fn = Func(cp) return def get_raceinfolist(self, nat_id = None, year_title = None): with dbapi2.connect(self.cp) as connection: cursor = connection.cursor() query = """ SELECT tr.title AS Track, yr.title AS Year, dr1.name AS First, dr2.name AS Second, dr3.name AS Third, nat.title AS Nation, fdr.name AS FastestDr, rc.fastest_time AS FastestLap FROM """ if nat_id is None: query += "raceinfos rc" else: query += "(SELECT * FROM raceinfos WHERE nation_id = '%s') rc" %(str(nat_id)) query +=""" JOIN tracks tr ON tr.id = rc.track_id """ if year_title is None: query += "JOIN years yr ON yr.id = rc.year_id" else: query += "JOIN (SELECT * FROM years WHERE title = '%s') yr ON yr.id = rc.year_id" %(year_title) query +=""" JOIN drivers dr1 ON dr1.id = rc.dr1_id JOIN drivers dr2 ON dr2.id = rc.dr2_id JOIN drivers dr3 ON dr3.id = rc.dr3_id JOIN nations nat ON nat.id = rc.nation_id JOIN drivers fdr ON fdr.id = rc.fastestdr_id ORDER BY rc.track_id ASC, rc.year_id ASC; """ cursor.execute(query) rows = cursor.fetchall() return rows def delete_raceinfo(self, n_raceinfo): raceinfo = n_raceinfo.split(":") with dbapi2.connect(self.cp) as connection: cursor = connection.cursor() query = "SELECT * FROM tracks WHERE title = '%s'" % (raceinfo[0]) cursor.execute(query) track_id = cursor.fetchone()[0] query = "SELECT * FROM years WHERE title = '%s'" % (raceinfo[1]) cursor.execute(query) year_id = cursor.fetchone()[0] query = "DELETE FROM raceinfos WHERE (track_id = '%s' AND year_id = '%s')" % (str(track_id), str(year_id)) cursor.execute(query) connection.commit() return def add_raceinfo(self, form): track_id = self.fn.get_id("tracks", form.get('Track')) year_id = self.fn.get_id("years", form.get('Year')) dr1_id = self.fn.get_id("drivers", form.get('First')) dr2_id = self.fn.get_id("drivers", form.get('Second')) dr3_id = self.fn.get_id("drivers", form.get('Third')) nation_id = self.fn.get_id("nations", form.get('Nation')) fastestdr_id = self.fn.get_id("drivers", form.get('FastestDr')) fastest_time = form['FastestLap'] if (dr1_id == dr2_id) or (dr1_id == dr3_id) or (dr2_id == dr3_id): return with dbapi2.connect(self.cp) as connection: cursor = connection.cursor() query = "INSERT INTO raceinfos VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s' ,'%s')" % (track_id, year_id, dr1_id, dr2_id, dr3_id, nation_id, fastestdr_id, fastest_time) cursor.execute(query) connection.commit() return def update_raceinfo(self, form): track_id = self.get_id("tracks", form.get('Track')) year_id = self.get_id("years", form.get('Year')) dr1_id = self.get_id("drivers", form.get('First')) dr2_id = self.get_id("drivers", form.get('Second')) dr3_id = self.get_id("drivers", form.get('Third')) nation_id = self.get_id("nations", form.get('Nation')) fastestdr_id = self.get_id("drivers", form.get('FastestDr')) fastest_time = form['FastestLap'] if (dr1_id == dr2_id) or (dr1_id == dr3_id) or (dr2_id == dr3_id): return with dbapi2.connect(self.cp) as connection: cursor = connection.cursor() query = "UPDATE raceinfos SET dr1_id = '{0}', dr2_id = '{1}', dr3_id = '{2}', nation_id = '{3}', fastestdr_id = '{4}', fastest_time = '{5}' WHERE track_id = '{6}' AND year_id = '{7}'".format(dr1_id, dr2_id, dr3_id, nation_id, fastestdr_id, fastest_time, track_id, year_id) cursor.execute(query) connection.commit() return def search_raceinfolist(self, searchtype, form): with dbapi2.connect(self.cp) as connection: cursor = connection.cursor() query = """SELECT tr.title AS Track, yr.title AS Year, dr1.name AS First, dr2.name AS Second, dr3.name AS Third, nat.title AS Nation, fdr.name AS FastestDr, rc.fastest_time AS FastestLap FROM raceinfos rc JOIN tracks tr ON tr.id = rc.track_id JOIN years yr ON yr.id = rc.year_id JOIN drivers dr1 ON dr1.id = rc.dr1_id JOIN drivers dr2 ON dr2.id = rc.dr2_id JOIN drivers dr3 ON dr3.id = rc.dr3_id JOIN nations nat ON nat.id = rc.nation_id JOIN drivers fdr ON fdr.id = rc.fastestdr_id ORDER BY rc.track_id ASC, rc.year_id ASC """ if searchtype == 'winner': query = "SELECT * FROM (" + query + ") AS Derived WHERE Derived.First ILIKE '%%%s%%'" % (form['SearchWinner']) if searchtype == 'track': query = "SELECT * FROM (" + query + ") AS Derived WHERE Derived.Track ILIKE '%%%s%%'" % (form['SearchTrack']) cursor.execute(query) rows = cursor.fetchall() return rows
#!/usr/bin/env python """ To run these tests, execute "python path.py". Example use: define a few nodes, here four nodes in 2D: >>> path = ((-100., -100.), (0., -50.), (0., 50.), (100., 100.)) and construct a path connecting them by default with 4-equidistant nodes >>> p = Path(path) This builds linear, quadratic or spline representation of the path, depending on the number of nodes. Now you can evaluate the path function at any point (between 0 and 1), the equidistant values 0, 1/3, 2/3 and 1 correspond to the four nodes we provided: >>> p(0) array([-100., -100.]) >>> p(1) array([ 100., 100.]) Rounding is only for tests to succeed despite the finite precision: >>> from numpy import round >>> round(p(1./3.), 10)
def inject(self, **kwargs): """injects user-defind code Args: raw (str): the data to inject when injecting raw bytes asm (str): the assembly to inject when injecting assembly c (str): the C code to inject when injecting C code internal (bool): ? TODO is_asm (bool): is the content assembling lang mark_func (bool): ? TODO size (int): how many size to inject target (str): ? TODO desc (str): descritpion of the data to inject silent (bool): silent mode Returns: int: injected address """ internal = kwargs.get('internal', False) is_asm = kwargs.get('is_asm', False) mark_func = kwargs.get('mark_func', False) return_size = kwargs.get('size', False) target = kwargs.get('target', 'patch') desc = kwargs.get('desc', '') if desc: desc = ' | "%s"' % desc # injected address is the next allocation position addr = self.binary.next_alloc(target) c = kwargs.get('c') if c: asm = compiler.compile(c, self.binary.linker) raw = self.asm(asm, addr=addr, att_syntax=True) typ = 'c' is_asm = True else: raw, typ = self._compile(addr, **kwargs) self._lint(addr, raw, typ, is_asm=kwargs.get('is_asm')) if typ == 'asm': ret = self.asm(self.arch.ret()) if raw[-len(ret):] != ret and not internal: self.warn('Injected asm does not return!') self.info( pfcol('INJECT') + '@0x%x-0x%x%s' % (addr, addr + len(raw), desc)) if not kwargs.get('silent'): if typ == 'asm' or is_asm: self.debug(dis=self.arch.dis(raw, addr=addr)) else: self.debug(binascii.hexlify(raw)) # then we allocate addr = self.binary.alloc(len(raw), target=target) if mark_func: self.marked_funcs.append(Func(self, addr, len(raw))) self.elf.write(addr, raw) if return_size: return addr, len(raw) else: return addr
def __init__(self, id, func, args, kwargs): self.id = id self.func = Func(func, args, kwargs, self)