How to use read_report method in Nose2

Best Python code snippet using nose2

test_on_reports.py

Source:test_on_reports.py Github

copy

Full Screen

...69 @classmethod70 def tearDownClass(cls):71 cls.db.drop_all()72 73 def read_report(self, path):74 doc = FinancialReport(75 io.TextIOWrapper(PDFFileIO(path), "utf-8"), 76 records_spec=self.spec, voc=self.voc77 )78 return doc79 ## reports/cng_2016_y.pdf80 def test_update_names_cng_2016_y_ics(self):81 doc = self.read_report("reports/cng_2016_y.pdf")82 self.assertEqual(83 doc.ics.names, 84 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]85 )86 def test_update_names_cng_2016_y_bls(self):87 doc = self.read_report("reports/cng_2016_y.pdf")88 self.assertEqual(89 doc.bls.names, 90 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]91 )92 def test_update_names_cng_2016_y_cfs(self):93 doc = self.read_report("reports/cng_2016_y.pdf")94 self.assertEqual(95 doc.cfs.names, 96 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]97 )98 99 ## reports/decora_2016_q1.pdf100 def test_update_names_decora_2016_q1_ics(self):101 doc = self.read_report("reports/decora_2016_q1.pdf")102 self.assertEqual(103 doc.ics.names, 104 [(3, (2016, 3, 31)), (3, (2015, 3, 31))]105 )106 def test_update_names_decora_2016_q1_bls(self):107 doc = self.read_report("reports/decora_2016_q1.pdf")108 self.assertEqual(109 doc.bls.names, 110 [(3, (2016, 3, 31)), (3, (2015, 12, 31))]111 )112 def test_update_names_decora_2016_q1_cfs(self):113 doc = self.read_report("reports/decora_2016_q1.pdf")114 self.assertEqual(115 doc.cfs.names, 116 [(3, (2016, 3, 31)), (3, (2015, 3, 31))]117 )118 ## reports/polsat_2016_q3.pdf119 def test_update_names_polsat_2016_q3_ics(self):120 doc = self.read_report("reports/polsat_2016_q3.pdf")121 self.assertEqual(122 doc.ics.names, 123 [(3, (2016, 9, 30)), (3, (2015, 9, 30)), 124 (9, (2016, 9, 30)), (9, (2015, 9, 30))]125 )126 def test_update_names_polsat_2016_q3_bls(self):127 doc = self.read_report("reports/polsat_2016_q3.pdf")128 self.assertEqual(129 doc.bls.names, 130 [(3, (2016, 9, 30)), (3, (2015, 12, 31))]131 )132 def test_update_names_polsat_2016_q3_cfs(self):133 doc = self.read_report("reports/polsat_2016_q3.pdf")134 self.assertEqual(135 doc.cfs.names, 136 [(9, (2016, 9, 30)), (9, (2015, 9, 30))]137 )138 ## reports/bdx_2016_q3.pdf139 def test_update_names_bdx_2016_q3_ics(self):140 doc = self.read_report("reports/bdx_2016_q3.pdf")141 self.assertEqual(142 doc.ics.names, 143 [(9, (2016, 9, 30)), (9, (2015, 9, 30)), 144 (3, (2016, 9, 30)), (3, (2015, 9, 30))]145 )146 def test_update_names_bdx_2016_y3_bls(self):147 doc = self.read_report("reports/bdx_2016_q3.pdf")148 self.assertEqual(149 doc.bls.names, 150 [(9, (2016, 9, 30)), (3, (2015, 12, 31))]151 )152 def test_update_names_bdx_2016_q3_cfs(self):153 doc = self.read_report("reports/bdx_2016_q3.pdf")154 self.assertEqual(155 doc.cfs.names, 156 [(9, (2016, 9, 30)), (9, (2015, 9, 30))]157 ) 158 ## reports/pgnig_2016_y.pdf159 def test_update_names_pgnig_2016_y_ics(self):160 doc = self.read_report("reports/pgnig_2016_y.pdf")161 self.assertEqual(162 doc.ics.names, 163 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]164 )165 def test_update_names_pgnig_2016_y_bls(self):166 doc = self.read_report("reports/pgnig_2016_y.pdf")167 self.assertEqual(168 doc.bls.names, 169 [(12, (2016, 12, 31)), (12, (2015, 12, 31)), (12, (2014, 12, 31))]170 )171 def test_update_names_pgnig_2016_y_cfs(self):172 doc = self.read_report("reports/pgnig_2016_y.pdf")173 self.assertEqual(174 doc.cfs.names, 175 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]176 ) 177 ## reports/ltx_2016_q1.pdf178 def test_update_names_ltx_2016_q1_ics(self):179 doc = self.read_report("reports/ltx_2016_q1.pdf")180 self.assertEqual(181 doc.ics.names, 182 [(3, (2016, 3, 31)), (3, (2015, 3, 31))]183 )184 def test_update_names_ltx_2016_q1_bls(self):185 doc = self.read_report("reports/ltx_2016_q1.pdf")186 self.assertEqual(187 doc.bls.names, 188 [(6, (2016, 3, 31)), (6, (2015, 12, 31)), (6, (2015, 3, 31))]189 )190 def test_update_names_ltx_2016_q1_cfs(self):191 doc = self.read_report("reports/ltx_2016_q1.pdf")192 self.assertEqual(193 doc.cfs.names, 194 [(3, (2016, 3, 31)), (3, (2015, 3, 31))]195 ) 196 ## reports/lpp_2016_q1.pdf197 def test_update_names_lpp_2016_q_ics(self):198 doc = self.read_report("reports/lpp_2016_q1.pdf")199 self.assertEqual(200 doc.ics.names, 201 [(3, (2016, 3, 31)), (3, (2015, 3, 31))]202 )203 def test_update_names_lpp_2016_q_bls(self):204 doc = self.read_report("reports/lpp_2016_q1.pdf")205 self.assertEqual(206 doc.bls.names, 207 [(3, (2016, 3, 31)), (3, (2015, 3, 31)), (3, (2015, 12, 31))]208 )209 def test_update_names_lpp_2016_q_cfs(self):210 doc = self.read_report("reports/lpp_2016_q1.pdf")211 self.assertEqual(212 doc.cfs.names, 213 [(3, (2016, 3, 31)), (3, (2015, 3, 31))]214 )215 ## reports/gri_2016_y.pdf216 @unittest.skip217 def test_update_names_gri_2016_y_ics(self):218 doc = self.read_report("reports/gri_2016_y.pdf")219 self.assertEqual(220 doc.ics.names, 221 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]222 )223 @unittest.skip224 def test_update_names_gri_2016_y_bls(self):225 doc = self.read_report("reports/gri_2016_y.pdf")226 self.assertEqual(227 doc.bls.names, 228 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]229 )230 @unittest.skip231 def test_update_names_gri_2016_y_cfs(self):232 doc = self.read_report("reports/gri_2016_y.pdf")233 self.assertEqual(234 doc.cfs.names, 235 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]236 )237 ## reports/wieleton_2016_y.pdf238 def test_update_names_wieleton_2016_y_ics(self):239 doc = self.read_report("reports/wieleton_2016_y.pdf")240 self.assertEqual(241 doc.ics.names, 242 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]243 )244 def test_update_names_wieleton_2016_y_bls(self):245 doc = self.read_report("reports/wieleton_2016_y.pdf")246 self.assertEqual(247 doc.bls.names, 248 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]249 )250 def test_update_names_wieleton_2016_y_cfs(self):251 doc = self.read_report("reports/wieleton_2016_y.pdf")252 self.assertEqual(253 doc.cfs.names, 254 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]255 )256 ## reports/rpc_2016_q3.pdf257 def test_update_names_rpc_2016_q3_ics(self):258 doc = self.read_report("reports/rpc_2016_q3.pdf")259 self.assertEqual(260 doc.ics.names, 261 [(3, (2015, 9, 30)), (9, (2015, 9, 30)),262 (3, (2016, 9, 30)), (9, (2016, 9, 30))]263 )264 def test_update_names_rpc_2016_q3_bls(self):265 doc = self.read_report("reports/rpc_2016_q3.pdf")266 self.assertEqual(267 doc.bls.names, 268 [(3, (2015, 12, 31)), (3, (2016, 9, 30))]269 )270 def test_update_names_rpc_2016_q3_cfs(self):271 doc = self.read_report("reports/rpc_2016_q3.pdf") 272 self.assertEqual(273 doc.cfs.names, 274 [(3, (2015, 9, 30)), (3, (2016, 9, 30))]275 )276 ## reports/kst_2016_y.pdf277 def test_update_names_kst_2016_y_ics(self):278 doc = self.read_report("reports/kst_2016_y.pdf")279 self.assertEqual(280 doc.ics.names, 281 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]282 )283 def test_update_names_kst_2016_y_bls(self):284 doc = self.read_report("reports/kst_2016_y.pdf")285 self.assertEqual(286 doc.bls.names, 287 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]288 )289 def test_update_names_kst_2016_y_cfs(self):290 doc = self.read_report("reports/kst_2016_y.pdf")291 self.assertEqual(292 doc.cfs.names, 293 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]294 )295 ## reports/helio_2016_m2.pdf296 def test_update_names_helio_2016_m2_ics(self):297 doc = self.read_report("reports/helio_2016_m2.pdf") 298 self.assertEqual(299 doc.ics.names, 300 [(6, (2016, 12, 31)), (6, (2015, 12, 31))]301 )302 def test_update_names_helio_2016_m2_bls(self):303 doc = self.read_report("reports/helio_2016_m2.pdf") 304 self.assertEqual(305 doc.bls.names, 306 [(6, (2016, 12, 31)), (6, (2015, 12, 31)), (6, (2015, 12, 31))]307 )308 def test_update_names_helio_2016_m2_cfs(self):309 doc = self.read_report("reports/helio_2016_m2.pdf")310 self.assertEqual(311 doc.cfs.names, 312 [(6, (2016, 12, 31)), (6, (2015, 12, 31))]313 )314 ## reports/graal_2015_m1.pdf315 def test_update_names_graal_2015_m1_ics(self):316 doc = self.read_report("reports/graal_2015_m1.pdf") 317 self.assertEqual(318 doc.ics.names, 319 [(6, (2015, 6, 30)), (6, (2014, 6, 30))]320 )321 def test_update_names_graal_2015_m1_bls(self):322 doc = self.read_report("reports/graal_2015_m1.pdf") 323 self.assertEqual(324 doc.bls.names, 325 [(6, (2015, 6, 30)), (6, (2014, 12, 31)), (6, (2014, 6, 30))]326 )327 def test_update_names_graal_2015_m1_cfs(self):328 doc = self.read_report("reports/graal_2015_m1.pdf") 329 self.assertEqual(330 doc.cfs.names, 331 [(6, (2015, 6, 30)), (6, (2014, 6, 30))]332 )333 ## reports/arctic_2016_q3.pdf334 def test_update_names_arctic_2016_q3_ics(self):335 doc = self.read_report("reports/arctic_2016_q3.pdf") 336 self.assertEqual(337 doc.ics.names, 338 [(3, (2016, 9, 30)), (9, (2016, 9, 30)),339 (3, (2015, 9, 30)), (9, (2015, 9, 30))]340 )341 def test_update_names_arctic_2016_q3_bls(self):342 doc = self.read_report("reports/arctic_2016_q3.pdf")343 self.assertEqual(344 doc.bls.names, 345 [(3, (2016, 9, 30)), (3, (2016, 6, 30)),346 (3, (2015, 12, 31)), (3, (2015, 9, 30))]347 )348 def test_update_names_arctic_2016_q3_cfs(self):349 doc = self.read_report("reports/arctic_2016_q3.pdf") 350 self.assertEqual(351 doc.cfs.names, 352 [(3, (2016, 9, 30)), (9, (2016, 9, 30)),353 (3, (2015, 9, 30)), (9, (2015, 9, 30))]354 )355 ## reports/pge_2016_y.pdf356 def test_update_names_pge_2016_y_ics(self):357 doc = self.read_report("reports/pge_2016_y.pdf")358 self.assertEqual(359 doc.ics.names, 360 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]361 )362 def test_update_names_pge_2016_y_bls(self):363 doc = self.read_report("reports/pge_2016_y.pdf")364 self.assertEqual(365 doc.bls.names, 366 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]367 )368 def test_update_names_pge_2016_y_cfs(self):369 doc = self.read_report("reports/pge_2016_y.pdf")370 self.assertEqual(371 doc.cfs.names, 372 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]373 )374 ## reports/protektor_2016_q3.pdf375 def test_update_names_protektor_2016_q3_ics(self):376 doc = self.read_report("reports/protektor_2016_q3.pdf")377 self.assertEqual(378 doc.ics.names, 379 [(9, (2016, 9, 30)), (9, (2015, 9, 30))]380 )381 def test_update_names_protektor_2016_q3_bls(self):382 doc = self.read_report("reports/protektor_2016_q3.pdf")383 self.assertEqual(384 doc.bls.names, 385 [(3, (2016, 9, 30)), (3, (2015, 12, 31))]386 )387 def test_update_names_protektor_2016_q3_cfs(self):388 doc = self.read_report("reports/protektor_2016_q3.pdf") 389 self.assertEqual(390 doc.cfs.names, 391 [(9, (2016, 9, 30)), (9, (2015, 9, 30))]392 )393 ## reports/kghm_2016_y.pdf394 def test_update_names_kghm_2016_y_ics(self):395 doc = self.read_report("reports/kghm_2016_y.pdf")396 self.assertEqual(397 doc.ics.names, 398 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]399 )400 def test_update_names_kghm_2016_y_bls(self):401 doc = self.read_report("reports/kghm_2016_y.pdf")402 self.assertEqual(403 doc.bls.names, 404 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]405 )406 def test_update_names_kghm_2016_y_cfs(self):407 doc = self.read_report("reports/kghm_2016_y.pdf")408 self.assertEqual(409 doc.cfs.names, 410 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]411 )412 ## reports/obl_2016_y.pdf413 def test_update_names_obl_2016_y_ics(self):414 doc = self.read_report("reports/obl_2016_y.pdf")415 self.assertEqual(416 doc.ics.names, 417 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]418 )419 def test_update_names_obl_2016_y_bls(self):420 doc = self.read_report("reports/obl_2016_y.pdf")421 self.assertEqual(422 doc.bls.names, 423 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]424 )425 def test_update_names_obl_2016_y_cfs(self):426 doc = self.read_report("reports/obl_2016_y.pdf")427 self.assertEqual(428 doc.cfs.names, 429 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]430 )431 ## reports/lotos_2016_m2.pdf432 def test_update_names_lotos_2016_m2_ics(self):433 doc = self.read_report("reports/lotos_2016_m2.pdf")434 self.assertEqual(435 doc.ics.names, 436 [(3, (2016, 6, 30)), (6, (2016, 6, 30)),437 (3, (2015, 6, 30)), (6, (2015, 6, 30))]438 )439 def test_update_names_lotos_2016_m2_bls(self):440 doc = self.read_report("reports/lotos_2016_m2.pdf")441 self.assertEqual(442 doc.bls.names, 443 [(6, (2016, 6, 30)), (6, (2015, 12, 31))]444 )445 def test_update_names_lotos_2016_m2_cfs(self):446 doc = self.read_report("reports/lotos_2016_m2.pdf") 447 self.assertEqual(448 doc.cfs.names, 449 [(6, (2016, 6, 30)), (6, (2015, 6, 30))]450 )451 ## reports/dbc_2015_y.pdf452 def test_update_names_dbc_2015_y_ics(self):453 doc = self.read_report("reports/dbc_2015_y.pdf") 454 self.assertEqual(455 doc.ics.names, 456 [(12, (2015, 12, 31)), (12, (2014, 12, 31))]457 )458 def test_update_names_dbc_2015_y_bls(self):459 doc = self.read_report("reports/dbc_2015_y.pdf")460 self.assertEqual(461 doc.bls.names, 462 [(12, (2015, 12, 31)), (12, (2014, 12, 31))]463 )464 def test_update_names_dbc_2015_y_cfs(self):465 doc = self.read_report("reports/dbc_2015_y.pdf")466 self.assertEqual(467 doc.cfs.names, 468 [(12, (2015, 12, 31)), (12, (2014, 12, 31))]469 )470 ## reports/otl_2016_y.pdf471 def test_update_names_otl_2016_y_ics(self):472 doc = self.read_report("reports/otl_2016_y.pdf")473 self.assertEqual(474 doc.ics.names, 475 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]476 )477 def test_update_names_otl_2016_y_bls(self):478 doc = self.read_report("reports/otl_2016_y.pdf") 479 self.assertEqual(480 doc.bls.names, 481 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]482 )483 def test_update_names_otl_2016_y_cfs(self):484 doc = self.read_report("reports/otl_2016_y.pdf") 485 self.assertEqual(486 doc.cfs.names, 487 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]488 )489 ## reports/acp_2010_q1.pdf490 def test_update_names_acp_2010_q1_ics(self):491 doc = self.read_report("reports/acp_2010_q1.pdf")492 self.assertEqual(493 doc.ics.names, 494 [(3, (2010, 3, 31)), (3, (2009, 3, 31))]495 )496 def test_update_names_acp_2010_q1_bls(self):497 doc = self.read_report("reports/acp_2010_q1.pdf")498 self.assertEqual(499 doc.bls.names, 500 [(3, (2010, 3, 31)), (3, (2009, 12, 31)), (3, (2009, 3, 31))]501 )502 def test_update_names_acp_2010_q1_cfs(self):503 doc = self.read_report("reports/acp_2010_q1.pdf")504 self.assertEqual(505 doc.cfs.names, 506 [(3, (2010, 3, 31)), (3, (2009, 3, 31))]507 )508 ## reports/arctic_2015_q1.pdf509 def test_update_names_arctic_2015_q1_ics(self):510 doc = self.read_report("reports/arctic_2015_q1.pdf") 511 self.assertEqual(512 doc.ics.names, 513 [(3, (2015, 3, 31)), (3, (2014, 3, 31)), (12, (2014, 12, 31))]514 )515 def test_update_names_arctic_2015_q1_bls(self):516 doc = self.read_report("reports/arctic_2015_q1.pdf") 517 self.assertEqual(518 doc.bls.names, 519 [(3, (2015, 3, 31)), (3, (2014, 12, 31)), (3, (2014, 3, 31))]520 )521 def test_update_names_arctic_2015_q1_cfs(self):522 doc = self.read_report("reports/arctic_2015_q1.pdf")523 self.assertEqual(524 doc.cfs.names, 525 [(3, (2015, 3, 31)), (3, (2014, 3, 31)), (12, (2014, 12, 31))]526 )527 ## reports/ltx_2016_y.pdf528 def test_update_names_ltx_2016_y_ics(self):529 doc = self.read_report("reports/ltx_2016_y.pdf")530 self.assertEqual(531 doc.ics.names, 532 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]533 )534 def test_update_names_ltx_2016_y_bls(self):535 doc = self.read_report("reports/ltx_2016_y.pdf")536 self.assertEqual(537 doc.bls.names, 538 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]539 )540 def test_update_names_ltx_2016_y_cfs(self):541 doc = self.read_report("reports/ltx_2016_y.pdf")542 self.assertEqual(543 doc.cfs.names, 544 [(12, (2016, 12, 31)), (12, (2015, 12, 31))]...

