import csv

class Synthesizer():

    def __init__(self):
        self.word_map = {}

        filename = "../../dictionaries/VoxForgeDict"
        for line in csv.reader(open(filename), delimiter=' ', skipinitialspace=True):
            if len(line) > 2:
                self.word_map[line[0]] = line[2:]
        print len(self.word_map)

    def string_to_phonemes(self, string):
        string = string.upper()
        string = string.replace('.','')
        string = string.replace(',','')
        words = string.split()

        result = ""
        for word in words:
            print self.word_map[word]

Add the function for parsing strings to phonemes

Response:
import csv

class Synthesizer():

    def __init__(self):
        self.word_map = {}

        filename = "../../dictionaries/VoxForgeDict"
        for line in csv.reader(open(filename), delimiter=' ', skipinitialspace=True):
            if len(line) > 2:
                self.word_map[line[0]] = line[2:]
        print len(self.word_map)

    def string_to_phonemes(self, string):
        string = string.upper()
        string = string.replace('.','')
        string = string.replace(',','')
        words = string.split()

        result = ""
        for word in words:
            print self.word_map[word]

Prompt: Add the function for parsing strings to phonemes


from Bio import SeqIO
import logging
import unittest

from select_taxa import select_genomes_by_ids
import translate

class Test(unittest.TestCase):

    def setUp(self):
        self.longMessage = True
        logging.root.setLevel(logging.DEBUG)

    def test_translate_genomes(self):
        # Select genomes
        genomes = select_genomes_by_ids(['13305.1']).values()

        # Call translate
        dnafiles, aafiles = translate.translate_genomes(genomes)

        # Verify expected output
        first_header = '13305.1|NC_008253.1|YP_667942.1|None|thr'
        first = next(SeqIO.parse(dnafiles[0], 'fasta'))
        self.assertEqual(first_header, first.id)
        first = next(SeqIO.parse(aafiles[0], 'fasta'))
        self.assertEqual(first_header, first.id)

        # Verify no header appears twice
        headers = [record.id for record in SeqIO.parse(aafiles[0], 'fasta')]
        self.assertEqual(len(headers), len(set(headers)))

    def test_translate_93125_2(self):
        # Select genomes
        genomes = select_genomes_by_ids(['93125.2']).values()

        # Call translate
        aafiles = translate.translate_genomes(genomes)[1]

        # Verify no header appears twice
        headers = [record.id for record in SeqIO.parse(aafiles[0], 'fasta')]
        self.assertEqual(len(headers), len(set(headers)))

Response:
from Bio import SeqIO
import logging
import unittest

from select_taxa import select_genomes_by_ids
import translate

class Test(unittest.TestCase):

    def setUp(self):
        self.longMessage = True
        logging.root.setLevel(logging.DEBUG)

    def test_translate_genomes(self):
        # Select genomes
        genomes = select_genomes_by_ids(['13305.1']).values()

        # Call translate
        dnafiles, aafiles = translate.translate_genomes(genomes)

        # Verify expected output
        first_header = '13305.1|NC_008253.1|YP_667942.1|None|thr'
        first = next(SeqIO.parse(dnafiles[0], 'fasta'))
        self.assertEqual(first_header, first.id)
        first = next(SeqIO.parse(aafiles[0], 'fasta'))
        self.assertEqual(first_header, first.id)

        # Verify no header appears twice
        headers = [record.id for record in SeqIO.parse(aafiles[0], 'fasta')]
        self.assertEqual(len(headers), len(set(headers)))

    def test_translate_93125_2(self):
        # Select genomes
        genomes = select_genomes_by_ids(['93125.2']).values()

        # Call translate
        aafiles = translate.translate_genomes(genomes)[1]

        # Verify no header appears twice
        headers = [record.id for record in SeqIO.parse(aafiles[0], 'fasta')]
        self.assertEqual(len(headers), len(set(headers)))

Prompt:
Verify no header appears twice when translating 93125.2
from Bio import SeqIO
import logging
import unittest

from select_taxa import select_genomes_by_ids
import translate

class Test(unittest.TestCase):

    def setUp(self):
        self.longMessage = True
        logging.root.setLevel(logging.DEBUG)

    def test_translate_genomes(self):
        # Select genomes
        genomes = select_genomes_by_ids(['13305.1']).values()

        # Call translate
        dnafiles, aafiles = translate.translate_genomes(genomes)

        # Verify expected output
        first_header = '13305.1|NC_008253.1|YP_667942.1|None|thr'
        first = next(SeqIO.parse(dnafiles[0], 'fasta'))
        self.assertEqual(first_header, first.id)
        first = next(SeqIO.parse(aafiles[0], 'fasta'))
        self.assertEqual(first_header, first.id)


from django.conf.urls import patterns, include, url
from django.core.urlresolvers import reverse_lazy

from MyInfo import views as my_info_views
from django_cas import views as cas_views
from oam_base import views as base_views

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', my_info_views.index, name='index'),
    url(r'^MyInfo/', include('MyInfo.urls', namespace='MyInfo')),
    url(r'^AccountPickup/', include('AccountPickup.urls', namespace='AccountPickup')),
    url(r'^PasswordReset/', include('PasswordReset.urls', namespace='PasswordReset')),

    url(r'^accounts/login/$', cas_views.login, {'next_page': reverse_lazy('AccountPickup:next_step')}, name='CASLogin'),
    url(r'^accounts/logout/$', cas_views.logout, name='CASLogout'),

    url(r'^error/denied/$', base_views.rate_limited, name='rate_limited'),

    url(r'^ajax/', include('ajax.urls')),

    url(r'^admin/', include(admin.site.urls)),
)

handler500 = 'oam_base.views.custom_error'

Response:
from django.conf.urls import patterns, include, url
from django.core.urlresolvers import reverse_lazy

from MyInfo import views as my_info_views
from django_cas import views as cas_views
from oam_base import views as base_views

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', my_info_views.index, name='index'),
    url(r'^MyInfo/', include('MyInfo.urls', namespace='MyInfo')),
    url(r'^AccountPickup/', include('AccountPickup.urls', namespace='AccountPickup')),
    url(r'^PasswordReset/', include('PasswordReset.urls', namespace='PasswordReset')),

    url(r'^accounts/login/$', cas_views.login, {'next_page': reverse_lazy('AccountPickup:next_step')}, name='CASLogin'),
    url(r'^accounts/logout/$', cas_views.logout, name='CASLogout'),

    url(r'^error/denied/$', base_views.rate_limited, name='rate_limited'),

    url(r'^ajax/', include('ajax.urls')),

    url(r'^admin/', include(admin.site.urls)),
)

handler500 = 'oam_base.views.custom_error'

Prompt:
Make the ratelimited error URL follow established conventions.

from django.conf.urls import patterns, include, url
from django.core.urlresolvers import reverse_lazy

from MyInfo import views as my_info_views
from django_cas import views as cas_views
from oam_base import views as base_views

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', my_info_views.index, name='index'),
    url(r'^MyInfo/', include('MyInfo.urls', namespace='MyInfo')),
    url(r'^AccountPickup/', include('AccountPickup.urls', namespace='AccountPickup')),
    url(r'^PasswordReset/', include('PasswordReset.urls', namespace='PasswordReset')),

    url(r'^accounts/login/$', cas_views.login, {'next_page': reverse_lazy('AccountPickup:next_step')}, name='CASLogin'),
    url(r'^accounts/logout/$', cas_views.logout, name='CASLogout'),

    url(r'^error/denied$', base_views.rate_limited, name='rate_limited'),

    url(r'^ajax/', include('ajax.urls')),

    url(r'^admin/', include(admin.site.urls)),
)

handler500 = 'oam_base.views.custom_error'


# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-08-28 16:37
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('teamstats', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='video',
            name='mp4',
            field=models.FilePathField(blank=True, match='.*\\.mp4$', null=True, path='videos', recursive=True),
        ),
        migrations.AlterField(
            model_name='video',
            name='ogg',
            field=models.FilePathField(blank=True, match='.*\\.ogv$', null=True, path='videos', recursive=True),
        ),
        migrations.AlterField(
            model_name='video',
            name='webm',
            field=models.FilePathField(blank=True, match='.*\\.webm$', null=True, path='videos', recursive=True),
        ),
    ]

Add migrations for media files

Add migrations for media files

Response:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-08-28 16:37
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('teamstats', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='video',
            name='mp4',
            field=models.FilePathField(blank=True, match='.*\\.mp4$', null=True, path='videos', recursive=True),
        ),
        migrations.AlterField(
            model_name='video',
            name='ogg',
            field=models.FilePathField(blank=True, match='.*\\.ogv$', null=True, path='videos', recursive=True),
        ),
        migrations.AlterField(
            model_name='video',
            name='webm',
            field=models.FilePathField(blank=True, match='.*\\.webm$', null=True, path='videos', recursive=True),
        ),
    ]

Prompt: Add migrations for media files


from selenium import webdriver
import unittest

