Best Python code snippet using tempest_python
RunController.py
Source:RunController.py  
1# Author: Thomas C.F. Goolsby - tgoolsby@mit.edu2# This file was created in support of the CNCPT Thesis3# Fall 2020 - EM.THE45import cProfile6import datetime7import io8import os9import pickle10import pstats11import random12from pstats import SortKey1314import numpy as np1516from ArchitectureGeneration.Architecture import Architecture17from Simulation.Communication.Network import auto_network_architectures18from Simulation.DataManagement.PostProcessing import post_process19from Simulation.SimulationManager import SimulationManager20from Simulation.Utility.Constants import intialize_constants212223class RunController:24    """ 2526    """2728    def __init__(self, output_path=None, profile=False):29        """3031        :param output_path:32        :return:33        """34        if output_path is None:35            path = os.path.join(os.path.dirname(__file__), "..", 'Output')36            self.output_path = os.path.abspath(path)37        else:38            self.output_path = os.path.abspath(output_path)39        self.timestamp_format = "%Y-%m-%d-%H%M%S"40        self.seedstamp_format = "%d"41        self.SimulationManager = SimulationManager42        self.profile = profile4344        try:45            os.mkdir(self.output_path)46        except FileExistsError:47            pass4849    def run_set_CnCPT(self, CONOPCon, CompCon, LeadershipPriority, FixedArchGenerator, VariableArchInstance, controls,50                      seeds=[0], name=None,51                      constants=None):52        """5354        :param all_units:55        :param controls:56        :param seeds:57        :param name:58        :param constants:59        :return:60        """61        set_data = {}6263        if constants is None:64            constants = intialize_constants()6566        # Generate Top level directory67        if name is not None:68            output_path = os.path.join(self.output_path, name)69            try:70                os.mkdir(output_path)71            except FileExistsError:72                pass73        else:74            output_path = self.output_path7576        # Generate Timestamp level directory77        datestring = datetime.datetime.now().strftime(self.timestamp_format)78        output_path = os.path.join(output_path, datestring)79        try:80            os.mkdir(output_path)81        except FileExistsError:82            pass8384        # Run Seeds85        for seed in seeds:86            FixedArchUnits = FixedArchGenerator()87            VariableArch = Architecture.create_from_code(VariableArchInstance.ArchCode, CONOPCon, CompCon,88                                                         LeadershipPriority, VariableArchInstance.side,89                                                         VariableArchInstance.name)90            all_units = VariableArch.units + FixedArchUnits91            seed_data = self.run_seed(all_units, controls, constants, seed, output_path)92            set_data[seed] = seed_data93        data = {"score_mean": np.mean([set_data[seed]["score"] for seed in set_data]),94                "score_var": np.var([set_data[seed]["score"] for seed in set_data]),95                "vsm_ships_mean": np.mean([set_data[seed]["vsm_ships"] for seed in set_data]),96                "vsm_ships_var": np.var([set_data[seed]["vsm_ships"] for seed in set_data]),97                "vsm_aircraft_mean": np.mean([set_data[seed]["vsm_aircraft"] for seed in set_data]),98                "vsm_aircraft_var": np.var([set_data[seed]["vsm_aircraft"] for seed in set_data]),99                "vscm_ships_mean": np.mean([set_data[seed]["vscm_ships"] for seed in set_data]),100                "vscm_ships_var": np.var([set_data[seed]["vscm_ships"] for seed in set_data]),101                "vscm_aircraft_mean": np.mean([set_data[seed]["vscm_aircraft"] for seed in set_data]),102                "vscm_aircraft_var": np.var([set_data[seed]["vscm_aircraft"] for seed in set_data]),103                "vscm_blue_mean": np.mean([set_data[seed]["vscm_blue"] for seed in set_data]),104                "vscm_blue_var": np.var([set_data[seed]["vscm_blue"] for seed in set_data]),105                "fam_ships_mean": np.mean([set_data[seed]["fam_ships"] for seed in set_data]),106                "fam_ships_var": np.var([set_data[seed]["fam_ships"] for seed in set_data]),107                "fam_aircraft_mean": np.mean([set_data[seed]["fam_aircraft"] for seed in set_data]),108                "fam_aircraft_var": np.var([set_data[seed]["fam_aircraft"] for seed in set_data]),109                "facm_ships_mean": np.mean([set_data[seed]["facm_ships"] for seed in set_data]),110                "facm_ships_var": np.var([set_data[seed]["facm_ships"] for seed in set_data]),111                "facm_aircraft_mean": np.mean([set_data[seed]["facm_aircraft"] for seed in set_data]),112                "facm_aircraft_var": np.var([set_data[seed]["facm_aircraft"] for seed in set_data]),113                "facm_red_mean": np.mean([set_data[seed]["facm_red"] for seed in set_data]),114                "facm_red_var": np.var([set_data[seed]["facm_red"] for seed in set_data]),115                "individual_seed_data_mean": set_data}116        data["score_mean_variance"] = data["score_mean"] - ((2 * ((data["score_var"]) ** 2)) / 2.0)117        return data, output_path118119    def run_set(self, FixedArchGenerator, VariableArchGenerator, controls,120                seeds=[0], name=None,121                constants=None):122        """123        :param controls:124        :param seeds:125        :param name:126        :param constants:127        :return:128        """129        set_data = {}130131        if constants is None:132            constants = intialize_constants()133134        # Generate Top level directory135        if name is not None:136            output_path = os.path.join(self.output_path, name)137            try:138                os.mkdir(output_path)139            except FileExistsError:140                pass141        else:142            output_path = self.output_path143144        # Generate Timestamp level directory145        datestring = datetime.datetime.now().strftime(self.timestamp_format)146        output_path = os.path.join(output_path, datestring)147        try:148            os.mkdir(output_path)149        except FileExistsError:150            pass151152        # Run Seeds153        for seed in seeds:154            FixedArchUnits = FixedArchGenerator()155            VariableArchUnits = VariableArchGenerator()156            all_units = VariableArchUnits + FixedArchUnits157            seed_data = self.run_seed(all_units, controls, constants, seed, output_path)158            set_data[seed] = seed_data159160        data = {"score_mean": np.mean([set_data[seed]["score"] for seed in set_data]),161                "score_var": np.var([set_data[seed]["score"] for seed in set_data]),162                "vsm_ships_mean": np.mean([set_data[seed]["vsm_ships"] for seed in set_data]),163                "vsm_ships_var": np.var([set_data[seed]["vsm_ships"] for seed in set_data]),164                "vsm_aircraft_mean": np.mean([set_data[seed]["vsm_aircraft"] for seed in set_data]),165                "vsm_aircraft_var": np.var([set_data[seed]["vsm_aircraft"] for seed in set_data]),166                "vscm_ships_mean": np.mean([set_data[seed]["vscm_ships"] for seed in set_data]),167                "vscm_ships_var": np.var([set_data[seed]["vscm_ships"] for seed in set_data]),168                "vscm_aircraft_mean": np.mean([set_data[seed]["vscm_aircraft"] for seed in set_data]),169                "vscm_aircraft_var": np.var([set_data[seed]["vscm_aircraft"] for seed in set_data]),170                "vscm_blue_mean": np.mean([set_data[seed]["vscm_blue"] for seed in set_data]),171                "vscm_blue_var": np.var([set_data[seed]["vscm_blue"] for seed in set_data]),172                "fam_ships_mean": np.mean([set_data[seed]["fam_ships"] for seed in set_data]),173                "fam_ships_var": np.var([set_data[seed]["fam_ships"] for seed in set_data]),174                "fam_aircraft_mean": np.mean([set_data[seed]["fam_aircraft"] for seed in set_data]),175                "fam_aircraft_var": np.var([set_data[seed]["fam_aircraft"] for seed in set_data]),176                "facm_ships_mean": np.mean([set_data[seed]["facm_ships"] for seed in set_data]),177                "facm_ships_var": np.var([set_data[seed]["facm_ships"] for seed in set_data]),178                "facm_aircraft_mean": np.mean([set_data[seed]["facm_aircraft"] for seed in set_data]),179                "facm_aircraft_var": np.var([set_data[seed]["facm_aircraft"] for seed in set_data]),180                "facm_red_mean": np.mean([set_data[seed]["facm_red"] for seed in set_data]),181                "facm_red_var": np.var([set_data[seed]["facm_red"] for seed in set_data]),182                "individual_seed_data_mean": set_data}183        data["score_mean_variance"] = data["score_mean"] - ((2 * ((data["score_var"]) ** 2)) / 2.0)184        return data, output_path185186    def run_seed(self, all_units, controls, constants, seed, output_path):187        """188189        :param all_units:190        :param controls:191        :param constants:192        :param seed:193        :param output_path:194        :return:195        """196197        seed_string = self.seedstamp_format % seed198        output_path = os.path.join(output_path, seed_string)199        success = False200        while not success:201            try:202                os.mkdir(output_path)203            except FileExistsError:204                print('This Output Path is not Empty: {0}'.format(output_path))205            else:206                success = True207208        open(os.path.join(output_path, 'meta_data.txt'), "w").write(output_path)209210        networks = auto_network_architectures(all_units)211        SimulationManager = self.SimulationManager(all_units, networks, constants, output_path,212                                                   start_time=controls['start_time'],213                                                   end_time=controls['end_time'],214                                                   full_data_logging=controls["full_data_logging"])215216        # Seed217        random.seed(seed)218219        for unit in all_units:220            unit.register(SimulationManager, constants)221222        self.run(SimulationManager, controls)223224        seed_data = post_process(SimulationManager)225        if controls["full_data_logging"]:226            log_data = {"kill_log": SimulationManager.kill_log,227                        "isr_log": SimulationManager.isr_log,228                        "weapon_log": SimulationManager.weapon_log,229                        "drawdown_log": SimulationManager.drawdown_log}230            with open(os.path.join(SimulationManager.output_path, "Simulation_Logs.pkl"), 'wb') as f:231                pickle.dump(log_data, f)232        SimulationManager.data_logger.close_data_logger(SimulationManager)233        return seed_data234235    def run(self, simulation_manager, controls):236        """237238        :param simulation_manager:239        :param controls:240        :return:241        """242        if self.profile:243            pr = cProfile.Profile()244            pr.enable()245            simulation_manager.run(until=controls['end_time'])246            pr.disable()247            s = io.StringIO()248            sortby = SortKey.CUMULATIVE249            ps = pstats.Stats(pr, stream=s).sort_stats(sortby)250            ps.print_stats()251            print(s.getvalue())252        else:
...diana_pad_gen.py
Source:diana_pad_gen.py  
1import copy2string_set = ['0', '1', '2', '3', '4', '1', '0', '3', '2', None, '2', '3', '0', '1', None, '3', '2', '1', '0', None, '4', None, None, None, '0']3pair_set=['00', '01', '02', '03', '04', '10', '11', '12', '13', '14', '20', '21', '22', '23', '24', '30', '31', '32', '33', '34', '40', '41', '42', '43', '44']4init_set=[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]5total_set=[init_set]6def printAllKLength(char_set, k):7    n = len(char_set)8    printAllKLengthRec(char_set, "", n, k)9def fill(x,y,ch,set_data):10  ch_int=int(ch)11  x_int=int(pair_set[5*x+y][0])12  y_int=int(pair_set[5*x+y][1])13  if set_data[5*x+y] or set_data[5*y+x] or set_data[5*x_int+ch_int] or set_data[5*ch_int+x_int] or set_data[5*y_int+ch_int] or set_data[5*ch_int+y_int] is not None:14    return False15  else:16    set_data[5*x_int+ch_int]=pair_set[5*x+y][1]17    set_data[5*ch_int+x_int]=pair_set[5*x+y][1]18    set_data[5*y_int+ch_int]=pair_set[5*x+y][0]19    set_data[5*ch_int+y_int]=pair_set[5*x+y][0]20    set_data[5*y+x]=ch21    set_data[5*x+y]=ch22    return True23def revert_fill(x,y,ch,set_data):24  ch_int=int(ch)25  x_int=int(pair_set[5*x+y][0])26  y_int=int(pair_set[5*x+y][1])27  set_data[5*x_int+ch_int]=None28  set_data[5*ch_int+x_int]=None29  set_data[5*y_int+ch_int]=None30  set_data[5*ch_int+y_int]=None31  set_data[5*y+x]=None32  set_data[5*x+y]=None33def find_empty(set_data):34  for i in range(25):35    if set_data[i] is None:36      x=i//537      y=i-5*x38      return (x,y)39  return None40def mapping(ch):41  if ch == "0":42    return "A"43  if ch == "1":44    return "B"45  if ch == "2":46    return "C"47  if ch == "3":48    return "D"49  if ch == "4":50    return "E"51  if ch is None:52    return "-"53def print_table(set_data):54  print(" ")55  for i in range(8):56    print("-",end="-")57  print("-")58  for i in ["|","  |","A","B","C","D","E"]:59    print(i,end=" ")60  print("|")61  for i in range(8):62    print("-",end="-")63  print("-")64  for i in range(5):65    print("|",end=" ")66    print(mapping(str(i)),"|",end=" ")67    for j in range(5):68      print(mapping(set_data[5*i+j]),end=" ")69    print("|")70    for j in range(8):71      print("-",end="-")72    print("-")73def solver():74  total_set=[[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]]75  flagGlob=True76  while flagGlob:77    t_set=copy.deepcopy(total_set)78    for i in total_set:79      iter=copy.deepcopy(i)80      ans=find_empty(iter)81      if ans is None:82        flagGlob=False83        pass84      else:85        x,y=ans[0],ans[1]86        test=copy.deepcopy(iter)87        flag=True88        for j in ["0","1","2","3","4"]:89          dat=fill(x,y,j,iter)90          if dat:91            flag=False92            t_set.append(iter)93            iter=copy.deepcopy(test)94        t_set.remove(test)95    total_set=copy.deepcopy(t_set)      96  for i in total_set:97    print_table(i)98  print(len(total_set))99# The main recursive method100# to print all possible101# strings of length k102def printAllKLengthRec(char_set, prefix, n, k):103    global string_set104    # Base case: k is 0,105    # print prefix106    if k == 0:107        string_set.append(prefix)108        return109    # One by one add all characters110    # from set and recursively111    # call for k equals to k-1112    for i in range(n):113        # Next character of input added114        newPrefix = prefix + char_set[i]115        # k is decreased, because116        # we have added a new character117        printAllKLengthRec(char_set, newPrefix, n, k - 1)118for i in range(0,25):119  string_set.append(None)120#printAllKLength(["0","1","2","3","4"],2)121solver()122#iter=init_set123#dat=fill(0,0,"0",iter)124#print(iter)125#iter=string_set...sweep_lm_data.py
Source:sweep_lm_data.py  
1#!/usr/bin/env python32def set_data_based_on_shortname(args):3    def set_data(fmt, num_shards):4        if num_shards == 0:5            args.data = fmt.format(0)6        else:7            args.data = ":".join([fmt.format(i) for i in range(num_shards)])8    # mmap datasets9    if args.data == "CC-NEWS-en.v7.1":10        set_data("/private/home/myleott/data/data-bin/CC-NEWS-en.v7.1/shard{}", 10)11    elif args.data == "fb_posts":12        set_data("/data/tmp/fb_posts.en.2018-2019.bpe.mmap-bin/shard{}", 100)13    elif args.data == "fb_posts_gfs":14        set_data(15            "/mnt/vol/gfsai-flash2-east/ai-group/users/myleott/fb_posts/fb_posts.en.2018-2019.bpe.mmap-bin/shard{}",16            100,17        )18    elif args.data == "bookwiki_aml-mmap-bin":19        set_data("/data/tmp/bookwiki_aml-mmap-bin/shard{}", 5)20    elif args.data == "bookwiki_aml_CC-NEWS-en.v7.1":21        set_data("/data/tmp/bookwiki_aml_CC-NEWS-en.v7.1/shard{}", 5)22    # old datasets23    elif args.data == "CC-NEWS-en.v6":24        set_data("/private/home/myleott/data/data-bin/CC-NEWS-en.v6", 0)25    elif args.data == "CC-NEWS-en.v9":26        set_data(27            "/private/home/namangoyal/fairseq-py/data-bin/CC-NEWS-en.v9/shard{}", 10028        )29    elif args.data == "bookwiki":30        set_data("/private/home/myleott/data/data-bin/bookwiki.10shards/shard{}", 10)31    elif args.data == "bookwiki_full":32        set_data("/private/home/myleott/data/data-bin/bookwiki-bin", 0)33    elif args.data == "fb_posts_old":34        set_data("/data/tmp/mono.english.public.2018-2019.shard{}.sents.bpe-bin", 100)35    elif args.data == "fb_posts_gfs":36        set_data(37            "/mnt/vol/gfsai-flash2-east/ai-group/users/myleott/fb_posts/en/mono.english.public.2018-2019.shard{}.sents.bpe-bin",38            100,39        )40    elif args.data == "wmt19_en_news_docs":41        set_data(42            "/private/home/myleott/data/data-bin/wmt19_en_news_docs/wmt19_en_news_docs.bpe.shard{}",43            100,44        )45    else:46        set_data(args.data, 0)...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!!
