Best Python code snippet using tavern
make_annotations_bn.py
Source:make_annotations_bn.py
1#!/usr/bin/env python2# Copyright 2015 David Snyder3# Apache 2.0.4#5# This script creates four files for each HUB4 Broadcast News6# transcript file. The four files are for the music, speech, ad,7# and other transcripts. Each line of the output files define the8# start and end times of the individual events.9#10# This file is meant to be invoked by make_bn.sh.11import sys, re, os12def is_speech(line):13 if "<Segment" in line and "Speaker=" in line:14 return True15 return False16def is_other_type2(line):17 if "Type=Commercial" in line or "Type=Filler" in line or "Type=Local_News" in line:18 return True19 return False20def is_music(line):21 if "Type=Music" in line:22 return True23 return False24def is_other_type1(line):25 if "Type=Other" in line:26 return True27 return False28def extract_speech(line):29 m = re.search('(?<=S_time=)\d+.\d+', line)30 start = float(m.group(0))31 m = re.search('(?<=E_time=)\d+.\d+', line)32 end = float(m.group(0))33 if start > end:34 print "Skipping annotation where end time is before start time:", line35 return start, end36def extract_other_type2(line):37 m = re.search('(?<=S_time=)\d+.\d+', line)38 start = float(m.group(0))39 m = re.search('(?<=E_time=)\d+.\d+', line)40 end = float(m.group(0))41 if start > end:42 print "Skipping annotation where end time is before start time:", line43 return start, end44def extract_music(line):45 m = re.search('(?<=Time=)\d+.\d+', line)46 time = float(m.group(0))47 m = re.search('(?<=Level=)\w', line)48 level = m.group(0)49 is_on = False50 if level == "L" or level == "H":51 is_on = True52 elif level == "O":53 is_on = False54 else:55 print "Encountered bad token on line:", line56 sys.exit()57 return time, is_on58def extract_other_type1(line):59 m = re.search('(?<=Time=)\d+.\d+', line)60 time = float(m.group(0))61 m = re.search('(?<=Level=)\w', line)62 level = m.group(0)63 is_on = False64 if level == "L" or level == "H":65 is_on = True66 elif level == "O":67 is_on = False68 else:69 print "Encountered bad token on line:", line70 sys.exit()71 return time, is_on72def process_file(annos):73 speech = ""74 music = ""75 other_type2 = ""76 other_type1 = ""77 start_new_music_segment = True78 start_new_other_segment = True79 max_time = 0.080 prev_music_time = "0.0"81 prev_other_time = "0.0"82 for line in annos:83 if is_speech(line):84 speech_start, speech_end = extract_speech(line)85 speech = speech + str(speech_start) + " " + str(speech_end) + "\n"86 max_time = max(speech_end, max_time)87 elif is_other_type2(line):88 other_type2_start, other_type2_end = extract_other_type2(line)89 other_type2 = other_type2 + str(other_type2_start) + " " + str(other_type2_end) + "\n"90 max_time = max(other_type2_end, max_time)91 elif is_music(line):92 time, is_on = extract_music(line)93 max_time = max(time, max_time)94 if is_on and start_new_music_segment:95 prev_music_time = time96 start_new_music_segment = False97 elif not is_on and not start_new_music_segment:98 music = music + str(prev_music_time) + " " + str(time) + "\n"99 start_new_music_segment = True100 elif is_other_type1(line):101 time, is_on = extract_other_type1(line)102 max_time = max(time, max_time)103 if is_on and start_new_other_segment:104 prev_other_time = time105 start_new_other_segment = False106 elif not is_on and not start_new_other_segment:107 other_type1 = other_type1 + str(prev_other_time) + " " + str(time) + "\n"108 start_new_other_segment = True109 if not start_new_music_segment:110 music = music + str(prev_music_time) + " " + str(max_time) + "\n"111 if not start_new_other_segment:112 other_type1 = other_type1 + str(prev_other_time) + " " + str(max_time) + "\n"113 other = other_type1 + other_type2114 return speech, music, other115def main():116 in_dir = sys.argv[1]117 out_dir = sys.argv[2]118 utts = ""119 for root, dirs, files in os.walk(in_dir):120 for file in files:121 if file.endswith(".txt"):122 anno_in = open(os.path.join(root, file), 'r').readlines()123 speech, music, other = process_file(anno_in)124 utt = file.replace(".txt", "")125 utts = utts + utt + "\n"126 speech_fi_str = utt + "_speech.key"127 music_fi_str = utt + "_music.key"128 other_fi_str = utt + "_other.key"129 speech_fi = open(os.path.join(out_dir, speech_fi_str), 'w')130 speech_fi.write(speech)131 music_fi = open(os.path.join(out_dir, music_fi_str), 'w')132 music_fi.write(music)133 other_fi = open(os.path.join(out_dir, other_fi_str), 'w')134 other_fi.write(other)135 utts_fi = open(os.path.join(out_dir, "utt_list"), 'w')136 utts_fi.write(utts)137if __name__=="__main__":...
test_init.py
Source:test_init.py
...25 """Test is_on, turn_on, turn_off methods."""26 self.assertTrue(setup_component(27 self.hass, switch.DOMAIN, {switch.DOMAIN: {CONF_PLATFORM: 'test'}}28 ))29 self.assertTrue(switch.is_on(self.hass))30 self.assertEqual(31 STATE_ON,32 self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)33 self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id))34 self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))35 self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))36 switch.turn_off(self.hass, self.switch_1.entity_id)37 switch.turn_on(self.hass, self.switch_2.entity_id)38 self.hass.block_till_done()39 self.assertTrue(switch.is_on(self.hass))40 self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id))41 self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id))42 # Turn all off43 switch.turn_off(self.hass)44 self.hass.block_till_done()45 self.assertFalse(switch.is_on(self.hass))46 self.assertEqual(47 STATE_OFF,48 self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)49 self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id))50 self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))51 self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))52 # Turn all on53 switch.turn_on(self.hass)54 self.hass.block_till_done()55 self.assertTrue(switch.is_on(self.hass))56 self.assertEqual(57 STATE_ON,58 self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)59 self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id))60 self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id))61 self.assertTrue(switch.is_on(self.hass, self.switch_3.entity_id))62 def test_setup_two_platforms(self):63 """Test with bad configuration."""64 # Test if switch component returns 0 switches65 test_platform = loader.get_component(self.hass, 'switch.test')66 test_platform.init(True)67 loader.set_component(self.hass, 'switch.test2', test_platform)68 test_platform.init(False)69 self.assertTrue(setup_component(70 self.hass, switch.DOMAIN, {71 switch.DOMAIN: {CONF_PLATFORM: 'test'},72 '{} 2'.format(switch.DOMAIN): {CONF_PLATFORM: 'test2'},73 }74 ))75async def test_switch_context(hass):...
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!!