#! usr/bin/env python3

import unittest
from sqlviz import Schema

# Tests will go here...eventually

class InventorySchemaSpec (unittest.TestCase):

    def setUp (self):
        self.schema = Schema(
            """DROP TABLE Inventory;

            CREATE TABLE Inventory
            (
            id INT PRIMARY KEY,
            product VARCHAR(50) UNIQUE,
            quantity INT,
            price DECIMAL(18,2)
            );""")

    def test_n_tables(self):
        self.assertEqual(self.schema.n_tables(),  1,
                        "The Inventory schema should contain 1 table.")

    def test_n_keys(self):
        self.assertEqual(self.schema.n_keys(),  {"PRIMARY KEY": 1, "FOREIGN KEY": 0},
                        "The Inventory schema should contain 1 primary key and 0 foreign keys.")

    def test_n_datatypes(self):
        self.assertEqual(self.schema.n_datatypes(), {"INT": 2, "VARCHAR": 1, "DECIMAL": 1},
                        "The Inventory schema should contain two INTs, one VARCHAR, and one DECIMAL")

    def lengths(self):
        self.assertEqual(self.schema.lengths(), {"VARCHAR": [50], "DECIMAL": [(18,2)]},
                        "The Inventory schema should contain one VARCHAR(50) and one DECIMAL(18,2)")

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

Fix misplaced colon in test suite

Fix misplaced colon in test suite

Response:
#! usr/bin/env python3

import unittest
from sqlviz import Schema

# Tests will go here...eventually

class InventorySchemaSpec (unittest.TestCase):

    def setUp (self):
        self.schema = Schema(
            """DROP TABLE Inventory;

            CREATE TABLE Inventory
            (
            id INT PRIMARY KEY,
            product VARCHAR(50) UNIQUE,
            quantity INT,
            price DECIMAL(18,2)
            );""")

    def test_n_tables(self):
        self.assertEqual(self.schema.n_tables(),  1,
                        "The Inventory schema should contain 1 table.")

    def test_n_keys(self):
        self.assertEqual(self.schema.n_keys(),  {"PRIMARY KEY": 1, "FOREIGN KEY": 0},
                        "The Inventory schema should contain 1 primary key and 0 foreign keys.")

    def test_n_datatypes(self):
        self.assertEqual(self.schema.n_datatypes(), {"INT": 2, "VARCHAR": 1, "DECIMAL": 1},
                        "The Inventory schema should contain two INTs, one VARCHAR, and one DECIMAL")

    def lengths(self):
        self.assertEqual(self.schema.lengths(), {"VARCHAR": [50], "DECIMAL": [(18,2)]},
                        "The Inventory schema should contain one VARCHAR(50) and one DECIMAL(18,2)")

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

Prompt:
Fix misplaced colon in test suite
#! usr/bin/env python3

import unittest
from sqlviz import Schema

# Tests will go here...eventually

class InventorySchemaSpec (unittest.TestCase):

    def setUp (self):
        self.schema = Schema(
            """DROP TABLE Inventory;

            CREATE TABLE Inventory
            (
            id INT PRIMARY KEY,
            product VARCHAR(50) UNIQUE,
            quantity INT,
            price DECIMAL(18,2)
            );""")

    def test_n_tables(self):
        self.assertEqual(self.schema.n_tables(),  1,
                        "The Inventory schema should contain 1 table.")

    def test_n_keys(self):
        self.assertEqual(self.schema.n_keys(),  {"PRIMARY KEY": 1, "FOREIGN KEY:" 0},
                        "The Inventory schema should contain 1 primary key and 0 foreign keys.")

    def test_n_datatypes(self):
        self.assertEqual(self.schema.n_datatypes(), {"INT": 2, "VARCHAR": 1, "DECIMAL": 1},
                        "The Inventory schema should contain two INTs, one VARCHAR, and one DECIMAL")

    def lengths(self):
        self.assertEqual(self.schema.lengths(), {"VARCHAR": [50], "DECIMAL": [(18,2)]},
                        "The Inventory schema should contain one VARCHAR(50) and one DECIMAL(18,2)")

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


import os.path
import json

PACKAGE_DIR = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(PACKAGE_DIR, 'data/smartphone_dev_id.json')) as f:
    SMARTPHONE_DEV_IDS = json.load(f)
with open(os.path.join(PACKAGE_DIR, 'data/tablet_dev_id.json')) as f:
    TABLET_DEV_IDS = json.load(f)

Fix uses of file objects

Fix uses of file objects

Response:
import os.path
import json

PACKAGE_DIR = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(PACKAGE_DIR, 'data/smartphone_dev_id.json')) as f:
    SMARTPHONE_DEV_IDS = json.load(f)
with open(os.path.join(PACKAGE_DIR, 'data/tablet_dev_id.json')) as f:
    TABLET_DEV_IDS = json.load(f)

Prompt:
Fix uses of file objects
import os.path
import json

PACKAGE_DIR = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(PACKAGE_DIR, 'data/smartphone_dev_id.json')) as f:
    SMARTPHONE_DEV_IDS = json.load(open(f))
with open(os.path.join(PACKAGE_DIR, 'data/tablet_dev_id.json')) as f:
    TABLET_DEV_IDS = json.load(open())


# -*- coding: utf-8 -*-
from django.db import models, migrations
from django.core.management import call_command

app_name = 'api'
fixture = model_name = 'PartOfSpeech'

def load_fixture(apps, schema_editor):
    call_command('loaddata', fixture, app_label=app_name)

def unload_fixture(apps, schema_editor):
    "Deleting all entries for this model"

    PartOfSpeech = apps.get_model(app_name, model_name)
    PartOfSpeech.objects.all().delete()

class Migration(migrations.Migration):
    dependencies = [
        (app_name, '0008_posmention'),
     ]

    operations = [
        migrations.RunPython(load_fixture, reverse_code=unload_fixture),
    ]

Fix bug in unload_fixture method

Fix bug in unload_fixture method

Response:
# -*- coding: utf-8 -*-
from django.db import models, migrations
from django.core.management import call_command

app_name = 'api'
fixture = model_name = 'PartOfSpeech'

def load_fixture(apps, schema_editor):
    call_command('loaddata', fixture, app_label=app_name)

def unload_fixture(apps, schema_editor):
    "Deleting all entries for this model"

    PartOfSpeech = apps.get_model(app_name, model_name)
    PartOfSpeech.objects.all().delete()

class Migration(migrations.Migration):
    dependencies = [
        (app_name, '0008_posmention'),
     ]

    operations = [
        migrations.RunPython(load_fixture, reverse_code=unload_fixture),
    ]

Prompt:
Fix bug in unload_fixture method
# -*- coding: utf-8 -*-
from django.db import models, migrations
from django.core.management import call_command

app_name = 'api'
fixture = model_name = 'PartOfSpeech'

def load_fixture(apps, schema_editor):
    call_command('loaddata', fixture, app_label=app_name)

def unload_fixture(apps, schema_editor):
    "Deleting all entries for this model"

    PartOfSpeech = apps.get_model(api, model_name)
    PartOfSpeech.objects.all().delete()

class Migration(migrations.Migration):
    dependencies = [
        (app_name, '0008_posmention'),
     ]

    operations = [
        migrations.RunPython(load_fixture, reverse_code=unload_fixture),
    ]


from flexget.manager import Session

def with_session(func):
    """"Creates a session if one was not passed via keyword argument to the function"""

    def wrapper(*args, **kwargs):
        if not kwargs.get('session'):
            kwargs['session'] = Session(autoflush=True)
            try:
                return func(*args, **kwargs)
            finally:
                kwargs['session'].close()
        else:
            return func(*args, **kwargs)
    return wrapper

Response:
from flexget.manager import Session

def with_session(func):
    """"Creates a session if one was not passed via keyword argument to the function"""

    def wrapper(*args, **kwargs):
        if not kwargs.get('session'):
            kwargs['session'] = Session(autoflush=True)
            try:
                return func(*args, **kwargs)
            finally:
                kwargs['session'].close()
        else:
            return func(*args, **kwargs)
    return wrapper

Prompt:
Fix with_session decorator for python 2.5

git-svn-id: ad91b9aa7ba7638d69f912c9f5d012e3326e9f74@1957 3942dd89-8c5d-46d7-aeed-044bccf3e60c

from flexget.manager import Session

def with_session(func):
    """"Creates a session if one was not passed via keyword argument to the function"""

    def wrapper(*args, **kwargs):
        passed_session = kwargs.get('session')
        if not passed_session:
            session = Session(autoflush=True)
            try:
                return func(*args, session=session, **kwargs)
            finally:
                session.close()
        else:
            return func(*args, **kwargs)
    return wrapper


import json
from django.http import HttpResponse
from maps.models import QuestionSet

def list_question_sets(request):
    objects = QuestionSet.objects.all()
    items = []
    for obj in objects:
        items.append({
            'title': obj.title,
            'max_duration': obj.max_duration.seconds,
            'creator': {
                'full_name': obj.creator.get_full_name()
            }
        })
    return HttpResponse(json.dumps(items))

Add API method for question sets list

Add API method for question sets list

Response:
import json
from django.http import HttpResponse
from maps.models import QuestionSet

def list_question_sets(request):
    objects = QuestionSet.objects.all()
    items = []
    for obj in objects:
        items.append({
            'title': obj.title,
            'max_duration': obj.max_duration.seconds,
            'creator': {
                'full_name': obj.creator.get_full_name()
            }
        })
    return HttpResponse(json.dumps(items))

Prompt:
Add API method for question sets list

from django.http import HttpResponse

def list_question_sets(request):
    return HttpResponse('Lol, udachi')


'''
Module for running arbitrairy tests
'''

import time

def echo(text):
    '''
    Return a string - used for testing the connection

    CLI Example:
    salt '*' test.echo 'foo bar baz quo qux'
    '''
    print 'Echo got called!'
    return text

def ping():
    '''
    Just used to make sure the minion is up and responding
    Return True

    CLI Example:
    salt '*' test.ping
    '''
    return True

def fib(num):
    '''
    Return a fibonachi sequence up to the passed number, and the time it took
    to compute in seconds. Used for performance tests

    CLI Example:
    salt '*' test.fib 3
    '''
    start = time.time()
    a, b = 0, 1
    ret = [0]
    while b < num:
        ret.append(b)
        a, b = b, a + b
    return ret, time.time() - start

