import os

from jupyterhub.auth import LocalAuthenticator
from jupyterhub.spawner import LocalProcessSpawner
from tornado import gen

class FakeUserAuth(LocalAuthenticator):
    """Authenticate fake users"""

    @gen.coroutine
    def authenticate(self, handler, data):
        """If the user is on the whitelist, authenticate regardless of password.
        If not, then don't authenticate.

        """
        username = data['username']
        if not self.check_whitelist(username):
            return
        return username

    @staticmethod
    def system_user_exists(user):
        return True

class FakeUserSpawner(LocalProcessSpawner):

    def user_env(self, env):
        env['USER'] = self.user.name
        env['HOME'] = os.getcwd()
        env['SHELL'] = '/bin/bash'
        return env

    def make_preexec_fn(self, name):
        home = os.getcwd()
        def preexec():
            # start in the cwd
            os.chdir(home)
        return preexec

Remove os.setpgrp() from fake spawner

Remove os.setpgrp() from fake spawner

Response:
import os

from jupyterhub.auth import LocalAuthenticator
from jupyterhub.spawner import LocalProcessSpawner
from tornado import gen

class FakeUserAuth(LocalAuthenticator):
    """Authenticate fake users"""

    @gen.coroutine
    def authenticate(self, handler, data):
        """If the user is on the whitelist, authenticate regardless of password.
        If not, then don't authenticate.

        """
        username = data['username']
        if not self.check_whitelist(username):
            return
        return username

    @staticmethod
    def system_user_exists(user):
        return True

class FakeUserSpawner(LocalProcessSpawner):

    def user_env(self, env):
        env['USER'] = self.user.name
        env['HOME'] = os.getcwd()
        env['SHELL'] = '/bin/bash'
        return env

    def make_preexec_fn(self, name):
        home = os.getcwd()
        def preexec():
            # start in the cwd
            os.chdir(home)
        return preexec

Prompt:
Remove os.setpgrp() from fake spawner

import os

from jupyterhub.auth import LocalAuthenticator
from jupyterhub.spawner import LocalProcessSpawner
from tornado import gen

class FakeUserAuth(LocalAuthenticator):
    """Authenticate fake users"""

    @gen.coroutine
    def authenticate(self, handler, data):
        """If the user is on the whitelist, authenticate regardless of password.
        If not, then don't authenticate.

        """
        username = data['username']
        if not self.check_whitelist(username):
            return
        return username

    @staticmethod
    def system_user_exists(user):
        return True

class FakeUserSpawner(LocalProcessSpawner):

    def user_env(self, env):
        env['USER'] = self.user.name
        env['HOME'] = os.getcwd()
        env['SHELL'] = '/bin/bash'
        return env

    def make_preexec_fn(self, name):
        home = os.getcwd()
        def preexec():
            # don't forward signals
            os.setpgrp()
            # start in the cwd
            os.chdir(home)
        return preexec


from django.shortcuts import redirect
from rest_framework import viewsets

from .models import User
from .permissions import IsUserOrReadOnly
from .serializers import AuthenticatedUserSerializer, UserSerializer

class UserViewSet(viewsets.ModelViewSet):

    """API endpoint for viewing and editing users."""

    queryset = User.objects.all()
    permission_classes = (IsUserOrReadOnly,)

    def get_serializer_class(self):
        return (AuthenticatedUserSerializer
                if self.request.user == self.get_object()
                else UserSerializer)

    def retrieve(self, request, pk=None):
        """Retrieve given user or current user if ``pk`` is "me"."""
        if pk == 'me' and request.user.is_authenticated():
            return redirect('user-detail', request.user.pk)
        else:
            return super().retrieve(request, pk)

Use Python 3 style for super

Use Python 3 style for super

Response:
from django.shortcuts import redirect
from rest_framework import viewsets

from .models import User
from .permissions import IsUserOrReadOnly
from .serializers import AuthenticatedUserSerializer, UserSerializer

class UserViewSet(viewsets.ModelViewSet):

    """API endpoint for viewing and editing users."""

    queryset = User.objects.all()
    permission_classes = (IsUserOrReadOnly,)

    def get_serializer_class(self):
        return (AuthenticatedUserSerializer
                if self.request.user == self.get_object()
                else UserSerializer)

    def retrieve(self, request, pk=None):
        """Retrieve given user or current user if ``pk`` is "me"."""
        if pk == 'me' and request.user.is_authenticated():
            return redirect('user-detail', request.user.pk)
        else:
            return super().retrieve(request, pk)

Prompt:
Use Python 3 style for super

from django.shortcuts import redirect
from rest_framework import viewsets

from .models import User
from .permissions import IsUserOrReadOnly
from .serializers import AuthenticatedUserSerializer, UserSerializer

class UserViewSet(viewsets.ModelViewSet):

    """API endpoint for viewing and editing users."""

    queryset = User.objects.all()
    permission_classes = (IsUserOrReadOnly,)

    def get_serializer_class(self):
        return (AuthenticatedUserSerializer
                if self.request.user == self.get_object()
                else UserSerializer)

    def retrieve(self, request, pk=None):
        """Retrieve given user or current user if ``pk`` is "me"."""
        if pk == 'me' and request.user.is_authenticated():
            return redirect('user-detail', request.user.pk)
        else:
            return super(UserViewSet, self).retrieve(request, pk)


from django.db import models

# Route model
# Start and end locations with additional stop-overs
class Route(models.Model):
    origin = models.CharField(max_length=63)
    destination = models.CharField(max_length=63)

    def __unicode__(self):
        return "{} to {}".format(
            self.origin,
            self.destination
            )

class Waypoint(models.Model):
    waypoint = models.CharField(max_length=63)
    route = models.ForeignKey(Route)

    def __unicode__(self):
        return str(self.waypoint)

# RoadTrip model
# Start and end dates, Route and TripDetails
class RoadTrip(models.Model):
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    route = models.OneToOneField(Route)

    def __unicode__(self):
        return "{} from {} to {}".format(
            self.route,
            self.start_date,
            self.end_date
            )

# TripDetail model
# Additional trip details, such as traveling with children or pets
class TripDetail(models.Model):
    description = models.CharField(max_length=127)
    trip = models.ForeignKey(RoadTrip)

    def __unicode__(self):
        return str(self.description)

Add unicode methods to model classes

Add unicode methods to model classes

Response:
from django.db import models

# Route model
# Start and end locations with additional stop-overs
class Route(models.Model):
    origin = models.CharField(max_length=63)
    destination = models.CharField(max_length=63)

    def __unicode__(self):
        return "{} to {}".format(
            self.origin,
            self.destination
            )

class Waypoint(models.Model):
    waypoint = models.CharField(max_length=63)
    route = models.ForeignKey(Route)

    def __unicode__(self):
        return str(self.waypoint)

# RoadTrip model
# Start and end dates, Route and TripDetails
class RoadTrip(models.Model):
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    route = models.OneToOneField(Route)

    def __unicode__(self):
        return "{} from {} to {}".format(
            self.route,
            self.start_date,
            self.end_date
            )

# TripDetail model
# Additional trip details, such as traveling with children or pets
class TripDetail(models.Model):
    description = models.CharField(max_length=127)
    trip = models.ForeignKey(RoadTrip)

    def __unicode__(self):
        return str(self.description)

Prompt:
Add unicode methods to model classes

from django.db import models

# Route model
# Start and end locations with additional stop-overs
class Route(models.Model):
    origin = models.CharField(max_length=63)
    destination = models.CharField(max_length=63)

class Waypoint(models.Model):
    waypoint = models.CharField(max_length=63)
    route = models.ForeignKey(Route)

# RoadTrip model
# Start and end dates, Route and TripDetails
class RoadTrip(models.Model):
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    route = models.OneToOneField(Route)

# TripDetail model
# Additional trip details, such as traveling with children or pets
class TripDetail(models.Model):
    description = models.CharField(max_length=127)
    trip = models.ForeignKey(RoadTrip)


from mopidy.models import Track

class BaseCurrentPlaylistControllerTest(object):
    uris = []
    backend_class = None

    def setUp(self):
        self.backend = self.backend_class()

    def test_add(self):
        playlist = self.backend.current_playlist

        for uri in self.uris:
            playlist.add(uri)
            self.assertEqual(uri, playlist.tracks[-1].uri)

    def test_add_at_position(self):
        playlist = self.backend.current_playlist

        for uri in self.uris:
            playlist.add(uri, 0)
            self.assertEqual(uri, playlist.tracks[0].uri)

        # FIXME test other placements

class BasePlaybackControllerTest(object):
    backend_class = None

    def setUp(self):
        self.backend = self.backend_class()

    def test_play_with_no_current_track(self):
        playback = self.backend.playback

        self.assertEqual(playback.state, playback.STOPPED)

        result = playback.play()

        self.assertEqual(result, False)
        self.assertEqual(playback.state, playback.STOPPED)

    def test_next(self):
        playback = self.backend.playback

        current_song = playback.playlist_position

        playback.next()

        self.assertEqual(playback.playlist_position, current_song+1)

Response:
from mopidy.models import Track

class BaseCurrentPlaylistControllerTest(object):
    uris = []
    backend_class = None

    def setUp(self):
        self.backend = self.backend_class()

    def test_add(self):
        playlist = self.backend.current_playlist

        for uri in self.uris:
            playlist.add(uri)
            self.assertEqual(uri, playlist.tracks[-1].uri)

    def test_add_at_position(self):
        playlist = self.backend.current_playlist

        for uri in self.uris:
            playlist.add(uri, 0)
            self.assertEqual(uri, playlist.tracks[0].uri)

        # FIXME test other placements

class BasePlaybackControllerTest(object):
    backend_class = None

    def setUp(self):
        self.backend = self.backend_class()

    def test_play_with_no_current_track(self):
        playback = self.backend.playback

        self.assertEqual(playback.state, playback.STOPPED)

        result = playback.play()

        self.assertEqual(result, False)
        self.assertEqual(playback.state, playback.STOPPED)

    def test_next(self):
        playback = self.backend.playback

        current_song = playback.playlist_position

        playback.next()

        self.assertEqual(playback.playlist_position, current_song+1)

Prompt:
Update test to check basic case for playback without current track

from mopidy.models import Track

class BaseCurrentPlaylistControllerTest(object):
    uris = []
    backend_class = None

    def setUp(self):
        self.backend = self.backend_class()

    def test_add(self):
        playlist = self.backend.current_playlist

        for uri in self.uris:
            playlist.add(uri)
            self.assertEqual(uri, playlist.tracks[-1].uri)

    def test_add_at_position(self):
        playlist = self.backend.current_playlist

        for uri in self.uris:
            playlist.add(uri, 0)
            self.assertEqual(uri, playlist.tracks[0].uri)

        # FIXME test other placements