class FunctionalTest(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(3)

    def tearDown(self):
        self.browser.quit()

    def test_app_home_title(self):
        self.browser.get('http://localhost:8000/system_maintenance')
        self.assertIn('System Maintenance', self.browser.title)

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

Add simple functional test to test the title of the app's home page

Response:
from selenium import webdriver
import unittest

class FunctionalTest(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(3)

    def tearDown(self):
        self.browser.quit()

    def test_app_home_title(self):
        self.browser.get('http://localhost:8000/system_maintenance')
        self.assertIn('System Maintenance', self.browser.title)

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

Prompt: Add simple functional test to test the title of the app's home page


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

import hashlib
import argparse
import re
import os

parser = argparse.ArgumentParser( description="Given a list of python wheels, generate a list of hyperlinks to GitHub with sha512 fragment identifier" )
parser.add_argument( '--hash', choices=['md5','sha256', 'sha512'], default='sha512')
parser.add_argument( '-f', '--format', choices=['html','md'], default='html')
parser.add_argument( 'files', metavar="python.whl", type=argparse.FileType(mode='rb'), nargs='+' )

args = parser.parse_args()

for f in args.files:
    name = os.path.basename(f.name)
    #version="1.1.0"
    version = re.match(r'SimpleITK-([0-9]+\.[0-9]+(\.[0-9]+)?(rc[0-9]+)?)', name ).group(1)
    print("version:{0}".format(version))
    if args.hash == "md5":
       hash_value = hashlib.md5(f.read()).hexdigest()
    elif args.hash == "sha256":
       hash_value = hashlib.sha256(f.read()).hexdigest()
    elif args.hash == "sha512":
       hash_value = hashlib.sha512(f.read()).hexdigest()

    tag = "v{0}".format(version)

    #host="SourceForge"
    #url = "https://sourceforge.net/projects/simpleitk/files/SimpleITK/{0}/Python/{1}#{2}={3}".format(version,name,args.hash,hash_value)

    host = "GitHub"
    url = "https://github.com/SimpleITK/SimpleITK/releases/download/{0}/{1}#{2}={3}".format(tag,name,args.hash,hash_value)

    if args.format == 'html':
        print "
{1} (hosted at {2})
".format(url,name,host)
    elif args.format == 'md':
        print "[{1}]({0})".format(url,name)

    f.close()

Add script to generate download links used on simpleitk.org

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

import hashlib
import argparse
import re
import os

parser = argparse.ArgumentParser( description="Given a list of python wheels, generate a list of hyperlinks to GitHub with sha512 fragment identifier" )
parser.add_argument( '--hash', choices=['md5','sha256', 'sha512'], default='sha512')
parser.add_argument( '-f', '--format', choices=['html','md'], default='html')
parser.add_argument( 'files', metavar="python.whl", type=argparse.FileType(mode='rb'), nargs='+' )

args = parser.parse_args()

for f in args.files:
    name = os.path.basename(f.name)
    #version="1.1.0"
    version = re.match(r'SimpleITK-([0-9]+\.[0-9]+(\.[0-9]+)?(rc[0-9]+)?)', name ).group(1)
    print("version:{0}".format(version))
    if args.hash == "md5":
       hash_value = hashlib.md5(f.read()).hexdigest()
    elif args.hash == "sha256":
       hash_value = hashlib.sha256(f.read()).hexdigest()
    elif args.hash == "sha512":
       hash_value = hashlib.sha512(f.read()).hexdigest()

    tag = "v{0}".format(version)

    #host="SourceForge"
    #url = "https://sourceforge.net/projects/simpleitk/files/SimpleITK/{0}/Python/{1}#{2}={3}".format(version,name,args.hash,hash_value)

    host = "GitHub"
    url = "https://github.com/SimpleITK/SimpleITK/releases/download/{0}/{1}#{2}={3}".format(tag,name,args.hash,hash_value)

    if args.format == 'html':
        print "<li><a href=\"{0}\" title=\"Click to download {1}\">{1} (hosted at {2})</a></li>".format(url,name,host)
    elif args.format == 'md':
        print "[{1}]({0})".format(url,name)

    f.close()

Prompt: Add script to generate download links used on simpleitk.org


from __future__ import print_function

from datetime import datetime

from django.db.backends import util

import sqlparse

from debug_toolbar.utils import ms_from_timedelta

class PrintQueryWrapper(util.CursorDebugWrapper):
    def execute(self, sql, params=()):
        starttime = datetime.now()
        try:
            return self.cursor.execute(sql, params)
        finally:
            raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
            execution_time = ms_from_timedelta(datetime.now() - starttime)
            formatted_sql = sqlparse.format(raw_sql, reindent=True)
            print('%s [%.2fms]' % (formatted_sql, execution_time))

util.CursorDebugWrapper = PrintQueryWrapper

Response:
from __future__ import print_function

from datetime import datetime

from django.db.backends import util

import sqlparse

from debug_toolbar.utils import ms_from_timedelta

class PrintQueryWrapper(util.CursorDebugWrapper):
    def execute(self, sql, params=()):
        starttime = datetime.now()
        try:
            return self.cursor.execute(sql, params)
        finally:
            raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
            execution_time = ms_from_timedelta(datetime.now() - starttime)
            formatted_sql = sqlparse.format(raw_sql, reindent=True)
            print('%s [%.2fms]' % (formatted_sql, execution_time))

util.CursorDebugWrapper = PrintQueryWrapper

Prompt:
Replace print statement by print function.

from datetime import datetime

from django.db.backends import util

import sqlparse

from debug_toolbar.utils import ms_from_timedelta

class PrintQueryWrapper(util.CursorDebugWrapper):
    def execute(self, sql, params=()):
        starttime = datetime.now()
        try:
            return self.cursor.execute(sql, params)
        finally:
            raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
            execution_time = datetime.now() - starttime
            print sqlparse.format(raw_sql, reindent=True),
            print ' [%.2fms]' % (ms_from_timedelta(execution_time),)
            print

util.CursorDebugWrapper = PrintQueryWrapper


# -*- coding: utf-8 -*-
"""
    app.models
    ~~~~~~~~~~

    Provides the SQLAlchemy models
"""
from __future__ import (
    absolute_import, division, print_function, with_statement,
    unicode_literals)

import savalidation.validators as val

from datetime import datetime as dt
from app import db
from savalidation import ValidationMixin

class HDRO(db.Model, ValidationMixin):
    # auto keys
    id = db.Column(db.Integer, primary_key=True)
    utc_created = db.Column(db.DateTime, nullable=False, default=dt.utcnow())
    utc_updated = db.Column(
        db.DateTime, nullable=False, default=dt.utcnow(), onupdate=dt.utcnow())

    # other keys
    rid = db.Column(db.String(16), nullable=False, index=True, unique=True)
    country = db.Column(db.String(32), nullable=False)
    indicator = db.Column(db.String(128), nullable=False)
    value = db.Column(db.Numeric, nullable=False)
    year = db.Column(db.Integer, nullable=False)

    # validation
    val.validates_constraints()

    def __repr__(self):
        return ('<HDRO(%r, %r)>' % (self.country, self.indicator))

Add unique constraint to rid

Add unique constraint to rid

Response:
# -*- coding: utf-8 -*-
"""
    app.models
    ~~~~~~~~~~

    Provides the SQLAlchemy models
"""
from __future__ import (
    absolute_import, division, print_function, with_statement,
    unicode_literals)

import savalidation.validators as val

from datetime import datetime as dt
from app import db
from savalidation import ValidationMixin

class HDRO(db.Model, ValidationMixin):
    # auto keys
    id = db.Column(db.Integer, primary_key=True)
    utc_created = db.Column(db.DateTime, nullable=False, default=dt.utcnow())
    utc_updated = db.Column(
        db.DateTime, nullable=False, default=dt.utcnow(), onupdate=dt.utcnow())

    # other keys
    rid = db.Column(db.String(16), nullable=False, index=True, unique=True)
    country = db.Column(db.String(32), nullable=False)
    indicator = db.Column(db.String(128), nullable=False)
    value = db.Column(db.Numeric, nullable=False)
    year = db.Column(db.Integer, nullable=False)

    # validation
    val.validates_constraints()

    def __repr__(self):
        return ('<HDRO(%r, %r)>' % (self.country, self.indicator))

Prompt:
Add unique constraint to rid
# -*- coding: utf-8 -*-
"""
    app.models
    ~~~~~~~~~~

    Provides the SQLAlchemy models
"""
from __future__ import (
    absolute_import, division, print_function, with_statement,
    unicode_literals)

import savalidation.validators as val

from datetime import datetime as dt
from app import db
from savalidation import ValidationMixin

class HDRO(db.Model, ValidationMixin):
    # auto keys
    id = db.Column(db.Integer, primary_key=True)
    utc_created = db.Column(db.DateTime, nullable=False, default=dt.utcnow())
    utc_updated = db.Column(
        db.DateTime, nullable=False, default=dt.utcnow(), onupdate=dt.utcnow())

    # other keys
    rid = db.Column(db.String(16), nullable=False, index=True)
    country = db.Column(db.String(32), nullable=False)
    indicator = db.Column(db.String(128), nullable=False)
    value = db.Column(db.Numeric, nullable=False)
    year = db.Column(db.Integer, nullable=False)

    # validation
    val.validates_constraints()

    def __repr__(self):
        return ('<HDRO(%r, %r)>' % (self.country, self.indicator))


from . import TestCase

import JsonStats.FetchStats.Plugins
from JsonStats.FetchStats import Fetcher

class TestPluginMount(TestCase):

    def setUp(self):
        # Do stuff that has to happen on every test in this instance
        self.fetcher = Fetcher

        class _example_plugin(Fetcher):
            def __init__(self):
                self.context = 'testplugin'
                self._load_data()
            def _load_data(self):
                self._loaded(True)
            def dump(self):
                return {}
            def dump_json(self):
                return self.json.dumps(self.dump())

        self.example_plugin = _example_plugin

    def test_get_plugins(self):
        """
        Verify that after loading plugins we can see them attached to
        the Mount.
        """
        example_plugin = self.example_plugin()
        discovered = len(self.fetcher.get_plugins())
        assert discovered == 1

Response:
from . import TestCase

import JsonStats.FetchStats.Plugins
from JsonStats.FetchStats import Fetcher

class TestPluginMount(TestCase):

    def setUp(self):
        # Do stuff that has to happen on every test in this instance
        self.fetcher = Fetcher

        class _example_plugin(Fetcher):
            def __init__(self):
                self.context = 'testplugin'
                self._load_data()
            def _load_data(self):
                self._loaded(True)
            def dump(self):
                return {}
            def dump_json(self):
                return self.json.dumps(self.dump())

        self.example_plugin = _example_plugin

    def test_get_plugins(self):
        """
        Verify that after loading plugins we can see them attached to
        the Mount.
        """
        example_plugin = self.example_plugin()
        discovered = len(self.fetcher.get_plugins())
        assert discovered == 1

Prompt:
Fix the plugin mount text. And make it way more intelligent.

from JsonStats.FetchStats.Plugins import *

from . import TestCase

import JsonStats.FetchStats.Plugins
from JsonStats.FetchStats import Fetcher

class TestPluginMount(TestCase):

    def setUp(self):
        # Do stuff that has to happen on every test in this instance
        self.fetcher = Fetcher

    def test_get_plugins(self):
        """
        Verify that after loading plugins we can see them attached to
        the Mount.
        """
        discovered = len(self.fetcher.get_plugins())
        expected = len(JsonStats.FetchStats.Plugins.__all__)
        self.assertEqual(discovered, expected)


#
# -*- coding: utf-8 -*-
#
# This file is part of reclass (http://github.com/madduck/reclass)
#
# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
# Released under the terms of the Artistic Licence 2.0
#

from output import OutputLoader
from storage import StorageBackendLoader

def get_data(storage_type, nodes_uri, classes_uri, applications_postfix, node):
    storage_class = StorageBackendLoader(storage_type).load()
    storage = storage_class(nodes_uri, classes_uri, applications_postfix)
    if not node:
        ret = storage.inventory()
    else:
        ret = storage.nodeinfo(node)

    return ret

def output(data, fmt, pretty_print=False):
    output_class = OutputLoader(fmt).load()
    outputter = output_class()
    return outputter.dump(data, pretty_print=pretty_print)

Response:
#
# -*- coding: utf-8 -*-
#
# This file is part of reclass (http://github.com/madduck/reclass)
#
# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
# Released under the terms of the Artistic Licence 2.0
#

from output import OutputLoader
from storage import StorageBackendLoader

def get_data(storage_type, nodes_uri, classes_uri, applications_postfix, node):
    storage_class = StorageBackendLoader(storage_type).load()
    storage = storage_class(nodes_uri, classes_uri, applications_postfix)
    if not node:
        ret = storage.inventory()
    else:
        ret = storage.nodeinfo(node)

    return ret

def output(data, fmt, pretty_print=False):
    output_class = OutputLoader(fmt).load()
    outputter = output_class()
    return outputter.dump(data, pretty_print=pretty_print)

Prompt:
Allow node to be None to trigger inventory

Signed-off-by: martin f. krafft <acc3492a66a5949176a2fc8886cf441478ca46a1@madduck.net>

#
# -*- coding: utf-8 -*-
#
# This file is part of reclass (http://github.com/madduck/reclass)
#
# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
# Released under the terms of the Artistic Licence 2.0
#

from output import OutputLoader
from storage import StorageBackendLoader

def get_data(storage_type, nodes_uri, classes_uri, applications_postfix, node):
    storage_class = StorageBackendLoader(storage_type).load()
    storage = storage_class(nodes_uri, classes_uri, applications_postfix)
    if node is False:
        ret = storage.inventory()
    else:
        ret = storage.nodeinfo(node)

    return ret

def output(data, fmt, pretty_print=False):
    output_class = OutputLoader(fmt).load()
    outputter = output_class()
    return outputter.dump(data, pretty_print=pretty_print)


#!/usr/bin/python

from setuptools import setup

setup(name="catsnap",
      version="6.0.0",
      description="catalog and store images",
      author="Erin Call",
      author_email="hello@erincall.com",
      url="https://github.com/ErinCall/",
      packages=['catsnap',
                'catsnap.document',
                'catsnap.config',
                'catsnap.batch'],
      install_requires=[
          "Flask==0.9",
          "gunicorn==0.14.6",
          "boto==2.5.2",
          "requests==0.13.2",
          "argparse==1.2.1",
          "psycopg2==2.4.6",
          "sqlalchemy==0.8.0b2",
          "yoyo-migrations==4.1.6",
          "wand==0.3.3",
          "celery==3.1.16",
          "redis==2.10.3",
          "gevent==1.1b5",
          "Flask-Sockets==0.1",
          "PyYAML==3.11",

          "mock==1.0.1",
          "nose==1.1.2",
          "splinter==0.5.3",
          "bcrypt==1.1.1",
      ],
      )

Response:
#!/usr/bin/python

from setuptools import setup

setup(name="catsnap",
      version="6.0.0",
      description="catalog and store images",
      author="Erin Call",
      author_email="hello@erincall.com",
      url="https://github.com/ErinCall/",
      packages=['catsnap',
                'catsnap.document',
                'catsnap.config',
                'catsnap.batch'],
      install_requires=[
          "Flask==0.9",
          "gunicorn==0.14.6",
          "boto==2.5.2",
          "requests==0.13.2",
          "argparse==1.2.1",
          "psycopg2==2.4.6",
          "sqlalchemy==0.8.0b2",
          "yoyo-migrations==4.1.6",
          "wand==0.3.3",
          "celery==3.1.16",
          "redis==2.10.3",
          "gevent==1.1b5",
          "Flask-Sockets==0.1",
          "PyYAML==3.11",

          "mock==1.0.1",
          "nose==1.1.2",
          "splinter==0.5.3",
          "bcrypt==1.1.1",
      ],
      )

Prompt:
Upgrade to a newer gevent for OSX Yosemity compat

See https://github.com/gevent/gevent/issues/656

#!/usr/bin/python

from setuptools import setup

setup(name="catsnap",
      version="6.0.0",
      description="catalog and store images",
      author="Erin Call",
      author_email="hello@erincall.com",
      url="https://github.com/ErinCall/",
      packages=['catsnap',
                'catsnap.document',
                'catsnap.config',
                'catsnap.batch'],
      install_requires=[
          "Flask==0.9",
          "gunicorn==0.14.6",
          "boto==2.5.2",
          "requests==0.13.2",
          "argparse==1.2.1",
          "psycopg2==2.4.6",
          "sqlalchemy==0.8.0b2",
          "yoyo-migrations==4.1.6",
          "wand==0.3.3",
          "celery==3.1.16",
          "redis==2.10.3",
          "gevent==1.0.2",
          "Flask-Sockets==0.1",
          "PyYAML==3.11",

          "mock==1.0.1",
          "nose==1.1.2",
          "splinter==0.5.3",
          "bcrypt==1.1.1",
      ],
      )


# Copyright 2013 Russell Heilling
import logging

from google.appengine.api import users

from pulldb import base
from pulldb import session
from pulldb.models.users import User

class Profile(session.SessionHandler):
  def get(self):
    app_user = users.get_current_user()
    template_values = self.base_template_values()
    template_values.update({
        'user': user_key(app_user).get(),
    })
    template = self.templates.get_template('users_profile.html')
    self.response.write(template.render(template_values))

def user_key(app_user=users.get_current_user(), create=True):
  logging.debug("Looking up user key for: %r", app_user)
  key = None
  user = User.query(User.userid == app_user.user_id()).get()
  if user:
    key = user.key
  elif create:
    logging.info('Adding user to datastore: %s', app_user.nickname())
    user = User(userid=app_user.user_id(),
                nickname=app_user.nickname())
    user.put()
    key = user.key
  return user.key

app = base.create_app([
    ('/users/me', Profile),
])

Add logging to track down a bug

Add logging to track down a bug

Response:
# Copyright 2013 Russell Heilling
import logging

from google.appengine.api import users

from pulldb import base
from pulldb import session
from pulldb.models.users import User

class Profile(session.SessionHandler):
  def get(self):
    app_user = users.get_current_user()
    template_values = self.base_template_values()
    template_values.update({
        'user': user_key(app_user).get(),
    })
    template = self.templates.get_template('users_profile.html')
    self.response.write(template.render(template_values))

def user_key(app_user=users.get_current_user(), create=True):
  logging.debug("Looking up user key for: %r", app_user)
  key = None
  user = User.query(User.userid == app_user.user_id()).get()
  if user:
    key = user.key
  elif create:
    logging.info('Adding user to datastore: %s', app_user.nickname())
    user = User(userid=app_user.user_id(),
                nickname=app_user.nickname())
    user.put()
    key = user.key
  return user.key

app = base.create_app([
    ('/users/me', Profile),
])

Prompt:
Add logging to track down a bug

# Copyright 2013 Russell Heilling
import logging

from google.appengine.api import users

from pulldb import base
from pulldb import session
from pulldb.models.users import User

class Profile(session.SessionHandler):
  def get(self):
    app_user = users.get_current_user()
    template_values = self.base_template_values()
    template_values.update({
        'user': user_key(app_user).get(),
    })
    template = self.templates.get_template('users_profile.html')
    self.response.write(template.render(template_values))

def user_key(app_user=users.get_current_user(), create=True):
  key = None
  user = User.query(User.userid == app_user.user_id()).get()
  if user:
    key = user.key
  elif create:
    logging.info('Adding user to datastore: %s', app_user.nickname())
    user = User(userid=app_user.user_id(),
                nickname=app_user.nickname())
    user.put()
    key = user.key
  return user.key

app = base.create_app([
    ('/users/me', Profile),
])


"""
Predict app's urls
"""
#
# pylint: disable=bad-whitespace
#
from django.conf.urls import patterns, include, url

from .views import *

def url_tree(regex, *urls):
    """Quick access to stitching url patterns"""
    return url(regex, include(patterns('', *urls)))

urlpatterns = patterns('',
  url(r'^$', Datasets.as_view(), name="view_my_datasets"),
  url_tree(r'^upload/',
    url(r'^$', UploadChoices.as_view(), name="upload"),
    url(r'^(?P<type>[\w-]+)/', UploadView.as_view(), name="upload"),
  ),
  url_tree(r'^(?P<slug>\w{32})/',
    url(r'^$',          DatasetView.as_view(),   name="view_single_dataset"),
    url(r'^note/$',     AddNote.as_view(),       name="add_note"),
  ),
)

Response:
"""
Predict app's urls
"""
#
# pylint: disable=bad-whitespace
#
from django.conf.urls import patterns, include, url

from .views import *

def url_tree(regex, *urls):
    """Quick access to stitching url patterns"""
    return url(regex, include(patterns('', *urls)))

urlpatterns = patterns('',
  url(r'^$', Datasets.as_view(), name="view_my_datasets"),
  url_tree(r'^upload/',
    url(r'^$', UploadChoices.as_view(), name="upload"),
    url(r'^(?P<type>[\w-]+)/', UploadView.as_view(), name="upload"),
  ),
  url_tree(r'^(?P<slug>\w{32})/',
    url(r'^$',          DatasetView.as_view(),   name="view_single_dataset"),
    url(r'^note/$',     AddNote.as_view(),       name="add_note"),
  ),
)

Prompt:
Remove callback url and bring uploads together

"""
Predict app's urls
"""
#
# pylint: disable=bad-whitespace
#
from django.conf.urls import patterns, include, url

from .views import *

def url_tree(regex, *urls):
    """Quick access to stitching url patterns"""
    return url(regex, include(patterns('', *urls)))

urlpatterns = patterns('',
  url(r'^$', Datasets.as_view(), name="view_my_datasets"),
  url_tree(r'^upload/',
    url(r'^$', UploadChoices.as_view(), name="upload"),
    url(r'^manual/$', UploadManual.as_view(), name="upload_manual"),
    url_tree(r'^(?P<type>[\w-]+)/',
      url(r'^$', UploadView.as_view(), name="upload"),
      url(r'^(?P<fastq>[\w-]+)/$', UploadView.as_view(), name="upload"),
    ),
  ),
  url_tree(r'^(?P<slug>\w{32})/',
    url(r'^$',          DatasetView.as_view(),   name="view_single_dataset"),
    url(r'^callback/$', Callback.as_view(),      name="callback"),
    url(r'^note/$',     AddNote.as_view(),       name="add_note"),
  ),
)


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

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

import re
with open('glooey/__init__.py') as file:
    version_pattern = re.compile("__version__ = '(.*)'")
    version = version_pattern.search(file.read()).group(1)

with open('README.rst') as file:
    readme = file.read()

setup(
    name='glooey',
    version=version,
    author='Kale Kundert',
    author_email='kale@thekunderts.net',
    description='An object-oriented GUI library for pyglet.',
    long_description=readme,
    url='https://github.com/kxgames/glooey',
    packages=[
        'glooey',
    ],
    include_package_data=True,
    install_requires=[
        'pyglet',
        'more_itertools',
        'vecrec',
        'autoprop',
        'debugtools',
    ],
    license='MIT',
    zip_safe=False,
    keywords=[
        'glooey',
        'pyglet',
        'gui',
        'library',
    ],
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3.5',
        'Topic :: Games/Entertainment',
        'Topic :: Software Development :: User Interfaces',
        'Topic :: Software Development :: Libraries',
    ],
)

Add debugtools as a dependency.

Add debugtools as a dependency.

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

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

import re
with open('glooey/__init__.py') as file:
    version_pattern = re.compile("__version__ = '(.*)'")
    version = version_pattern.search(file.read()).group(1)

with open('README.rst') as file:
    readme = file.read()

setup(
    name='glooey',
    version=version,
    author='Kale Kundert',
    author_email='kale@thekunderts.net',
    description='An object-oriented GUI library for pyglet.',
    long_description=readme,
    url='https://github.com/kxgames/glooey',
    packages=[
        'glooey',
    ],
    include_package_data=True,
    install_requires=[
        'pyglet',
        'more_itertools',
        'vecrec',
        'autoprop',
        'debugtools',
    ],
    license='MIT',
    zip_safe=False,
    keywords=[
        'glooey',
        'pyglet',
        'gui',
        'library',
    ],
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3.5',
        'Topic :: Games/Entertainment',
        'Topic :: Software Development :: User Interfaces',
        'Topic :: Software Development :: Libraries',
    ],
)

