Best Python code snippet using fMBT_python
test_hopfield.py
Source:test_hopfield.py
...13 KeyError,14 network.set_diagonal_weights_rule,15 *("asd",)16 )17 def test_det_net_set_patterns(self):18 patterns = utils.generate_n_random_patterns(3, 5)19 network = pr.DeterministicHopfieldNetwork()20 network.set_patterns(patterns)21 expected = (network.patterns == patterns).all()22 self.assertTrue(expected)23 def test_det_net_generate_weights_test_case_0(self):24 patterns = np.array([[1, 2], [3, 4]])25 network = pr.DeterministicHopfieldNetwork()26 network.set_diagonal_weights_rule("zero")27 network.set_patterns(patterns)28 network.generate_weights()29 expected_weights = np.array([[0, 7], [7, 0]])30 expected = (network.weights == expected_weights).all()31 self.assertTrue(expected)32 def test_det_net_generate_weights_test_case_1(self):33 patterns = np.array([[1, 2], [3, 4]])34 network = pr.DeterministicHopfieldNetwork()35 network.set_diagonal_weights_rule("non-zero")36 network.set_patterns(patterns)37 network.generate_weights()38 expected_weights = np.array([[5, 7], [7, 10]])39 expected = (network.weights == expected_weights).all()40 self.assertTrue(expected)41 def test_det_net_generate_weights_test_case_2(self):42 patterns = np.array([[-1, 1, 1], [1, -1, 1]])43 network = pr.DeterministicHopfieldNetwork()44 network.set_diagonal_weights_rule("zero")45 network.set_patterns(patterns)46 network.generate_weights()47 expected_weights = 1/3 * np.array([[0, -2, 0], [-2, 0, 0], [0, 0, 0]])48 expected = (network.weights == expected_weights).all()49 self.assertTrue(expected)50 def test_det_net_generate_weights_test_case_3(self):51 patterns = np.array([[-1, 1, 1], [1, -1, 1]])52 network = pr.DeterministicHopfieldNetwork()53 network.set_diagonal_weights_rule("non-zero")54 network.set_patterns(patterns)55 network.generate_weights()56 expected_weights = 1/3 * np.array([[2, -2, 0], [-2, 2, 0], [0, 0, 2]])57 expected = (network.weights == expected_weights).all()58 self.assertTrue(expected)59 def test_det_net_generate_weights_shape(self):60 n_patterns = 1061 n_bits = 562 patterns = utils.generate_n_random_patterns(n_patterns, n_bits)63 network = pr.DeterministicHopfieldNetwork()64 network.set_diagonal_weights_rule("zero")65 network.set_patterns(patterns)66 network.generate_weights()67 self.assertEqual(network.weights.shape, (n_bits, n_bits))68 def test_det_net_generate_weights_diagonal(self):69 n_patterns = 670 n_bits = 10071 patterns = utils.generate_n_random_patterns(n_patterns, n_bits)72 network = pr.DeterministicHopfieldNetwork()73 network.set_patterns(patterns)74 network.set_diagonal_weights_rule("zero")75 network.generate_weights()76 diagonal = network.weights.diagonal()77 self.assertFalse(np.any(diagonal))78 def test_det_net_update_neuron_shape(self):79 n_patterns = 580 n_bits = 10081 patterns = utils.generate_n_random_patterns(n_patterns, n_bits)82 network = pr.DeterministicHopfieldNetwork()83 network.set_patterns(patterns)84 network.set_diagonal_weights_rule("zero")85 network.generate_weights()86 original_pattern = patterns[3, :]87 neuron_index = 1088 updated_pattern = network.update_neuron(89 original_pattern, neuron_index)90 self.assertEqual(updated_pattern.shape, original_pattern.shape)91 def test_det_net_update_neuron_difference(self):92 n_patterns = 1093 n_bits = 3294 patterns = utils.generate_n_random_patterns(n_patterns, n_bits)95 network = pr.DeterministicHopfieldNetwork()96 network.set_diagonal_weights_rule("zero")97 network.set_patterns(patterns)98 network.generate_weights()99 original_pattern = patterns[1, :]100 neuron_index = 3101 updated_pattern = network.update_neuron(102 original_pattern, neuron_index)103 n_differences = sum(updated_pattern != original_pattern)104 self.assertTrue(n_differences <= 1)105 def test_det_net_get_state_of_local_field(self):106 net = pr.DeterministicHopfieldNetwork()107 self.assertEqual(net.get_state_of_local_field(0.1), 1)108 self.assertEqual(net.get_state_of_local_field(0), 1)109 self.assertEqual(net.get_state_of_local_field(-1), -1)110 def test_det_net_update_random_neuron(self):111 n_patterns = 20112 n_bits = 100113 patterns = utils.generate_n_random_patterns(n_patterns, n_bits)114 network = pr.DeterministicHopfieldNetwork()115 network.set_patterns(patterns)116 network.set_diagonal_weights_rule("zero")117 network.generate_weights()118 original_pattern = patterns[15, :]119 updated_pattern = network.update_random_neuron(original_pattern)120 n_differences = sum(updated_pattern != original_pattern)121 self.assertTrue(n_differences <= 1)122 def test_stoch_net_update_random_neuron(self):123 n_patterns = 20124 n_bits = 100125 patterns = utils.generate_n_random_patterns(n_patterns, n_bits)126 network = pr.StochasticHopfieldNetwork()127 network.set_patterns(patterns)128 network.set_diagonal_weights_rule("zero")129 network.set_noise_parameter(2)130 network.generate_weights()131 original_pattern = patterns[15, :]132 updated_pattern = network.update_random_neuron(original_pattern)133 n_differences = sum(updated_pattern != original_pattern)134 self.assertTrue(n_differences <= 1)135 def test_noise_parameter(self):136 network = pr.StochasticHopfieldNetwork()137 network.set_noise_parameter(3)138 self.assertEqual(network.noise_parameter, 3)139if __name__ == "__main__":...
ignoracle_test.py
Source:ignoracle_test.py
...6@unittest.skip("Pending a fix for wpull 2.x interface")7class TestIgnoracle(unittest.TestCase):8 def setUp(self):9 self.oracle = Ignoracle()10 self.oracle.set_patterns([p1, p2])11 def test_ignores_returns_responsible_pattern(self):12 self.assertEqual(self.oracle.ignores('http://www.example.com/foo.css?body=1'), p1)13 self.assertEqual(self.oracle.ignores('http://www.example.com/bar/abc/def/baz'), p2)14 def test_ignores_skips_invalid_patterns(self):15 self.oracle.set_patterns(['???', p2])16 self.assertEqual(self.oracle.ignores('http://www.example.com/bar/abc/def/baz'), p2)17 def test_ignores_supports_netloc_parameterization(self):18 pattern = '{primary_netloc}/foo\.css\?'19 self.oracle.set_patterns([pattern])20 result = self.oracle.ignores('http://www.example.com/foo.css?body=1', primary_netloc='www.example.com')21 self.assertEqual(result, pattern)22 def test_permits_empty_brace_pairs(self):23 pattern = '{primary_netloc}{}/foo\.css\?{}'24 self.oracle.set_patterns([pattern])25 result = self.oracle.ignores('http://www.example.com{}/foo.css?{}body=1', primary_netloc='www.example.com')26 self.assertEqual(result, pattern)27 def test_permits_empty_brace_pairs_and_regex_repetitions(self):28 pattern = '{primary_netloc}{1}/foo\.css\?{}'29 self.oracle.set_patterns([pattern])30 result = self.oracle.ignores('http://www.example.com/foo.css?{}body=1', primary_netloc='www.example.com')31 self.assertEqual(result, pattern)32 def test_parameterization_skips_regex_ranges(self):33 pattern = '/(.*)/(\\1/){3,}'34 self.oracle.set_patterns([pattern])35 result = self.oracle.ignores('http://www.example.com/foo/foo/foo/foo/foo')36 self.assertEqual(result, pattern)37 def test_parameterization_skips_pattern_with_unknown_parameter(self):38 wrong = '/(.*)/(\\1/){boom}'39 right = '/(.*)/(\\1/){3,}'40 self.oracle.set_patterns([wrong, right])41 result = self.oracle.ignores('http://www.example.com/foo/foo/foo/foo/foo')42 self.assertEqual(result, right)43 def test_ignores_supports_url_parameterization(self):44 pattern = '{primary_url}foo\.css\?'45 self.oracle.set_patterns([pattern])46 result = self.oracle.ignores('http://www.example.com/foo.css?body=1', primary_url='http://www.example.com/')47 self.assertEqual(result, pattern)48 def test_ignores_escapes_url(self):49 pattern = '{primary_url}foo\.css\?'50 self.oracle.set_patterns([pattern])51 result = self.oracle.ignores('http://www.example.com/bar.css??/foo.css?body=1', primary_url='http://www.example.com/bar.css??/')52 self.assertEqual(result, pattern)53 def test_ignores_with_parameterized_url_replaces_none_placeholder_with_empty_string(self):54 pattern = '{primary_url}foo\.css\?'55 self.oracle.set_patterns([pattern])56 # This should treat the pattern as if it were "foo\.css\?"57 result = self.oracle.ignores('http://www.example.com/foo.css?body=1')58 self.assertEqual(result, pattern)59 def test_ignores_returns_false_for_unsuccessful_match(self):60 self.assertFalse(self.oracle.ignores('http://www.example.com/media/qux.jpg'))61 def test_set_patterns_converts_bytes_to_utf8(self):62 self.oracle.set_patterns([b'foobar'])63 self.assertEqual(self.oracle.patterns[0], 'foobar')64@unittest.skip("Pending a fix for wpull 2.x interface")65class TestRecordInfoParameterization(unittest.TestCase):66 def test_uses_top_url_if_present(self):67 record_info = dict(68 top_url='http://www.example.com/'69 )70 result = parameterize_record_info(record_info)71 self.assertEqual('http://www.example.com/', result['primary_url'])72 self.assertEqual('www.example.com', result['primary_netloc'])73 def test_uses_url_for_level_zero_url(self):74 record_info = dict(75 url='http://www.example.com/',76 level=0...
walker.py
Source:walker.py
...9 def __init__(self, dir):10 self.dir = dir11 self.cache = []12 self.patterns = []13 def set_patterns(self, patterns, ignore=True):14 p = _Pattern(patterns, ignore)15 self.patterns.append(p)16 def start(self, iscache=False):17 for dirpath, dirs, files in os.walk(self.dir):18 for f in files:19 fpath = os.path.join(dirpath, f)20 if not self.__is_notice(fpath):21 continue22 if iscache:23 self.cache.append(fpath)24 print "chche"25 yield fpath26 def __is_notice(self, dirpath):27 for p in self.patterns:28 if not self.__is_target_pattern(p, dirpath):29 return False30 return True31 def __is_target_pattern(self, pattern, dirpath):32 " Pattern is so complex..."33 if not self.patterns:34 return True35 for t in pattern.patterns:36 if t.search(dirpath):37 if pattern.is_ignore:38 return False39 return True40 if pattern.is_ignore:41 return True42 return False43 def back(self, num):44 if not num:45 raise ValueError("Should over 0")46 if not self.cache:47 raise KeyError("You do not set chache")48 if num > 0:49 num = 0 - num50 return self.cache[num]51class _Pattern:52 def __init__(self, patterns, ignore=True):53 self.patterns = [re.compile(i) for i in patterns]54 self.is_ignore = ignore55if __name__ == '__main__':56 print __file__57 full_path = os.path.abspath(__file__)58 paths = full_path.split("/")59 home = "/".join(paths[:6])60 walker = Walker(home)61 walker.set_patterns([".*py$"], False)62 walker.set_patterns([".*__init__.py"])63 #walker.set_patterns([".*pyc$",".*cgi$", "/\.git"])64 n = walker.start(iscache=True)65 print "---- sart ------"66 while True:67 try:68 i = 169 print n.next()70 print "#####",walker.back(i)71 i +=172 except StopIteration:73 print "end"74 break...
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!!