class BasePlaybackControllerTest(object):
    backend_class = None

    def setUp(self):
        self.backend = self.backend_class()

    def test_play(self):
        playback = self.backend.playback

        self.assertEqual(playback.state, playback.STOPPED)

        playback.play()

        self.assertEqual(playback.state, playback.PLAYING)

    def test_next(self):
        playback = self.backend.playback

        current_song = playback.playlist_position

        playback.next()

        self.assertEqual(playback.playlist_position, current_song+1)


"""Test code format and coding standards."""
import importlib
import inspect
import pkgutil
import unittest

def setUpModule():
    global modules
    modules = [name for _, name, ispkg in pkgutil.walk_packages(['rewind'],
                                                                'rewind.')
               if not ispkg and not name.startswith('rewind.test.') and
                  not name.startswith('rewind.messages.')]
    assert modules, "Expected to have found a couple of modules. Did not."
    modules = map(importlib.import_module, modules)

def tearDownModule():
    """Clearing up global namespace in test_code."""
    global modules
    del modules

def _get_public_classes_from_object(obj, prepend_name=''):
    classes = [(prepend_name+name, value)
               for name, value in inspect.getmembers(obj)
               if inspect.isclass(value) and not name.startswith('_')]
    result = list(classes)
    for name, value in classes:
        partialres = _get_public_classes_from_object(value,
                                                     '{0}.'.format(name))
        result.extend(partialres)
    return result

def _get_public_classes():
    classes = []
    for module in modules:
        assert inspect.ismodule(module)
        someclasses = _get_public_classes_from_object(module,
                                                      '{0}.'.format(module.__name__))
        classes.extend(someclasses)
    return classes

class TestPydoc(unittest.TestCase):
    """Tests for pydoc."""

    def testAllPublicClasses(self):
        """Test that all public classes have a pydoc."""
        classes = _get_public_classes()
        self.assertNotEqual(len(classes), 0)
        for classname, clazz in classes:
            doc = inspect.getdoc(clazz)
            msg = "{0} lacks a Pydoc string.".format(classname)
            self.assertTrue(doc and len(doc) > 4, msg)

Test that asserts all public classes have pydoc

Response:
"""Test code format and coding standards."""
import importlib
import inspect
import pkgutil
import unittest

def setUpModule():
    global modules
    modules = [name for _, name, ispkg in pkgutil.walk_packages(['rewind'],
                                                                'rewind.')
               if not ispkg and not name.startswith('rewind.test.') and
                  not name.startswith('rewind.messages.')]
    assert modules, "Expected to have found a couple of modules. Did not."
    modules = map(importlib.import_module, modules)

def tearDownModule():
    """Clearing up global namespace in test_code."""
    global modules
    del modules

def _get_public_classes_from_object(obj, prepend_name=''):
    classes = [(prepend_name+name, value)
               for name, value in inspect.getmembers(obj)
               if inspect.isclass(value) and not name.startswith('_')]
    result = list(classes)
    for name, value in classes:
        partialres = _get_public_classes_from_object(value,
                                                     '{0}.'.format(name))
        result.extend(partialres)
    return result

def _get_public_classes():
    classes = []
    for module in modules:
        assert inspect.ismodule(module)
        someclasses = _get_public_classes_from_object(module,
                                                      '{0}.'.format(module.__name__))
        classes.extend(someclasses)
    return classes

class TestPydoc(unittest.TestCase):
    """Tests for pydoc."""

    def testAllPublicClasses(self):
        """Test that all public classes have a pydoc."""
        classes = _get_public_classes()
        self.assertNotEqual(len(classes), 0)
        for classname, clazz in classes:
            doc = inspect.getdoc(clazz)
            msg = "{0} lacks a Pydoc string.".format(classname)
            self.assertTrue(doc and len(doc) > 4, msg)

Prompt: Test that asserts all public classes have pydoc


"""Py.test doctest custom plugin"""
# By Danilo J. S. Bellini
import sys, functools

def printer(value):
    """Prints the object representation using the given custom formatter."""
    if value is not None:
        print(printer.repr(value)) # This attribute has to be set elsewhere

def temp_replace(obj, attr_name, value):
    """
    Returns a decorator that replaces obj.attr = value before calling the
    wrapped function and restores obj.attr afterwards.
    """
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            backup = getattr(obj, attr_name)
            setattr(obj, attr_name, value)
            result = func(*args, **kwargs)
            setattr(obj, attr_name, backup)
            return result
        return wrapper
    return decorator

def pytest_configure(config):
    """
    Hook for changing ``doctest.DocTestRunner.run`` method so that the
    ``sys.__displayhook__`` calls the given printer function while a doctest
    is running.
    """
    import doctest
    enable_printer = temp_replace(sys, "__displayhook__", printer)
    doctest.DocTestRunner.run = enable_printer(doctest.DocTestRunner.run)
    # As the public method doctest.DocTestRunner.run replaces sys.displayhook
    # by sys.__displayhook__, we could also had changed "displayhook" on the
    # _DocTestRunner__run protected method

Create the plugin based on PyScanPrev conftest.py

Response:
"""Py.test doctest custom plugin"""
# By Danilo J. S. Bellini
import sys, functools

def printer(value):
    """Prints the object representation using the given custom formatter."""
    if value is not None:
        print(printer.repr(value)) # This attribute has to be set elsewhere

def temp_replace(obj, attr_name, value):
    """
    Returns a decorator that replaces obj.attr = value before calling the
    wrapped function and restores obj.attr afterwards.
    """
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            backup = getattr(obj, attr_name)
            setattr(obj, attr_name, value)
            result = func(*args, **kwargs)
            setattr(obj, attr_name, backup)
            return result
        return wrapper
    return decorator

def pytest_configure(config):
    """
    Hook for changing ``doctest.DocTestRunner.run`` method so that the
    ``sys.__displayhook__`` calls the given printer function while a doctest
    is running.
    """
    import doctest
    enable_printer = temp_replace(sys, "__displayhook__", printer)
    doctest.DocTestRunner.run = enable_printer(doctest.DocTestRunner.run)
    # As the public method doctest.DocTestRunner.run replaces sys.displayhook
    # by sys.__displayhook__, we could also had changed "displayhook" on the
    # _DocTestRunner__run protected method

Prompt: Create the plugin based on PyScanPrev conftest.py


#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import uuid

from functional.common import test

class VolumeQosTests(test.TestCase):
    """Functional tests for volume qos. """

    NAME = uuid.uuid4().hex
    HEADERS = ['Name']
    FIELDS = ['id', 'name']
    ID = None

    @classmethod
    def setUpClass(cls):
        opts = cls.get_show_opts(cls.FIELDS)
        raw_output = cls.openstack('volume qos create ' + cls.NAME + opts)
        cls.ID, name, rol = raw_output.split('\n')
        cls.assertOutput(cls.NAME, name)

    @classmethod
    def tearDownClass(cls):
        raw_output = cls.openstack('volume qos delete ' + cls.ID)
        cls.assertOutput('', raw_output)

    def test_volume_qos_list(self):
        opts = self.get_list_opts(self.HEADERS)
        raw_output = self.openstack('volume qos list' + opts)
        self.assertIn(self.NAME, raw_output)

    def test_volume_qos_show(self):
        opts = self.get_show_opts(self.FIELDS)
        raw_output = self.openstack('volume qos show ' + self.ID + opts)
        self.assertEqual(self.ID + "\n" + self.NAME + "\n", raw_output)

    def test_volume_qos_metadata(self):
        raw_output = self.openstack(
            'volume qos set --property a=b --property c=d ' + self.ID)
        self.assertEqual("", raw_output)
        opts = self.get_show_opts(['name', 'specs'])
        raw_output = self.openstack('volume qos show ' + self.ID + opts)
        self.assertEqual(self.NAME + "\na='b', c='d'\n", raw_output)

Add functional tests for volume qos

Add functional tests for volume qos

Change-Id: I80010b56b399bc027ac864304be60a3ee53bda00

Response:
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import uuid

from functional.common import test

class VolumeQosTests(test.TestCase):
    """Functional tests for volume qos. """

    NAME = uuid.uuid4().hex
    HEADERS = ['Name']
    FIELDS = ['id', 'name']
    ID = None

    @classmethod
    def setUpClass(cls):
        opts = cls.get_show_opts(cls.FIELDS)
        raw_output = cls.openstack('volume qos create ' + cls.NAME + opts)
        cls.ID, name, rol = raw_output.split('\n')
        cls.assertOutput(cls.NAME, name)

    @classmethod
    def tearDownClass(cls):
        raw_output = cls.openstack('volume qos delete ' + cls.ID)
        cls.assertOutput('', raw_output)

    def test_volume_qos_list(self):
        opts = self.get_list_opts(self.HEADERS)
        raw_output = self.openstack('volume qos list' + opts)
        self.assertIn(self.NAME, raw_output)

    def test_volume_qos_show(self):
        opts = self.get_show_opts(self.FIELDS)
        raw_output = self.openstack('volume qos show ' + self.ID + opts)
        self.assertEqual(self.ID + "\n" + self.NAME + "\n", raw_output)

    def test_volume_qos_metadata(self):
        raw_output = self.openstack(
            'volume qos set --property a=b --property c=d ' + self.ID)
        self.assertEqual("", raw_output)
        opts = self.get_show_opts(['name', 'specs'])
        raw_output = self.openstack('volume qos show ' + self.ID + opts)
        self.assertEqual(self.NAME + "\na='b', c='d'\n", raw_output)

Prompt:
Add functional tests for volume qos

Change-Id: I80010b56b399bc027ac864304be60a3ee53bda00


#!/usr/bin/env python
import os
from optparse import OptionParser

from jinja2 import Template

TEMPLATE = open(os.path.join(os.path.dirname(__file__), 'crontab.tpl')).read()

def main():
    parser = OptionParser()
    parser.add_option("-k", "--kitsune",
                      help="Location of kitsune (required)")
    parser.add_option("-u", "--user",
                      help=("Prefix cron with this user. "
                           "Only define for cron.d style crontabs"))
    parser.add_option("-p", "--python", default="/usr/bin/python2.6",
                      help="Python interpreter to use")

    (opts, args) = parser.parse_args()

    if not opts.kitsune:
        parser.error("-k must be defined")

    # To pick up the right PyOpenSSL:
    python_path = 'PYTHONPATH=/usr/local/lib64/python2.6/site-packages'

    ctx = {'django': 'cd %s; %s %s manage.py' % (
        opts.kitsune, python_path, opts.python),}
    ctx['cron'] = '%s cron' % ctx['django']

    if opts.user:
        for k, v in ctx.iteritems():
            ctx[k] = '%s %s' % (opts.user, v)

    # Needs to stay below the opts.user injection.
    ctx['python'] = opts.python

    print Template(TEMPLATE).render(**ctx)

