How to use test_encoding method in Molotov

Best Python code snippet using molotov_python

process.py

Source:process.py Github

copy

Full Screen

1import os2import torch3from transformers import BertTokenizer4from sklearn.model_selection import train_test_split5from src.utils.config import config6from src.utils.util import save_pkl, load_pkl7def process_text(question1, question2, label):8 """9 处理训练的数据10 :param question1:11 :param question2:12 :param label:13 :return:14 """15 tokenizer = BertTokenizer.from_pretrained(config.bert_base_path)16 train, test = [], []17 if os.path.exists(config.train_path):18 train = load_pkl(config.train_path, 'train_data')19 test = load_pkl(config.test_path, 'test_data')20 else:21 q1_train, q1_val, q2_train, q2_val, train_label, test_label = train_test_split(question1, question2, label,22 test_size=0.2, stratify=label)23 # train_data = tokenizer.batch_encode_plus(q1_train, q2_train, truncation=True, padding=True, max_length=375)24 # train_encoding.append(train_data['input_ids'])25 # train_encoding.append(train_data['token_type_ids'])26 # train_encoding.append(train_data['attention_mask'])27 #28 # test_data = tokenizer.batch_encode_plus(q1_val, q2_val, truncation=True, padding=True, max_length=375)29 # test_encoding.append(test_data['input_ids'])30 # test_encoding.append(test_data['token_type_ids'])31 # test_encoding.append(test_data['attention_mask'])32 for i in range(len(q1_train)):33 train_encoding = []34 train_data = tokenizer.encode_plus(str(q1_train[i]), str(q2_train[i]), truncation=True,35 padding=True, max_length=200)36 train_encoding.append(train_data['input_ids'])37 train_encoding.append(train_data['token_type_ids'])38 train_encoding.append(train_data['attention_mask'])39 train_encoding.append(int(train_label[i]))40 train.append(train_encoding)41 for i in range(len(q1_val)):42 test_encoding = []43 test_data = tokenizer.encode_plus(str(q1_val[i]), str(q2_val[i]), truncation=True,44 padding=True, max_length=200)45 test_encoding.append(test_data['input_ids'])46 test_encoding.append(test_data['token_type_ids'])47 test_encoding.append(test_data['attention_mask'])48 test_encoding.append(int(test_label[i]))49 test.append(test_encoding)50 save_pkl(config.train_path, train, 'train_data', use_bert=True)51 save_pkl(config.test_path, test, 'test_data', use_bert=True)52 return train, test53def process_pre_text(question1, question2):54 """55 处理预测数据56 :param question1:57 :param question2:58 :return:59 """60 tokenizer = BertTokenizer.from_pretrained(config.bert_base_path)61 train_encoding = []62 train_data = tokenizer.encode_plus(question1, question2, truncation=True, padding=True, max_length=200)63 train_encoding.append(train_data['input_ids'])64 train_encoding.append(train_data['token_type_ids'])65 train_encoding.append(train_data['attention_mask'])66 return [train_encoding]67def process_batch_pre_text(question1, question2):68 """69 处理批量数据70 """71 tokenizer = BertTokenizer.from_pretrained(config.bert_base_path)72 all_data = []73 for i in range(len(question1)):74 train_encoding = []75 train_data = tokenizer.encode_plus(str(question1[i]), str(question2[i]), truncation=True, padding=True,76 max_length=200)77 train_encoding.append(train_data['input_ids'])78 train_encoding.append(train_data['token_type_ids'])79 train_encoding.append(train_data['attention_mask'])80 all_data.append(train_encoding)...

Full Screen

Full Screen

run.py

Source:run.py Github

copy

Full Screen

