Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Management command to create experiments/alternatives #180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions experiments/management/commands/create_experiment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.db import IntegrityError
from django.core.management import BaseCommand

from experiments.models import Experiment


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('name', type=str, help='Chosen name for this experiment'),
parser.add_argument('alternatives', metavar='alternative', type=str, nargs='+', help='New alternative names ("control" is automatically created)'),
parser.add_argument('--add', default=False, type=bool, help='Add alternatives to an existing experiment rather than create a new one'),

def handle(self, *args, **options):
if options['add']:
try:
experiment = Experiment.objects.get(name=options['name'])
except Experiment.DoesNotExist:
self.stdout.write(self.style.ERROR("Could not find an experiment named '%s'" % options['name']))
return
else:
try:
experiment = Experiment.objects.create(name=options['name'])
except IntegrityError:
self.stdout.write(self.style.ERROR("An experiment named '%s' already exists" % options['name']))
Copy link

@JonathanUsername JonathanUsername Jun 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.stderr is convention for errors surely

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raise CommandError("Wibble failure")

Just checked docs and this might be the recommended practice

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oooh

return

for alternative in options['alternatives']:
if ':' in alternative:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better we include the : syntax in help message for parser?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

alternative, weight = alternative.split(':')
experiment.ensure_alternative_exists(alternative, weight)
else:
experiment.ensure_alternative_exists(alternative)

self.stdout.write(self.style.SUCCESS("Experiment '%s' created" % experiment.name))