if __name__ == "__main__":
    main()

Add local site-packages to PYTHONPATH.

Response:
#!/usr/bin/env python
import os
from optparse import OptionParser

from jinja2 import Template

TEMPLATE = open(os.path.join(os.path.dirname(__file__), 'crontab.tpl')).read()

def main():
    parser = OptionParser()
    parser.add_option("-k", "--kitsune",
                      help="Location of kitsune (required)")
    parser.add_option("-u", "--user",
                      help=("Prefix cron with this user. "
                           "Only define for cron.d style crontabs"))
    parser.add_option("-p", "--python", default="/usr/bin/python2.6",
                      help="Python interpreter to use")

    (opts, args) = parser.parse_args()

    if not opts.kitsune:
        parser.error("-k must be defined")

    # To pick up the right PyOpenSSL:
    python_path = 'PYTHONPATH=/usr/local/lib64/python2.6/site-packages'

    ctx = {'django': 'cd %s; %s %s manage.py' % (
        opts.kitsune, python_path, opts.python),}
    ctx['cron'] = '%s cron' % ctx['django']

    if opts.user:
        for k, v in ctx.iteritems():
            ctx[k] = '%s %s' % (opts.user, v)

    # Needs to stay below the opts.user injection.
    ctx['python'] = opts.python

    print Template(TEMPLATE).render(**ctx)

if __name__ == "__main__":
    main()

Prompt:
Add local site-packages to PYTHONPATH.

To pick up the local version of PyOpenSSL.

#!/usr/bin/env python
import os
from optparse import OptionParser

from jinja2 import Template

TEMPLATE = open(os.path.join(os.path.dirname(__file__), 'crontab.tpl')).read()

def main():
    parser = OptionParser()
    parser.add_option("-k", "--kitsune",
                      help="Location of kitsune (required)")
    parser.add_option("-u", "--user",
                      help=("Prefix cron with this user. "
                           "Only define for cron.d style crontabs"))
    parser.add_option("-p", "--python", default="/usr/bin/python2.6",
                      help="Python interpreter to use")

    (opts, args) = parser.parse_args()

    if not opts.kitsune:
        parser.error("-k must be defined")

    ctx = {'django': 'cd %s; %s manage.py' % (opts.kitsune, opts.python),}
    ctx['cron'] = '%s cron' % ctx['django']

    if opts.user:
        for k, v in ctx.iteritems():
            ctx[k] = '%s %s' % (opts.user, v)

    # Needs to stay below the opts.user injection.
    ctx['python'] = opts.python

    print Template(TEMPLATE).render(**ctx)

if __name__ == "__main__":
    main()


#!/usr/bin/env python

import sys
import os
import unittest

from straight.plugin.loader import StraightPluginLoader

class PluginTestCase(unittest.TestCase):

    def setUp(self):
        self.loader = StraightPluginLoader()
        sys.path.append(os.path.join(os.path.dirname(__file__), 'more-test-plugins'))
        sys.path.append(os.path.join(os.path.dirname(__file__), 'some-test-plugins'))

    def tearDown(self):
        del sys.path[-1]
        del sys.path[-1]

    def test_load(self):
        modules = list(self.loader.load('testplugin'))
        assert len(modules) == 2, modules

    def test_plugin(self):
        assert self.loader.load('testplugin')[0].do(1) == 2

if __name__ == '__main__':
    unittest.main()

Response:
#!/usr/bin/env python

import sys
import os
import unittest

from straight.plugin.loader import StraightPluginLoader

class PluginTestCase(unittest.TestCase):

    def setUp(self):
        self.loader = StraightPluginLoader()
        sys.path.append(os.path.join(os.path.dirname(__file__), 'more-test-plugins'))
        sys.path.append(os.path.join(os.path.dirname(__file__), 'some-test-plugins'))

    def tearDown(self):
        del sys.path[-1]
        del sys.path[-1]

    def test_load(self):
        modules = list(self.loader.load('testplugin'))
        assert len(modules) == 2, modules

    def test_plugin(self):
        assert self.loader.load('testplugin')[0].do(1) == 2

if __name__ == '__main__':
    unittest.main()

Prompt:
Fix test case for multiple locations of a namespace

#!/usr/bin/env python

import sys
import os
import unittest

from straight.plugin.loader import StraightPluginLoader

class PluginTestCase(unittest.TestCase):

    def setUp(self):
        self.loader = StraightPluginLoader()
        self.added_path = os.path.join(os.path.dirname(__file__), 'more-test-plugins')
        self.added_path = os.path.join(os.path.dirname(__file__), 'some-test-plugins')
        sys.path.append(self.added_path)

    def tearDown(self):
        del sys.path[-1]
        del sys.path[-1]

    def test_load(self):
        modules = list(self.loader.load('testplugin'))
        assert len(modules) == 2, modules

    def test_plugin(self):
        assert self.loader.load('testplugin')[0].do(1) == 2

if __name__ == '__main__':
    unittest.main()


import os
import sys
import unittest

from autosklearn.pipeline.components.base import find_components, \
    AutoSklearnClassificationAlgorithm

this_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(this_dir)

class TestBase(unittest.TestCase):

    def test_find_components(self):
        c = find_components('dummy_components',
                            os.path.join(this_dir, 'dummy_components'),
                            AutoSklearnClassificationAlgorithm)
        self.assertEqual(len(c), 2)
        self.assertEqual(c['dummy_component_1'].__name__, 'DummyComponent1')
        self.assertEqual(c['dummy_component_2'].__name__, 'DummyComponent2')

Response:
import os
import sys
import unittest

from autosklearn.pipeline.components.base import find_components, \
    AutoSklearnClassificationAlgorithm

this_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(this_dir)

class TestBase(unittest.TestCase):

    def test_find_components(self):
        c = find_components('dummy_components',
                            os.path.join(this_dir, 'dummy_components'),
                            AutoSklearnClassificationAlgorithm)
        self.assertEqual(len(c), 2)
        self.assertEqual(c['dummy_component_1'].__name__, 'DummyComponent1')
        self.assertEqual(c['dummy_component_2'].__name__, 'DummyComponent2')

Prompt:
FIX fix unit test by fixing import paths

import unittest

from autosklearn.pipeline.components.base import find_components, \
    AutoSklearnClassificationAlgorithm

class TestBase(unittest.TestCase):

    def test_find_components(self):
        c = find_components('dummy_components', 'dummy_components',
                            AutoSklearnClassificationAlgorithm)
        self.assertEqual(len(c), 2)
        self.assertEqual(c['dummy_component_1'].__name__, 'DummyComponent1')
        self.assertEqual(c['dummy_component_2'].__name__, 'DummyComponent2')


#!/usr/bin/env python
'''
A CLI tool for formulating an Abba url using data from PostgreSQL
'''

from __future__ import print_function

import argparse
import psycopg2
import sys

TOOL_DESCRIPTION = '''
Formulates an Abba url using data from PostgreSQL

The query passed to this tool should return three columns, which will
become the label, success count, and trial count in Abba.  If the query
flag is not specified, the query will be taken from standard input.

Note that the db parameters are optional, and if not provided psycopg2
will attempt to connect to the default locally-hosted database.
'''