1# -*- coding: latin-1 -*-2from pysys.constants import *3from pysys.basetest import BaseTest4import io, locale5# contains a non-ascii � character that is different in utf-8 vs latin-16TEST_STR = u'Hello � world' 7# use a different encoding to the default/local encoding8TEST_ENCODING = 'latin-1' if PREFERRED_ENCODING.lower() == 'utf-8' else 'utf-8'9class PySysTest(BaseTest):10 def execute(self):11 self.log.info('Python local/default/preferred encoding is %s; will test with non-local encoding %s', PREFERRED_ENCODING, TEST_ENCODING)12 if PREFERRED_ENCODING in ['ANSI_X3.4-1968', 'ascii']: self.skipTest('cannot run in ASCII locale')13 self.__myDefaultEncoding = None14 self.write_text('test-nonlocal.txt', os.linesep.join([TEST_STR, TEST_STR, 'otherstring']), encoding=TEST_ENCODING)15 def validate(self):16 if TEST_ENCODING == 'utf-8': # can't even read the file without an exception on utf8 systems17 self.assertGrep('test-nonlocal.txt', expr=TEST_STR, contains=False) # without encoding arg, won't work18 self.assertLineCount('test-nonlocal.txt', expr=TEST_STR, condition='==2', encoding=TEST_ENCODING)19 self.assertGrep('test-nonlocal.txt', expr=TEST_STR, contains=True, encoding=TEST_ENCODING)20 self.waitForGrep('test-nonlocal.txt', expr=TEST_STR, condition='==2', timeout=2, abortOnError=True, encoding=TEST_ENCODING)21 self.assertLastGrep('test-nonlocal.txt', expr=TEST_STR, contains=True, ignores=['^$', 'otherstring'], encoding=TEST_ENCODING)22 self.assertOrderedGrep('test-nonlocal.txt', exprList=[TEST_STR, TEST_STR], encoding=TEST_ENCODING)23 self.assertTrue(self.logFileContents('test-nonlocal.txt', encoding=TEST_ENCODING))24 self.assertDiff('test-nonlocal.txt', 'test-nonlocal.txt', filedir1=self.output, filedir2=self.output, encoding=TEST_ENCODING)25 self.assertThat('%s==%s', repr(TEST_STR), repr(self.getExprFromFile('test-nonlocal.txt', TEST_STR, encoding=TEST_ENCODING)))26 27 self.log.info('')28 self.log.info('now testing using getDefaultFileEncoding:')29 self.__myDefaultEncoding = TEST_ENCODING30 self.assertLineCount('test-nonlocal.txt', expr=TEST_STR, condition='==2')31 self.assertGrep('test-nonlocal.txt', expr=TEST_STR, contains=True)32 self.waitForGrep('test-nonlocal.txt', expr=TEST_STR, condition='==2', timeout=2, abortOnError=True)33 self.assertLastGrep('test-nonlocal.txt', expr=TEST_STR, contains=True, ignores=['^$', 'otherstring'])34 self.assertOrderedGrep('test-nonlocal.txt', exprList=[TEST_STR, TEST_STR])35 self.assertTrue(self.logFileContents('test-nonlocal.txt'))36 self.assertDiff('test-nonlocal.txt', 'test-nonlocal.txt', filedir1=self.output, filedir2=self.output)37 self.assertThat('%s==%s', repr(TEST_STR), repr(self.getExprFromFile('test-nonlocal.txt', TEST_STR)))38 39 40 def getDefaultFileEncoding(self, file, **xargs):41 if self.__myDefaultEncoding != None:42 self.log.info(' called getDefaultFileEncoding for %s with %s', file, xargs)43 return self.__myDefaultEncoding44 ...

Full Screen

Full Screen

test_encoding.py

Source:test_encoding.py Github

copy

Full Screen

...41 client = TestClient(run_panini)42 client.start()43 yield client44 client.stop()45def test_encoding(client):46 response = client.request("test_encoding.foo", {"data": "some correct data"})47 assert response["len"] == 1748 response = client.request("test_encoding.foo", {"data": "не латинские символы"})49 assert response["len"] == 2050def test_correct_message_format(client):51 response = client.request("test_encoding.correct", {"data": "some data"})52 assert response["success"] is True53def test_incorrect_message_format(client):54 with pytest.raises(OSError):55 client.request("test_encoding.message.correct", {"data": "some data"})56 with pytest.raises(OSError):...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Molotov automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful