How to use test_patch2 method in autotest

Best Python code snippet using autotest_python

day11.py

Source:day11.py Github

copy

Full Screen

1# Code for the 2018 AoC, day 112# https://adventofcode.com/2018/day/113# Michael Bell4# 12/11/20185from tqdm import tqdm6from joblib import Parallel, delayed7import sys8def dict_keymax(d):9 """10 Given a dictionary of arbitrary keys and integer values >= 0, return the key of the max11 item with the max value.12 """13 max_k = sys.maxsize * -114 max_v = sys.maxsize * -115 for k in d:16 v = d[k]17 if v > max_v:18 max_v = v19 max_k = k20 return max_k21def compute_power(x, y, sn):22 rack_id = x + 1023 power = rack_id * (rack_id * y + sn)24 power = int((power / 100) % 10) # Keep only the hundreds digit25 return power - 526def make_power_grid(nx, ny, sn):27 power_grid = []28 for y in range(ny):29 row = []30 for x in range(nx):31 row.append(compute_power(x+1, y+1, sn))32 power_grid.append(row)33 return power_grid34def get_patch(power_grid, x, y, nx=3, ny=3):35 patch = []36 for row in power_grid[(y - 1):(y - 1 + ny)]:37 patch_row = [val for val in row[(x - 1):(x - 1 + nx)]]38 patch.append(patch_row)39 40 return patch41def get_total_power(grid):42 total_power = 043 for row in grid:44 total_power += sum(row)45 return total_power46def find_max_power_patch(grid, patch_size=3):47 powers = {}48 slicer = slice(None, -patch_size+1) if patch_size > 1 else slice(None, None)49 for y, row in enumerate(grid[slicer]):50 for x, val in enumerate(row[slicer]):51 patch = get_patch(grid, x+1, y+1, patch_size, patch_size)52 powers[(x+1, y+1)] = get_total_power(patch)53 max_power_loc = dict_keymax(powers)54 max_power = max(powers.values())55 return (max_power_loc[0], max_power_loc[1], patch_size), max_power56def find_max_power_patch_any_size(grid):57 """58 This is super slow!59 """60 powers = Parallel(n_jobs=8)(61 delayed(find_max_power_patch)(grid, patch_size)62 for patch_size in tqdm(range(1, 301))63 )64 max_power_config = max(powers, key=lambda x: x[1])65 return max_power_config66if __name__ == '__main__':67 assert compute_power(3, 5, 8) == 468 assert compute_power(122, 79, 57) == -569 assert compute_power(217, 196, 39) == 070 assert compute_power(101, 153, 71) == 471 test_grid1 = make_power_grid(300, 300, 18)72 test_patch1 = get_patch(test_grid1, 33, 45)73 assert test_patch1[0] == [4, 4, 4] 74 assert test_patch1[-1] == [1, 2, 4]75 assert get_total_power(test_patch1) == 2976 test_grid2 = make_power_grid(300, 300, 42)77 test_patch2 = get_patch(test_grid2, 21, 61) 78 assert test_patch2[0] == [4, 3, 3]79 assert test_patch2[-1] == [3, 3, 4]80 assert get_total_power(test_patch2) == 3081 loc1, pwr1 = find_max_power_patch(test_grid1)82 assert (loc1[0], loc1[1]) == (33, 45) and pwr1 == 2983 loc1, pwr1 = find_max_power_patch(test_grid1, 16)84 assert (loc1[0], loc1[1]) == (90, 269) and pwr1 == 11385 # cfg1, pwr1 = find_max_power_patch_any_size(test_grid1)86 # assert cfg1 == (90,269,16) and pwr1 == 11387 loc2, pwr2 = find_max_power_patch(test_grid2)88 assert (loc2[0], loc2[1]) == (21, 61) and pwr2 == 3089 # cfg2, pwr2 = find_max_power_patch_any_size(test_grid2)90 # assert cfg2 == (232,251,12) and pwr2 == 11991 grid = make_power_grid(300, 300, 7347)92 loc, pwr = find_max_power_patch(grid)93 print(f"Solution 1: ({loc[0]}, {loc[1]}) (power={pwr})")94 cfg, pwr = find_max_power_patch_any_size(grid)...

Full Screen

Full Screen

test_oz.py

Source:test_oz.py Github

copy

Full Screen

1import unittest2from datetime import datetime3from unittest.mock import patch4from conversation.oz import is_oz_message, is_oz_on, respond_oz_message, log_to_oz5class TestOz(unittest.TestCase):6 def test_is_oz_on(self):7 # should not be enabled for test chat8 self.assertFalse(is_oz_on({"chat_id": "222772"}))9 @patch('conversation.oz.is_oz_enabled')10 def test_is_oz_calls_persistence(self, test_patch):11 test_patch.return_value = False12 self.assertFalse(is_oz_on({"chat_id": "222773"}))13 test_patch.assert_called_once()14 def test_is_oz_message(self):15 self.assertFalse(is_oz_message({"chat_id": "222772", "text": "oz oz"}))16 self.assertFalse(is_oz_message({"chat_id": "222773", "text": "oz oz"}))17 self.assertTrue(is_oz_message({"chat_id": "798772222", "text": "oz oz"}))18 self.assertFalse(is_oz_message({"chat_id": "798772222", "text": "boz boz"}))19 @patch('integration.integration.send_reply_message')20 def test_log_to_oz(self, test_patch):21 message = TestOz._create_message()22 log_to_oz(message)23 message["chat_id"] = "798772222"24 test_patch.assert_called_once_with(message, "222772 1234 foo", {"move": "oz ask"})25 @patch('conversation.oz.enabled_oz')26 @patch('integration.integration.send_reply_message')27 def test_respond_oz_message_on(self, test_patch1, test_patch2):28 respond_oz_message({"text": "oz on"})29 test_patch1.assert_called_once()30 test_patch2.assert_called_once()31 @patch('conversation.oz.disable_oz')32 @patch('integration.integration.send_reply_message')33 def test_respond_oz_message_on(self, test_patch1, test_patch2):34 respond_oz_message({"text": "oz off"})35 test_patch1.assert_called_once()36 test_patch2.assert_called_once()37 @patch('conversation.oz.get_last_request')38 @patch('conversation.oz.get_message')39 @patch('integration.integration.send_reply_message')40 def test_respond_oz_message_fake(self, patch_send_reply_message, patch_get_message, patch_get_last):41 old_message = TestOz._create_message()42 patch_get_message.return_value = old_message43 patch_get_last.return_value = old_message44 respond_oz_message({"text": "oz 222772 1234 text bar bar"})45 patch_get_message.assert_called_once_with("222772", "1234")46 patch_get_last.assert_called_once_with("222772")47 patch_send_reply_message.assert_called_once_with(old_message, "bar bar", {"move": "oz text"}, False)48 @staticmethod49 def _create_message():50 return {"chat_id": "222772", "msg_id": "1234", "text": "foo", "language": "en", "date": datetime.utcnow()}51if __name__ == '__main__':...

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 autotest 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