Best Python code snippet using playwright-python
core_z3.py
Source:core_z3.py
...9import pandas as pd10# Global variables to be used later on.11var = {} # to store the variables of the csp12s = None13def set_csp(pos_x, neg_x, n, k):14 """Creates the variables and the constraints for a given number of nodes."""15 # Create a dictionary to store all the variables16 global var17 var = {}18 def get_lr(i):19 """20 Given a node i, returns all its possible left children.21 LR(i) = even([i + 1, min(2i, n â 1)]), i = 1,...,n22 """23 return tuple([_ for _ in range(i + 1, min(2 * i, n - 1) + 1) if _ % 2 == 0])24 def get_rr(i):25 """26 Given a node i, returns all its possible right children.27 RR(i) = odd([i + 2, min(2i + 1, n )]), i=1,...,n28 """29 return tuple([_ for _ in range(i + 2, min(2 * i + 1, n) + 1) if _ % 2 != 0])30 # Create the variables.31 # Create a variable 'v' for each node in the tree32 # It is True iff node i is a leaf node33 for i in range(1, n + 1):34 var['v%i' % i] = Bool('v%i' % i)35 # Create a variable 'l' for each possible left child of current node36 # It is True iff node i has node j as left child37 for i in range(1, n + 1):38 for j in get_lr(i):39 var['l%i,%i' % (i, j)] = Bool('l%i,%i' % (i, j))40 # Create a variable 'r' for each possible right child of current node41 # It is True iff node i has node j as right child42 for i in range(1, n + 1):43 for j in get_rr(i):44 var['r%i,%i' % (i, j)] = Bool('r%i,%i' % (i, j))45 # Create a variable 'p' to tell the parent of current node46 # It is True iff the parent of node j is node i47 for i in range(1, n):48 for j in range(i + 1, min(2 * i + 1, n) + 1):49 var['p%i,%i' % (j, i)] = Bool('p%i,%i' % (j, i))50 # Create a variable 'a' for each couple (node, feature)51 # It is True iff feature r is assigned to node j52 for i in range(1, k + 1):53 for j in range(1, n + 1):54 var['a%i,%i' % (i, j)] = Bool('a%i,%i' % (i, j))55 # Create a variable 'u' for each couple (node, feature)56 # It is True iff feature r is being discriminated against by node j57 for i in range(1, k + 1):58 for j in range(1, n + 1):59 var['u%i,%i' % (i, j)] = Bool('u%i,%i' % (i, j))60 # Create a variable 'd0' for each couple (node, feature)61 # It is True iff feature r is discriminated for value 0 by node j62 # or by one of its ancestors63 for i in range(1, k + 1):64 for j in range(1, n + 1):65 var['d0%i,%i' % (i, j)] = Bool('d0%i,%i' % (i, j))66 # Create a variable 'd1' for each couple (node, feature)67 # It is True iff feature r is discriminated for value 1 by node j68 # or by one of its ancestors69 for i in range(1, k + 1):70 for j in range(1, n + 1):71 var['d1%i,%i' % (i, j)] = Bool('d1%i,%i' % (i, j))72 # Create a variable 'c' for each node.73 # It is True iff class of leaf node j is 174 for j in range(1, n + 1):75 var['c%i' % j] = Bool('c%i' % j)76 # Constraints.77 global s78 s = Solver()79 # enable multithreading80 set_param('parallel.enable', True)81 # TREE BUILDING CONSTRAINTS82 # These constraints allow to represent a full binary tree, that is a tree83 # in which each node has exactly 0 or 2 children. Since the tree cannot be84 # made up by the root alone, the number of nodes 'n' must be an odd number >= 3.85 # Given n, the constraints permit to find all the possible full binary trees86 # which can be built with n nodes. The number of legit trees with n nodes87 # is given by the Catalan numbers sequence.88 # Constraint 1: the root node is not a leaf.89 # NOT v190 s.add(Not(var['v1']))91 # Constraint 2: if a node is a leaf node, then it has no children.92 # vi -> NOT li,j, j in LR(i)93 for i in range(1, n + 1):94 for j in get_lr(i):95 s.add(Implies(var['v%i' % i], Not(var['l%i,%i' % (i, j)])))96 # Constraint 3: the left child and the right child of the i-th node97 # are numbered consecutively.98 # li,j <-> ri,j+1, j in LR(i)99 for i in range(1, n + 1):100 for j in get_lr(i):101 s.add(Implies(var['l%i,%i' % (i, j)], var['r%i,%i' % (i, j + 1)]))102 s.add(Implies(var['r%i,%i' % (i, j + 1)], var['l%i,%i' % (i, j)]))103 # Constraint 4: a non-leaf node must have a child.104 # NOT vi -> (SUM for j in LR(i) of li,j = 1)105 for i in range(1, n + 1):106 sum_list = []107 for j in get_lr(i):108 sum_list.append(var['l%i,%i' % (i, j)])109 exp = PbEq([(x, 1) for x in sum_list], 1)110 s.add(Implies(Not(var['v%i' % i]), exp))111 # Constraint 5: if the i-th node is a parent then it must have a child112 # pj,i <-> li,j, j in LR(i)113 # pj,i <-> ri,j, j in RR(i)114 for i in range(1, n + 1):115 for j in get_lr(i):116 s.add(Implies(var['p%i,%i' % (j, i)], var['l%i,%i' % (i, j)]))117 s.add(Implies(var['l%i,%i' % (i, j)], var['p%i,%i' % (j, i)]))118 for j in get_rr(i):119 s.add(Implies(var['p%i,%i' % (j, i)], var['r%i,%i' % (i, j)]))120 s.add(Implies(var['r%i,%i' % (i, j)], var['p%i,%i' % (j, i)]))121 # Constraint 6: all the nodes but the first must have a parent.122 # (SUM for i=floor(j/2), min(j-1, N) of pj,i = 1), j =2,...,n123 for j in range(2, n + 1):124 sum_list = []125 for i in range(floor(j / 2), min(j - 1, n) + 1):126 sum_list.append(var['p%i,%i' % (j, i)])127 exp = PbEq([(x, 1) for x in sum_list], 1)128 s.add(exp)129 '''130 # Constraint 6.1: each left/right child must have exactly a parent131 for j in range(2, n + 1):132 left_list = []133 right_list = []134 for i in range(1, n):135 if 'l%i,%i' % (i, j) in var:136 left_list.append(var['l%i,%i' % (i, j)])137 if 'r%i,%i' % (i, j) in var:138 right_list.append(var['r%i,%i' % (i, j)])139 if len(left_list) > 0:140 s.add(PbLe([(x, 1) for x in left_list], 1))141 if len(right_list) > 0:142 s.add(PbLe([(x, 1) for x in right_list], 1))143 '''144 # '''145 # Constraint 6.2: nodes on the same level must be labeled increasingly146 # li,j -> lh,(j-2), and ri,j -> rh,(j-2), h < i147 for i in range(n - 2, 0, -1):148 for j in reversed(get_lr(i)):149 if 'l%i,%i' % (i, j) in var:150 node_list = []151 for h in range(i - 1, 0, -1):152 if 'l%i,%i' % (h, j - 2) in var:153 node_list.append(var['l%i,%i' % (h, j - 2)])154 if len(node_list) > 0:155 s.add(Implies(var['l%i,%i' % (i, j)], PbGe([(x, 1) for x in node_list], 1)))156 for j in reversed(get_rr(i)):157 if 'r%i,%i' % (i, j) in var:158 node_list = []159 for h in range(i - 1, 0, -1):160 if 'r%i,%i' % (h, j - 2) in var:161 node_list.append(var['r%i,%i' % (h, j - 2)])162 if len(node_list) > 0:163 s.add(Implies(var['r%i,%i' % (i, j)], PbGe([(x, 1) for x in node_list], 1)))164 # '''165 # LEARNING CONSTRAINTS166 # These constraints allow to learn a decision tree starting from a167 # dataset of binary features. The resulting tree is represented168 # as a total assignment to the variables. The values of these variables169 # must be used to build a tree and implement the classifier.170 # Constraint 7: to discriminate a feature for value 0 at node j = 2,...,n171 # d0r,j <-> (OR for i=floor(j/2), j-1 of ((pj,i AND d0r,i) OR (ar,i AND ri,j)))172 # d0r,1 = 0173 for r in range(1, k + 1):174 s.add(Not(var['d0%i,%i' % (r, 1)])) # d0r,1 = 0175 for j in range(2, n + 1):176 or_list = []177 for i in range(floor(j / 2), j):178 if i >= 1 and (j in get_lr(i) or j in get_rr(i)) and 'r%i,%i' % (i, j) in var:179 or_list.append(Or(And(var['p%i,%i' % (j, i)], var['d0%i,%i' % (r, i)]),180 And(var['a%i,%i' % (r, i)], var['r%i,%i' % (i, j)])))181 s.add(Implies(var['d0%i,%i' % (r, j)], Or(or_list)))182 s.add(Implies(Or(or_list), var['d0%i,%i' % (r, j)]))183 # Constraint 8: to discriminate a feature for value 1 at node j = 2,...,n184 # d1r,j <-> (OR for i=floor(j/2), j-1 of ((pj,i AND d1r,i) OR (ar,i AND li,j)))185 # d1r,1 = 0186 for r in range(1, k + 1):187 s.add(Not(var['d1%i,%i' % (r, 1)])) # d1r,1 = 0188 for j in range(2, n + 1):189 or_list = []190 for i in range(floor(j / 2), j):191 if i >= 1 and (j in get_lr(i) or j in get_rr(i)) and 'l%i,%i' % (i, j) in var:192 or_list.append(Or(And(var['p%i,%i' % (j, i)], var['d1%i,%i' % (r, i)]),193 And(var['a%i,%i' % (r, i)], var['l%i,%i' % (i, j)])))194 s.add(Implies(var['d1%i,%i' % (r, j)], Or(or_list)))195 s.add(Implies(Or(or_list), var['d1%i,%i' % (r, j)]))196 # Constraint 9: using a feature r at node j, r = 1,...,k, j = 1,...,n197 # AND for i = floor(j/2), j-1 of (ur,i ^ pj,i -> -ar,j)198 # ur,j <-> (ar,j OR (OR for i = floor(j/2), j-1 of (ur,j AND pj,i)))199 for r in range(1, k + 1):200 for j in range(1, n + 1):201 and_list = []202 or_list = []203 for i in range(floor(j / 2), j):204 if i >= 1: # and j in getLR(i) or j in getRR(i):205 # ur,i ^ pj,i -> -ar,j206 and_list.append(207 Implies(And(var['u%i,%i' % (r, i)], var['p%i,%i' % (j, i)]), Not(var['a%i,%i' % (r, j)])))208 # AND for i = floor(j/2), j-1 of (ur,i ^ pj,i -> -ar,j)209 s.add(And(and_list))210 or_list.append(And(var['u%i,%i' % (r, i)], var['p%i,%i' % (j, i)]))211 s.add(Implies(var['u%i,%i' % (r, j)], Or(var['a%i,%i' % (r, j)], *or_list)))212 s.add(Implies(Or(var['a%i,%i' % (r, j)], *or_list), var['u%i,%i' % (r, j)]))213 # Constraint 10: for a non-leaf node j, exactly one feature is used214 # NOT vj -> (SUM for r=1, k of ar,j = 1)215 for j in range(1, n + 1):216 sum_list = []217 for r in range(1, k + 1):218 sum_list.append(var['a%i,%i' % (r, j)])219 exp = PbEq([(x, 1) for x in sum_list], 1)220 s.add(Implies(Not(var['v%i' % j]), exp))221 # Constraint 11: for a leaf node j, no feature is used222 # vj -> (SUM for r=1, k of ar,j = 0)223 for j in range(1, n + 1):224 sum_list = []225 for r in range(1, k + 1):226 sum_list.append(var['a%i,%i' % (r, j)])227 exp = Not(Or(sum_list))228 s.add(Implies(var['v%i' % j], exp))229 # Constraint 12: any positive example must be discriminated if the leaf230 # node is associated with the negative class.231 # vj AND NOT cj -> OR for r=1, k of d*r,j232 # * = 0 if current training example's feature value is 0233 # * = 1 if current training example's feature value is 1234 for example in pos_x:235 for j in range(1, n + 1):236 or_list = []237 for r in range(1, k + 1):238 if example[r - 1] == 0:239 or_list.append(var['d0%i,%i' % (r, j)])240 else:241 or_list.append(var['d1%i,%i' % (r, j)])242 s.add(Implies(And(var['v%i' % j], Not(var['c%i' % j])), Or(or_list)))243 # Constraint 13: any negative example must be discriminated if the leaf244 # node is associated with the positive class.245 # vj AND cj -> OR for r=1, k of d*r,j246 # * = 0 if current training example's feature value is 0247 # * = 1 if current training example's feature value is 1248 for example in neg_x:249 for j in range(1, n + 1):250 or_list = []251 for r in range(1, k + 1):252 if example[r - 1] == 0:253 or_list.append(var['d0%i,%i' % (r, j)])254 else:255 or_list.append(var['d1%i,%i' % (r, j)])256 s.add(Implies(And(var['v%i' % j], var['c%i' % j]), Or(or_list)))257 # Constraint 13.1: only a leaf node can be associated to a class.258 # ci -> vi, i=1,..,n259 for i in range(1, n + 1):260 s.add(Implies(var['c%i' % i], var['v%i' % i]))261 #'''262 # Additional constraint 1263 for i in range(1, n + 1):264 for t in range(0, n + 1):265 var['_lambda%i,%i' % (t, i)] = Bool('_lambda%i,%i' % (t, i))266 var['_tau%i,%i' % (t, i)] = Bool('_tau%i,%i' % (t, i))267 # lambda0,i = 1, tau0,i = 1268 if t == 0:269 s.add(var['_lambda%i,%i' % (t, i)])270 s.add(var['_tau%i,%i' % (t, i)])271 for i in range(1, n + 1):272 for t in range(1, floor(i / 2) + 1):273 if i > 1:274 # lambda t,i -> (lambda t,i-1 OR lambda t-1,i-1 AND vi)275 s.add(Implies(var['_lambda%i,%i' % (t, i)], Or(var['_lambda%i,%i' % (t, i - 1)],276 And(var['_lambda%i,%i' % (t - 1, i - 1)],277 var['v%i' % i]))))278 # (lambda t,i-1 OR lambda t-1,i-1 AND vi) -> lambda t,i279 s.add(Implies(280 Or(var['_lambda%i,%i' % (t, i - 1)], And(var['_lambda%i,%i' % (t - 1, i - 1)], var['v%i' % i])),281 var['_lambda%i,%i' % (t, i)]))282 for t in range(1, i + 1):283 if i > 1:284 # tau t,i -> (tau t,i-1 OR tau t-1,i-1 AND vi)285 s.add(Implies(var['_tau%i,%i' % (t, i)], Or(var['_tau%i,%i' % (t, i - 1)],286 And(var['_tau%i,%i' % (t - 1, i - 1)],287 Not(var['v%i' % i])))))288 # (tau t,i-1 OR tau t-1,i-1 AND vi) -> tau t,i289 s.add(Implies(290 Or(var['_tau%i,%i' % (t, i - 1)], And(var['_tau%i,%i' % (t - 1, i - 1)], Not(var['v%i' % i]))),291 var['_tau%i,%i' % (t, i)]))292 # Additional constraint 2293 for i in range(1, n + 1):294 for t in range(1, floor(i / 2) + 1):295 if 'l%i,%i' % (i, 2 * (i - t + 1)) in var and 'r%i,%i' % (i, 2 * (i - t + 1) + 1) in var:296 s.add(Implies(var['_lambda%i,%i' % (t, i)], And(Not(var['l%i,%i' % (i, 2 * (i - t + 1))]),297 Not(var['r%i,%i' % (i, 2 * (i - t + 1) + 1)]))))298 for t in range(ceil(i / 2), i + 1):299 if 'l%i,%i' % (i, 2 * (t - 1)) in var and 'r%i,%i' % (i, 2 * (t - 1)) in var:300 s.add(Implies(var['_tau%i,%i' % (t, i)], And(Not(var['l%i,%i' % (i, 2 * (t - 1))]),301 Not(var['r%i,%i' % (i, 2 * t - 1)]))))302 #'''303def get_solution(x_values, y_values, target_nodes):304 """ Returns all the possible solutions, or an empty tuple if no solution is found."""305 n = target_nodes # number of nodes306 # select only the rows where the target feature equals 1307 pos_x = x_values[y_values.astype(np.bool), :]308 # select only the rows where the target feature equals 0309 neg_x = x_values[~y_values.astype(np.bool), :]310 k = len(x_values[0])311 set_csp(pos_x, neg_x, n, k)312 global s313 status = s.check()314 solution = None315 if status == z3.sat:316 m = s.model()317 v_var = {}318 l_var = {}319 r_var = {}320 a_var = {}321 c_var = {}322 global var323 for k, v in var.items():324 try:325 if k.startswith('v'):...
server.py
Source:server.py
...121 self.request_subscribers[path] = future122 return await future123 def set_auth(self, path: str, username: str, password: str):124 self.auth[path] = (username, password)125 def set_csp(self, path: str, value: str):126 self.csp[path] = value127 def reset(self):128 self.request_subscribers.clear()129 self.auth.clear()130 self.csp.clear()131 self.gzip_routes.clear()132 self.routes.clear()133 def set_route(self, path, callback):134 self.routes[path] = callback135 def enable_gzip(self, path):136 self.gzip_routes.add(path)137 def set_redirect(self, from_, to):138 def handle_redirect(request):139 request.setResponseCode(HTTPStatus.FOUND)...
csp.py
Source:csp.py
...10 self.domains = {}11 self.assertions = []12 self.stack = []13 def add_domain(self, domainname, domain):14 domain.set_csp( self )15 if len( self.domains ) == 0:16 self.topdomain = domain17 self.domains[ domainname ] = domain18 self.dnames.append( domainname )19 def add_assertion(self, assertion):20 self.assertions.append( assertion )21 assertion.set_csp( self )22 def set_goal(self, goal):23 self.goal = goal24 goal.set_csp( self )25 def run(self):26 27 solutions = 028 while True:29 ret = False30 tuple = Tuple()31 ret = self.recurse_domains( deque( self.dnames ), tuple )32 if ret:33 self.assertTuple( tuple )34 self.stack.append( tuple )35 if self.goal.goal_reached():36 print "goal reached..."37 solutions += 138 for tuple in self.stack:...
domain.py
Source:domain.py
...5 self.curgen = None6 self.generators = []7 self.curval = None8 self.curvals = []9 def set_csp(self, csp):10 self.csp = csp11 def stack_size(self):12 return len(self.generators)13 def push_generator(self, tuple):14 self.generators.append( self.curgen )15 self.curgen = self.get_generator( tuple )16 self.curvals.append( self.curval )17 self.curval = None18 def refresh_generator(self, tuple):19 self.curgen = self.get_generator( tuple )20 self.curval = None 21 def pop_generator(self):22 if len(self.generators) > 0:23 self.curgen = self.generators.pop()...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!