Example #1
0
def diff_dicts(dict1, dict2):
    """Compares two dicts and returns the difference as a string,
    if there is any.

    Sorts two dicts (including sorting of the lists!!) and then diffs them.
    This will ignore string types ('unicode' vs 'string').

    args:
        dict1: First dict
        dict2: Second dict

    returns:
        A diff string if there's any difference, otherwise None.
    """
    dict1 = order_dict(dict1)
    dict2 = order_dict(dict2)

    if dict1 == dict2:
        return

    dict1 = pprint.pformat(dict1).splitlines()
    dict2 = pprint.pformat(dict2).splitlines()

    # Remove unicode identifiers.
    dict1 = map(lambda line: line.replace('u\'', '\''), dict1)
    dict2 = map(lambda line: line.replace('u\'', '\''), dict2)

    return '\n'.join(difflib.unified_diff(dict1, dict2, n=2))
Example #2
0
def test_format(obj, precision=6):
    tf = lambda o: test_format(o, precision)
    delimit = lambda o: ', '.join(o)
    otype = type(obj)
    if otype is str:
        return "'%s'" % obj
    elif otype is float or otype is int:
        if otype is int:
            obj = float(obj)
        fstr = '%%.%df' % precision
        return fstr % obj
    elif otype is set:
        if len(obj) == 0:
            return 'set()'
        return '{%s}' % delimit(sorted(map(tf, obj)))
    elif otype is dict:
        return '{%s}' % delimit(sorted(tf(k)+': '+tf(v) for k,v in obj.items()))
    elif otype is list:
        return '[%s]' % delimit(map(tf, obj))
    elif otype is tuple:
        return '(%s%s)' % (delimit(map(tf, obj)), ',' if len(obj) is 1 else '')
    elif otype.__name__ in ['Vec','Mat']:
        entries = tf({x:obj.f[x] for x in obj.f if obj.f[x] != 0})
        return '%s(%s, %s)' % (otype.__name__, test_format(obj.D), entries)
    else:
        return str(obj)
Example #3
0
    def schema_shell(self):
        """Performs the 'schema-shell' command."""
        schema_shell_home = self.env.get(SCHEMA_SHELL_HOME)
        assert (schema_shell_home is not None), \
            ("Environment variable undefined: %r" % SCHEMA_SHELL_HOME)
        assert os.path.isdir(schema_shell_home), \
            ("Invalid home directory for KijiSchema shell: %r" % schema_shell_home)
        schema_shell_script = os.path.join(schema_shell_home, "bin", "kiji-schema-shell")
        assert os.path.isfile(schema_shell_script), \
            ("KijiSchema shell not found: %r" % schema_shell_script)

        env = dict(self.env)

        classpath = env.get(KIJI_CLASSPATH, "").split(":") + list(self.express.get_classpath())
        env[KIJI_CLASSPATH] = ":".join(classpath)

        java_opts = env.get("JAVA_OPTS", "")
        # FIXME: I cannot find any trace of the Java system property "express.tmpjars"!
        # java_opts += (" -Dexpress.tmpjars=%s" % ???)

        # Relevant for KijiSchema 1.1 only and will be removed in Express 3.0:
        java_opts += " -Dorg.kiji.schema.impl.AvroCellEncoder.SCHEMA_VALIDATION=DISABLED"
        env["JAVA_OPTS"] = java_opts

        cmd = [schema_shell_script]
        logging.debug("Launching kiji-schema shell with:\n%s\with KIJI_CLASSPATH:\n%s",
                      " \\\n\t".join(map(repr, cmd)), "\n".join(map(tab_indent, classpath)))
        logging.debug("Computed KIJI_CLASSPATH:")
        proc = subprocess.Popen(cmd, env=env)
        try:
            return proc.wait()
        except subprocess.SubProcessError:
            proc.kill()
Example #4
0
 def testSequential(self):
     dq = ResizableDispatchQueue(self.slow, 1)
     map(dq.put, range(3))
     # This should finish in about 0.6 seconds, with nothing still on
     # the queue because we stop after 0.5 seconds so all three tasks
     # will have been dispatched one after another.
     return self._stopAndTest(0.5, dq, [])
