def test_summary_logger_can_accept_kwargs(self, tmpdir, simple_chromosomes, simple_evaluation_function): log_file = tmpdir.join('log.txt') logger = SummaryLogger(target=log_file, stdout=False) pop = Population(chromosomes=simple_chromosomes, eval_function=simple_evaluation_function) pop.evaluate() # lets make a first simple log logger.log(pop, foo="bar", buzz="meh") with open(log_file, "r") as f: read_lines = f.readlines() assert len(read_lines) == 1 first_line = read_lines[0] assert "bar" in first_line assert "meh" in first_line last_entry = first_line.split(",") assert len(last_entry) == 6 # lets log another one logger.log(pop, buzz="moh") with open(log_file, "r") as f: read_lines = f.readlines() assert len(read_lines) == 2 first_line = read_lines[-1] assert "moh" in first_line last_entry = first_line.split(",") assert len(last_entry) == 5
def test_summarylogger_can_write_to_stdout(self, capsys, simple_chromosomes, simple_evaluation_function): logger = SummaryLogger(target=None, stdout=True) pop = Population(chromosomes=range(10), eval_function=lambda x: x) pop.evaluate() logger.log(pop) logger.log(pop) read_stdout = [line for line in capsys.readouterr().out.split('\n') if line != ''] assert len(read_stdout) == 2
def test_summarylogger_write_file_mo_stdout(self, tmpdir, capsys): log_file = tmpdir.join('log.txt') logger = SummaryLogger(target=log_file, stdout=False) pop = Population(chromosomes=range(10), eval_function=lambda x: x) # we should see that a file was created with an appropriate number of rows pop.evaluate() logger.log(pop) with open(log_file, "r") as f: assert len(f.readlines()) == 1 # we should see that a file was created with an appropriate number of rows logger.log(pop) with open(log_file, "r") as f: assert len(f.readlines()) == 2 read_stdout = [line for line in capsys.readouterr().out.split('\n') if line != ''] # there should be nothing printed assert len(read_stdout) == 0