def parse_arguments():
    '''
    Parse the arguments from the command line for this program
    '''
    parser = argparse.ArgumentParser(
        description=TOOL_DESCRIPTION,
        formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument(
        '-d', '--db_params', metavar='PARAMS',
        help='A libpq connection string with params for the target database'
    )
    parser.add_argument(
        '-q', '--query',
        help='The query which will provide the data for Abba',
    )
    return parser.parse_args()

def build_url_from_database_query(dsn, query):
    '''
    Build an Abba URL using data from a PostgreSQL connection and query
    '''
    url_template = 'http://thumbtack.com/labs/abba#{}'

    cursor = psycopg2.connect(dsn).cursor()
    cursor.execute(query)

    if not cursor.rowcount:
        return url_template.format('')

    rows = cursor.fetchall()
    if len(rows[0]) != 3:
        raise ValueError('Query does not return 3 columns of data')

    groups_querystr = '&'.join('{}={}%2C{}'.format(*row) for row in rows)

    return url_template.format(groups_querystr)

def main():
    args = parse_arguments()
    query = args.query if args.query is not None else sys.stdin.read()
    params = args.db_params if args.db_params else ''

    print(build_url_from_database_query(params, query))

if __name__ == '__main__':
    main()

Add tool for pulling data from PostgreSQL to Abba

Response:
#!/usr/bin/env python
'''
A CLI tool for formulating an Abba url using data from PostgreSQL
'''

from __future__ import print_function

import argparse
import psycopg2
import sys

TOOL_DESCRIPTION = '''
Formulates an Abba url using data from PostgreSQL

The query passed to this tool should return three columns, which will
become the label, success count, and trial count in Abba.  If the query
flag is not specified, the query will be taken from standard input.

Note that the db parameters are optional, and if not provided psycopg2
will attempt to connect to the default locally-hosted database.
'''

def parse_arguments():
    '''
    Parse the arguments from the command line for this program
    '''
    parser = argparse.ArgumentParser(
        description=TOOL_DESCRIPTION,
        formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument(
        '-d', '--db_params', metavar='PARAMS',
        help='A libpq connection string with params for the target database'
    )
    parser.add_argument(
        '-q', '--query',
        help='The query which will provide the data for Abba',
    )
    return parser.parse_args()

def build_url_from_database_query(dsn, query):
    '''
    Build an Abba URL using data from a PostgreSQL connection and query
    '''
    url_template = 'http://thumbtack.com/labs/abba#{}'

    cursor = psycopg2.connect(dsn).cursor()
    cursor.execute(query)

    if not cursor.rowcount:
        return url_template.format('')

    rows = cursor.fetchall()
    if len(rows[0]) != 3:
        raise ValueError('Query does not return 3 columns of data')

    groups_querystr = '&'.join('{}={}%2C{}'.format(*row) for row in rows)

    return url_template.format(groups_querystr)

def main():
    args = parse_arguments()
    query = args.query if args.query is not None else sys.stdin.read()
    params = args.db_params if args.db_params else ''

    print(build_url_from_database_query(params, query))

if __name__ == '__main__':
    main()

Prompt: Add tool for pulling data from PostgreSQL to Abba


from .settings import CONNECTION_NAME

class Log4DjangoRouter(object):
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'log4django':
            return CONNECTION_NAME
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'log4django':
            return CONNECTION_NAME
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label == 'log4django' or obj2._meta.app_label == 'log4django':
            return True
        return None

    def allow_syncdb(self, db, model):
        if db == CONNECTION_NAME and model._meta.app_label == 'log4django':
            return True
        return None

Fix syncdb in database router

Fix syncdb in database router

Response:
from .settings import CONNECTION_NAME

class Log4DjangoRouter(object):
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'log4django':
            return CONNECTION_NAME
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'log4django':
            return CONNECTION_NAME
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label == 'log4django' or obj2._meta.app_label == 'log4django':
            return True
        return None

    def allow_syncdb(self, db, model):
        if db == CONNECTION_NAME and model._meta.app_label == 'log4django':
            return True
        return None

Prompt:
Fix syncdb in database router

from .settings import CONNECTION_NAME

class Log4DjangoRouter(object):
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'log4django':
            return CONNECTION_NAME
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'log4django':
            return CONNECTION_NAME
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label == 'log4django' or obj2._meta.app_label == 'log4django':
            return True
        return None

    def allow_syncdb(self, db, model):
        if db == CONNECTION_NAME:
            return model._meta.app_label == 'log4django'
        elif model._meta.app_label == 'log4django':
            return False
        return None


"""
Usage: python -m recipy example_script3.py OUTPUT.npy
"""

from __future__ import print_function
import sys

import numpy

if len(sys.argv) < 2:
    print(__doc__, file=sys.stderr)
    sys.exit(1)

arr = numpy.arange(10)
arr = arr + 500
# We've made a fairly big change here!

numpy.save(sys.argv[1], arr)

Add example script for python -m recipy usage

Response:
"""
Usage: python -m recipy example_script3.py OUTPUT.npy
"""

from __future__ import print_function
import sys

import numpy

if len(sys.argv) < 2:
    print(__doc__, file=sys.stderr)
    sys.exit(1)

arr = numpy.arange(10)
arr = arr + 500
# We've made a fairly big change here!

numpy.save(sys.argv[1], arr)

Prompt: Add example script for python -m recipy usage


import sys
import logging

# NOTE: Be sure to keep the logging level commented out before commiting.  See:
#   https://github.com/numba/numba/issues/31
# A good work around is to make your tests handle a debug flag, per
# numba.tests.test_support.main().

class _RedirectingHandler(logging.Handler):
    '''
    A log hanlder that applies its formatter and redirect the emission
    to a parent handler.
    '''
    def set_handler(self, handler):
        self.handler = handler

    def emit(self, record):
        # apply our own formatting
        record.msg = self.format(record)
        record.args = [] # clear the args
        # use parent handler to emit record
        self.handler.emit(record)

def _config_logger():
    root = logging.getLogger(__name__)
    format = "\n\033[1m%(levelname)s -- "\
             "%(module)s:%(lineno)d:%(funcName)s\033[0m\n%(message)s"
    try:
        parent_hldr = root.parent.handlers[0]
    except IndexError: # parent handler is not initialized?
        # build our own handler --- uses sys.stderr by default.
        parent_hldr = logging.StreamHandler()
    hldr = _RedirectingHandler()
    hldr.set_handler(parent_hldr)
    fmt = logging.Formatter(format)
    hldr.setFormatter(fmt)
    root.addHandler(hldr)
    root.propagate = False # do not propagate to the root logger

_config_logger()

try:
    from . import minivect
except ImportError:
    print(logging.error("Did you forget to update submodule minivect?"))
    print(logging.error("Run 'git submodule init' followed by 'git submodule update'"))
    raise

from . import _numba_types
from ._numba_types import *
from . import decorators
from .decorators import *

def test():
    raise Exception("run nosetests from the numba directory")

__all__ = _numba_types.__all__ + decorators.__all__

Response:
import sys
import logging

# NOTE: Be sure to keep the logging level commented out before commiting.  See:
#   https://github.com/numba/numba/issues/31
# A good work around is to make your tests handle a debug flag, per
# numba.tests.test_support.main().

class _RedirectingHandler(logging.Handler):
    '''
    A log hanlder that applies its formatter and redirect the emission
    to a parent handler.
    '''
    def set_handler(self, handler):
        self.handler = handler

    def emit(self, record):
        # apply our own formatting
        record.msg = self.format(record)
        record.args = [] # clear the args
        # use parent handler to emit record
        self.handler.emit(record)

def _config_logger():
    root = logging.getLogger(__name__)
    format = "\n\033[1m%(levelname)s -- "\
             "%(module)s:%(lineno)d:%(funcName)s\033[0m\n%(message)s"
    try:
        parent_hldr = root.parent.handlers[0]
    except IndexError: # parent handler is not initialized?
        # build our own handler --- uses sys.stderr by default.
        parent_hldr = logging.StreamHandler()
    hldr = _RedirectingHandler()
    hldr.set_handler(parent_hldr)
    fmt = logging.Formatter(format)
    hldr.setFormatter(fmt)
    root.addHandler(hldr)
    root.propagate = False # do not propagate to the root logger

_config_logger()

try:
    from . import minivect
except ImportError:
    print(logging.error("Did you forget to update submodule minivect?"))
    print(logging.error("Run 'git submodule init' followed by 'git submodule update'"))
    raise

from . import _numba_types
from ._numba_types import *
from . import decorators
from .decorators import *

def test():
    raise Exception("run nosetests from the numba directory")

__all__ = _numba_types.__all__ + decorators.__all__

Prompt:
Update logging facility.  Don't overide root logger with basicConfig.

import sys
import logging

# NOTE: Be sure to keep the logging level commented out before commiting.  See:
#   https://github.com/numba/numba/issues/31
# A good work around is to make your tests handle a debug flag, per
# numba.tests.test_support.main().
logging.basicConfig(#level=logging.DEBUG,
                    format="\n\033[1m%(levelname)s -- %(module)s:%(lineno)d:%(funcName)s\033[0m\n%(message)s")

try:
    from . import minivect
except ImportError:
    print(logging.error("Did you forget to update submodule minivect?"))
    print(logging.error("Run 'git submodule init' followed by 'git submodule update'"))
    raise

from . import _numba_types
from ._numba_types import *
from . import decorators
from .decorators import *

def test():
    raise Exception("run nosetests from the numba directory")

__all__ = _numba_types.__all__ + decorators.__all__


import featureflow as ff
from random_samples import ShuffledSamples
from random_samples import InfiniteSampler
from preprocess import PreprocessingPipeline

def learning_pipeline():
    class LearningPipeline(ff.BaseModel):
        samples = ff.PickleFeature(ff.IteratorNode)

        shuffled = ff.PickleFeature(
            ShuffledSamples,
            nsamples=ff.Var('nsamples'),
            dtype=ff.Var('dtype'),
            needs=samples)

    return LearningPipeline

def infinite_streaming_learning_pipeline(cls):
    roots = filter(lambda feature: feature.is_root, cls.features.itervalues())

    if len(roots) != 1:
        raise ValueError('cls must have a single root feature')

    root = roots[0]

    class InfiniteLearningPipeline(cls):
        dataset = ff.Feature(
            InfiniteSampler,
            nsamples=ff.Var('nsamples'),
            dtype=ff.Var('dtype'),
            feature_filter=ff.Var('feature_filter'),
            parallel=ff.Var('parallel'))

        pipeline = ff.ClobberPickleFeature(
            PreprocessingPipeline,
            needs=cls.features,
            store=True)

        @classmethod
        def load_network(cls):
            if not cls.exists():
                raise RuntimeError('No network has been trained or saved')

            instance = cls()
            for p in instance.pipeline:
                try:
                    return p.network
                except AttributeError:
                    pass

            raise RuntimeError('There is no network in the pipeline')

    root.needs = InfiniteLearningPipeline.dataset
    InfiniteLearningPipeline.__name__ = cls.__name__
    InfiniteLearningPipeline.__module__ = cls.__module__

    return InfiniteLearningPipeline

Response:
import featureflow as ff
from random_samples import ShuffledSamples
from random_samples import InfiniteSampler
from preprocess import PreprocessingPipeline

def learning_pipeline():
    class LearningPipeline(ff.BaseModel):
        samples = ff.PickleFeature(ff.IteratorNode)

        shuffled = ff.PickleFeature(
            ShuffledSamples,
            nsamples=ff.Var('nsamples'),
            dtype=ff.Var('dtype'),
            needs=samples)

    return LearningPipeline

def infinite_streaming_learning_pipeline(cls):
    roots = filter(lambda feature: feature.is_root, cls.features.itervalues())

    if len(roots) != 1:
        raise ValueError('cls must have a single root feature')

    root = roots[0]

    class InfiniteLearningPipeline(cls):
        dataset = ff.Feature(
            InfiniteSampler,
            nsamples=ff.Var('nsamples'),
            dtype=ff.Var('dtype'),
            feature_filter=ff.Var('feature_filter'),
            parallel=ff.Var('parallel'))

        pipeline = ff.ClobberPickleFeature(
            PreprocessingPipeline,
            needs=cls.features,
            store=True)

        @classmethod
        def load_network(cls):
            if not cls.exists():
                raise RuntimeError('No network has been trained or saved')

            instance = cls()
            for p in instance.pipeline:
                try:
                    return p.network
                except AttributeError:
                    pass

            raise RuntimeError('There is no network in the pipeline')

    root.needs = InfiniteLearningPipeline.dataset
    InfiniteLearningPipeline.__name__ = cls.__name__
    InfiniteLearningPipeline.__module__ = cls.__module__

    return InfiniteLearningPipeline

Prompt:
Add a new option allowing client code to turn off parallelism

import featureflow as ff
from random_samples import ShuffledSamples
from random_samples import InfiniteSampler
from preprocess import PreprocessingPipeline

def learning_pipeline():
    class LearningPipeline(ff.BaseModel):
        samples = ff.PickleFeature(ff.IteratorNode)

        shuffled = ff.PickleFeature(
            ShuffledSamples,
            nsamples=ff.Var('nsamples'),
            dtype=ff.Var('dtype'),
            needs=samples)

    return LearningPipeline

def infinite_streaming_learning_pipeline(cls):
    roots = filter(lambda feature: feature.is_root, cls.features.itervalues())

    if len(roots) != 1:
        raise ValueError('cls must have a single root feature')

    root = roots[0]

    class InfiniteLearningPipeline(cls):
        dataset = ff.Feature(
            InfiniteSampler,
            nsamples=ff.Var('nsamples'),
            dtype=ff.Var('dtype'))

        pipeline = ff.ClobberPickleFeature(
            PreprocessingPipeline,
            needs=cls.features,
            store=True)

        @classmethod
        def load_network(cls):
            if not cls.exists():
                raise RuntimeError('No network has been trained or saved')

            instance = cls()
            for p in instance.pipeline:
                try:
                    return p.network
                except AttributeError:
                    pass

            raise RuntimeError('There is no network in the pipeline')

    root.needs = InfiniteLearningPipeline.dataset
    InfiniteLearningPipeline.__name__ = cls.__name__
    InfiniteLearningPipeline.__module__ = cls.__module__

    return InfiniteLearningPipeline


from _ebcf_alexa import env
from unittest.mock import patch, call
import pytest

@pytest.yield_fixture
def mock_now():
    with patch.object(env, 'now') as now:
        yield now

@patch('datetime.datetime')
def test_now_is_utc(fake_datetime):
    assert env.now()
    assert fake_datetime.now.call_args == call(tz=env.UTC)

def test_local_now(mock_now):
    assert env.localnow() == mock_now.return_value.astimezone.return_value
    assert mock_now.return_value.astimezone.call_args == call(env.TZ)

def test_date(mock_now):
    assert env.date() == mock_now.return_value.date.return_value

def test_local_date():
    with patch.object(env, 'localnow') as ln:
        assert env.localdate() == ln.return_value.date.return_value

Add unit tests for env module.

Add unit tests for env module.

These are pretty simple - just tests wiring to datetime and pytz

Response:
from _ebcf_alexa import env
from unittest.mock import patch, call
import pytest

@pytest.yield_fixture
def mock_now():
    with patch.object(env, 'now') as now:
        yield now

@patch('datetime.datetime')
def test_now_is_utc(fake_datetime):
    assert env.now()
    assert fake_datetime.now.call_args == call(tz=env.UTC)

def test_local_now(mock_now):
    assert env.localnow() == mock_now.return_value.astimezone.return_value
    assert mock_now.return_value.astimezone.call_args == call(env.TZ)

def test_date(mock_now):
    assert env.date() == mock_now.return_value.date.return_value

def test_local_date():
    with patch.object(env, 'localnow') as ln:
        assert env.localdate() == ln.return_value.date.return_value

Prompt:
Add unit tests for env module.

These are pretty simple - just tests wiring to datetime and pytz


# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import gettext

import six

if six.PY2:
    gettext.install('tacker', unicode=1)
else:
    gettext.install('tacker')

Response:
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import gettext

import six

if six.PY2:
    gettext.install('tacker', unicode=1)
else:
    gettext.install('tacker')

Prompt:
Fix gettext wrong argument error in py34

Closes-Bug: #1550202
Change-Id: I468bef7a8c0a9fa93576744e7869dfa5f2569fa0

# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import gettext

gettext.install('tacker', unicode=1)


import queue

from bot.multithreading.work import Work
from bot.multithreading.worker import QueueWorker
from bot.multithreading.worker.pool.name_generator import WorkerPoolNameGenerator
from bot.multithreading.worker.pool.spawner import WorkerSpawner

class QueueWorkerPool(QueueWorker):
    def __init__(self, base_name: str, work_queue: queue.Queue, error_handler: callable, worker_starter: callable,
                 min_workers: int, max_workers: int, max_seconds_idle: int):
        """
        :param min_workers: Minimum number of workers that must be running at every time ready to accept works.
        :param max_workers: Maximum number of workers that can be spawned on heavy workload situations.
        :param max_seconds_idle: Maximum number of seconds that the additional workers over min_workers
            that were spawned will remain alive without processing works (ie. in idle state).
        """
        super().__init__(base_name, work_queue, error_handler)
        name_generator = WorkerPoolNameGenerator(base_name, max_workers, max_seconds_idle)
        self.spawner = WorkerSpawner(
            name_generator, self.queue, error_handler, worker_starter,
            min_workers, max_workers, max_seconds_idle
        )

    def start(self):
        # called from main thread
        self.spawner.spawn_initial_workers()

    def run(self):
        # this worker is not meant to be run, it only spawns workers when needed
        pass

    def post(self, work: Work):
        # put on the queue
        super().post(work)
        # this should be quick and performs no I/O, so posting it to another worker would be inefficient
        self.spawner.spawn_worker_if_needed()

Create a worker pool to handle pool of workers that can grow or shrink as necessary

Response:
import queue

from bot.multithreading.work import Work
from bot.multithreading.worker import QueueWorker
from bot.multithreading.worker.pool.name_generator import WorkerPoolNameGenerator
from bot.multithreading.worker.pool.spawner import WorkerSpawner

class QueueWorkerPool(QueueWorker):
    def __init__(self, base_name: str, work_queue: queue.Queue, error_handler: callable, worker_starter: callable,
                 min_workers: int, max_workers: int, max_seconds_idle: int):
        """
        :param min_workers: Minimum number of workers that must be running at every time ready to accept works.
        :param max_workers: Maximum number of workers that can be spawned on heavy workload situations.
        :param max_seconds_idle: Maximum number of seconds that the additional workers over min_workers
            that were spawned will remain alive without processing works (ie. in idle state).
        """
        super().__init__(base_name, work_queue, error_handler)
        name_generator = WorkerPoolNameGenerator(base_name, max_workers, max_seconds_idle)
        self.spawner = WorkerSpawner(
            name_generator, self.queue, error_handler, worker_starter,
            min_workers, max_workers, max_seconds_idle
        )

    def start(self):
        # called from main thread
        self.spawner.spawn_initial_workers()

    def run(self):
        # this worker is not meant to be run, it only spawns workers when needed
        pass

    def post(self, work: Work):
        # put on the queue
        super().post(work)
        # this should be quick and performs no I/O, so posting it to another worker would be inefficient
        self.spawner.spawn_worker_if_needed()

Prompt: Create a worker pool to handle pool of workers that can grow or shrink as necessary


import redis
from celery import Celery

from .driller import Driller

app = Celery('tasks', broker='amqp://guest@localhost//', backend='redis://localhost')
redis_pool = redis.ConnectionPool(host='localhost', port=6379, db=1)

@app.task
def drill(binary, input, fuzz_bitmap, qemu_dir):
    redis_inst = redis.Redis(connection_pool=redis_pool)

    driller = Driller(binary, input, fuzz_bitmap, qemu_dir, redis=redis_inst)
    return driller.drill()

Response:
import redis
from celery import Celery

from .driller import Driller

app = Celery('tasks', broker='amqp://guest@localhost//', backend='redis://localhost')
redis_pool = redis.ConnectionPool(host='localhost', port=6379, db=1)

@app.task
def drill(binary, input, fuzz_bitmap, qemu_dir):
    redis_inst = redis.Redis(connection_pool=redis_pool)

    driller = Driller(binary, input, fuzz_bitmap, qemu_dir, redis=redis_inst)
    return driller.drill()

Prompt:
Remove out_dir from the drill task's list of arguments

import redis
from celery import Celery

from .driller import Driller

app = Celery('tasks', broker='amqp://guest@localhost//', backend='redis://localhost')
redis_pool = redis.ConnectionPool(host='localhost', port=6379, db=1)

@app.task
def drill(binary, input, out_dir, fuzz_bitmap, qemu_dir):
    redis_inst = redis.Redis(connection_pool=redis_pool)

    driller = Driller(binary, input, fuzz_bitmap, qemu_dir, redis=redis_inst)
    return driller.drill()


# -*- coding: utf-8 -*-

from __future__ import absolute_import

# Import python libs
import os

# Import third party libs
import yaml
import logging

# Import salt libs
import salt.utils.files
import salt.utils.platform

log = logging.getLogger(__name__)

def shell():
    '''
    Return the default shell to use on this system
    '''
    # Provides:
    #   shell
    if salt.utils.platform.is_windows():
        env_var = 'COMSPEC'
        default = r'C:\Windows\system32\cmd.exe'
    else:
        env_var = 'SHELL'
        default = '/bin/sh'

    return {'shell': os.environ.get(env_var, default)}

def config():
    '''
    Return the grains set in the grains file
    '''
    if 'conf_file' not in __opts__:
        return {}
    if os.path.isdir(__opts__['conf_file']):
        gfn = os.path.join(
                __opts__['conf_file'],
                'grains'
                )
    else:
        gfn = os.path.join(
                os.path.dirname(__opts__['conf_file']),
                'grains'
                )
    if os.path.isfile(gfn):
        with salt.utils.files.fopen(gfn, 'rb') as fp_:
            try:
                return yaml.safe_load(fp_.read())
            except Exception:
                log.warning("Bad syntax in grains file! Skipping.")
                return {}
    return {}

Return COMSPEC as the shell for Windows

Return COMSPEC as the shell for Windows

Response:
# -*- coding: utf-8 -*-

from __future__ import absolute_import

# Import python libs
import os

# Import third party libs
import yaml
import logging

# Import salt libs
import salt.utils.files
import salt.utils.platform

log = logging.getLogger(__name__)

def shell():
    '''
    Return the default shell to use on this system
    '''
    # Provides:
    #   shell
    if salt.utils.platform.is_windows():
        env_var = 'COMSPEC'
        default = r'C:\Windows\system32\cmd.exe'
    else:
        env_var = 'SHELL'
        default = '/bin/sh'

    return {'shell': os.environ.get(env_var, default)}

def config():
    '''
    Return the grains set in the grains file
    '''
    if 'conf_file' not in __opts__:
        return {}
    if os.path.isdir(__opts__['conf_file']):
        gfn = os.path.join(
                __opts__['conf_file'],
                'grains'
                )
    else:
        gfn = os.path.join(
                os.path.dirname(__opts__['conf_file']),
                'grains'
                )
    if os.path.isfile(gfn):
        with salt.utils.files.fopen(gfn, 'rb') as fp_:
            try:
                return yaml.safe_load(fp_.read())
            except Exception:
                log.warning("Bad syntax in grains file! Skipping.")
                return {}
    return {}

Prompt:
Return COMSPEC as the shell for Windows

# -*- coding: utf-8 -*-

from __future__ import absolute_import

# Import python libs
import os

# Import third party libs
import yaml
import logging

# Import salt libs
import salt.utils.files

log = logging.getLogger(__name__)

def shell():
    '''
    Return the default shell to use on this system
    '''
    # Provides:
    #   shell
    return {'shell': os.environ.get('SHELL', '/bin/sh')}

def config():
    '''
    Return the grains set in the grains file
    '''
    if 'conf_file' not in __opts__:
        return {}
    if os.path.isdir(__opts__['conf_file']):
        gfn = os.path.join(
                __opts__['conf_file'],
                'grains'
                )
    else:
        gfn = os.path.join(
                os.path.dirname(__opts__['conf_file']),
                'grains'
                )
    if os.path.isfile(gfn):
        with salt.utils.files.fopen(gfn, 'rb') as fp_:
            try:
                return yaml.safe_load(fp_.read())
            except Exception:
                log.warning("Bad syntax in grains file! Skipping.")
                return {}
    return {}


from setuptools import setup, find_packages

setup(
    name="django-postgres-geometry",
    version="0.1.2",
    packages=find_packages(),
    install_requires=['django', 'psycopg2'],
    description="Django ORM field for Postgres geometry types",
    author="Daniele Esposti",
    author_email="expo@expobrain.net",
    maintainer="Daniele Esposti",
    maintainer_email="expo@expobrain.net",
    url="http://github.com/expobrain/django-postgres-geometry",
)

Fix typo in package name

Fix typo in package name

Response:
from setuptools import setup, find_packages

setup(
    name="django-postgres-geometry",
    version="0.1.2",
    packages=find_packages(),
    install_requires=['django', 'psycopg2'],
    description="Django ORM field for Postgres geometry types",
    author="Daniele Esposti",
    author_email="expo@expobrain.net",
    maintainer="Daniele Esposti",
    maintainer_email="expo@expobrain.net",
    url="http://github.com/expobrain/django-postgres-geometry",
)

Prompt:
Fix typo in package name

from setuptools import setup, find_packages

setup(
    name="django-posgtres-geometry",
    version="0.1.2",
    packages=find_packages(),
    install_requires=['django', 'psycopg2'],
    description="Django ORM field for Postgres geometry types",
    author="Daniele Esposti",
    author_email="expo@expobrain.net",
    maintainer="Daniele Esposti",
    maintainer_email="expo@expobrain.net",
    url="http://github.com/expobrain/django-postgres-geometry",
)


from __future__ import print_function, unicode_literals

from django.core.management.base import BaseCommand

from popolo.models import Membership

from candidates.models import PersonExtra

class Command(BaseCommand):

    help = "Find elections in not_standing that should be removed"

    def add_arguments(self, parser):
        parser.add_argument(
            '--delete', action='store_true',
            help="Don't just find these broken cases, also fix them",
        )

    def handle(self, *args, **options):
        for person_extra in PersonExtra.objects.filter(
            not_standing__isnull=False
        ):
            election_to_remove = []
            for election in person_extra.not_standing.all():
                candidacies = Membership.objects.filter(
                    person=person_extra.base,
                    extra__election=election,
                    role=election.candidate_membership_role,
                )
                if candidacies.exists():
                    election_to_remove.append(election)
            # Now print out the elections we would remove from
            # not_standing for that person.  (And, if --delete is
            # specified, actually remove it.)
            for election in election_to_remove:
                fmt = '{person} is marked as not standing in {election}'
                print(fmt.format(person=person_extra.base, election=election))
                print('  ... but also has a candidacy in that election!')
                if options['delete']:
                    fmt = "  Deleting {election} from {person}'s not_standing"
                    print(fmt.format(
                        election=election.name,
                        person=person_extra.base.name,
                    ))
                    person_extra.not_standing.remove(election)

Add a script to fix the not_standing relationships of people

There was a bug in bulk adding people which meant that their
"not_standing" status for an election wasn't removed when reinstating
them as a candidate in that election.

That bug has been fixed in the parent commit, but there are still people
in the database who have a candidacy (represented by a Membership and
MembershipExtra) in an election, but also have that election in their
not_standing.

This commit introduces a script that will find those cases, report them
and (if --delete is specified) fix the inconsistency by removing the
election from their not_standing.

Response:
from __future__ import print_function, unicode_literals

from django.core.management.base import BaseCommand

from popolo.models import Membership

from candidates.models import PersonExtra

class Command(BaseCommand):

    help = "Find elections in not_standing that should be removed"

    def add_arguments(self, parser):
        parser.add_argument(
            '--delete', action='store_true',
            help="Don't just find these broken cases, also fix them",
        )

    def handle(self, *args, **options):
        for person_extra in PersonExtra.objects.filter(
            not_standing__isnull=False
        ):
            election_to_remove = []
            for election in person_extra.not_standing.all():
                candidacies = Membership.objects.filter(
                    person=person_extra.base,
                    extra__election=election,
                    role=election.candidate_membership_role,
                )
                if candidacies.exists():
                    election_to_remove.append(election)
            # Now print out the elections we would remove from
            # not_standing for that person.  (And, if --delete is
            # specified, actually remove it.)
            for election in election_to_remove:
                fmt = '{person} is marked as not standing in {election}'
                print(fmt.format(person=person_extra.base, election=election))
                print('  ... but also has a candidacy in that election!')
                if options['delete']:
                    fmt = "  Deleting {election} from {person}'s not_standing"
                    print(fmt.format(
                        election=election.name,
                        person=person_extra.base.name,
                    ))
                    person_extra.not_standing.remove(election)

Prompt:
Add a script to fix the not_standing relationships of people

There was a bug in bulk adding people which meant that their
"not_standing" status for an election wasn't removed when reinstating
them as a candidate in that election.

That bug has been fixed in the parent commit, but there are still people
in the database who have a candidacy (represented by a Membership and
MembershipExtra) in an election, but also have that election in their
not_standing.

This commit introduces a script that will find those cases, report them
and (if --delete is specified) fix the inconsistency by removing the
election from their not_standing.


"""Configuration for Django system."""

__version__ = "0.9.3"
__version_info__ = tuple(
    [
        int(num) if num.isdigit() else num
        for num in __version__.replace("-", ".", 1).split(".")
    ]
)

Increment version number to 0.9.3

Increment version number to 0.9.3

Response:
"""Configuration for Django system."""

__version__ = "0.9.3"
__version_info__ = tuple(
    [
        int(num) if num.isdigit() else num
        for num in __version__.replace("-", ".", 1).split(".")
    ]
)

Prompt:
Increment version number to 0.9.3

"""Configuration for Django system."""

__version__ = "0.9.2"
__version_info__ = tuple(
    [
        int(num) if num.isdigit() else num
        for num in __version__.replace("-", ".", 1).split(".")
    ]
)


# Copyright 2014 0xc0170
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os

UV4 = os.path.join("C:","Keil","UV4","UV4.exe")
IARBUILD = os.path.join('C:','Program Files (x86)','IAR Systems','Embedded Workbench 7.0','common','bin','IarBuild.exe')

# Be able to locate project generator anywhere in a project
# By default it's tools/project_generator (2 folders deep from root)
PROJECT_ROOT= os.path.join('..','..')

if os.name == "posix":
    # Expects either arm-none-eabi to be installed here, or
    # even better, a symlink from /usr/local/arm-none-eabi to the most recent
    # version.
    gcc_bin_path = "/usr/local/arm-none-eabi/bin/"
elif os.name == "nt":
    gcc_bin_path = ""

try:
	from user_settings import *
except:
	logging.info("Using default settings.")

Response:
# Copyright 2014 0xc0170
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os

UV4 = os.path.join("C:","Keil","UV4","UV4.exe")
IARBUILD = os.path.join('C:','Program Files (x86)','IAR Systems','Embedded Workbench 7.0','common','bin','IarBuild.exe')

# Be able to locate project generator anywhere in a project
# By default it's tools/project_generator (2 folders deep from root)
PROJECT_ROOT= os.path.join('..','..')

if os.name == "posix":
    # Expects either arm-none-eabi to be installed here, or
    # even better, a symlink from /usr/local/arm-none-eabi to the most recent
    # version.
    gcc_bin_path = "/usr/local/arm-none-eabi/bin/"
elif os.name == "nt":
    gcc_bin_path = ""

try:
	from user_settings import *
except:
	logging.info("Using default settings.")

Prompt:
Add message if you're using default settings

# Copyright 2014 0xc0170
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os

UV4 = os.path.join("C:","Keil","UV4","UV4.exe")
IARBUILD = os.path.join('C:','Program Files (x86)','IAR Systems','Embedded Workbench 7.0','common','bin','IarBuild.exe')

# Be able to locate project generator anywhere in a project
# By default it's tools/project_generator (2 folders deep from root)
PROJECT_ROOT= os.path.join('..','..')

if os.name == "posix":
    # Expects either arm-none-eabi to be installed here, or
    # even better, a symlink from /usr/local/arm-none-eabi to the most recent
    # version.
    gcc_bin_path = "/usr/local/arm-none-eabi/bin/"
elif os.name == "nt":
    gcc_bin_path = ""

try:
	from user_settings import *
except:
	pass


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import get_object_or_404
from django.db import models, migrations

def add_initial_date(apps, schema_editor):
    Machine = apps.get_model("server", "Machine")
    for machine in Machine.objects.all():
        if not machine.first_checkin:
            machine.first_checkin = machine.last_checkin
            machine.save()

class Migration(migrations.Migration):

    dependencies = [
        ('server', '0005_auto_20150717_1827'),
    ]

    operations = [
        migrations.AddField(
            model_name='machine',
            name='first_checkin',
            field=models.DateTimeField(auto_now_add=True, null=True),
        ),
        migrations.AddField(
            model_name='machine',
            name='sal_version',
            field=models.TextField(null=True, blank=True),
        ),
        migrations.RunPython(add_initial_date),
    ]

Response:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import get_object_or_404
from django.db import models, migrations

def add_initial_date(apps, schema_editor):
    Machine = apps.get_model("server", "Machine")
    for machine in Machine.objects.all():
        if not machine.first_checkin:
            machine.first_checkin = machine.last_checkin
            machine.save()

class Migration(migrations.Migration):

    dependencies = [
        ('server', '0005_auto_20150717_1827'),
    ]

    operations = [
        migrations.AddField(
            model_name='machine',
            name='first_checkin',
            field=models.DateTimeField(auto_now_add=True, null=True),
        ),
        migrations.AddField(
            model_name='machine',
            name='sal_version',
            field=models.TextField(null=True, blank=True),
        ),
        migrations.RunPython(add_initial_date),
    ]

Prompt:
Add in the first checkin date if it doesn't exist

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('server', '0005_auto_20150717_1827'),
    ]

    operations = [
        migrations.AddField(
            model_name='machine',
            name='first_checkin',
            field=models.DateTimeField(auto_now_add=True, null=True),
        ),
        migrations.AddField(
            model_name='machine',
            name='sal_version',
            field=models.TextField(null=True, blank=True),
        ),
    ]


# Generated by Django 2.2.24 on 2021-09-14 17:54

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('app_manager', '0018_migrate_case_search_labels'),
    ]

    operations = [
        migrations.AddField(
            model_name='exchangeapplication',
            name='required_privileges',
            field=models.TextField(help_text='Space-separated list of privilege strings from corehq.privileges', null=True),
        ),
    ]