Example #5
0
 def testNarrowNarrowWiden(self):
     dq = ResizableDispatchQueue(self.slow, 3)
     map(dq.put, range(11))
     reactor.callLater(0.1, dq.setWidth, 2)
     reactor.callLater(0.3, dq.setWidth, 1)
     reactor.callLater(0.7, dq.setWidth, 3)
     return self._stopAndTest(0.9, dq, [10])
def parsexyz(filename):
    lst = parseALL(filename," Number     Number       Type","         ")
    lines = map(float,lst[1:-1])
    xyz = [lines[x:x+3] for x in xrange(0, len(lines), 3)] 
    b = xyz[1::2]
    Z = map(int,lines[1::6])
    return (Z,np.array(b)) 
Example #7
0
def trigger_mopage_refresh(obj, event):
    event_pages = filter(None,
                          map(lambda parent: IEventPage(parent, None),
                              aq_chain(obj)))
    if not event_pages:
        # We are not within an event page.
        # We only trigger when publishing an event page
        # or a child of an event page.
        return

    triggers = filter(None,
                      map(lambda parent: IPublisherMopageTrigger(parent, None),
                          aq_chain(obj)))
    if not triggers or not triggers[0].is_enabled():
        return

    for events in event_pages:
        IMopageModificationDate(events).touch()

    from collective.taskqueue import taskqueue

    trigger_url = triggers[0].build_trigger_url()
    callback_path = '/'.join(getSite().getPhysicalPath()
                             + ('taskqueue_events_trigger_mopage_refresh',))
    taskqueue.add(callback_path, params={'target': trigger_url})
Example #8
0
def run_clustering_example(run):
  global current_graph_state
  n = 100
  accs = []
  for i in range(100):
    current_graph_state = GraphState()
    sampler = run()
    samples = [sampler() for i in range(n)]
    templ = current_graph_state.to_JSON()
    rand_params = hs_rand_template_params(templ)
    print hs_sample_bayes_net(templ, rand_params)
    varvals = state_to_varvals(hs_sample_bayes_net(templ, rand_params))
    frozen_samples = [freeze_value(samp, varvals) for samp in samples]
    true_latents = [x[0] for x in frozen_samples]
    print true_latents
    templ = condition_on_frozen_samples(current_graph_state, samples, [x[1] for x in frozen_samples])
    print 'best score', params_score(templ, rand_params)
    state_params_list = infer_states_and_parameters(templ)
    rand_cs = params_to_cluster_centers(rand_params)
    iter_accs = []
    j = 0
    for (state, params, score) in state_params_list:
      print 'score', score
      cs = params_to_cluster_centers(params)
      # if j > 1:
      #   varvals = state_to_varvals(state)
      #   state_latents = [freeze_value(samp[0], varvals) for samp in samples]
      #   acc = cluster_assignment_accuracy(true_latents, state_latents)
      #   iter_accs.append(acc)
      j += 1
    accs.append(iter_accs)
  print map(mean, zip(*accs))
def find_segments(doc, key, use_segment_table = True):
    key_pieces = key.split(':')
    while len(key_pieces) < 3:
        key_pieces.append('*')

    filter_func = lambda x: str(x.ifos) == key_pieces[0] and (str(x.name) == key_pieces[1] or key_pieces[1] == '*') and (str(x.version) == key_pieces[2] or key_pieces[2] == '*') 

    # Find all segment definers matching the critieria
    seg_def_table = lsctables.SegmentDefTable.get_table(doc)
    seg_defs      = filter(filter_func, seg_def_table)
    seg_def_ids   = map(lambda x: str(x.segment_def_id), seg_defs)

    # Find all segments belonging to those definers
    if use_segment_table:
        seg_table     = lsctables.SegmentTable.get_table(doc)
        seg_entries   = filter(lambda x: str(x.segment_def_id) in seg_def_ids, seg_table)
    else:
        seg_sum_table = lsctables.SegmentSumTable.get_table(doc)
        seg_entries   = filter(lambda x: str(x.segment_def_id) in seg_def_ids, seg_sum_table)

    # Combine into a segmentlist
    ret = segmentlist(map(lambda x: segment(x.start_time, x.end_time), seg_entries))

    ret.coalesce()

    return ret
