from parcels import Grid, Particle, JITParticle, AdvectionRK4, Geographic, GeographicPolar
import numpy as np
import pytest
from datetime import timedelta as delta

ptype = {'scipy': Particle, 'jit': JITParticle}

@pytest.fixture
def lon(xdim=200):
    return np.linspace(-170, 170, xdim, dtype=np.float32)

@pytest.fixture
def lat(ydim=100):
    return np.linspace(-80, 80, ydim, dtype=np.float32)

@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_advection_zonal(lon, lat, mode, npart=10):
    """ Particles at high latitude move geographically faster due to
        the pole correction in `GeographicPolar`.
    """
    U = np.ones((lon.size, lat.size), dtype=np.float32)
    V = np.zeros((lon.size, lat.size), dtype=np.float32)
    grid = Grid.from_data(U, lon, lat, V, lon, lat,
                          u_units=GeographicPolar(), v_units=Geographic())

    pset = grid.ParticleSet(npart, pclass=ptype[mode],
                            lon=np.zeros(npart, dtype=np.float32) + 20.,
                            lat=np.linspace(0, 80, npart, dtype=np.float32))
    pset.execute(AdvectionRK4, endtime=delta(hours=2), dt=delta(seconds=30))
    assert (np.diff(np.array([p.lon for p in pset])) > 1.e-4).all()

@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_advection_meridional(lon, lat, mode, npart=10):
    """ Particles at high latitude move geographically faster due to
        the pole correction in `GeographicPolar`.
    """
    U = np.zeros((lon.size, lat.size), dtype=np.float32)
    V = np.ones((lon.size, lat.size), dtype=np.float32)
    grid = Grid.from_data(U, lon, lat, V, lon, lat,
                          u_units=GeographicPolar(), v_units=Geographic())

    pset = grid.ParticleSet(npart, pclass=ptype[mode],
                            lon=np.linspace(-60, 60, npart, dtype=np.float32),
                            lat=np.linspace(0, 30, npart, dtype=np.float32))
    delta_lat = np.diff(np.array([p.lat for p in pset]))
    pset.execute(AdvectionRK4, endtime=delta(hours=2), dt=delta(seconds=30))
    assert np.allclose(np.diff(np.array([p.lat for p in pset])), delta_lat, rtol=1.e-4)

Tests: Add a set of advection tests for meridional and zonal advection

Response:
from parcels import Grid, Particle, JITParticle, AdvectionRK4, Geographic, GeographicPolar
import numpy as np
import pytest
from datetime import timedelta as delta

ptype = {'scipy': Particle, 'jit': JITParticle}

@pytest.fixture
def lon(xdim=200):
    return np.linspace(-170, 170, xdim, dtype=np.float32)

@pytest.fixture
def lat(ydim=100):
    return np.linspace(-80, 80, ydim, dtype=np.float32)

@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_advection_zonal(lon, lat, mode, npart=10):
    """ Particles at high latitude move geographically faster due to
        the pole correction in `GeographicPolar`.
    """
    U = np.ones((lon.size, lat.size), dtype=np.float32)
    V = np.zeros((lon.size, lat.size), dtype=np.float32)
    grid = Grid.from_data(U, lon, lat, V, lon, lat,
                          u_units=GeographicPolar(), v_units=Geographic())

    pset = grid.ParticleSet(npart, pclass=ptype[mode],
                            lon=np.zeros(npart, dtype=np.float32) + 20.,
                            lat=np.linspace(0, 80, npart, dtype=np.float32))
    pset.execute(AdvectionRK4, endtime=delta(hours=2), dt=delta(seconds=30))
    assert (np.diff(np.array([p.lon for p in pset])) > 1.e-4).all()

@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_advection_meridional(lon, lat, mode, npart=10):
    """ Particles at high latitude move geographically faster due to
        the pole correction in `GeographicPolar`.
    """
    U = np.zeros((lon.size, lat.size), dtype=np.float32)
    V = np.ones((lon.size, lat.size), dtype=np.float32)
    grid = Grid.from_data(U, lon, lat, V, lon, lat,
                          u_units=GeographicPolar(), v_units=Geographic())

    pset = grid.ParticleSet(npart, pclass=ptype[mode],
                            lon=np.linspace(-60, 60, npart, dtype=np.float32),
                            lat=np.linspace(0, 30, npart, dtype=np.float32))
    delta_lat = np.diff(np.array([p.lat for p in pset]))
    pset.execute(AdvectionRK4, endtime=delta(hours=2), dt=delta(seconds=30))
    assert np.allclose(np.diff(np.array([p.lat for p in pset])), delta_lat, rtol=1.e-4)

Prompt: Tests: Add a set of advection tests for meridional and zonal advection


#!/usr/bin/env python
from setuptools import setup, find_packages
from cron_sentry.version import VERSION

setup(
    name='cron-sentry',
    version=VERSION,
    author='Yipit Coders',
    author_email='coders@yipit.com',
    description='Cron-Sentry is a command-line wrapper that reports unsuccessful runs to Sentry (https://www.getsentry.com)',
    long_description=open('README.md').read(),
    license='MIT',
    classifiers=[
        'Topic :: Utilities',
        'Intended Audience :: System Administrators',
        'Operating System :: OS Independent',
        'License :: OSI Approved :: MIT License',
    ],
    url='http://github.com/yipit/cron-sentry',
    packages=find_packages(),
    install_requires=['raven', 'argparse'],
    data_files=[],
    entry_points={
        'console_scripts': [
            # `raven-cron` entry point is for backwards compatibility purposes.
            # it should get removed in future releases
            'raven-cron = cron_sentry.runner:run',
            'cron-sentry = cron_sentry.runner:run',
        ]
    }
)

Response:
#!/usr/bin/env python
from setuptools import setup, find_packages
from cron_sentry.version import VERSION

setup(
    name='cron-sentry',
    version=VERSION,
    author='Yipit Coders',
    author_email='coders@yipit.com',
    description='Cron-Sentry is a command-line wrapper that reports unsuccessful runs to Sentry (https://www.getsentry.com)',
    long_description=open('README.md').read(),
    license='MIT',
    classifiers=[
        'Topic :: Utilities',
        'Intended Audience :: System Administrators',
        'Operating System :: OS Independent',
        'License :: OSI Approved :: MIT License',
    ],
    url='http://github.com/yipit/cron-sentry',
    packages=find_packages(),
    install_requires=['raven', 'argparse'],
    data_files=[],
    entry_points={
        'console_scripts': [
            # `raven-cron` entry point is for backwards compatibility purposes.
            # it should get removed in future releases
            'raven-cron = cron_sentry.runner:run',
            'cron-sentry = cron_sentry.runner:run',
        ]
    }
)

Prompt:
Add missing package requirement of argparse.

#!/usr/bin/env python
from setuptools import setup, find_packages
from cron_sentry.version import VERSION

setup(
    name='cron-sentry',
    version=VERSION,
    author='Yipit Coders',
    author_email='coders@yipit.com',
    description='Cron-Sentry is a command-line wrapper that reports unsuccessful runs to Sentry (https://www.getsentry.com)',
    long_description=open('README.md').read(),
    license='MIT',
    classifiers=[
        'Topic :: Utilities',
        'Intended Audience :: System Administrators',
        'Operating System :: OS Independent',
        'License :: OSI Approved :: MIT License',
    ],
    url='http://github.com/yipit/cron-sentry',
    packages=find_packages(),
    install_requires=['raven'],
    data_files=[],
    entry_points={
        'console_scripts': [
            # `raven-cron` entry point is for backwards compatibility purposes.
            # it should get removed in future releases
            'raven-cron = cron_sentry.runner:run',
            'cron-sentry = cron_sentry.runner:run',
        ]
    }
)


from django.core.cache import cache
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    """Command to clear the entire cache."""
    help = 'Clears the cache.'

    def handle(self, *args, **kwargs):
        cache.clear()
        self.stdout.write('Cache has been cleared.', ending="\n")

Add management command for clearing cache

Response:
from django.core.cache import cache
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    """Command to clear the entire cache."""
    help = 'Clears the cache.'

    def handle(self, *args, **kwargs):
        cache.clear()
        self.stdout.write('Cache has been cleared.', ending="\n")

Prompt: Add management command for clearing cache


class CWModel(object):
    def __init__(self, json_dict=None):
        if json_dict is not None:
            self.__dict__.update(json_dict)

    def __repr__(self):
        string = ''
        for k, v in self.__dict__.items():
            string = ''.join([string, '{}: {}\n'.format(k, v)])
        return string

Add super model class via upload

Add super model class via upload

Response:
class CWModel(object):
    def __init__(self, json_dict=None):
        if json_dict is not None:
            self.__dict__.update(json_dict)

    def __repr__(self):
        string = ''
        for k, v in self.__dict__.items():
            string = ''.join([string, '{}: {}\n'.format(k, v)])
        return string

Prompt: Add super model class via upload


from . import STLWriter
def getMetaData():
    return {
        'type': 'mesh_writer',
        'plugin': {
            "name": "STL Writer"
        },
        'mesh_writer': {
            'extension': 'stl',
            'description': 'STL File'
        }
    }

def register(app):
    return STLWriter.STLWriter()

Response:
from . import STLWriter
def getMetaData():
    return {
        'type': 'mesh_writer',
        'plugin': {
            "name": "STL Writer"
        },
        'mesh_writer': {
            'extension': 'stl',
            'description': 'STL File'
        }
    }

def register(app):
    return STLWriter.STLWriter()

Prompt:
Add writer metadata to the STL writer plugin so it can be used in Cura

from . import STLWriter
def getMetaData():
    return {
        'type': 'mesh_writer',
        'plugin': {
            "name": "STL Writer"
        }
    }

def register(app):
    return STLWriter.STLWriter()


import heapq
from operator import itemgetter
from collections import Counter
class Solution(object):
    def topKFrequent(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        c = Counter(nums)
        return map(itemgetter(1), heapq.nlargest(k, [(cnt, n) for n, cnt in c.iteritems()]))

Add py solution for 347. Top K Frequent Elements

347. Top K Frequent Elements: https://leetcode.com/problems/top-k-frequent-elements/

Response:
import heapq
from operator import itemgetter
from collections import Counter
class Solution(object):
    def topKFrequent(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        c = Counter(nums)
        return map(itemgetter(1), heapq.nlargest(k, [(cnt, n) for n, cnt in c.iteritems()]))

Prompt:
Add py solution for 347. Top K Frequent Elements

347. Top K Frequent Elements: https://leetcode.com/problems/top-k-frequent-elements/


import os
import sys
import yaml

import paste.util.multidict

possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
                                   os.pardir,
                                   os.pardir))
if os.path.exists(os.path.join(possible_topdir,
                               'devstack',
                               '__init__.py')):
    sys.path.insert(0, possible_topdir)

from devstack import log
from devstack import utils

class CustomDumper(yaml.SafeDumper):
    def ignore_aliases(self, _data):
        return True

fn = sys.argv[1]
with open(fn, "r") as fh:
    data = fh.read()

b = yaml.load(data)

names = set()
for c in b['components']:
    names.add(c)

idf = 'packages'
pkgs = paste.util.multidict.MultiDict()
for name in names:
    data = b['components'][name]
    #print name
    for p in data.get(idf) or []:
        pname = p['name']
        pkgs.add(pname, p)

common = list()
for pkg in sorted(list(set(pkgs.keys()))):
    items = pkgs.getall(pkg)
    if len(items) > 1:
        print("Package dupe on: %r with %s dups" % (pkg, len(items)))
        versions = set()
        for v in items:
            if v.get('version'):
                versions.add(str(v.get('version')))
        if len(versions) > 1:
            print("\tWith many versions: %s" % (versions))
        else:
            print("\tAll with the same version %s" % (versions))
            common.append(items[0])

idf = 'pips'
pkgs = paste.util.multidict.MultiDict()
for name in names:
    data = b['components'][name]
    for p in data.get(idf) or []:
        pname = p['name']
        pkgs.add(pname, p)

print("-" * 20)
common_pips = list()
for pkg in sorted(list(set(pkgs.keys()))):
    items = pkgs.getall(pkg)
    if len(items) > 1:
        print("Pip dupe on: %r with %s dups" % (pkg, len(items)))
        versions = set()
        for v in items:
            if v.get('version'):
                versions.add(str(v.get('version')))
        if len(versions) > 1:
            print("\tWith many versions: %s" % (versions))
        else:
            print("\tAll with the same version %s" % (versions))
            common_pips.append(items[0])

#data = {'common': {'packages': common, 'pips': common_pips}}
#formatted =  yaml.dump(data,
#                    line_break="\n",
#                    indent=4,
#                    explicit_start=True,
#                    explicit_end=True,
#                    default_flow_style=False,
#                    Dumper=CustomDumper,
#                    )

#print formatted

Add tool that will show the shared packages

Response:
import os
import sys
import yaml

import paste.util.multidict

possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
                                   os.pardir,
                                   os.pardir))
if os.path.exists(os.path.join(possible_topdir,
                               'devstack',
                               '__init__.py')):
    sys.path.insert(0, possible_topdir)

from devstack import log
from devstack import utils

class CustomDumper(yaml.SafeDumper):
    def ignore_aliases(self, _data):
        return True

fn = sys.argv[1]
with open(fn, "r") as fh:
    data = fh.read()

b = yaml.load(data)

names = set()
for c in b['components']:
    names.add(c)

idf = 'packages'
pkgs = paste.util.multidict.MultiDict()
for name in names:
    data = b['components'][name]
    #print name
    for p in data.get(idf) or []:
        pname = p['name']
        pkgs.add(pname, p)

common = list()
for pkg in sorted(list(set(pkgs.keys()))):
    items = pkgs.getall(pkg)
    if len(items) > 1:
        print("Package dupe on: %r with %s dups" % (pkg, len(items)))
        versions = set()
        for v in items:
            if v.get('version'):
                versions.add(str(v.get('version')))
        if len(versions) > 1:
            print("\tWith many versions: %s" % (versions))
        else:
            print("\tAll with the same version %s" % (versions))
            common.append(items[0])

idf = 'pips'
pkgs = paste.util.multidict.MultiDict()
for name in names:
    data = b['components'][name]
    for p in data.get(idf) or []:
        pname = p['name']
        pkgs.add(pname, p)

print("-" * 20)
common_pips = list()
for pkg in sorted(list(set(pkgs.keys()))):
    items = pkgs.getall(pkg)
    if len(items) > 1:
        print("Pip dupe on: %r with %s dups" % (pkg, len(items)))
        versions = set()
        for v in items:
            if v.get('version'):
                versions.add(str(v.get('version')))
        if len(versions) > 1:
            print("\tWith many versions: %s" % (versions))
        else:
            print("\tAll with the same version %s" % (versions))
            common_pips.append(items[0])

#data = {'common': {'packages': common, 'pips': common_pips}}
#formatted =  yaml.dump(data,
#                    line_break="\n",
#                    indent=4,
#                    explicit_start=True,
#                    explicit_end=True,
#                    default_flow_style=False,
#                    Dumper=CustomDumper,
#                    )

#print formatted

Prompt: Add tool that will show the shared packages


from __future__ import unicode_literals

from django.db import migrations

def run_data_migration(apps, schema_editor):
    Attribute = apps.get_model('domain', 'Attribute')
    QuestionSet = apps.get_model('questions', 'QuestionSet')

    questionsets = QuestionSet.objects.filter(is_collection=True)
    for questionset in questionsets:
        if questionset.attribute.key != 'id':
            try:
                questionset.attribute = Attribute.objects.get(parent=questionset.attribute, key='id')
                questionset.save()
            except Attribute.DoesNotExist:
                pass

class Migration(migrations.Migration):

    dependencies = [
        ('questions', '0064_widget_type_choices'),
    ]

    operations = [
        migrations.RunPython(run_data_migration),
    ]

Add data migration to update questionsets

Response:
from __future__ import unicode_literals

from django.db import migrations

def run_data_migration(apps, schema_editor):
    Attribute = apps.get_model('domain', 'Attribute')
    QuestionSet = apps.get_model('questions', 'QuestionSet')

    questionsets = QuestionSet.objects.filter(is_collection=True)
    for questionset in questionsets:
        if questionset.attribute.key != 'id':
            try:
                questionset.attribute = Attribute.objects.get(parent=questionset.attribute, key='id')
                questionset.save()
            except Attribute.DoesNotExist:
                pass

class Migration(migrations.Migration):

    dependencies = [
        ('questions', '0064_widget_type_choices'),
    ]

    operations = [
        migrations.RunPython(run_data_migration),
    ]

Prompt: Add data migration to update questionsets


"""Plotting functions."""

from .time_series import plot_time_series, plot_bursts, plot_instantaneous_measure
from .filt import plot_filter_properties, plot_frequency_response, plot_impulse_response
from .rhythm import plot_swm_pattern, plot_lagged_coherence
from .spectral import plot_power_spectra, plot_scv, plot_scv_rs_lines, plot_scv_rs_matrix, plot_spectral_hist

Response:
"""Plotting functions."""

from .time_series import plot_time_series, plot_bursts, plot_instantaneous_measure
from .filt import plot_filter_properties, plot_frequency_response, plot_impulse_response
from .rhythm import plot_swm_pattern, plot_lagged_coherence
from .spectral import plot_power_spectra, plot_scv, plot_scv_rs_lines, plot_scv_rs_matrix, plot_spectral_hist

Prompt:
Make plot_instantaneous_measure accessible from root of plots

"""Plotting functions."""

from .time_series import plot_time_series, plot_bursts
from .filt import plot_filter_properties, plot_frequency_response, plot_impulse_response
from .rhythm import plot_swm_pattern, plot_lagged_coherence
from .spectral import plot_power_spectra, plot_scv, plot_scv_rs_lines, plot_scv_rs_matrix, plot_spectral_hist


from datetime import date, timedelta

from newswall.models import Story

class ProviderBase(object):
    def __init__(self, source, config):
        self.source = source
        self.config = config

    def update(self):
        raise NotImplementedError

    def create_story(self, object_url, **kwargs):
        defaults = {'source': self.source}
        defaults.update(kwargs)

        if defaults.get('title'):
            if Story.objects.filter(
                    title=defaults.get('title'),
                    timestamp__gte=date.today() - timedelta(days=3),
                    ).exists():
                defaults['is_active'] = False

        return Story.objects.get_or_create(object_url=object_url,
            defaults=defaults)

Response:
from datetime import date, timedelta

from newswall.models import Story

class ProviderBase(object):
    def __init__(self, source, config):
        self.source = source
        self.config = config

    def update(self):
        raise NotImplementedError

    def create_story(self, object_url, **kwargs):
        defaults = {'source': self.source}
        defaults.update(kwargs)

        if defaults.get('title'):
            if Story.objects.filter(
                    title=defaults.get('title'),
                    timestamp__gte=date.today() - timedelta(days=3),
                    ).exists():
                defaults['is_active'] = False

        return Story.objects.get_or_create(object_url=object_url,
            defaults=defaults)

Prompt:
Set stories to inactive if a story with the same title has been published recently

from newswall.models import Story

class ProviderBase(object):
    def __init__(self, source, config):
        self.source = source
        self.config = config

    def update(self):
        raise NotImplementedError

    def create_story(self, object_url, **kwargs):
        defaults = {'source': self.source}
        defaults.update(kwargs)

        return Story.objects.get_or_create(object_url=object_url,
            defaults=defaults)


import flask

import mmstats

application = app = flask.Flask(__name__)
app.config['DEBUG'] = True

class Stats(mmstats.MmStats):
    ok = mmstats.CounterField(label="mmstats.example.ok")
    bad = mmstats.CounterField(label="mmstats.example.bad")
    working = mmstats.BoolField(label="mmstats.example.working")

stats = Stats()

def set_working(sender):
    stats.working = True
flask.request_started.connect(set_working, app)

def unset_working(sender, response):
    stats.working = False
flask.request_finished.connect(unset_working, app)

def inc_response(sender, response):
    if response.status_code == 200:
        stats.ok.inc()
    elif response.status_code == 500:
        stats.bad.inc()
flask.request_finished.connect(inc_response, app)

@app.route('/')
def ok():
    return "OK or see /status for a single process's status"

@app.route('/500')
def bad():
    return 'oh noes!', 500

@app.route('/status')
def status():
    return """\
<html>
    <body>
        <pre>
            ok:      %s
            bad:     %s
            working: %s
        </pre>
    </body>
</html>""" % (stats.ok, stats.bad, stats.working)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5001)

Response:
import flask

import mmstats

application = app = flask.Flask(__name__)
app.config['DEBUG'] = True

class Stats(mmstats.MmStats):
    ok = mmstats.CounterField(label="mmstats.example.ok")
    bad = mmstats.CounterField(label="mmstats.example.bad")
    working = mmstats.BoolField(label="mmstats.example.working")

stats = Stats()

def set_working(sender):
    stats.working = True
flask.request_started.connect(set_working, app)

def unset_working(sender, response):
    stats.working = False
flask.request_finished.connect(unset_working, app)

def inc_response(sender, response):
    if response.status_code == 200:
        stats.ok.inc()
    elif response.status_code == 500:
        stats.bad.inc()
flask.request_finished.connect(inc_response, app)

@app.route('/')
def ok():
    return "OK or see /status for a single process's status"

@app.route('/500')
def bad():
    return 'oh noes!', 500

@app.route('/status')
def status():
    return """\
<html>
    <body>
        <pre>
            ok:      %s
            bad:     %s
            working: %s
        </pre>
    </body>
</html>""" % (stats.ok, stats.bad, stats.working)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5001)

Prompt:
Make the default OK page in the flask example obvious to find

import flask

import mmstats

application = app = flask.Flask(__name__)
app.config['DEBUG'] = True

class Stats(mmstats.MmStats):
    ok = mmstats.CounterField(label="mmstats.example.ok")
    bad = mmstats.CounterField(label="mmstats.example.bad")
    working = mmstats.BoolField(label="mmstats.example.working")

stats = Stats()

def set_working(sender):
    stats.working = True
flask.request_started.connect(set_working, app)

def unset_working(sender, response):
    stats.working = False
flask.request_finished.connect(unset_working, app)

def inc_response(sender, response):
    if response.status_code == 200:
        stats.ok.inc()
    elif response.status_code == 500:
        stats.bad.inc()
flask.request_finished.connect(inc_response, app)

@app.route('/200')
def ok():
    return 'OK'

@app.route('/500')
def bad():
    return 'oh noes!', 500

@app.route('/status')
def status():
    return """\
<html>
    <body>
        <pre>
            ok:      %s
            bad:     %s
            working: %s
        </pre>
    </body>
</html>""" % (stats.ok, stats.bad, stats.working)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5001)


import pickle
from sqlobject import *
from sqlobject.tests.dbtest import *

########################################
## Pickle instances
########################################

class TestPickle(SQLObject):
    question = StringCol()
    answer = IntCol()

test_question = 'The Ulimate Question of Life, the Universe and Everything'
test_answer = 42

def test_pickleCol():
    setupClass(TestPickle)
    connection = TestPickle._connection
    test = TestPickle(question=test_question, answer=test_answer)

    pickle_data = pickle.dumps(test, pickle.HIGHEST_PROTOCOL)
    connection.cache.clear()
    test = pickle.loads(pickle_data)
    test2 = connection.cache.tryGet(test.id, TestPickle)

    assert test2 is test
    assert test.question == test_question
    assert test.answer == test_answer

    if (connection.dbName == 'sqlite') and connection._memory:
        return # The following test requires a different connection

    test = TestPickle.get(
        test.id,
        # make a different DB URI and open another connection
        connection=getConnection(registry=''))
    raises(pickle.PicklingError, pickle.dumps, test, pickle.HIGHEST_PROTOCOL)

Fix flake8 E113 unexpected indentation

Fix flake8 E113 unexpected indentation

Response:
import pickle
from sqlobject import *
from sqlobject.tests.dbtest import *

########################################
## Pickle instances
########################################

class TestPickle(SQLObject):
    question = StringCol()
    answer = IntCol()

test_question = 'The Ulimate Question of Life, the Universe and Everything'
test_answer = 42

def test_pickleCol():
    setupClass(TestPickle)
    connection = TestPickle._connection
    test = TestPickle(question=test_question, answer=test_answer)

    pickle_data = pickle.dumps(test, pickle.HIGHEST_PROTOCOL)
    connection.cache.clear()
    test = pickle.loads(pickle_data)
    test2 = connection.cache.tryGet(test.id, TestPickle)

    assert test2 is test
    assert test.question == test_question
    assert test.answer == test_answer

    if (connection.dbName == 'sqlite') and connection._memory:
        return # The following test requires a different connection

    test = TestPickle.get(
        test.id,
        # make a different DB URI and open another connection
        connection=getConnection(registry=''))
    raises(pickle.PicklingError, pickle.dumps, test, pickle.HIGHEST_PROTOCOL)

Prompt:
Fix flake8 E113 unexpected indentation

import pickle
from sqlobject import *
from sqlobject.tests.dbtest import *

########################################
## Pickle instances
########################################

class TestPickle(SQLObject):
    question = StringCol()
    answer = IntCol()

test_question = 'The Ulimate Question of Life, the Universe and Everything'
test_answer = 42

def test_pickleCol():
    setupClass(TestPickle)
    connection = TestPickle._connection
    test = TestPickle(question=test_question, answer=test_answer)

    pickle_data = pickle.dumps(test, pickle.HIGHEST_PROTOCOL)
    connection.cache.clear()
    test = pickle.loads(pickle_data)
    test2 = connection.cache.tryGet(test.id, TestPickle)

    assert test2 is test
    assert test.question == test_question
    assert test.answer == test_answer

    if (connection.dbName == 'sqlite') and connection._memory:
        return # The following test requires a different connection

    test = TestPickle.get(test.id,
        connection=getConnection(registry='')) # to make a different DB URI
                                               # and open another connection
    raises(pickle.PicklingError, pickle.dumps, test, pickle.HIGHEST_PROTOCOL)


#!/usr/bin/env python
"""
    stack-builder.hiera_config
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    This module will read metadata set during instance
    launch and override any yaml under the /etc/puppet/data
    directory (except data_mappings) that has a key matching
    the metadata
"""

import yaml
import os

hiera_dir = '/etc/puppet/data'
metadata_path = '/root/config.yaml'

#debug
#metadata_path = './sample.json'
#hiera_dir = './openstack-installer/data/'

# Child processes cannot set environment variables, so
# create a bash file to set some exports for facter
def facter_config():
    with open(metadata_path, 'r') as metadata:
        meta = yaml.load(metadata.read())
        print meta
        with open('/root/fact_exports', 'w') as facts:
            for key,value in meta.items():
                # Things with spaces can't be exported
                if ' ' not in value:
                    facts.write('FACTER_' + str(key) + '="' + str(value) + '"\n')

#TODO
def hostname_config():
    with open(metadata_path, 'r') as metadata:
        meta = yaml.load(metadata.read())
        with open('/root/openstack-installer/manifests/setup.pp', 'a') as facts:
            for key,value in meta.items():
                pass

facter_config()

Fix bug dealing with spaces in exports

Fix bug dealing with spaces in exports

Response:
#!/usr/bin/env python
"""
    stack-builder.hiera_config
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    This module will read metadata set during instance
    launch and override any yaml under the /etc/puppet/data
    directory (except data_mappings) that has a key matching
    the metadata
"""

import yaml
import os

hiera_dir = '/etc/puppet/data'
metadata_path = '/root/config.yaml'

#debug
#metadata_path = './sample.json'
#hiera_dir = './openstack-installer/data/'

# Child processes cannot set environment variables, so
# create a bash file to set some exports for facter
def facter_config():
    with open(metadata_path, 'r') as metadata:
        meta = yaml.load(metadata.read())
        print meta
        with open('/root/fact_exports', 'w') as facts:
            for key,value in meta.items():
                # Things with spaces can't be exported
                if ' ' not in value:
                    facts.write('FACTER_' + str(key) + '="' + str(value) + '"\n')

#TODO
def hostname_config():
    with open(metadata_path, 'r') as metadata:
        meta = yaml.load(metadata.read())
        with open('/root/openstack-installer/manifests/setup.pp', 'a') as facts:
            for key,value in meta.items():
                pass

facter_config()

Prompt:
Fix bug dealing with spaces in exports

#!/usr/bin/env python
"""
    stack-builder.hiera_config
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    This module will read metadata set during instance
    launch and override any yaml under the /etc/puppet/data
    directory (except data_mappings) that has a key matching
    the metadata
"""

import yaml
import os

hiera_dir = '/etc/puppet/data'
metadata_path = '/root/config.yaml'

#debug
#metadata_path = './sample.json'
#hiera_dir = './openstack-installer/data/'

# Child processes cannot set environment variables, so
# create a bash file to set some exports for facter
def facter_config():
    with open(metadata_path, 'r') as metadata:
        meta = yaml.load(metadata.read())
        print meta
        with open('/root/fact_exports', 'w') as facts:
            for key,value in meta.items():
                facts.write('FACTER_' + str(key) + '="' + str(value) + '"\n')

#TODO
def hostname_config():
    with open(metadata_path, 'r') as metadata:
        meta = yaml.load(metadata.read())
        with open('/root/openstack-installer/manifests/setup.pp', 'a') as facts:
            for key,value in meta.items():
                pass

facter_config()


from __future__ import unicode_literals, division, absolute_import
import logging

from sqlalchemy import desc, func

from flexget.manager import Session
from flexget.plugin import register_plugin, priority, DependencyError
from flexget.utils.tools import multiply_timedelta
try:
    from flexget.plugins.filter.series import Series, Episode
except ImportError:
    raise DependencyError(issued_by='est_released_series', missing='series plugin', silent=True)

log = logging.getLogger('est_series')

class EstimatesReleasedSeries(object):

    @priority(0)  # Run only if better online lookups fail
    def estimate(self, entry):
        if all(field in entry for field in ['series_name', 'series_season', 'series_episode']):
            session = Session()
            series = session.query(Series).filter(Series.name == entry['series_name']).first()
            if not series:
                return
            episodes = (session.query(Episode).join(Series).
                filter(Episode.season != None).
                filter(Series.id == series.id).
                filter(Episode.season == func.max(Episode.season).select()).
                order_by(desc(Episode.number)).limit(2).all())
            if len(episodes) < 2:
                return
            last_diff = episodes[0].first_seen - episodes[1].first_seen
            return episodes[0].first_seen + multiply_timedelta(last_diff, 0.9)
            # TODO: Some fancier logic? Season break estimates?

register_plugin(EstimatesReleasedSeries, 'est_released_series', groups=['estimate_release'])

Response:
from __future__ import unicode_literals, division, absolute_import
import logging

from sqlalchemy import desc, func

from flexget.manager import Session
from flexget.plugin import register_plugin, priority, DependencyError
from flexget.utils.tools import multiply_timedelta
try:
    from flexget.plugins.filter.series import Series, Episode
except ImportError:
    raise DependencyError(issued_by='est_released_series', missing='series plugin', silent=True)

log = logging.getLogger('est_series')

class EstimatesReleasedSeries(object):

    @priority(0)  # Run only if better online lookups fail
    def estimate(self, entry):
        if all(field in entry for field in ['series_name', 'series_season', 'series_episode']):
            session = Session()
            series = session.query(Series).filter(Series.name == entry['series_name']).first()
            if not series:
                return
            episodes = (session.query(Episode).join(Series).
                filter(Episode.season != None).
                filter(Series.id == series.id).
                filter(Episode.season == func.max(Episode.season).select()).
                order_by(desc(Episode.number)).limit(2).all())
            if len(episodes) < 2:
                return
            last_diff = episodes[0].first_seen - episodes[1].first_seen
            return episodes[0].first_seen + multiply_timedelta(last_diff, 0.9)
            # TODO: Some fancier logic? Season break estimates?

register_plugin(EstimatesReleasedSeries, 'est_released_series', groups=['estimate_release'])

Prompt:
Raise DependencyError when series plugin is missing

from __future__ import unicode_literals, division, absolute_import
import logging

from sqlalchemy import desc, func

from flexget.manager import Session
from flexget.plugin import register_plugin, priority
from flexget.plugins.filter.series import SeriesDatabase, Series, Episode
from flexget.utils.tools import multiply_timedelta

log = logging.getLogger('est_series')

class EstimatesReleasedSeries(SeriesDatabase):

    @priority(0)  # Run only if better online lookups fail
    def estimate(self, entry):
        if all(field in entry for field in ['series_name', 'series_season', 'series_episode']):
            session = Session()
            series = session.query(Series).filter(Series.name == entry['series_name']).first()
            if not series:
                return
            episodes = (session.query(Episode).join(Series).
                filter(Episode.season != None).
                filter(Series.id == series.id).
                filter(Episode.season == func.max(Episode.season).select()).
                order_by(desc(Episode.number)).limit(2).all())
            if len(episodes) < 2:
                return
            last_diff = episodes[0].first_seen - episodes[1].first_seen
            return episodes[0].first_seen + multiply_timedelta(last_diff, 0.9)
            # TODO: Some fancier logic? Season break estimates?

register_plugin(EstimatesReleasedSeries, 'est_released_series', groups=['estimate_release'])


#!/usr/bin/env python

from setuptools import setup

import colors

setup(
    name='ansicolors',
    version=colors.__version__,
    description='ANSI colors for Python',
    long_description=open('README.rst').read(),
    author='Giorgos Verigakis',
    author_email='verigak@gmail.com',
    url='http://github.com/verigak/colors/',
    license='ISC',
    py_modules=['colors'],
    classifiers=[
        'Environment :: Console',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: ISC License (ISCL)',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3'
    ]
)