Prompt:
Add debugtools as a dependency.

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

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

import re
with open('glooey/__init__.py') as file:
    version_pattern = re.compile("__version__ = '(.*)'")
    version = version_pattern.search(file.read()).group(1)

with open('README.rst') as file:
    readme = file.read()

setup(
    name='glooey',
    version=version,
    author='Kale Kundert',
    author_email='kale@thekunderts.net',
    description='An object-oriented GUI library for pyglet.',
    long_description=readme,
    url='https://github.com/kxgames/glooey',
    packages=[
        'glooey',
    ],
    include_package_data=True,
    install_requires=[
        'pyglet',
        'more_itertools',
        'vecrec',
        'autoprop',
    ],
    license='MIT',
    zip_safe=False,
    keywords=[
        'glooey',
        'pyglet',
        'gui',
        'library',
    ],
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3.5',
        'Topic :: Games/Entertainment',
        'Topic :: Software Development :: User Interfaces',
        'Topic :: Software Development :: Libraries',
    ],
)


Use HTML entity for colon

Use HTML entity for colon

Response:
import os
import ast
import datetime
import re

grandparent = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
checks_dir = os.path.join(grandparent, "proselint", "checks")
listing = os.listdir(checks_dir)

def is_check(fn):
    return fn[-3:] == ".py" and not fn == "__init__.py"

