Best Python code snippet using robotframework
request.py
Source:request.py
1# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)2# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php3import os4import re5import sys6import urlparse7import urllib8from command import Command, BadCommand9from paste.deploy import loadapp, loadserver10from paste.wsgilib import raw_interactive11class RequestCommand(Command):12 min_args = 213 usage = 'CONFIG_FILE URL [OPTIONS/ARGUMENTS]'14 takes_config_file = 115 summary = "Run a request for the described application"16 description = """\17 This command makes an artifical request to a web application that18 uses a paste.deploy configuration file for the server and19 application.20 Use 'paster request config.ini /url' to request /url. Use21 'paster post config.ini /url < data' to do a POST with the given22 request body.23 If the URL is relative (doesn't begin with /) it is interpreted as24 relative to /.command/. The variable environ['paste.command_request']25 will be set to True in the request, so your application can distinguish26 these calls from normal requests.27 Note that you can pass options besides the options listed here; any unknown28 options will be passed to the application in environ['QUERY_STRING'].29 """30 31 parser = Command.standard_parser(quiet=True)32 parser.add_option('-n', '--app-name',33 dest='app_name',34 metavar='NAME',35 help="Load the named application (default main)")36 parser.add_option('--config-var',37 dest='config_vars',38 metavar='NAME:VALUE',39 action='append',40 help="Variable to make available in the config for %()s substitution "41 "(you can use this option multiple times)")42 parser.add_option('--header',43 dest='headers',44 metavar='NAME:VALUE',45 action='append',46 help="Header to add to request (you can use this option multiple times)")47 parser.add_option('--display-headers',48 dest='display_headers',49 action='store_true',50 help='Display headers before the response body')51 ARG_OPTIONS = ['-n', '--app-name', '--config-var', '--header']52 OTHER_OPTIONS = ['--display-headers']53 54 ## FIXME: some kind of verbosity?55 ## FIXME: allow other methods than POST and GET?56 _scheme_re = re.compile(r'^[a-z][a-z]+:', re.I)57 def command(self):58 vars = {}59 app_spec = self.args[0]60 url = self.args[1]61 url = urlparse.urljoin('/.command/', url)62 if self.options.config_vars:63 for item in self.option.config_vars:64 if ':' not in item:65 raise BadCommand(66 "Bad option, should be name:value : --config-var=%s" % item)67 name, value = item.split(':', 1)68 vars[name] = value69 headers = {}70 if self.options.headers:71 for item in self.options.headers:72 if ':' not in item:73 raise BadCommand(74 "Bad option, should be name:value : --header=%s" % item)75 name, value = item.split(':', 1)76 headers[name] = value.strip()77 if not self._scheme_re.search(app_spec):78 app_spec = 'config:'+app_spec79 if self.options.app_name:80 if '#' in app_spec:81 app_spec = app_spec.split('#', 1)[0]82 app_spec = app_spec + '#' + options.app_name83 app = loadapp(app_spec, relative_to=os.getcwd(), global_conf=vars)84 if self.command_name.lower() == 'post':85 request_method = 'POST'86 else:87 request_method = 'GET'88 qs = []89 for item in self.args[2:]:90 if '=' in item:91 item = urllib.quote(item.split('=', 1)[0]) + '=' + urllib.quote(item.split('=', 1)[1])92 else:93 item = urllib.quote(item)94 qs.append(item)95 qs = '&'.join(qs)96 97 environ = {98 'REQUEST_METHOD': request_method,99 ## FIXME: shouldn't be static (an option?):100 'CONTENT_TYPE': 'text/plain',101 'wsgi.run_once': True,102 'wsgi.multithread': False,103 'wsgi.multiprocess': False,104 'wsgi.errors': sys.stderr,105 'QUERY_STRING': qs,106 'HTTP_ACCEPT': 'text/plain;q=1.0, */*;q=0.1',107 'paste.command_request': True,108 }109 if request_method == 'POST':110 environ['wsgi.input'] = sys.stdin111 environ['CONTENT_LENGTH'] = '-1'112 for name, value in headers.items():113 if name.lower() == 'content-type':114 name = 'CONTENT_TYPE'115 else:116 name = 'HTTP_'+name.upper().replace('-', '_')117 environ[name] = value118 119 status, headers, output, errors = raw_interactive(app, url, **environ)120 assert not errors, "errors should be printed directly to sys.stderr"121 if self.options.display_headers:122 for name, value in headers:123 sys.stdout.write('%s: %s\n' % (name, value))124 sys.stdout.write('\n')125 sys.stdout.write(output)126 sys.stdout.flush()127 status_int = int(status.split()[0])128 if status_int != 200:129 return status_int130 def parse_args(self, args):131 if args == ['-h']:132 Command.parse_args(self, args)133 return134 # These are the arguments parsed normally:135 normal_args = []136 # And these are arguments passed to the URL:137 extra_args = []138 # This keeps track of whether we have the two required positional arguments:139 pos_args = 0140 while args:141 start = args[0]142 if not start.startswith('-'):143 if pos_args < 2:144 pos_args += 1145 normal_args.append(start)146 args.pop(0)147 continue148 else:149 normal_args.append(start)150 args.pop(0)151 continue152 else:153 found = False154 for option in self.ARG_OPTIONS:155 if start == option:156 normal_args.append(start)157 args.pop(0)158 if not args:159 raise BadCommand(160 "Option %s takes an argument" % option)161 normal_args.append(args.pop(0))162 found = True163 break164 elif start.startswith(option+'='):165 normal_args.append(start)166 args.pop(0)167 found = True168 break169 if found:170 continue171 if start in self.OTHER_OPTIONS:172 normal_args.append(start)173 args.pop(0)174 continue175 extra_args.append(start)176 args.pop(0)177 Command.parse_args(self, normal_args)178 # Add the extra arguments back in:179 self.args = self.args + extra_args...
regent.py
Source:regent.py
1#!/usr/bin/env python2# Copyright 2018 Stanford University3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15#16from __future__ import print_function17import json, os, platform, subprocess, sys18def load_json_config(filename):19 try:20 with open(filename, 'r') as f:21 return json.load(f)22 except IOError:23 return None24os_name = platform.system()25# Find Regent.26regent_dir = os.path.dirname(os.path.realpath(__file__))27terra_dir = os.path.join(regent_dir, 'terra')28# Find Legion (in the environment, or relative to Regent).29if 'LG_RT_DIR' in os.environ:30 runtime_dir = os.path.realpath(os.environ['LG_RT_DIR'])31else:32 runtime_dir = os.path.join(os.path.dirname(regent_dir), 'runtime')33bindings_dir = os.path.join(os.path.dirname(runtime_dir), 'bindings', 'regent')34# Find CUDA.35if 'CUDA' in os.environ:36 cuda_dir = os.path.realpath(os.environ['CUDA'])37elif 'CUDATOOLKIT_HOME' in os.environ:38 cuda_dir = os.path.realpath(os.environ['CUDATOOLKIT_HOME'])39else:40 cuda_dir = None41cuda_include_dir = os.path.join(cuda_dir, 'include') if cuda_dir is not None else None42# Find RDIR.43if 'USE_RDIR' in os.environ:44 use_rdir = os.environ['USE_RDIR']45else:46 rdir_config_filename = os.path.join(regent_dir, '.rdir.json')47 rdir = load_json_config(rdir_config_filename)48 use_rdir = '1' if rdir in ['auto', 'manual'] else '0'49# Detect use of CMake.50if 'USE_CMAKE' in os.environ:51 cmake = os.environ['USE_CMAKE'] == '1'52else:53 cmake_config_filename = os.path.join(regent_dir, '.cmake.json')54 cmake = load_json_config(cmake_config_filename)55cmake_build_dir = os.path.join(regent_dir, 'build')56include_path = (57 (os.environ['INCLUDE_PATH'].split(';')58 if 'INCLUDE_PATH' in os.environ else []) +59 [bindings_dir,60 runtime_dir,61])62if cuda_include_dir is not None:63 include_path.append(cuda_include_dir)64LD_LIBRARY_PATH = 'LD_LIBRARY_PATH'65if os_name == 'Darwin':66 LD_LIBRARY_PATH = 'DYLD_LIBRARY_PATH'67lib_path = (68 (os.environ[LD_LIBRARY_PATH].split(':')69 if LD_LIBRARY_PATH in os.environ else []) +70 [os.path.join(terra_dir, 'build'),71 (os.path.join(cmake_build_dir, 'lib') if cmake else bindings_dir)])72def root_dir():73 return os.path.dirname(runtime_dir)74def regent(args, env = {}, **kwargs):75 terra_exe = os.path.join(terra_dir, 'terra')76 if not os.path.exists(terra_exe):77 terra_exe = os.path.join(terra_dir, 'bin', 'terra')78 if 'TERRA_PATH' in os.environ:79 terra_path = os.environ['TERRA_PATH'].split(';')80 else:81 terra_path = []82 normal_args = [arg for arg in args if not arg.startswith('-')]83 terra_path += (84 ['?.t', '?.rg'] +85 ([os.path.join(os.path.dirname(os.path.realpath(normal_args[0])), '?.t'),86 os.path.join(os.path.dirname(os.path.realpath(normal_args[0])), '?.rg')]87 if len(normal_args) >= 1 and os.path.exists(normal_args[0]) else []) +88 [os.path.join(regent_dir, 'src', '?.t'),89 os.path.join(regent_dir, 'src', 'rdir', 'plugin', 'src', '?.t'),90 os.path.join(terra_dir, 'tests', 'lib', '?.t'),91 os.path.join(terra_dir, 'release', 'include', '?.t'),92 os.path.join(bindings_dir, '?.t')])93 terra_env = {94 'TERRA_PATH': ';'.join(terra_path),95 LD_LIBRARY_PATH: ':'.join(lib_path),96 'INCLUDE_PATH': ';'.join(include_path),97 'LG_RT_DIR': runtime_dir,98 'USE_CMAKE': '1' if cmake else '0',99 'CMAKE_BUILD_DIR': cmake_build_dir,100 'USE_RDIR': use_rdir,101 }102 if cuda_dir is not None:103 terra_env['CUDA_HOME'] = cuda_dir104 cmd = []105 if 'LAUNCHER' in os.environ:106 cmd = cmd + (os.environ['LAUNCHER'].split()107 if 'LAUNCHER' in os.environ else [])108 cmd = cmd + [terra_exe] + args109 cmd_env = dict(os.environ.items())110 cmd_env.update(terra_env)111 cmd_env.update(env)112 return subprocess.Popen(113 cmd, env = cmd_env, **kwargs)114if __name__ == '__main__':...
preprocssing.py
Source:preprocssing.py
1import os2import pandas as pd3import yaml4import argparse5from src.data.normalize_text import normalize6from src.data.dictionary import build_vocab7def load_data_from_dir(dir_path, normal_args=None, sent_dict=None, topic_dict=None):8 sent_path = dir_path + '/sentiments.txt'9 text_path = dir_path + '/sents.txt'10 topic_path = dir_path + '/topics.txt'11 i2sent = None12 i2topic = None13 if sent_dict is not None:14 i2sent = {i: sent for sent, i in sent_dict.items()}15 if topic_dict is not None:16 i2topic = {i: topic for topic, i in topic_dict.items()}17 if normal_args is None:18 normal_args = {}19 with open(text_path, 'r', encoding='utf-8') as pf:20 texts = pf.readlines()21 texts = [normalize(s, **normal_args) for s in texts]22 with open(sent_path, 'r', encoding='utf-8') as pf:23 sents = pf.readlines()24 sents = [int(i) for i in sents]25 if i2sent is not None:26 sents = [i2sent[i] for i in sents]27 with open(topic_path, 'r', encoding='utf-8') as pf:28 topics = pf.readlines()29 topics = [int(i) for i in topics]30 if i2topic is not None:31 topics = [i2topic[i] for i in topics]32 df = pd.DataFrame({"text": texts, "sentiment": sents, "topic": topics})33 return df34if __name__ == '__main__':35 # load configs36 parser = argparse.ArgumentParser()37 parser.add_argument("--config_path", "-cp", default='configs/configs.yaml', help="path to configs")38 args = parser.parse_args()39 with open(args.config_path, 'r') as pf:40 configs = yaml.load(pf, Loader=yaml.FullLoader)41 normal_args = configs['DATA']['NORMALIZE']42 raw_path = configs['DATA']['RAW']['data_path']43 gen_cf = configs['DATA']['GENERAL']44 # sent_dict45 sent_dict = configs['DATA']['LABEL']['sentiment']46 topic_dict = configs['DATA']['LABEL']['topic']47 # path configure48 data_path = gen_cf['data_path']49 train_name = gen_cf['train_name']50 val_name = gen_cf['val_name']51 test_name = gen_cf['test_name']52 train_path = data_path + '/' + train_name53 val_path = data_path + '/' + val_name54 test_path = data_path + '/' + test_name55 # load raw data56 train_df = load_data_from_dir(raw_path + '/train', normal_args, sent_dict, topic_dict)57 val_df = load_data_from_dir(raw_path + '/dev', normal_args, sent_dict, topic_dict)58 test_df = load_data_from_dir(raw_path + '/test', normal_args, sent_dict, topic_dict)59 # save data60 if os.path.exists(data_path) is False:61 os.mkdir(data_path)62 train_df.to_csv(train_path, index=False)63 val_df.to_csv(val_path, index=False)64 test_df.to_csv(test_path, index=False)65 # create vocabulary66 texts = train_df['text'].tolist()67 texts.extend(val_df['text'].tolist())68 seq_vocab, max_seq_len, max_char_len = build_vocab(texts, thresh_num=gen_cf['thresh_count_word'])69 seq_vocab.save(gen_cf['vocab_path'])70 print("Max sequence length: ", max_seq_len)...
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!!