Fix migration with help text

Fix migration with help text

Response:
# Generated by Django 2.2.24 on 2021-09-14 17:54

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('app_manager', '0018_migrate_case_search_labels'),
    ]

    operations = [
        migrations.AddField(
            model_name='exchangeapplication',
            name='required_privileges',
            field=models.TextField(help_text='Space-separated list of privilege strings from corehq.privileges', null=True),
        ),
    ]

Prompt:
Fix migration with help text

# Generated by Django 2.2.24 on 2021-09-13 21:04

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('app_manager', '0018_migrate_case_search_labels'),
    ]

    operations = [
        migrations.AddField(
            model_name='exchangeapplication',
            name='required_privileges',
            field=models.TextField(null=True),
        ),
    ]


from django import template

from ..forms import WaitingListEntryForm

register = template.Library()

@register.simple_tag(takes_context=True)
def waitinglist_entry_form(context):
    """
    Get a (new) form object to post a new comment.

    Syntax::

        {% waitinglist_entry_form as [varname] %}

    """
    initial = {}
    if "request" in context:
        initial.update({
            "referrer": context["request"].META.get("HTTP_REFERER", ""),
            "campaign": context["request"].GET.get("wlc", "")
        })
    return WaitingListEntryForm(initial=initial)

Response:
from django import template