for fn in listing:
    if is_check(fn):
        M = ast.parse(''.join(open(os.path.join(checks_dir, fn))))
        docstring = ast.get_docstring(M)
        error_code = re.search("error_code: (.*)\n", docstring).group(1)
        head, sep, tail = docstring.partition("title: ")
        docstring = head + sep + "     " + error_code + ":" + tail[4:]

        post_filename = os.path.join(
            os.path.join(grandparent, "site", "_posts"),
            str(datetime.date.today()) + "-" + docstring[0:6] + ".md")

        # Chop off the first two lines
        for i in xrange(2):
            docstring = '\n'.join(docstring.split('\n')[1:])

        # Create a new post in the blog.
        with open(post_filename, 'w') as f:
            f.write(docstring)

Prompt:
Use HTML entity for colon

import os
import ast
import datetime
import re

grandparent = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
checks_dir = os.path.join(grandparent, "proselint", "checks")
listing = os.listdir(checks_dir)

def is_check(fn):
    return fn[-3:] == ".py" and not fn == "__init__.py"

for fn in listing:
    if is_check(fn):
        M = ast.parse(''.join(open(os.path.join(checks_dir, fn))))
        docstring = ast.get_docstring(M)
        error_code = re.search("error_code: (.*)\n", docstring).group(1)
        head, sep, tail = docstring.partition("title: ")
        docstring = head + sep + "     " + error_code + ":" + tail[4:]

        post_filename = os.path.join(
            os.path.join(grandparent, "site", "_posts"),
            str(datetime.date.today()) + "-" + docstring[0:6] + ".md")

        # Chop off the first two lines
        for i in xrange(2):
            docstring = '\n'.join(docstring.split('\n')[1:])

        # Create a new post in the blog.
        with open(post_filename, 'w') as f:
            f.write(docstring)


from __future__ import division
from __future__ import print_function
from __future__ import absolute_import

import os
import subprocess

if __name__ == "__main__":
    for hiddens_dim in [512, 256, 128, 64, 32, 16]:
        print('Dumping files for (%d)' % hiddens_dim)
        model_path = '/mnt/visual_communication_dataset/trained_models_5_30_18/hiddens_fc6/%d/model_best.pth.tar' % hiddens_dim
        out_dir = './dump_hiddens_outputs/%d/' % hiddens_dim
        if not os.path.isdir(out_dir):
            os.makedirs(out_dir)
        command = 'CUDA_VISIBLE_DEVICES=7 python dump.py {model} --train-test-split-dir ./train_test_split/1 --out-dir {outdir} --average-labels --overwrite-layer fc6 --cuda'.format(model=model_path, outdir=out_dir)
        subprocess.call(command, shell=True)

Add dump script for all hiddens

Add dump script for all hiddens

Response:
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import

import os
import subprocess

if __name__ == "__main__":
    for hiddens_dim in [512, 256, 128, 64, 32, 16]:
        print('Dumping files for (%d)' % hiddens_dim)
        model_path = '/mnt/visual_communication_dataset/trained_models_5_30_18/hiddens_fc6/%d/model_best.pth.tar' % hiddens_dim
        out_dir = './dump_hiddens_outputs/%d/' % hiddens_dim
        if not os.path.isdir(out_dir):
            os.makedirs(out_dir)
        command = 'CUDA_VISIBLE_DEVICES=7 python dump.py {model} --train-test-split-dir ./train_test_split/1 --out-dir {outdir} --average-labels --overwrite-layer fc6 --cuda'.format(model=model_path, outdir=out_dir)
        subprocess.call(command, shell=True)

Prompt: Add dump script for all hiddens


#!/usr/bin/env python3

from setuptools import setup

setup(
    name='todoman',
    description='A simple CalDav-based todo manager.',
    author='Hugo Osvaldo Barrera',
    author_email='hugo@barrera.io',
    url='https://gitlab.com/hobarrera/todoman',
    license='MIT',
    packages=['todoman'],
    entry_points={
        'console_scripts': [
            'todo = todoman.cli:run',
        ]
    },
    install_requires=[
        'click',
        'icalendar',
        'urwid',
        'pyxdg',
        'atomicwrites',
        # https://github.com/tehmaze/ansi/pull/7
        'ansi>=0.1.3',
        'parsedatetime',
        'setuptools_scm',
    ],
    use_scm_version={'version_scheme': 'post-release'},
    setup_requires=['setuptools_scm'],
    # TODO: classifiers
)

Response:
#!/usr/bin/env python3

from setuptools import setup

setup(
    name='todoman',
    description='A simple CalDav-based todo manager.',
    author='Hugo Osvaldo Barrera',
    author_email='hugo@barrera.io',
    url='https://gitlab.com/hobarrera/todoman',
    license='MIT',
    packages=['todoman'],
    entry_points={
        'console_scripts': [
            'todo = todoman.cli:run',
        ]
    },
    install_requires=[
        'click',
        'icalendar',
        'urwid',
        'pyxdg',
        'atomicwrites',
        # https://github.com/tehmaze/ansi/pull/7
        'ansi>=0.1.3',
        'parsedatetime',
        'setuptools_scm',
    ],
    use_scm_version={'version_scheme': 'post-release'},
    setup_requires=['setuptools_scm'],
    # TODO: classifiers
)

Prompt:
Update URL to current point to gitlab.com

#!/usr/bin/env python3

from setuptools import setup

setup(
    name='todoman',
    description='A simple CalDav-based todo manager.',
    author='Hugo Osvaldo Barrera',
    author_email='hugo@barrera.io',
    url='https://git.barrera.io/hobarrera/todoman',
    license='MIT',
    packages=['todoman'],
    entry_points={
        'console_scripts': [
            'todo = todoman.cli:run',
        ]
    },
    install_requires=[
        'click',
        'icalendar',
        'urwid',
        'pyxdg',
        'atomicwrites',
        # https://github.com/tehmaze/ansi/pull/7
        'ansi>=0.1.3',
        'parsedatetime',
        'setuptools_scm',
    ],
    use_scm_version={'version_scheme': 'post-release'},
    setup_requires=['setuptools_scm'],
    # TODO: classifiers
)