Example #10
0
 def step(self):
     r = self.table.step(self.tps)
     self.t += self.tps
     if not self.table.fullyOcc():
         self.lastseent = self.t
         self.lastseen = map(int,self.table.ball.getpos())
     #print self.lastseen, self.lastseent
     
     # Update particles
     weights = [p.weight for p in self.particles]
     ps = self.getPartPs()
     newws = [w*p for w,p in zip(weights, ps)]
     newws.append(self.newp)
     newws = map(lambda x: np.power(x,self.temp), newws)
     totw = sum(newws)
     newws = map(lambda x: x / totw, newws)
     #seff = sum(map(lambda w: 1 / (w*w), newws))
     newparts = copy.copy(self.particles); newparts.append("Empty")
     newps = selectReplace(newparts,newws,len(self.particles))
     rejns = 0.
     for i in range(len(newps)):
         if newps[i] == "Empty": newps[i] = Particle(self.table,self.kapv,self.kapb,self.kapm,self.perr,self.tps,self.lastseent, self.lastseen); rejns += 1.
         #else: newps[i] = copy.deepcopy(newps[i])
     for p in newps: p.weight = 1
     self.lastrej = rejns / self.npart
     self.particles = newps
     return r
Example #11
0
    def get_disks(self):
        disks_to_attach_names = self.__get_disks_to_attach_names()
        self.disks = map(lambda disk: self.__add_attach_data_to_disk(disk, disks_to_attach_names), self.disks)

        return map(lambda disk: {"name": disk.getAttribute('ovf:diskId'),
                                 "capacity": int(disk.getAttribute('ovf:capacity')) / (2**30),
                                 "attach": disk.getAttribute('attach')}, self.disks)
Example #12
0
    def plot_data(self,v):
        """
        Simple plot window that can be updated very fast.
        No grid or resize like plot()
        """
        if self.plotwin == None:
            self.plotwin = Tk()
            self.plotwin.title('Phoenix plot')
            self.plotwin.protocol("WM_DELETE_WINDOW", self.clean_qplot)
            self.canvas = Canvas(self.plotwin, background='white', width=WIDTH + 20, height=HALF_HEIGHT*2 + 20)
            self.canvas.pack()

            self.canvas.create_rectangle(10, 10, WIDTH+10, HALF_HEIGHT*2 + 10, outline='#009900')
            self.canvas.create_line([(10, HALF_HEIGHT+10), (WIDTH+10, HALF_HEIGHT+10)], fill='#00ff00')
        if len(self.plot_trace) != 0:
            map(lambda x: self.canvas.delete(x), self.plot_trace)
            self.plot_trace = []
            self.plotwin.update()
                    
        numchans = len(v[0]) - 1
        npoints = len(v)
        xscale = WIDTH/v[-1][0]
        yscale = HALF_HEIGHT/YMAX
        for ch in range(numchans):
            a = []
            for i in range(npoints):
                x = 10 + v[i][0] * xscale
                y = (HALF_HEIGHT + 10) - v[i][ch+1] * yscale
                a.append((x, y))
            line = self.canvas.create_line(a, fill=self.colors[ch])            
            self.plot_trace.append(line)
        self.plotwin.update()
def farpairs_ranges(ratings):
    pairs = []
    for i in xrange(len(ratings)):
        for j in xrange(len(ratings)):
            dist = abs(i-j)
            std1 = ratings[i][2]
            avg1 = ratings[i][1]
            std2 = ratings[j][2]
            avg2 = ratings[j][1]

            if (avg1 + std1 > avg2 - std2 and avg1 + std1 <= avg2 + std2) or \
               (avg2 + std2 > avg1 - std1 and avg2 + std2 <= avg1 + std1):
                pairs.append([i, j, dist])
    pairs.sort(lambda x,y: x[2] < y[2] and -1 or 1)
    pairs = map(lambda x: x[:1], pairs)
    idxs = []
    for p in pairs:
        idxs.extend(p)
    subset = set()
    while len(idxs) > 0 and len(subset) < WINDOW:
        subset.add(idxs.pop())
    
    subset = filter(lambda x: x in subset, range(len(ratings)))
    subset = map(lambda x: ratings[x], subset)
            
    return reorder(ratings, subset)
