Best Python code snippet using avocado_python
spark.py
Source:spark.py
...50 def __init__(self, start):51 self.rules = {}52 self.rule2func = {}53 self.rule2name = {}54 self.collectRules()55 self.augment(start)56 self.ruleschanged = 157 _NULLABLE = '\e_'58 _START = 'START'59 _BOF = '|-'60 #61 # When pickling, take the time to generate the full state machine;62 # some information is then extraneous, too. Unfortunately we63 # can't save the rule2func map.64 #65 def __getstate__(self):66 if self.ruleschanged:67 #68 # XXX - duplicated from parse()69 #70 self.computeNull()71 self.newrules = {}72 self.new2old = {}73 self.makeNewRules()74 self.ruleschanged = 075 self.edges, self.cores = {}, {}76 self.states = { 0: self.makeState0() }77 self.makeState(0, self._BOF)78 #79 # XXX - should find a better way to do this..80 #81 changes = 182 while changes:83 changes = 084 for k, v in self.edges.items():85 if v is None:86 state, sym = k87 if self.states.has_key(state):88 self.goto(state, sym)89 changes = 190 rv = self.__dict__.copy()91 for s in self.states.values():92 del s.items93 del rv['rule2func']94 del rv['nullable']95 del rv['cores']96 return rv97 def __setstate__(self, D):98 self.rules = {}99 self.rule2func = {}100 self.rule2name = {}101 self.collectRules()102 start = D['rules'][self._START][0][1][1] # Blech.103 self.augment(start)104 D['rule2func'] = self.rule2func105 D['makeSet'] = self.makeSet_fast106 self.__dict__ = D107 #108 # A hook for GenericASTBuilder and GenericASTMatcher. Mess109 # thee not with this; nor shall thee toucheth the _preprocess110 # argument to addRule.111 #112 def preprocess(self, rule, func): return rule, func113 def addRule(self, doc, func, _preprocess=1):114 fn = func115 rules = doc.split()116 index = []117 for i in xrange(len(rules)):118 if rules[i] == '::=':119 index.append(i-1)120 index.append(len(rules))121 for i in xrange(len(index)-1):122 lhs = rules[index[i]]123 rhs = rules[index[i]+2:index[i+1]]124 rule = (lhs, tuple(rhs))125 if _preprocess:126 rule, fn = self.preprocess(rule, func)127 if self.rules.has_key(lhs):128 self.rules[lhs].append(rule)129 else:130 self.rules[lhs] = [ rule ]131 self.rule2func[rule] = fn132 self.rule2name[rule] = func.__name__[2:]133 self.ruleschanged = 1134 def collectRules(self):135 for name in _namelist(self):136 if name[:2] == 'p_':137 func = getattr(self, name)138 doc = func.__doc__139 self.addRule(doc, func)140 def augment(self, start):141 rule = '%s ::= %s %s' % (self._START, self._BOF, start)142 self.addRule(rule, lambda args: args[1], 0)143 def computeNull(self):144 self.nullable = {}145 tbd = []146 for rulelist in self.rules.values():147 lhs = rulelist[0][0]148 self.nullable[lhs] = 0149 for rule in rulelist:150 rhs = rule[1]151 if len(rhs) == 0:152 self.nullable[lhs] = 1153 continue154 #155 # We only need to consider rules which156 # consist entirely of nonterminal symbols.157 # This should be a savings on typical158 # grammars.159 #160 for sym in rhs:161 if not self.rules.has_key(sym):162 break163 else:164 tbd.append(rule)165 changes = 1166 while changes:167 changes = 0168 for lhs, rhs in tbd:169 if self.nullable[lhs]:170 continue171 for sym in rhs:172 if not self.nullable[sym]:173 break174 else:175 self.nullable[lhs] = 1176 changes = 1177 def makeState0(self):178 s0 = _State(0, [])179 for rule in self.newrules[self._START]:180 s0.items.append((rule, 0))181 return s0182 def finalState(self, tokens):183 #184 # Yuck.185 #186 if len(self.newrules[self._START]) == 2 and len(tokens) == 0:187 return 1188 start = self.rules[self._START][0][1][1]189 return self.goto(1, start)190 def makeNewRules(self):191 worklist = []192 for rulelist in self.rules.values():193 for rule in rulelist:194 worklist.append((rule, 0, 1, rule))195 for rule, i, candidate, oldrule in worklist:196 lhs, rhs = rule197 n = len(rhs)198 while i < n:199 sym = rhs[i]200 if not self.rules.has_key(sym) or \201 not self.nullable[sym]:202 candidate = 0203 i = i + 1204 continue205 newrhs = list(rhs)206 newrhs[i] = self._NULLABLE+sym207 newrule = (lhs, tuple(newrhs))208 worklist.append((newrule, i+1,209 candidate, oldrule))210 candidate = 0211 i = i + 1212 else:213 if candidate:214 lhs = self._NULLABLE+lhs215 rule = (lhs, rhs)216 if self.newrules.has_key(lhs):217 self.newrules[lhs].append(rule)218 else:219 self.newrules[lhs] = [ rule ]220 self.new2old[rule] = oldrule221 def typestring(self, token):222 return None223 def error(self, token):224 print "Syntax error at or near `%s' token" % token225 raise SystemExit226 def parse(self, tokens):227 sets = [ [(1,0), (2,0)] ]228 self.links = {}229 if self.ruleschanged:230 self.computeNull()231 self.newrules = {}232 self.new2old = {}233 self.makeNewRules()234 self.ruleschanged = 0235 self.edges, self.cores = {}, {}236 self.states = { 0: self.makeState0() }237 self.makeState(0, self._BOF)238 for i in xrange(len(tokens)):239 sets.append([])240 if sets[i] == []:241 break 242 self.makeSet(tokens[i], sets, i)243 else:244 sets.append([])245 self.makeSet(None, sets, len(tokens))246 finalitem = (self.finalState(tokens), 0)247 if finalitem not in sets[-2]:248 if len(tokens) > 0:249 self.error(tokens[i-1])250 else:251 self.error(None)252 return self.buildTree(self._START, finalitem,253 tokens, len(sets)-2)254 def isnullable(self, sym):255 #256 # For symbols in G_e only. If we weren't supporting 1.5,257 # could just use sym.startswith().258 #259 return self._NULLABLE == sym[0:len(self._NULLABLE)]260 def skip(self, (lhs, rhs), pos=0):261 n = len(rhs)262 while pos < n:263 if not self.isnullable(rhs[pos]):264 break265 pos = pos + 1266 return pos267 def makeState(self, state, sym):268 assert sym is not None269 #270 # Compute \epsilon-kernel state's core and see if271 # it exists already.272 #273 kitems = []274 for rule, pos in self.states[state].items:275 lhs, rhs = rule276 if rhs[pos:pos+1] == (sym,):277 kitems.append((rule, self.skip(rule, pos+1)))278 tcore = tuple(sorted(kitems))279 if self.cores.has_key(tcore):280 return self.cores[tcore]281 #282 # Nope, doesn't exist. Compute it and the associated283 # \epsilon-nonkernel state together; we'll need it right away.284 #285 k = self.cores[tcore] = len(self.states)286 K, NK = _State(k, kitems), _State(k+1, [])287 self.states[k] = K288 predicted = {}289 edges = self.edges290 rules = self.newrules291 for X in K, NK:292 worklist = X.items293 for item in worklist:294 rule, pos = item295 lhs, rhs = rule296 if pos == len(rhs):297 X.complete.append(rule)298 continue299 nextSym = rhs[pos]300 key = (X.stateno, nextSym)301 if not rules.has_key(nextSym):302 if not edges.has_key(key):303 edges[key] = None304 X.T.append(nextSym)305 else:306 edges[key] = None307 if not predicted.has_key(nextSym):308 predicted[nextSym] = 1309 for prule in rules[nextSym]:310 ppos = self.skip(prule)311 new = (prule, ppos)312 NK.items.append(new)313 #314 # Problem: we know K needs generating, but we315 # don't yet know about NK. Can't commit anything316 # regarding NK to self.edges until we're sure. Should317 # we delay committing on both K and NK to avoid this318 # hacky code? This creates other problems..319 #320 if X is K:321 edges = {}322 if NK.items == []:323 return k324 #325 # Check for \epsilon-nonkernel's core. Unfortunately we326 # need to know the entire set of predicted nonterminals327 # to do this without accidentally duplicating states.328 #329 tcore = tuple(sorted(predicted.keys()))330 if self.cores.has_key(tcore):331 self.edges[(k, None)] = self.cores[tcore]332 return k333 nk = self.cores[tcore] = self.edges[(k, None)] = NK.stateno334 self.edges.update(edges)335 self.states[nk] = NK336 return k337 def goto(self, state, sym):338 key = (state, sym)339 if not self.edges.has_key(key):340 #341 # No transitions from state on sym.342 #343 return None344 rv = self.edges[key]345 if rv is None:346 #347 # Target state isn't generated yet. Remedy this.348 #349 rv = self.makeState(state, sym)350 self.edges[key] = rv351 return rv352 def gotoT(self, state, t):353 return [self.goto(state, t)]354 def gotoST(self, state, st):355 rv = []356 for t in self.states[state].T:357 if st == t:358 rv.append(self.goto(state, t))359 return rv360 def add(self, set, item, i=None, predecessor=None, causal=None):361 if predecessor is None:362 if item not in set:363 set.append(item)364 else:365 key = (item, i)366 if item not in set:367 self.links[key] = []368 set.append(item)369 self.links[key].append((predecessor, causal))370 def makeSet(self, token, sets, i):371 cur, next = sets[i], sets[i+1]372 ttype = token is not None and self.typestring(token) or None373 if ttype is not None:374 fn, arg = self.gotoT, ttype375 else:376 fn, arg = self.gotoST, token377 for item in cur:378 ptr = (item, i)379 state, parent = item380 add = fn(state, arg)381 for k in add:382 if k is not None:383 self.add(next, (k, parent), i+1, ptr)384 nk = self.goto(k, None)385 if nk is not None:386 self.add(next, (nk, i+1))387 if parent == i:388 continue389 for rule in self.states[state].complete:390 lhs, rhs = rule391 for pitem in sets[parent]:392 pstate, pparent = pitem393 k = self.goto(pstate, lhs)394 if k is not None:395 why = (item, i, rule)396 pptr = (pitem, parent)397 self.add(cur, (k, pparent),398 i, pptr, why)399 nk = self.goto(k, None)400 if nk is not None:401 self.add(cur, (nk, i))402 def makeSet_fast(self, token, sets, i):403 #404 # Call *only* when the entire state machine has been built!405 # It relies on self.edges being filled in completely, and406 # then duplicates and inlines code to boost speed at the407 # cost of extreme ugliness.408 #409 cur, next = sets[i], sets[i+1]410 ttype = token is not None and self.typestring(token) or None411 for item in cur:412 ptr = (item, i)413 state, parent = item414 if ttype is not None:415 k = self.edges.get((state, ttype), None)416 if k is not None:417 #self.add(next, (k, parent), i+1, ptr)418 #INLINED --v419 new = (k, parent)420 key = (new, i+1)421 if new not in next:422 self.links[key] = []423 next.append(new)424 self.links[key].append((ptr, None))425 #INLINED --^426 #nk = self.goto(k, None)427 nk = self.edges.get((k, None), None)428 if nk is not None:429 #self.add(next, (nk, i+1))430 #INLINED --v431 new = (nk, i+1)432 if new not in next:433 next.append(new)434 #INLINED --^435 else:436 add = self.gotoST(state, token)437 for k in add:438 if k is not None:439 self.add(next, (k, parent), i+1, ptr)440 #nk = self.goto(k, None)441 nk = self.edges.get((k, None), None)442 if nk is not None:443 self.add(next, (nk, i+1))444 if parent == i:445 continue446 for rule in self.states[state].complete:447 lhs, rhs = rule448 for pitem in sets[parent]:449 pstate, pparent = pitem450 #k = self.goto(pstate, lhs)451 k = self.edges.get((pstate, lhs), None)452 if k is not None:453 why = (item, i, rule)454 pptr = (pitem, parent)455 #self.add(cur, (k, pparent),456 # i, pptr, why)457 #INLINED --v458 new = (k, pparent)459 key = (new, i)460 if new not in cur:461 self.links[key] = []462 cur.append(new)463 self.links[key].append((pptr, why))464 #INLINED --^465 #nk = self.goto(k, None)466 nk = self.edges.get((k, None), None)467 if nk is not None:468 #self.add(cur, (nk, i))469 #INLINED --v470 new = (nk, i)471 if new not in cur:472 cur.append(new)473 #INLINED --^474 def predecessor(self, key, causal):475 for p, c in self.links[key]:476 if c == causal:477 return p478 assert 0479 def causal(self, key):480 links = self.links[key]481 if len(links) == 1:482 return links[0][1]483 choices = []484 rule2cause = {}485 for p, c in links:486 rule = c[2]487 choices.append(rule)488 rule2cause[rule] = c489 return rule2cause[self.ambiguity(choices)]490 def deriveEpsilon(self, nt):491 if len(self.newrules[nt]) > 1:492 rule = self.ambiguity(self.newrules[nt])493 else:494 rule = self.newrules[nt][0]495 #print rule496 rhs = rule[1]497 attr = [None] * len(rhs)498 for i in xrange(len(rhs)-1, -1, -1):499 attr[i] = self.deriveEpsilon(rhs[i])500 return self.rule2func[self.new2old[rule]](attr)501 def buildTree(self, nt, item, tokens, k):502 state, parent = item503 choices = []504 for rule in self.states[state].complete:505 if rule[0] == nt:506 choices.append(rule)507 rule = choices[0]508 if len(choices) > 1:509 rule = self.ambiguity(choices)510 #print rule511 rhs = rule[1]512 attr = [None] * len(rhs)513 for i in xrange(len(rhs)-1, -1, -1):514 sym = rhs[i]515 if not self.newrules.has_key(sym):516 if sym != self._BOF:517 attr[i] = tokens[k-1]518 key = (item, k)519 item, k = self.predecessor(key, None)520 #elif self.isnullable(sym):521 elif self._NULLABLE == sym[0:len(self._NULLABLE)]:522 attr[i] = self.deriveEpsilon(sym)523 else:524 key = (item, k)525 why = self.causal(key)526 attr[i] = self.buildTree(sym, why[0],527 tokens, why[1])528 item, k = self.predecessor(key, why)529 return self.rule2func[self.new2old[rule]](attr)530 def ambiguity(self, rules):531 #532 # XXX - problem here and in collectRules() if the same rule533 # appears in >1 method. Also undefined results if rules534 # causing the ambiguity appear in the same method.535 #536 537 sortlist = []538 name2index = {}539 for i in xrange(len(rules)):540 lhs, rhs = rule = rules[i]541 name = self.rule2name[self.new2old[rule]]542 sortlist.append((len(rhs), name))543 name2index[name] = i544 sortlist.sort()545 list = map(lambda (a,b): b, sortlist)546 return rules[name2index[self.resolve(list)]]...
adhoc
Source:adhoc
...155 return premises156 elif type(provenance) is list:157 return { premise for elem in provenance for premise in collectIntermediateConclusions(elem) }158 else: return set()159 def collectRules(provenance):160 if type(provenance) is dict:161 rules = { rule for key, value in provenance.items() for rule in collectRules(value) }162 if 'axiom' in provenance and provenance['axiom'].startswith('Rule'):163 rules = rules | { provenance['axiom'][len('Rule') + 1:-1] }164 return rules165 elif type(provenance) is list:166 rules = { rule for elem in provenance for rule in collectRules(elem) }167 return rules168 else: return set()169 def getProvenance(relName, t):170 provenance1 = execSouffleCmd('explain ' + printSouffleString(relName, t))171 provenance2 = json.loads(provenance1)172 provenance3 = cleanProvenanceJson(provenance2)173 provenance4 = squashDesirableConclusions(provenance3)174 175 # if provenance3 != provenance4:176 # logging.info(provenance3)177 # logging.info(provenance4)178 return provenance4179 def why(relName, t):180 provenance = getProvenance(relName, t)181 #for hypRelName, hypT in collectIntermediateConclusions(provenance):182 # if hypRelName in idbRelationsExpected and hypT not in idbRelationsExpected[hypRelName]:183 # logging.debug('Tuple {} depends on absurd hypothesis {}'.format((relName, t), (hypRelName, hypT)))184 # return None185 rules = collectRules(provenance)186 #print(rules)187 outputStr = "WHY constraint added: [not ("188 for ruleName in rules:189 outputStr += str(ruleName) + " and "190 outputStr = outputStr[:len(outputStr)-5]191 outputStr += ")] because "192 outputStr += str(t) + " is undesirable and produced"193 print(outputStr)194 #logging.info('Tuple {} depends on rules {}'.format((relName, t), sorted(rules)))195 ans = z3.BoolVal(True)196 for ruleName in rules: ans = z3.And(ans, allRuleNames[ruleName])197 return ans198 def whyNot():199 ans = z3.BoolVal(False)200 outputStr = "WHYNOT constraint added: ["201 for ruleName in allRuleNames:202 if ruleName not in currRuleSetLarge:203 ans = z3.Or(ans, allRuleNames[ruleName])204 outputStr += str(ruleName) + " or "205 outputStr = outputStr[:len(outputStr)-4]206 outputStr += "] because "207 outputStr += str(t) + " is desirable and unproduced"208 print(outputStr)209 return ans210 ################################################################################################################211 # 3c. Iterate over all tuples212 for relName in idbRelationsExpected:213 #logging.info(relName)214 relProduced = loadRelation(problemDirName + '/' + relName + '.csv')215 # 3c(i) Tuples which are expected and produced216 # We do not have anything to learn from these tuples217 #for t in relProduced & idbRelationsExpected[relName]:218 #logging.info('Solution produces desirable tuple: ' + printSouffleString(relName, t))219 # 3c(ii) Tuples which are unexpected but still produced220 # Here, we ask the question: ``Why was this tuple produced?''221 222 for t in relProduced - idbRelationsExpected[relName]:223 #logging.info('Solution produces undesirable tuple ' + printSouffleString(relName, t))224 solved = False225 whyRelT = why(relName, t)226 if whyRelT != None:227 solver.add(z3.Not(whyRelT))228 numWhy+=1229 doSanity()230 # 3c(iii) Tuples which were expected but not produced231 # Here, we ask the question: ``Why was this tuple not produced?''232 unproducedDesirable = False233 for t in idbRelationsExpected[relName] - relProduced:234 #logging.info('Solution fails to produce desirable tuple ' + printSouffleString(relName, t))235 solved = False236 unproducedDesirable = True237 if unproducedDesirable:238 whyNotRelT = whyNot()239 if whyNotRelT != None:240 solver.add(whyNotRelT)241 numWhyNot+=1242 doSanity()243 244 # pass245 ################################################################################################################246 # 3d. Find useful rules247 if solved:248 ans = { ruleName for relName in idbRelationsExpected \249 for t in idbRelationsExpected[relName] \250 for ruleName in collectRules(getProvenance(relName, t)) }251 print(ans)...
rule_manager.py
Source:rule_manager.py
1import subprocess as sp2import re3def main():4 lstAps = ['ap_h0', 'ap_d0', 'ap_v0']5 hshAps = collectRules(lstAps)6 for strAp in hshAps:7 print('------------------list for ap=%s\n%s' % (strAp, str(hshAps[strAp])))8 applyRules(hshAps)9 # Rule Collector10 # Run for at least about 20 seconds11 # Once past that threshold, keep running while more rules are still being collected for any APs12 # New rules must be compared with the old ones in case they are just being reordered in the list13 # When rules stabilize for a certain time window, save everything into a file that can be read before the experiment starts 14def collectRules(lstAps):15 """16 Collects forwarding rules from all APs given and returns them in the form of a 17 dictionary with all the APs names and lists containing each rule.18 """19 hshNodes = {}20 for pAp in lstAps:21 strAp = str(pAp)22 hshNodes[strAp] = list()23 proc = sp.Popen(['ovs-ofctl', 'dump-flows', strAp], stdout=sp.PIPE)24 # Extract only stdout from index 025 strOut = proc.communicate()[0]26 # Remove whitespace characters27 rules = strOut.replace(' ', '')28 # Extract each line as a separate rule...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!