Skip to content

Commit

Permalink
Add --prune flag to genetic subcommand
Browse files Browse the repository at this point in the history
This addition will about rizza to weed out positive tests that no
longer pass, and remove them from the saved file.
Empty saved files will be deleted as well.

fixes #15
  • Loading branch information
JacobCallahan committed Jan 15, 2018
1 parent 8e9de99 commit 6377b18
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 20 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
History
=======

0.4.1(2018-01-15)
=================

+ Added --prune option to genetic command

0.4.0(2018-01-13)
=================

Expand Down
44 changes: 27 additions & 17 deletions rizza/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rizza.entity_tester import EntityTester
from rizza.genetic_tester import GeneticEntityTester
from rizza.helpers.config import Config
from rizza.helpers import prune
from rizza.task_manager import TaskManager


Expand Down Expand Up @@ -121,30 +122,39 @@ def genetic(self):
parser.add_argument(
"--fresh", action="store_true",
help="Don't attempt to load in saved results.")
parser.add_argument(
"--prune", action="store_true", help="Remove positive tests that "
"don't pass. Can specify 'All' for entity")
parser.add_argument(
"--debug", action="store_true",
help="Enable debug loggin level.")

args = parser.parse_args(sys.argv[2:])
self.conf.load_cli_args(args, command=True)

gtester = GeneticEntityTester(
config=self.conf,
entity=args.entity,
method=args.method,
population_count=args.population_count,
max_generations=args.max_generations,
max_recursive_generations=args.max_recursive_generations,
max_recursive_depth=args.max_recursive_depth,
disable_recursion=args.disable_recursion,
seek_bad=args.seek_bad,
fresh=args.fresh
)
self.conf.init_logger(
path='logs/{}.log'.format(gtester.test_name),
level='debug' if args.debug else None
)
gtester.run()
if args.prune:
self.conf.init_logger(
path='logs/prune.log', level='debug' if args.debug else None
)
prune.genetic_prune(self.conf, args.entity)
else:
gtester = GeneticEntityTester(
config=self.conf,
entity=args.entity,
method=args.method,
population_count=args.population_count,
max_generations=args.max_generations,
max_recursive_generations=args.max_recursive_generations,
max_recursive_depth=args.max_recursive_depth,
disable_recursion=args.disable_recursion,
seek_bad=args.seek_bad,
fresh=args.fresh
)
self.conf.init_logger(
path='logs/{}.log'.format(gtester.test_name),
level='debug' if args.debug else None
)
gtester.run()

def config(self):
parser = argparse.ArgumentParser()
Expand Down
6 changes: 4 additions & 2 deletions rizza/genetic_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@ def run(self, mock=False, save_only_passed=False):

def run_best(self):
"""Pull the best saved test, if any, run it, and return the id"""
self.config.RIZZA['GENETICS']['ALLOW RECURSION'] = False
self.config.RIZZA['GENETICS']['MAX GENERATIONS'] = 1
test = self._load_test()
if test:
test = self._genes_to_task(test)
logger.info('Creating {}...'.format(self.entity))
result = test.execute()
if 'pass' in result:
return result['pass'].get('id', 1)
return 1
return result['pass'].get('id', -1)
return -1
44 changes: 44 additions & 0 deletions rizza/helpers/prune.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- encoding: utf-8 -*-
"""A utility that tries saved genetic tests and removes those failing"""
import yaml
from pathlib import Path
from logzero import logger
from rizza import entity_tester
from rizza import genetic_tester


def genetic_prune(conf, entity='All'):
"""Check all saved genetic_tester tests for an entity, prune failures"""
if entity == 'All':
for target in list(entity_tester.EntityTester.pull_entities()):
genetic_prune(conf, target)
else:
test_file = Path('data/genetic_tests/{}.yaml'.format(entity))
logger.debug('Current target file: {}'.format(test_file))
to_remove = []
if test_file.exists() and test_file.stat().st_size > 10:
logger.debug('Beginning tests for {}'.format(entity))
tests = yaml.load(test_file.open('r'))
for test in tests:
ent, method, mode = test.split(' ')
if mode == 'positive':
logger.debug('Running test {}'.format(method))
result = genetic_tester.GeneticEntityTester(
conf, entity, method
).run_best()
if result == -1:
logger.debug('{} failed.'.format(test))
to_remove.append(test)
else:
logger.debug('{} passed.'.format(test))
for test in to_remove:
logger.warning('Removing {} from {}'.format(test, test_file))
del tests[test]
logger.debug('Deleting file {}'.format(test_file))
test_file.unlink()
logger.debug('Writing tests to {}'.format(test_file))
yaml.dump(tests, test_file.open('w+'), default_flow_style=False)
logger.info('Done pruning {}'.format(entity))
if test_file.exists() and test_file.stat().st_size < 10:
logger.warning('Deleting empty file {}'.format(test_file))
test_file.unlink()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name='rizza',
version='0.4.0',
version='0.4.1',
description="An increasingly intelligent method to test RH Satellite.",
long_description=readme + '\n\n' + history,
author="Jacob J Callahan",
Expand Down

0 comments on commit 6377b18

Please sign in to comment.