def excute(task_queue): while not task_queue.empty(): input_file = task_queue.get() wordMap = mapper.map(input_file) print("Average length of words:" + str(sum([len(word) for word in wordMap]) / len(wordMap))) wordReduced = reducer.reduce(mapper.map(input_file)) outputFile = open('output.txt', 'w') outputFile.write(str(wordReduced)) print("Output file gernerated sucessfully.") return outputFile.write(str(wordReduced))
def _yield_outputs(institution_id): """Yields set of output documents. """ defaults.init(institution_id.canonical_name) for mapping_info in _yield_mapping_targets(): yield mapper.map(mapping_info)
def test(self): input, desired = t[0], t[1] for key,value in desired.iteritems(): self.assertEqual(map(key, input, output=False, year=year, date_ini=date_ini, date_end=date_end), value)
def on_request(ch, method, props, body): request = json.loads(body) print(' [*] Got ') print(request) f = open("mapper.py", "w") f.write(request['func']) f.close() from mapper import map files = ['data/' + file for file in os.listdir('data')] response = {} for file in files: f = open(file, 'r', encoding='ISO-8859-1') text = ''.join(f.readlines()) response = Counter(response) + Counter(map(text)) ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties( correlation_id=props.correlation_id, content_type='application/json'), body=json.dumps(response)) ch.basic_ack(delivery_tag=method.delivery_tag)
def main(): fileName = 'input.txt' wordMap = mapper.map(fileName) wordReduced = reducer.reduce(wordMap) avgLenOfWords = str(sum([len(word) for word in wordMap]) / len(wordMap)) print("Average length of words:" + avgLenOfWords) outputFile = open('output.txt', 'w') outputFile.write("Average length of words:" + str(avgLenOfWords) + " " + str(wordReduced)) print("Output file gernerated sucessfully.")
def main(): filename = 'input_text.txt' word_map = mapper.map(filename) word_reduced = combiner.combine(word_map) outfile = open('file.txt', 'w') outfile.write(str(word_reduced)) print("File written successfully in file.txt") sys.exit(1)
def main(): filename = 'input_text.txt' word_map = mapper.map(filename) word_reduced = combiner.combine(word_map) outfile = open('file.txt', 'wb') outfile.write(str(word_reduced)) print "File written successfully in file.txt" sys.exit(1)
def generateThread(self): Clock.schedule_once(lambda dt: self.updateProgress(0)) # NOTE: PBar path = os.path.join(self.fileSelector.path, self.fileSelector.selection[0]) Clock.schedule_once(lambda dt: self.updateProgress(5)) # NOTE: PBar data = bitsFromFile(path) Clock.schedule_once(lambda dt: self.updateProgress(10)) # NOTE: PBar scale = int(self.scaleInput.text) ratio = float(self.ratioInput.text) multiple = int(self.multipleInput.text) if self.nameInput.text == None or self.nameInput.text == "": name = path else: name = os.path.join(self.fileSelector.path, self.nameInput.text) map(data, scale=scale, ratio=ratio, multiple=multiple, name=name) Clock.schedule_once(lambda dt: self.updateProgress(100)) # NOTE: PBar
def cascade4(filenames, debug = False): if debug: print("Cascade of 4 processes with queues. read->process->map->reduce") lines = [item for sublist in [list(data.extractData(fn)) for fn in filenames] for item in sublist] finalDict = {} masterq = multiprocessing.Queue() lpq = multiprocessing.Queue() mapq = multiprocessing.Queue() redq = multiprocessing.Queue() lineProc = os.fork() if lineProc == 0: for toProcess in iter(lpq.get, None): if(data.preprocess(toProcess) is not None): for item in data.preprocess(toProcess): if len(item) > 0: mapq.put(item) mapq.put(None) time.sleep(0.3) os._exit(0) else: mapProc = os.fork() if(mapProc == 0): for toMap in iter(mapq.get, None): for item in mapper.map(toMap): if len(item) > 0: redq.put(item) redq.put(None) time.sleep(0.2) os._exit(0) else: reducProc = os.fork() if(reducProc == 0): r = myReducer.reducer() for toReduce in iter(redq.get, None): result = r.onlineReduce(toReduce) masterq.put(result) masterq.put(None) time.sleep(0.1) os._exit(0) else: for l in lines: lpq.put(l) lpq.put(None) for k,v in iter(masterq.get, None): finalDict[k] = v if debug: l = list(finalDict.iteritems()) print("\t{} files processed. Dictionary of {} instances of {} words made".format(len(filenames), len(l), sum([v for _,v in l]))) os.wait() return
def main(): # TODO: # # 1. Show in external screen # 2. Buttons # 3. Minimal GUI # 4. Keep global config for font size so it can be changed on the fly # 5. Keep some model of the selected verses outside of changed slot # 6. Set both viewport and preview to same "model" (generated text) # 7. Separate verse selection change and text redraw app = QApplication(sys.argv) presenter = Presenter(mapper.map()) sys.exit(app.exec_())
def branching(filenames, debug = False): if debug: print("Data Extraction Split Randomly Over 4 processes:\n a \n / \ \n b c\n /\nd") outref = os.fork() split1 = chunkList(filenames) c = random.choice([0,1]) if outref == 0: message = "\tb" fns = split1[c] else: message = "\ta" fns = split1[1 - c] lines = [item for sublist in [list(data.extractData(fn)) for fn in fns] for item in sublist] split2 = chunkList(lines) cc = random.choice([0,1]) inref = os.fork() if inref == 0: dlines = split2[cc] message = "\tc" if outref == 0: message = "\td" else: dlines = split2[1 - cc] prep = [item for sublist in [data.preprocess(d) for d in dlines] if sublist is not None for item in sublist ] mapd = [item for sublist in [list(mapper.map(l)) for l in prep] if len(sublist) > 0 for item in sublist ] r = myReducer.reducer() for d in mapd: r.reduce(d) if debug: print(message + "({})".format(os.getpid()) + ": ({}|{})".format(fn,len(list(r.dictionary.iteritems())))) if(inref == 0): os._exit(0) else: os.wait() if(outref == 0): os._exit(0) else: os.wait() return
def new_map(args): srckeys, dstkey, props = args srcs = [find_sig(k) for k in srckeys] dst = find_sig(dstkey) if not (all(srcs) and dst): print(srckeys, ' and ', dstkey, ' not found on network!') return map = mapper.map(srcs, dst) if not map: print('error: failed to create map', srckeys, "->", dstkey) return else: print('created map: ', srckeys, ' -> ', dstkey) if props and type(props) is dict: set_map_properties(args, map) map.push()
def new_map(args): srckeys, dstkey, props = args srcs = [find_sig(k) for k in srckeys] dst = find_sig(dstkey) if not (all(srcs) and dst): print srckeys, ' and ', dstkey, ' not found on network!' return map = mapper.map(srcs, dst) if not map: print 'error: failed to create map', srckeys, "->", dstkey return; else: print 'created map: ', srckeys, ' -> ', dstkey if props and type(props) is dict: set_map_properties(props, map) map.push()
def setUp(self): self.server = Server(url) self.db = map(self.server.get_or_create_db(db_name)) # TODO: create greeting/all view design_doc = { '_id': '_design/greeting', 'language': 'javascript', 'views': { 'all': { "map": """ function (doc) { if (doc.doc_type == "Greeting") emit(doc._id, doc); } """ } } } self.db.save_doc(design_doc)
def selectGolfCourse(golfCourseName): session.attributes['yes'] = 'featured' #return ft_{number} or None cName = map(golfCourseName.lower()) print '++++++' + golfCourseName + '+++++++' print str(cName) #GOLF_INFO session.attributes['info'] = map_info('ft_' + str(cName)) if cName != None: golf_course = render_template('g_' + str(cName)) return question(golf_course) \ .standard_card(title='Golf Georgia', text=golfCourseName.capitalize(), large_image_url=map_img(cName)) else: return statement('whot?')
def on_request(ch, method, props, body): request = json.loads(body) print(' [*] Got ') print(request) f = open("mapper.py", "w") f.write(request['func']) f.close() from mapper import map f = open('distributed systems.txt', 'r', encoding='utf-8') f.seek(int(request['seek'])) if int(request['seek']) != 0: f.readline() text = ''.join(f.readlines(5120)) response = map(text) ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties( correlation_id=props.correlation_id, content_type='application/json'), body=json.dumps(response)) ch.basic_ack(delivery_tag=method.delivery_tag)
print('--> received query response:', f) except: print('exception') print(sig, f) src = mapper.device("src") outsig = src.add_output_signal("outsig", 1, 'f', None, 0, 1000) outsig.set_callback(h) dest = mapper.device("dest") insig = dest.add_input_signal("insig", 1, 'f', None, 0, 1, h) while not src.ready or not dest.ready: src.poll() dest.poll(10) map = mapper.map(outsig, insig) map.mode = mapper.MODE_LINEAR map.push() while not map.ready: src.poll(10) dest.poll(10) for i in range(100): print('updating destination to', i, '-->') insig.update(i) outsig.query_remotes() src.poll(10) dest.poll(10)
k_fuzzy_sets = int(sys.argv[1]) k_min = int(sys.argv[2]) k_aggregation = sys.argv[3] data_filename = sys.argv[4] header_filename = sys.argv[5] output_filename = sys.argv[6] ''' Initialization. ''' variables, classes, distribution = conveniences.read_header_file( header_filename, k_fuzzy_sets) ''' Map and combine dataset instances with common antecedents. ''' combined_data = mapper.map(data_filename, variables, classes) ''' Reduce (generate prototypes). ''' prototypes = reducer.reduce(combined_data, variables, classes, k_min, k_aggregation) ''' Write prototypes to file. ''' conveniences.write_prototypes(prototypes, output_filename)
db.add_map_callback(lambda x,y:database_cb('map',x,y)) while not dev.ready(): dev.poll(10) db.poll() start.now() outsig = dev.signal("outsig") for i in range(1000): dev.poll(10) db.poll() outsig.update([i+1,i+2,i+3,i+4]) if i==250: map = mapper.map(outsig, dev.signal("insig")) map.mode = mapper.MODE_EXPRESSION map.expression = 'y=y{-1}+x' map.source().minimum = [1,2,3,4] map.source().bound_min = mapper.BOUND_WRAP map.source().bound_max = mapper.BOUND_CLAMP map.push() # # test creating multi-source map # map = mapper.map([sig1, sig2], sig3) # map.mode = mapper.MODE_EXPRESSION # map.expression = 'y=x0-x1' # map.push() if i==500: print 'muting map'
def h(sig, id, f, tt): print(' handler got', sig.name, '=', f, 'at time', tt.get_double()) src = mapper.device("src") outsig1 = src.add_output_signal("outsig1", 1, 'i', None, 0, 1000) outsig2 = src.add_output_signal("outsig2", 1, 'i', None, 0, 1000) dest = mapper.device("dest") insig1 = dest.add_input_signal("insig1", 1, 'f', None, 0, 1, h) insig2 = dest.add_input_signal("insig2", 1, 'f', None, 0, 1, h) while not src.ready or not dest.ready: src.poll() dest.poll(10) map1 = mapper.map(outsig1, insig1) map1.mode = mapper.MODE_LINEAR map1.push() map2 = mapper.map(outsig2, insig2) map2.mode = mapper.MODE_LINEAR map2.push() while not map1.ready or not map2.ready: src.poll() dest.poll(10) for i in range(50): now = src.start_queue() print('Updating output signals to', i, 'at time', now.get_double()) outsig1.update(i)
def h(sig, id, f, tt): print ' handler got', sig.name, '=', f, 'at time', tt.get_double() src = mapper.device("src") outsig1 = src.add_output_signal("outsig1", 1, 'i', None, 0, 1000) outsig2 = src.add_output_signal("outsig2", 1, 'i', None, 0, 1000) dest = mapper.device("dest") insig1 = dest.add_input_signal("insig1", 1, 'f', None, 0, 1, h) insig2 = dest.add_input_signal("insig2", 1, 'f', None, 0, 1, h) while not src.ready() or not dest.ready(): src.poll() dest.poll(10) map1 = mapper.map(outsig1, insig1) map1.mode = mapper.MODE_LINEAR map1.push() map2 = mapper.map(outsig2, insig2) map2.mode = mapper.MODE_LINEAR map2.push() while not map1.ready() or not map2.ready(): src.poll() dest.poll(10) for i in range(50): now = src.start_queue() print 'Updating output signals to', i, 'at time', now.get_double() outsig1.update(i)
def __init__(self, file_name): m = map("script.dots") self.path_tracer = path_tracer(m)
def h(sig, id, f, timetag): try: print('--> source received', f) except: print('exception') print(sig, f) src = mapper.device("src") outsig = src.add_output_signal("outsig", 1, 'f', None, 0, 1000) outsig.set_callback(h) dest = mapper.device("dest") insig = dest.add_input_signal("insig", 1, 'f', None, 0, 1) while not src.ready or not dest.ready: src.poll(10) dest.poll(10) map = mapper.map(insig, outsig).push() while not map.ready: src.poll(10) dest.poll(10) for i in range(100): print('updating destination to', i, '-->') insig.update(i) src.poll(10) dest.poll(10)
def cascadeMarkovMapReduce(filenames, debug = False, maxIterations = -1, maxTime = 0): procs = listChunks(range(psutil.cpu_count()), 4) if debug: print("System of 4 processes with queues. map->reduce->markov->sample, running for {} seconds".format(maxTime)) #Initial Setup: Get the data from the files and split it up lines = [item for sublist in [list(data.extractData(fn)) for fn in filenames] for item in sublist] prep = [l for l in [data.preprocess(d) for d in lines] if l is not None] datalines = [" ".join(item) for sublist in [list(data.splitify(line)) for line in prep] for item in sublist] initialSize = len(datalines) finalDict = {} dataq = multiprocessing.Queue() markovq = multiprocessing.Queue() selectq = multiprocessing.Queue() redq = multiprocessing.Queue() sampleq = multiprocessing.Queue() for d in datalines: dataq.put(d) initialT = time.time() redProc = os.fork() stopCondition = False; if redProc == 0: rp = psutil.Process(os.getpid()) rp.cpu_affinity(procs[0]) # rp.nice(-10) red = myReducer.reducer() for toProcess in iter(redq.get, None): val = [red.onlineReduce(m) for m in toProcess] markovq.put(val) markovq.put(None) time.sleep(0.3) os._exit(0) else: markovProc = os.fork() if(markovProc == 0): mp = psutil.Process(os.getpid()) mp.cpu_affinity(procs[1]) # mp.nice(-10) mod = markov.markovNGramModel() for toModel in iter(markovq.get, None): for ng in markov.nGrams([w for w,_ in toModel]): mod.update(ng) scores = {word : score for word,score in toModel} samples = [mod.sampleGen(w,) for w,_ in toModel] selectq.put((samples,scores)) selectq.put(None) time.sleep(0.2) os._exit(0) else: selectProc = os.fork() if(selectProc == 0): sp = psutil.Process(os.getpid()) # sp.nice(-10) r = myReducer.reducer() for toScore in iter(selectq.get, None): samples = toScore[0] scores = toScore[1] sampleScores = [] for s in [w for w in samples]: total = 0 for w in samples: if w in scores: total += scores[w] sampleScores.append(total) scoredSamples = sorted(zip(samples,sampleScores), key=lambda t: t[1]) coin = random.choice([1,-1]) num = random.choice(range(len(samples))) for winner,score in scoredSamples[:coin*num]: sampleq.put(winner) sampleq.put(None) time.sleep(0.1) os._exit(0) else: dp = psutil.Process(os.getpid()) # dp.nice(-10) count = 0 t = 0 while(count < initialSize): count += 1 toProcess = dataq.get() maps = [item for item in mapper.map(toProcess)] redq.put(maps) t = time.time() - initialT if(toProcess is None): stopCondition = True if(debug): print("{} examples of real data processed in {} seconds".format(count, t)) tick = 0 while(not stopCondition): if(debug): count += 1 t = time.time() - initialT if(tick < t // 1): tick = t // 1 print("Sample at {} seconds: {}".format(t, toProcess)) if(toProcess is None): stopCondition = True if(t > maxTime): stopCondition = True toProcess = sampleq.get() maps = [item for item in mapper.map(toProcess)] redq.put(maps) redq.put(None) if debug: print("Last Sample: {}".format(toProcess)) print("{} examples used, {} samples generated".format(initialSize, count)) os.wait() return
def deserialise(db, src_dev_names, dst_dev_names, mapping_json): f = json.loads(mapping_json) f = deunicode(f) #The version we're currently working with version = ''; if 'fileversion' in f: version = f['fileversion'] elif 'mapping' in f and 'fileversion' in f['mapping']: version = f['mapping']['fileversion'] if 'mapping' in f: f = f['mapping'] else: print 'malformed file' modeIdx = { 'undefined': mapper.MODE_UNDEFINED, 'raw': mapper.MODE_RAW, 'linear': mapper.MODE_LINEAR, 'expression': mapper.MODE_EXPRESSION } boundIdx = { 'none': mapper.BOUND_NONE, 'mute': mapper.BOUND_MUTE, 'clamp': mapper.BOUND_CLAMP, 'fold': mapper.BOUND_FOLD, 'wrap': mapper.BOUND_WRAP } locIdx = { 'undefined': mapper.LOC_UNDEFINED, 'source': mapper.LOC_SOURCE, 'destination': mapper.LOC_DESTINATION } src_dev = db.device(src_dev_names[0]) if not src_dev: print "error loading file: couldn't find device ", src_dev_names[0], " in database" return dst_dev = db.device(dst_dev_names[0]) if not dst_dev: print "error loading file: couldn't find device ", dst_dev_names[0], " in database" return if version == '2.1': print "converting version 2.1 mapping file..." # we need to modify a few things for compatibility f['maps'] = [] for connection in f['connections']: if not connection.has_key('mode'): continue; map = {} src = { 'name': connection['source'][0].split('/', 1)[1] } dst = { 'name': connection['destination'][0].split('/', 1)[1] } if connection.has_key('mute'): map['muted'] = connection['mute'] if connection.has_key('expression'): map['expression'] = str(connection['expression'] .replace('s[', 'src[') .replace('d[', 'dst[')) if connection.has_key('srcMin'): src['minimum'] = connection['srcMin'] if connection.has_key('srcMax'): src['maximum'] = connection['srcMax'] if connection.has_key('destMin'): dst['minimum'] = connection['destMin'] if connection.has_key('destMax'): dst['maximum'] = connection['destMax'] if connection.has_key('boundMin'): dst['bound_min'] = connection['boundMin'] if connection.has_key('boundMax'): dst['bound_max'] = connection['boundMax'] mode = connection['mode'] if mode == 'reverse': map['mode'] = 'expression' map['expression'] = 'y=x' map['sources'] = [ dst ] map['destinations'] = [ src ] else: if mode == 'calibrate': map['mode'] = 'linear' dst['calibrating'] = true else: map['mode'] = mode map['sources'] = [ src ] map['destinations'] = [ dst ] f['maps'].append(map) del f['connections'] # "upgrade" version to 2.2 version = '2.2' # This is a version 2.2 save file if version == '2.2': print "loading version 2.2 mapping file..." # todo: we need to enable users to explictly define device matching # for now we will just choose the first device... for map_props in f['maps']: sigs_found = True # The name of the source signals without device name src_sigs = [] for slot_props in map_props['sources']: name = slot_props['name'] name = name.split('/', 1)[1] sig = src_dev.signal(name) if not sig: sigs_found = False continue src_sigs.append(sig) if not sigs_found: continue # And the destination signals dst_sigs = [] for slot_props in map_props['destinations']: name = slot_props['name'] name = name.split('/', 1)[1] sig = dst_dev.signal(name) if not sig: sigs_found = False continue dst_sigs.append(sig) if not sigs_found: continue map = mapper.map(src_sigs[0], dst_sigs[0]) if not map: print "error creating map" continue # set slot properties first index = 0 for slot_props in map_props['sources']: slot = map.source(index) for prop in slot_props: if prop == 'name': # do nothing pass elif prop == 'bound_min': slot.bound_min = boundaryStrings[slot_props[prop]] elif prop == 'bound_min': slot.bound_max = boundaryStrings[slot_props[prop]] elif prop == 'minimum': t = type(slot_props['minimum']) if t is int or t is float: slot.minimum = float(slot_props['minimum']) else: if t is str: slot_props['minimum'] = slot_props['minimum'].replace(',',' ').split() numargs = len(slot_props['minimum']) for i in range(numargs): slot_props['minimum'][i] = float(slot_props['minimum'][i]) if numargs == 1: slot_props['minimum'] = slot_props['minimum'][0] slot.minimum = slot_props['minimum'] elif prop == 'maximum': t = type(slot_props['maximum']) if t is int or t is float: slot.maximum = float(slot_props['maximum']) else: if t is str: slot_props['maximum'] = slot_props['maximum'].replace(',',' ').split() numargs = len(slot_props['maximum']) for i in range(numargs): slot_props['maximum'][i] = float(slot_props['maximum'][i]) if numargs == 1: slot_props['maximum'] = slot_props['maximum'][0] slot.maximum = slot_props['maximum'] else: slot.set_property(prop, slot_props[prop]) index += 1 index = 0 for slot_props in map_props['destinations']: slot = map.destination(index) for prop in slot_props: if prop == 'name': # do nothing pass elif prop == 'bound_min': slot.bound_min = boundaryStrings[slot_props[prop]] elif prop == 'bound_min': slot.bound_max = boundaryStrings[slot_props[prop]] elif prop == 'minimum': t = type(slot_props['minimum']) if t is int or t is float: slot.minimum = float(slot_props['minimum']) else: if t is str: slot_props['minimum'] = slot_props['minimum'].replace(',',' ').split() numargs = len(slot_props['minimum']) for i in range(numargs): slot_props['minimum'][i] = float(slot_props['minimum'][i]) if numargs == 1: slot_props['minimum'] = slot_props['minimum'][0] slot.minimum = slot_props['minimum'] elif prop == 'maximum': t = type(slot_props['maximum']) if t is int or t is float: slot.maximum = float(slot_props['maximum']) else: if t is str: slot_props['maximum'] = slot_props['maximum'].replace(',',' ').split() numargs = len(slot_props['maximum']) for i in range(numargs): slot_props['maximum'][i] = float(slot_props['maximum'][i]) if numargs == 1: slot_props['maximum'] = slot_props['maximum'][0] slot.maximum = slot_props['maximum'] else: slot.set_property(prop, slot_props[prop]) index += 1 # set map properties for prop in map_props: if prop == 'sources' or prop == 'destinations': # do nothing pass elif prop == 'expression': expression = map_props['expression'] expression = re.sub(r'src\[(\d*)\]', 'x\g<1>', expression) expression = re.sub(r'dst\[(\d*)\]', 'y\g<1>', expression) expression = re.sub(r'(x|y)0', '\g<1>', expression) map.expression = expression elif prop == 'muted': map.muted = map_props['muted'] elif prop == 'mode': mode = map_props['mode'] if mode == 'linear': map.mode = mapper.MODE_LINEAR elif mode == 'expression': map.mode = mapper.MODE_EXPRESSION else: map.properties[prop] = map_props[prop] map.push() else: print 'Unknown file version'
src = mapper.device("src") outsig = src.add_output_signal("outsig", 1, "f", None, 0, 100) outsig.reserve_instances(5) dest = mapper.device("dest") insig = dest.add_input_signal("insig", 1, "f", None, 0, 1, h) insig.remove_instance(0) insig.reserve_instances([100, 200, 300]) insig.set_instance_stealing_mode(mapper.STEAL_OLDEST) while not src.ready() or not dest.ready(): src.poll() dest.poll(10) map = mapper.map(outsig, insig).push() while not map.ready(): src.poll(10) dest.poll(10) for i in range(100): r = random.randint(0, 5) id = random.randint(0, 5) if r == 0: print("--> retiring sender instance", id) outsig.release_instance(id) else: print("--> sender instance", id, "updated to", i) outsig.update_instance(id, i) print_instance_ids()
def h(sig, id, val, timetag): print(' handler got', sig.name, '=', val, 'at time', timetag.get_double()) srcs = [mapper.device("src"), mapper.device("src")] outsigs = [srcs[0].add_output_signal("outsig", 1, 'i'), srcs[1].add_output_signal("outsig", 1, 'i')] dest = mapper.device("dest") insig = dest.add_input_signal("insig", 1, 'f', None, None, None, h) while not srcs[0].ready or not srcs[1].ready or not dest.ready: srcs[0].poll(10) srcs[1].poll(10) dest.poll(10) map = mapper.map(outsigs, insig) if not map: print('error: map not created') else: map.mode = mapper.MODE_EXPRESSION map.expression = "y=x0+x1" map.push() while not map.ready: srcs[0].poll(10) srcs[1].poll(10) dest.poll(10) for i in range(100): outsigs[0].update(i) outsigs[1].update(100-i)
db.add_map_callback(lambda x,y:database_cb('map',x,y)) while not dev.ready: dev.poll(10) db.poll() start.now() outsig = dev.signal("outsig") for i in range(1000): dev.poll(10) db.poll() outsig.update([i+1,i+2,i+3,i+4]) if i==250: map = mapper.map(outsig, dev.signal("insig")) map.mode = mapper.MODE_EXPRESSION map.expression = 'y=y{-1}+x' map.source().minimum = [1,2,3,4] map.source().bound_min = mapper.BOUND_WRAP map.source().bound_max = mapper.BOUND_CLAMP map.push() # # test creating multi-source map # map = mapper.map([sig1, sig2], sig3) # map.mode = mapper.MODE_EXPRESSION # map.expression = 'y=x0-x1' # map.push() if i==500: print('muting map')
def h(sig, id, f, timetag): try: print '--> source received', f except: print 'exception' print sig, f src = mapper.device("src") outsig = src.add_output_signal("outsig", 1, 'f', None, 0, 1000) outsig.set_callback(h) dest = mapper.device("dest") insig = dest.add_input_signal("insig", 1, 'f', None, 0, 1) while not src.ready() or not dest.ready(): src.poll(10) dest.poll(10) map = mapper.map(insig, outsig).push() while not map.ready(): src.poll(10) dest.poll(10) for i in range(100): print 'updating destination to', i, '-->' insig.update(i) src.poll(10) dest.poll(10)
src = mapper.device("src") outsig = src.add_output_signal("outsig", 1, 'f', None, 0, 100) outsig.reserve_instances(5) dest = mapper.device("dest") insig = dest.add_input_signal("insig", 1, 'f', None, 0, 1, h) insig.remove_instance(0) insig.reserve_instances([100, 200, 300]) insig.set_instance_stealing_mode(mapper.STEAL_OLDEST) while not src.ready or not dest.ready: src.poll() dest.poll(10) map = mapper.map(outsig, insig).push() while not map.ready: src.poll(10) dest.poll(10) for i in range(100): r = random.randint(0, 5) id = random.randint(0, 5) if r == 0: print('--> retiring sender instance', id) outsig.release_instance(id) else: print('--> sender instance', id, 'updated to', i) outsig.update_instance(id, i) print_instance_ids()
rows = cur.fetchall() for i in range(len(cap)): if cap[i][3:5] == '29': #print "------------------" + `i` + "---------------------" #result = result + "+++++++++++++ START of PACKET ++++++++++++++++" result_d = result_d + "packet number: " + ` i ` + "\n" + cap[i] + "\n" result_o = result_o + "packet number: " + ` i ` + "\n" + cap[i] + "\n" result_n1 = result_n1 + "packet number: " + ` i ` + "\n" + cap[i] + "\n" result_n2 = result_n2 + "packet number: " + ` i ` + "\n" + cap[i] + "\n" result_n3 = result_n3 + "packet number: " + ` i ` + "\n" + cap[i] + "\n" result_n4 = result_n4 + "packet number: " + ` i ` + "\n" + cap[i] + "\n" result_n5 = result_n5 + "packet number: " + ` i ` + "\n" + cap[i] + "\n" result_n6 = result_n6 + "packet number: " + ` i ` + "\n" + cap[i] + "\n" result_d = result_d + mapper.map(cap[i]) + "\n" result_o = result_o + opcode_id.findOpcodes(cap[i]) + "\n" #print result_o if "09:be:09:ff" in cap[i]: result = result + "metadata" else: result = result + "no" bytes = cap[i].split(':') my_dict_1 = {i: bytes.count(i) for i in bytes} result_n1 = result_n1 + ` my_dict_1 ` + "\n" done = [] my_dict_2 = {} for j in range(len(bytes) - 1): if bytes[j] + ":" + bytes[j + 1] not in done: my_dict_2[bytes[j] + ":" + bytes[j + 1]] = cap[i].count(bytes[j] + ":" +
def deserialise(db, src_dev_names, dst_dev_names, mapping_json): f = json.loads(mapping_json) f = deunicode(f) #The version we're currently working with version = ''; if 'fileversion' in f: version = f['fileversion'] elif 'mapping' in f and 'fileversion' in f['mapping']: version = f['mapping']['fileversion'] if 'mapping' in f: f = f['mapping'] else: print('malformed file') modeIdx = { 'undefined': mapper.MODE_UNDEFINED, 'raw': mapper.MODE_RAW, 'linear': mapper.MODE_LINEAR, 'expression': mapper.MODE_EXPRESSION } boundIdx = { 'none': mapper.BOUND_NONE, 'mute': mapper.BOUND_MUTE, 'clamp': mapper.BOUND_CLAMP, 'fold': mapper.BOUND_FOLD, 'wrap': mapper.BOUND_WRAP } locIdx = { 'undefined': mapper.LOC_UNDEFINED, 'source': mapper.LOC_SOURCE, 'destination': mapper.LOC_DESTINATION } src_dev = db.device(src_dev_names[0]) if not src_dev: print("error loading file: couldn't find device ", src_dev_names[0], " in database") return dst_dev = db.device(dst_dev_names[0]) if not dst_dev: print("error loading file: couldn't find device ", dst_dev_names[0], " in database") return if version == '2.1': print("converting version 2.1 mapping file...") # we need to modify a few things for compatibility f['maps'] = [] for connection in f['connections']: if not connection.has_key('mode'): continue; map = {} src = { 'name': connection['source'][0].split('/', 1)[1] } dst = { 'name': connection['destination'][0].split('/', 1)[1] } if connection.has_key('mute'): map['muted'] = connection['mute'] if connection.has_key('expression'): map['expression'] = str(connection['expression'] .replace('s[', 'src[') .replace('d[', 'dst[')) if connection.has_key('srcMin'): src['minimum'] = connection['srcMin'] if connection.has_key('srcMax'): src['maximum'] = connection['srcMax'] if connection.has_key('destMin'): dst['minimum'] = connection['destMin'] if connection.has_key('destMax'): dst['maximum'] = connection['destMax'] if connection.has_key('boundMin'): dst['bound_min'] = connection['boundMin'] if connection.has_key('boundMax'): dst['bound_max'] = connection['boundMax'] mode = connection['mode'] if mode == 'reverse': map['mode'] = 'expression' map['expression'] = 'y=x' map['sources'] = [ dst ] map['destinations'] = [ src ] else: if mode == 'calibrate': map['mode'] = 'linear' dst['calibrating'] = true else: map['mode'] = mode map['sources'] = [ src ] map['destinations'] = [ dst ] f['maps'].append(map) del f['connections'] # "upgrade" version to 2.2 version = '2.2' # This is a version 2.2 save file if version == '2.2': print("loading version 2.2 mapping file...") # todo: we need to enable users to explictly define device matching # for now we will just choose the first device... for map_props in f['maps']: sigs_found = True # The name of the source signals without device name src_sigs = [] for slot_props in map_props['sources']: name = slot_props['name'] name = name.split('/', 1)[1] sig = src_dev.signal(name) if not sig: sigs_found = False continue src_sigs.append(sig) if not sigs_found: continue # And the destination signals dst_sigs = [] for slot_props in map_props['destinations']: name = slot_props['name'] name = name.split('/', 1)[1] sig = dst_dev.signal(name) if not sig: sigs_found = False continue dst_sigs.append(sig) if not sigs_found: continue map = mapper.map(src_sigs[0], dst_sigs[0]) if not map: print("error creating map") continue # set slot properties first index = 0 for slot_props in map_props['sources']: slot = map.source(index) for prop in slot_props: if prop == 'name': # do nothing pass elif prop == 'bound_min': slot.bound_min = boundaryStrings[slot_props[prop]] elif prop == 'bound_min': slot.bound_max = boundaryStrings[slot_props[prop]] elif prop == 'minimum': t = type(slot_props['minimum']) if t is int or t is float: slot.minimum = float(slot_props['minimum']) else: if t is str: slot_props['minimum'] = slot_props['minimum'].replace(',',' ').split() numargs = len(slot_props['minimum']) for i in range(numargs): slot_props['minimum'][i] = float(slot_props['minimum'][i]) if numargs == 1: slot_props['minimum'] = slot_props['minimum'][0] slot.minimum = slot_props['minimum'] elif prop == 'maximum': t = type(slot_props['maximum']) if t is int or t is float: slot.maximum = float(slot_props['maximum']) else: if t is str: slot_props['maximum'] = slot_props['maximum'].replace(',',' ').split() numargs = len(slot_props['maximum']) for i in range(numargs): slot_props['maximum'][i] = float(slot_props['maximum'][i]) if numargs == 1: slot_props['maximum'] = slot_props['maximum'][0] slot.maximum = slot_props['maximum'] else: slot.set_property(prop, slot_props[prop]) index += 1 index = 0 for slot_props in map_props['destinations']: slot = map.destination(index) for prop in slot_props: if prop == 'name': # do nothing pass elif prop == 'bound_min': slot.bound_min = boundaryStrings[slot_props[prop]] elif prop == 'bound_min': slot.bound_max = boundaryStrings[slot_props[prop]] elif prop == 'minimum': t = type(slot_props['minimum']) if t is int or t is float: slot.minimum = float(slot_props['minimum']) else: if t is str: slot_props['minimum'] = slot_props['minimum'].replace(',',' ').split() numargs = len(slot_props['minimum']) for i in range(numargs): slot_props['minimum'][i] = float(slot_props['minimum'][i]) if numargs == 1: slot_props['minimum'] = slot_props['minimum'][0] slot.minimum = slot_props['minimum'] elif prop == 'maximum': t = type(slot_props['maximum']) if t is int or t is float: slot.maximum = float(slot_props['maximum']) else: if t is str: slot_props['maximum'] = slot_props['maximum'].replace(',',' ').split() numargs = len(slot_props['maximum']) for i in range(numargs): slot_props['maximum'][i] = float(slot_props['maximum'][i]) if numargs == 1: slot_props['maximum'] = slot_props['maximum'][0] slot.maximum = slot_props['maximum'] else: slot.set_property(prop, slot_props[prop]) index += 1 # set map properties for prop in map_props: if prop == 'sources' or prop == 'destinations': # do nothing pass elif prop == 'expression': expression = map_props['expression'] expression = re.sub(r'src\[(\d*)\]', 'x\g<1>', expression) expression = re.sub(r'dst\[(\d*)\]', 'y\g<1>', expression) expression = re.sub(r'(x|y)0', '\g<1>', expression) map.expression = expression elif prop == 'muted': map.muted = map_props['muted'] elif prop == 'mode': mode = map_props['mode'] if mode == 'linear': map.mode = mapper.MODE_LINEAR elif mode == 'expression': map.mode = mapper.MODE_EXPRESSION else: map.properties[prop] = map_props[prop] map.push() else: print('Unknown file version')