Full Screen

Full Screen

validate.py

Source:validate.py Github

copy

Full Screen

...48 result = v.validate(corpus)49 invalid_tracks = result.invalid_items50 write_report(report_path, invalid_tracks)51 else:52 invalid_tracks = read_report(report_path)53 print('Validate tracks - Already done')54 invalid_utts = []55 for utt in corpus.utterances.values():56 if utt.track in invalid_tracks:57 invalid_utts.append(utt.idx)58 return invalid_utts59def find_invalid_character_ratios(output_path, corpus):60 #61 # Find invalid chracter ratios62 #63 report_path = os.path.join(output_path, 'invalid_character_ratio.json')64 if not os.path.isfile(report_path):65 print('Validate character ratio ...')66 v = validation.UtteranceTranscriptionRatioValidator(67 max_characters_per_second=25,68 label_list_idx=audiomate.corpus.LL_WORD_TRANSCRIPT,69 num_threads=470 )71 result = v.validate(corpus)72 invalid_utts = result.invalid_items73 write_report(report_path, invalid_utts)74 else:75 invalid_utts = read_report(report_path)76 print('Validate character ratio - Already done')77 return invalid_utts.keys()78def find_invalid_transcripts(output_path, corpus):79 #80 # Find transcripts that can't be normalized81 #82 report_path = os.path.join(output_path, 'invalid_transcripts.json')83 if not os.path.isfile(report_path):84 print('Validate transcript normalization ...')85 v = validators.TextNormalizationValidator()86 result = v.validate(corpus)87 invalid_utts = result.invalid_items88 write_report(report_path, invalid_utts)89 else:90 invalid_utts = read_report(report_path)91 print('Validate transcript normalization - Already done')92 return invalid_utts.keys()93def write_report(path, report):94 with open(path, 'w', encoding='utf-8') as f:95 json.dump(report, f, ensure_ascii=False)96def read_report(path):97 with open(path, 'r', encoding='utf-8') as f:98 return json.load(f)99if __name__ == '__main__':...

