How to use send_instances method in Contexts

Best Python code snippet using Contexts

Run Contexts automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

failure_only_tests.py

Source: failure_only_tests.py Github

copy
1from io import StringIO
2from contexts.plugins.reporting import cli
3import contexts
4
5
6# TODO: test_class_started, failed, errored, etc
7
8class WhenAnUnexpectedErrorOccurs:
9    def context(self):
10        self.master = cli.FailuresOnlyMaster(StringIO())
11        self.before = cli.FailuresOnlyBefore()
12        self.after = cli.FailuresOnlyAfter()
13
14        send_instances(self.master, [])
15        send_instances(self.before, [self.master])
16        send_instances(self.after, [self.master])
17
18    def because_an_unexpected_error_occurs(self):
19        self.exception = contexts.catch(self.before.unexpected_error, Exception())
20
21    def it_should_be_ok(self):
22        assert self.exception is None
23
24
25def send_instances(plug, instances):
26    gen = plug.request_plugins()
27    next(gen)
28    try:
29        gen.send({type(x): x for x in instances})
30    except StopIteration:
31        pass
32
Full Screen

server.py

Source: server.py Github

copy
1import sys
2
3x = sys.argv[1:]
4IP = x[0]
5Port = int(x[1])
6No_of_Clients = int(x[2])
7
8
9all_connections = []
10all_addresses = []
11all_snakes = []
12
13import pickle
14import socket
15import curses
16from curses import KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP
17import random
18from random import randint
19import threading
20from queue import Queue
21import time
22
23random.seed(123)
24
25
26
27S = socket.socket()
28Host = ""#socket.gethostname()
29
30#Binding the socket and listening for the connections
31def bindSocket():
32    try:
33        print ('Binding the socket' + str(Port))
34        S.bind((Host,Port))
35        S.listen(No_of_Clients)
36
37    except socket.error as msg:
38        print('Socket Binding error' + str(msg) + '\n Retrying...')
39        bindSocket()
40
41
42#Handling Connections from multiple clients and saving to a list
43#closing all previous connections when server1.py file is restarted
44def accepting_connections():
45    for c in all_connections:
46        c.close()
47    del all_connections[:]
48    del all_addresses[:]
49
50    while len(all_connections) != No_of_Clients:
51        try:
52            conn, address = S.accept()
53            S.setblocking(1) #prevents timeout
54            # conn.send("Waiting for other players to join \n{} have joined\t {} left")
55            
56            all_connections.append(conn)
57            all_addresses.append(address)
58
59            print("Connection has been establisghed : " + address[0] )
60
61        except:
62            print("Error Accepting Connections")
63
64
65
66WIDTH = 35
67HEIGHT = 20
68MAX_X = WIDTH - 2
69MAX_Y = HEIGHT - 2
70SNAKE_LENGTH = 5
71#SNAKE_X = SNAKE_LENGTH + 1 # To be selected randomly for each snake while initiation
72#SNAKE_Y = 3                # To be selected randomly for each snake while initiation
73TIMEOUT = 100
74
75def generator(x,y):
76    while True:
77        r = random.randint(x,y)
78        yield r
79
80
81
82
83
84#######################            Class Snake Starts Here            #########################
85class Snake(object):
86
87    REV_DIR_MAP = {
88        KEY_UP: KEY_DOWN, KEY_DOWN: KEY_UP,
89        KEY_LEFT: KEY_RIGHT, KEY_RIGHT: KEY_LEFT,
90    }                                                       ### Its a Dictionary
91
92
93    """random.randint(5,WIDTH-10)"""
94    """random.randint(1,HEIGHT-1)"""
95    def __init__(self, x , y  ):                       ### Constructor
96        
97        print (x,y)
98        self.body_list = []
99        self.hit_score = 0
100        self.timeout = TIMEOUT
101        self.killn = 0
102
103        for i in range(SNAKE_LENGTH, 0, -1):
104            self.body_list.append(Body(x - i, y))
105
106        self.body_list.append(Body(x, y, '0'))
107        self.direction = KEY_RIGHT
108        self.last_head_coor = (x, y)
109        self.direction_map = {
110            KEY_UP: self.move_up,
111            KEY_DOWN: self.move_down,
112            KEY_LEFT: self.move_left,
113            KEY_RIGHT: self.move_right
114        }
115
116    @property
117    def score(self):                                        ### Function
118        return 'Score : {}'.format(self.hit_score)
119
120    @property
121    def kill(self):                                        ### Function
122        return 'You Killed : {} Snakes'.format(self.killn)
123    
124
125    def killed(self):
126        self.killn += 1  
127
128    def add_body(self, body_list):                          ### Function
129        self.body_list.extend(body_list)
130
131    def eat_food(self, food):                               ### Function
132        food.reset()
133        body = Body(self.last_head_coor[0], self.last_head_coor[1])
134        self.body_list.insert(-1, body)
135        self.hit_score += 1
136
137    @property
138    def collided(self):   
139        if(self.head.x==WIDTH or self.head.x == 0):
140            return True
141        elif(self.head.y==HEIGHT or self.head.y == 0):
142            return True                                  ### Function
143        return any([body.coor == self.head.coor
144                    for body in self.body_list[:-1]]) 
145
146
147
148    def update(self):                                       ### Function
149        last_body = self.body_list.pop(0)
150        last_body.x = self.body_list[-1].x
151        last_body.y = self.body_list[-1].y
152        self.body_list.insert(-1, last_body)
153        self.last_head_coor = (self.head.x, self.head.y)
154        self.direction_map[self.direction]()
155
156    def change_direction(self, direction):                  ### Function
157        if direction != Snake.REV_DIR_MAP[self.direction]:
158            self.direction = direction
159
160
161    @property
162    def head(self):                                         ### Function
163        return self.body_list[-1]
164
165    @property                                               ### Function
166    def coor(self):
167        return self.head.x, self.head.y
168
169    def move_up(self):                                      ### Function
170        self.head.y -= 1
171
172
173    def move_down(self):                                    ### Function
174        self.head.y += 1
175
176
177    def move_left(self):                                    ### Function
178        self.head.x -= 1
179
180
181    def move_right(self):                                   ### Function
182        self.head.x += 1
183
184#########################               Class Body Starts here            ##################################
185class Body(object):
186    def __init__(self, x, y, char='#'):                     ### Constructor
187        self.x = x
188        self.y = y
189        self.char = char
190
191    @property
192    def coor(self):                                         ### Function
193        return self.x, self.y
194
195
196#########################               Class food Starts Here            ###################################
197class Food(object):         
198    def __init__(self, char='&'):                   ### Constructor
199        self.x = randint(1, MAX_X)
200        self.y = randint(1, MAX_Y)
201        self.char = char
202
203    def reset(self):                                        ### Function
204        self.x = randint(1, MAX_X)
205        self.y = randint(1, MAX_Y)
206
207
208# check collision between different snakes
209#To check
210def check_collision():
211    list_collided = []
212    for i in range(len(all_snakes)):
213        for j in range(i+1,len(all_snakes)):
214            if all_snakes[i].head.coor == all_snakes[j].head.coor:
215                list_collided.append(i)
216                list_collided.append(j)
217        for j in range(len(all_snakes)):
218            if i==j:
219                continue
220            for body in all_snakes[i].body_list[:-1]:
221                if body.coor == all_snakes[j].head.coor and i not in list_collided:
222                    list_collided.append(i)
223                    all_snakes[j].killed()
224    
225    return list_collided
226
227#Delete collided snakes and close their connections
228#To Check
229def del_connections(list):
230    dclist = []
231    dalist = []
232    dslist = []
233    for i in list:
234        dclist.append(all_connections[i])
235        dalist.append(all_addresses[i])
236        dslist.append(all_snakes[i])
237        all_connections[i].close()
238    for c in dclist:
239        all_connections.remove(c)
240    for a in dalist:
241        all_addresses.remove(a)
242    for s in dslist:
243        all_snakes.remove(s)
244
245
246
247#Check if the game is ended
248def isEnd():
249    if len(all_connections) >= 1:
250        return False
251    else:
252        return True
253
254
255#Create instances of snake
256def create_snakes():
257    a = generator(5,WIDTH-10)
258    b = generator(3,HEIGHT-3)
259    while len(all_snakes) != No_of_Clients:
260        print(len(all_snakes))
261        i = a.__next__()
262        j = b.__next__()
263        x=1
264        s = Snake(i,j)
265        for snake in all_snakes:
266            if s.head.y == snake.head.y:
267                x = 0
268
269        if x:
270            all_snakes.append(s)   
271
272
273
274    
275#Send all the snakes and food and ID to client
276#to be done in multithreading
277def send_instances(food):
278    for i in range(len(all_snakes)):
279       
280       trysend = 0
281       while True:
282            try:
283                to_send = pickle.dumps([all_snakes,food,i])
284                all_connections[i].send(to_send)
285                break
286            except:
287                if trysend == 0:
288                    trysend = trysend +1
289                    continue
290                else:
291                    all_connections[i].close()
292                    all_addresses.pop(i)
293                    all_snakes.pop(i)
294                    all_connections.pop(i)
295                    break
296
297
298
299
300
301#Updated snakes to be received & update
302#To be done in multithreading
303def receive_updates():
304    eaten = False
305    for i in range(len(all_snakes)):
306        tryrecv =0
307        while True:
308            try:
309                received = all_connections[i].recv(5*1024)
310                updated = pickle.loads(received)
311                all_snakes[i] = updated[0]
312                if updated[1] == True:
313                    eaten = True
314                break
315            except:
316                if tryrecv == 0:
317                    tryrecv = tryrecv +1
318                    continue
319                else:
320                    all_connections[i].close()
321                    all_addresses.pop(i)
322                    all_snakes.pop(i)
323                    all_connections.pop(i)
324                    break
325
326
327    return eaten
328    
329
330
331
332
333
334
335            
336def main():
337    bindSocket()
338    accepting_connections()
339    
340    create_snakes()
341    print(len(all_snakes))
342    food = Food('*')
343
344
345
346    while isEnd() == False:
347    
348        send_instances(food)
349        print("Sent")
350        eaten = receive_updates()
351        print("Received")
352        if eaten:
353            food.reset()
354
355        list_collided = []
356        for i in range(len(all_snakes)):
357            if all_snakes[i].collided and i not in list_collided:
358                list_collided.append(i)
359
360
361
362
363        del_connections(list_collided)
364        del_connections(check_collision())
365
366
367
368    print("Game Ended")
369
370
371
372main()
373
Full Screen