Response:
#!/usr/bin/env python

from setuptools import setup

import colors

setup(
    name='ansicolors',
    version=colors.__version__,
    description='ANSI colors for Python',
    long_description=open('README.rst').read(),
    author='Giorgos Verigakis',
    author_email='verigak@gmail.com',
    url='http://github.com/verigak/colors/',
    license='ISC',
    py_modules=['colors'],
    classifiers=[
        'Environment :: Console',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: ISC License (ISCL)',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3'
    ]
)

Prompt:
Add requirement for version 2.6 and higher

Fixes #1
#!/usr/bin/env python

from setuptools import setup

import colors

setup(
    name='ansicolors',
    version=colors.__version__,
    description='ANSI colors for Python',
    long_description=open('README.rst').read(),
    author='Giorgos Verigakis',
    author_email='verigak@gmail.com',
    url='http://github.com/verigak/colors/',
    license='ISC',
    py_modules=['colors'],
    classifiers=[
        'Environment :: Console',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: ISC License (ISCL)',
        'Programming Language :: Python'
    ]
)


import numpy as np
from .. import dark_scale

def test_dark_temp_scale():
    scale = dark_scale.dark_temp_scale(-10., -14)
    assert np.allclose(scale, 0.70)

    scale = dark_scale.dark_temp_scale(-10., -14, scale_4c=2.0)
    assert scale == 0.5  # Should be an exact match

Add dark cal scale test

Add dark cal scale test

Response:
import numpy as np
from .. import dark_scale

def test_dark_temp_scale():
    scale = dark_scale.dark_temp_scale(-10., -14)
    assert np.allclose(scale, 0.70)

    scale = dark_scale.dark_temp_scale(-10., -14, scale_4c=2.0)
    assert scale == 0.5  # Should be an exact match

Prompt: Add dark cal scale test


# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Celery configuration values."""

BROKER_URL = "redis://localhost"
BROKER_POOL_LIMIT = 20
BROKER_TRANSPORT_OPTIONS = {
    "visibility_timeout": 60*60*6,
    "fanout_prefix": True,
    "fanout_patterns": True
}
# Use custom json encoder.
CELERY_ACCEPT_CONTENT = ["kjson"]
CELERY_RESULT_SERIALIZER = "kjson"
CELERY_TASK_SERIALIZER = "kjson"
CELERY_TASK_RESULT_EXPIRES = 3600
CELERY_TIMEZONE = "UTC"
CELERY_ENABLE_UTC = True
CELERY_IGNORE_RESULT = True
CELERY_DISABLE_RATE_LIMITS = True
# Use a different DB than the redis default one.
CELERY_RESULT_BACKEND = "redis://localhost/1"

Set celery task results to expire in 1h

Set celery task results to expire in 1h

Response:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Celery configuration values."""

BROKER_URL = "redis://localhost"
BROKER_POOL_LIMIT = 20
BROKER_TRANSPORT_OPTIONS = {
    "visibility_timeout": 60*60*6,
    "fanout_prefix": True,
    "fanout_patterns": True
}
# Use custom json encoder.
CELERY_ACCEPT_CONTENT = ["kjson"]
CELERY_RESULT_SERIALIZER = "kjson"
CELERY_TASK_SERIALIZER = "kjson"
CELERY_TASK_RESULT_EXPIRES = 3600
CELERY_TIMEZONE = "UTC"
CELERY_ENABLE_UTC = True
CELERY_IGNORE_RESULT = True
CELERY_DISABLE_RATE_LIMITS = True
# Use a different DB than the redis default one.
CELERY_RESULT_BACKEND = "redis://localhost/1"

Prompt:
Set celery task results to expire in 1h

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Celery configuration values."""

BROKER_URL = "redis://localhost"
BROKER_POOL_LIMIT = 20
BROKER_TRANSPORT_OPTIONS = {
    "visibility_timeout": 60*60*6,
    "fanout_prefix": True,
    "fanout_patterns": True
}
# Use custom json encoder.
CELERY_ACCEPT_CONTENT = ["kjson"]
CELERY_RESULT_SERIALIZER = "kjson"
CELERY_TASK_SERIALIZER = "kjson"
CELERY_TIMEZONE = "UTC"
CELERY_ENABLE_UTC = True
CELERY_IGNORE_RESULT = True
CELERY_DISABLE_RATE_LIMITS = True
# Use a different DB than the redis default one.
CELERY_RESULT_BACKEND = "redis://localhost/1"


"""This module runs the api server."""
import os
from app import flask_app, db
from app.models import User, BucketList, BucketListItem
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.restful import Resource, Api
from app.api_v1.resources import TestResource, \
    BucketListsApi, BucketListApi, UserLogin, UserRegister

app = flask_app
api = Api(app=app, prefix='/api/v1.0')
manager = Manager(app)
migrate = Migrate(app, db)

# add resources
api.add_resource(TestResource, '/')
api.add_resource(BucketListsApi, '/bucketlists/')
api.add_resource(BucketListApi, '/bucketlists/<id>/')
api.add_resource(UserLogin, '/auth/login/')
api.add_resource(UserRegister, '/auth/register/')

def make_shell_context():
    """Add app, database and models to the shell."""
    return dict(app=app, db=db, User=User, BucketList=BucketList,
                BucketListItem=BucketListItem)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)

@manager.command
def run_tests():
    """Run tests."""
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)

if __name__ == '__main__':
    manager.run()

Response:
"""This module runs the api server."""
import os
from app import flask_app, db
from app.models import User, BucketList, BucketListItem
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.restful import Resource, Api
from app.api_v1.resources import TestResource, \
    BucketListsApi, BucketListApi, UserLogin, UserRegister

app = flask_app
api = Api(app=app, prefix='/api/v1.0')
manager = Manager(app)
migrate = Migrate(app, db)

# add resources
api.add_resource(TestResource, '/')
api.add_resource(BucketListsApi, '/bucketlists/')
api.add_resource(BucketListApi, '/bucketlists/<id>/')
api.add_resource(UserLogin, '/auth/login/')
api.add_resource(UserRegister, '/auth/register/')

def make_shell_context():
    """Add app, database and models to the shell."""
    return dict(app=app, db=db, User=User, BucketList=BucketList,
                BucketListItem=BucketListItem)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)

@manager.command
def run_tests():
    """Run tests."""
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)

if __name__ == '__main__':
    manager.run()

Prompt:
[Feature] Create BucketList & BucketLists endpoints.

"""This module runs the api server."""
import os
from app import flask_app, db
from app.models import User, BucketList, BucketListItem
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.restful import Resource, Api
from app.api_v1.resources import TestResource, \
    BucketListApi, UserLogin, UserRegister

app = flask_app
api = Api(app=app, prefix='/api/v1.0')
manager = Manager(app)
migrate = Migrate(app, db)

# add resources
api.add_resource(TestResource, '/')
api.add_resource(BucketListApi, '/bucketlists/')
api.add_resource(UserLogin, '/auth/login/')
api.add_resource(UserRegister, '/auth/register/')

def make_shell_context():
    """Add app, database and models to the shell."""
    return dict(app=app, db=db, User=User, BucketList=BucketList,
                BucketListItem=BucketListItem)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)

@manager.command
def run_tests():
    """Run tests."""
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)

if __name__ == '__main__':
    manager.run()


import mock

from wheelcms_axle.middleware import FixMessageMiddleware

class TestFixMessageMiddleware(object):
    def test_oldstyle(self):
        """ mock CookieStorage raising IndexError """
        with mock.patch("django.contrib.messages.storage.cookie"
                        ".CookieStorage._decode", side_effect=IndexError):
            request = mock.Mock(COOKIES={'messages': 'dummy'})
            FixMessageMiddleware().process_request(request)
            assert 'messages' not in request.COOKIES

    def test_newstyle(self):
        """ mock ordinary execution """
        with mock.patch("django.contrib.messages.storage.cookie"
                        ".CookieStorage._decode", return_value=None):
            request = mock.Mock(COOKIES={'messages': 'dummy'})
            FixMessageMiddleware().process_request(request)
            assert 'messages' in request.COOKIES

Add middleware, if not for keeping the coverage up :)

Response:
import mock

from wheelcms_axle.middleware import FixMessageMiddleware

class TestFixMessageMiddleware(object):
    def test_oldstyle(self):
        """ mock CookieStorage raising IndexError """
        with mock.patch("django.contrib.messages.storage.cookie"
                        ".CookieStorage._decode", side_effect=IndexError):
            request = mock.Mock(COOKIES={'messages': 'dummy'})
            FixMessageMiddleware().process_request(request)
            assert 'messages' not in request.COOKIES

    def test_newstyle(self):
        """ mock ordinary execution """
        with mock.patch("django.contrib.messages.storage.cookie"
                        ".CookieStorage._decode", return_value=None):
            request = mock.Mock(COOKIES={'messages': 'dummy'})
            FixMessageMiddleware().process_request(request)
            assert 'messages' in request.COOKIES

Prompt: Add middleware, if not for keeping the coverage up :)


import datetime
import logging

from django.core.management.base import BaseCommand

import mysite.profile.tasks
import mysite.search.models
import mysite.search.tasks

## FIXME: Move to a search management command?
def periodically_check_if_bug_epoch_eclipsed_the_cached_search_epoch():
    logging.info("Checking if bug epoch eclipsed the cached search epoch")
    cache_time = mysite.search.models.Epoch.get_for_string('search_cache')
    bug_time = mysite.search.models.Epoch.get_for_string('search_cache')
    if cache_time < bug_time:
        mysite.search.tasks.clear_search_cache()
        mysite.search.models.Epoch.bump_for_string('search_cache')
    logging.info("Finished dealing with bug epoch vs. cached search epoch.")

class Command(BaseCommand):
    help = "Run this once hourly for the OpenHatch profile app."

    def handle(self, *args, **options):
        mysite.profile.tasks.sync_bug_epoch_from_model_then_fill_recommended_bugs_cache()

        # Every 4 hours, clear search cache
        if (datetime.datetime.utcnow().hour % 4) == 0:
            periodically_check_if_bug_epoch_eclipsed_the_cached_search_epoch()

Response:
import datetime
import logging

from django.core.management.base import BaseCommand

import mysite.profile.tasks
import mysite.search.models
import mysite.search.tasks

## FIXME: Move to a search management command?
def periodically_check_if_bug_epoch_eclipsed_the_cached_search_epoch():
    logging.info("Checking if bug epoch eclipsed the cached search epoch")
    cache_time = mysite.search.models.Epoch.get_for_string('search_cache')
    bug_time = mysite.search.models.Epoch.get_for_string('search_cache')
    if cache_time < bug_time:
        mysite.search.tasks.clear_search_cache()
        mysite.search.models.Epoch.bump_for_string('search_cache')
    logging.info("Finished dealing with bug epoch vs. cached search epoch.")

class Command(BaseCommand):
    help = "Run this once hourly for the OpenHatch profile app."

    def handle(self, *args, **options):
        mysite.profile.tasks.sync_bug_epoch_from_model_then_fill_recommended_bugs_cache()

        # Every 4 hours, clear search cache
        if (datetime.datetime.utcnow().hour % 4) == 0:
            periodically_check_if_bug_epoch_eclipsed_the_cached_search_epoch()

Prompt:
Remove apparently superfluous call to fill_recommended_bugs_cache.

import datetime
import logging

from django.core.management.base import BaseCommand