def collatz(start):
    '''
    Execute the collatz conjecture from the passed starting number, returns
    the sequence and the time it took to compute. Used for performance tests.

    CLI Example:
    salt '*' test.collatz 3
    '''
    begin = time.time()
    steps = []
    while start != 1:
        steps.append(start)
        if start > 1:
            if start % 2 == 0:
                start = start / 2
            else:
                start = start * 3 + 1
    return steps, time.time() - begin

Fix assignment issue in coallatz

Fix assignment issue in coallatz

Response:
'''
Module for running arbitrairy tests
'''

import time

def echo(text):
    '''
    Return a string - used for testing the connection

    CLI Example:
    salt '*' test.echo 'foo bar baz quo qux'
    '''
    print 'Echo got called!'
    return text

def ping():
    '''
    Just used to make sure the minion is up and responding
    Return True

    CLI Example:
    salt '*' test.ping
    '''
    return True

def fib(num):
    '''
    Return a fibonachi sequence up to the passed number, and the time it took
    to compute in seconds. Used for performance tests

    CLI Example:
    salt '*' test.fib 3
    '''
    start = time.time()
    a, b = 0, 1
    ret = [0]
    while b < num:
        ret.append(b)
        a, b = b, a + b
    return ret, time.time() - start

def collatz(start):
    '''
    Execute the collatz conjecture from the passed starting number, returns
    the sequence and the time it took to compute. Used for performance tests.

    CLI Example:
    salt '*' test.collatz 3
    '''
    begin = time.time()
    steps = []
    while start != 1:
        steps.append(start)
        if start > 1:
            if start % 2 == 0:
                start = start / 2
            else:
                start = start * 3 + 1
    return steps, time.time() - begin

Prompt:
Fix assignment issue in coallatz

'''
Module for running arbitrairy tests
'''

import time

def echo(text):
    '''
    Return a string - used for testing the connection

    CLI Example:
    salt '*' test.echo 'foo bar baz quo qux'
    '''
    print 'Echo got called!'
    return text

def ping():
    '''
    Just used to make sure the minion is up and responding
    Return True

    CLI Example:
    salt '*' test.ping
    '''
    return True

def fib(num):
    '''
    Return a fibonachi sequence up to the passed number, and the time it took
    to compute in seconds. Used for performance tests

    CLI Example:
    salt '*' test.fib 3
    '''
    start = time.time()
    a, b = 0, 1
    ret = [0]
    while b < num:
        ret.append(b)
        a, b = b, a + b
    return ret, time.time() - start

def collatz(start):
    '''
    Execute the collatz conjecture from the passed starting number, returns
    the sequence and the time it took to compute. Used for performance tests.

    CLI Example:
    salt '*' test.collatz 3
    '''
    start = time.time()
    steps = []
    while start != 1:
        steps.append(start)
        if start > 1:
            if start % 2 == 0:
                start = start / 2
            else:
                start = start * 3 + 1
    return steps, time.time() - start


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

import sys
import os
import pytest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../src/epicslide"))

import rst

class TestRst(object):
    def test_pygments(self):
        p = rst.Pygments('')
        print p.__dict__
        print p.run()
        assert False

    def html_parts(self):
        assert False

    def html_body(self):
        assert False

Structure for the Rst test

Structure for the Rst test

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

import sys
import os
import pytest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../src/epicslide"))

import rst

class TestRst(object):
    def test_pygments(self):
        p = rst.Pygments('')
        print p.__dict__
        print p.run()
        assert False

    def html_parts(self):
        assert False

    def html_body(self):
        assert False

Prompt: Structure for the Rst test


from twisted.trial import unittest

from ooni.plugoo import work, tests

class WorkerTestCase(unittest.TestCase):
    def testWorkGenerator(self):
        class DummyTest:
            assets = {}
        dummytest = DummyTest()
        asset = []
        for i in range(10):
            asset.append(i)
        dummytest.assets['asset'] = asset
        wgen = work.WorkGenerator(dummytest)
        for j, x in enumerate(wgen):
            pass
        self.assertEqual(i, j)

Write test case for worker

Write test case for worker

Response:
from twisted.trial import unittest

from ooni.plugoo import work, tests

class WorkerTestCase(unittest.TestCase):
    def testWorkGenerator(self):
        class DummyTest:
            assets = {}
        dummytest = DummyTest()
        asset = []
        for i in range(10):
            asset.append(i)
        dummytest.assets['asset'] = asset
        wgen = work.WorkGenerator(dummytest)
        for j, x in enumerate(wgen):
            pass
        self.assertEqual(i, j)

Prompt: Write test case for worker


# -*- coding: utf-8 -*-
'''
tests.unit.returners.pgjsonb_test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Unit tests for the PGJsonb returner (pgjsonb).
'''

# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
import logging

# Import Salt Testing libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
    MagicMock,
    NO_MOCK,
    NO_MOCK_REASON,
    patch
)

# Import Salt libs
import salt.returners.pgjsonb as pgjsonb

log = logging.getLogger(__name__)

@skipIf(NO_MOCK, NO_MOCK_REASON)
class PGJsonbCleanOldJobsTestCase(TestCase, LoaderModuleMockMixin):
    '''
    Tests for the local_cache.clean_old_jobs function.
    '''
    def setup_loader_modules(self):
        return {pgjsonb: {'__opts__': {'keep_jobs': 1, 'archive_jobs': 0}}}

    def test_clean_old_jobs_purge(self):
        '''
        Tests that the function returns None when no jid_root is found.
        '''
        connect_mock = MagicMock()
        with patch.object(pgjsonb, '_get_serv', connect_mock):
            with patch.dict(pgjsonb.__salt__, {'config.option': MagicMock()}):
                self.assertEqual(pgjsonb.clean_old_jobs(), None)

    def test_clean_old_jobs_archive(self):
        '''
        Tests that the function returns None when no jid_root is found.
        '''
        connect_mock = MagicMock()
        with patch.object(pgjsonb, '_get_serv', connect_mock):
            with patch.dict(pgjsonb.__salt__, {'config.option': MagicMock()}):
                with patch.dict(pgjsonb.__opts__, {'archive_jobs': 1}):
                    self.assertEqual(pgjsonb.clean_old_jobs(), None)

Revert "Remove pgjsonb returner unit tests"

This reverts commit ab4a670ff22878d5115f408baf0304a0ba3ec994.

Response:
# -*- coding: utf-8 -*-
'''
tests.unit.returners.pgjsonb_test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Unit tests for the PGJsonb returner (pgjsonb).
'''

# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
import logging

# Import Salt Testing libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
    MagicMock,
    NO_MOCK,
    NO_MOCK_REASON,
    patch
)

# Import Salt libs
import salt.returners.pgjsonb as pgjsonb

log = logging.getLogger(__name__)

@skipIf(NO_MOCK, NO_MOCK_REASON)
class PGJsonbCleanOldJobsTestCase(TestCase, LoaderModuleMockMixin):
    '''
    Tests for the local_cache.clean_old_jobs function.
    '''
    def setup_loader_modules(self):
        return {pgjsonb: {'__opts__': {'keep_jobs': 1, 'archive_jobs': 0}}}

    def test_clean_old_jobs_purge(self):
        '''
        Tests that the function returns None when no jid_root is found.
        '''
        connect_mock = MagicMock()
        with patch.object(pgjsonb, '_get_serv', connect_mock):
            with patch.dict(pgjsonb.__salt__, {'config.option': MagicMock()}):
                self.assertEqual(pgjsonb.clean_old_jobs(), None)

    def test_clean_old_jobs_archive(self):
        '''
        Tests that the function returns None when no jid_root is found.
        '''
        connect_mock = MagicMock()
        with patch.object(pgjsonb, '_get_serv', connect_mock):
            with patch.dict(pgjsonb.__salt__, {'config.option': MagicMock()}):
                with patch.dict(pgjsonb.__opts__, {'archive_jobs': 1}):
                    self.assertEqual(pgjsonb.clean_old_jobs(), None)

Prompt:
Revert "Remove pgjsonb returner unit tests"

This reverts commit ab4a670ff22878d5115f408baf0304a0ba3ec994.


import argparse
import numpy as np

from PIL import Image

lookup = " .,:-!?X#"

def image_to_ascii(image, width=128):
    """
    PIL image object -> 2d array of values

    """
    def scale_height(h, w, new_width):
        print "original height: {}".format(h)
        print "original width: {}".format(w)
        print "new width: {}".format(new_width)
        conversion_factor = float(1.0 * new_width / w)
        print "conversion factor {}".format(conversion_factor)
        new_height = (h * conversion_factor) / 2.0
        print "new height: {}".format(new_height)
        return int(new_height)

    quantised = image.quantize(len(lookup))

    #quantised.show()
    array = np.asarray(quantised.resize((width, scale_height(image.height, image.width, width))))

    return [[lookup[k] for k in i] for i in array]

def convert_file(fn):
    converted = ""
    try:
        image = Image.open(fn)
        converted = image_to_ascii(image)
    except Exception as e:
        print e.message

    return converted

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-f", "--filename",
                        help="Convert this file to ASCII art",
                        required=True)
    args = parser.parse_args()
    print args
    filename = args.filename
    converted = convert_file(filename)
    print '\n'.join(''.join(i) for i in converted)

Response:
import argparse
import numpy as np

from PIL import Image

lookup = " .,:-!?X#"

def image_to_ascii(image, width=128):
    """
    PIL image object -> 2d array of values

    """
    def scale_height(h, w, new_width):
        print "original height: {}".format(h)
        print "original width: {}".format(w)
        print "new width: {}".format(new_width)
        conversion_factor = float(1.0 * new_width / w)
        print "conversion factor {}".format(conversion_factor)
        new_height = (h * conversion_factor) / 2.0
        print "new height: {}".format(new_height)
        return int(new_height)

    quantised = image.quantize(len(lookup))

    #quantised.show()
    array = np.asarray(quantised.resize((width, scale_height(image.height, image.width, width))))

    return [[lookup[k] for k in i] for i in array]

def convert_file(fn):
    converted = ""
    try:
        image = Image.open(fn)
        converted = image_to_ascii(image)
    except Exception as e:
        print e.message

    return converted

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-f", "--filename",
                        help="Convert this file to ASCII art",
                        required=True)
    args = parser.parse_args()
    print args
    filename = args.filename
    converted = convert_file(filename)
    print '\n'.join(''.join(i) for i in converted)

Prompt:
CORRECT height/width conversion this time around

import argparse
import numpy as np

from PIL import Image

lookup = " .,:-?X#"

def image_to_ascii(image):
    """
    PIL image object -> 2d array of values

    """
    quantised = image.quantize(len(lookup))

    quantised.show()
    array = np.asarray(quantised.resize((128,64)))

    return [[lookup[k] for k in i] for i in array]

def convert_file(fn):
    converted = ""
    try:
        image = Image.open(fn)
        converted = image_to_ascii(image)
    except Exception as e:
        print e.message

    return converted

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-f", "--filename",
                        help="Convert this file to ASCII art",
                        required=True)
    args = parser.parse_args()
    print args
    filename = args.filename
    converted = convert_file(filename)
    print '\n'.join(''.join(i) for i in converted)


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

from django.apps import apps as global_apps
from django.contrib.contenttypes.management import update_contenttypes
from django.db import models, migrations
from django.utils.timezone import now

MARKER = '.. Migrated from django_comments_xtd.Comment model.\n\n'

comments_app_name = 'django_comments_xtd'
content_type = 'job'

def migrate_old_content(apps, schema_editor):
    try:
        Comment = apps.get_model(comments_app_name, 'XtdComment')
    except LookupError:
        # django_comments_xtd isn't installed.
        return
    update_contenttypes(apps.app_configs['contenttypes'])
    JobReviewComment = apps.get_model('jobs', 'JobReviewComment')
    Job = apps.get_model('jobs', 'Job')
    ContentType = apps.get_model('contenttypes', 'ContentType')
    db_alias = schema_editor.connection.alias
    try:
        job_contenttype = ContentType.objects.using(db_alias).get(name=content_type)
    except ContentType.DoesNotExist:
        return
    old_comments = Comment.objects.using(db_alias).filter(
        content_type=job_contenttype.pk, is_public=True, is_removed=False,
    )
    found_jobs = {}
    comments = []
    for comment in old_comments:
        try:
            job = found_jobs[comment.object_pk]
        except KeyError:
            try:
                job = Job.objects.using(db_alias).get(pk=comment.object_pk)
                found_jobs[comment.object_pk] = job
            except Job.DoesNotExist:
                continue
        review_comment = JobReviewComment(
            job=job,
            comment=MARKER + comment.comment,
            creator=comment.user,
            created=comment.submit_date,
            updated=now(),
        )
        comments.append(review_comment)
    JobReviewComment.objects.using(db_alias).bulk_create(comments)

def delete_migrated_content(apps, schema_editor):
    JobReviewComment = apps.get_model('jobs', 'JobReviewComment')
    db_alias = schema_editor.connection.alias
    JobReviewComment.objects.using(db_alias).filter(comment__startswith=MARKER).delete()

class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0001_initial'),
        ('jobs', '0011_jobreviewcomment'),
    ]
    if global_apps.is_installed(comments_app_name):
        dependencies.append((comments_app_name, '0001_initial'))

    operations = [
        migrations.RunPython(migrate_old_content, delete_migrated_content),
    ]

Migrate old comments over jobs.JobReviewComment

Fixes #591

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

from django.apps import apps as global_apps
from django.contrib.contenttypes.management import update_contenttypes
from django.db import models, migrations
from django.utils.timezone import now

MARKER = '.. Migrated from django_comments_xtd.Comment model.\n\n'

comments_app_name = 'django_comments_xtd'
content_type = 'job'

def migrate_old_content(apps, schema_editor):
    try:
        Comment = apps.get_model(comments_app_name, 'XtdComment')
    except LookupError:
        # django_comments_xtd isn't installed.
        return
    update_contenttypes(apps.app_configs['contenttypes'])
    JobReviewComment = apps.get_model('jobs', 'JobReviewComment')
    Job = apps.get_model('jobs', 'Job')
    ContentType = apps.get_model('contenttypes', 'ContentType')
    db_alias = schema_editor.connection.alias
    try:
        job_contenttype = ContentType.objects.using(db_alias).get(name=content_type)
    except ContentType.DoesNotExist:
        return
    old_comments = Comment.objects.using(db_alias).filter(
        content_type=job_contenttype.pk, is_public=True, is_removed=False,
    )
    found_jobs = {}
    comments = []
    for comment in old_comments:
        try:
            job = found_jobs[comment.object_pk]
        except KeyError:
            try:
                job = Job.objects.using(db_alias).get(pk=comment.object_pk)
                found_jobs[comment.object_pk] = job
            except Job.DoesNotExist:
                continue
        review_comment = JobReviewComment(
            job=job,
            comment=MARKER + comment.comment,
            creator=comment.user,
            created=comment.submit_date,
            updated=now(),
        )
        comments.append(review_comment)
    JobReviewComment.objects.using(db_alias).bulk_create(comments)

def delete_migrated_content(apps, schema_editor):
    JobReviewComment = apps.get_model('jobs', 'JobReviewComment')
    db_alias = schema_editor.connection.alias
    JobReviewComment.objects.using(db_alias).filter(comment__startswith=MARKER).delete()

class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0001_initial'),
        ('jobs', '0011_jobreviewcomment'),
    ]
    if global_apps.is_installed(comments_app_name):
        dependencies.append((comments_app_name, '0001_initial'))

    operations = [
        migrations.RunPython(migrate_old_content, delete_migrated_content),
    ]

Prompt:
Migrate old comments over jobs.JobReviewComment

Fixes #591


from .exposure import histogram, equalize, equalize_hist, \
                      rescale_intensity, cumulative_distribution, \
                      adjust_gamma, adjust_sigmoid, adjust_log

from ._adapthist import equalize_adapthist
from .unwrap import unwrap

__all__ = ['histogram',
           'equalize',
           'equalize_hist',
           'equalize_adapthist',
           'rescale_intensity',
           'cumulative_distribution',
           'adjust_gamma',
           'adjust_sigmoid',
           'adjust_log',
           'unwrap']

Response:
from .exposure import histogram, equalize, equalize_hist, \
                      rescale_intensity, cumulative_distribution, \
                      adjust_gamma, adjust_sigmoid, adjust_log

from ._adapthist import equalize_adapthist
from .unwrap import unwrap

__all__ = ['histogram',
           'equalize',
           'equalize_hist',
           'equalize_adapthist',
           'rescale_intensity',
           'cumulative_distribution',
           'adjust_gamma',
           'adjust_sigmoid',
           'adjust_log',
           'unwrap']

Prompt:
Make unwrap visible in the exposure package.

from .exposure import histogram, equalize, equalize_hist, \
                      rescale_intensity, cumulative_distribution, \
                      adjust_gamma, adjust_sigmoid, adjust_log

from ._adapthist import equalize_adapthist

__all__ = ['histogram',
           'equalize',
           'equalize_hist',
           'equalize_adapthist',
           'rescale_intensity',
           'cumulative_distribution',
           'adjust_gamma',
           'adjust_sigmoid',
           'adjust_log']


import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Text Playing Game")
        self.set_border_width(10)
        self.set_size_request(500, 400)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Set GUI title and size

Set GUI title
and size

Response:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Text Playing Game")
        self.set_border_width(10)
        self.set_size_request(500, 400)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Prompt:
Set GUI title
and size

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="")

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()


#!/usr/bin/env python
#
# Setup script for Django Evolution

from ez_setup import use_setuptools
use_setuptools()

from setuptools import setup, find_packages
from setuptools.command.test import test

from django_evolution import get_package_version, VERSION

def run_tests(*args):
    import os
    os.system('tests/runtests.py')

test.run_tests = run_tests

PACKAGE_NAME = 'django_evolution'

download_url = (
    'http://downloads.reviewboard.org/releases/django-evolution/%s.%s/' %
    (VERSION[0], VERSION[1]))

