def splitTerm(term: str) -> List[str]: splittedCamel = splitCamelCase(term) splittedUnderscore = flatten([splitUnderscore(term) for term in splittedCamel]) splittedDot = flatten([splitDot(term) for term in splittedUnderscore]) # could be done more efficiently, but probably not worth the effort if len(splittedDot) > 1: return [splitTerm(term) for term in splittedDot] return term
def getValidatedSimilarRepository(repositoryName): httpList = [ 'httputil', 'http-client', 'async-http-client', 'http-request', 'okhttp', 'netty-http-client', 'google-http-java-client' ] tictacToeList = [ 'ticTacToe', 'TicTacToe-MVVM', 'ticTacToe2', 'Tic-Tac-Toe', 'ticTacToe3', 'tic-tac-toe-android-app' ] calendarList = [ 'EasyCalendar', 'android-calendar-card', 'Material-Calendar-View', 'calendar', 'Etar-Calendar', 'CosmoCalendar', 'CalendarListview' ] oauthList = [ 'signpost', 'scribejava', 'spring-security-oauth', 'apis', 'oauth2-shiro', 'oauth2-server' ] dataBaseDriver = [ 'sqlite-jdbc', 'neo4j-java-driver', 'arangodb-java-driver', 'snowflake-jdbc', 'mssql-jdbc' ] return flatten([ subList for subList in [httpList, tictacToeList, calendarList, oauthList, dataBaseDriver] if repositoryName in subList ])
def preProcessSingle(termList: List[str]) -> List[Any]: splitTerms = flatten([splitTerm(term) for term in termList]) cleanedTerms = removeStopWords(splitTerms) lowerTerms = toLower(cleanedTerms) cleanedTerms = removeStopWords(lowerTerms) stemmedTerms = stem(cleanedTerms) return stemmedTerms
def build_message(self, message_struct, params): """Return a packed representation of the given params mapped onto the given message struct.""" if not len(params) == len(message_struct): print "[!] Error building message: wrong number of parameters" print "[!] Dumping values" print "[!] message_struct (%d): %s" % (len(message_struct), message_struct) print "[!] params (%d): %s" % (len(params), params) sys.exit(1) message = tuple() format = '>' for member in message_struct: message += (params[member['name']],) if member.has_key('size'): if member['size'] == 'dlen': if member.has_key('offset'): format += str(params[member['size']] - member['offset']) \ + member['type'] else: format += str(params[member['size']]) + member['type'] else: format += str(member['size']) + member['type'] else: format += member['type'] message = tuple(flatten(message)) return self.pack(format, message)
def offspringAsList(self, REQUEST=None): """ Return my offsprings' page names as a flat list, excluding my name. """ list = flatten(self.offspringNesting()) list.remove(self.pageName()) return list
def build_message(self, message_struct, params): """Return a packed representation of the given params mapped onto the given message struct.""" if not len(params) == len(message_struct): print "[!] Error building message: wrong number of parameters" print "[!] Dumping values" print "[!] message_struct (%d): %s" % (len(message_struct), message_struct) print "[!] params (%d): %s" % (len(params), params) sys.exit(1) message = tuple() format = '>' for member in message_struct: message += (params[member['name']], ) if member.has_key('size'): if member['size'] == 'dlen': if member.has_key('offset'): format += str(params[member['size']] - member['offset']) \ + member['type'] else: format += str(params[member['size']]) + member['type'] else: format += str(member['size']) + member['type'] else: format += member['type'] message = tuple(flatten(message)) return self.pack(format, message)
def ancestorsAsList(self, REQUEST=None): """ Return the names of all my ancestor pages as a flat list, eldest first. If there are multiple lines of ancestry, return only the first. """ try: return flatten(self.ancestorsNesting())[:-1] except: return [] # XXX temp, get rid of
def modelTopics(featureLists: List[List[List[str]]], topicMin = 2, topicLimit = 256): # combine all terms from a single repository into one document/ term list in order to compare the repositories later easily combinedFeatureLists = [flatten(featureList) for featureList in featureLists] processedFeatures = preProcess(combinedFeatureLists) dictionary, corpus = buildCorpus(processedFeatures) topicCounts = [1 << exponent for exponent in range(int(math.log(topicMin, 2)), int(math.log(topicLimit, 2)) + 1)] topicModels, alphas, betas = generateTopicModels(dictionary, corpus, topicCounts) return topicModels, dictionary, corpus, processedFeatures, topicCounts, alphas, betas
def modal_effects(**keys): controller = keys['controller'] source = keys['source'] selected = controller.make_selection([(mode.__doc__,mode) for mode in modes], choose, prompt='Select %d mode(s):'%choose) if choose > 1: chosen = tuple((robustApply(mode, **keys) for mode in selected)) else: chosen = (robustApply(selected, **keys), ) # get the targets targets, unflatten = flatten(mode.next() for mode in chosen) targets = yield targets if not isinstance(targets, tuple): targets = (targets,) for t, mode in zip(unflatten(targets), chosen): yield mode.send(t) for res in mode: yield res
def modal_effects(controller, source): selected = controller.make_selection([(mode.__doc__,mode) for mode in modes], choose, prompt='Select %d mode(s):'%choose) if choose > 1: chosen = tuple((mode(controller, source) for mode in selected)) else: chosen = (selected(controller, source), ) # get the costs # We need to have a "payment = yield NoCost" in the mode to pass # back the cost in case the mode needs to reference is (see Profane Command) empty_costs = tuple((mode.next() for mode in chosen)) payment = yield effects(controller, source).next() # get the targets - demultiplex them targets, unflatten = flatten(mode.send(payment) for mode in chosen) targets = yield targets if not isinstance(targets, tuple): targets = (targets,) for t, mode in zip(unflatten(targets), chosen): yield mode.send(t) for res in mode: yield res
def get_ip_list(self): ip_source = self.params.config['blacklist']['ip'] res = [] for url in ip_source: r = requests.get(url) if r.status_code == 200: ips = Utils.extract_ips(r.text) if ips: res.append(ips) else: pass # throw exception # print 'Could not download source: ' + url # sorted unique list is returned return sorted(unique(flatten(res)))
def modal_effects(**keys): controller = keys['controller'] source = keys['source'] selected = controller.make_selection( [(mode.__doc__, mode) for mode in modes], choose, prompt='Select %d mode(s):' % choose) if choose > 1: chosen = tuple((robustApply(mode, **keys) for mode in selected)) else: chosen = (robustApply(selected, **keys), ) # get the targets targets, unflatten = flatten(mode.next() for mode in chosen) targets = yield targets if not isinstance(targets, tuple): targets = (targets, ) for t, mode in zip(unflatten(targets), chosen): yield mode.send(t) for res in mode: yield res
def effects(controller, source): selected = controller.make_selection( [(mode.__doc__, mode) for mode in modes], choose, prompt='Select %d mode(s):' % choose) if choose > 1: chosen = tuple((mode(controller, source) for mode in selected)) else: chosen = (selected(controller, source), ) # get the costs # We need to have a "payment = yield NoCost" in the mode to pass # back the cost in case the mode needs to reference is (see Profane Command) empty_costs = tuple((mode.next() for mode in chosen)) payment = yield source.cost # get the targets - demultiplex them targets, unflatten = flatten(mode.send(payment) for mode in chosen) targets = yield targets if not isinstance(targets, tuple): targets = (targets, ) for t, mode in zip(unflatten(targets), chosen): yield mode.send(t) for res in mode: yield res
def generateQueriesAndTrueSel(cur,query,cols,queries,workload="uniform",maxrnum=1): first = True pcols = generateInvariantPointColumns(query) pcolsf = [ (query.tables[i].tid,query.tables[i].columns[j].cid) for i in range(len(pcols)) for j in pcols[i] ] pattern = " " first = True if len(pcolsf) > 0: for tid,cid in pcolsf: if first: pattern +=" t%s_c%s = %%s " % (tid,cid) first = False else: pattern +=" and t%s_c%s = %%s " % (tid,cid) #cur.execute("select * from t;") #histogram = np.array(cur.fetchall()) #Place histogram lock here. #if workload == "distinct": # freqs = histogram[:,-1]/float(np.sum(histogram[:,-1])) #elif workload == "uniform": # freqs = np.ones(histogram[:,-1].shape)/len(histogram[:,-1]) #else: # raise Exception("This workload does not exist.") rcols = generateInvariantRangeColumns(query) rcolsf = [ (query.tables[i].tid,query.tables[i].columns[j].cid) for i in range(len(rcols)) for j in rcols[i] ] nrcols = len(rcolsf) minpattern = " " maxpattern = " " if len(rcolsf) > 0: for tid,cid in rcolsf: if first: minpattern +=" t%s_c%s >= %%s " % (tid,cid) first = False else: minpattern +=" and t%s_c%s >= %%s " % (tid,cid) maxpattern +=" and t%s_c%s <= %%s " % (tid,cid) rejected = 0 pvals = [] uvals = [] lvals = [] cards = [] minmaxq_prev = None minv = None maxv = None card = None while len(cards) < queries: #We start by drawing a random cluster center. Right now, we let it if True: #vals = histogram[np.random.choice(len(freqs),p=freqs)] if workload == "distinct": rnum = np.random.randint(1,maxrnum+1) cur.execute("select * from t where rnum = %s limit 1" % rnum) elif workload == "uniform": #This is the point version randf = np.random.rand() cur.execute("select * from t where %s >= rnum-cnt and %s < rnum limit 1" % (maxrnum*randf,maxrnum*randf)) else: raise Exception("unkown workload type.") vals = np.array(cur.fetchone(),np.int32)[1:] if len(rcolsf) == 0: pvals.append(vals[:-1]) cards.append(vals[-1]) else: if not np.all(vals[len(flatten(pcols)):-1] != 0): continue if len(pcolsf) > 0: ppattern = pattern % tuple(vals[:-1-len(flatten(rcols))]) else: ppattern = "" #We need to have a binary search here to get the range queries runing. #Step 1: Find min and max values. minmaxq = generateMinMaxQueryString(cur,query) if len(pcolsf) > 0: minmaxq += " WHERE %s" % ppattern else: minmaxq += " %s" % ppattern if minmaxq_prev != minmaxq: minmaxq_prev = minmaxq cur.execute(minmaxq) minmax = np.array(cur.fetchone()) minv = np.array([minmax[i*2] for i in range(nrcols)]) maxv = np.array([minmax[i*2+1] for i in range(nrcols)]) card = vals[-1] center = vals[len(flatten(pcols)):-1] upper_range = maxv-center lower_range = center-minv #Unfortunately, we have to do the binary search. We draw a random point from the lower_factor = 0.0 lower_card = 0 upper_factor = 1.0 upper_card = card minscale = np.random.random_sample(lower_range.shape) maxscale = np.random.random_sample(upper_range.shape) join_query_string = generateJoinQueryString(cur,query) join_query_string += (" %s" % ppattern) try_upper = np.rint(center + maxscale*upper_range) try_lower = np.rint(center - minscale*lower_range) join_query_string += minpattern % tuple(try_lower) join_query_string += maxpattern % tuple(try_upper) cur.execute(join_query_string) card = cur.fetchone()[0] pvals.append(list(vals[:-1-len(flatten(rcols))])) lvals.append(list(try_lower)) uvals.append(list(try_upper)) cards.append(card) return pvals, lvals, uvals, cards
def run_gambit(strategies: List[Strategy], table: List[List[float]]) -> None: """Run the gambit enum mixed command line tool and pretty print the result. :param strategies: List of the strategies used to create the table. :param table: Grand Table scores as a 2D matrix. To understand how gambit-enummixed should be used, here is an example: Let's say we have a table that looks like this. |A|B| A |1|2| B |3|4| gambit-enummixed wants this table in NFG, Normal Form Game, in this case that would be a file structured in the following way: ' NFG 1 R "" { "1" "2" } { 2 2 } 1 1 3 2 2 3 4 4 ' The first line means: NFG: Normal Form Game 1: normal form version 1, only version that is supported R: use rational numbers, obsolete distinction so every file should have an R here. "": The name of the game, irrelevant and thus empty. { "1" "2" }: The names of the players, we want to have 2 players since it is a 2D table. { 2 2 }: The number of actions (strategies in our case) for each player. The other lines are the table in normal form. First the score of A against A (and the opposing score A against A), then the score of B against A (and the opposing score A against B), then the score of A against B (and the opposing score B against A), finally the score of B against B (and the opposing score B against B). The trick is that it first increments the action of player 1, and every time it rolls over to the first action, it increments the action of player 2. Since newlines don't matter to the tool, we can structure the input like this: gambit_input = 'NFG 1 R "" { "1" "2" } { 2 2 } 1 1 3 2 2 3 4 4' with some escape characters before the quotation marks. Now we want to call gambit-enummixed with this input but it only accepts files. So we used the 'echo' command and the pipe operator '|', to pass it the input as if it where a file. We also want to suppress any unnecessary output and round to 2 decimal places. This is accomplished by adding the '-q' and '-d2' flags. The final command looks like this: echo 'NFG 1 R "" { "1" "2" } { 2 2 } 1 1 3 2 2 3 4 4' | ./gambit-enummixed -q -d2 If you run it, the output will be NE,0.00,1.00,0.00,1.00 This means that there is a Nash equilibrium (NE), if both players play their first action with a probability of 0, and their second action with a probability of 1 This is better visible if you reformat the output and add action names: A: 0.00, 0.00 B: 1.00, 1.00 So their is a Nash equilibrium when both players always play action B, given the table this is no surprise. Note: On Windows this command only works in Powershell as it uses some POSIX notation Also in the output format 0.00 is replaced with ---- """ # Determine padding for pretty printing output. padding = max(map(lambda s: len(s.name), strategies)) + 1 name_format = '{:>' + padding.__repr__() + '}' num_format = '{:^6}' linesize = padding + 14 hline = ("=" * linesize) + "|" # Make a call to gambit nr_of_strats = len(strategies) gambit_input = 'NFG 1 R "" { "1" "2" } { ' + \ nr_of_strats.__repr__() + " " + nr_of_strats.__repr__() + " } " + \ " ".join(map(repr, flatten(zip(flatten(transpose(table)), flatten(table))))) # Only execute the command if gambit-enummixed exists as an executable, # either in the same folder or added to the PATH variable. # Current directory is added to the path variable for non Windows operating systems. if shutil.which("gambit-enummixed") is not None: command = "echo '" + gambit_input + "' | " + shutil.which( "gambit-enummixed", path="./:$PATH") + " -q -d2" result = execute_command(command) else: raise Exception("gambit-enummixed executable not found.") for line in result.splitlines(): line = line.__repr__() line = line.strip("'") line = line.replace('0.00', '----') values = line.split(sep=",") print(hline) for i, strategy in enumerate(strategies): name = name_format.format(strategy.name) x = num_format.format(values[i + 1]) y = num_format.format(values[i + 1 + nr_of_strats]) print(name + ":" + x + "|" + y + "|") print(hline)
def add(self, win): for w in flatten(win): self._windows.append(w) w.container = self w.wrapper.prod()
def add(self, buttons): for btn in flatten(buttons): if btn.group != self: self._items.append(btn) btn.group = self