import mysite.profile.tasks
import mysite.search.models
import mysite.search.tasks

## FIXME: Move to a search management command?
def periodically_check_if_bug_epoch_eclipsed_the_cached_search_epoch():
    logging.info("Checking if bug epoch eclipsed the cached search epoch")
    cache_time = mysite.search.models.Epoch.get_for_string('search_cache')
    bug_time = mysite.search.models.Epoch.get_for_string('search_cache')
    if cache_time < bug_time:
        mysite.search.tasks.clear_search_cache()
        mysite.search.models.Epoch.bump_for_string('search_cache')
    logging.info("Finished dealing with bug epoch vs. cached search epoch.")

class Command(BaseCommand):
    help = "Run this once hourly for the OpenHatch profile app."

    def handle(self, *args, **options):
        mysite.profile.tasks.sync_bug_epoch_from_model_then_fill_recommended_bugs_cache()
        mysite.profile.tasks.fill_recommended_bugs_cache()

        # Every 4 hours, clear search cache
        if (datetime.datetime.utcnow().hour % 4) == 0:
            periodically_check_if_bug_epoch_eclipsed_the_cached_search_epoch()


import pathlib

from mopidy import config, exceptions, ext

__version__ = "2.0.0"

class Extension(ext.Extension):

    dist_name = "Mopidy-MPRIS"
    ext_name = "mpris"
    version = __version__

    def get_default_config(self):
        return config.read(pathlib.Path(__file__).parent / "ext.conf")

    def get_config_schema(self):
        schema = super().get_config_schema()
        schema["desktop_file"] = config.Deprecated()
        schema["bus_type"] = config.String(choices=["session", "system"])
        return schema

    def validate_environment(self):
        try:
            import pydbus  # noqa
        except ImportError as e:
            raise exceptions.ExtensionError("pydbus library not found", e)

    def setup(self, registry):
        from .frontend import MprisFrontend

        registry.add("frontend", MprisFrontend)

Use pathlib to read ext.conf

Use pathlib to read ext.conf

Response:
import pathlib

from mopidy import config, exceptions, ext

__version__ = "2.0.0"

class Extension(ext.Extension):

    dist_name = "Mopidy-MPRIS"
    ext_name = "mpris"
    version = __version__

    def get_default_config(self):
        return config.read(pathlib.Path(__file__).parent / "ext.conf")

    def get_config_schema(self):
        schema = super().get_config_schema()
        schema["desktop_file"] = config.Deprecated()
        schema["bus_type"] = config.String(choices=["session", "system"])
        return schema

    def validate_environment(self):
        try:
            import pydbus  # noqa
        except ImportError as e:
            raise exceptions.ExtensionError("pydbus library not found", e)

    def setup(self, registry):
        from .frontend import MprisFrontend

        registry.add("frontend", MprisFrontend)

Prompt:
Use pathlib to read ext.conf

import os

from mopidy import config, exceptions, ext

__version__ = "2.0.0"

class Extension(ext.Extension):

    dist_name = "Mopidy-MPRIS"
    ext_name = "mpris"
    version = __version__

    def get_default_config(self):
        conf_file = os.path.join(os.path.dirname(__file__), "ext.conf")
        return config.read(conf_file)

    def get_config_schema(self):
        schema = super().get_config_schema()
        schema["desktop_file"] = config.Deprecated()
        schema["bus_type"] = config.String(choices=["session", "system"])
        return schema

    def validate_environment(self):
        try:
            import pydbus  # noqa
        except ImportError as e:
            raise exceptions.ExtensionError("pydbus library not found", e)

    def setup(self, registry):
        from .frontend import MprisFrontend

        registry.add("frontend", MprisFrontend)


# Configuration file for Jupyter-notebook.
# https://github.com/jupyter/docker-demo-images/blob/master/resources/jupyter_notebook_config.partial.py

c = get_config()
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888 #9999

# Whether to trust or not X-Scheme/X-Forwarded-Proto and X-Real-Ip/X-Forwarded-
# For headerssent by the upstream reverse proxy. Necessary if the proxy handles
# SSL
c.NotebookApp.trust_xheaders = True

# Include our extra templates
c.NotebookApp.extra_template_paths = ['/srv/templates/']

# Supply overrides for the tornado.web.Application that the IPython notebook
# uses.
#c.NotebookApp.tornado_settings = {
#    'headers': {
#        'X-Frame-Options': 'ALLOW FROM nature.com'
#    },
#    'template_path':['/srv/ga/', '/srv/ipython/IPython/html',
#                     '/srv/ipython/IPython/html/templates']
#}

#c.Spawner.env_keep = ['PATH', 'PYTHONPATH', 'CONDA_ROOT', 'CONDA_DEFAULT_ENV', 'VIRTUAL_ENV', 'LANG', 'LC_ALL', 'NRN_NMODL_PATH']

# http://www.harrisgeospatial.com/Support/HelpArticlesDetail/TabId/219/ArtMID/900/ArticleID/14776/Integrating-the-Jupyter-Notebook-with-ESE.aspx
# We need to create an exception in the Jupyter Notebook security that will allow the Jupyter web page to be embedded in an HTML iframe
c.NotebookApp.tornado_settings = {
   'headers': {
       'Content-Security-Policy': "frame-ancestors 'self' http://*.projectpyrho.org "
   }
   #'static_url_prefix': 'https://cdn.jupyter.org/notebook/try-4.0.5/'
}
#'http://yourhostname:9191/Jupyter/'

Add config file TODO: edit to work with IFrames

Response:
# Configuration file for Jupyter-notebook.
# https://github.com/jupyter/docker-demo-images/blob/master/resources/jupyter_notebook_config.partial.py

c = get_config()
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888 #9999

# Whether to trust or not X-Scheme/X-Forwarded-Proto and X-Real-Ip/X-Forwarded-
# For headerssent by the upstream reverse proxy. Necessary if the proxy handles
# SSL
c.NotebookApp.trust_xheaders = True

# Include our extra templates
c.NotebookApp.extra_template_paths = ['/srv/templates/']

# Supply overrides for the tornado.web.Application that the IPython notebook
# uses.
#c.NotebookApp.tornado_settings = {
#    'headers': {
#        'X-Frame-Options': 'ALLOW FROM nature.com'
#    },
#    'template_path':['/srv/ga/', '/srv/ipython/IPython/html',
#                     '/srv/ipython/IPython/html/templates']
#}

#c.Spawner.env_keep = ['PATH', 'PYTHONPATH', 'CONDA_ROOT', 'CONDA_DEFAULT_ENV', 'VIRTUAL_ENV', 'LANG', 'LC_ALL', 'NRN_NMODL_PATH']

# http://www.harrisgeospatial.com/Support/HelpArticlesDetail/TabId/219/ArtMID/900/ArticleID/14776/Integrating-the-Jupyter-Notebook-with-ESE.aspx
# We need to create an exception in the Jupyter Notebook security that will allow the Jupyter web page to be embedded in an HTML iframe
c.NotebookApp.tornado_settings = {
   'headers': {
       'Content-Security-Policy': "frame-ancestors 'self' http://*.projectpyrho.org "
   }
   #'static_url_prefix': 'https://cdn.jupyter.org/notebook/try-4.0.5/'
}
#'http://yourhostname:9191/Jupyter/'

Prompt: Add config file TODO: edit to work with IFrames


"""
Python testing for humans.

Usage: pspec [<path>...]

Options:
  -h --help    show this
"""

from attest.hook import AssertImportHook
from docopt import docopt
import os
import sys
from .collectors import PSpecTests

def main():
    # When run as a console script (i.e. ``pspec``), the CWD isn't
    # ``sys.path[0]``, but it should be.
    cwd = os.getcwd()
    if sys.path[0] not in ('', cwd):
        sys.path.insert(0, cwd)

    arguments = docopt(__doc__)
    paths = arguments['<path>']
    if not paths:
        paths = [name for name in os.listdir('.')
                 if os.path.isfile('%s/__init__.py' % name)]
    with AssertImportHook():
        tests = PSpecTests(paths)

    tests.run()

if __name__ == '__main__':
    main()

Put CWD at start of sys.path

Put CWD at start of sys.path

Response:
"""
Python testing for humans.

Usage: pspec [<path>...]

Options:
  -h --help    show this
"""

from attest.hook import AssertImportHook
from docopt import docopt
import os
import sys
from .collectors import PSpecTests

def main():
    # When run as a console script (i.e. ``pspec``), the CWD isn't
    # ``sys.path[0]``, but it should be.
    cwd = os.getcwd()
    if sys.path[0] not in ('', cwd):
        sys.path.insert(0, cwd)

    arguments = docopt(__doc__)
    paths = arguments['<path>']
    if not paths:
        paths = [name for name in os.listdir('.')
                 if os.path.isfile('%s/__init__.py' % name)]
    with AssertImportHook():
        tests = PSpecTests(paths)

    tests.run()

if __name__ == '__main__':
    main()

Prompt:
Put CWD at start of sys.path

"""
Python testing for humans.

Usage: pspec [<path>...]

Options:
  -h --help    show this
"""

from attest.hook import AssertImportHook
from docopt import docopt
import os
import sys
from .collectors import PSpecTests

def main():
    arguments = docopt(__doc__)
    paths = arguments['<path>']
    if not paths:
        paths = [name for name in os.listdir('.')
                 if os.path.isfile('%s/__init__.py' % name)]
    with AssertImportHook():
        tests = PSpecTests(paths)

    tests.run()

if __name__ == '__main__':
    main()


{
    'bslmf_addreference': [ {'OS': 'Windows'} ],
    'bslstl_iteratorutil': [ {'OS': 'SunOS'} ],
    'bslstl_unorderedmultiset': [ {'OS': 'SunOS'} ],
    'bsls_atomic' : [
        {'case': 7, 'HOST': 'VM', 'policy': 'skip' },
        {'case': 8, 'HOST': 'VM', 'policy': 'skip' },
    ],
    'bsls_stopwatch' : [ {'case': 6, 'HOST': 'VM', 'policy': 'skip' } ],
}

Response:
{
    'bslmf_addreference': [ {'OS': 'Windows'} ],
    'bslstl_iteratorutil': [ {'OS': 'SunOS'} ],
    'bslstl_unorderedmultiset': [ {'OS': 'SunOS'} ],
    'bsls_atomic' : [
        {'case': 7, 'HOST': 'VM', 'policy': 'skip' },
        {'case': 8, 'HOST': 'VM', 'policy': 'skip' },
    ],
    'bsls_stopwatch' : [ {'case': 6, 'HOST': 'VM', 'policy': 'skip' } ],
}

Prompt:
Remove exception for bslalg_constructorproxy test driver on AIX shared library builds.

{
    'bslmf_addreference': [ {'OS': 'Windows'} ],
    'bslstl_iteratorutil': [ {'OS': 'SunOS'} ],
    'bslstl_unorderedmultiset': [ {'OS': 'SunOS'} ],
    'bslalg_constructorproxy': [ {'OS': 'AIX', 'library': 'shared_library'} ],
    'bsls_atomic' : [
        {'case': 7, 'HOST': 'VM', 'policy': 'skip' },
        {'case': 8, 'HOST': 'VM', 'policy': 'skip' },
    ],
    'bsls_stopwatch' : [ {'case': 6, 'HOST': 'VM', 'policy': 'skip' } ],
}