Full Screen

Full Screen

day3.py

Source:day3.py Github

copy

Full Screen

1#!/usr/bin/env python2from collections import defaultdict3from textwrap import dedent4def read_report(s):5 return s.strip().splitlines()6def get_bit_counts(report):7 counts = defaultdict(lambda: defaultdict(int))8 for number in report:9 for i, bit in enumerate(number):10 counts[i][bit] += 111 return dict(counts)12def get_diagnostics(report):13 gamma, epsilon = "", ""14 counts = get_bit_counts(report)15 for i in range(len(counts)):16 freq = sorted(counts[i].items(), key=lambda item: item[1])17 gamma += freq[1][0]18 epsilon += freq[0][0]19 return int(gamma, 2), int(epsilon, 2)20def get_life_support_rating(report):21 candidates = report[:]22 for i in range(len(report[0])):23 counts = get_bit_counts(candidates)24 freq = sorted(counts[i].items(), key=lambda item: (item[1], item[0]), reverse=True)25 most_common = freq[0][0]26 candidates = [c for c in candidates if c[i] == most_common]27 if len(candidates) == 1:28 break29 oxygen = candidates[0]30 candidates = report[:]31 for i in range(len(report[0])):32 counts = get_bit_counts(candidates)33 freq = sorted(counts[i].items(), key=lambda item: (item[1], item[0]), reverse=True)34 least_common = freq[1][0]35 candidates = [c for c in candidates if c[i] == least_common]36 if len(candidates) == 1:37 break38 co2 = candidates[0]39 return int(oxygen, 2), int(co2, 2)40def main():41 example = dedent("""42 0010043 1111044 1011045 1011146 1010147 0111148 0011149 1110050 1000051 1100152 0001053 0101054 """)55 report = read_report(example)56 gamma, epsilon = get_diagnostics(report)57 assert (gamma, epsilon) == (22, 9)58 oxygen, co2 = get_life_support_rating(report)59 assert (oxygen, co2) == (23, 10)60 with open("input.txt") as f:61 report = read_report(f.read())62 gamma, epsilon = get_diagnostics(report)63 print("part 1")64 print(gamma * epsilon)65 oxygen, co2 = get_life_support_rating(report)66 print("part 2")67 print(oxygen * co2)68if __name__ == "__main__":...

