class DemoTest(unittest.TestCase): def setUp(self): self.host_url = r'http://127.0.0.1:12356' self.comm = Common(self.host_url) @unittest.skip("demonstrating skipping") def test_nothing(self): self.fail("shouldn't happen") def test_index(self): uri_index = '/' respanse_index_test = self.comm.get(uri_index) self.assertEqual( respanse_index_test.text, 'please input your username(your english name) and password(your english name)' ) def test_login(self): uri = r'/login' username = '******' password = '******' payload = 'username='******'&password='******'please select One Equipment:\n10001:Knife\n10002:Big Sword\n10003:KuiHuaBaoDian' ) def tearDown(self): pass
class SampleTest(unittest.TestCase): def setUp(self): self.host_url = r'http://127.0.0.1:12356' self.comm = Common(self.host_url) def test_selectEq(self): uri_selectEq = r'/selectEq' equipmentid = '10003' payload_selectEq = 'equipmentid=' + equipmentid res_selectEq = self.comm.post(uri_selectEq, payload_selectEq) self.assertEqual( res_selectEq.text, '{"equipmentid": "10003", "Message": "your pick up equipmentid:10003 please select your enemyid:\\n20001:Terran\\n20002:ORC\\n20003:Undead"}' ) def test_kill(self): equipmentid = '10003' enmyid = '20001' uri_kill = '/kill' payload_enmyid = 'enmyid=' + enmyid + '&equipmentid=' + equipmentid res_enmyid = self.comm.post(uri_kill, payload_enmyid) self.assertEqual(res_enmyid.text, 'Error 9904: Your kill yourself!!') def tearDown(self): pass
def __init__(self): self.driver = WebDriver() self.driver.implicitly_wait(2) self.driver.get(Config.main_page) self.common = Common(self) self.stf = Stf(self) self.common.get_seed() self.common.write_seed_to_file()
class Application: def __init__(self): self.driver = WebDriver() self.driver.implicitly_wait(2) self.driver.get(Config.main_page) self.common = Common(self) self.stf = Stf(self) self.common.get_seed() self.common.write_seed_to_file() def destroy(self): self.driver.close() self.driver.quit()
def content_cost(input, target): # First normalize both the input and target (preprocess for VGG16) input_norm = normalize_batch(input) target_norm = normalize_batch(target) input_layers = Common.forward_vgg(input_norm, False) target_layers = Common.forward_vgg(target_norm, False) accumulated_loss = 0 for layer in range(len(input_layers)): accumulated_loss = accumulated_loss + torch.mean( torch.square(input_layers[layer] - target_layers[layer])) return accumulated_loss
def test_common(): """this is not a real test It's just for see print statements. Try exchange del and print statement positions """ obj = Common() del obj print('hi')
def style_cost(input, target): # First normalize both the input and target (preprocess for VGG16) input_norm = normalize_batch(input) target_norm = normalize_batch(target) input_layers = Common.forward_vgg(input_norm, True) target_layers = Common.forward_vgg(target_norm, True) # layer weights layer_weights = [0.3, 0.7, 0.7, 0.3] # The accumulated losses for the style accumulated_loss = 0 for layer in range(len(input_layers)): accumulated_loss = accumulated_loss + layer_weights[layer] * \ torch.mean(torch.square(compute_gram(input_layers[layer]) - compute_gram(target_layers[layer]))) return accumulated_loss
def show_img(img): # Convert to tensor img = torch.from_numpy(img.reshape(1, 3, 256, 256)).float() # Put through network gen_img = Common.transformation_net(img) gen_img = gen_img.detach().numpy() # Clip the floats gen_img = np.clip(gen_img, 0, 255) # Convert to ints (for images) gen_img = gen_img.astype('uint8') gen_img = gen_img.reshape(3, 256, 256).transpose(1, 2, 0) # Show the image plt.imshow(gen_img) plt.show()
def training_show_img(): # Get an image from the validation set img = load_training_batch(0, 10, 'val')[4] # Convert to tensor train_img = torch.from_numpy(img.reshape(1, 3, 256, 256)).float() # Put through network gen_img = Common.transformation_net(train_img) gen_img = gen_img.detach().numpy() # Clip the floats gen_img = np.clip(gen_img, 0, 255) # Convert to ints (for images) gen_img = gen_img.astype('uint8') gen_img = gen_img.reshape(3, 256, 256).transpose(1, 2, 0) # Show the image plt.imshow(gen_img) plt.show()
def setUp(self): self.host_url = r'http://127.0.0.1:12356' self.comm = Common(self.host_url)
from src.common import Common # 实例化Common host_url = r'http://127.0.0.1:12356' comm = Common(host_url) # 访问首页 uri_index = '/' res_index = comm.get(uri_index) print('访问首页' + res_index.text) # 存储战场的登录 uri = r'/login' username = '******' password = '******' payload = 'username='******'&password='******'/selectEq' equipmentid = '10003' payload_selectEq = 'equipmentid=' + equipmentid res_selectEq = comm.post(uri_selectEq, payload_selectEq) print(u'存储战场的武器选择' + res_selectEq.text) # 存储战场另一个武器选择 enmyid = '20001' uri_kill = '/kill' payload_enmyid = 'enmyid=' + enmyid + '&equipmentid=' + equipmentid
def test_str(): obj = Common() assert str(obj) == 'Common'
def test_repr(): obj = Common() assert repr(obj) == '<Common>'
# The content batch is the same as the train batch, except train batch has noise added to it train_batch = load_training_batch(batch, Common.BATCH_SIZE, 'train') content_batch = np.copy(train_batch) # Add noise to the training batch train_batch = add_noise(train_batch) # Convert the batches to tensors train_batch = torch.from_numpy(train_batch).float() content_batch = torch.from_numpy(content_batch).float() # Zero the gradients opt.zero_grad() # Forward propagate gen_images = Common.transformation_net(train_batch) # Compute loss loss = total_cost(gen_images, [content_batch, Common.STYLE_IMAGE_TENSOR]) # Backprop loss.backward() # Clip the gradient to minimize chance of exploding gradients torch.nn.utils.clip_grad_norm_(Common.transformation_net.parameters(), 1.0) # Apply gradients opt.step()
def test_get_stock_codes_from_tse(self): stock_codes = Common.get_stock_codes_from_tse((2017, 9, 7)) self.assertEqual(912, len(stock_codes))
def test_is_in_future(self): tomorrow_obj = datetime.date.today() + datetime.timedelta(1) self.assertTrue(Common.is_in_future(tomorrow_obj)) with self.assertRaises(TypeError): Common.is_in_future('abc')