from ..forms import WaitingListEntryForm

register = template.Library()

@register.simple_tag(takes_context=True)
def waitinglist_entry_form(context):
    """
    Get a (new) form object to post a new comment.

    Syntax::

        {% waitinglist_entry_form as [varname] %}

    """
    initial = {}
    if "request" in context:
        initial.update({
            "referrer": context["request"].META.get("HTTP_REFERER", ""),
            "campaign": context["request"].GET.get("wlc", "")
        })
    return WaitingListEntryForm(initial=initial)

Prompt:
Update template tag to also take context

from django import template

from ..forms import WaitingListEntryForm

register = template.Library()

@register.assignment_tag
def waitinglist_entry_form():
    """
    Get a (new) form object to post a new comment.

    Syntax::

        {% waitinglist_entry_form as [varname] %}

    """
    return WaitingListEntryForm()


from django.db import models

try:
    from django_extensions.db.fields.encrypted import EncryptedTextField, EncryptedCharField
except ImportError:

    class EncryptedCharField():
         def __init__(self, **kwargs):
             pass

    class EncryptedTextField():
         def __init__(self, **kwargs):
             pass

class Secret(models.Model):
    name = EncryptedCharField(blank=True, max_length=255)
    text = EncryptedTextField(blank=True)

Response:
from django.db import models

