Best Python code snippet using autotest_python
trie.py
Source:trie.py
...31 def add_emits(self, emits):32 if not isinstance(emits, dict):33 raise Exception("keywords need a dict")34 self.emits.update(emits)35 def set_failure(self, state):36 self.failure = state37 def get_transitions(self):38 return self.success.keys()39 def next_state(self, word):40 return self.success.get(word)41class Trie(object):42 def __init__(self, words=None):43 self.root = State("", 0)44 self.root.set_failure(self.root)45 self.is_create_failure = False46 if words:47 self.create_trie(words)48 def create_trie(self, words):49 if isinstance(words, list):50 self.create_trie_from_list(words)51 elif isinstance(words, dict):52 self.create_trie_from_dict(words)53 return self54 def create_trie_from_list(self, keywords):55 for keyword in keywords:56 self.add_keyword(keyword, '')57 self.create_failure()58 return self59 def create_trie_from_dict(self, keywords):60 for keyword, value in keywords.items():61 self.add_keyword(keyword, value)62 self.create_failure()63 return self64 def add_keyword(self, keyword, value):65 current_state = self.root66 word_list = list(keyword)67 for word in word_list:68 current_state = current_state.add_word(word)69 current_state.add_one_emit(keyword, value)70 def create_failure(self):71 root = self.root72 state_queue = queue.Queue()73 for k, v in self.root.success.items():74 state_queue.put(v)75 v.set_failure(root)76 while (not state_queue.empty()):77 current_state = state_queue.get()78 transitions = current_state.get_transitions()79 for word in transitions:80 target_state = current_state.next_state(word)81 state_queue.put(target_state)82 trace_state = current_state.failure83 while trace_state.next_state(word) is None and trace_state.deepth != 0:84 trace_state = trace_state.failure85 if trace_state.next_state(word) is not None:86 target_state.set_failure(trace_state.next_state(word))87 target_state.add_emits(trace_state.next_state(word).emits)88 else:89 target_state.set_failure(trace_state)90 self.is_create_failure = True91 def get_state(self, current_state, word):92 new_current_state = current_state.next_state(word)93 while new_current_state is None and current_state.deepth != 0:94 current_state = current_state.failure95 new_current_state = current_state.next_state(word)96 return new_current_state97 def parse_text(self, text, allow_over_laps=True):98 matchs = []99 if not self.is_create_failure:100 self.create_failure()101 position = 0102 current_state = self.root103 for word in list(text):...
test_version_result.py
Source:test_version_result.py
...23 vr.init('input', True)24 self.assertRaises(ArgumentNullError, vr.set_failure, failure=pf, argument='minor')25 vr = self.VersionResult()26 vr.init('input', False)27 vr.set_failure(failure=pf, argument='minor')28 fail = vr.get_version_parse_exception()29 self.assertIsInstance(fail, ArgumentNullError)30 def test_argument_error(self):31 pf = self.ParseFailureKind.ARGUMENT_ERR32 vr = self.VersionResult()33 vr.init('input', True)34 self.assertRaises(ArgumentError, vr.set_failure, failure=pf, argument='minor')35 vr = self.VersionResult()36 vr.init('input', False)37 vr.set_failure(failure=pf, argument='minor')38 fail = vr.get_version_parse_exception()39 self.assertIsInstance(fail, ArgumentError)40 41 def test_general_argument_error(self):42 self.assertRaises(ArgumentError, Version.parse, input=".1.2.3.4")43 pf = self.ParseFailureKind.NONE44 vr = self.VersionResult()45 vr.init('input', True)46 self.assertRaises(ArgumentError, vr.set_failure, failure=pf, argument='minor')47 vr = self.VersionResult()48 vr.init('input', False)49 vr.set_failure(failure=pf, argument='minor')50 fail = vr.get_version_parse_exception()51 self.assertIsInstance(fail, ArgumentError)52 53 def test_argument_out_or_range_error(self):54 pf = self.ParseFailureKind.ARGUMENT_OUT_OF_RANGE_ERR55 vr = self.VersionResult()56 vr.init('input', True)57 self.assertRaises(ArgumentOutOfRangeError, vr.set_failure, failure=pf, argument='minor')58 vr = self.VersionResult()59 vr.init('input', False)60 vr.set_failure(failure=pf, argument='minor')61 fail = vr.get_version_parse_exception()62 self.assertIsInstance(fail, ArgumentOutOfRangeError)63 64 def test_format_error(self):65 pf = self.ParseFailureKind.FORMAT_ERR66 vr = self.VersionResult()67 vr.init('input', True)68 self.assertRaises(ArgumentError, vr.set_failure, failure=pf, argument='not a number')69 self.assertRaises(FormatError, vr.set_failure, failure=pf, argument=self)70 vr = self.VersionResult()71 vr.init('input', False)72 vr.set_failure(failure=pf, argument='not a number')73 fail = vr.get_version_parse_exception()74 self.assertIsInstance(fail, ArgumentError)75 76 def test_property_parsed_version(self):77 v = Version.parse("1.2.3.4")78 vr = self.VersionResult()79 vr.parsed_version = v80 self.assertEqual(vr.parsed_version, v)81if __name__ == '__main__':...
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!!