Example #14
0
	def test_001_diff_phasor_vcc(self):
		a = [1+2j,2+3.5j,3.5+4j,4+5j,5+6j]
		b = [1j,1j,1j,1j,1j]
		c = [-1j+3,1j,-7+0j,2.5j+0.333,3.2j]
		d = [(0.35979271051026462+0.89414454782483865j),
		     (0.19421665709046287+0.024219594550527801j),
		     (0.12445564785882557+0.40766238899138718j),
		     (0.041869638845043688+0.97860437393366329j),
		     (0.068927762235083234+0.16649764877365247j)]
		e = [(0.16207552830286298+0.435385030608331j),
		     (0.47195779613669675+0.37824764113272558j),
		     (0.13911998015446148+0.6585095669811617j),
		     (0.093510743358783954+0.98446560079828938j),
		     (0.86036393297704694+0.72043005342024602j)]
		multconj = lambda x,y: x.conjugate()*y
		src_data        = a+b+c+d+e
		expected_result = [0j,0j,0j,0j,0j]+map(multconj,a,b)+map(multconj,b,c)+map(multconj,c,d)+map(multconj,d,e)
		src = blocks.vector_source_c(src_data)
		s2v = blocks.stream_to_vector(gr.sizeof_gr_complex, 5)
		diff_phasor_vcc = grdab.diff_phasor_vcc(5)
		v2s = blocks.vector_to_stream(gr.sizeof_gr_complex, 5)
		dst = blocks.vector_sink_c()
		self.tb.connect(src, s2v, diff_phasor_vcc, v2s, dst)
		self.tb.run()
		result_data = dst.data()
		# print expected_result
		# print result_data
		self.assertComplexTuplesAlmostEqual(expected_result, result_data, 6)
Example #15
0
    def get_budgets(self):  # {{{
        # Get categories
        categories = self.get_categories()

        # Issue request for budget utilization
        first_of_this_month = date.today().replace(day=1)
        eleven_months_ago = (first_of_this_month - timedelta(days=330)).replace(day=1)
        url = "{}/getBudget.xevent".format(MINT_ROOT_URL)
        params = {
            'startDate': eleven_months_ago.strftime('%m/%d/%Y'),
            'endDate': first_of_this_month.strftime('%m/%d/%Y'),
            'rnd': Mint.get_rnd(),
        }
        response = json.loads(self.get(url, params=params, headers=JSON_HEADER).text)

        # Make the skeleton return structure
        budgets = {
            'income': response['data']['income'][
                str(max(map(int, response['data']['income'].keys())))
            ]['bu'],
            'spend': response['data']['spending'][
                str(max(map(int, response['data']['income'].keys())))
            ]['bu']
        }

        # Fill in the return structure
        for direction in budgets.keys():
            for budget in budgets[direction]:
                budget['cat'] = self.get_category_from_id(
                    budget['cat'],
                    categories
                )

        return budgets
Example #16
0
 def test_0(self):
     accuracies = map(get_lfw_restricted_accuracy,
                      map(lambda x: '%02i' % x, range(1, 11)))
     accuracy_avg = np.mean(np.array(accuracies))
     accuracy_std = np.std(np.array(accuracies))
     print('Accuracy on LFW test set (10 fold CV): %4.3g +/- %4.3g' % (
         accuracy_avg, accuracy_std))
Example #17
0
def moderate_tags(html):
    """replaces instances of <a> and <img>
    with "item in moderation" alerts
    """
    from askbot.conf import settings
    soup = BeautifulSoup(html, 'html5lib')
    replaced = False
    if settings.MODERATE_LINKS:
        links = soup.find_all('a')
        if links:
            template = get_template('widgets/moderated_link.jinja')
            aviso = BeautifulSoup(template.render(), 'html5lib').find('body')
            map(lambda v: v.replaceWith(aviso), links)
            replaced = True

    if settings.MODERATE_IMAGES:
        images = soup.find_all('img')
        if images:
            template = get_template('widgets/moderated_link.jinja')
            aviso = BeautifulSoup(template.render(), 'html5lib').find('body')
            map(lambda v: v.replaceWith(aviso), images)
            replaced = True

    if replaced:
        return force_text(soup.find('body').renderContents(), 'utf-8')

    return html
Example #18
0
def reverse_lists(lists):
	"""
	>>> reverse_lists([[1,2,3], [4,5,6]])
	[[3, 2, 1], [6, 5, 4]]
	"""

	return list(map(list, map(reversed, lists)))