try:
    from django_extensions.db.fields.encrypted import EncryptedTextField, EncryptedCharField
except ImportError:

    class EncryptedCharField():
         def __init__(self, **kwargs):
             pass

    class EncryptedTextField():
         def __init__(self, **kwargs):
             pass

class Secret(models.Model):
    name = EncryptedCharField(blank=True, max_length=255)
    text = EncryptedTextField(blank=True)

Prompt:
Remove bogus semicolons, thanks justinlilly

from django.db import models

try:
    from django_extensions.db.fields.encrypted import EncryptedTextField, EncryptedCharField
except ImportError:

    class EncryptedCharField():
         def __init__(self, **kwargs):
             pass;

    class EncryptedTextField():
         def __init__(self, **kwargs):
             pass;

class Secret(models.Model):
    name = EncryptedCharField(blank=True, max_length=255)
    text = EncryptedTextField(blank=True)


"""
start_servers.py

<Purpose>
  A simple script to start the three cloud-side Uptane servers:
    the Director (including its per-vehicle repositories)
    the Image Repository
    the Timeserver

  To run the demo services in non-interactive mode, run:
    python start_servers.py

  To run the demo services in interactive mode, run:
    python -i -c "from demo.start_servers import *; main()"

  In either mode, the demo services will respond to commands sent via XMLRPC.

"""
import threading
import demo
import demo.demo_timeserver as dt
import demo.demo_director as dd
import demo.demo_image_repo as di
from six.moves import xmlrpc_server

def main():

  # Start demo Image Repo, including http server and xmlrpc listener (for
  # webdemo)
  di.clean_slate()

  # Start demo Director, including http server and xmlrpc listener (for
  # manifests, registrations, and webdemo)
  dd.clean_slate()

  # Start demo Timeserver, including xmlrpc listener (for requests from demo
  # Primary)
  dt.listen()

if __name__ == '__main__':
  main()

DEMO: Create a single script to run the three demo services

(image repo, director, and timeserver)

Response:
"""
start_servers.py

<Purpose>
  A simple script to start the three cloud-side Uptane servers:
    the Director (including its per-vehicle repositories)
    the Image Repository
    the Timeserver

  To run the demo services in non-interactive mode, run:
    python start_servers.py

  To run the demo services in interactive mode, run:
    python -i -c "from demo.start_servers import *; main()"

  In either mode, the demo services will respond to commands sent via XMLRPC.

"""
import threading
import demo
import demo.demo_timeserver as dt
import demo.demo_director as dd
import demo.demo_image_repo as di
from six.moves import xmlrpc_server

def main():

  # Start demo Image Repo, including http server and xmlrpc listener (for
  # webdemo)
  di.clean_slate()

  # Start demo Director, including http server and xmlrpc listener (for
  # manifests, registrations, and webdemo)
  dd.clean_slate()

  # Start demo Timeserver, including xmlrpc listener (for requests from demo
  # Primary)
  dt.listen()

if __name__ == '__main__':
  main()

Prompt:
DEMO: Create a single script to run the three demo services

(image repo, director, and timeserver)


from typing import Dict, Callable
from rofi import Rofi

def menu(r: Rofi, prompt: str, options: Dict[str, Callable], *args, **kwargs):
    """
    Create a menu using rofi to execute on of some options, all args not documented
        are passed directly into Rofi.select
    :param options: A dict of strings to show on the menu the action they execute
    :param rofi: the rofi instance to use

    :returns: The name of the option selected and the value returned by the
        function executed, or, if none is selected, None
    """
    index, key = r.select(prompt, options.keys(), *args, **kwargs)
    if key == -1: return None

    name, action = list(options.items())[index]
    result = action()
    return name, action

Add a common helper for selection menus

[rofi] Add a common helper for selection menus

Response:
from typing import Dict, Callable
from rofi import Rofi

def menu(r: Rofi, prompt: str, options: Dict[str, Callable], *args, **kwargs):
    """
    Create a menu using rofi to execute on of some options, all args not documented
        are passed directly into Rofi.select
    :param options: A dict of strings to show on the menu the action they execute
    :param rofi: the rofi instance to use

    :returns: The name of the option selected and the value returned by the
        function executed, or, if none is selected, None
    """
    index, key = r.select(prompt, options.keys(), *args, **kwargs)
    if key == -1: return None

    name, action = list(options.items())[index]
    result = action()
    return name, action

Prompt: [rofi] Add a common helper for selection menus


import frappe
from frappe.templates.pages.style_settings import default_properties

def execute():
	frappe.reload_doc('website', 'doctype', 'style_settings')
	style_settings = frappe.get_doc("Style Settings", "Style Settings")
	if not style_settings.apply_style:
		style_settings.update(default_properties)
		style_settings.apply_style = 1
		style_settings.save()

Response:
import frappe
from frappe.templates.pages.style_settings import default_properties

def execute():
	frappe.reload_doc('website', 'doctype', 'style_settings')
	style_settings = frappe.get_doc("Style Settings", "Style Settings")
	if not style_settings.apply_style:
		style_settings.update(default_properties)
		style_settings.apply_style = 1
		style_settings.save()

Prompt:
[minor] Fix default website style patch (reload doc)
import frappe
from frappe.templates.pages.style_settings import default_properties

def execute():
	style_settings = frappe.get_doc("Style Settings", "Style Settings")
	if not style_settings.apply_style:
		style_settings.update(default_properties)
		style_settings.apply_style = 1
		style_settings.save()


from . import persist

import time
import threading

# Map from view_id to threading.Timer objects
timers = {}

# For compatibility this is a class with unchanged API from SL3.
class Daemon:
    def start(self, callback):
        self._callback = callback

    def hit(self, view):
        assert self._callback, "Queue: Can't hit before start."

        vid = view.id()
        delay = get_delay()  # [seconds]
        return queue_lint(vid, delay, self._callback)

def queue_lint(vid, delay, callback):
    hit_time = time.monotonic()

    def worker():
        callback(vid, hit_time)

    try:
        timers[vid].cancel()
    except KeyError:
        pass

    timers[vid] = timer = threading.Timer(delay, worker)
    timer.start()

    return hit_time

def get_delay():
    """Return the delay between a lint request and when it will be processed.

    If the lint mode is not background, there is no delay. Otherwise, if
    a "delay" setting is not available in any of the settings, MIN_DELAY is used.
    """
    if persist.settings.get('lint_mode') != 'background':
        return 0

    return persist.settings.get('delay')

queue = Daemon()

Response:
from . import persist

import time
import threading

# Map from view_id to threading.Timer objects
timers = {}

# For compatibility this is a class with unchanged API from SL3.
class Daemon:
    def start(self, callback):
        self._callback = callback

    def hit(self, view):
        assert self._callback, "Queue: Can't hit before start."

        vid = view.id()
        delay = get_delay()  # [seconds]
        return queue_lint(vid, delay, self._callback)

def queue_lint(vid, delay, callback):
    hit_time = time.monotonic()

    def worker():
        callback(vid, hit_time)

    try:
        timers[vid].cancel()
    except KeyError:
        pass

    timers[vid] = timer = threading.Timer(delay, worker)
    timer.start()

    return hit_time

def get_delay():
    """Return the delay between a lint request and when it will be processed.

    If the lint mode is not background, there is no delay. Otherwise, if
    a "delay" setting is not available in any of the settings, MIN_DELAY is used.
    """
    if persist.settings.get('lint_mode') != 'background':
        return 0

    return persist.settings.get('delay')

queue = Daemon()

Prompt:
Remove MIN_DELAY bc a default setting is guaranteed

from . import persist

import time
import threading

# Map from view_id to threading.Timer objects
timers = {}

# For compatibility this is a class with unchanged API from SL3.
class Daemon:
    def start(self, callback):
        self._callback = callback

    def hit(self, view):
        assert self._callback, "Queue: Can't hit before start."

        vid = view.id()
        delay = get_delay()  # [seconds]
        return queue_lint(vid, delay, self._callback)

def queue_lint(vid, delay, callback):
    hit_time = time.monotonic()

    def worker():
        callback(vid, hit_time)

    try:
        timers[vid].cancel()
    except KeyError:
        pass

    timers[vid] = timer = threading.Timer(delay, worker)
    timer.start()

    return hit_time

MIN_DELAY = 0.1

def get_delay():
    """Return the delay between a lint request and when it will be processed.

    If the lint mode is not background, there is no delay. Otherwise, if
    a "delay" setting is not available in any of the settings, MIN_DELAY is used.
    """
    if persist.settings.get('lint_mode') != 'background':
        return 0

    return persist.settings.get('delay', MIN_DELAY)

queue = Daemon()


#!/usr/bin/env python

from setuptools import setup, find_packages
from sys import version_info

version = "1.3"
deps = ["pbs", "requests>=0.12.1"]