def WIKIXML2TW(inputfilename, outputfilename):
   "Convert Wikimedia XML dump to TiddlyWiki import file"
   inputfile = open(inputfilename, "r")
   xmlinput = unicode(inputfile.read(), errors='ignore')
   outputfilemenu = open(outputfilename + '.menu', "w")
   outputfile = open(outputfilename, "w")
   outputfile.write('
'.encode('utf-8'))
   startpos = 0

   while startpos > -1:
     titleString = ''
     textString = ''
     startpos = xmlinput.find('<title>', startpos)
     if startpos > -1:
       titleString = xmlinput[startpos+len('<title>'):xmlinput.find('</title>', startpos)]
     if startpos > -1:
       startpos = xmlinput.find('</title>', startpos)
     if startpos > -1:
       startpos = xmlinput.find('<text xml:space="preserve">', startpos)
     if startpos > -1:
       textString = xmlinput[startpos+len('<text xml:space="preserve">'):xmlinput.find('</text>', startpos)]
     if startpos > -1:
       startpos = xmlinput.find('</text>', startpos)
     if titleString != '':
       originalTitleString = titleString
       titleString = titleString.replace(' ', '_')
       outputfile.write('

'.encode('utf-8'))
       outputfile.write(textString.encode('utf-8'))
       outputfile.write('

\n'.encode('utf-8'))

   outputfile.write('
'.encode('utf-8'))
   outputfile.close()
   outputfilemenu.close()
   return;

Convert wiki XML to Tiddlywiki import format

Response:
def WIKIXML2TW(inputfilename, outputfilename):
   "Convert Wikimedia XML dump to TiddlyWiki import file"
   inputfile = open(inputfilename, "r")
   xmlinput = unicode(inputfile.read(), errors='ignore')
   outputfilemenu = open(outputfilename + '.menu', "w")
   outputfile = open(outputfilename, "w")
   outputfile.write('<html><head></head><body><div id="storeArea">'.encode('utf-8'))
   startpos = 0

   while startpos > -1:
     titleString = ''
     textString = ''
     startpos = xmlinput.find('<title>', startpos)
     if startpos > -1:
       titleString = xmlinput[startpos+len('<title>'):xmlinput.find('</title>', startpos)]
     if startpos > -1:
       startpos = xmlinput.find('</title>', startpos)
     if startpos > -1:
       startpos = xmlinput.find('<text xml:space="preserve">', startpos)
     if startpos > -1:
       textString = xmlinput[startpos+len('<text xml:space="preserve">'):xmlinput.find('</text>', startpos)]
     if startpos > -1:
       startpos = xmlinput.find('</text>', startpos)
     if titleString != '':
       originalTitleString = titleString
       titleString = titleString.replace(' ', '_')
       outputfile.write('<div title="'.encode('utf-8'))
       outputfile.write(titleString.encode('utf-8'))
       outputfilemenu.write('[[' + originalTitleString + '|' + titleString.encode('utf-8') + ']]\n')
       outputfile.write('" creator="YourName" modifier="YourName" created="201309161317" modified="201309161321" tags="MediaWikiFormat" changecount="1"><pre>'.encode('utf-8'))
       outputfile.write(textString.encode('utf-8'))
       outputfile.write('</pre></div>\n'.encode('utf-8'))

   outputfile.write('</div></body></html>'.encode('utf-8'))
   outputfile.close()
   outputfilemenu.close()
   return;

Prompt: Convert wiki XML to Tiddlywiki import format


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

from functools import wraps
from pollirio import commands

def old_expose(cmd):
    def inner(fn):
        def wrapped(*args, **kwargs):
            commands[cmd] = fn
            fn(*args)
        return wraps(fn)(wrapped)
    return inner

def expose(cmd, args=None):
    def decorator(fn):
        commands[cmd] = {"func":fn, "args":args}
        return fn
    return decorator

def plugin_run(name, *args):
    if name in commands:
        return commands.get(name)["func"](*args)

def check_args(name, bot, ievent):
    if name in commands:
        if commands.get(name)["args"]:
            # TODO: check if we have all the arguments
            print len(ievent.args), commands.get(name)["args"]
            if len(ievent.args) < commands.get(name)["args"]:
                bot.msg(ievent.channel, "%s: %s" % (ievent.nick, commands.get(name)["func"].__doc__))
                return False
            else:
                return True
        else:
            return True
    return False

from lart import *
from polygen import *
#from bts import *
from misc import *

Disable bts plugin for general usage

Disable bts plugin for general usage

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

from functools import wraps
from pollirio import commands

def old_expose(cmd):
    def inner(fn):
        def wrapped(*args, **kwargs):
            commands[cmd] = fn
            fn(*args)
        return wraps(fn)(wrapped)
    return inner

def expose(cmd, args=None):
    def decorator(fn):
        commands[cmd] = {"func":fn, "args":args}
        return fn
    return decorator

def plugin_run(name, *args):
    if name in commands:
        return commands.get(name)["func"](*args)

def check_args(name, bot, ievent):
    if name in commands:
        if commands.get(name)["args"]:
            # TODO: check if we have all the arguments
            print len(ievent.args), commands.get(name)["args"]
            if len(ievent.args) < commands.get(name)["args"]:
                bot.msg(ievent.channel, "%s: %s" % (ievent.nick, commands.get(name)["func"].__doc__))
                return False
            else:
                return True
        else:
            return True
    return False

from lart import *
from polygen import *
#from bts import *
from misc import *

Prompt:
Disable bts plugin for general usage

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

from functools import wraps
from pollirio import commands

def old_expose(cmd):
    def inner(fn):
        def wrapped(*args, **kwargs):
            commands[cmd] = fn
            fn(*args)
        return wraps(fn)(wrapped)
    return inner

def expose(cmd, args=None):
    def decorator(fn):
        commands[cmd] = {"func":fn, "args":args}
        return fn
    return decorator

def plugin_run(name, *args):
    if name in commands:
        return commands.get(name)["func"](*args)

def check_args(name, bot, ievent):
    if name in commands:
        if commands.get(name)["args"]:
            # TODO: check if we have all the arguments
            print len(ievent.args), commands.get(name)["args"]
            if len(ievent.args) < commands.get(name)["args"]:
                bot.msg(ievent.channel, "%s: %s" % (ievent.nick, commands.get(name)["func"].__doc__))
                return False
            else:
                return True
        else:
            return True
    return False

from lart import *
from polygen import *
from bts import *
from misc import *


"""Module docstring.

This serves as a long usage message.
"""
import sys
import getopt

def main():
    # parse command line options
    try:
        opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
    except getopt.error, msg:
        print msg
        print "for help use --help"
        sys.exit(2)
    # process options
    for o, a in opts:
        if o in ("-h", "--help"):
            print __doc__
            sys.exit(0)
    # process arguments
    for arg in args:
        process(arg) # process() is defined elsewhere

if __name__ == "__main__":
    main()

Write a function that print something n times including relatives spaces

Response:
"""Module docstring.

This serves as a long usage message.
"""
import sys
import getopt

def main():
    # parse command line options
    try:
        opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
    except getopt.error, msg:
        print msg
        print "for help use --help"
        sys.exit(2)
    # process options
    for o, a in opts:
        if o in ("-h", "--help"):
            print __doc__
            sys.exit(0)
    # process arguments
    for arg in args:
        process(arg) # process() is defined elsewhere

if __name__ == "__main__":
    main()

Prompt: Write a function that print something n times including relatives spaces


import logging

from mopidy.frontends.base import BaseFrontend
from mopidy.frontends.mpd.dispatcher import MpdDispatcher
from mopidy.frontends.mpd.process import MpdProcess
from mopidy.utils.process import unpickle_connection

logger = logging.getLogger('mopidy.frontends.mpd')

class MpdFrontend(BaseFrontend):
    """
    The MPD frontend.

    **Settings:**

    - :attr:`mopidy.settings.MPD_SERVER_HOSTNAME`
    - :attr:`mopidy.settings.MPD_SERVER_PORT`
    """

    def __init__(self, *args, **kwargs):
        super(MpdFrontend, self).__init__(*args, **kwargs)
        self.process = None
        self.dispatcher = MpdDispatcher(self.backend)

    def start(self):
        """Starts the MPD server."""
        self.process = MpdProcess(self.core_queue)
        self.process.start()

    def destroy(self):
        """Destroys the MPD server."""
        self.process.destroy()

    def process_message(self, message):
        """
        Processes messages with the MPD frontend as destination.

        :param message: the message
        :type message: dict
        """
        assert message['to'] == 'frontend', \
            u'Message recipient must be "frontend".'
        if message['command'] == 'mpd_request':
            response = self.dispatcher.handle_request(message['request'])
            connection = unpickle_connection(message['reply_to'])
            connection.send(response)
        else:
            logger.warning(u'Cannot handle message: %s', message)

Response:
import logging

from mopidy.frontends.base import BaseFrontend
from mopidy.frontends.mpd.dispatcher import MpdDispatcher
from mopidy.frontends.mpd.process import MpdProcess
from mopidy.utils.process import unpickle_connection

logger = logging.getLogger('mopidy.frontends.mpd')

class MpdFrontend(BaseFrontend):
    """
    The MPD frontend.

    **Settings:**

    - :attr:`mopidy.settings.MPD_SERVER_HOSTNAME`
    - :attr:`mopidy.settings.MPD_SERVER_PORT`
    """

    def __init__(self, *args, **kwargs):
        super(MpdFrontend, self).__init__(*args, **kwargs)
        self.process = None
        self.dispatcher = MpdDispatcher(self.backend)

    def start(self):
        """Starts the MPD server."""
        self.process = MpdProcess(self.core_queue)
        self.process.start()

    def destroy(self):
        """Destroys the MPD server."""
        self.process.destroy()

    def process_message(self, message):
        """
        Processes messages with the MPD frontend as destination.

        :param message: the message
        :type message: dict
        """
        assert message['to'] == 'frontend', \
            u'Message recipient must be "frontend".'
        if message['command'] == 'mpd_request':
            response = self.dispatcher.handle_request(message['request'])
            connection = unpickle_connection(message['reply_to'])
            connection.send(response)
        else:
            logger.warning(u'Cannot handle message: %s', message)

Prompt:
Make MpdFrontend a subclass of BaseFrontend

import logging

from mopidy.frontends.mpd.dispatcher import MpdDispatcher
from mopidy.frontends.mpd.process import MpdProcess
from mopidy.utils.process import unpickle_connection

logger = logging.getLogger('mopidy.frontends.mpd')

class MpdFrontend(object):
    """
    The MPD frontend.

    **Settings:**

    - :attr:`mopidy.settings.MPD_SERVER_HOSTNAME`
    - :attr:`mopidy.settings.MPD_SERVER_PORT`
    """

    def __init__(self, core_queue, backend):
        self.core_queue = core_queue
        self.process = None
        self.dispatcher = MpdDispatcher(backend)

    def start(self):
        """Starts the MPD server."""
        self.process = MpdProcess(self.core_queue)
        self.process.start()

    def process_message(self, message):
        """
        Processes messages with the MPD frontend as destination.

        :param message: the message
        :type message: dict
        """
        assert message['to'] == 'frontend', \
            u'Message recipient must be "frontend".'
        if message['command'] == 'mpd_request':
            response = self.dispatcher.handle_request(message['request'])
            connection = unpickle_connection(message['reply_to'])
            connection.send(response)
        else:
            logger.warning(u'Cannot handle message: %s', message)


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

import argparse
import numpy as np
import urllib, urllib2
import cStringIO
import sys

import zlib
import libtiff

def main():

  parser = argparse.ArgumentParser(description='Post a file as a tiff')
  parser.add_argument('baseurl', action="store" )
  parser.add_argument('token', action="store" )
  parser.add_argument('channel', action="store" )
  parser.add_argument('filename', action="store" )
  parser.add_argument('resolution', action="store", type=int, default=0 )
  parser.add_argument('xoffset', action="store", type=int, default=0 )
  parser.add_argument('yoffset', action="store", type=int, default=0)
  parser.add_argument('zoffset', action="store", type=int, default=0)

  result = parser.parse_args()

  url = 'http://%s/ca/%s/%s/tiff/%s/%s/%s/%s/' % ( result.baseurl, result.token, result.channel, result.resolution, result.xoffset, result.yoffset, result.zoffset )

  # open the file name as a tiff file
  fh = open ( result.filename )

  # Get cube in question
  try:
    f = urllib2.urlopen ( url, fh.read() )
  except urllib2.URLError, e:
    print "Failed %s.  Exception %s." % (url,e)
    sys.exit(-1)

if __name__ == "__main__":
  main()

Migrate this version to new workstation.

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

import argparse
import numpy as np
import urllib, urllib2
import cStringIO
import sys

import zlib
import libtiff

def main():

  parser = argparse.ArgumentParser(description='Post a file as a tiff')
  parser.add_argument('baseurl', action="store" )
  parser.add_argument('token', action="store" )
  parser.add_argument('channel', action="store" )
  parser.add_argument('filename', action="store" )
  parser.add_argument('resolution', action="store", type=int, default=0 )
  parser.add_argument('xoffset', action="store", type=int, default=0 )
  parser.add_argument('yoffset', action="store", type=int, default=0)
  parser.add_argument('zoffset', action="store", type=int, default=0)

  result = parser.parse_args()

  url = 'http://%s/ca/%s/%s/tiff/%s/%s/%s/%s/' % ( result.baseurl, result.token, result.channel, result.resolution, result.xoffset, result.yoffset, result.zoffset )

  # open the file name as a tiff file
  fh = open ( result.filename )

  # Get cube in question
  try:
    f = urllib2.urlopen ( url, fh.read() )
  except urllib2.URLError, e:
    print "Failed %s.  Exception %s." % (url,e)
    sys.exit(-1)

if __name__ == "__main__":
  main()

Prompt: Migrate this version to new workstation.


import json
import ijson

class TimeSeriesJSON:

    def __init__(self, f=None, fn=None, tag='item'):
        """
        :param f: file object (_io.TextIOWrapper)
        :param fn: file name as a string
        :param tag: tag for dividing json items
            default value is 'item' because this value is default in ijson
        """
        if f is not None:
            self.__type = 'file'
            self.__file = f
        elif fn is not None:
            self.__type = 'file_name'
            self.__file_name = fn
            self.__file = open(fn)

        self.__items = self.parse_json_items(tag)

    def parse_json_items(self, tag, limit=0):
        self.__items = []
        self.__file.seek(0)
        cnt = 0

        objs = ijson.items(self.__file, tag)
        for obj in objs:
            item = json.dumps(obj,
                              sort_keys=True,
                              indent=4,
                              ensure_ascii=True)
            self.__items.append(item)
            cnt += 1
            if limit != 0 and cnt >= limit:
                break

        return self.__items

Response:
import json
import ijson

class TimeSeriesJSON:

    def __init__(self, f=None, fn=None, tag='item'):
        """
        :param f: file object (_io.TextIOWrapper)
        :param fn: file name as a string
        :param tag: tag for dividing json items
            default value is 'item' because this value is default in ijson
        """
        if f is not None:
            self.__type = 'file'
            self.__file = f
        elif fn is not None:
            self.__type = 'file_name'
            self.__file_name = fn
            self.__file = open(fn)

        self.__items = self.parse_json_items(tag)

    def parse_json_items(self, tag, limit=0):
        self.__items = []
        self.__file.seek(0)
        cnt = 0

        objs = ijson.items(self.__file, tag)
        for obj in objs:
            item = json.dumps(obj,
                              sort_keys=True,
                              indent=4,
                              ensure_ascii=True)
            self.__items.append(item)
            cnt += 1
            if limit != 0 and cnt >= limit:
                break

        return self.__items

Prompt:
Remove unnecessary backslashes in parse_json_items

import json
import ijson

class TimeSeriesJSON:

    def __init__(self, f=None, fn=None, tag='item'):
        """
        :param f: file object (_io.TextIOWrapper)
        :param fn: file name as a string
        :param tag: tag for dividing json items
            default value is 'item' because this value is default in ijson
        """
        if f is not None:
            self.__type = 'file'
            self.__file = f
        elif fn is not None:
            self.__type = 'file_name'
            self.__file_name = fn
            self.__file = open(fn)

        self.__items = self.parse_json_items(tag)

    def parse_json_items(self, tag, limit=0):
        self.__items = []
        self.__file.seek(0)
        cnt = 0

        objs = ijson.items(self.__file, tag)
        for obj in objs:
            item = json.dumps(obj, \
                              sort_keys=True, \
                              indent=4, \
                              ensure_ascii=True)
            self.__items.append(item)
            cnt += 1
            if limit != 0 and cnt >= limit:
                break

        return self.__items


from django import forms
from django.contrib import admin
from django.utils.importlib import import_module

from setmagic import settings
from setmagic.models import Setting

_denied = lambda *args: False

class SetMagicAdmin(admin.ModelAdmin):
    list_display = 'label', 'current_value',
    list_editable = 'current_value',
    list_display_links = None

    has_add_permission = _denied
    has_delete_permission = _denied

    # Make all fields read-only at the change form
    def get_readonly_fields(self, *args, **kwargs):
        return self.opts.get_all_field_names()

    def changelist_view(self, *args, **kwargs):
        settings._sync()
        return super(SetMagicAdmin, self).changelist_view(*args, **kwargs)

    def get_queryset(self, request):
        return Setting.objects.filter(name__in=settings.defs)

    def get_changelist_form(self, *args, **kwargs):
        class Form(forms.ModelForm):

            class Meta:
                fields = self.list_editable

            def __init__(self, *args, **kwargs):
                super(Form, self).__init__(*args, **kwargs)

                # Do nothing for empty forms
                if not self.instance.pk:
                    return

                # Set a custom field
                custom_field = settings.defs[self.instance.name].get('field')
                if custom_field:
                    if isinstance(custom_field, str):
                        module, name = custom_field.rsplit('.', 1)
                        custom_field = getattr(import_module(module), name)()
                    self.fields['current_value'] = custom_field

        return Form

admin.site.register(Setting, SetMagicAdmin)

Response:
from django import forms
from django.contrib import admin
from django.utils.importlib import import_module

from setmagic import settings
from setmagic.models import Setting

_denied = lambda *args: False

class SetMagicAdmin(admin.ModelAdmin):
    list_display = 'label', 'current_value',
    list_editable = 'current_value',
    list_display_links = None

    has_add_permission = _denied
    has_delete_permission = _denied

    # Make all fields read-only at the change form
    def get_readonly_fields(self, *args, **kwargs):
        return self.opts.get_all_field_names()

    def changelist_view(self, *args, **kwargs):
        settings._sync()
        return super(SetMagicAdmin, self).changelist_view(*args, **kwargs)

    def get_queryset(self, request):
        return Setting.objects.filter(name__in=settings.defs)

    def get_changelist_form(self, *args, **kwargs):
        class Form(forms.ModelForm):

            class Meta:
                fields = self.list_editable

            def __init__(self, *args, **kwargs):
                super(Form, self).__init__(*args, **kwargs)

                # Do nothing for empty forms
                if not self.instance.pk:
                    return

                # Set a custom field
                custom_field = settings.defs[self.instance.name].get('field')
                if custom_field:
                    if isinstance(custom_field, str):
                        module, name = custom_field.rsplit('.', 1)
                        custom_field = getattr(import_module(module), name)()
                    self.fields['current_value'] = custom_field

        return Form

admin.site.register(Setting, SetMagicAdmin)

Prompt:
Use importlib to load custom fields by str

from django import forms
from django.contrib import admin

from setmagic import settings
from setmagic.models import Setting

_denied = lambda *args: False

class SetMagicAdmin(admin.ModelAdmin):
    list_display = 'label', 'current_value',
    list_editable = 'current_value',
    list_display_links = None

    has_add_permission = _denied
    has_delete_permission = _denied

    # Make all fields read-only at the change form
    def get_readonly_fields(self, *args, **kwargs):
        return self.opts.get_all_field_names()

    def changelist_view(self, *args, **kwargs):
        settings._sync()
        return super(SetMagicAdmin, self).changelist_view(*args, **kwargs)

    def get_queryset(self, request):
        return Setting.objects.filter(name__in=settings.defs)

    def get_changelist_form(self, *args, **kwargs):
        class Form(forms.ModelForm):

            class Meta:
                fields = self.list_editable

            def __init__(self, *args, **kwargs):
                super(Form, self).__init__(*args, **kwargs)

                # Do nothing for empty forms
                if not self.instance.pk:
                    return

                # Set a custom field
                custom_field = settings.defs[self.instance.name].get('field')
                if custom_field:
                    self.fields['current_value'] = custom_field

        return Form

admin.site.register(Setting, SetMagicAdmin)


"""text to JSON

Revision ID: 151b2f642877
Revises: ac115763654
Create Date: 2015-06-12 14:40:56.956657

"""

# revision identifiers, used by Alembic.
revision = '151b2f642877'
down_revision = 'ac115763654'

from alembic import op
import sqlalchemy as sa

def upgrade():
    query = 'ALTER TABLE project ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)
    query = 'ALTER TABLE "user" ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)
    query = 'ALTER TABLE task ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)
    query = 'ALTER TABLE task_run ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)

def downgrade():
    query = 'ALTER TABLE project ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)
    query = 'ALTER TABLE "user" ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)
    query = 'ALTER TABLE task ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)
    query = 'ALTER TABLE task_run ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)

Fix alembic revision after merge master

Fix alembic revision after merge master

Response:
"""text to JSON

Revision ID: 151b2f642877
Revises: ac115763654
Create Date: 2015-06-12 14:40:56.956657

"""

# revision identifiers, used by Alembic.
revision = '151b2f642877'
down_revision = 'ac115763654'

from alembic import op
import sqlalchemy as sa

def upgrade():
    query = 'ALTER TABLE project ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)
    query = 'ALTER TABLE "user" ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)
    query = 'ALTER TABLE task ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)
    query = 'ALTER TABLE task_run ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)

def downgrade():
    query = 'ALTER TABLE project ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)
    query = 'ALTER TABLE "user" ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)
    query = 'ALTER TABLE task ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)
    query = 'ALTER TABLE task_run ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)

Prompt:
Fix alembic revision after merge master

"""text to JSON

Revision ID: 151b2f642877
Revises: aee7291c81
Create Date: 2015-06-12 14:40:56.956657

"""

# revision identifiers, used by Alembic.
revision = '151b2f642877'
down_revision = 'aee7291c81'

from alembic import op
import sqlalchemy as sa

def upgrade():
    query = 'ALTER TABLE project ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)
    query = 'ALTER TABLE "user" ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)
    query = 'ALTER TABLE task ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)
    query = 'ALTER TABLE task_run ALTER COLUMN info TYPE JSON USING info::JSON;'
    op.execute(query)

def downgrade():
    query = 'ALTER TABLE project ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)
    query = 'ALTER TABLE "user" ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)
    query = 'ALTER TABLE task ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)
    query = 'ALTER TABLE task_run ALTER COLUMN info TYPE TEXT USING info::TEXT;'
    op.execute(query)


from utilities.simulation_utilities import check_inputs

import pytest
import numpy as np

@pytest.mark.parametrize("input,expected", [
    (None, np.ndarray([0])),
    ([0], np.array([0])),
    (1, np.array([1])),
    (range(5), np.array([0,1,2,3,4]))
])
def test_check_inputs(input, expected):
    assert np.allclose(check_inputs(input), expected)

Add a test for check_inputs.

Add a test for check_inputs.

Response:
from utilities.simulation_utilities import check_inputs

import pytest
import numpy as np

@pytest.mark.parametrize("input,expected", [
    (None, np.ndarray([0])),
    ([0], np.array([0])),
    (1, np.array([1])),
    (range(5), np.array([0,1,2,3,4]))
])
def test_check_inputs(input, expected):
    assert np.allclose(check_inputs(input), expected)

Prompt: Add a test for check_inputs.


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

def solve():
    L, N = input().split(' ')
    N = int(N)
    result = ''

    while N > 0:
        N -= 1
        result = L[N % len(L)] + result
        N = int(N / len(L))

    return result

def main():
    T = int(input())
    for i in range(T):
        print('Case #{i}: {answer}'.format(i=i+1, answer=solve()))

if __name__ == '__main__':
    main()

Add solution v2 for Labelmaker.

Add solution v2 for Labelmaker.

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

def solve():
    L, N = input().split(' ')
    N = int(N)
    result = ''

    while N > 0:
        N -= 1
        result = L[N % len(L)] + result
        N = int(N / len(L))

    return result

def main():
    T = int(input())
    for i in range(T):
        print('Case #{i}: {answer}'.format(i=i+1, answer=solve()))

if __name__ == '__main__':
    main()

Prompt: Add solution v2 for Labelmaker.


#!/usr/bin/env python
# 05.12.2005, c
from __future__ import division, print_function, absolute_import
import sys

def configuration(parent_package='',top_path=None):
    from numpy.distutils.misc_util import Configuration
    from numpy.distutils.system_info import get_info, dict_append

    config = Configuration('umfpack', parent_package, top_path)
    config.add_data_dir('tests')

    umf_info = get_info('umfpack', notfound_action=1)

    ## The following addition is needed when linking against a umfpack built
    ## from the latest SparseSuite. Not (strictly) needed when linking against
    ## the version in the ubuntu repositories.
    if not sys.platform == 'darwin':
        umf_info['libraries'].insert(0, 'rt')

    umfpack_i_file = config.paths('umfpack.i')[0]

    def umfpack_i(ext, build_dir):
        if umf_info:
            return umfpack_i_file

    blas_info = get_info('blas_opt')
    build_info = {}
    dict_append(build_info, **umf_info)
    dict_append(build_info, **blas_info)

    config.add_extension('__umfpack',
                         sources=[umfpack_i],
                         depends=['umfpack.i'],
                         **build_info)

    return config

if __name__ == "__main__":
    from numpy.distutils.core import setup
    setup(**configuration(top_path='').todict())

Response:
#!/usr/bin/env python
# 05.12.2005, c
from __future__ import division, print_function, absolute_import
import sys

def configuration(parent_package='',top_path=None):
    from numpy.distutils.misc_util import Configuration
    from numpy.distutils.system_info import get_info, dict_append

    config = Configuration('umfpack', parent_package, top_path)
    config.add_data_dir('tests')

    umf_info = get_info('umfpack', notfound_action=1)

    ## The following addition is needed when linking against a umfpack built
    ## from the latest SparseSuite. Not (strictly) needed when linking against
    ## the version in the ubuntu repositories.
    if not sys.platform == 'darwin':
        umf_info['libraries'].insert(0, 'rt')

    umfpack_i_file = config.paths('umfpack.i')[0]

    def umfpack_i(ext, build_dir):
        if umf_info:
            return umfpack_i_file

    blas_info = get_info('blas_opt')
    build_info = {}
    dict_append(build_info, **umf_info)
    dict_append(build_info, **blas_info)

    config.add_extension('__umfpack',
                         sources=[umfpack_i],
                         depends=['umfpack.i'],
                         **build_info)

    return config

if __name__ == "__main__":
    from numpy.distutils.core import setup
    setup(**configuration(top_path='').todict())

Prompt:
Add handling for building scikit-umfpack on the Mac, which doesn't have the librt file added to the umfpack dependencies.

#!/usr/bin/env python
# 05.12.2005, c
from __future__ import division, print_function, absolute_import

def configuration(parent_package='',top_path=None):
    from numpy.distutils.misc_util import Configuration
    from numpy.distutils.system_info import get_info, dict_append

    config = Configuration('umfpack', parent_package, top_path)
    config.add_data_dir('tests')

    umf_info = get_info('umfpack', notfound_action=1)

    ## The following addition is needed when linking against a umfpack built
    ## from the latest SparseSuite. Not (strictly) needed when linking against
    ## the version in the ubuntu repositories.
    umf_info['libraries'].insert(0, 'rt')

    umfpack_i_file = config.paths('umfpack.i')[0]

    def umfpack_i(ext, build_dir):
        if umf_info:
            return umfpack_i_file

    blas_info = get_info('blas_opt')
    build_info = {}
    dict_append(build_info, **umf_info)
    dict_append(build_info, **blas_info)

    config.add_extension('__umfpack',
                         sources=[umfpack_i],
                         depends=['umfpack.i'],
                         **build_info)

    return config

if __name__ == "__main__":
    from numpy.distutils.core import setup
    setup(**configuration(top_path='').todict())


import unittest
from frijoles import app

class FrijolesAPITestCase(unittest.TestCase):
    def setUp(self):
        self.app = app.test_client()

    def test_basic(self):
        res = self.app.get('/api/v1/')
        self.assertEqual(res.status_code, 200)

Fix wrong test case name

Fix wrong test case name

Response:
import unittest
from frijoles import app

class FrijolesAPITestCase(unittest.TestCase):
    def setUp(self):
        self.app = app.test_client()

    def test_basic(self):
        res = self.app.get('/api/v1/')
        self.assertEqual(res.status_code, 200)

Prompt:
Fix wrong test case name

import unittest
from frijoles import app

class TamalesAPITestCase(unittest.TestCase):
    def setUp(self):
        self.app = app.test_client()

    def test_basic(self):
        res = self.app.get('/api/v1/')
        self.assertEqual(res.status_code, 200)


# -*- coding: utf-8 -*-
"""
Unit tests for pytils
"""
__all__ = ["test_numeral", "test_dt", "test_translit", "test_utils", "test_typo"]

import unittest
import sys

def get_django_suite():
    try:
        import django
    except ImportError:
        return unittest.TestSuite()

    import pytils.test.templatetags
    return pytils.test.templatetags.get_suite()

def get_suite():
    """Return TestSuite for all unit-test of pytils"""
    suite = unittest.TestSuite()
    for module_name in __all__:
        imported_module = __import__("pytils.test."+module_name,
                                       globals(),
                                       locals(),
                                       ["pytils.test"])

        loader = unittest.defaultTestLoader
        suite.addTest(loader.loadTestsFromModule(imported_module))
        suite.addTest(get_django_suite())

    return suite

def run_tests_from_module(module, verbosity=1):
    """Run unit-tests for single module"""
    suite = unittest.TestSuite()
    loader = unittest.defaultTestLoader
    suite.addTest(loader.loadTestsFromModule(module))
    unittest.TextTestRunner(verbosity=verbosity).run(suite)

def run(verbosity=1):
    """Run all unit-test of pytils"""
    suite = get_suite()
    res = unittest.TextTestRunner(verbosity=verbosity).run(suite)
    if res.errors or res.failures:
        sys.exit(1)

if __name__ == '__main__':
    run(2)

Response:
# -*- coding: utf-8 -*-
"""
Unit tests for pytils
"""
__all__ = ["test_numeral", "test_dt", "test_translit", "test_utils", "test_typo"]

import unittest
import sys

def get_django_suite():
    try:
        import django
    except ImportError:
        return unittest.TestSuite()

    import pytils.test.templatetags
    return pytils.test.templatetags.get_suite()

def get_suite():
    """Return TestSuite for all unit-test of pytils"""
    suite = unittest.TestSuite()
    for module_name in __all__:
        imported_module = __import__("pytils.test."+module_name,
                                       globals(),
                                       locals(),
                                       ["pytils.test"])

        loader = unittest.defaultTestLoader
        suite.addTest(loader.loadTestsFromModule(imported_module))
        suite.addTest(get_django_suite())

    return suite

def run_tests_from_module(module, verbosity=1):
    """Run unit-tests for single module"""
    suite = unittest.TestSuite()
    loader = unittest.defaultTestLoader
    suite.addTest(loader.loadTestsFromModule(module))
    unittest.TextTestRunner(verbosity=verbosity).run(suite)

def run(verbosity=1):
    """Run all unit-test of pytils"""
    suite = get_suite()
    res = unittest.TextTestRunner(verbosity=verbosity).run(suite)
    if res.errors or res.failures:
        sys.exit(1)

if __name__ == '__main__':
    run(2)

Prompt:
Py3: Exit with non-0 status if there are failed tests or errors.

# -*- coding: utf-8 -*-
"""
Unit tests for pytils
"""
__all__ = ["test_numeral", "test_dt", "test_translit", "test_utils", "test_typo"]

import unittest

def get_django_suite():
    try:
        import django
    except ImportError:
        return unittest.TestSuite()

    import pytils.test.templatetags
    return pytils.test.templatetags.get_suite()

def get_suite():
    """Return TestSuite for all unit-test of pytils"""
    suite = unittest.TestSuite()
    for module_name in __all__:
        imported_module = __import__("pytils.test."+module_name,
                                       globals(),
                                       locals(),
                                       ["pytils.test"])

        loader = unittest.defaultTestLoader
        suite.addTest(loader.loadTestsFromModule(imported_module))
        suite.addTest(get_django_suite())

    return suite

def run_tests_from_module(module, verbosity=1):
    """Run unit-tests for single module"""
    suite = unittest.TestSuite()
    loader = unittest.defaultTestLoader
    suite.addTest(loader.loadTestsFromModule(module))
    unittest.TextTestRunner(verbosity=verbosity).run(suite)

def run(verbosity=1):
    """Run all unit-test of pytils"""
    suite = get_suite()
    unittest.TextTestRunner(verbosity=verbosity).run(suite)

if __name__ == '__main__':
    run(2)


Response:
from django.db import models
from django.db.models.signals import post_save
import mimetypes
import wellknown

#
# create default host-meta handler
#

from wellknown.resources import HostMeta
wellknown.register('host-meta', handler=HostMeta(), content_type='application/xrd+xml')

#
# resource model
#

class Resource(models.Model):
    path = models.CharField(max_length=128)
    content = models.TextField(blank=True)
    content_type = models.CharField(max_length=128, blank=True)

    class Meta:
        ordering = ('path',)

    def __unicode__(self):
        return self.path

    def save(self, **kwargs):
        self.path = self.path.strip('/')
        if not self.content_type:
            self.content_type = mimetypes.guess_type(self.path)[0] or 'text/plain'
        super(Resource, self).save(**kwargs)

#
# update resources when models are saved
#

def save_handler(sender, **kwargs):
    reg = kwargs['instance']
    wellknown.register(
        reg.path,
        content=reg.content,
        content_type=reg.content_type,
        update=True
    )

post_save.connect(save_handler, sender=Resource)

Prompt:
Remove code that was causing a problem running syncdb. Code seems to be redundant anyway.

from django.db import models
from django.db.models.signals import post_save
import mimetypes
import wellknown

#
# create default host-meta handler
#

from wellknown.resources import HostMeta
wellknown.register('host-meta', handler=HostMeta(), content_type='application/xrd+xml')

#
# resource model
#

class Resource(models.Model):
    path = models.CharField(max_length=128)
    content = models.TextField(blank=True)
    content_type = models.CharField(max_length=128, blank=True)

    class Meta:
        ordering = ('path',)

    def __unicode__(self):
        return self.path

    def save(self, **kwargs):
        self.path = self.path.strip('/')
        if not self.content_type:
            self.content_type = mimetypes.guess_type(self.path)[0] or 'text/plain'
        super(Resource, self).save(**kwargs)

#
# update resources when models are saved
#

def save_handler(sender, **kwargs):
    reg = kwargs['instance']
    wellknown.register(
        reg.path,
        content=reg.content,
        content_type=reg.content_type,
        update=True
    )

post_save.connect(save_handler, sender=Resource)

#
# cache resources
#

for res in Resource.objects.all():
    wellknown.register(res.path, content=res.content, content_type=res.content_type)


import os
import unittest

import numpy as np
import chainer
from chainer import optimizers

import q_function
import nstep_q_learning
import async
import simple_abc
import random_seed
import replay_buffer
from simple_abc import ABC

class TestNSQ(unittest.TestCase):

    def setUp(self):
        pass

    def test_abc(self):
        self._test_abc(1)
        self._test_abc(5)

    def _test_abc(self, t_max):

        nproc = 8

        def agent_func():
            n_actions = 3
            q_func = q_function.FCSIQFunction(1, n_actions, 10, 2)
            opt = optimizers.RMSprop(1e-3, eps=1e-2)
            opt.setup(q_func)
            return nstep_q_learning.NStepQLearning(q_func, opt, t_max,
                                                   0.9, 0.1,
                                                   i_target=10)

        def env_func():
            return simple_abc.ABC()

        def run_func(agent, env):
            total_r = 0
            episode_r = 0

            for i in xrange(5000):

                total_r += env.reward
                episode_r += env.reward

                action = agent.act(env.state, env.reward, env.is_terminal)

                if env.is_terminal:
                    print 'i:{} episode_r:{}'.format(i, episode_r)
                    episode_r = 0
                    env.initialize()
                else:
                    env.receive_action(action)

            print 'pid:{}, total_r:{}'.format(os.getpid(), total_r)

            return agent

        # Train
        final_agent = async.run_async(nproc, agent_func, env_func, run_func)

        # Test
        env = env_func()
        total_r = env.reward
        while not env.is_terminal:
            action = final_agent.q_function.sample_greedily_with_value(
                env.state.reshape((1,) + env.state.shape))[0][0]
            print 'state:', env.state, 'action:', action
            env.receive_action(action)
            total_r += env.reward
        self.assertAlmostEqual(total_r, 1)

Add a ABC test for n-step Q-learning

Add a ABC test for n-step Q-learning

Response:
import os
import unittest

import numpy as np
import chainer
from chainer import optimizers

import q_function
import nstep_q_learning
import async
import simple_abc
import random_seed
import replay_buffer
from simple_abc import ABC

class TestNSQ(unittest.TestCase):

    def setUp(self):
        pass

    def test_abc(self):
        self._test_abc(1)
        self._test_abc(5)

    def _test_abc(self, t_max):

        nproc = 8

        def agent_func():
            n_actions = 3
            q_func = q_function.FCSIQFunction(1, n_actions, 10, 2)
            opt = optimizers.RMSprop(1e-3, eps=1e-2)
            opt.setup(q_func)
            return nstep_q_learning.NStepQLearning(q_func, opt, t_max,
                                                   0.9, 0.1,
                                                   i_target=10)

        def env_func():
            return simple_abc.ABC()

        def run_func(agent, env):
            total_r = 0
            episode_r = 0

            for i in xrange(5000):

                total_r += env.reward
                episode_r += env.reward

                action = agent.act(env.state, env.reward, env.is_terminal)

                if env.is_terminal:
                    print 'i:{} episode_r:{}'.format(i, episode_r)
                    episode_r = 0
                    env.initialize()
                else:
                    env.receive_action(action)

            print 'pid:{}, total_r:{}'.format(os.getpid(), total_r)

            return agent

        # Train
        final_agent = async.run_async(nproc, agent_func, env_func, run_func)

        # Test
        env = env_func()
        total_r = env.reward
        while not env.is_terminal:
            action = final_agent.q_function.sample_greedily_with_value(
                env.state.reshape((1,) + env.state.shape))[0][0]
            print 'state:', env.state, 'action:', action
            env.receive_action(action)
            total_r += env.reward
        self.assertAlmostEqual(total_r, 1)

Prompt: Add a ABC test for n-step Q-learning


from __future__ import absolute_import, print_function, unicode_literals
from builtins import dict, str
import logging
import xml.etree.ElementTree as ET
from indra.util import UnicodeXMLTreeBuilder as UTB
from indra.sparser.processor import SparserProcessor

logger = logging.getLogger('sparser')

def process_xml(xml_str):
    try:
        tree = ET.XML(xml_str, parser=UTB())
    except ET.ParseError as e:
        logger.error('Could not parse XML string')
        logger.error(e)
        return None
    sp = _process_elementtree(tree)
    return sp

def _process_elementtree(tree):
    sp = SparserProcessor(tree)
    sp.get_modifications()
    sp.get_activations()
    return sp

Print XML parse errors in Sparser API

Print XML parse errors in Sparser API

Response:
from __future__ import absolute_import, print_function, unicode_literals
from builtins import dict, str
import logging
import xml.etree.ElementTree as ET
from indra.util import UnicodeXMLTreeBuilder as UTB
from indra.sparser.processor import SparserProcessor

logger = logging.getLogger('sparser')

def process_xml(xml_str):
    try:
        tree = ET.XML(xml_str, parser=UTB())
    except ET.ParseError as e:
        logger.error('Could not parse XML string')
        logger.error(e)
        return None
    sp = _process_elementtree(tree)
    return sp

def _process_elementtree(tree):
    sp = SparserProcessor(tree)
    sp.get_modifications()
    sp.get_activations()
    return sp

Prompt:
Print XML parse errors in Sparser API

from __future__ import absolute_import, print_function, unicode_literals
from builtins import dict, str
import logging
import xml.etree.ElementTree as ET
from indra.util import UnicodeXMLTreeBuilder as UTB
from indra.sparser.processor import SparserProcessor

logger = logging.getLogger('sparser')

def process_xml(xml_str):
    try:
        tree = ET.XML(xml_str, parser=UTB())
    except ET.ParseError:
        logger.error('Could not parse XML string')
        return None
    sp = _process_elementtree(tree)
    return sp

def _process_elementtree(tree):
    sp = SparserProcessor(tree)
    sp.get_modifications()
    sp.get_activations()
    return sp


import os.path

VERSION = (1, 0, 1, 'stable')

def get_version():
    """
    Return the version as a string. If this is flagged as a development
    release and mercurial can be loaded the specifics about the changeset
    will be appended to the version string.
    """
    if 'dev' in VERSION:
        try:
            from mercurial import hg, ui

            repo_path = os.path.join(os.path.dirname(__file__), '..')
            repo = hg.repository(ui.ui(), repo_path)
            ctx = repo['tip']
            build_info = 'dev %s %s:%s' % (ctx.branch(), ctx.rev(), str(ctx))
        except:
            # mercurial module missing or repository not found
            build_info = 'dev-unknown'
    v = VERSION[:VERSION.index('dev')] + (build_info,)
    return '.'.join(map(str, v))

Response:
import os.path

VERSION = (1, 0, 1, 'stable')

def get_version():
    """
    Return the version as a string. If this is flagged as a development
    release and mercurial can be loaded the specifics about the changeset
    will be appended to the version string.
    """
    if 'dev' in VERSION:
        try:
            from mercurial import hg, ui

            repo_path = os.path.join(os.path.dirname(__file__), '..')
            repo = hg.repository(ui.ui(), repo_path)
            ctx = repo['tip']
            build_info = 'dev %s %s:%s' % (ctx.branch(), ctx.rev(), str(ctx))
        except:
            # mercurial module missing or repository not found
            build_info = 'dev-unknown'
    v = VERSION[:VERSION.index('dev')] + (build_info,)
    return '.'.join(map(str, v))

Prompt:
Bump version to 1.0.1 for unit test fix.

import os.path

VERSION = (1, 0, 0, 'stable')

def get_version():
    """
    Return the version as a string. If this is flagged as a development
    release and mercurial can be loaded the specifics about the changeset
    will be appended to the version string.
    """
    if 'dev' in VERSION:
        try:
            from mercurial import hg, ui

            repo_path = os.path.join(os.path.dirname(__file__), '..')
            repo = hg.repository(ui.ui(), repo_path)
            ctx = repo['tip']
            build_info = 'dev %s %s:%s' % (ctx.branch(), ctx.rev(), str(ctx))
        except:
            # mercurial module missing or repository not found
            build_info = 'dev-unknown'
    v = VERSION[:VERSION.index('dev')] + (build_info,)
    return '.'.join(map(str, v))


from __future__ import absolute_import, unicode_literals
from .api.psd_image import PSDImage
from .api.composer import compose

Include compose in the top level

Include compose in the top level

Response:
from __future__ import absolute_import, unicode_literals
from .api.psd_image import PSDImage
from .api.composer import compose

Prompt:
Include compose in the top level

from __future__ import absolute_import, unicode_literals
from .api.psd_image import PSDImage


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# (c) 2012 Urban Airship and Contributors

import os
import sys
import mithril

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

if sys.argv[-1] == 'publish':
    os.system('python setup.py sdist upload')
    sys.exit()

required = [
    'netaddr==0.7.6',
]
packages = [
    'mithril',
    'mithril.tests',
    'mithril.migrations',
]

setup(
    name='django-mithril',
    version='%d.%d.%d' % mithril.__version__,
    description='IP Whitelisting for Django',
    long_description=open('README.md').read(),
    author='Chris Dickinson',
    author_email='chrisdickinson@urbanairship.com',
    url='http://urbanairship.github.com/django-mithril/',
    packages=packages,
    package_data={'': ['LICENSE']},
    include_package_data=True,
    install_requires=required,
    license=open("LICENSE").read(),
    zip_safe=False,
    classifiers=(
        'Development Status :: 1 - Alpha',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
    ),
)

Response:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# (c) 2012 Urban Airship and Contributors

import os
import sys
import mithril

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

if sys.argv[-1] == 'publish':
    os.system('python setup.py sdist upload')
    sys.exit()

required = [
    'netaddr==0.7.6',
]
packages = [
    'mithril',
    'mithril.tests',
    'mithril.migrations',
]

setup(
    name='django-mithril',
    version='%d.%d.%d' % mithril.__version__,
    description='IP Whitelisting for Django',
    long_description=open('README.md').read(),
    author='Chris Dickinson',
    author_email='chrisdickinson@urbanairship.com',
    url='http://urbanairship.github.com/django-mithril/',
    packages=packages,
    package_data={'': ['LICENSE']},
    include_package_data=True,
    install_requires=required,
    license=open("LICENSE").read(),
    zip_safe=False,
    classifiers=(
        'Development Status :: 1 - Alpha',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
    ),
)

Prompt:
Make sure to include migrations! :hurtrealbad:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# (c) 2012 Urban Airship and Contributors

import os
import sys
import mithril

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

if sys.argv[-1] == 'publish':
    os.system('python setup.py sdist upload')
    sys.exit()

required = [
    'netaddr==0.7.6',
]
packages = [
    'mithril',
    'mithril.tests',
]

setup(
    name='django-mithril',
    version='%d.%d.%d' % mithril.__version__,
    description='IP Whitelisting for Django',
    long_description=open('README.md').read(),
    author='Chris Dickinson',
    author_email='chrisdickinson@urbanairship.com',
    url='http://urbanairship.github.com/django-mithril/',
    packages=packages,
    package_data={'': ['LICENSE']},
    include_package_data=True,
    install_requires=required,
    license=open("LICENSE").read(),
    zip_safe=False,
    classifiers=(
        'Development Status :: 1 - Alpha',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
    ),
)