Example #19
0
def calculateSparseDictCOO(data_set, data_label_hash, jump=1, valid_flag=False):
	row = []
	col = []
	data = []
	row_valid = []
	col_valid = []
	data_valid = []

	doc_ids = set(sorted(map(lambda row:int(row[0]), data_set)))
	base_ids_list = filter(lambda ids: ids % jump == 0, doc_ids)
	train_ids = base_ids_list
	valid_ids = set()
	if valid_flag:
		valid_index = filter(lambda ids: ids % validation_perc == 0, range(len(base_ids_list)))
		valid_ids = [base_ids_list[i] for i in valid_index]
		base_ids = set(base_ids_list)
		train_ids = sorted(base_ids - set(valid_ids))

	labels = map(lambda trid: int(data_label_hash[trid]), train_ids)
	labels_valid = map(lambda vlid: int(data_label_hash[vlid]), valid_ids)
	for i in range(len(data_set)):
		if int(data_set[i][0]) in train_ids:
			row.append(int(data_set[i][0]))
			col.append(int(data_set[i][1])-1)
			data.append(int(data_set[i][2]))
			# labels.append(int(data_label_hash[int(data_set[i][0])]))
		elif int(data_set[i][0]) in valid_ids:
			row_valid.append(int(data_set[i][0]))
			col_valid.append(int(data_set[i][1])-1)
			data_valid.append(int(data_set[i][2]))
			# labels_valid.append(int(data_label_hash[int(data_set[i][0])]))

	train = translate(row), col, data, labels
	valid = translate(row_valid), col_valid, data_valid, labels_valid
	return train, valid
Example #20
0
  def child_urls(self):
    if self.response().status_code != 200 or self.response().headers['content-type'] != 'text/html' :
      return []

    hrefs = map(lambda a: str(a.get('href')), self.soup().find_all('a'))
    childrens = filter(bool, map(self.process_href, hrefs))
    return childrens
Example #21
0
 def testJustOne(self):
     dq = ResizableDispatchQueue(self.slow, 1)
     map(dq.put, range(3))
     # This should finish in about 0.2 seconds, with 2 things still on
     # the queue because we stop it after 0.001 so task 0 is already
     # executing but 1 and 2 are queued.
     return self._stopAndTest(0.001, dq, [1, 2])
Example #22
0
	def __call__(self, url, count_of_crawler):
		"""
		Function which fetch the content from the given URL and collect all the
		URL in the content and pass the first url of the page to fetch the
		content.
		"""
		try:
			page = urllib2.urlopen(url)
			soup = BeautifulSoup(page.read())	

			links_on_page = map(lambda anchor: anchor.get('href'), 
						soup.find_all('a'))

			cleaned_url = map(lambda link: link if urlparse(link).scheme 
	 				and urlparse(url).netloc else (urlparse(url)
					.scheme+"://"+urlparse(url).netloc+link if 
					link[0] == "/" else url+link), links_on_page)
			visited_url.append(url)
			total_collected_url.append(cleaned_url)
			next_url_to_visit = [next_url for next_url in cleaned_url\
				 if not next_url in visited_url and not "#" in next_url][0]
		
			if count_of_crawler and next_url_to_visit:	
				count_of_crawler = crawler(next_url_to_visit, 
								count_of_crawler-1)
	
		except:
			print "It seems there is some issue in URL "+url
	
		return count_of_crawler
Example #23
0
 def testParallel(self):
     dq = ResizableDispatchQueue(self.slow, 3)
     map(dq.put, range(3))
     # This should finish in about 0.2 seconds, with nothing on the
     # queue.  We stop after just 0.001 seconds. All three tasks should
     # be dispatched immediately.
     return self._stopAndTest(0.001, dq, [])