trainer_slave.py

Source: trainer_slave.py Github

copy
1#!/usr/bin/env python
2
3import sys, os.path, math, gc, time
4import collections, random, itertools
5from mpi4py import MPI
6
7import decoder
8import model
9import svector
10import oracle
11import maxmargin
12import monitor, log
13
14### Per-feature learning rates
15# possible values are: "arow", "gauss-newton"
16update_feature_scales = "gauss-newton"
17
18# The following two parameters set the initial feature learning rates.
19# If a feature is in initial_feature_learning_rates, its value there
20# is its initial learning rate. Otherwise, it is initial_learning_rate.
21initial_learning_rate = 1.
22initial_feature_learning_rates = svector.Vector()
23
24# The maximum learning rate for any feature (or None)
25max_learning_rate = 0.1
26
27# The following only applies to arow:
28# How quickly to slow down per-feature learning rates.
29learning_rate_decay = 0.01
30
31# The following only applies to gauss-newton:
32# The learning rate for a feature with unit variance.
33unit_learning_rate = 0.01
34initial_learning_rate_strength = 1
35
36# Weight on BLEU score used to compute hope translations.
37hope_weight = 1.
38
39# Which features to show in log file
40watch_features = svector.Vector("lm1=1 lm2=1 pef=1 pfe=1 lef=1 lfe=1 word=1 green=1 unknown=1")
41
42class ForestInstance(maxmargin.Instance):
43    def __init__(self, sentid, goal):
44        maxmargin.Instance.__init__(self, instance_id=sentid)
45        self.sentid = sentid
46        self.goal = goal
47
48    def get_fear(self):
49        """Assumes that oraclemodel.input() has been called"""
50        if not self.goal:
51            raise NotImplementedError()
52        weights = theoracle.make_weights(additive="edge")
53        # use in-place operations because theoracle.make_weights might
54        # be a subclass of svector.Vector
55        weights += self.qp.mweights
56        self.goal.reweight(weights)
57
58        fear_vector, fear = decoder.get_nbest(self.goal, 1, 1)[0]
59        fear_ovector = theoracle.finish(fear_vector, fear)
60        fear_mvector = theoracle.clean(fear_vector)
61
62        if log.level >= 1:
63            log.write("fear hyp: %s\n" % " ".join(sym.tostring(e) for e in fear))
64            log.write("fear features: %s\n" % fear_mvector)
65            log.write("fear oracle: %s\n" % fear_ovector)
66
67        return maxmargin.Hypothesis(fear_mvector, fear_ovector)
68
69    def get_hope(self):
70        """Assumes that oraclemodel.input() has been called"""
71        if not self.goal:
72            _, hope = min((self.qp.mweights.dot(hyp.mvector) + hope_weight * self.qp.oweights.dot(hyp.ovector), hyp) for hyp in self.hyps)
73            return hope
74            
75        weights = theoracle.make_weights(additive="edge")
76        # use in-place operations because theoracle.make_weights might
77        # be a subclass of svector.Vector
78        weights *= -hope_weight
79        weights += self.qp.mweights
80        self.goal.reweight(weights)
81
82        hope_vector, hope = decoder.get_nbest(self.goal, 1, 1)[0]
83        hope_ovector = theoracle.finish(hope_vector, hope)
84        hope_mvector = theoracle.clean(hope_vector)
85
86        if log.level >= 1:
87            log.write("hope hyp: %s\n" % " ".join(sym.tostring(e) for e in hope))
88            log.write("hope features: %s\n" % hope_mvector)
89            log.write("hope oracle: %s\n" % hope_ovector)
90        return maxmargin.Hypothesis(hope_mvector, hope_ovector)
91
92class Learner(object):
93    def __init__(self):
94        self.sum_weights_helper = svector.Vector()
95        self.n_weights = 0
96
97        self.sum_updates2 = svector.Vector()
98        self.n_updates = 0
99
100    def compute_feature_learning_rate(self, f):
101        if f in initial_feature_learning_rates:
102            r0 = initial_feature_learning_rates[f]
103        else:
104            r0 = initial_learning_rate
105        if update_feature_scales == "arow":
106            # \Sigma^{-1} := \Sigma^{-1} + learning_rate_decay * sum_updates2[f]
107            variance = 1./r0 + sum_updates2[f] * learning_rate_decay
108            r = 1. / variance
109        elif update_feature_scales == "gauss-newton":
110            variance = (initial_learning_rate_strength/r0 + self.sum_updates2[f] / unit_learning_rate) / (initial_learning_rate_strength + self.n_updates)
111            r = 1. / variance
112        if max_learning_rate:
113            r = min(max_learning_rate, r)
114        return r
115
116    def train(self, sent, instances):
117        # Set up quadratic program
118        qp = maxmargin.QuadraticProgram()
119        for instance in instances:
120            qp.add_instance(instance)
121
122        # Make oracle weight vector
123        oweights = theoracle.make_weights(additive="sentence")
124        oweights *= -1
125
126        # Make vector of learning rates
127        # We have to be careful to assign a learning rate to every feature in the forest
128        # This is not very efficient
129        feats = set()
130        for instance in qp.instances:
131            if hasattr(instance, "goal") and instance.goal:
132                for item in instance.goal.bottomup():
133                    for ded in item.deds:
134                        feats.update(ded.dcost)
135            for hyp in instance.hyps:
136                feats.update(hyp.mvector)
137        learning_rates = svector.Vector()
138        for feat in feats:
139            learning_rates[feat] = self.compute_feature_learning_rate(feat)
140        if log.level >= 3:
141            log.writeln("learning rate vector: %s" % learning_rates)
142
143        # Solve the quadratic program
144        qp.optimize(thedecoder.weights, oweights, learning_rate=learning_rates)
145
146        thedecoder.weights.compact()
147        log.write("feature weights: %s\n" % (thedecoder.weights * watch_features))
148
149        # Update weight sum for averaging
150        # sum_weights_helper = \sum_{i=0}^n (i \Delta w_i)
151        self.sum_weights_helper += self.n_weights * qp.delta_mweights()
152        self.n_weights += 1
153
154        # Update feature scales
155        if update_feature_scales:
156            for instance in qp.instances:
157                """u = svector.Vector(instance.hope.mvector)
158                for hyp in instance.hyps:
159                    u -= hyp.alpha*hyp.mvector
160                self.sum_updates2 += u*u"""
161                for hyp in instance.hyps:
162                    if hyp is not instance.hope: # hyp = instance.hope is a non-update
163                        u = instance.hope.mvector - hyp.mvector
164                        self.sum_updates2 += hyp.alpha*(u*u)
165                        self.n_updates += hyp.alpha
166
167            log.write("feature learning rates: %s\n" % (" ".join("%s=%s" % (f, self.compute_feature_learning_rate(f)) for f in watch_features)))
168
169        theoracle.update(sent.score_comps)
170
171        # make a plain Instance (without forest)
172        # we used to designate a hope translation,
173        #send_instance = maxmargin.Instance(cur_instance.hyps, hope=cur_instance.hope, instance_id=cur_instance.sentid)
174        # but now are letting the other node choose.
175        send_instances = []
176        for instance in instances:
177            if hasattr(instance, "goal") and instance.goal:
178                send_instances.append(maxmargin.Instance(instance.hyps, instance_id=instance.sentid))
179
180        assert len(send_instances) == 1
181        return send_instances[0]
182
183decoder_errors = 0
184def process(sent):
185    # Add an flen attribute that gives the length of the input sentence.
186    # In the lattice-decoding case, we have to make a guess.
187    distance = sent.compute_distance()
188    sent.flen = distance.get((0,sent.n-1), None) # could be missing if n == 0
189
190    theoracle.input(sent)
191
192    global decoder_errors
193    try:
194        goal = thedecoder.translate(sent)
195        thedecoder.process_output(sent, goal)
196        decoder_errors = 0
197        if goal is None: raise Exception("parse failure")
198    except Exception:
199        import traceback
200        log.write("decoder raised exception: %s" % "".join(traceback.format_exception(*sys.exc_info())))
201        decoder_errors += 1
202        if decoder_errors >= 3:
203            log.write("decoder failed too many times, passing exception through!\n")
204            raise
205        else:
206            return
207
208    # Augment forest with oracle features
209    # this is overkill if we aren't going to search for hope/fear
210    goal.rescore(theoracle.models, thedecoder.weights, add=True)
211
212    best_vector, best = decoder.get_nbest(goal, 1)[0]
213    best_mvector = theoracle.clean(best_vector)
214    best_ovector = theoracle.finish(best_vector, best)
215    best_loss = theoracle.make_weights(additive="sentence").dot(best_ovector)
216    log.writeln("best hyp: %s %s cost=%s loss=%s"  % (" ".join(sym.tostring(e) for e in best), best_vector, thedecoder.weights.dot(best_mvector), best_loss))
217
218    sent.score_comps = best_ovector
219    sent.ewords = [sym.tostring(e) for e in best]
220
221    return goal
222
223if __name__ == "__main__":
224    gc.set_threshold(100000,10,10)
225    
226    import optparse
227    optparser = optparse.OptionParser()
228    # Most of these aren't actually used here...ugly
229    optparser.add_option("-W", dest="outweightfilename", help="filename to write weights to")
230    optparser.add_option("-L", dest="outscorefilename", help="filename to write BLEU scores to")
231    optparser.add_option("-B", dest="bleuvariant", default="NIST")
232    optparser.add_option("-S", dest="stopfile")
233    optparser.add_option("-p", dest="parallel", type=int, help="how many slaves to start", default=1)
234    optparser.add_option("--input-lattice", dest="input_lattice", action="store_true")
235    optparser.add_option("--holdout", "--heldout-ratio", dest="heldout_ratio", help="fraction of sentences to hold out", type=float, default=None)
236    optparser.add_option("--heldout-sents", dest="heldout_sents", help="number of sentences to hold out", type=int, default=None)
237    optparser.add_option("--heldout-policy", dest="heldout_policy", default="last", help="which sentences to hold out (first, last, uniform)")
238    optparser.add_option("--no-shuffle", dest="shuffle_sentences", action="store_false", default=True)
239
240    try:
241        configfilename = sys.argv[1]
242    except IndexError:
243        sys.stderr.write("usage: trainer_slave.py config-file [options...]\n")
244        sys.exit(1)
245
246    if log.level >= 1:
247        log.write("Reading configuration from %s\n" % configfilename)
248    execfile(configfilename)
249
250    opts, args = optparser.parse_args(args=sys.argv[2:])
251
252    maxmargin.watch_features = watch_features
253
254    theoracle = oracle.Oracle(order=4, variant=opts.bleuvariant, oracledoc_size=10)
255    thedecoder = make_decoder()
256    thelearner = Learner()
257    weight_stack = []
258
259    if log.level >= 1:
260        gc.collect()
261        log.write("all structures loaded, memory=%s\n" % (monitor.memory()))
262
263    comm = MPI.Comm.Get_parent()
264    log.prefix = '[%s] ' % (comm.Get_rank(),)
265
266    instances = []
267    while True:
268        msg = comm.recv()
269
270        if msg[0] == 'train':
271            sent = msg[1]
272            goal = process(sent)
273            instances.append(ForestInstance(sent.id, goal))
274
275            while comm.Iprobe(tag=1):
276                msg = comm.recv(tag=1)
277                if msg[0] == 'update':
278                    log.writeln("receive update for sentence %s" % msg[1].id)
279                    instances.append(msg[1].instance)
280            
281            instance = thelearner.train(sent, instances)
282            sent.instance = instance # it would be nicer if sent and instance were the same object
283            comm.send(sent, dest=0)
284            instances = []
285
286        elif msg[0] == 'translate':
287            sent = msg[1]
288            process(sent)
289            comm.send(sent, dest=0)
290
291        elif msg[0] == 'gather-weights':
292            # Average weights (Daume trick)
293            sum_weights = float(thelearner.n_weights) * thedecoder.weights - thelearner.sum_weights_helper
294            sum_weights.compact()
295            log.writeln("send summed weights")
296            comm.gather(sum_weights, root=0)
297            comm.gather(thelearner.n_weights, root=0)
298
299        elif msg[0] == 'push-weights':
300            weight_stack.append(svector.Vector(thedecoder.weights))
301            thedecoder.weights = msg[1]
302            log.writeln("receive weights: %s" % (thedecoder.weights * watch_features))
303
304        elif msg[0] == 'pop-weights':
305            thedecoder.weights = weight_stack.pop()
306            log.writeln("restore weights: %s" % (thedecoder.weights * watch_features))
307
308        elif msg[0] == 'end':
309            break
310
311        else:
312            log.writeln("unknown command: %s" % (msg[0],))
313
314
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Contexts on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)