from __future__ import division
import os
import libtbx.load_env
from libtbx import easy_run

have_dials_regression = libtbx.env.has_module("dials_regression")
if have_dials_regression:
  dials_regression = libtbx.env.find_in_repositories(
    relative_path="dials_regression",
    test=os.path.isdir)

def exercise_hot_pixel_mask_to_xy():
  if not have_dials_regression:
    print "Skipping exercise_hot_pixel_mask_to_xy(): dials_regression not available."
    return

  data_file = os.path.join(dials_regression, "i23", "hot_pixel_mask",
                           "hot_mask_0.pickle")

  commands = ["dev.dials.hot_pixel_mask_to_xy", data_file]
  command = " ".join(commands)
  result = easy_run.fully_buffered(command=command).raise_if_errors()

  return

def run():
  exercise_hot_pixel_mask_to_xy()
  return

if __name__ == '__main__':
  from dials.test import cd_auto
  with cd_auto(__file__):
    run()
    print "OK"

Test for jiffy to get hot pixel coordinates as x, y

Response:
from __future__ import division
import os
import libtbx.load_env
from libtbx import easy_run

have_dials_regression = libtbx.env.has_module("dials_regression")
if have_dials_regression:
  dials_regression = libtbx.env.find_in_repositories(
    relative_path="dials_regression",
    test=os.path.isdir)

def exercise_hot_pixel_mask_to_xy():
  if not have_dials_regression:
    print "Skipping exercise_hot_pixel_mask_to_xy(): dials_regression not available."
    return

  data_file = os.path.join(dials_regression, "i23", "hot_pixel_mask",
                           "hot_mask_0.pickle")

  commands = ["dev.dials.hot_pixel_mask_to_xy", data_file]
  command = " ".join(commands)
  result = easy_run.fully_buffered(command=command).raise_if_errors()

  return

def run():
  exercise_hot_pixel_mask_to_xy()
  return

if __name__ == '__main__':
  from dials.test import cd_auto
  with cd_auto(__file__):
    run()
    print "OK"

Prompt: Test for jiffy to get hot pixel coordinates as x, y


'''
Application from a .kv
======================

The root application is created from the corresponding .kv. Check the test.kv
file to see what will be the root widget.

This example shows how you can change the directory where the .kv lives.
'''

import kivy
kivy.require('1.0.7')

from kivy.app import App

class TestApp(App):
    kv_directory = 'template1'

if __name__ == '__main__':
    TestApp().run()

Response:
'''
Application from a .kv
======================

The root application is created from the corresponding .kv. Check the test.kv
file to see what will be the root widget.

This example shows how you can change the directory where the .kv lives.
'''

import kivy
kivy.require('1.0.7')

from kivy.app import App

class TestApp(App):
    kv_directory = 'template1'

if __name__ == '__main__':
    TestApp().run()

Prompt:
Correct description comment, add a line break
'''
Application from a .kv
======================

The root application is created from the corresponding .kv. Check the test.kv
file to see what will be the root widget.
This example show how you can change the directory where the .kv live.
'''

import kivy
kivy.require('1.0.7')

from kivy.app import App

class TestApp(App):
    kv_directory = 'template1'

if __name__ == '__main__':
    TestApp().run()


import sys
sys.path.append('../..')

from SALib.analyze import rbd_fast
from SALib.sample import latin
from SALib.test_functions import Ishigami
from SALib.util import read_param_file

# Read the parameter range file and generate samples
problem = read_param_file('../../src/SALib/test_functions/params/Ishigami.txt')

# Generate samples
param_values = latin.sample(problem, 1000)

# Run the "model" and save the output in a text file
# This will happen offline for external models
Y = Ishigami.evaluate(param_values)

# Perform the sensitivity analysis using the model output
# Specify which column of the output file to analyze (zero-indexed)
Si = rbd_fast.analyze(problem, param_values, Y, print_to_console=True)
# Returns a dictionary with key 'S1'
# e.g. Si['S1'] contains the first-order index for each parameter, in the
# same order as the parameter file

Response:
import sys
sys.path.append('../..')

from SALib.analyze import rbd_fast
from SALib.sample import latin
from SALib.test_functions import Ishigami
from SALib.util import read_param_file

# Read the parameter range file and generate samples
problem = read_param_file('../../src/SALib/test_functions/params/Ishigami.txt')

# Generate samples
param_values = latin.sample(problem, 1000)

# Run the "model" and save the output in a text file
# This will happen offline for external models
Y = Ishigami.evaluate(param_values)

# Perform the sensitivity analysis using the model output
# Specify which column of the output file to analyze (zero-indexed)
Si = rbd_fast.analyze(problem, param_values, Y, print_to_console=True)
# Returns a dictionary with key 'S1'
# e.g. Si['S1'] contains the first-order index for each parameter, in the
# same order as the parameter file

Prompt:
Fix incorrect description of returned dict entries

import sys
sys.path.append('../..')

from SALib.analyze import rbd_fast
from SALib.sample import latin
from SALib.test_functions import Ishigami
from SALib.util import read_param_file

# Read the parameter range file and generate samples
problem = read_param_file('../../src/SALib/test_functions/params/Ishigami.txt')

# Generate samples
param_values = latin.sample(problem, 1000)

# Run the "model" and save the output in a text file
# This will happen offline for external models
Y = Ishigami.evaluate(param_values)

# Perform the sensitivity analysis using the model output
# Specify which column of the output file to analyze (zero-indexed)
Si = rbd_fast.analyze(problem, param_values, Y, print_to_console=True)
# Returns a dictionary with keys 'S1' and 'ST'
# e.g. Si['S1'] contains the first-order index for each parameter, in the
# same order as the parameter file


# -*- coding: utf-8 -*-
# © 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{

    'name': 'Medical Medicament - US Locale',
    'version': '10.0.1.0.0',
    'author': "LasLabs, Odoo Community Association (OCA)",
    'category': 'Medical',
    'depends': [
        'medical_base_us',
        'medical_medication',
        'medical_manufacturer',
    ],
    'website': 'https://laslabs.com',
    'license': 'AGPL-3',
    'data': [
        'views/medical_medicament_view.xml',
        'security/ir.model.access.csv',
    ],
    'installable': True,
    'auto_install': True,
}

Response:
# -*- coding: utf-8 -*-
# © 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{

    'name': 'Medical Medicament - US Locale',
    'version': '10.0.1.0.0',
    'author': "LasLabs, Odoo Community Association (OCA)",
    'category': 'Medical',
    'depends': [
        'medical_base_us',
        'medical_medication',
        'medical_manufacturer',
    ],
    'website': 'https://laslabs.com',
    'license': 'AGPL-3',
    'data': [
        'views/medical_medicament_view.xml',
        'security/ir.model.access.csv',
    ],
    'installable': True,
    'auto_install': True,
}

Prompt:
[FIX] medical_medicament_us: Add missing dependency
* medical.medicament.ndc relates to medical.manufacturer, which does not exist without it defined as a dependency.

# -*- coding: utf-8 -*-
# © 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{

    'name': 'Medical Medicament - US Locale',
    'version': '10.0.1.0.0',
    'author': "LasLabs, Odoo Community Association (OCA)",
    'category': 'Medical',
    'depends': [
        'medical_base_us',
        'medical_medication',
    ],
    'website': 'https://laslabs.com',
    'license': 'AGPL-3',
    'data': [
        'views/medical_medicament_view.xml',
        'security/ir.model.access.csv',
    ],
    'installable': True,
    'auto_install': True,
}


from itertools import chain

def threes_and_fives_gen(num=1000):
    for i in range(num):
        if i % 3 == 0 or i % 5 == 0:
            yield i

def threes_and_fives_fun(n):
    return set(chain(range(3, n+1, 3), range(5, n+1, 5)))

def solve(n):
    return sum(
        filter(lambda x: x%3==0 or x%5==0,
               range(1, n)
        )
    )

def solve_2(n):
    return sum(
            x
            for x in range(1, n)
            if x%3==0 or x%5==0
    )

if __name__ == '__main__':
    print(solve_2(1000000))

Add gen exp solution to 1

Add gen exp solution to 1

Response:
from itertools import chain

def threes_and_fives_gen(num=1000):
    for i in range(num):
        if i % 3 == 0 or i % 5 == 0:
            yield i

def threes_and_fives_fun(n):
    return set(chain(range(3, n+1, 3), range(5, n+1, 5)))

def solve(n):
    return sum(
        filter(lambda x: x%3==0 or x%5==0,
               range(1, n)
        )
    )

def solve_2(n):
    return sum(
            x
            for x in range(1, n)
            if x%3==0 or x%5==0
    )

if __name__ == '__main__':
    print(solve_2(1000000))

Prompt:
Add gen exp solution to 1

from itertools import chain

def threes_and_fives_gen(num=1000):
    for i in range(num):
        if i % 3 == 0 or i % 5 == 0:
            yield i

def threes_and_fives_fun(n):
    return set(chain(range(3, n+1, 3), range(5, n+1, 5)))

def solve(n):
    return sum(
        filter(lambda x: x%3==0 or x%5==0,
               range(1, n)
        )
    )

if __name__ == '__main__':
    print(sum(three_and_fives_gen(10000000)))


# -*- coding: utf-8 -*-
import RPi.GPIO as gpio

pin_power=3
pin_light=5

# Setup
gpio.setmode(gpio.BOARD)
gpio.setup(pin_power, gpio.OUT)
gpio.setup(pin_light, gpio.OUT)
gpio.output(pin_power, gpio.HIGH)
gpio.output(pin_light, gpio.LOW)

Response:
# -*- coding: utf-8 -*-
import RPi.GPIO as gpio

pin_power=3
pin_light=5

# Setup
gpio.setmode(gpio.BOARD)
gpio.setup(pin_power, gpio.OUT)
gpio.setup(pin_light, gpio.OUT)
gpio.output(pin_power, gpio.HIGH)
gpio.output(pin_light, gpio.LOW)

Prompt:
Modify the pin number due to the hardware case

# -*- coding: utf-8 -*-
import RPi.GPIO as gpio

pin_power=12
pin_light=16

# Setup
gpio.setmode(gpio.BOARD)
gpio.setup(pin_power, gpio.OUT)
gpio.setup(pin_light, gpio.OUT)
gpio.output(pin_power, gpio.HIGH)
gpio.output(pin_light, gpio.LOW)


# -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2016, Shoop Ltd. All rights reserved.
#
# This source code is licensed under the AGPLv3 license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import unicode_literals

from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from shuup.admin.toolbar import Toolbar
from shuup.admin.utils.picotable import ChoicesFilter, Column, TextFilter
from shuup.admin.utils.views import PicotableListView
from shuup.core.models import Shop, ShopStatus

class ShopListView(PicotableListView):
    model = Shop
    default_columns = [
        Column("name", _(u"Name"), sort_field="translations__name", display="name", filter_config=TextFilter(
            filter_field="translations__name",
            placeholder=_("Filter by name...")
        )),
        Column("domain", _(u"Domain")),
        Column("identifier", _(u"Identifier")),
        Column("status", _(u"Status"), filter_config=ChoicesFilter(choices=ShopStatus.choices)),
    ]

    def get_toolbar(self):
        if settings.SHUUP_ENABLE_MULTIPLE_SHOPS:
            return super(ShopListView, self).get_toolbar()
        else:
            return Toolbar([])

Modify shops for dynamic columns