Example #24
0
def initialize_constants():
    global __version__, __appname__, modules, functions, basenames, scripts

    src = open('src/calibre/constants.py', 'rb').read()
    nv = re.search(r'numeric_version\s+=\s+\((\d+), (\d+), (\d+)\)', src)
    __version__ = '%s.%s.%s'%(nv.group(1), nv.group(2), nv.group(3))
    __appname__ = re.search(r'__appname__\s+=\s+(u{0,1})[\'"]([^\'"]+)[\'"]',
            src).group(2)
    epsrc = re.compile(r'entry_points = (\{.*?\})', re.DOTALL).\
            search(open('src/calibre/linux.py', 'rb').read()).group(1)
    entry_points = eval(epsrc, {'__appname__': __appname__})

    def e2b(ep):
        return re.search(r'\s*(.*?)\s*=', ep).group(1).strip()

    def e2s(ep, base='src'):
        return (base+os.path.sep+re.search(r'.*=\s*(.*?):', ep).group(1).replace('.', '/')+'.py').strip()

    def e2m(ep):
        return re.search(r'.*=\s*(.*?)\s*:', ep).group(1).strip()

    def e2f(ep):
        return ep[ep.rindex(':')+1:].strip()

    basenames, functions, modules, scripts = {}, {}, {}, {}
    for x in ('console', 'gui'):
        y = x + '_scripts'
        basenames[x] = list(map(e2b, entry_points[y]))
        functions[x] = list(map(e2f, entry_points[y]))
        modules[x] = list(map(e2m, entry_points[y]))
        scripts[x] = list(map(e2s, entry_points[y]))