Full Screen

Full Screen

diagnostic.py

Source:diagnostic.py Github

copy

Full Screen

1#!/usr/bin/env python2# -*- coding: utf-8 -*-3def read_report(case):4 with open(f'day3/{case}') as f:5 report = f.read().splitlines()6 return report7# Part 18def check_consumption(report):9 n = len(report) # rows10 m = len(report[0]) # cols11 count = [0]*m12 for word in report:13 for i, b in enumerate(word):14 count[i] += int(b)15 gamma = ''.join(['1' if c > n/2 else '0' for c in count])16 epsilon = ''.join(['0' if c > n/2 else '1' for c in count]) # epsilon = ~gamma17 return int(epsilon, 2) * int(gamma, 2)18print('Part 1. Testing...', end=' ')19report = read_report('test')20chk = check_consumption(report)21print('Done!')22print(f'Consumption: {chk}')23print('Part 1.', end=' ')24report = read_report('input')25chk = check_consumption(report)26print('Done!')27print(f'Consumption: {chk}')28# Part 229def count_ones(report, idx):30 count = 031 for word in report:32 count += int(word[idx])33 return count34## oxygen35def check_oxygen(report, i):36 # Recursive because why not37 if len(report) == 1:38 return report[0]39 40 count = count_ones(report, i)41 n = len(report)42 b = '1' if count >= n/2 else '0'43 report = list(filter(lambda x: x[i] == b, report))44 # print(i, b, count, n)45 return check_oxygen(report, i+1)46## co247def check_co2(report, i):48 # The same as above because no time :_49 if len(report) == 1:50 return report[0]51 52 count = count_ones(report, i)53 n = len(report)54 b = '0' if count >= n/2 else '1'55 report = list(filter(lambda x: x[i] == b, report))56 # print(i, b, count, n)57 return check_co2(report, i+1)58print('Part 2. Testing... ', end=' ')59report = read_report('test')60oxygen = check_oxygen(report, 0)61report = read_report('test')62co2 = check_co2(report, 0)63assert int(oxygen, 2) * int(co2, 2) == 23064print('Done!')65print('Part 2...', end=' ')66report = read_report('input')67oxygen = check_oxygen(report, 0)68report = read_report('input')69co2 = check_co2(report, 0)70chk = int(oxygen, 2) * int(co2, 2)71print('Done!')...

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