Best Python code snippet using tempest_python
create_random_goals.py
Source:create_random_goals.py  
1#!/usr/bin/env python2import commands3import getopt4import os5import yaml6import random7import sys8import math9NUM_GOALS = 1010MIN_DIST = 0.911MIN_DIST_INIT_OTHER = 0.512real = False13if real:14    MIN_DIST_INIT_OWN = 1.5  # was 2 for exps15    X_RANGE = [-1.2, 1.2]16    Y_RANGE = [-1.6, 1.6]17else:18    MIN_DIST_INIT_OWN = 219    X_RANGE = [-2.0, 2.0]20    Y_RANGE = [-2.0, 2.0]21MAX_TRIES_SINGLE = 100022MAX_TRIES_GLOBAL = 5023def create_position():24    res = dict()25    res['x'] = random.uniform(X_RANGE[0], X_RANGE[1])26    res['y'] = random.uniform(Y_RANGE[0], Y_RANGE[1])27    res['ang'] = random.uniform(0, 2 * math.pi)28    return res29def dist(pos_a, pos_b):30    return math.hypot(pos_a['x'] - pos_b['x'], pos_a['y'] - pos_b['y'])31class CreateRandomGoals(object):32    num_robots = None33    num_obstacles = None34    def __init__(self, argv):35        self.seed = 036        try:37            opts, args = getopt.getopt(argv, "n:o:s:", ["num_robots=", "num_obstacles=", "seed="])38        except getopt.GetoptError:39            print("wrong usage")40            sys.exit(2)41        for opt, arg in opts:42            if opt in ("-n", "--num_robots"):43                self.num_robots = int(arg)44            if opt in ("-o", "--num_obstacles"):45                self.num_obstacles = int(arg)46            if opt in ("-s", "--seed"):47                self.seed = int(arg)48        if not self.num_robots or not self.num_obstacles:49            print("wrong usage")50            sys.exit(2)51        random.seed(self.seed)52        yaml_file = dict()53        for n in range(self.num_robots):54            yaml_file["_".join(['robot', str(n)])] = dict()55        yaml_file['num_robots'] = self.num_robots56        yaml_file['num_obstacles'] = self.num_obstacles57        found = False58        for g in range(MAX_TRIES_GLOBAL):59            print("generating obstacles")60            self.created_obstacles = []61            for idx in range(self.num_robots):62                yaml_file["_".join(['robot', str(idx)])]['goals'] = list()63            for obstacle in range(self.num_obstacles):64                pos = None65                while not self.check_if_valid(pos, self.created_obstacles):66                    pos = create_position()67                self.created_obstacles.append(pos)68            for idx, obst in enumerate(self.created_obstacles):69                yaml_file["_".join(['obst', str(idx)])] = obst70            init_conf = None71            for g in range(NUM_GOALS + 1):72                current_conf = []73                for _ in range(MAX_TRIES_GLOBAL):74                    found = True75                    for n in range(self.num_robots):76                        found_single = False77                        for _ in range(MAX_TRIES_SINGLE):78                            pos = create_position()79                            if self.check_if_valid(pos, current_conf, init_conf, n):80                                current_conf.append(pos)81                                found_single = True82                                break83                        if not found_single:84                            found = False85                            break86                    if found:87                        break88                    else:89                        current_conf = []90                if not found:91                    break92                for idx, p in enumerate(current_conf):93                    if g == 0:94                        print("found init conf")95                        init_conf = current_conf96                        yaml_file["_".join(['robot', str(idx)])]['init_pose'] = p97                    else:98                        if real:99                            init_conf = current_conf100                        yaml_file["_".join(['robot', str(idx)])]['goals'].append(p)101            if found:102                break103        if not found:104            print("could not find configuration")105            sys.exit(2)106        self.output_dir = commands.getoutput('rospack find collvoid_stage')107        if self.seed == 0:108            file_name = os.path.join(self.output_dir,109                                     "_".join( ['goals', 'robots', str(self.num_robots), 'obstacles', str(self.num_obstacles), 'created.yaml']))110        else:111            file_name = os.path.join(self.output_dir,112                                     "_".join( ['goals', 'robots', str(self.num_robots), 'obstacles', str(self.num_obstacles), str(self.seed), 'created.yaml']))113        with open(file_name, 'w') as f:114            yaml.dump(yaml_file, f)115    def check_if_valid(self, pos, current_conf, init_conf=None, n=0):116        if pos is None:117            return False118        for pos_b in self.created_obstacles:119            if dist(pos, pos_b) < MIN_DIST:120                return False121        if init_conf is not None:122            if dist(pos, init_conf[n]) < MIN_DIST_INIT_OWN:123                return False124            for p in init_conf:125                if dist(pos, p) < MIN_DIST_INIT_OTHER:126                    return False127        for pos_b in current_conf:128            if dist(pos, pos_b) < MIN_DIST:129                return False130        return True131if __name__ == '__main__':...zmod4450_test.py
Source:zmod4450_test.py  
1from time import sleep2from smbus import SMBus3i2cbus = SMBus(1)4#device address5dev = 0x32            #renesas zmod44506#device registers on library7ZMOD4450_I2C_ADDRESS    = 0x328ZMOD44XX_ADDR_PID       = 0x009ZMOD44XX_ADDR_CONF      = 0x2010ZMOD44XX_ADDR_PROD_DATA = 0x2611ZMOD44XX_ADDR_CMD       = 0x9312ZMOD44XX_ADDR_STATUS    = 0x9413#device registers resque14ZMOD44XX_ADDR_PREPARE   = 0xB715ZMOD44XX_LEN_PID        = 216ZMOD44XX_LEN_CONF       = 617ZMOD44XX_LEN_PROD_DATA  = 518STATUS_SEQUENCER_RUNNING_MASK   = 0x80 # Sequencer is running */19STATUS_SLEEP_TIMER_ENABLED_MASK = 0x40 # SleepTimer_enabled */20STATUS_ALARM_MASK               = 0x20 # Alarm */21STATUS_LAST_SEQ_STEP_MASK       = 0x1F # Last executed sequencer step */22STATUS_POR_EVENT_MASK           = 0x80 # POR_event */23STATUS_ACCESS_CONFLICT_MASK     = 0x40 # AccessConflict */24#device configuration defaults25data_set_4450_continuous = [ 0x20, 0x04, 0x40, 0x09,26                             0x03, 0x00, 0x00, 0x80, 0x08 ]27data_set_4450i = [0x00, 0x28, 0xC3, 0xE3,28                  0x00, 0x00, 0x80, 0x40 ]29zmod4450_continuous = {30  'start': 0xC0,31  'h': {'addr': 0x40, 'len': 2},32  'd': {'addr': 0x50, 'len': 4, 'data': data_set_4450_continuous[0:4]},33  'm': {'addr': 0x60, 'len': 1, 'data': data_set_4450_continuous[4:5]},34  's': {'addr': 0x68, 'len': 4, 'data': data_set_4450_continuous[5:9]},35  'r': {'addr': 0x99, 'len': 2}36}37zmod44xxi = {38  'start': 0x80,39  'h': {'addr': 0x40, 'len': 2},40  'd': {'addr': 0x50, 'len': 2, 'data': data_set_4450i[0:2]},41  'm': {'addr': 0x60, 'len': 2, 'data': data_set_4450i[2:4]},42  's': {'addr': 0x68, 'len': 4, 'data': data_set_4450i[4:8]},43  'r': {'addr': 0x97, 'len': 4}44}45# continuos mode46init_conf = zmod44xxi47meas_conf = zmod4450_continuous48hspf = 049pid = 050config = 051product = 052mox_lr = 053mox_er = 054data = [0,0,0,0]55def read_byte(reg):56  return i2cbus.read_byte_data(dev, reg)57def write_byte(reg, data):58  return i2cbus.write_byte_data(dev, reg, data)59def write_bytes(reg, data, length):60  for i in range(length):61    write_byte(reg + i, data[i])62def read_bytes(reg, length):63  result = []64  for i in range(length):65    result.append(read_byte(reg + i))66  return result67def print_bytes(arr):68  print(list(map(hex,arr)))69def read_status():70  result = read_byte(ZMOD44XX_ADDR_STATUS)71  print('STATUS:', result)72  73def read_sensor_info():74  global pid, config, product75  print(write_byte(ZMOD44XX_ADDR_CMD, 0))76  sleep(0.2)77  pid = read_bytes(ZMOD44XX_ADDR_PID, ZMOD44XX_LEN_PID)78  print('PID:')79  print_bytes(pid)80  81  config = read_bytes(ZMOD44XX_ADDR_CONF, ZMOD44XX_LEN_CONF)82  print('CONF:')83  print_bytes(config)84  85  product = read_bytes(ZMOD44XX_ADDR_PROD_DATA, ZMOD44XX_LEN_PROD_DATA)86  print('PROD_DATA:')87  print_bytes(product)88  89def init_sensor():90  global hspf, data, mox_lr, mox_er91  result = read_byte(ZMOD44XX_ADDR_PREPARE)92  print('PREPARE:', result)93  94  hspf = (0-(config[2] * 256.0 + config[3]) * ((config[4] + 640.0) * (config[5] + 80.0) - 512000.0)) / 12288000.0;95  print('hspf:', hspf)96  if (0.0 > hspf) or (4096.0 < hspf):97    print('... init out of range')98  99  data[0] = int(hspf) >> 8100  data[1] = int(hspf) & 0x00FF101  write_bytes(init_conf['h']['addr'], data, init_conf['h']['len'])102  write_bytes(init_conf['d']['addr'], init_conf['d']['data'], init_conf['d']['len'])103  write_bytes(init_conf['m']['addr'], init_conf['m']['data'], init_conf['m']['len'])104  write_bytes(init_conf['s']['addr'], init_conf['s']['data'], init_conf['s']['len'])105  write_byte(ZMOD44XX_ADDR_CMD, init_conf['start'])106  sleep(0.02)107  read_status()108  109  result = read_bytes(init_conf['r']['addr'], init_conf['r']['len'])110  mox_lr = result[0] << 8 | result[1]111  mox_er = result[2] << 8 | result[3]112def init_measurement():113  global data114  result = read_byte(ZMOD44XX_ADDR_PREPARE)115  print('PREPARE:',result)116  hspf = (-(config[2] * 256.0 + config[3]) *117                 ((config[4] + 640.0) * (config[5] - 600.0) - 512000.0)) / 12288000.0;118  data[0] = int(hspf) >> 8119  data[1] = int(hspf) & 0x00FF120  write_bytes(meas_conf['h']['addr'], data, meas_conf['h']['len'])121  write_bytes(meas_conf['d']['addr'], meas_conf['d']['data'], meas_conf['d']['len'])122  write_bytes(meas_conf['m']['addr'], meas_conf['m']['data'], meas_conf['m']['len'])123  write_bytes(meas_conf['s']['addr'], meas_conf['s']['data'], meas_conf['s']['len'])124print('______________________________')125read_status()126read_sensor_info()127init_sensor()128init_measurement()129print('_______________________________')130# print(i2cbus.write_byte_data(dev1, test_add1, 0xff))131# print(i2cbus.write_byte_data(dev1, test_add2, 0xff))132# print(i2cbus.read_byte_data(dev1, test_add1))133# print(i2cbus.read_byte_data(dev1, test_add2))...base_sampling.py
Source:base_sampling.py  
...4    cls_args = []5    def __init__(self, *args, **kwargs):6        ...7    @classmethod8    def get_init_conf(cls, conf):9        if not cls.cls_args:10            cls.log.warning('No parameters to initialize ?')11            return dict12        init_conf = dict()13        for arg in cls.cls_args:14            if conf.get(arg):15                init_conf[arg] = conf.get(arg)16        return init_conf17    def run(self, df: DataFrame) -> DataFrame:...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!!