# require argparse on Python <2.7 and <3.2
if (version_info[0] == 2 and version_info[1] < 7) or \
   (version_info[0] == 3 and version_info[1] < 2):
    deps.append("argparse")

setup(name="livestreamer",
      version=version,
      description="CLI program that launches streams from various streaming services in a custom video player",
      url="https://github.com/chrippa/livestreamer",
      author="Christopher Rosell",
      author_email="chrippa@tanuki.se",
      license="BSD",
      packages=["livestreamer", "livestreamer.stream", "livestreamer.plugins"],
      package_dir={'': 'src'},
      entry_points={
          "console_scripts": ['livestreamer=livestreamer.cli:main']
      },
      install_requires=deps,
      classifiers=["Operating System :: POSIX",
                   "Operating System :: Microsoft :: Windows",
                   "Environment :: Console",
                   "Development Status :: 5 - Production/Stable",
                   "Topic :: Internet :: WWW/HTTP",
                   "Topic :: Multimedia :: Sound/Audio",
                   "Topic :: Utilities"]
)

Add .stream to packages list.

Add .stream to packages list.

Response:
#!/usr/bin/env python

from setuptools import setup, find_packages
from sys import version_info

version = "1.3"
deps = ["pbs", "requests>=0.12.1"]

# require argparse on Python <2.7 and <3.2
if (version_info[0] == 2 and version_info[1] < 7) or \
   (version_info[0] == 3 and version_info[1] < 2):
    deps.append("argparse")

setup(name="livestreamer",
      version=version,
      description="CLI program that launches streams from various streaming services in a custom video player",
      url="https://github.com/chrippa/livestreamer",
      author="Christopher Rosell",
      author_email="chrippa@tanuki.se",
      license="BSD",
      packages=["livestreamer", "livestreamer.stream", "livestreamer.plugins"],
      package_dir={'': 'src'},
      entry_points={
          "console_scripts": ['livestreamer=livestreamer.cli:main']
      },
      install_requires=deps,
      classifiers=["Operating System :: POSIX",
                   "Operating System :: Microsoft :: Windows",
                   "Environment :: Console",
                   "Development Status :: 5 - Production/Stable",
                   "Topic :: Internet :: WWW/HTTP",
                   "Topic :: Multimedia :: Sound/Audio",
                   "Topic :: Utilities"]
)

Prompt:
Add .stream to packages list.

#!/usr/bin/env python

from setuptools import setup, find_packages
from sys import version_info

version = "1.3"
deps = ["pbs", "requests>=0.12.1"]

# require argparse on Python <2.7 and <3.2
if (version_info[0] == 2 and version_info[1] < 7) or \
   (version_info[0] == 3 and version_info[1] < 2):
    deps.append("argparse")

setup(name="livestreamer",
      version=version,
      description="CLI program that launches streams from various streaming services in a custom video player",
      url="https://github.com/chrippa/livestreamer",
      author="Christopher Rosell",
      author_email="chrippa@tanuki.se",
      license="BSD",
      packages=["livestreamer", "livestreamer/plugins"],
      package_dir={'': 'src'},
      entry_points={
          "console_scripts": ['livestreamer=livestreamer.cli:main']
      },
      install_requires=deps,
      classifiers=["Operating System :: POSIX",
                   "Operating System :: Microsoft :: Windows",
                   "Environment :: Console",
                   "Development Status :: 5 - Production/Stable",
                   "Topic :: Internet :: WWW/HTTP",
                   "Topic :: Multimedia :: Sound/Audio",
                   "Topic :: Utilities"]
)


from spack import *

class Nccmp(Package):
    """Compare NetCDF Files"""
    homepage = "http://nccmp.sourceforge.net/"
    url      = "http://downloads.sourceforge.net/project/nccmp/nccmp-1.8.2.0.tar.gz"

    version('1.8.2.0', '81e6286d4413825aec4327e61a28a580')

    depends_on('netcdf')

    def install(self, spec, prefix):
        # Configure says: F90 and F90FLAGS are replaced by FC and
        # FCFLAGS respectively in this configure, please unset
        # F90/F90FLAGS and set FC/FCFLAGS instead and rerun configure
        # again.
        env.pop('F90', None)
        env.pop('F90FLAGS', None)

        configure('--prefix=%s' % prefix)
        make()
        make("check")
        make("install")

Response:
from spack import *

class Nccmp(Package):
    """Compare NetCDF Files"""
    homepage = "http://nccmp.sourceforge.net/"
    url      = "http://downloads.sourceforge.net/project/nccmp/nccmp-1.8.2.0.tar.gz"

    version('1.8.2.0', '81e6286d4413825aec4327e61a28a580')

    depends_on('netcdf')

    def install(self, spec, prefix):
        # Configure says: F90 and F90FLAGS are replaced by FC and
        # FCFLAGS respectively in this configure, please unset
        # F90/F90FLAGS and set FC/FCFLAGS instead and rerun configure
        # again.
        env.pop('F90', None)
        env.pop('F90FLAGS', None)

        configure('--prefix=%s' % prefix)
        make()
        make("check")
        make("install")

Prompt:
Tweak nccmp to be more spack-compatible.

- Spack doesn't set F90, but it confuses the nccmp build.  Just remove
  it from the environment.

- TODO: should build environment unset this variable?

from spack import *
import os

class Nccmp(Package):
    """Compare NetCDF Files"""
    homepage = "http://nccmp.sourceforge.net/"
    url      = "http://downloads.sourceforge.net/project/nccmp/nccmp-1.8.2.0.tar.gz"

    version('1.8.2.0', '81e6286d4413825aec4327e61a28a580')

    depends_on('netcdf')

    def install(self, spec, prefix):
        # Configure says: F90 and F90FLAGS are replaced by FC and
        # FCFLAGS respectively in this configure, please unset
        # F90/F90FLAGS and set FC/FCFLAGS instead and rerun configure
        # again.
        os.environ['FC'] = os.environ['F90']
        del os.environ['F90']
        try:
            os.environ['FCFLAGS'] = os.environ['F90FLAGS']
            del os.environ['F90FLAGS']
        except KeyError:    # There are no flags
            pass

        configure('--prefix=%s' % prefix)

        make()
        make("check")
        make("install")


#!/usr/bin/env python

# Jamendo database dumps can be fetched from: http://img.jamendo.com/data/dbdump_artistalbumtrack.xml.gz

import xml.etree.cElementTree as ElementTree
import sys, gzip, time, os.path, urllib

class DownloadJamendo:

	def __init__(self, destination):
		if not os.path.exists(destination):
			os.mkdir(destination)
		self.destination = destination

	def parse(self, dump):
		for event, elem in ElementTree.iterparse(dump):
			if elem.tag == "artist":
				self.proc_artist(elem)

	def proc_artist(self, elem):
		for artist_e in elem.getchildren():
			if artist_e.tag == "Albums":
				for album_e in artist_e.getchildren():
					self.proc_album(album_e)

	def proc_album(self, elem):
		for album_e in elem.getchildren():
			if album_e.tag == "Tracks":
				for track_e in album_e.getchildren():
					self.proc_track(track_e)

	def proc_track(self, elem):
		track_id = None
		track_license = None

		for track_e in elem.getchildren():
			if track_e.tag == "id":
				track_id = int(track_e.text)

			if track_e.tag == "license":
				track_license = track_e.text

		if track_id and track_license:
			if self.free_license(track_license):
				trackurl = "http://api.jamendo.com/get2/stream/track/redirect/?id=%d&streamencoding=ogg2" % track_id
				trackfile = os.path.join(self.destination, "%d.ogg" % track_id)
				if os.path.exists(trackfile):
					print "Already downloaded track %d" % track_id
				else:
					print "Downloading %s to %s" % (trackurl, trackfile)
					urllib.urlretrieve(trackurl, trackfile)

	def free_license(self, license):
		return ("http://creativecommons.org/licenses/by-sa" in license or "http://creativecommons.org/licenses/by/" in license or "http://artlibre.org/licence.php/lal.html" in license)

if __name__ == "__main__":

	if len(sys.argv) != 3:
		print "Usage: download-jamendo.py <database dump> <destination>"
		sys.exit(1)

	if sys.argv[1][-2:] == "gz":
		dump = gzip.open(sys.argv[1], "r")
	else:
		dump = open(sys.argv[1], "r")

	downloader = DownloadJamendo(sys.argv[2])
	downloader.parse(dump)

Add a script to download all the fully free tracks from Jamendo (as Ogg Vorbis)

Response:
#!/usr/bin/env python

# Jamendo database dumps can be fetched from: http://img.jamendo.com/data/dbdump_artistalbumtrack.xml.gz

import xml.etree.cElementTree as ElementTree
import sys, gzip, time, os.path, urllib

class DownloadJamendo:

	def __init__(self, destination):
		if not os.path.exists(destination):
			os.mkdir(destination)
		self.destination = destination

	def parse(self, dump):
		for event, elem in ElementTree.iterparse(dump):
			if elem.tag == "artist":
				self.proc_artist(elem)

	def proc_artist(self, elem):
		for artist_e in elem.getchildren():
			if artist_e.tag == "Albums":
				for album_e in artist_e.getchildren():
					self.proc_album(album_e)

	def proc_album(self, elem):
		for album_e in elem.getchildren():
			if album_e.tag == "Tracks":
				for track_e in album_e.getchildren():
					self.proc_track(track_e)

	def proc_track(self, elem):
		track_id = None
		track_license = None

		for track_e in elem.getchildren():
			if track_e.tag == "id":
				track_id = int(track_e.text)

			if track_e.tag == "license":
				track_license = track_e.text

		if track_id and track_license:
			if self.free_license(track_license):
				trackurl = "http://api.jamendo.com/get2/stream/track/redirect/?id=%d&streamencoding=ogg2" % track_id
				trackfile = os.path.join(self.destination, "%d.ogg" % track_id)
				if os.path.exists(trackfile):
					print "Already downloaded track %d" % track_id
				else:
					print "Downloading %s to %s" % (trackurl, trackfile)
					urllib.urlretrieve(trackurl, trackfile)

	def free_license(self, license):
		return ("http://creativecommons.org/licenses/by-sa" in license or "http://creativecommons.org/licenses/by/" in license or "http://artlibre.org/licence.php/lal.html" in license)

if __name__ == "__main__":

	if len(sys.argv) != 3:
		print "Usage: download-jamendo.py <database dump> <destination>"
		sys.exit(1)

	if sys.argv[1][-2:] == "gz":
		dump = gzip.open(sys.argv[1], "r")
	else:
		dump = open(sys.argv[1], "r")

	downloader = DownloadJamendo(sys.argv[2])
	downloader.parse(dump)

Prompt: Add a script to download all the fully free tracks from Jamendo (as Ogg Vorbis)