Response:
# -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2016, Shoop Ltd. All rights reserved.
#
# This source code is licensed under the AGPLv3 license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import unicode_literals

from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from shuup.admin.toolbar import Toolbar
from shuup.admin.utils.picotable import ChoicesFilter, Column, TextFilter
from shuup.admin.utils.views import PicotableListView
from shuup.core.models import Shop, ShopStatus

class ShopListView(PicotableListView):
    model = Shop
    default_columns = [
        Column("name", _(u"Name"), sort_field="translations__name", display="name", filter_config=TextFilter(
            filter_field="translations__name",
            placeholder=_("Filter by name...")
        )),
        Column("domain", _(u"Domain")),
        Column("identifier", _(u"Identifier")),
        Column("status", _(u"Status"), filter_config=ChoicesFilter(choices=ShopStatus.choices)),
    ]

    def get_toolbar(self):
        if settings.SHUUP_ENABLE_MULTIPLE_SHOPS:
            return super(ShopListView, self).get_toolbar()
        else:
            return Toolbar([])

Prompt:
Modify shops for dynamic columns

Refs SH-64

# -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2016, Shoop Ltd. All rights reserved.
#
# This source code is licensed under the AGPLv3 license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import unicode_literals

from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from shuup.admin.toolbar import Toolbar
from shuup.admin.utils.picotable import ChoicesFilter, Column, TextFilter
from shuup.admin.utils.views import PicotableListView
from shuup.core.models import Shop, ShopStatus

class ShopListView(PicotableListView):
    model = Shop
    columns = [
        Column("name", _(u"Name"), sort_field="translations__name", display="name", filter_config=TextFilter(
            filter_field="translations__name",
            placeholder=_("Filter by name...")
        )),
        Column("domain", _(u"Domain")),
        Column("identifier", _(u"Identifier")),
        Column("status", _(u"Status"), filter_config=ChoicesFilter(choices=ShopStatus.choices)),
    ]

    def get_toolbar(self):
        if settings.SHUUP_ENABLE_MULTIPLE_SHOPS:
            return super(ShopListView, self).get_toolbar()
        else:
            return Toolbar([])


from setuptools import setup

from setuptools.command.test import test as TestCommand

class PyTest(TestCommand):
    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        #import here, cause outside the eggs aren't loaded
        import pytest
        errno = pytest.main(self.test_args)
        sys.exit(errno)

def readme():
    with open('README.pandoc') as f:
        return f.read()

setup( name             = 'pyop'
     , version          = '0.0.1'
     , description      = 'Matrix free linear transformations'
     , long_description = readme()
     , keywords         = [ 'Linear Algebra', 'Linear Transformations']
     , author           = 'Daniel Hensley and Ryan Orendorff'
     , author_email     = 'ryan@rdodesigns.com'
     , license          = 'BSD'
     , classifiers      =
        [ 'Development Status :: 1 - Planning'
        , 'Intended Audience :: Science/Research'
        , 'License :: OSI Approved :: BSD License'
        , 'Programming Language :: Python'
        , 'Topic :: Scientific/Engineering :: Mathematics'
        , 'Topic :: Software Development :: Libraries :: Python Modules'
        ]
     , packages         = ['pyop']
     , install_requires =
        [ 'six >= 1.6'
        , 'numpy >= 1.8'
        , 'scipy >= 0.14.0'
        ]
     , zip_safe         = False
     , tests_require    = ['pytest']
     , cmdclass         = {'test': PyTest}
     )

Install requirements now include SciPy.

Response:
from setuptools import setup

from setuptools.command.test import test as TestCommand

class PyTest(TestCommand):
    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        #import here, cause outside the eggs aren't loaded
        import pytest
        errno = pytest.main(self.test_args)
        sys.exit(errno)

def readme():
    with open('README.pandoc') as f:
        return f.read()

setup( name             = 'pyop'
     , version          = '0.0.1'
     , description      = 'Matrix free linear transformations'
     , long_description = readme()
     , keywords         = [ 'Linear Algebra', 'Linear Transformations']
     , author           = 'Daniel Hensley and Ryan Orendorff'
     , author_email     = 'ryan@rdodesigns.com'
     , license          = 'BSD'
     , classifiers      =
        [ 'Development Status :: 1 - Planning'
        , 'Intended Audience :: Science/Research'
        , 'License :: OSI Approved :: BSD License'
        , 'Programming Language :: Python'
        , 'Topic :: Scientific/Engineering :: Mathematics'
        , 'Topic :: Software Development :: Libraries :: Python Modules'
        ]
     , packages         = ['pyop']
     , install_requires =
        [ 'six >= 1.6'
        , 'numpy >= 1.8'
        , 'scipy >= 0.14.0'
        ]
     , zip_safe         = False
     , tests_require    = ['pytest']
     , cmdclass         = {'test': PyTest}
     )

Prompt:
Install requirements now include SciPy.

Used in the operators subpackage, and will likely be used elsewhere due
to the sparse package being inside scipy.

from setuptools import setup

from setuptools.command.test import test as TestCommand

class PyTest(TestCommand):
    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        #import here, cause outside the eggs aren't loaded
        import pytest
        errno = pytest.main(self.test_args)
        sys.exit(errno)

def readme():
    with open('README.pandoc') as f:
        return f.read()

setup( name             = 'pyop'
     , version          = '0.0.1'
     , description      = 'Matrix free linear transformations'
     , long_description = readme()
     , keywords         = [ 'Linear Algebra', 'Linear Transformations']
     , author           = 'Daniel Hensley and Ryan Orendorff'
     , author_email     = 'ryan@rdodesigns.com'
     , license          = 'BSD'
     , classifiers      =
        [ 'Development Status :: 1 - Planning'
        , 'Intended Audience :: Science/Research'
        , 'License :: OSI Approved :: BSD License'
        , 'Programming Language :: Python'
        , 'Topic :: Scientific/Engineering :: Mathematics'
        , 'Topic :: Software Development :: Libraries :: Python Modules'
        ]
     , packages         = ['pyop']
     , install_requires = ['six >= 1.6', 'numpy >= 1.8']
     , zip_safe         = False
     , tests_require    = ['pytest']
     , cmdclass         = {'test': PyTest}
     )


import json

from twisted.trial.unittest import TestCase
from twisted.internet.defer import inlineCallbacks

from vumi_http_retry.tests.redis import create_client, zitems

class TestRedis(TestCase):
    @inlineCallbacks
    def setUp(self):
        self.redis = yield create_client()

    @inlineCallbacks
    def tearDown(self):
        yield self.redis.delete('foo')
        yield self.redis.transport.loseConnection()

    @inlineCallbacks
    def test_add_request(self):
        self.assertEqual((yield zitems(self.redis, 'foo')), [])

        yield self.redis.zadd('foo', 1, json.dumps({'bar': 23}))

        self.assertEqual((yield zitems(self.redis, 'foo')), [
            (1, {'bar': 23}),
        ])

        yield self.redis.zadd('foo', 2, json.dumps({'baz': 42}))

        self.assertEqual((yield zitems(self.redis, 'foo')), [
            (1, {'bar': 23}),
            (2, {'baz': 42}),
        ])

Add test for redis test helper

Add test for redis test helper

Response:
import json

from twisted.trial.unittest import TestCase
from twisted.internet.defer import inlineCallbacks

from vumi_http_retry.tests.redis import create_client, zitems

class TestRedis(TestCase):
    @inlineCallbacks
    def setUp(self):
        self.redis = yield create_client()

    @inlineCallbacks
    def tearDown(self):
        yield self.redis.delete('foo')
        yield self.redis.transport.loseConnection()

    @inlineCallbacks
    def test_add_request(self):
        self.assertEqual((yield zitems(self.redis, 'foo')), [])

        yield self.redis.zadd('foo', 1, json.dumps({'bar': 23}))

        self.assertEqual((yield zitems(self.redis, 'foo')), [
            (1, {'bar': 23}),
        ])

        yield self.redis.zadd('foo', 2, json.dumps({'baz': 42}))

        self.assertEqual((yield zitems(self.redis, 'foo')), [
            (1, {'bar': 23}),
            (2, {'baz': 42}),
        ])

Prompt: Add test for redis test helper


import sys

def format_cols(cols):
    widths = [0] * len(cols[0])

    for i in cols:
        for idx, val in enumerate(i):
            widths[idx] = max(len(val), widths[idx])

    f = ""

    t = []
    for i in widths:
        t.append("%%-0%ds" % (i,))

    return "    ".join(t)

def column_report(title, fields, cols):
    l = []
    l.append("[" + title + "]")
    l.append("")

    f = format_cols([fields] + cols)

    header = f % tuple(fields)
    l.append(header)
    l.append("-" * len(header))
    for i in cols:
        l.append(f % tuple(i))

    l.append("")
    l.append("")
    return "\n".join(l)

def basename(uri):
    return uri.rstrip("/").split("/")[-1]

def step(desc):
    print desc
    print "=" * len(desc)
    print

def end_step():
    raw_input("Press enter to run the next step.")
    print
    print

def check_response(r, expected_statuses=None):
    if expected_statuses == None:
        expected_statuses = [200]

    ok = False

    for i in expected_statuses:
        if r.status_code == i:
            ok = True
            break

    if not ok:
        print "Request failed to succeed:"
        print "Status: %s" % (r.status_code,)
        print r.content
        sys.exit(1)

Use sys in error cases.

Use sys in error cases.

Response:
import sys

def format_cols(cols):
    widths = [0] * len(cols[0])

    for i in cols:
        for idx, val in enumerate(i):
            widths[idx] = max(len(val), widths[idx])

    f = ""

    t = []
    for i in widths:
        t.append("%%-0%ds" % (i,))

    return "    ".join(t)

def column_report(title, fields, cols):
    l = []
    l.append("[" + title + "]")
    l.append("")

    f = format_cols([fields] + cols)

    header = f % tuple(fields)
    l.append(header)
    l.append("-" * len(header))
    for i in cols:
        l.append(f % tuple(i))

    l.append("")
    l.append("")
    return "\n".join(l)

def basename(uri):
    return uri.rstrip("/").split("/")[-1]

def step(desc):
    print desc
    print "=" * len(desc)
    print

def end_step():
    raw_input("Press enter to run the next step.")
    print
    print

def check_response(r, expected_statuses=None):
    if expected_statuses == None:
        expected_statuses = [200]

    ok = False

    for i in expected_statuses:
        if r.status_code == i:
            ok = True
            break

    if not ok:
        print "Request failed to succeed:"
        print "Status: %s" % (r.status_code,)
        print r.content
        sys.exit(1)

Prompt:
Use sys in error cases.

def format_cols(cols):
    widths = [0] * len(cols[0])

    for i in cols:
        for idx, val in enumerate(i):
            widths[idx] = max(len(val), widths[idx])

    f = ""

    t = []
    for i in widths:
        t.append("%%-0%ds" % (i,))

    return "    ".join(t)

def column_report(title, fields, cols):
    l = []
    l.append("[" + title + "]")
    l.append("")

    f = format_cols([fields] + cols)

    header = f % tuple(fields)
    l.append(header)
    l.append("-" * len(header))
    for i in cols:
        l.append(f % tuple(i))

    l.append("")
    l.append("")
    return "\n".join(l)

def basename(uri):
    return uri.rstrip("/").split("/")[-1]

def step(desc):
    print desc
    print "=" * len(desc)
    print