# Build the package
setup(
    name=PACKAGE_NAME,
    version=get_package_version(),
    description='A database schema evolution tool for the Django web framework.',
    url='http://code.google.com/p/django-evolution/',
    author='Ben Khoo',
    author_email='khoobks@westnet.com.au',
    maintainer='Christian Hammond',
    maintainer_email='christian@beanbaginc.com',
    download_url=download_url,
    packages=find_packages(exclude=['tests']),
    install_requires=[
        'Django>=1.4.10,<1.7.0',
    ],
    include_package_data=True,
    classifiers=[
        'Development Status :: 4 - Beta',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Software Development',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ]
)

Response:
#!/usr/bin/env python
#
# Setup script for Django Evolution

from ez_setup import use_setuptools
use_setuptools()

from setuptools import setup, find_packages
from setuptools.command.test import test

from django_evolution import get_package_version, VERSION

def run_tests(*args):
    import os
    os.system('tests/runtests.py')

test.run_tests = run_tests

PACKAGE_NAME = 'django_evolution'

download_url = (
    'http://downloads.reviewboard.org/releases/django-evolution/%s.%s/' %
    (VERSION[0], VERSION[1]))

# Build the package
setup(
    name=PACKAGE_NAME,
    version=get_package_version(),
    description='A database schema evolution tool for the Django web framework.',
    url='http://code.google.com/p/django-evolution/',
    author='Ben Khoo',
    author_email='khoobks@westnet.com.au',
    maintainer='Christian Hammond',
    maintainer_email='christian@beanbaginc.com',
    download_url=download_url,
    packages=find_packages(exclude=['tests']),
    install_requires=[
        'Django>=1.4.10,<1.7.0',
    ],
    include_package_data=True,
    classifiers=[
        'Development Status :: 4 - Beta',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Software Development',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ]
)

Prompt:
Revert "Allow Django Evolution to install along with Django >= 1.7."

This reverts commit 28b280bb04f806f614f6f2cd25ce779b551fef9e.

This was committed to the wrong branch.

#!/usr/bin/env python
#
# Setup script for Django Evolution

from ez_setup import use_setuptools
use_setuptools()

from setuptools import setup, find_packages
from setuptools.command.test import test

from django_evolution import get_package_version, VERSION

def run_tests(*args):
    import os
    os.system('tests/runtests.py')

test.run_tests = run_tests

PACKAGE_NAME = 'django_evolution'

download_url = (
    'http://downloads.reviewboard.org/releases/django-evolution/%s.%s/' %
    (VERSION[0], VERSION[1]))

# Build the package
setup(
    name=PACKAGE_NAME,
    version=get_package_version(),
    description='A database schema evolution tool for the Django web framework.',
    url='http://code.google.com/p/django-evolution/',
    author='Ben Khoo',
    author_email='khoobks@westnet.com.au',
    maintainer='Christian Hammond',
    maintainer_email='christian@beanbaginc.com',
    download_url=download_url,
    packages=find_packages(exclude=['tests']),
    install_requires=[
        'Django>=1.4.10',
    ],
    include_package_data=True,
    classifiers=[
        'Development Status :: 4 - Beta',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Software Development',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ]
)


from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATES[0]['OPTIONS']['debug'] = True

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'CHANGEME!!!'

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# Process all tasks synchronously.
# Helpful for local development and running tests
CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
CELERY_ALWAYS_EAGER = True

try:
    from .local import *
except ImportError:
    pass

Response:
from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATES[0]['OPTIONS']['debug'] = True

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'CHANGEME!!!'

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# Process all tasks synchronously.
# Helpful for local development and running tests
CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
CELERY_ALWAYS_EAGER = True

try:
    from .local import *
except ImportError:
    pass

Prompt:
Update TEMPLATE_DEBUG to Django 1.8 version
from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'CHANGEME!!!'

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# Process all tasks synchronously.
# Helpful for local development and running tests
CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
CELERY_ALWAYS_EAGER = True

try:
    from .local import *
except ImportError:
    pass


from ppb_vector import Vector2
import pytest
from hypothesis import given, assume, note
from math import isclose, isinf
from utils import angle_isclose, units, vectors

reflect_data = (
    (Vector2(1, 1), Vector2(0, -1), Vector2(1, -1)),
    (Vector2(1, 1), Vector2(-1, 0), Vector2(-1, 1)),
    (Vector2(0, 1), Vector2(0, -1), Vector2(0, -1)),
    (Vector2(-1, -1), Vector2(1, 0), Vector2(1, -1)),
    (Vector2(-1, -1), Vector2(-1, 0), Vector2(1, -1))
)

@pytest.mark.parametrize("initial_vector, surface_normal, expected_vector", reflect_data)
def test_reflect(initial_vector, surface_normal, expected_vector):
    assert initial_vector.reflect(surface_normal).isclose(expected_vector)

@given(initial=vectors(), normal=units())
def test_reflect_prop(initial: Vector2, normal: Vector2):
    assume(initial ^ normal != 0)
    reflected = initial.reflect(normal)
    returned = reflected.reflect(normal)
    note(f"Reflected: {reflected}")
    assert not any(map(isinf, reflected))
    assert initial.isclose(returned)
    assert isclose((initial * normal), -(reflected * normal))
    assert angle_isclose(normal.angle(initial),
                         180 - normal.angle(reflected)
    )

Response:
from ppb_vector import Vector2
import pytest
from hypothesis import given, assume, note
from math import isclose, isinf
from utils import angle_isclose, units, vectors

reflect_data = (
    (Vector2(1, 1), Vector2(0, -1), Vector2(1, -1)),
    (Vector2(1, 1), Vector2(-1, 0), Vector2(-1, 1)),
    (Vector2(0, 1), Vector2(0, -1), Vector2(0, -1)),
    (Vector2(-1, -1), Vector2(1, 0), Vector2(1, -1)),
    (Vector2(-1, -1), Vector2(-1, 0), Vector2(1, -1))
)

@pytest.mark.parametrize("initial_vector, surface_normal, expected_vector", reflect_data)
def test_reflect(initial_vector, surface_normal, expected_vector):
    assert initial_vector.reflect(surface_normal).isclose(expected_vector)

@given(initial=vectors(), normal=units())
def test_reflect_prop(initial: Vector2, normal: Vector2):
    assume(initial ^ normal != 0)
    reflected = initial.reflect(normal)
    returned = reflected.reflect(normal)
    note(f"Reflected: {reflected}")
    assert not any(map(isinf, reflected))
    assert initial.isclose(returned)
    assert isclose((initial * normal), -(reflected * normal))
    assert angle_isclose(normal.angle(initial),
                         180 - normal.angle(reflected)
    )

Prompt:
test_reflect_prop: Add a property tying reflect() and angle()

from ppb_vector import Vector2
import pytest
from hypothesis import given, assume, note
from math import isclose, isinf
from utils import units, vectors

reflect_data = (
    (Vector2(1, 1), Vector2(0, -1), Vector2(1, -1)),
    (Vector2(1, 1), Vector2(-1, 0), Vector2(-1, 1)),
    (Vector2(0, 1), Vector2(0, -1), Vector2(0, -1)),
    (Vector2(-1, -1), Vector2(1, 0), Vector2(1, -1)),
    (Vector2(-1, -1), Vector2(-1, 0), Vector2(1, -1))
)

@pytest.mark.parametrize("initial_vector, surface_normal, expected_vector", reflect_data)
def test_reflect(initial_vector, surface_normal, expected_vector):
    assert initial_vector.reflect(surface_normal).isclose(expected_vector)

@given(initial=vectors(), normal=units())
def test_reflect_prop(initial: Vector2, normal: Vector2):
    assume(initial ^ normal != 0)
    reflected = initial.reflect(normal)
    returned = reflected.reflect(normal)
    note(f"Reflected: {reflected}")
    assert not any(map(isinf, reflected))
    assert initial.isclose(returned)
    assert isclose((initial * normal), -(reflected * normal))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>
# Copyright 2012 Google, Inc.
#
# 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.

from sys import version_info
from os import name as OS_NAME

__all__= ['unittest', 'skipIfNtMove']

if version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest

skipIfNtMove = unittest.skipIf(OS_NAME == 'nt', "windows can not detect moves")

Response:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>
# Copyright 2012 Google, Inc.
#
# 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.

from sys import version_info
from os import name as OS_NAME

__all__= ['unittest', 'skipIfNtMove']

if version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest

skipIfNtMove = unittest.skipIf(OS_NAME == 'nt', "windows can not detect moves")

Prompt:
Declare unittest lib used within python version

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>
# Copyright 2012 Google, Inc.
#
# 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 requests

def test_rest_api_responsive():
    stmt_str = '{"statements": [{"sbo": "http://identifiers.org/sbo/SBO:0000526", "type": "Complex", "id": "acc6d47c-f622-41a4-8ae9-d7b0f3d24a2f", "members": [{"db_refs": {"TEXT": "MEK", "BE": "MEK"}, "name": "MEK"}, {"db_refs": {"TEXT": "ERK", "NCIT": "C26360", "BE": "ERK"}, "name": "ERK"}], "evidence": [{"text": "MEK binds ERK", "source_api": "trips"}]}]}'
    url = 'http://ec2-34-226-201-156.compute-1.amazonaws.com:8080/' + \
        'assemblers/cyjs'
    res = requests.post(url, stmt_str)
    assert res.status_code == 200

Add smoke test for REST API on Travis

Add smoke test for REST API on Travis

Response:
import requests

def test_rest_api_responsive():
    stmt_str = '{"statements": [{"sbo": "http://identifiers.org/sbo/SBO:0000526", "type": "Complex", "id": "acc6d47c-f622-41a4-8ae9-d7b0f3d24a2f", "members": [{"db_refs": {"TEXT": "MEK", "BE": "MEK"}, "name": "MEK"}, {"db_refs": {"TEXT": "ERK", "NCIT": "C26360", "BE": "ERK"}, "name": "ERK"}], "evidence": [{"text": "MEK binds ERK", "source_api": "trips"}]}]}'
    url = 'http://ec2-34-226-201-156.compute-1.amazonaws.com:8080/' + \
        'assemblers/cyjs'
    res = requests.post(url, stmt_str)
    assert res.status_code == 200

Prompt: Add smoke test for REST API on Travis


#! python3

''' rename files in local directory with random integer names.

windows screen saver isn't very good at randomizing fotos shown.
Change file names regularly to provide more variety
'''

import os
import re
import random
import time

random.seed()
new_names = set()
original_files = []

for entry in os.listdir():
    if os.path.isfile(entry):
        if re.match(".*jpg", entry):
            original_files.append(entry)

for counter in range(0, len(original_files)):
    new_value = random.randint(0,1000000000)
    # Make sure the new names are unique
    # -- note this is only the new set, the new name
    #    may still duplicate an old name. The set is
    #    to minimize this chance
    while new_value in new_names:
        new_value = random.randint(0,1000000000)
    new_names.add(new_value)

for of in original_files:
    nf = str(new_names.pop()).zfill(10) + ".jpg"

    try:
        os.rename(of, nf)
    except Exception as e:
        print("{}: {}".format(of, e))

time.sleep(5)

Response:
#! python3

''' rename files in local directory with random integer names.

windows screen saver isn't very good at randomizing fotos shown.
Change file names regularly to provide more variety
'''

import os
import re
import random
import time

random.seed()
new_names = set()
original_files = []

for entry in os.listdir():
    if os.path.isfile(entry):
        if re.match(".*jpg", entry):
            original_files.append(entry)

for counter in range(0, len(original_files)):
    new_value = random.randint(0,1000000000)
    # Make sure the new names are unique
    # -- note this is only the new set, the new name
    #    may still duplicate an old name. The set is
    #    to minimize this chance
    while new_value in new_names:
        new_value = random.randint(0,1000000000)
    new_names.add(new_value)

for of in original_files:
    nf = str(new_names.pop()).zfill(10) + ".jpg"

    try:
        os.rename(of, nf)
    except Exception as e:
        print("{}: {}".format(of, e))

time.sleep(5)

Prompt:
Increase namespace, sleep before cmd window closes
''' rename files in local directory with random integer names.

windows screen saver isn't very good at randomizing fotos shown.
Change file names regularly to provide more variety
'''

import os
import re
import random

random.seed()
new_names = set()
original_files = []

for entry in os.listdir():
    if os.path.isfile(entry):
        if re.match(".*jpg", entry):
            original_files.append(entry)

for counter in range(0, len(original_files)):
    new_value = random.randint(0,100000)
    while new_value in new_names:
        new_value = random.randint(0,100000)
    new_names.add(new_value)

for of in original_files:
    nf = str(new_names.pop()).zfill(6) + ".jpg"

    try:
        os.rename(of, nf)
    except Exception as e:
        print("{}: {}".format(of, e))


from __future__ import absolute_import

from etcd3.client import Etcd3Client
from etcd3.client import client
from etcd3.client import Transactions
from etcd3.members import Member

__author__ = 'Louis Taylor'
__email__ = 'louis@kragniz.eu'
__version__ = '0.1.0'

__all__ = ['Etcd3Client', 'client', 'etcdrpc', 'utils', 'Transactions',
           'Member']

Make Member part of the public api

Make Member part of the public api

Response:
from __future__ import absolute_import

from etcd3.client import Etcd3Client
from etcd3.client import client
from etcd3.client import Transactions
from etcd3.members import Member

__author__ = 'Louis Taylor'
__email__ = 'louis@kragniz.eu'
__version__ = '0.1.0'

__all__ = ['Etcd3Client', 'client', 'etcdrpc', 'utils', 'Transactions',
           'Member']

Prompt:
Make Member part of the public api

from __future__ import absolute_import

from etcd3.client import Etcd3Client
from etcd3.client import client
from etcd3.client import Transactions

__author__ = 'Louis Taylor'
__email__ = 'louis@kragniz.eu'
__version__ = '0.1.0'

__all__ = ['Etcd3Client', 'client', 'etcdrpc', 'utils', 'Transactions']


from st2common.transport import actionexecution, publishers

# TODO(manas) : Exchanges, Queues and RoutingKey design discussion pending.

__all__ = ['actionexecution', 'publishers']

Response:
from st2common.transport import actionexecution, publishers

# TODO(manas) : Exchanges, Queues and RoutingKey design discussion pending.

__all__ = ['actionexecution', 'publishers']

Prompt:
Add TODO to capture the fact that design discussion is pending.

from st2common.transport import actionexecution, publishers

__all__ = ['actionexecution', 'publishers']


import inspect
import sys
import types

from rmake.lib.apiutils import thaw, freeze

_nodeTypes = {}
class _NodeTypeRegistrar(type):
    def __init__(self, name, bases, dict):
        type.__init__(self, name, bases, dict)
        _nodeTypes[self.nodeType] = self

class NodeType(object):
    __metaclass__ = _NodeTypeRegistrar
    nodeType = 'UNKNOWN'
    def __init__(self):
        pass

    def freeze(self):
        return (self.nodeType, self.__dict__)

    @classmethod
    def thaw(class_, d):
        return class_(**d)

class Client(NodeType):
    nodeType = 'CLIENT'

def thawNodeType(info):
    nodeType = info[0]
    return _nodeTypes[nodeType].thaw(info[1])

Use metaclasses to register node types.

Use metaclasses to register node types.

Response:
import inspect
import sys
import types

from rmake.lib.apiutils import thaw, freeze

_nodeTypes = {}
class _NodeTypeRegistrar(type):
    def __init__(self, name, bases, dict):
        type.__init__(self, name, bases, dict)
        _nodeTypes[self.nodeType] = self

class NodeType(object):
    __metaclass__ = _NodeTypeRegistrar
    nodeType = 'UNKNOWN'
    def __init__(self):
        pass

    def freeze(self):
        return (self.nodeType, self.__dict__)

    @classmethod
    def thaw(class_, d):
        return class_(**d)

class Client(NodeType):
    nodeType = 'CLIENT'

def thawNodeType(info):
    nodeType = info[0]
    return _nodeTypes[nodeType].thaw(info[1])

Prompt:
Use metaclasses to register node types.

import inspect
import sys
import types

from rmake.lib.apiutils import thaw, freeze

class NodeType(object):
    nodeType = 'UNKNOWN'
    def __init__(self):
        pass

    def freeze(self):
        return (self.nodeType, self.__dict__)

    @classmethod
    def thaw(class_, d):
        return class_(**d)

class Client(NodeType):
    nodeType = 'CLIENT'

_nodeTypes = {}
def registerNodeTypes(moduleName):
    global _nodeTypes
    for item in sys.modules[moduleName].__dict__.values():
        if inspect.isclass(item) and issubclass(item, NodeType):
            _nodeTypes[item.nodeType] = item
registerNodeTypes(__name__)

def registerNodeType(class_):
    _nodeTypes[class_.nodeType] = class_

def thawNodeType(info):
    nodeType = info[0]
    return _nodeTypes[nodeType].thaw(info[1])


from django.core.urlresolvers import reverse
from django.utils import timezone
from django.utils.encoding import iri_to_uri

import datetime

from knights.library import Library

register = Library()

@register.helper
def now(fmt):
    return timezone.now().strftime(fmt)

@register.helper
def url(name, *args, **kwargs):
    try:
        return reverse(name, args=args, kwargs=kwargs)
    except:
        return None

@register.helper
def static(filename):
    try:
        from django.conf import settings
    except ImportError:
        prefix = ''
    else:
        prefix = iri_to_uri(getattr(settings, filename, ''))
    return prefix

Make the `now` helper timezone aware

Response:
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.utils.encoding import iri_to_uri

import datetime

from knights.library import Library

register = Library()

@register.helper
def now(fmt):
    return timezone.now().strftime(fmt)

@register.helper
def url(name, *args, **kwargs):
    try:
        return reverse(name, args=args, kwargs=kwargs)
    except:
        return None

@register.helper
def static(filename):
    try:
        from django.conf import settings
    except ImportError:
        prefix = ''
    else:
        prefix = iri_to_uri(getattr(settings, filename, ''))
    return prefix

Prompt:
Make the `now` helper timezone aware

Thanks @tysonclugg

from django.core.urlresolvers import reverse
from django.utils.encoding import iri_to_uri

import datetime

from knights.library import Library

register = Library()

@register.helper
def now(fmt):
    return datetime.datetime.now().strftime(fmt)

@register.helper
def url(name, *args, **kwargs):
    try:
        return reverse(name, args=args, kwargs=kwargs)
    except:
        return None

@register.helper
def static(filename):
    try:
        from django.conf import settings
    except ImportError:
        prefix = ''
    else:
        prefix = iri_to_uri(getattr(settings, filename, ''))
    return prefix


"""elmo URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.views.generic import TemplateView
from django.contrib.auth.views import logout

urlpatterns = [
    url(r'^landing/$', TemplateView.as_view(template_name='landing.html'), name='landing'),
    url(r'^admin/', admin.site.urls),
    url(r'^auth/logout/$', logout, {'next_page': '/'}, name='logout'),
    url(r'^auth/', include('social_django.urls', namespace='social')),
    url(r'^', include('moon_tracker.urls')),
]

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ] + urlpatterns

Add support for Django debug sidebar.

Add support for Django debug sidebar.

Response:
"""elmo URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.views.generic import TemplateView
from django.contrib.auth.views import logout

urlpatterns = [
    url(r'^landing/$', TemplateView.as_view(template_name='landing.html'), name='landing'),
    url(r'^admin/', admin.site.urls),
    url(r'^auth/logout/$', logout, {'next_page': '/'}, name='logout'),
    url(r'^auth/', include('social_django.urls', namespace='social')),
    url(r'^', include('moon_tracker.urls')),
]

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ] + urlpatterns

Prompt:
Add support for Django debug sidebar.

"""elmo URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django.contrib.auth.views import logout

urlpatterns = [
    url(r'^landing/$', TemplateView.as_view(template_name='landing.html'), name='landing'),
    url(r'^admin/', admin.site.urls),
    url(r'^auth/logout/$', logout, {'next_page': '/'}, name='logout'),
    url(r'^auth/', include('social_django.urls', namespace='social')),
    url(r'^', include('moon_tracker.urls')),
]


Add extra space below ending of import

Add extra space below ending of import

Response:
from django.contrib import admin

from . import models

admin.site.register(models.Weekday)
admin.site.register(models.Meal)
admin.site.register(models.MealOption)
admin.site.register(models.Course)
admin.site.register(models.Timetable)
admin.site.register(models.Dish)
admin.site.register(models.Admin)

Prompt:
Add extra space below ending of import

from django.contrib import admin

from . import models

admin.site.register(models.Weekday)
admin.site.register(models.Meal)
admin.site.register(models.MealOption)
admin.site.register(models.Course)
admin.site.register(models.Timetable)
admin.site.register(models.Dish)
admin.site.register(models.Admin)


import configparser #To read the profiles.
import os #To join paths.
import pytest

##  Makes sure that the variants for the Ultimaker 3 Extended are exactly the
#   same as for the Ultimaker 3.
#
#   Once we have specialised profiles or a mechanism to inherit variants too, we
#   may remove this test and have different profiles for the extended where
#   needed, but until then we should keep them the same. It's happened all too
#   often that we updated the variants for the UM3 but forgot about the UM3E.
@pytest.mark.parametrize("um3_file, um3e_file", [
    #List the corresponding files below.
    ("ultimaker3_aa0.8.inst.cfg", "ultimaker3_extended_aa0.8.inst.cfg"),
    ("ultimaker3_aa04.inst.cfg", "ultimaker3_extended_aa04.inst.cfg"),
    ("ultimaker3_bb0.8.inst.cfg", "ultimaker3_extended_bb0.8.inst.cfg"),
    ("ultimaker3_bb04.inst.cfg", "ultimaker3_extended_bb04.inst.cfg")
])
def test_ultimaker3extended_variants(um3_file, um3e_file):
    directory = os.path.join(os.path.dirname(__file__), "..", "resources", "variants") #TODO: Hardcoded path relative to this test file.
    um3 = configparser.ConfigParser()
    um3.read_file(open(os.path.join(directory, um3_file)))
    um3e = configparser.ConfigParser()
    um3e.read_file(open(os.path.join(directory, um3e_file)))
    assert um3["values"] == um3e["values"]

Add test to ensure that UM3 and UM3E variants are kept the same

It's a bit chunky, but functional.

Response:
import configparser #To read the profiles.
import os #To join paths.
import pytest

##  Makes sure that the variants for the Ultimaker 3 Extended are exactly the
#   same as for the Ultimaker 3.
#
#   Once we have specialised profiles or a mechanism to inherit variants too, we
#   may remove this test and have different profiles for the extended where
#   needed, but until then we should keep them the same. It's happened all too
#   often that we updated the variants for the UM3 but forgot about the UM3E.
@pytest.mark.parametrize("um3_file, um3e_file", [
    #List the corresponding files below.
    ("ultimaker3_aa0.8.inst.cfg", "ultimaker3_extended_aa0.8.inst.cfg"),
    ("ultimaker3_aa04.inst.cfg", "ultimaker3_extended_aa04.inst.cfg"),
    ("ultimaker3_bb0.8.inst.cfg", "ultimaker3_extended_bb0.8.inst.cfg"),
    ("ultimaker3_bb04.inst.cfg", "ultimaker3_extended_bb04.inst.cfg")
])
def test_ultimaker3extended_variants(um3_file, um3e_file):
    directory = os.path.join(os.path.dirname(__file__), "..", "resources", "variants") #TODO: Hardcoded path relative to this test file.
    um3 = configparser.ConfigParser()
    um3.read_file(open(os.path.join(directory, um3_file)))
    um3e = configparser.ConfigParser()
    um3e.read_file(open(os.path.join(directory, um3e_file)))
    assert um3["values"] == um3e["values"]

Prompt:
Add test to ensure that UM3 and UM3E variants are kept the same

It's a bit chunky, but functional.


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

import unittest
from flask import Flask
from flask.ext.knot import Knot, get_container

def create_app():
    app = Flask(__name__)
    app.config['TESTING'] = True
    return app

class TestKnot(unittest.TestCase):
    def test_acts_like_container(self):
        app = create_app()
        dic = Knot(app)

        def foo(c):
            return 'bar'

        dic.add_factory(foo)

        self.assertEqual(dic.provide('foo'), 'bar')

    def test_does_use_app_config_on_initialization(self):
        app = create_app()
        app.config['foo'] = 'bar'
        dic = Knot(app)

        self.assertEqual(dic['foo'], 'bar')

    def test_does_not_use_app_config_after_initialization(self):
        app = create_app()
        app.config['foo'] = 'bar'
        dic = Knot()
        dic.init_app(app)

        self.assertRaises(KeyError, lambda: dic['foo'])

    def test_container_is_shared(self):
        app1 = create_app()
        app2 = create_app()

        dic = Knot()
        dic.init_app(app1)
        dic.init_app(app2)

        dic1 = get_container(app1)
        dic2 = get_container(app2)

        assert dic1 is dic2

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

Add test for shared container.

Add test for shared container.

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

import unittest
from flask import Flask
from flask.ext.knot import Knot, get_container

def create_app():
    app = Flask(__name__)
    app.config['TESTING'] = True
    return app

class TestKnot(unittest.TestCase):
    def test_acts_like_container(self):
        app = create_app()
        dic = Knot(app)

        def foo(c):
            return 'bar'

        dic.add_factory(foo)

        self.assertEqual(dic.provide('foo'), 'bar')

    def test_does_use_app_config_on_initialization(self):
        app = create_app()
        app.config['foo'] = 'bar'
        dic = Knot(app)

        self.assertEqual(dic['foo'], 'bar')

    def test_does_not_use_app_config_after_initialization(self):
        app = create_app()
        app.config['foo'] = 'bar'
        dic = Knot()
        dic.init_app(app)

        self.assertRaises(KeyError, lambda: dic['foo'])

    def test_container_is_shared(self):
        app1 = create_app()
        app2 = create_app()

        dic = Knot()
        dic.init_app(app1)
        dic.init_app(app2)

        dic1 = get_container(app1)
        dic2 = get_container(app2)

        assert dic1 is dic2

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

Prompt:
Add test for shared container.
# -*- coding: utf-8 -*-

import unittest
from flask import Flask
from flask.ext.knot import Knot

def create_app():
    app = Flask(__name__)
    app.config['TESTING'] = True
    return app

class TestKnot(unittest.TestCase):
    def test_acts_like_container(self):
        app = create_app()
        dic = Knot(app)

        def foo(c):
            return 'bar'

        dic.add_factory(foo)

        self.assertEqual(dic.provide('foo'), 'bar')

    def test_does_use_app_config_on_initialization(self):
        app = create_app()
        app.config['foo'] = 'bar'
        dic = Knot(app)

        self.assertEqual(dic['foo'], 'bar')

    def test_does_not_use_app_config_after_initialization(self):
        app = create_app()
        app.config['foo'] = 'bar'
        dic = Knot()
        dic.init_app(app)

        self.assertRaises(KeyError, lambda: dic['foo'])

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


import numpy as np
from numpy.testing import *

import scipy.ndimage as ndimage

def test_byte_order_median():
    """Regression test for #413: median_filter does not handle bytes orders."""
    a = np.arange(9, dtype='<f4').reshape(3, 3)
    ref = ndimage.filters.median_filter(a,(3, 3))
    b = np.arange(9, dtype='>f4').reshape(3, 3)
    t = ndimage.filters.median_filter(b, (3, 3))
    assert_array_almost_equal(ref, t)

def test_zoom_output_shape():
    """Ticket #643"""
    x = np.arange(12).reshape((3,4))
    ndimage.zoom(x, 2, output=np.zeros((6,8)))

if __name__ == "__main__":
    run_module_suite()

Response:
import numpy as np
from numpy.testing import *

import scipy.ndimage as ndimage

def test_byte_order_median():
    """Regression test for #413: median_filter does not handle bytes orders."""
    a = np.arange(9, dtype='<f4').reshape(3, 3)
    ref = ndimage.filters.median_filter(a,(3, 3))
    b = np.arange(9, dtype='>f4').reshape(3, 3)
    t = ndimage.filters.median_filter(b, (3, 3))
    assert_array_almost_equal(ref, t)

def test_zoom_output_shape():
    """Ticket #643"""
    x = np.arange(12).reshape((3,4))
    ndimage.zoom(x, 2, output=np.zeros((6,8)))

if __name__ == "__main__":
    run_module_suite()

Prompt:
Use run_module_suite instead of deprecated NumpyTest.

import numpy as np
from numpy.testing import *

import scipy.ndimage as ndimage

def test_byte_order_median():
    """Regression test for #413: median_filter does not handle bytes orders."""
    a = np.arange(9, dtype='<f4').reshape(3, 3)
    ref = ndimage.filters.median_filter(a,(3, 3))
    b = np.arange(9, dtype='>f4').reshape(3, 3)
    t = ndimage.filters.median_filter(b, (3, 3))
    assert_array_almost_equal(ref, t)

def test_zoom_output_shape():
    """Ticket #643"""
    x = np.arange(12).reshape((3,4))
    ndimage.zoom(x, 2, output=np.zeros((6,8)))

if __name__ == "__main__":
    NumpyTest().run()


# Copyright (c) 2017 Ruben Dulek
# The PostProcessingPlugin is released under the terms of the AGPLv3 or higher.

import re #To perform the search and replace.

from ..Script import Script

##  Performs a search-and-replace on all g-code.
#
#   Due to technical limitations, the search can't cross the border between
#   layers.
class SearchAndReplace(Script):
    def getSettingDataString(self):
        return """{
            "name": "Search and Replace",
            "key": "SearchAndReplace",
            "metadata": {},
            "version": 2,
            "settings":
            {
                "search":
                {
                    "label": "Search",
                    "description": "All occurrences of this text will get replaced by the replacement text.",
                    "type": "str",
                    "default_value": ""
                },
                "replace":
                {
                    "label": "Replace",
                    "description": "The search text will get replaced by this text.",
                    "type": "str",
                    "default_value": ""
                },
                "is_regex":
                {
                    "label": "Use Regular Expressions",
                    "description": "When enabled, the search text will be interpreted as a regular expression.",
                    "type": "bool",
                    "default_value": false
                }
            }
        }"""

    def execute(self, data):
        search_string = self.getSettingValueByKey("search")
        if not self.getSettingValueByKey("is_regex"):
            search_string = re.escape(search_string) #Need to search for the actual string, not as a regex.
        search_regex = re.compile(search_string)

        replace_string = self.getSettingValueByKey("replace")

        for layer_number, layer in enumerate(data):
            data[layer_number] = re.sub(search_regex, replace_string, layer) #Replace all.

        return data

Add search and replace script

Add search and replace script

Response:
# Copyright (c) 2017 Ruben Dulek
# The PostProcessingPlugin is released under the terms of the AGPLv3 or higher.

import re #To perform the search and replace.

from ..Script import Script

##  Performs a search-and-replace on all g-code.
#
#   Due to technical limitations, the search can't cross the border between
#   layers.
class SearchAndReplace(Script):
    def getSettingDataString(self):
        return """{
            "name": "Search and Replace",
            "key": "SearchAndReplace",
            "metadata": {},
            "version": 2,
            "settings":
            {
                "search":
                {
                    "label": "Search",
                    "description": "All occurrences of this text will get replaced by the replacement text.",
                    "type": "str",
                    "default_value": ""
                },
                "replace":
                {
                    "label": "Replace",
                    "description": "The search text will get replaced by this text.",
                    "type": "str",
                    "default_value": ""
                },
                "is_regex":
                {
                    "label": "Use Regular Expressions",
                    "description": "When enabled, the search text will be interpreted as a regular expression.",
                    "type": "bool",
                    "default_value": false
                }
            }
        }"""

    def execute(self, data):
        search_string = self.getSettingValueByKey("search")
        if not self.getSettingValueByKey("is_regex"):
            search_string = re.escape(search_string) #Need to search for the actual string, not as a regex.
        search_regex = re.compile(search_string)

        replace_string = self.getSettingValueByKey("replace")

        for layer_number, layer in enumerate(data):
            data[layer_number] = re.sub(search_regex, replace_string, layer) #Replace all.

        return data

Prompt: Add search and replace script


import json
import logging
import time
from datetime import datetime

import zmq
from django.conf import settings
from django.core.management.base import NoArgsCommand

from job_runner.apps.job_runner.models import Run

logger = logging.getLogger(__name__)

class Command(NoArgsCommand):
    help = 'Broadcast runs in queue to workers'

    def handle_noargs(self, **options):
        logger.info('Starting queue broadcaster')
        context = zmq.Context(1)
        publisher = context.socket(zmq.PUB)
        publisher.bind(
            'tcp://*:{0}'.format(settings.JOB_RUNNER_BROADCASTER_PORT))

        # give the subscribers some time to (re-)connect.
        time.sleep(2)

        while True:
            self._broadcast(publisher)
            time.sleep(5)

        publisher.close()
        context.term()

    def _broadcast(self, publisher):
        """
        Broadcast runs that are scheduled to run now.

        :param publisher:
            A ``zmq`` publisher.

        """
        enqueueable_runs = Run.objects.awaiting_enqueue().filter(
            schedule_dts__lte=datetime.utcnow()).select_related()

        for run in enqueueable_runs:
            worker = run.job.job_template.worker
            message = [
                'master.broadcast.{0}'.format(worker.api_key),
                json.dumps({'run_id': run.id, 'action': 'enqueue'})
            ]
            logger.debug('Sending: {0}'.format(message))
            publisher.send_multipart(message)

Response:
import json
import logging
import time
from datetime import datetime

import zmq
from django.conf import settings
from django.core.management.base import NoArgsCommand

from job_runner.apps.job_runner.models import Run

logger = logging.getLogger(__name__)

class Command(NoArgsCommand):
    help = 'Broadcast runs in queue to workers'

    def handle_noargs(self, **options):
        logger.info('Starting queue broadcaster')
        context = zmq.Context(1)
        publisher = context.socket(zmq.PUB)
        publisher.bind(
            'tcp://*:{0}'.format(settings.JOB_RUNNER_BROADCASTER_PORT))

        # give the subscribers some time to (re-)connect.
        time.sleep(2)

        while True:
            self._broadcast(publisher)
            time.sleep(5)

        publisher.close()
        context.term()

    def _broadcast(self, publisher):
        """
        Broadcast runs that are scheduled to run now.

        :param publisher:
            A ``zmq`` publisher.

        """
        enqueueable_runs = Run.objects.awaiting_enqueue().filter(
            schedule_dts__lte=datetime.utcnow()).select_related()

        for run in enqueueable_runs:
            worker = run.job.job_template.worker
            message = [
                'master.broadcast.{0}'.format(worker.api_key),
                json.dumps({'run_id': run.id, 'action': 'enqueue'})
            ]
            logger.debug('Sending: {0}'.format(message))
            publisher.send_multipart(message)

Prompt:
Add time.sleep after binding publisher to give subscribers time to (re-)connect.

import json
import logging
import time
from datetime import datetime

import zmq
from django.conf import settings
from django.core.management.base import NoArgsCommand

from job_runner.apps.job_runner.models import Run

logger = logging.getLogger(__name__)

class Command(NoArgsCommand):
    help = 'Broadcast runs in queue to workers'

    def handle_noargs(self, **options):
        logger.info('Starting queue broadcaster')
        context = zmq.Context(1)
        publisher = context.socket(zmq.PUB)
        publisher.bind(
            'tcp://*:{0}'.format(settings.JOB_RUNNER_BROADCASTER_PORT))

        while True:
            self._broadcast(publisher)
            time.sleep(5)

        publisher.close()
        context.term()

    def _broadcast(self, publisher):
        """
        Broadcast runs that are scheduled to run now.

        :param publisher:
            A ``zmq`` publisher.

        """
        enqueueable_runs = Run.objects.awaiting_enqueue().filter(
            schedule_dts__lte=datetime.utcnow()).select_related()

        for run in enqueueable_runs:
            worker = run.job.job_template.worker
            message = [
                'master.broadcast.{0}'.format(worker.api_key),
                json.dumps({'run_id': run.id, 'action': 'enqueue'})
            ]
            logger.debug('Sending: {0}'.format(message))
            publisher.send_multipart(message)


from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector
from .. import items

class DatasetSpider(CrawlSpider):

    pages = 9466
    name = 'dataset'
    allowed_domains = ['data.gc.ca']
    start_urls = []

    for i in range(1, pages + 1):
        start_urls.append('http://data.gc.ca/data/en/dataset?page=' + str(i))

    rules = [Rule(SgmlLinkExtractor(allow=['/dataset/[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}']),
                  'parse_dataset')]

    def parse_dataset(self, response):
        sel = Selector(response)
        dataset = items.DatasetItem()
        dataset['url'] = response.url
        dataset['name'] = sel.xpath("//div[@class='span-6']/article/div[@class='module'][1]/section[@class='module-content indent-large'][1]/h1/text()").extract()
        dataset['frequency'] = sel.xpath("//div[@class='span-2']/aside[@class='secondary']/div[@class='module-related'][2]/ul[1]/li[@class='margin-bottom-medium']/text()").re('[A-Z]{1}[a-z]+')[0].encode('ascii','ignore')

        return dataset

Response:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector
from .. import items

class DatasetSpider(CrawlSpider):

    pages = 9466
    name = 'dataset'
    allowed_domains = ['data.gc.ca']
    start_urls = []

    for i in range(1, pages + 1):
        start_urls.append('http://data.gc.ca/data/en/dataset?page=' + str(i))

    rules = [Rule(SgmlLinkExtractor(allow=['/dataset/[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}']),
                  'parse_dataset')]

    def parse_dataset(self, response):
        sel = Selector(response)
        dataset = items.DatasetItem()
        dataset['url'] = response.url
        dataset['name'] = sel.xpath("//div[@class='span-6']/article/div[@class='module'][1]/section[@class='module-content indent-large'][1]/h1/text()").extract()
        dataset['frequency'] = sel.xpath("//div[@class='span-2']/aside[@class='secondary']/div[@class='module-related'][2]/ul[1]/li[@class='margin-bottom-medium']/text()").re('[A-Z]{1}[a-z]+')[0].encode('ascii','ignore')

        return dataset

Prompt:
Add regex to filter out tab/space/newline

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector
from .. import items

class DatasetSpider(CrawlSpider):

    pages = 9466
    name = 'dataset'
    allowed_domains = ['data.gc.ca']
    start_urls = []

    for i in range(1, pages + 1):
        start_urls.append('http://data.gc.ca/data/en/dataset?page=' + str(i))

    rules = [Rule(SgmlLinkExtractor(allow=['/dataset/[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}']),
                  'parse_dataset')]

    def parse_dataset(self, response):
        sel = Selector(response)
        dataset = items.DatasetItem()
        dataset['url'] = response.url
        dataset['name'] = sel.xpath("//div[@class='span-6']/article/div[@class='module'][1]/section[@class='module-content indent-large'][1]/h1/text()").extract()
        dataset['frequency'] = sel.xpath("//div[@class='span-2']/aside[@class='secondary']/div[@class='module-related'][2]/ul[1]/li[@class='margin-bottom-medium']/text()").extract()

        return dataset


import sublime, sublime_plugin

import requests
from requests import delete, get, head, options, patch, post, put

class RequestCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        self.import_variables()
        selections = self.get_selections()
        for s in selections:
            response = eval(s)
            print(response.text)

    def import_variables(self):
        with open('/Users/kylebebak/GoogleDrive/Code/Config/ST/Packages/http_requests/_requests_variables.py') as f:
            exec(f.read(), globals())

    def get_selections(self):
        view = self.view
        selections = []
        for region in view.sel():
            if not region.empty():
                selections.append( view.substr(region) )
            else:
                selections.append( view.substr(view.line(region)) )
        return selections

Response:
import sublime, sublime_plugin

import requests
from requests import delete, get, head, options, patch, post, put

class RequestCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        self.import_variables()
        selections = self.get_selections()
        for s in selections:
            response = eval(s)
            print(response.text)

    def import_variables(self):
        with open('/Users/kylebebak/GoogleDrive/Code/Config/ST/Packages/http_requests/_requests_variables.py') as f:
            exec(f.read(), globals())

    def get_selections(self):
        view = self.view
        selections = []
        for region in view.sel():
            if not region.empty():
                selections.append( view.substr(region) )
            else:
                selections.append( view.substr(view.line(region)) )
        return selections

Prompt:
Allow multiple requests to be made at once by iterating over selections

import sublime, sublime_plugin

import requests
from requests import delete, get, head, options, patch, post, put

class RequestCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        self.import_variables()
        request = self.view.substr(self.view.line(self.view.sel()[0]))
        response = eval(request)
        print(response.text)

    def import_variables(self):
        with open('/Users/kylebebak/GoogleDrive/Code/Config/ST/Packages/http_requests/_request_variables.py') as f:
            exec(f.read(), globals())


#!/usr/bin/env python3
# utility.py
# PixPack Photo Organiser
# It contains some useful functions to increase user experience

import locale
import os

def sys_trans_var():
    # check system language
    sys_loc = locale.getlocale()
    sys_lang = sys_loc[0] # system default language
    if sys_lang == 'en_EN' or sys_lang == 'en_GB':
        return 0
    elif sys_lang == 'tr_TR':
        return 1
    else:
        return 0

def name_existing_photos(dest_directory, dest_file, copy_suffix):
    # rename if the file is existed already, for instance: photo_1
    dest_file_path = os.path.join(dest_directory, dest_file)
    i=1
    if os.path.exists(dest_file_path):
        dest_directory = os.path.join(dest_directory, "copies")
        if not os.path.exists(dest_directory):
            os.makedirs(dest_directory)
    while os.path.exists(dest_file_path):
        dest_file_path = os.path.join(dest_directory, dest_file)
        file_name = os.path.basename(dest_file_path)
        name, ext = os.path.splitext(file_name)
        name = name + "_" + str(copy_suffix) + str(i)
        new_name = name + ext
        dest_file_path = os.path.join(dest_directory, new_name)
        i+=1
    return dest_file_path

Response:
#!/usr/bin/env python3
# utility.py
# PixPack Photo Organiser
# It contains some useful functions to increase user experience

import locale
import os

def sys_trans_var():
    # check system language
    sys_loc = locale.getlocale()
    sys_lang = sys_loc[0] # system default language
    if sys_lang == 'en_EN' or sys_lang == 'en_GB':
        return 0
    elif sys_lang == 'tr_TR':
        return 1
    else:
        return 0

def name_existing_photos(dest_directory, dest_file, copy_suffix):
    # rename if the file is existed already, for instance: photo_1
    dest_file_path = os.path.join(dest_directory, dest_file)
    i=1
    if os.path.exists(dest_file_path):
        dest_directory = os.path.join(dest_directory, "copies")
        if not os.path.exists(dest_directory):
            os.makedirs(dest_directory)
    while os.path.exists(dest_file_path):
        dest_file_path = os.path.join(dest_directory, dest_file)
        file_name = os.path.basename(dest_file_path)
        name, ext = os.path.splitext(file_name)
        name = name + "_" + str(copy_suffix) + str(i)
        new_name = name + ext
        dest_file_path = os.path.join(dest_directory, new_name)
        i+=1
    return dest_file_path

Prompt:
Store the duplicated items separately in related folder

#!/usr/bin/env python3
# utility.py
# PixPack Photo Organiser
# It contains some useful functions to increase user experience

import locale
import os

def sys_trans_var():
    # check system language
    sys_loc = locale.getlocale()
    sys_lang = sys_loc[0] # system default language
    if sys_lang == 'en_EN' or sys_lang == 'en_GB':
        return 0
    elif sys_lang == 'tr_TR':
        return 1
    else:
        return 0

def name_existing_photos(dest_directory, dest_file, copy_suffix):
    # rename if the file is existed already, for instance: photo_1
    dest_file_path = os.path.join(dest_directory, dest_file)
    i=1
    while os.path.exists(dest_file_path):
        dest_file_path = os.path.join(dest_directory, dest_file)
        file_name = os.path.basename(dest_file_path)
        name, ext = os.path.splitext(file_name)
        name = name + "_" + str(copy_suffix) + str(i)
        new_name = name + ext
        dest_file_path = os.path.join(dest_directory, new_name)
        i+=1
    return dest_file_path


#!/usr/bin/env python

"""
This is the main file. The script finds the game window and sets up the
coordinates for each block.

The MIT License (MIT)
(c) 2016
"""

import pyautogui, logging, time

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d: %(message)s', datefmt='%H:%M:%S')

# logging.disable(logging.DEBUG) # uncomment to block debug log messages

__author__ = "Alex Flores Escarcega"
__copyright__ = "Copyright 2007, Alex Flores Escarcega"
__credits__ = ["Alex Flores Escarcega"]
__license__ = "MIT"
__version__ = "1.0.1"
__maintainer__ = "Alex Flores Escarcega"
__email__ = "alex.floresescar@gmail.com"
__status__ = "Development"

def main():
    """
    No inputs
    No outputs
    Starts up the gameregionfinder
    """

if __name__ == "__main__":
        main()

Response:
#!/usr/bin/env python

"""
This is the main file. The script finds the game window and sets up the
coordinates for each block.

The MIT License (MIT)
(c) 2016
"""

import pyautogui, logging, time

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d: %(message)s', datefmt='%H:%M:%S')

# logging.disable(logging.DEBUG) # uncomment to block debug log messages

__author__ = "Alex Flores Escarcega"
__copyright__ = "Copyright 2007, Alex Flores Escarcega"
__credits__ = ["Alex Flores Escarcega"]
__license__ = "MIT"
__version__ = "1.0.1"
__maintainer__ = "Alex Flores Escarcega"
__email__ = "alex.floresescar@gmail.com"
__status__ = "Development"

def main():
    """
    No inputs
    No outputs
    Starts up the gameregionfinder
    """

if __name__ == "__main__":
        main()

Prompt:
Add imports, logging configs, and authorship info

#!/usr/bin/env python

"""
This is the main file. The script finds the game window and sets up the
coordinates for each block.

The MIT License (MIT)
(c) 2016
"""

def main():
    """
    No inputs
    No outputs
    Starts up the gameregionfinder
    """

if __name__ == "__main__":
        main()


import os
import pytest
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')

def test_docker_service_enabled(host):
    service = host.service('docker')
    assert service.is_enabled

def test_docker_service_running(host):
    service = host.service('docker')
    assert service.is_running

@pytest.mark.parametrize('socket_def', [
    # all IPv4 tcp sockets on port 8883 (MQQTS)
    ('tcp://8883'),
])
def test_listening_sockets(host, socket_def):
    socket = host.socket(socket_def)
    assert socket.is_listening

# Tests to write:
# - using the localhost listener:
#    - verify that a message can be published
#    - verify that a published message can be subscribed to

Response:
import os
import pytest
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')

def test_docker_service_enabled(host):
    service = host.service('docker')
    assert service.is_enabled

def test_docker_service_running(host):
    service = host.service('docker')
    assert service.is_running

@pytest.mark.parametrize('socket_def', [
    # all IPv4 tcp sockets on port 8883 (MQQTS)
    ('tcp://8883'),
])
def test_listening_sockets(host, socket_def):
    socket = host.socket(socket_def)
    assert socket.is_listening

# Tests to write:
# - using the localhost listener:
#    - verify that a message can be published
#    - verify that a published message can be subscribed to

Prompt:
Remove test enforcing listening on port 1883

import os
import pytest
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')

def test_docker_service_enabled(host):
    service = host.service('docker')
    assert service.is_enabled

def test_docker_service_running(host):
    service = host.service('docker')
    assert service.is_running

@pytest.mark.parametrize('socket_def', [
    # listening on localhost, for tcp sockets on port 1883 (MQQT)
    ('tcp://127.0.0.1:1883'),
    # all IPv4 tcp sockets on port 8883 (MQQTS)
    ('tcp://8883'),
])
def test_listening_sockets(host, socket_def):
    socket = host.socket(socket_def)
    assert socket.is_listening

# Tests to write:
# - using the localhost listener:
#    - verify that a message can be published
#    - verify that a published message can be subscribed to


import pytest
import bluesky.plans as bp

def test_legacy_plan_names():
    assert bp.outer_product_scan is bp.grid_scan
    assert bp.relative_outer_product_scan is bp.rel_grid_scan
    assert bp.relative_scan is bp.rel_scan
    assert bp.relative_spiral is bp.rel_spiral
    assert bp.relative_spiral_fermat is bp.rel_spiral_fermat
    assert bp.relative_list_scan is bp.rel_list_scan
    assert relative_log_scan is rel_log_scan
    assert relative_adaptive_scan is rel_adaptive_scan

Add test to check for legacy plan names

TST: Add test to check for legacy plan names

Response:
import pytest
import bluesky.plans as bp

def test_legacy_plan_names():
    assert bp.outer_product_scan is bp.grid_scan
    assert bp.relative_outer_product_scan is bp.rel_grid_scan
    assert bp.relative_scan is bp.rel_scan
    assert bp.relative_spiral is bp.rel_spiral
    assert bp.relative_spiral_fermat is bp.rel_spiral_fermat
    assert bp.relative_list_scan is bp.rel_list_scan
    assert relative_log_scan is rel_log_scan
    assert relative_adaptive_scan is rel_adaptive_scan

Prompt: TST: Add test to check for legacy plan names


import csv
import sys

from django.core.management.base import BaseCommand, CommandError

from pombola.core.models import Organisation, OrganisationRelationship

MAPS_URL_TEMPLATE = 'https://www.google.com/maps/place/{lat}+{lon}/@{lat},{lon},17z'

def encode_row_values_to_utf8(row):
    return {
        k: unicode(v).encode('utf-8')
        for k, v in row.items()
    }

class Command(BaseCommand):

    def handle(self, *args, **options):

        fields = [
            'OfficeID',
            'OfficeName',
            'MapURL',
            'PhysicalAddress',
            'Latitude',
            'Longitude',
            'PartyName',
            'PartyID'
        ]

        writer = csv.DictWriter(sys.stdout, fieldnames=fields)

        writer.writeheader()

        for o in Organisation.objects. \
            filter(kind__slug='constituency-office'). \
            order_by('slug'):
            places = o.place_set.filter(
                kind__slug='constituency-office',
                name__startswith='Approximate position of ',
            )
            place_count = places.count()
            if place_count > 1:
                msg = "Multiple places ({place_count}) found for {organisation_id}"
                raise CommandError(msg.format(
                    place_count=place_count,
                    organisation_id=o.id
                ))
            row = {'OfficeID': o.id, 'OfficeName': o.name}
            if place_count:
                location = places[0].location
                row['Latitude'] = location.y
                row['Longitude'] = location.x
                row['MapURL'] = MAPS_URL_TEMPLATE.format(
                    lat=location.y,
                    lon=location.x,
                )
            # Try to find the party whose office this is:
            try:
                party = OrganisationRelationship.objects.get(
                    organisation_b=o,
                    kind__name='has_office'
                ).organisation_a
                row['PartyID'] = party.id
                row['PartyName'] = party.name
            except OrganisationRelationship.DoesNotExist:
                message = "No party found for office with ID {office_id}\n"
                self.stderr.write(message.format(office_id=o.id))
            # Now try to find the address from the office
            # organisation's contacts:
            addresses = o.contacts.filter(kind__slug='address')
            address_count = addresses.count()
            if address_count > 1:
                msg = "Multiple address found for {organisation_id}"
                raise CommandError(msg.format(
                    address_count=address_count,
                    organisation_id=o.id
                ))
            if address_count:
                row['PhysicalAddress'] = addresses[0].value
            writer.writerow(encode_row_values_to_utf8(row))

ZA: Add a command to output a CSV file of constituency offices

Response:
import csv
import sys

from django.core.management.base import BaseCommand, CommandError

from pombola.core.models import Organisation, OrganisationRelationship

MAPS_URL_TEMPLATE = 'https://www.google.com/maps/place/{lat}+{lon}/@{lat},{lon},17z'

def encode_row_values_to_utf8(row):
    return {
        k: unicode(v).encode('utf-8')
        for k, v in row.items()
    }

class Command(BaseCommand):

    def handle(self, *args, **options):

        fields = [
            'OfficeID',
            'OfficeName',
            'MapURL',
            'PhysicalAddress',
            'Latitude',
            'Longitude',
            'PartyName',
            'PartyID'
        ]

        writer = csv.DictWriter(sys.stdout, fieldnames=fields)

        writer.writeheader()

        for o in Organisation.objects. \
            filter(kind__slug='constituency-office'). \
            order_by('slug'):
            places = o.place_set.filter(
                kind__slug='constituency-office',
                name__startswith='Approximate position of ',
            )
            place_count = places.count()
            if place_count > 1:
                msg = "Multiple places ({place_count}) found for {organisation_id}"
                raise CommandError(msg.format(
                    place_count=place_count,
                    organisation_id=o.id
                ))
            row = {'OfficeID': o.id, 'OfficeName': o.name}
            if place_count:
                location = places[0].location
                row['Latitude'] = location.y
                row['Longitude'] = location.x
                row['MapURL'] = MAPS_URL_TEMPLATE.format(
                    lat=location.y,
                    lon=location.x,
                )
            # Try to find the party whose office this is:
            try:
                party = OrganisationRelationship.objects.get(
                    organisation_b=o,
                    kind__name='has_office'
                ).organisation_a
                row['PartyID'] = party.id
                row['PartyName'] = party.name
            except OrganisationRelationship.DoesNotExist:
                message = "No party found for office with ID {office_id}\n"
                self.stderr.write(message.format(office_id=o.id))
            # Now try to find the address from the office
            # organisation's contacts:
            addresses = o.contacts.filter(kind__slug='address')
            address_count = addresses.count()
            if address_count > 1:
                msg = "Multiple address found for {organisation_id}"
                raise CommandError(msg.format(
                    address_count=address_count,
                    organisation_id=o.id
                ))
            if address_count:
                row['PhysicalAddress'] = addresses[0].value
            writer.writerow(encode_row_values_to_utf8(row))

Prompt: ZA: Add a command to output a CSV file of constituency offices