Example #25
0
 def testNarrowNarrowWidenNarrow(self):
     items = range(14)
     dq = ResizableDispatchQueue(self.slow, 3)
     map(dq.put, items)
     # jobs 0, 1, 2 will be dispatched at time 0.00
     reactor.callLater(0.1, dq.setWidth, 2)
     reactor.callLater(
         0.1, self._testPending, dq, [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
     # At time 0.20 jobs 0, 1, and 2 will all finish, but the
     # queue now has width 2, so 3 and 4 will be dispatched.
     reactor.callLater(
         0.25, self._testPending, dq, [5, 6, 7, 8, 9, 10, 11, 12, 13])
     reactor.callLater(0.3, dq.setWidth, 1)
     # 3 and 4 will finish at 0.40, and the queue is now width 1, so 5
     # will be dispatched at 0.40. Then 6 will be dispatched at 0.60.
     reactor.callLater(0.7, dq.setWidth, 3)
     # At 0.7 the queue is widened to 3, so 2 more jobs (7, 8) will be
     # dispatched. 6 will finish at 0.80, so one more job (9) will be
     # dispatched at 0.8.
     reactor.callLater(0.85, dq.setWidth, 1)
     # 7 and 8 will have finished at 0.9, at which point the queue is
     # down to size 1, but 9 is still in progress. At 1.0, 9 will finish
     # and 10 will launch. At 1.20, 10 will finish and 11 will be
     # dispatched.  11 will finish at about 1.40.
     return self._stopAndTest(1.35, dq, [12, 13])
Example #26
0
	def render(self, path, context={}, elements={}, content_type='text/html', headers={}, **kwargs):

		''' Return a response containing a rendered Jinja template. Creates a session if one doesn't exist. '''
		
		if isinstance(self.context, dict) and len(self.context) > 0:
			tmp_context = self.context
			self.context = self.baseContext
			map(self._setcontext, tmp_context)
		else:
			self.context = self.baseContext
				
		# Build response HTTP headers
		response_headers = {}
		for key, value in self.baseHeaders.items():
			response_headers[key] = value
		if len(headers) > 0:
			for key, value in headers.items():
				response_headers[key] = value
		
		# Consider kwargs
		if len(kwargs) > 0:
			for k, v in kwargs.items():
				self.context[k] = v
		
		# Bind runtime-level template context
		try:
			self.context = self._bindRuntimeTemplateContext(self.context)
		except NotImplementedError, e:
			if config.debug:
				raise ## in production, the show must go on...
			else:
				pass
    def to_dict(self):
        """
        Returns the model properties as a dict
        """
        result = {}

        for attr, _ in iteritems(self.swagger_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value

        return result
Example #28
0
    def vectorize(self):
        ld = '('
        rd = ')'
        outstr = ''

        if self.__dict__[field_rarity]:
            outstr += ld + self.__dict__[field_rarity] + rd + ' '

        coststr = self.__dict__[field_cost].vectorize(delimit = True)
        if coststr:
            outstr += coststr + ' '

        typestr = ' '.join(map(lambda s: '(' + s + ')',
                               self.__dict__[field_supertypes] + self.__dict__[field_types]))
        if typestr:
            outstr += typestr + ' '

        if self.__dict__[field_subtypes]:
            outstr += ' '.join(self.__dict__[field_subtypes]) + ' '

        if self.__dict__[field_pt]:
            outstr += ' '.join(map(lambda s: '(' + s + ')',
                                   self.__dict__[field_pt].replace('/', '/ /').split()))
            outstr += ' '
        
        if self.__dict__[field_loyalty]:
            outstr += '((' + self.__dict__[field_loyalty] + ')) '
            
        outstr += self.__dict__[field_text].vectorize()

        if self.bside:
            outstr = '_ASIDE_ ' + outstr + '\n\n_BSIDE_ ' + self.bside.vectorize()

        return outstr
Example #29
0
def search_in_board(words, board):
    trie = Trie.create(words+words[::-1])
    acc_hash = {}
    handled_paths = []
    pos_list = [(i,j) for i in range(len(board)) for j in range(len(board[0]))]
    while len(pos_list) > 0:
        i,j = pos_list.pop(0)
        cur_char = board[i][j]
        # ((0,0),'o',[])
        cur_word_point = ([(i,j)], cur_char)
        # [((1,0),'e'),((0,1),'a')]
        neighbors = find_neighbors((i,j),board)
        cur_words = acc_hash.get((i,j), [])
        # remove all the paths which have been handled
        cur_words = filter(lambda x: x[0] not in handled_paths, cur_words)
        filtered_prefixs = filter_by_prefix(
                cur_words+[cur_word_point], neighbors, trie)
        # [((0,1),'oa',[(0,0)])]
        update_acc_hash(acc_hash, filtered_prefixs)
        # add all the paths which have been handled
        map(lambda x: handled_paths.append(x[0]), cur_words)
        # add some position for new path
        for cur_word_point in filtered_prefixs:
            cur_pos = cur_word_point[0][-1]
            if cur_pos not in pos_list:
                pos_list.append(cur_pos)


    # return acc_hash
    word_points = filter_words(acc_hash)
    return map(lambda x: (x[1], x[0]), word_points)
Example #30
0
def store_counts(data, stats, now):
	logger.info('Storing count snapshot')
	with database.transaction() as t:
		t.execute('SELECT name, id FROM count_types ORDER BY ord')
		name_data = t.fetchall()
		name_order = map(lambda x: x[0], name_data)
		names = dict(name_data)
		# It seems MySQL complains with insert ... select in some cases.
		# So we do some insert-select-insert magic here. That is probably
		# slower, but no idea how to help that. And it should work.
		t.execute('SELECT name, id FROM clients WHERE name IN (' + (','.join(['%s'] * len(data))) + ')', data.keys())
		clients = dict(t.fetchall())
		# Create a snapshot for each client
		t.executemany('INSERT INTO count_snapshots (timestamp, client) VALUES(%s, %s)', map(lambda client: (now, client), clients.values()))
		t.execute('SELECT client, id FROM count_snapshots WHERE timestamp = %s', (now,))
		snapshots = dict(t.fetchall())
		# Push all the data in
		def truncate(data, limit):
			if data > 2**limit-1:
				logger.warn("Number %s overflow, truncating to %s", data, 2**limit-1)
				return 2**limit-1
			else:
				return data
		def clientdata(client):
			snapshot = snapshots[clients[client]]
			l = min(len(data[client]) / 2, len(name_order))
			return map(lambda name, index: (snapshot, names[name], truncate(data[client][index * 2], 63), truncate(data[client][index * 2 + 1], 63)), name_order[:l], range(0, l))
		def clientcaptures(client):
			snapshot = snapshots[clients[client]]
			return map(lambda i: (snapshot, i, truncate(stats[client][3 * i], 31), truncate(stats[client][3 * i + 1], 31), truncate(stats[client][3 * i + 2], 31)), range(0, len(stats[client]) / 3))
		def join_clients(c1, c2):
			c1.extend(c2)
			return c1
		t.executemany('INSERT INTO counts(snapshot, type, count, size) VALUES(%s, %s, %s, %s)', reduce(join_clients, map(clientdata, data.keys())))
		t.executemany('INSERT INTO capture_stats(snapshot, interface, captured, dropped, dropped_driver) VALUES(%s, %s, %s, %s, %s)', reduce(join_clients, map(clientcaptures, stats.keys())))