def test_summarylogger_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, logger=logger) # lets make a first simple log pop.log(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 pop.log(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_baselogger_can_write_to_stdout(self, capsys, simple_chromosomes, simple_evaluation_function): pop = Population(chromosomes=simple_chromosomes, eval_function=lambda x: x, logger=BaseLogger(target=None, stdout=True)) pop.log() read_stdout = [ line for line in capsys.readouterr().out.split('\n') if line != '' ] assert len(read_stdout) == len(pop)
def test_baselogger_can_accept_kwargs(self, tmpdir, simple_chromosomes, simple_evaluation_function): log_file = tmpdir.join('log.txt') logger = BaseLogger(target=log_file, stdout=False) pop = Population(chromosomes=simple_chromosomes, eval_function=simple_evaluation_function, logger=logger) # we should see that a file was created with an appropriate number of rows pop.log(foo="bar") with open(log_file, "r") as f: assert len(f.readlines()) == len(simple_chromosomes) assert all(["bar" in l for l in f.readlines()]) # we should see that a file was created with an appropriate number of rows pop.log(foo="meh") with open(log_file, "r") as f: assert len(f.readlines()) == (2 * len(simple_chromosomes)) assert all(['meh' in l for l in f.readlines()[-10:]])
def test_summarylogger_can_write_file_without_stdout( self, tmpdir, capsys, simple_chromosomes, simple_evaluation_function): log_file = tmpdir.join('log.txt') logger = SummaryLogger(target=log_file, stdout=False) pop = Population(chromosomes=range(10), eval_function=lambda x: x, logger=logger) # we should see that a file was created with an appropriate number of rows pop.log() 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 pop.log() 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
def test_baselogger_write_file_no_stdout(self, tmpdir, capsys, simple_chromosomes, simple_evaluation_function): log_file = tmpdir.join('log.txt') logger = BaseLogger(target=log_file, stdout=False) pop = Population(chromosomes=simple_chromosomes, eval_function=simple_evaluation_function, logger=logger) # we should see that a file was created with an appropriate number of rows pop.log() with open(log_file, "r") as f: assert len(f.readlines()) == len(simple_chromosomes) # we should see that a file was created with an appropriate number of rows pop.log() with open(log_file, "r") as f: assert len(f.readlines()) == (2 * len(simple_chromosomes)) read_stdout = [ line for line in capsys.readouterr().out.split('\n') if line != '' ] # there should be nothing printed assert len(read_stdout) == 0