def end_step():
    raw_input("Press enter to run the next step.")
    print
    print

def check_response(r, expected_statuses=None):
    if expected_statuses == None:
        expected_statuses = [200]

    ok = False

    for i in expected_statuses:
        if r.status_code == i:
            ok = True
            break

    if not ok:
        print "Request failed to succeed:"
        print "Status: %s" % (r.status_code,)
        print r.content
        sys.exit(1)


from multiprocessing import Queue
from Queue import Empty
import json

from .base import BaseQueue

sqs = None
def do_delayed_imports():
    global sqs
    from boto import sqs

class SQSQueue(BaseQueue):
    _cache = Queue()

    def __init__(self):
        BaseQueue.__init__(self)
        do_delayed_imports()

        self.conn = sqs.connect_to_region('us-west-2')
        self.unprocessed = self.conn.create_queue('structured-catalog-unprocessed')

    def push(self, job):
        m = sqs.message.Message()
        m.set_body(json.dumps(job))
        self.unprocessed.write(m)

    def get(self):
        try:
            msg = self._cache.get(block=False)
            self.remove(msg)
            return json.loads(msg.get_body())
        except Empty:
            rs = self.unprocessed.get_messages(num_messages=10)
            if not rs:
                return

            for msg in rs:
                self._cache.put(msg)
            return self.get()

    def remove(self, msg):
        self.unprocessed.delete_message(msg)

Response:
from multiprocessing import Queue
from Queue import Empty
import json

from .base import BaseQueue

sqs = None
def do_delayed_imports():
    global sqs
    from boto import sqs

class SQSQueue(BaseQueue):
    _cache = Queue()

    def __init__(self):
        BaseQueue.__init__(self)
        do_delayed_imports()

        self.conn = sqs.connect_to_region('us-west-2')
        self.unprocessed = self.conn.create_queue('structured-catalog-unprocessed')

    def push(self, job):
        m = sqs.message.Message()
        m.set_body(json.dumps(job))
        self.unprocessed.write(m)

    def get(self):
        try:
            msg = self._cache.get(block=False)
            self.remove(msg)
            return json.loads(msg.get_body())
        except Empty:
            rs = self.unprocessed.get_messages(num_messages=10)
            if not rs:
                return

            for msg in rs:
                self._cache.put(msg)
            return self.get()

    def remove(self, msg):
        self.unprocessed.delete_message(msg)

Prompt:
Use queue from multiprocessing library instead of Queue

from Queue import Queue, Empty
import json

from .base import BaseQueue

sqs = None
def do_delayed_imports():
    global sqs
    from boto import sqs

class SQSQueue(BaseQueue):
    _cache = Queue()

    def __init__(self):
        BaseQueue.__init__(self)
        do_delayed_imports()

        self.conn = sqs.connect_to_region('us-west-2')
        self.unprocessed = self.conn.create_queue('structured-catalog-unprocessed')

    def push(self, job):
        m = sqs.message.Message()
        m.set_body(json.dumps(job))
        self.unprocessed.write(m)

    def get(self):
        try:
            msg = self._cache.get(block=False)
            self.remove(msg)
            return json.loads(msg.get_body())
        except Empty:
            rs = self.unprocessed.get_messages(num_messages=10)
            if not rs:
                return

            for msg in rs:
                self._cache.put(msg)
            return self.get()

    def remove(self, msg):
        self.unprocessed.delete_message(msg)


from django_filters import FilterSet, CharFilter, IsoDateTimeFilter
from ui.models import Warc, Seed, Harvest
from django_filters import Filter
from django_filters.fields import Lookup

class ListFilter(Filter):
    def filter(self, qs, value):
        return super(ListFilter, self).filter(qs, Lookup(value.split(u","), "in"))

class WarcFilter(FilterSet):
    # Allows queries like /api/v1/warcs/?seedset=39c00280274a4db0b1cb5bfa4d527a1e
    seedset = CharFilter(name="harvest__historical_seed_set__seedset_id")
    seed = ListFilter(name="harvest__historical_seeds__seed_id", distinct=True)
    harvest_date_start = IsoDateTimeFilter(name="harvest__date_started", lookup_type='gte')
    harvest_date_end = IsoDateTimeFilter(name="harvest__date_started", lookup_type='lte')

    class Meta:
        model = Warc
        fields = ['seedset']

Response:
from django_filters import FilterSet, CharFilter, IsoDateTimeFilter
from ui.models import Warc, Seed, Harvest
from django_filters import Filter
from django_filters.fields import Lookup

class ListFilter(Filter):
    def filter(self, qs, value):
        return super(ListFilter, self).filter(qs, Lookup(value.split(u","), "in"))

class WarcFilter(FilterSet):
    # Allows queries like /api/v1/warcs/?seedset=39c00280274a4db0b1cb5bfa4d527a1e
    seedset = CharFilter(name="harvest__historical_seed_set__seedset_id")
    seed = ListFilter(name="harvest__historical_seeds__seed_id", distinct=True)
    harvest_date_start = IsoDateTimeFilter(name="harvest__date_started", lookup_type='gte')
    harvest_date_end = IsoDateTimeFilter(name="harvest__date_started", lookup_type='lte')

    class Meta:
        model = Warc
        fields = ['seedset']

Prompt:
Fix to take into account history in API queries.

from django_filters import FilterSet, CharFilter, IsoDateTimeFilter
from ui.models import Warc, Seed, Harvest
from django_filters import Filter
from django_filters.fields import Lookup

class ListFilter(Filter):
    def filter(self, qs, value):
        return super(ListFilter, self).filter(qs, Lookup(value.split(u","), "in"))

class WarcFilter(FilterSet):
    # Allows queries like /api/v1/warcs/?seedset=39c00280274a4db0b1cb5bfa4d527a1e
    seedset = CharFilter(name="harvest__seed_set__seedset_id")
    seed = ListFilter(name="harvest__seed_set__seeds__seed_id", distinct=True)
    # TODO: This will need to be changed to use historical seeds once #54 is completed.
    harvest_date_start = IsoDateTimeFilter(name="harvest__date_started", lookup_type='gte')
    harvest_date_end = IsoDateTimeFilter(name="harvest__date_started", lookup_type='lte')

    class Meta:
        model = Warc
        fields = ['seedset']


Add a test for update_condmatr

Add a test for update_condmatr

Response:
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

import unittest as ut

import stripeline.maptools as mt
import numpy as np

class TestMaptools(ut.TestCase):

    def test_condmatr(self):
        matr = np.zeros((2, 9), dtype='float64', order='F')
        mt.update_condmatr(numpix=2,
                           pixidx=[0, 0, 0],
                           angle=np.array([0.0, 1. / 8., 1. / 4.]) * np.pi,
                           m=matr)
        mt.update_condmatr(numpix=2,
                           pixidx=[1, 1, 1],
                           angle=np.array([0.0, 1. / 12., 1. / 8.]) * np.pi,
                           m=matr)

        expected = np.array([
            [3., 1.7071067811865475, 1.7071067811865475,
             1.7071067811865475, 1.5, 0.5,
             1.7071067811865475, 0.5, 1.5],
            [3., 2.5731321849709863, 1.2071067811865475,
             2.5731321849709863, 2.25, 0.9330127018922193,
             1.2071067811865475, 0.9330127018922193, 0.75]
        ])

        self.assertTrue(np.allclose(expected, matr))

Prompt: Add a test for update_condmatr


from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

import tornado.options
import server
import os

use_https = False

if use_https:
    default_port = 443
else:
    default_port = 80

tornado.options.define("port", default=default_port, help="run on the given port", type=int)

# Overwrite the port from the Heroku run from Procfile.
tornado.options.parse_command_line()

appPath = os.path.dirname(os.path.realpath(__file__))

if __name__ == '__main__':
    if use_https:
        http_server = HTTPServer(WSGIContainer(server.app), ssl_options={
            "certfile": appPath + '/ssl.crt',
            "keyfile": appPath + '/ssl.key'
        })
    else:
        http_server = HTTPServer(WSGIContainer(server.app))

    http_server.listen(tornado.options.options.port)

    IOLoop.instance().start()

Response:
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

import tornado.options
import server
import os

use_https = False

if use_https:
    default_port = 443
else:
    default_port = 80

tornado.options.define("port", default=default_port, help="run on the given port", type=int)

# Overwrite the port from the Heroku run from Procfile.
tornado.options.parse_command_line()

appPath = os.path.dirname(os.path.realpath(__file__))

if __name__ == '__main__':
    if use_https:
        http_server = HTTPServer(WSGIContainer(server.app), ssl_options={
            "certfile": appPath + '/ssl.crt',
            "keyfile": appPath + '/ssl.key'
        })
    else:
        http_server = HTTPServer(WSGIContainer(server.app))

    http_server.listen(tornado.options.options.port)

    IOLoop.instance().start()

Prompt:
Correct the check of use HTTPS in Tornado file.

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

import tornado.options
import server
import os

use_https = False

if use_https:
    default_port = 80
else:
    default_port = 443

tornado.options.define("port", default=default_port, help="run on the given port", type=int)

# Overwrite the port from the Heroku run from Procfile.
tornado.options.parse_command_line()

appPath = os.path.dirname(os.path.realpath(__file__))

if __name__ == '__main__':
    if use_https:
        http_server = HTTPServer(WSGIContainer(server.app), ssl_options={
            "certfile": appPath + '/ssl.crt',
            "keyfile": appPath + '/ssl.key'
        })
    else:
        http_server = HTTPServer(WSGIContainer(server.app))

    http_server.listen(tornado.options.options.port)

    IOLoop.instance().start()


Adjust spacing to match Twisted's.

Response:
# Copyright (c) 2003-2008 Ralph Meijer
# See LICENSE for details.

class Error(Exception):
    msg = ''

    def __str__(self):
        return self.msg

class NodeNotFound(Error):
    pass

class NodeExists(Error):
    pass

class NotSubscribed(Error):
    """
    Entity is not subscribed to this node.
    """

class SubscriptionExists(Error):
    pass

class Forbidden(Error):
    pass

class ItemForbidden(Error):
    pass

class ItemRequired(Error):
    pass

class NoInstantNodes(Error):
    pass

class InvalidConfigurationOption(Error):
    msg = 'Invalid configuration option'

class InvalidConfigurationValue(Error):
    msg = 'Bad configuration value'

class NodeNotPersistent(Error):
    pass

class NoRootNode(Error):
    pass

Prompt:
Adjust spacing to match Twisted's.

--HG--
extra : convert_revision : svn%3Ae60b0d31-0b1b-0410-851a-a02d0c527677/trunk%40258

# Copyright (c) 2003-2008 Ralph Meijer
# See LICENSE for details.

class Error(Exception):
    msg = ''

    def __str__(self):
        return self.msg

class NodeNotFound(Error):
    pass

class NodeExists(Error):
    pass

class NotSubscribed(Error):
    """
    Entity is not subscribed to this node.
    """

class SubscriptionExists(Error):
    pass

class Forbidden(Error):
    pass

class ItemForbidden(Error):
    pass

class ItemRequired(Error):
    pass

class NoInstantNodes(Error):
    pass

class InvalidConfigurationOption(Error):
    msg = 'Invalid configuration option'

class InvalidConfigurationValue(Error):
    msg = 'Bad configuration value'

class NodeNotPersistent(Error):
    pass

class NoRootNode(Error):
    pass