from datetime import datetime
from optparse import OptionParser
from urllib2 import urlopen

from BeautifulSoup import BeautifulSoup

if __name__ == '__main__':
    usage = "%prog <USERNAME> <WEB SERVER>"
    parser = OptionParser(usage=usage)
    opts,args = parser.parse_args()
    if len(args) != 2:
        parser.error("All arguments are required.")

    username,server = args
    if server[:7] != "http://":
        server = "http://%s" % (server,)
    soup = BeautifulSoup(urlopen('%s/user/%s/recent-tracks'
                                    % (server, username)))
    gobbles_list = soup.find('ul', {'class': 'gobbles'})
    timestamp = gobbles_list.find('li')['about'].split('#')[1].split('.')[0]
    print datetime.fromtimestamp(float(timestamp))

Add script to print time of most recent gobble.

Response:
<INSERT> from datetime import datetime
from optparse import OptionParser
from urllib2 import urlopen

from BeautifulSoup import BeautifulSoup

if __name__ == '__main__':
 <INSERT_END> <INSERT>   usage = "%prog <USERNAME> <WEB SERVER>"
    parser = OptionParser(usage=usage)
    opts,args = parser.parse_args()
    if len(args) != 2:
        parser.error("All arguments are required.")

    username,server = args
    if server[:7] != "http://":
        server = "http://%s" % (server,)
    soup = BeautifulSoup(urlopen('%s/user/%s/recent-tracks'
                                    % (server, username)))
    gobbles_list = soup.find('ul', {'class': 'gobbles'})
    timestamp = gobbles_list.find('li')['about'].split('#')[1].split('.')[0]
    print datetime.fromtimestamp(float(timestamp))
 <INSERT_END> <|endoftext|> from datetime import datetime
from optparse import OptionParser
from urllib2 import urlopen

from BeautifulSoup import BeautifulSoup

if __name__ == '__main__':
    usage = "%prog <USERNAME> <WEB SERVER>"
    parser = OptionParser(usage=usage)
    opts,args = parser.parse_args()
    if len(args) != 2:
        parser.error("All arguments are required.")

    username,server = args
    if server[:7] != "http://":
        server = "http://%s" % (server,)
    soup = BeautifulSoup(urlopen('%s/user/%s/recent-tracks'
                                    % (server, username)))
    gobbles_list = soup.find('ul', {'class': 'gobbles'})
    timestamp = gobbles_list.find('li')['about'].split('#')[1].split('.')[0]
    print datetime.fromtimestamp(float(timestamp))

Prompt: Add script to print time of most recent gobble.


from importlib import machinery

from django.template import loaders
from os import listdir
from os.path import dirname, splitext

MODULE_EXTENSIONS = tuple(machinery.all_suffixes())

def get_django_template_loaders():
    return [(loader.__name__.rsplit('.', 1)[1], loader)
            for loader in get_submodules(loaders) if hasattr(loader, 'Loader')]

def get_submodules(package):
    submodules = ("%s.%s" % (package.__name__, module) for module in package_contents(package))
    return [__import__(module, {}, {}, [module.rsplit(".", 1)[-1]]) for module in submodules]

def package_contents(package):
    package_path = dirname(loaders.__file__)
    contents = set([splitext(module)[0] for module in listdir(package_path) if module.endswith(MODULE_EXTENSIONS)])
    return contents

Response:
<INSERT> from importlib <INSERT_END> <REPLACE_OLD> imp

from <REPLACE_NEW> machinery

from <REPLACE_END> <REPLACE_OLD> tuple([suffix[0] for suffix in imp.get_suffixes()])

def <REPLACE_NEW> tuple(machinery.all_suffixes())

def <REPLACE_END> <|endoftext|> from importlib import machinery

from django.template import loaders
from os import listdir
from os.path import dirname, splitext

MODULE_EXTENSIONS = tuple(machinery.all_suffixes())

def get_django_template_loaders():
    return [(loader.__name__.rsplit('.', 1)[1], loader)
            for loader in get_submodules(loaders) if hasattr(loader, 'Loader')]

def get_submodules(package):
    submodules = ("%s.%s" % (package.__name__, module) for module in package_contents(package))
    return [__import__(module, {}, {}, [module.rsplit(".", 1)[-1]]) for module in submodules]

def package_contents(package):
    package_path = dirname(loaders.__file__)
    contents = set([splitext(module)[0] for module in listdir(package_path) if module.endswith(MODULE_EXTENSIONS)])
    return contents

Prompt:
Replace deprecated imp usage with importlib

import imp

from django.template import loaders
from os import listdir
from os.path import dirname, splitext

MODULE_EXTENSIONS = tuple([suffix[0] for suffix in imp.get_suffixes()])

def get_django_template_loaders():
    return [(loader.__name__.rsplit('.', 1)[1], loader)
            for loader in get_submodules(loaders) if hasattr(loader, 'Loader')]

def get_submodules(package):
    submodules = ("%s.%s" % (package.__name__, module) for module in package_contents(package))
    return [__import__(module, {}, {}, [module.rsplit(".", 1)[-1]]) for module in submodules]

def package_contents(package):
    package_path = dirname(loaders.__file__)
    contents = set([splitext(module)[0] for module in listdir(package_path) if module.endswith(MODULE_EXTENSIONS)])
    return contents


import django_rq
import logging
import os
import time

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'varify.conf.settings')

from django.core import management

log = logging.getLogger(__name__)

# We don't want this job to run forever so limit to this many attempts and
# if the queue is still not empty just give up.
MAX_ATTEMPTS = 100
# Time in seconds between polling the queue for the current count
POLL_DELAY = 300

queue = django_rq.get_queue('default')

attempts = 0

while attempts < MAX_ATTEMPTS:
    attempts += 1
    if queue.is_empty:
        management.call_command('samples', 'gene-ranks')
        log.debug('gene ranks updated')
        break
    log.debug('queue not empty, waiting {0} seconds'.format(POLL_DELAY))
    time.sleep(POLL_DELAY)
else:
    # Max attempts, log and exit
    log.error('Maximum attempts ({0}) made when trying to update gene ranks.'
              .format(MAX_ATTEMPTS))

Add script to call gene-ranks command after samples load

This will need to be added as a cron job like the recalculate_allele_frequencies script is used now.

Response:
<INSERT> import django_rq
import logging
import os
import time

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'varify.conf.settings')

from django.core import management

log = logging.getLogger(__name__)

# We don't want this job to run forever so limit to this many attempts and
# if the queue is still not empty just give up.
MAX_ATTEMPTS = 100
# Time in seconds between polling the queue for the current count
POLL_DELAY = 300

queue = django_rq.get_queue('default')

attempts = 0

while attempts < MAX_ATTEMPTS:
 <INSERT_END> <INSERT>   attempts += 1
    if queue.is_empty:
        management.call_command('samples', 'gene-ranks')
        log.debug('gene ranks updated')
        break
    log.debug('queue not empty, waiting {0} seconds'.format(POLL_DELAY))
    time.sleep(POLL_DELAY)
else:
    # Max attempts, log and exit
    log.error('Maximum attempts ({0}) made when trying to update gene ranks.'
              .format(MAX_ATTEMPTS))
 <INSERT_END> <|endoftext|> import django_rq
import logging
import os
import time

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'varify.conf.settings')

from django.core import management

log = logging.getLogger(__name__)

# We don't want this job to run forever so limit to this many attempts and
# if the queue is still not empty just give up.
MAX_ATTEMPTS = 100
# Time in seconds between polling the queue for the current count
POLL_DELAY = 300

queue = django_rq.get_queue('default')

attempts = 0

while attempts < MAX_ATTEMPTS:
    attempts += 1
    if queue.is_empty:
        management.call_command('samples', 'gene-ranks')
        log.debug('gene ranks updated')
        break
    log.debug('queue not empty, waiting {0} seconds'.format(POLL_DELAY))
    time.sleep(POLL_DELAY)
else:
    # Max attempts, log and exit
    log.error('Maximum attempts ({0}) made when trying to update gene ranks.'
              .format(MAX_ATTEMPTS))

Prompt:
Add script to call gene-ranks command after samples load

This will need to be added as a cron job like the recalculate_allele_frequencies script is used now.


c = get_config()

# Tell IPython to use PostgresContentsManager.
c.NotebookApp.contents_manager_class = 'pgcontents.pgmanager.PostgresContentsManager'

# Set the url for the database used to store files.  See
# http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html#postgresql
# for more info on db url formatting.
# c.PostgresContentsManager.db_url = 'postgresql://ssanderson@/pgcontents'

# Set a user ID. Defaults to the result of getpass.getuser()
# c.PostgresContentsManager.user_id = 'my_awesome_username'

# Set a maximum file size, if desired.
# c.PostgresContentsManager.max_file_size_bytes = 1000000 # 1MB File cap

Add an example configuration file.

DOC: Add an example configuration file.

Response:
<INSERT> c = get_config()

# Tell IPython to use PostgresContentsManager.
c.NotebookApp.contents_manager_class = 'pgcontents.pgmanager.PostgresContentsManager'

# Set the url for the database used to store files. <INSERT_END> <INSERT> See
# http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html#postgresql
# for more info on db url formatting.
# c.PostgresContentsManager.db_url = 'postgresql://ssanderson@/pgcontents'

# Set a user ID. Defaults to the result of getpass.getuser()
# c.PostgresContentsManager.user_id = 'my_awesome_username'

# Set a maximum file size, if desired.
# c.PostgresContentsManager.max_file_size_bytes = 1000000 # 1MB File cap
 <INSERT_END> <|endoftext|> c = get_config()

# Tell IPython to use PostgresContentsManager.
c.NotebookApp.contents_manager_class = 'pgcontents.pgmanager.PostgresContentsManager'

# Set the url for the database used to store files.  See
# http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html#postgresql
# for more info on db url formatting.
# c.PostgresContentsManager.db_url = 'postgresql://ssanderson@/pgcontents'

# Set a user ID. Defaults to the result of getpass.getuser()
# c.PostgresContentsManager.user_id = 'my_awesome_username'

# Set a maximum file size, if desired.
# c.PostgresContentsManager.max_file_size_bytes = 1000000 # 1MB File cap

Prompt: DOC: Add an example configuration file.


from admin_honeypot import views
from django.urls import path, re_path

app_name = 'admin_honeypot'

urlpatterns = [
    path('login/', views.AdminHoneypot.as_view(), name='login'),
    re_path(r'^.*$', views.AdminHoneypot.as_view(), name='index'),
]

Update url() to path() in the urlconf.

Update url() to path() in the urlconf.

Response:
<REPLACE_OLD> django.conf.urls <REPLACE_NEW> django.urls <REPLACE_END> <REPLACE_OLD> url

app_name <REPLACE_NEW> path, re_path

app_name <REPLACE_END> <REPLACE_OLD> url(r'^login/$', <REPLACE_NEW> path('login/', <REPLACE_END> <REPLACE_OLD> url(r'^.*$', <REPLACE_NEW> re_path(r'^.*$', <REPLACE_END> <|endoftext|> from admin_honeypot import views
from django.urls import path, re_path

app_name = 'admin_honeypot'

urlpatterns = [
    path('login/', views.AdminHoneypot.as_view(), name='login'),
    re_path(r'^.*$', views.AdminHoneypot.as_view(), name='index'),
]

Prompt:
Update url() to path() in the urlconf.

from admin_honeypot import views
from django.conf.urls import url

app_name = 'admin_honeypot'

urlpatterns = [
    url(r'^login/$', views.AdminHoneypot.as_view(), name='login'),
    url(r'^.*$', views.AdminHoneypot.as_view(), name='index'),
]


import mock
from prf.tests.prf_testcase import PrfTestCase
from prf.mongodb import get_document_cls, connect_dataset_aliases

class TestMongoDB(PrfTestCase):
    def setUp(self):
        super(TestMongoDB, self).setUp()
        self.drop_databases()
        self.unload_documents()

    def test_get_document_cls(self):
        cls = self.create_collection('default', 'col1')
        cls2 = self.create_collection('prf-test2', 'col2')
        cls3 = self.create_collection('default', 'col3')
        cls4 = self.create_collection('prf-test2', 'col3')

        dcls = get_document_cls('col1')
        dcls2 = get_document_cls('col2')
        dcls3 = get_document_cls('col3')
        assert cls == dcls
        assert cls2 == dcls2
        assert dcls2._meta['db_alias'] == 'prf-test2'
        # This is broken behavior with collision on collection names across dbs,
        # get_document_cls will return the most recently defined class with that name.
        assert dcls3 == cls4

    @mock.patch('prf.mongodb.mongo_connect')
    def test_connect_dataset_aliases_missing_config(self, connect):
        del self.conf.registry.settings['dataset.namespaces']
        connect_dataset_aliases(self.conf, self.conf.prf_settings())
        connect.assert_not_called()

Response:
<REPLACE_OLD> from <REPLACE_NEW> import mock
from <REPLACE_END> <REPLACE_OLD> get_document_cls

class <REPLACE_NEW> get_document_cls, connect_dataset_aliases

class <REPLACE_END> <REPLACE_OLD> cls4
 <REPLACE_NEW> cls4

    @mock.patch('prf.mongodb.mongo_connect')
    def test_connect_dataset_aliases_missing_config(self, connect):
        del self.conf.registry.settings['dataset.namespaces']
        connect_dataset_aliases(self.conf, self.conf.prf_settings())
        connect.assert_not_called()
 <REPLACE_END> <|endoftext|> import mock
from prf.tests.prf_testcase import PrfTestCase
from prf.mongodb import get_document_cls, connect_dataset_aliases

class TestMongoDB(PrfTestCase):
    def setUp(self):
        super(TestMongoDB, self).setUp()
        self.drop_databases()
        self.unload_documents()

    def test_get_document_cls(self):
        cls = self.create_collection('default', 'col1')
        cls2 = self.create_collection('prf-test2', 'col2')
        cls3 = self.create_collection('default', 'col3')
        cls4 = self.create_collection('prf-test2', 'col3')

        dcls = get_document_cls('col1')
        dcls2 = get_document_cls('col2')
        dcls3 = get_document_cls('col3')
        assert cls == dcls
        assert cls2 == dcls2
        assert dcls2._meta['db_alias'] == 'prf-test2'
        # This is broken behavior with collision on collection names across dbs,
        # get_document_cls will return the most recently defined class with that name.
        assert dcls3 == cls4

    @mock.patch('prf.mongodb.mongo_connect')
    def test_connect_dataset_aliases_missing_config(self, connect):
        del self.conf.registry.settings['dataset.namespaces']
        connect_dataset_aliases(self.conf, self.conf.prf_settings())
        connect.assert_not_called()

Prompt:
Make sure no crashes happen when no namespaces are set

from prf.tests.prf_testcase import PrfTestCase
from prf.mongodb import get_document_cls

class TestMongoDB(PrfTestCase):
    def setUp(self):
        super(TestMongoDB, self).setUp()
        self.drop_databases()
        self.unload_documents()

    def test_get_document_cls(self):
        cls = self.create_collection('default', 'col1')
        cls2 = self.create_collection('prf-test2', 'col2')
        cls3 = self.create_collection('default', 'col3')
        cls4 = self.create_collection('prf-test2', 'col3')

        dcls = get_document_cls('col1')
        dcls2 = get_document_cls('col2')
        dcls3 = get_document_cls('col3')
        assert cls == dcls
        assert cls2 == dcls2
        assert dcls2._meta['db_alias'] == 'prf-test2'
        # This is broken behavior with collision on collection names across dbs,
        # get_document_cls will return the most recently defined class with that name.
        assert dcls3 == cls4


from selenium import webdriver
import pytest

@pytest.fixture(scope='function')
def browser(request):
    browser_ = webdriver.Firefox()

    def fin():
        browser_.quit()
    request.addfinalizer(fin)

    return browser_

def test_can_show_a_relevant_code_snippet(browser):
    # Jan visits the site
    browser.get('http://localhost:8000')

    # He notices the title references the site name
    site_name = 'Scout'
    assert site_name.lower() in browser.title.lower()

    assert False, 'incomplete test'

Change FT to expect site name in title

Change FT to expect site name in title

Response:
<INSERT> # Jan visits the site
    <INSERT_END> <INSERT> # He notices the title references the site name
    site_name = 'Scout'
    <INSERT_END> <REPLACE_OLD> 'Django' <REPLACE_NEW> site_name.lower() <REPLACE_END> <REPLACE_OLD> browser.title
 <REPLACE_NEW> browser.title.lower()

    assert False, 'incomplete test'
 <REPLACE_END> <|endoftext|> from selenium import webdriver
import pytest

@pytest.fixture(scope='function')
def browser(request):
    browser_ = webdriver.Firefox()

    def fin():
        browser_.quit()
    request.addfinalizer(fin)

    return browser_

def test_can_show_a_relevant_code_snippet(browser):
    # Jan visits the site
    browser.get('http://localhost:8000')

    # He notices the title references the site name
    site_name = 'Scout'
    assert site_name.lower() in browser.title.lower()

    assert False, 'incomplete test'

Prompt:
Change FT to expect site name in title

from selenium import webdriver
import pytest

@pytest.fixture(scope='function')
def browser(request):
    browser_ = webdriver.Firefox()

    def fin():
        browser_.quit()
    request.addfinalizer(fin)

    return browser_

def test_can_show_a_relevant_code_snippet(browser):
    browser.get('http://localhost:8000')

    assert 'Django' in browser.title


import os
import json
import random
import flask
import requests
import hashlib

DNZ_URL = 'http://api.digitalnz.org/v3/records/'
DNZ_KEY = os.environ.get('DNZ_KEY')
records = {}

# TODO This should be switched to records associated with days.
# Create a hash table of all records.
for record in json.loads(open('data/records-2015.json').read())['records']:
    record_hash = hashlib.md5(str(record['id']).encode('utf-8')).hexdigest()
    records[record_hash] = record

app = flask.Flask(__name__)

@app.route('/')
def index():
    return flask.render_template('index.html')

@app.route('/hello')
def hello():
    return 'hello'

@app.route('/random')
def random_record():
    record_hash = random.choice(list(records.keys()))
    return str(get_metadata(records[record_hash]['id']))

def get_metadata(id):
    url = DNZ_URL + '{id}.json?api_key={key}'.format(id=id, key=DNZ_KEY)
    return requests.get(url).json()

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port, debug=True)

Add method to query DNZ Metadata API

Add method to query DNZ Metadata API

Response:
<REPLACE_OLD> flask

from hashlib import md5

records <REPLACE_NEW> flask
import requests
import hashlib

DNZ_URL = 'http://api.digitalnz.org/v3/records/'
DNZ_KEY = os.environ.get('DNZ_KEY')
records <REPLACE_END> <INSERT> TODO This should be switched to records associated with days.
# <INSERT_END> <REPLACE_OLD> records[md5(str(record['id']).encode('utf-8')).hexdigest()] <REPLACE_NEW> record_hash = hashlib.md5(str(record['id']).encode('utf-8')).hexdigest()
    records[record_hash] <REPLACE_END> <REPLACE_OLD> str(records[record_hash])

if <REPLACE_NEW> str(get_metadata(records[record_hash]['id']))

def get_metadata(id):
    url = DNZ_URL + '{id}.json?api_key={key}'.format(id=id, key=DNZ_KEY)
    return requests.get(url).json()

if <REPLACE_END> <|endoftext|> import os
import json
import random
import flask
import requests
import hashlib

DNZ_URL = 'http://api.digitalnz.org/v3/records/'
DNZ_KEY = os.environ.get('DNZ_KEY')
records = {}

# TODO This should be switched to records associated with days.
# Create a hash table of all records.
for record in json.loads(open('data/records-2015.json').read())['records']:
    record_hash = hashlib.md5(str(record['id']).encode('utf-8')).hexdigest()
    records[record_hash] = record

app = flask.Flask(__name__)

@app.route('/')
def index():
    return flask.render_template('index.html')

@app.route('/hello')
def hello():
    return 'hello'

@app.route('/random')
def random_record():
    record_hash = random.choice(list(records.keys()))
    return str(get_metadata(records[record_hash]['id']))

def get_metadata(id):
    url = DNZ_URL + '{id}.json?api_key={key}'.format(id=id, key=DNZ_KEY)
    return requests.get(url).json()

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port, debug=True)

Prompt:
Add method to query DNZ Metadata API

import os
import json
import random
import flask

from hashlib import md5

records = {}

# Create a hash table of all records.
for record in json.loads(open('data/records-2015.json').read())['records']:
    records[md5(str(record['id']).encode('utf-8')).hexdigest()] = record

app = flask.Flask(__name__)

@app.route('/')
def index():
    return flask.render_template('index.html')

@app.route('/hello')
def hello():
    return 'hello'

@app.route('/random')
def random_record():
    record_hash = random.choice(list(records.keys()))
    return str(records[record_hash])

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port, debug=True)


import werkzeug.debug
import werkzeug.serving

from jacquard.commands import BaseCommand
from jacquard.service import get_wsgi_app

class RunServer(BaseCommand):
    help = "run a (local, debug) server"

    def add_arguments(self, parser):
        parser.add_argument(
            '-p',
            '--port',
            type=int,
            default=8888,
            help="port to bind to",
        )
        parser.add_argument(
            '-b',
            '--bind',
            type=str,
            default='::1',
            help="address to bind to",
        )

    def handle(self, config, options):
        app = get_wsgi_app(config)

        werkzeug.serving.run_simple(
            options.bind,
            options.port,
            app,
            use_reloader=True,
            use_debugger=True,
            use_evalex=True,
            threaded=False,
            processes=1,
        )

Make this derive from the correct type

Make this derive from the correct type

Response:
<REPLACE_OLD> RunServer(object):
 <REPLACE_NEW> RunServer(BaseCommand):
 <REPLACE_END> <|endoftext|> import werkzeug.debug
import werkzeug.serving

from jacquard.commands import BaseCommand
from jacquard.service import get_wsgi_app

class RunServer(BaseCommand):
    help = "run a (local, debug) server"

    def add_arguments(self, parser):
        parser.add_argument(
            '-p',
            '--port',
            type=int,
            default=8888,
            help="port to bind to",
        )
        parser.add_argument(
            '-b',
            '--bind',
            type=str,
            default='::1',
            help="address to bind to",
        )

    def handle(self, config, options):
        app = get_wsgi_app(config)

        werkzeug.serving.run_simple(
            options.bind,
            options.port,
            app,
            use_reloader=True,
            use_debugger=True,
            use_evalex=True,
            threaded=False,
            processes=1,
        )

Prompt:
Make this derive from the correct type

import werkzeug.debug
import werkzeug.serving

from jacquard.commands import BaseCommand
from jacquard.service import get_wsgi_app

class RunServer(object):
    help = "run a (local, debug) server"

    def add_arguments(self, parser):
        parser.add_argument(
            '-p',
            '--port',
            type=int,
            default=8888,
            help="port to bind to",
        )
        parser.add_argument(
            '-b',
            '--bind',
            type=str,
            default='::1',
            help="address to bind to",
        )

    def handle(self, config, options):
        app = get_wsgi_app(config)

        werkzeug.serving.run_simple(
            options.bind,
            options.port,
            app,
            use_reloader=True,
            use_debugger=True,
            use_evalex=True,
            threaded=False,
            processes=1,
        )


from django.test import TestCase
from django.conf import settings

from whats_fresh.models import *
from django.contrib.gis.db import models

import os
import time
import sys
import datetime

class ImageTestCase(TestCase):
    def setUp(self):
        # Set MEDIA ROOT to sample data for this test
        TEST_ROOT = os.path.abspath(os.path.dirname(__file__))

        self._old_MEDIA_ROOT = settings.MEDIA_ROOT
        settings.MEDIA_ROOT = os.path.join(TEST_ROOT, 'testdata/media/')

        self.image = Image(image='cat.jpg', caption='Meow!')

        # Set creation time variable to test if image.creation records properly
        self.creation_time = datetime.datetime.now()
        self.image.save()

    def tearDown(self):
        # reset MEDIA_ROOT
        settings.MEDIA_ROOT = self._old_MEDIA_ROOT

    def test_image_uploaded(self):
        """
        Tests that the image was successfully uploaded
        """
        self.assertEquals(self.image.image.url, '/media/cat.jpg')
        self.assertEquals(self.image.caption, 'Meow!')

        self.assertTrue(
            self.image.created.replace(tzinfo=None) - self.creation_time <
            datetime.timedelta(milliseconds = 10))

    def test_replace_image(self):
        """
        Tests that the image was properly replaced with a new image.
        Also sets modified time variable for later testing to make
        sure the time variables were properly set and saved.
        """
        # Sleep 25 milliseconds so that the modified time won't be within
        # The defined range of 10 milliseconds
        time.sleep(0.025)

        self.image.image = 'dog.jpg'
        self.mod_time = datetime.datetime.now()
        self.image.save()

        self.assertEquals(self.image.image.url, '/media/dog.jpg')

        self.assertTrue(
            self.image.modified.replace(tzinfo=None) - self.mod_time <
            datetime.timedelta(milliseconds = 10))

class PreparationsTestCase(TestCase):
    def test_fields_exist(self):
        model = models.get_model('whats_fresh', 'Preparation')
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('name')))
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('description')))
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('additional_info')))

    def test_no_additional_fields(self):
        fields = Preparation._meta.get_all_field_names()
        expected_fields = ['name', 'description', 'additional_info', u'id']

        self.assertTrue(sorted(fields) == sorted(expected_fields))

Make preparation model tests actually work

Response:
<REPLACE_OLD>  <REPLACE_NEW> from django.test import TestCase
from django.conf import settings

from whats_fresh.models import *
from django.contrib.gis.db import models

import os
import time
import sys
import datetime

class ImageTestCase(TestCase):
    def setUp(self):
        # Set MEDIA ROOT to sample data for this test
        TEST_ROOT = os.path.abspath(os.path.dirname(__file__))

        self._old_MEDIA_ROOT = settings.MEDIA_ROOT
        settings.MEDIA_ROOT = os.path.join(TEST_ROOT, 'testdata/media/')

        self.image = Image(image='cat.jpg', caption='Meow!')

        # Set creation time variable to test if image.creation records properly
        self.creation_time = datetime.datetime.now()
        self.image.save()

    def tearDown(self):
        # reset MEDIA_ROOT
        settings.MEDIA_ROOT = self._old_MEDIA_ROOT

    def test_image_uploaded(self):
        """
        Tests that the image was successfully uploaded
        """
        self.assertEquals(self.image.image.url, '/media/cat.jpg')
        self.assertEquals(self.image.caption, 'Meow!')

        self.assertTrue(
            self.image.created.replace(tzinfo=None) - self.creation_time <
            datetime.timedelta(milliseconds = 10))

    def test_replace_image(self):
        """
        Tests that the image was properly replaced with a new image.
        Also sets modified time variable for later testing to make
        sure the time variables were properly set and saved.
        """
        # Sleep 25 milliseconds so that the modified time won't be within
        # The defined range of 10 milliseconds
        time.sleep(0.025)

        self.image.image = 'dog.jpg'
        self.mod_time = datetime.datetime.now()
        self.image.save()

        self.assertEquals(self.image.image.url, '/media/dog.jpg')

        self.assertTrue(
            self.image.modified.replace(tzinfo=None) - self.mod_time <
            datetime.timedelta(milliseconds = 10))

class PreparationsTestCase(TestCase):
    def test_fields_exist(self):
        model = models.get_model('whats_fresh', 'Preparation')
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('name')))
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('description')))
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('additional_info')))

    def test_no_additional_fields(self):
        fields = Preparation._meta.get_all_field_names()
        expected_fields = ['name', 'description', 'additional_info', u'id']

        self.assertTrue(sorted(fields) == sorted(expected_fields))

 <REPLACE_END> <|endoftext|> from django.test import TestCase
from django.conf import settings

from whats_fresh.models import *
from django.contrib.gis.db import models

import os
import time
import sys
import datetime

class ImageTestCase(TestCase):
    def setUp(self):
        # Set MEDIA ROOT to sample data for this test
        TEST_ROOT = os.path.abspath(os.path.dirname(__file__))

        self._old_MEDIA_ROOT = settings.MEDIA_ROOT
        settings.MEDIA_ROOT = os.path.join(TEST_ROOT, 'testdata/media/')

        self.image = Image(image='cat.jpg', caption='Meow!')

        # Set creation time variable to test if image.creation records properly
        self.creation_time = datetime.datetime.now()
        self.image.save()

    def tearDown(self):
        # reset MEDIA_ROOT
        settings.MEDIA_ROOT = self._old_MEDIA_ROOT

    def test_image_uploaded(self):
        """
        Tests that the image was successfully uploaded
        """
        self.assertEquals(self.image.image.url, '/media/cat.jpg')
        self.assertEquals(self.image.caption, 'Meow!')

        self.assertTrue(
            self.image.created.replace(tzinfo=None) - self.creation_time <
            datetime.timedelta(milliseconds = 10))

    def test_replace_image(self):
        """
        Tests that the image was properly replaced with a new image.
        Also sets modified time variable for later testing to make
        sure the time variables were properly set and saved.
        """
        # Sleep 25 milliseconds so that the modified time won't be within
        # The defined range of 10 milliseconds
        time.sleep(0.025)

        self.image.image = 'dog.jpg'
        self.mod_time = datetime.datetime.now()
        self.image.save()

        self.assertEquals(self.image.image.url, '/media/dog.jpg')

        self.assertTrue(
            self.image.modified.replace(tzinfo=None) - self.mod_time <
            datetime.timedelta(milliseconds = 10))

class PreparationsTestCase(TestCase):
    def test_fields_exist(self):
        model = models.get_model('whats_fresh', 'Preparation')
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('name')))
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('description')))
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('additional_info')))

    def test_no_additional_fields(self):
        fields = Preparation._meta.get_all_field_names()
        expected_fields = ['name', 'description', 'additional_info', u'id']

        self.assertTrue(sorted(fields) == sorted(expected_fields))

Prompt: Make preparation model tests actually work


#!/usr/bin/env python
# Copyright Datacratic 2016
# Author: Jean Raby <jean@datacratic.com>

# This script is called by runsv when the mldb service exits.
# See http://smarden.org/runit/runsv.8.html for more details.
# Two arguments are given to ./finish:
# The first one is ./run's exit code, or -1 if ./run didn't exit normally.
# The second one is the least significant byte of the exit status as
# determined by waitpid(2); for instance it is 0 if ./run exited normally,
# and the signal number if ./run was terminated by a signal.
# If runsv cannot start ./run for some reason, the exit code is 111 and the status is 0.

import os
import sys

sigmap = { 4: "SIGILL: illegal instruction - internal error",
           6: "SIGABRT: abort(3) called - internal error",
           9: "SIGKILL: killed from outside, did we run out of memory (OOM killed?)",
           11: "SIGSEGV: segfault - internal error",
           15: "SIGTERM: regular shutdown",
         }

msg = ""
sig = None
if len(sys.argv) == 3:
    exit_code = sys.argv[1]
    status_code = sys.argv[2]
    if os.WIFSIGNALED(int(status_code)):
        sig = os.WTERMSIG(int(status_code))

print  # we like space
print
if sig == None:
    print "MLDB exited."
else:
    msg = "MLDB exited due to signal %d." % (sig)
    if sig in sigmap:
        msg += " " + sigmap[sig]
    print msg
print
print

Response:
<REPLACE_OLD> sys

msg = ""
if <REPLACE_NEW> sys

sigmap = { 4: "SIGILL: illegal instruction - internal error",
           6: "SIGABRT: abort(3) called - internal error",
           9: "SIGKILL: killed from outside, did we run out of memory (OOM killed?)",
           11: "SIGSEGV: segfault - internal error",
           15: "SIGTERM: regular shutdown",
         }

msg = ""
sig = None
if <REPLACE_END> <REPLACE_OLD> os.WTERMSIG(int(status_code))
     <REPLACE_NEW> os.WTERMSIG(int(status_code))

print  # we like space
print
if sig == None:
    print "MLDB exited."
else:
 <REPLACE_END> <REPLACE_OLD> " Killed by <REPLACE_NEW> "MLDB exited due to <REPLACE_END> <REPLACE_OLD> sig

print "MLDB exited.%s" % (msg)

 <REPLACE_NEW> (sig)
    if sig in sigmap:
        msg += " " + sigmap[sig]
    print msg
print
print

 <REPLACE_END> <|endoftext|> #!/usr/bin/env python
# Copyright Datacratic 2016
# Author: Jean Raby <jean@datacratic.com>

# This script is called by runsv when the mldb service exits.
# See http://smarden.org/runit/runsv.8.html for more details.
# Two arguments are given to ./finish:
# The first one is ./run's exit code, or -1 if ./run didn't exit normally.
# The second one is the least significant byte of the exit status as
# determined by waitpid(2); for instance it is 0 if ./run exited normally,
# and the signal number if ./run was terminated by a signal.
# If runsv cannot start ./run for some reason, the exit code is 111 and the status is 0.

import os
import sys

sigmap = { 4: "SIGILL: illegal instruction - internal error",
           6: "SIGABRT: abort(3) called - internal error",
           9: "SIGKILL: killed from outside, did we run out of memory (OOM killed?)",
           11: "SIGSEGV: segfault - internal error",
           15: "SIGTERM: regular shutdown",
         }

msg = ""
sig = None
if len(sys.argv) == 3:
    exit_code = sys.argv[1]
    status_code = sys.argv[2]
    if os.WIFSIGNALED(int(status_code)):
        sig = os.WTERMSIG(int(status_code))

print  # we like space
print
if sig == None:
    print "MLDB exited."
else:
    msg = "MLDB exited due to signal %d." % (sig)
    if sig in sigmap:
        msg += " " + sigmap[sig]
    print msg
print
print

Prompt:
Add signal map and human readable description

#!/usr/bin/env python
# Copyright Datacratic 2016
# Author: Jean Raby <jean@datacratic.com>

# This script is called by runsv when the mldb service exits.
# See http://smarden.org/runit/runsv.8.html for more details.
# Two arguments are given to ./finish:
# The first one is ./run's exit code, or -1 if ./run didn't exit normally.
# The second one is the least significant byte of the exit status as
# determined by waitpid(2); for instance it is 0 if ./run exited normally,
# and the signal number if ./run was terminated by a signal.
# If runsv cannot start ./run for some reason, the exit code is 111 and the status is 0.

import os
import sys

msg = ""
if len(sys.argv) == 3:
    exit_code = sys.argv[1]
    status_code = sys.argv[2]
    if os.WIFSIGNALED(int(status_code)):
        sig = os.WTERMSIG(int(status_code))
        msg = " Killed by signal %d." % sig

print "MLDB exited.%s" % (msg)


# Copyright 2012 Kevin Goodsell
#
# This software is licensed under the Eclipse Public License (EPL) V1.0.

'''
Interface to the STAF API.
'''

# Using __all__ makes pydoc work properly. Otherwise it looks at the modules the
# items actually come from and assumes they don't belong in the docs for
# __init__.
__all__ = [
    'Handle', 'wrap_data', 'add_privacy_delimiters',
    'remove_privacy_delimiters', 'mask_private_data',
    'escape_privacy_delimiters', 'errors', 'strerror', 'STAFError',
    'STAFResultError', 'unmarshal', 'unmarshal_force', 'STAFUnmarshalError',
    'MapClassDefinition', 'MapClass', 'unmarshal_recursive',
    'unmarshal_non_recursive', 'unmarshal_none',
]

from ._staf import (
    Handle,
    wrap_data,
    add_privacy_delimiters,
    remove_privacy_delimiters,
    mask_private_data,
    escape_privacy_delimiters,
)

from ._errors import (
    errors,
    strerror,
    STAFError,
    STAFResultError,
)

from ._marshal import (
    unmarshal,
    unmarshal_force,
    STAFUnmarshalError,
    MapClassDefinition,
    MapClass,
    unmarshal_recursive,
    unmarshal_non_recursive,
    unmarshal_none,
)

Add missing function in __all__.

Add missing function in __all__.

Response:
<REPLACE_OLD> 'STAFUnmarshalError', 'MapClassDefinition',
 <REPLACE_NEW> 'unmarshal_force', 'STAFUnmarshalError',
 <REPLACE_END> <INSERT> 'MapClassDefinition', <INSERT_END> <REPLACE_OLD> 'unmarshal_recursive', 'unmarshal_non_recursive',
 <REPLACE_NEW> 'unmarshal_recursive',
 <REPLACE_END> <INSERT> 'unmarshal_non_recursive', <INSERT_END> <|endoftext|> # Copyright 2012 Kevin Goodsell
#
# This software is licensed under the Eclipse Public License (EPL) V1.0.

'''
Interface to the STAF API.
'''

# Using __all__ makes pydoc work properly. Otherwise it looks at the modules the
# items actually come from and assumes they don't belong in the docs for
# __init__.
__all__ = [
    'Handle', 'wrap_data', 'add_privacy_delimiters',
    'remove_privacy_delimiters', 'mask_private_data',
    'escape_privacy_delimiters', 'errors', 'strerror', 'STAFError',
    'STAFResultError', 'unmarshal', 'unmarshal_force', 'STAFUnmarshalError',
    'MapClassDefinition', 'MapClass', 'unmarshal_recursive',
    'unmarshal_non_recursive', 'unmarshal_none',
]

from ._staf import (
    Handle,
    wrap_data,
    add_privacy_delimiters,
    remove_privacy_delimiters,
    mask_private_data,
    escape_privacy_delimiters,
)

from ._errors import (
    errors,
    strerror,
    STAFError,
    STAFResultError,
)

from ._marshal import (
    unmarshal,
    unmarshal_force,
    STAFUnmarshalError,
    MapClassDefinition,
    MapClass,
    unmarshal_recursive,
    unmarshal_non_recursive,
    unmarshal_none,
)

Prompt:
Add missing function in __all__.

# Copyright 2012 Kevin Goodsell
#
# This software is licensed under the Eclipse Public License (EPL) V1.0.

'''
Interface to the STAF API.
'''

# Using __all__ makes pydoc work properly. Otherwise it looks at the modules the
# items actually come from and assumes they don't belong in the docs for
# __init__.
__all__ = [
    'Handle', 'wrap_data', 'add_privacy_delimiters',
    'remove_privacy_delimiters', 'mask_private_data',
    'escape_privacy_delimiters', 'errors', 'strerror', 'STAFError',
    'STAFResultError', 'unmarshal', 'STAFUnmarshalError', 'MapClassDefinition',
    'MapClass', 'unmarshal_recursive', 'unmarshal_non_recursive',
    'unmarshal_none',
]

from ._staf import (
    Handle,
    wrap_data,
    add_privacy_delimiters,
    remove_privacy_delimiters,
    mask_private_data,
    escape_privacy_delimiters,
)

from ._errors import (
    errors,
    strerror,
    STAFError,
    STAFResultError,
)

from ._marshal import (
    unmarshal,
    unmarshal_force,
    STAFUnmarshalError,
    MapClassDefinition,
    MapClass,
    unmarshal_recursive,
    unmarshal_non_recursive,
    unmarshal_none,
)


#!/usr/bin/env python

# Copyright (c) 2017 Orange and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

# pylint: disable=missing-docstring

import logging
import unittest

from functest.opnfv_tests.features import domino
from functest.utils import constants

class DominoTesting(unittest.TestCase):

    logging.disable(logging.CRITICAL)

    def setUp(self):
        self.domino = domino.Domino()

    def test_init(self):
        self.assertEqual(self.domino.project_name, "domino")
        self.assertEqual(self.domino.case_name, "domino-multinode")
        self.assertEqual(
            self.domino.repo,
            constants.CONST.__getattribute__("dir_repo_domino"))
        self.assertEqual(
            self.domino.cmd,
            'cd {} && ./tests/run_multinode.sh'.format(self.domino.repo))

if __name__ == "__main__":
    unittest.main(verbosity=2)

Add unit tests for domino

Add unit tests for domino

Change-Id: Ie6671080a3d38a17da0ee608a362605a6d9df9db
Signed-off-by: Cédric Ollivier <d48310251a4a484d041bc5d09a9ac4d86d20f793@orange.com>

Response:
<INSERT> #!/usr/bin/env python

# Copyright (c) 2017 Orange and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

# pylint: disable=missing-docstring

import logging
import unittest

from functest.opnfv_tests.features import domino
from functest.utils import constants

class DominoTesting(unittest.TestCase):

 <INSERT_END> <INSERT>   logging.disable(logging.CRITICAL)

    def setUp(self):
        self.domino = domino.Domino()

    def test_init(self):
        self.assertEqual(self.domino.project_name, "domino")
        self.assertEqual(self.domino.case_name, "domino-multinode")
        self.assertEqual(
            self.domino.repo,
            constants.CONST.__getattribute__("dir_repo_domino"))
        self.assertEqual(
            self.domino.cmd,
            'cd {} && ./tests/run_multinode.sh'.format(self.domino.repo))

if __name__ == "__main__":
    unittest.main(verbosity=2)
 <INSERT_END> <|endoftext|> #!/usr/bin/env python

# Copyright (c) 2017 Orange and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

# pylint: disable=missing-docstring

import logging
import unittest

from functest.opnfv_tests.features import domino
from functest.utils import constants

class DominoTesting(unittest.TestCase):

    logging.disable(logging.CRITICAL)

    def setUp(self):
        self.domino = domino.Domino()

    def test_init(self):
        self.assertEqual(self.domino.project_name, "domino")
        self.assertEqual(self.domino.case_name, "domino-multinode")
        self.assertEqual(
            self.domino.repo,
            constants.CONST.__getattribute__("dir_repo_domino"))
        self.assertEqual(
            self.domino.cmd,
            'cd {} && ./tests/run_multinode.sh'.format(self.domino.repo))

if __name__ == "__main__":
    unittest.main(verbosity=2)

Prompt:
Add unit tests for domino

Change-Id: Ie6671080a3d38a17da0ee608a362605a6d9df9db
Signed-off-by: Cédric Ollivier <d48310251a4a484d041bc5d09a9ac4d86d20f793@orange.com>


from setuptools import setup, find_packages
import platform

distro, version, _ = platform.dist()

# Default to cent7
data_files = [('/usr/lib/systemd/system', ['pkg/hubble.service']),]

if distro == 'redhat' or distro == 'centos':
    if version.startswith('6'):
        data_files = [('/etc/init.d', ['pkg/hubble']),]
    elif version.startswith('7'):
        data_files = [('/usr/lib/systemd/system', ['pkg/hubble.service']),]

setup(
    name="hubblestack",
    version="2.0",
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'hubble = hubble.daemon:run',
        ],
    },
    install_requires=[
        'salt >= 2016.3.4',
    ],
    data_files=data_files,
    options={
#        'build_scripts': {
#            'executable': '/usr/bin/env python',
#        },
        'bdist_rpm': {
            'requires': 'salt',
        },
    },

)

Add platform detection for centos 6/7

Add platform detection for centos 6/7

Response:
<REPLACE_OLD> find_packages
setup(
 <REPLACE_NEW> find_packages
import platform

distro, version, _ = platform.dist()

# Default to cent7
data_files = [('/usr/lib/systemd/system', ['pkg/hubble.service']),]

if distro == 'redhat' or distro == 'centos':
    if version.startswith('6'):
        data_files = [('/etc/init.d', ['pkg/hubble']),]
    elif version.startswith('7'):
        data_files = [('/usr/lib/systemd/system', ['pkg/hubble.service']),]

setup(
 <REPLACE_END> <REPLACE_OLD> data_files=[('/etc/init.d', ['pkg/hubble']),],
 <REPLACE_NEW> data_files=data_files,
 <REPLACE_END> <|endoftext|> from setuptools import setup, find_packages
import platform

distro, version, _ = platform.dist()

# Default to cent7
data_files = [('/usr/lib/systemd/system', ['pkg/hubble.service']),]

if distro == 'redhat' or distro == 'centos':
    if version.startswith('6'):
        data_files = [('/etc/init.d', ['pkg/hubble']),]
    elif version.startswith('7'):
        data_files = [('/usr/lib/systemd/system', ['pkg/hubble.service']),]

setup(
    name="hubblestack",
    version="2.0",
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'hubble = hubble.daemon:run',
        ],
    },
    install_requires=[
        'salt >= 2016.3.4',
    ],
    data_files=data_files,
    options={
#        'build_scripts': {
#            'executable': '/usr/bin/env python',
#        },
        'bdist_rpm': {
            'requires': 'salt',
        },
    },

)

Prompt:
Add platform detection for centos 6/7

from setuptools import setup, find_packages
setup(
    name="hubblestack",
    version="2.0",
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'hubble = hubble.daemon:run',
        ],
    },
    install_requires=[
        'salt >= 2016.3.4',
    ],
    data_files=[('/etc/init.d', ['pkg/hubble']),],
    options={
#        'build_scripts': {
#            'executable': '/usr/bin/env python',
#        },
        'bdist_rpm': {
            'requires': 'salt',
        },
    },

)


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

"""
byceps.blueprints.news.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from flask import abort, current_app, g

from ...services.news import service as news_service
from ...util.framework import create_blueprint
from ...util.templating import templated

blueprint = create_blueprint('news', __name__)

@blueprint.route('/', defaults={'page': 1})
@blueprint.route('/pages/<int:page>')
@templated
def index(page):
    """Show a page of news items."""
    items_per_page = _get_items_per_page_value()

    items = news_service.get_items_paginated(g.party.brand.id, page,
                                             items_per_page)

    return {
        'items': items,
        'page': page,
    }

@blueprint.route('/<slug>')
@templated
def view(slug):
    """Show a single news item."""
    item = news_service.find_item_by_slug(g.party.brand.id, slug)

    if item is None:
        abort(404)

    return {
        'item': item,
    }

def _get_items_per_page_value(default=4):
    return int(current_app.config.get('NEWS_ITEMS_PER_PAGE', default))

Response:
<INSERT> current_app, <INSERT_END> <REPLACE_OLD> __name__)

ITEMS_PER_PAGE = 4

@blueprint.route('/', <REPLACE_NEW> __name__)

@blueprint.route('/', <REPLACE_END> <INSERT>    items_per_page = _get_items_per_page_value()

 <INSERT_END> <REPLACE_OLD> ITEMS_PER_PAGE)

 <REPLACE_NEW> items_per_page)

 <REPLACE_END> <REPLACE_OLD> }
 <REPLACE_NEW> }

def _get_items_per_page_value(default=4):
    return int(current_app.config.get('NEWS_ITEMS_PER_PAGE', default))
 <REPLACE_END> <|endoftext|> # -*- coding: utf-8 -*-

"""
byceps.blueprints.news.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from flask import abort, current_app, g

from ...services.news import service as news_service
from ...util.framework import create_blueprint
from ...util.templating import templated

blueprint = create_blueprint('news', __name__)

@blueprint.route('/', defaults={'page': 1})
@blueprint.route('/pages/<int:page>')
@templated
def index(page):
    """Show a page of news items."""
    items_per_page = _get_items_per_page_value()

    items = news_service.get_items_paginated(g.party.brand.id, page,
                                             items_per_page)

    return {
        'items': items,
        'page': page,
    }

@blueprint.route('/<slug>')
@templated
def view(slug):
    """Show a single news item."""
    item = news_service.find_item_by_slug(g.party.brand.id, slug)

    if item is None:
        abort(404)

    return {
        'item': item,
    }

def _get_items_per_page_value(default=4):
    return int(current_app.config.get('NEWS_ITEMS_PER_PAGE', default))

Prompt:
Allow configuration of the number of news items per page

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

"""
byceps.blueprints.news.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from flask import abort, g

from ...services.news import service as news_service
from ...util.framework import create_blueprint
from ...util.templating import templated

blueprint = create_blueprint('news', __name__)

ITEMS_PER_PAGE = 4

@blueprint.route('/', defaults={'page': 1})
@blueprint.route('/pages/<int:page>')
@templated
def index(page):
    """Show a page of news items."""
    items = news_service.get_items_paginated(g.party.brand.id, page,
                                             ITEMS_PER_PAGE)

    return {
        'items': items,
        'page': page,
    }

@blueprint.route('/<slug>')
@templated
def view(slug):
    """Show a single news item."""
    item = news_service.find_item_by_slug(g.party.brand.id, slug)

    if item is None:
        abort(404)

    return {
        'item': item,
    }


import pya
import re

WIDTH = 2048
HEIGHT = 2048

app = pya.Application.instance()
win = app.main_window()

# Load technology file
tech = pya.Technology()
tech.load(tech_file)
layoutOptions = tech.load_layout_options

# Load def file in the main window
cell_view = win.load_layout(input_layout, layoutOptions, 0)
layout_view = cell_view.view()

layout_view.save_image(input_layout+".png", WIDTH, HEIGHT)

app.exit(0)

Add a klayout script to screenshot layouts

Response:
<REPLACE_OLD>  <REPLACE_NEW> import pya
import re

WIDTH = 2048
HEIGHT = 2048

app = pya.Application.instance()
win = app.main_window()

# Load technology file
tech = pya.Technology()
tech.load(tech_file)
layoutOptions = tech.load_layout_options

# Load def file in the main window
cell_view = win.load_layout(input_layout, layoutOptions, 0)
layout_view = cell_view.view()

layout_view.save_image(input_layout+".png", WIDTH, HEIGHT)

app.exit(0)
 <REPLACE_END> <|endoftext|> import pya
import re

WIDTH = 2048
HEIGHT = 2048

app = pya.Application.instance()
win = app.main_window()

# Load technology file
tech = pya.Technology()
tech.load(tech_file)
layoutOptions = tech.load_layout_options

# Load def file in the main window
cell_view = win.load_layout(input_layout, layoutOptions, 0)
layout_view = cell_view.view()

layout_view.save_image(input_layout+".png", WIDTH, HEIGHT)

app.exit(0)

Prompt: Add a klayout script to screenshot layouts


#!/usr/bin/env python
import os
import sys
import unittest
from flask.ext.script import Manager, Server
from app import app

manager = Manager(app)

server = Server(host='0.0.0.0', use_debugger=True)
manager.add_command("runserver", server)

@manager.command
def test():
    """Runs all the tests"""
    parent_dir = os.path.dirname(os.path.abspath(__file__))
    test_dir = os.path.join(parent_dir, 'tests')
    tests = unittest.TestLoader().discover(test_dir)
    results = unittest.TextTestRunner(verbosity=2).run(tests)
    ret = not results.wasSuccessful()
    sys.exit(ret)

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

Response:
<INSERT> sys
import <INSERT_END> <REPLACE_OLD> unittest.TextTestRunner(verbosity=2).run(tests)

if <REPLACE_NEW> unittest.TextTestRunner(verbosity=2).run(tests)
    ret = not results.wasSuccessful()
    sys.exit(ret)

if <REPLACE_END> <|endoftext|> #!/usr/bin/env python
import os
import sys
import unittest
from flask.ext.script import Manager, Server
from app import app

manager = Manager(app)

server = Server(host='0.0.0.0', use_debugger=True)
manager.add_command("runserver", server)

@manager.command
def test():
    """Runs all the tests"""
    parent_dir = os.path.dirname(os.path.abspath(__file__))
    test_dir = os.path.join(parent_dir, 'tests')
    tests = unittest.TestLoader().discover(test_dir)
    results = unittest.TextTestRunner(verbosity=2).run(tests)
    ret = not results.wasSuccessful()
    sys.exit(ret)

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

Prompt:
Set the exit code when running tests correctly

#!/usr/bin/env python
import os
import unittest
from flask.ext.script import Manager, Server
from app import app

manager = Manager(app)

server = Server(host='0.0.0.0', use_debugger=True)
manager.add_command("runserver", server)

@manager.command
def test():
    """Runs all the tests"""
    parent_dir = os.path.dirname(os.path.abspath(__file__))
    test_dir = os.path.join(parent_dir, 'tests')
    tests = unittest.TestLoader().discover(test_dir)
    results = unittest.TextTestRunner(verbosity=2).run(tests)

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


import time

from collections import deque
from pybloom import ScalableBloomFilter

class DecayScalableBloomFilter(ScalableBloomFilter):
    '''
    Stepwise decaying Bloom Filter
    '''
    def __init__(self, initial_capacity=1000, error_rate=0.01, window_period = 60):
        super(DecayScalableBloomFilter, self).__init__(initial_capacity, error_rate)
        self.window_period = 60
        self.timestamp = time.time()
        self._expired = False

    @property
    def expired(self):
        if time.time() - self.timestamp > self.window_period:
            self._expired = True
        return self._expired

class SlidingWindowScalableBloomFilter(object):
    '''
    Sliding Window Bloom Filter using a coarse expiration
    '''

    VALID_RES = {'Min': 60,
                 'Hour': 3600,
                 'Day': 86400}

    def __init__(self, initial_capacity=1000, window_period = "10_Min"):
        self.initial_capacity = initial_capacity
        self.error_rate = 0.01
        self._setup_window_period(window_period)

    def _setup_window_period(self, window_period):
        try:
            self.amount, self.res = window_period.split('_')
            self.amount = int(self.amount)
        except ValueError:
            raise Exception('Invalid window period')
        self.window_period = SlidingWindowScalableBloomFilter.VALID_RES[self.res]
        self.filters = deque(maxlen = self.amount)

    def total_error(self):
        '''
        Return the total error: temporal error + native bf error
        '''
        temporal_error = float(1.0 / self.amount)
        total_error = self.error_rate + temporal_error
        return total_error

    def __contains__(self, key):
        for f in reversed(self.filters):
            if key in f:
                return True
        return False

    def check_expiration(self):
        filter = self.filters[0]
        if filter.expired:
            filter = DecayScalableBloomFilter(initial_capacity=self.initial_capacity,
                                              error_rate=self.error_rate,
                                              window_period=self.window_period)
            self.filters.append(filter)

    def add(self, key):
        self.check_expiration()
        if key in self:
            return True
        if not self.filters:
            filter = DecayScalableBloomFilter(initial_capacity=self.initial_capacity,
                                              error_rate=self.error_rate,
                                              window_period=self.window_period)
            self.filters.append(filter)
        else:
            filter = self.filters[-1]
        filter.add(key)
        return False

Add simple SlidingWindowScalableBloomFilter for combating overengineer habit

Response:
<REPLACE_OLD>  <REPLACE_NEW> import time

from collections import deque
from pybloom import ScalableBloomFilter

class DecayScalableBloomFilter(ScalableBloomFilter):
    '''
    Stepwise decaying Bloom Filter
    '''
    def __init__(self, initial_capacity=1000, error_rate=0.01, window_period = 60):
        super(DecayScalableBloomFilter, self).__init__(initial_capacity, error_rate)
        self.window_period = 60
        self.timestamp = time.time()
        self._expired = False

    @property
    def expired(self):
        if time.time() - self.timestamp > self.window_period:
            self._expired = True
        return self._expired

class SlidingWindowScalableBloomFilter(object):
    '''
    Sliding Window Bloom Filter using a coarse expiration
    '''

    VALID_RES = {'Min': 60,
                 'Hour': 3600,
                 'Day': 86400}

    def __init__(self, initial_capacity=1000, window_period = "10_Min"):
        self.initial_capacity = initial_capacity
        self.error_rate = 0.01
        self._setup_window_period(window_period)

    def _setup_window_period(self, window_period):
        try:
            self.amount, self.res = window_period.split('_')
            self.amount = int(self.amount)
        except ValueError:
            raise Exception('Invalid window period')
        self.window_period = SlidingWindowScalableBloomFilter.VALID_RES[self.res]
        self.filters = deque(maxlen = self.amount)

    def total_error(self):
        '''
        Return the total error: temporal error + native bf error
        '''
        temporal_error = float(1.0 / self.amount)
        total_error = self.error_rate + temporal_error
        return total_error

    def __contains__(self, key):
        for f in reversed(self.filters):
            if key in f:
                return True
        return False

    def check_expiration(self):
        filter = self.filters[0]
        if filter.expired:
            filter = DecayScalableBloomFilter(initial_capacity=self.initial_capacity,
                                              error_rate=self.error_rate,
                                              window_period=self.window_period)
            self.filters.append(filter)

    def add(self, key):
        self.check_expiration()
        if key in self:
            return True
        if not self.filters:
            filter = DecayScalableBloomFilter(initial_capacity=self.initial_capacity,
                                              error_rate=self.error_rate,
                                              window_period=self.window_period)
            self.filters.append(filter)
        else:
            filter = self.filters[-1]
        filter.add(key)
        return False

 <REPLACE_END> <|endoftext|> import time

from collections import deque
from pybloom import ScalableBloomFilter

class DecayScalableBloomFilter(ScalableBloomFilter):
    '''
    Stepwise decaying Bloom Filter
    '''
    def __init__(self, initial_capacity=1000, error_rate=0.01, window_period = 60):
        super(DecayScalableBloomFilter, self).__init__(initial_capacity, error_rate)
        self.window_period = 60
        self.timestamp = time.time()
        self._expired = False

    @property
    def expired(self):
        if time.time() - self.timestamp > self.window_period:
            self._expired = True
        return self._expired

class SlidingWindowScalableBloomFilter(object):
    '''
    Sliding Window Bloom Filter using a coarse expiration
    '''

    VALID_RES = {'Min': 60,
                 'Hour': 3600,
                 'Day': 86400}

    def __init__(self, initial_capacity=1000, window_period = "10_Min"):
        self.initial_capacity = initial_capacity
        self.error_rate = 0.01
        self._setup_window_period(window_period)

    def _setup_window_period(self, window_period):
        try:
            self.amount, self.res = window_period.split('_')
            self.amount = int(self.amount)
        except ValueError:
            raise Exception('Invalid window period')
        self.window_period = SlidingWindowScalableBloomFilter.VALID_RES[self.res]
        self.filters = deque(maxlen = self.amount)

    def total_error(self):
        '''
        Return the total error: temporal error + native bf error
        '''
        temporal_error = float(1.0 / self.amount)
        total_error = self.error_rate + temporal_error
        return total_error

    def __contains__(self, key):
        for f in reversed(self.filters):
            if key in f:
                return True
        return False

    def check_expiration(self):
        filter = self.filters[0]
        if filter.expired:
            filter = DecayScalableBloomFilter(initial_capacity=self.initial_capacity,
                                              error_rate=self.error_rate,
                                              window_period=self.window_period)
            self.filters.append(filter)

    def add(self, key):
        self.check_expiration()
        if key in self:
            return True
        if not self.filters:
            filter = DecayScalableBloomFilter(initial_capacity=self.initial_capacity,
                                              error_rate=self.error_rate,
                                              window_period=self.window_period)
            self.filters.append(filter)
        else:
            filter = self.filters[-1]
        filter.add(key)
        return False

Prompt: Add simple SlidingWindowScalableBloomFilter for combating overengineer habit


from random import randint

def get_random_number(start=1, end=10):
    """Generates and returns random number between :start: and :end:"""
    return randint(start, end)

Fix python random number generator.

Fix python random number generator.

Response:
<REPLACE_OLD> def <REPLACE_NEW> from random import randint

def <REPLACE_END> <REPLACE_OLD> """https://xkcd.com/221/"""
 <REPLACE_NEW> """Generates and returns random number between :start: and :end:"""
 <REPLACE_END> <REPLACE_OLD> 4
 <REPLACE_NEW> randint(start, end)
 <REPLACE_END> <|endoftext|> from random import randint

def get_random_number(start=1, end=10):
    """Generates and returns random number between :start: and :end:"""
    return randint(start, end)

Prompt:
Fix python random number generator.

def get_random_number(start=1, end=10):
    """https://xkcd.com/221/"""
    return 4


from pathlib import Path

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def filename(path):
    """Removes parent path from a relative or absolute filename

    Returns: the filename
    """
    return Path(path).name

Fix filename template tag docstring

Fix filename template tag docstring

Response:
<REPLACE_OLD> traceback lines <REPLACE_NEW> parent path <REPLACE_END> <REPLACE_OLD> string (if any). It has no effect when
    no 'Traceback' pattern has been found.

 <REPLACE_NEW> relative or absolute filename

 <REPLACE_END> <DELETE> raws before <DELETE_END> <REPLACE_OLD> 'Traceback' pattern
 <REPLACE_NEW> filename
 <REPLACE_END> <|endoftext|> from pathlib import Path

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def filename(path):
    """Removes parent path from a relative or absolute filename

    Returns: the filename
    """
    return Path(path).name

Prompt:
Fix filename template tag docstring

from pathlib import Path

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def filename(path):
    """Removes traceback lines from a string (if any). It has no effect when
    no 'Traceback' pattern has been found.

    Returns: raws before the 'Traceback' pattern
    """
    return Path(path).name


# -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-11-07 08:44
from __future__ import unicode_literals

from django.db import migrations

def populate_entity_type(apps, schema_editor):
    """Populate entity type from attached descriptor schema."""
    Entity = apps.get_model('flow', 'Entity')

    for entity in Entity.objects.all():
        if entity.descriptor_schema is not None:
            entity.type = entity.descriptor_schema.slug
            entity.save()

class Migration(migrations.Migration):

    dependencies = [
        ('flow', '0024_process_entity_3'),
    ]

    operations = [
        migrations.RunPython(populate_entity_type)
    ]

Add migration to populate Entity type

Add migration to populate Entity type

Response:
<INSERT> # -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-11-07 08:44
from __future__ import unicode_literals

from django.db import migrations

def populate_entity_type(apps, schema_editor):
 <INSERT_END> <INSERT>   """Populate entity type from attached descriptor schema."""
    Entity = apps.get_model('flow', 'Entity')

    for entity in Entity.objects.all():
        if entity.descriptor_schema is not None:
            entity.type = entity.descriptor_schema.slug
            entity.save()

class Migration(migrations.Migration):

    dependencies = [
        ('flow', '0024_process_entity_3'),
    ]

    operations = [
        migrations.RunPython(populate_entity_type)
    ]
 <INSERT_END> <|endoftext|> # -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-11-07 08:44
from __future__ import unicode_literals

from django.db import migrations

def populate_entity_type(apps, schema_editor):
    """Populate entity type from attached descriptor schema."""
    Entity = apps.get_model('flow', 'Entity')

    for entity in Entity.objects.all():
        if entity.descriptor_schema is not None:
            entity.type = entity.descriptor_schema.slug
            entity.save()

class Migration(migrations.Migration):

    dependencies = [
        ('flow', '0024_process_entity_3'),
    ]

    operations = [
        migrations.RunPython(populate_entity_type)
    ]

Prompt: Add migration to populate Entity type


# -------------------------------------------------------
import socket, traceback
import time
import json

import numpy as np
from scipy.spatial.transform import Rotation as R

host = ''
port = 2390

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))

filein = open('saveUDP.txt', 'w')
t0 = time.time()

# Place IMU x-axis into wind going direction when launching script
is_init_done = False
wind_yaw = 0
while time.time()-t0 < 200:
    try:
        message, address = s.recvfrom(4096)
        #print(message)
        msg = json.loads(message.decode("utf-8"))
        if is_init_done==False:
          wind_yaw = msg["Yaw"]
          is_init_done = True
        msg['Yaw'] = msg['Yaw']-wind_yaw
        print(msg)

        ypr = [msg['Yaw'], msg['Pitch'], msg['Roll']]
        seq = 'ZYX' # small letters from intrinsic rotations

        r = R.from_euler(seq, ypr, degrees=True)

        # Compute coordinates in NED (could be useful to compare position with GPS position for example)
        line_length = 10
        base_to_kite = [0, 0, line_length]
        base_to_kite_in_NED = r.apply(base_to_kite)

        # Express kite coordinates as great roll, great pitch and small yaw angles
        grpy=r.as_euler(seq="XYZ")
        print(grpy*180/np.pi)

        filein.write('%s\n' % (message))
    except (KeyboardInterrupt, SystemExit):
       raise
    except:
       traceback.print_exc()
filein.close()
# -------------------------------------------------------

Response:
<REPLACE_OLD> json

host <REPLACE_NEW> json

import numpy as np
from scipy.spatial.transform import Rotation as R

host <REPLACE_END> <REPLACE_OLD> time.time()
while <REPLACE_NEW> time.time()

# Place IMU x-axis into wind going direction when launching script
is_init_done = False
wind_yaw = 0
while <REPLACE_END> <REPLACE_OLD> print(message)
        <REPLACE_NEW> #print(message)
        msg = <REPLACE_END> <INSERT>        if is_init_done==False:
          wind_yaw = msg["Yaw"]
          is_init_done = True
        msg['Yaw'] = msg['Yaw']-wind_yaw
        print(msg)

        ypr = [msg['Yaw'], msg['Pitch'], msg['Roll']]
        seq = 'ZYX' # small letters from intrinsic rotations

        r = R.from_euler(seq, ypr, degrees=True)

        # Compute coordinates in NED (could be useful to compare position with GPS position for example)
        line_length = 10
        base_to_kite = [0, 0, line_length]
        base_to_kite_in_NED = r.apply(base_to_kite)

        # Express kite coordinates as great roll, great pitch and small yaw angles
        grpy=r.as_euler(seq="XYZ")
        print(grpy*180/np.pi)

 <INSERT_END> <|endoftext|> # -------------------------------------------------------
import socket, traceback
import time
import json

import numpy as np
from scipy.spatial.transform import Rotation as R

host = ''
port = 2390

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))

filein = open('saveUDP.txt', 'w')
t0 = time.time()

# Place IMU x-axis into wind going direction when launching script
is_init_done = False
wind_yaw = 0
while time.time()-t0 < 200:
    try:
        message, address = s.recvfrom(4096)
        #print(message)
        msg = json.loads(message.decode("utf-8"))
        if is_init_done==False:
          wind_yaw = msg["Yaw"]
          is_init_done = True
        msg['Yaw'] = msg['Yaw']-wind_yaw
        print(msg)

        ypr = [msg['Yaw'], msg['Pitch'], msg['Roll']]
        seq = 'ZYX' # small letters from intrinsic rotations

        r = R.from_euler(seq, ypr, degrees=True)

        # Compute coordinates in NED (could be useful to compare position with GPS position for example)
        line_length = 10
        base_to_kite = [0, 0, line_length]
        base_to_kite_in_NED = r.apply(base_to_kite)

        # Express kite coordinates as great roll, great pitch and small yaw angles
        grpy=r.as_euler(seq="XYZ")
        print(grpy*180/np.pi)

        filein.write('%s\n' % (message))
    except (KeyboardInterrupt, SystemExit):
       raise
    except:
       traceback.print_exc()
filein.close()
# -------------------------------------------------------

Prompt:
Add computations of great roll, pitch and small yaw angle (kite angles)

# -------------------------------------------------------
import socket, traceback
import time
import json

host = ''
port = 2390

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))

filein = open('saveUDP.txt', 'w')
t0 = time.time()
while time.time()-t0 < 200:
    try:
        message, address = s.recvfrom(4096)
        print(message)
        json.loads(message.decode("utf-8"))
        filein.write('%s\n' % (message))
    except (KeyboardInterrupt, SystemExit):
       raise
    except:
       traceback.print_exc()
filein.close()
# -------------------------------------------------------


#!/usr/bin/env python

from os.path import join

def configuration(parent_package = '', top_path = None):
    from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs
    config = Configuration('spatial', parent_package, top_path)

    config.add_data_dir('tests')

    #config.add_extension('_vq',
    #    sources=[join('src', 'vq_module.c'), join('src', 'vq.c')],
    #    include_dirs = [get_numpy_include_dirs()])
    config.add_sconscript('SConstruct')

    return config

if __name__ == '__main__':
    from numpy.distutils.core import setup
    setup(maintainer = "SciPy Developers",
          author = "Eric Jones",
          maintainer_email = "scipy-dev@scipy.org",
          description = "Clustering Algorithms (Information Theory)",
          url = "http://www.scipy.org",
          license = "SciPy License (BSD Style)",
          **configuration(top_path='').todict()
          )

Response:
<REPLACE_OLD> Configuration('cluster', <REPLACE_NEW> Configuration('spatial', <REPLACE_END> <|endoftext|> #!/usr/bin/env python

from os.path import join

def configuration(parent_package = '', top_path = None):
    from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs
    config = Configuration('spatial', parent_package, top_path)

    config.add_data_dir('tests')

    #config.add_extension('_vq',
    #    sources=[join('src', 'vq_module.c'), join('src', 'vq.c')],
    #    include_dirs = [get_numpy_include_dirs()])
    config.add_sconscript('SConstruct')

    return config

if __name__ == '__main__':
    from numpy.distutils.core import setup
    setup(maintainer = "SciPy Developers",
          author = "Eric Jones",
          maintainer_email = "scipy-dev@scipy.org",
          description = "Clustering Algorithms (Information Theory)",
          url = "http://www.scipy.org",
          license = "SciPy License (BSD Style)",
          **configuration(top_path='').todict()
          )

Prompt:
Update setup.py file for numscons build.

git-svn-id: 003f22d385e25de9cff933a5ea4efd77cb5e7b28@4958 d6536bca-fef9-0310-8506-e4c0a848fbcf

#!/usr/bin/env python

from os.path import join

def configuration(parent_package = '', top_path = None):
    from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs
    config = Configuration('cluster', parent_package, top_path)

    config.add_data_dir('tests')

    #config.add_extension('_vq',
    #    sources=[join('src', 'vq_module.c'), join('src', 'vq.c')],
    #    include_dirs = [get_numpy_include_dirs()])
    config.add_sconscript('SConstruct')

    return config

if __name__ == '__main__':
    from numpy.distutils.core import setup
    setup(maintainer = "SciPy Developers",
          author = "Eric Jones",
          maintainer_email = "scipy-dev@scipy.org",
          description = "Clustering Algorithms (Information Theory)",
          url = "http://www.scipy.org",
          license = "SciPy License (BSD Style)",
          **configuration(top_path='').todict()
          )


# Multi-GPU Keras training.
# Captured from https://github.com/kuza55/keras-extras and
# https://medium.com/@kuza55/transparent-multi-gpu-training-on-tensorflow-with-keras-8b0016fd9012.

from keras.layers import merge
from keras.layers.core import Lambda
from keras.models import Model

import tensorflow as tf

def make_parallel(model, gpu_count):
    def get_slice(data, idx, parts):
        shape = tf.shape(data)
        size = tf.concat([ shape[:1] // parts, shape[1:] ],axis=0)
        stride = tf.concat([ shape[:1] // parts, shape[1:]*0 ],axis=0)
        start = stride * idx
        return tf.slice(data, start, size)

    outputs_all = []
    for i in range(len(model.outputs)):
        outputs_all.append([])

    #Place a copy of the model on each GPU, each getting a slice of the batch
    for i in range(gpu_count):
        with tf.device('/gpu:%d' % i):
            with tf.name_scope('tower_%d' % i) as scope:

                inputs = []
                #Slice each input into a piece for processing on this GPU
                for x in model.inputs:
                    input_shape = tuple(x.get_shape().as_list())[1:]
                    slice_n = Lambda(get_slice, output_shape=input_shape, arguments={'idx':i,'parts':gpu_count})(x)
                    inputs.append(slice_n)

                outputs = model(inputs)

                if not isinstance(outputs, list):
                    outputs = [outputs]

                #Save all the outputs for merging back together later
                for l in range(len(outputs)):
                    outputs_all[l].append(outputs[l])

    # merge outputs on CPU
    with tf.device('/cpu:0'):
        merged = []
        for outputs in outputs_all:
            merged.append(merge(outputs, mode='concat', concat_axis=0))

        return Model(input=model.inputs, output=merged)

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(4000, input_dim=8000, activation='tanh'))
model.add(Dense(2000, input_dim=8000, activation='relu'))
model.add(Dense(500, activation='relu'))
model.add(Dense(300, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
print (model.summary())
model = make_parallel(model, 4)
optimizer = keras.optimizers.Adam(lr=0.0001)
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
x = np.random.rand(131072, 8000)
y = np.random.randint(0, 2, (131072, 1))
model.fit(x, y, batch_size=2048*4)

Add an example from kuza55.

Add an example from kuza55.

Response:
<REPLACE_OLD>  <REPLACE_NEW> # Multi-GPU Keras training.
# Captured from https://github.com/kuza55/keras-extras and
# https://medium.com/@kuza55/transparent-multi-gpu-training-on-tensorflow-with-keras-8b0016fd9012.

from keras.layers import merge
from keras.layers.core import Lambda
from keras.models import Model

import tensorflow as tf

def make_parallel(model, gpu_count):
    def get_slice(data, idx, parts):
        shape = tf.shape(data)
        size = tf.concat([ shape[:1] // parts, shape[1:] ],axis=0)
        stride = tf.concat([ shape[:1] // parts, shape[1:]*0 ],axis=0)
        start = stride * idx
        return tf.slice(data, start, size)

    outputs_all = []
    for i in range(len(model.outputs)):
        outputs_all.append([])

    #Place a copy of the model on each GPU, each getting a slice of the batch
    for i in range(gpu_count):
        with tf.device('/gpu:%d' % i):
            with tf.name_scope('tower_%d' % i) as scope:

                inputs = []
                #Slice each input into a piece for processing on this GPU
                for x in model.inputs:
                    input_shape = tuple(x.get_shape().as_list())[1:]
                    slice_n = Lambda(get_slice, output_shape=input_shape, arguments={'idx':i,'parts':gpu_count})(x)
                    inputs.append(slice_n)

                outputs = model(inputs)

                if not isinstance(outputs, list):
                    outputs = [outputs]

                #Save all the outputs for merging back together later
                for l in range(len(outputs)):
                    outputs_all[l].append(outputs[l])

    # merge outputs on CPU
    with tf.device('/cpu:0'):
        merged = []
        for outputs in outputs_all:
            merged.append(merge(outputs, mode='concat', concat_axis=0))

        return Model(input=model.inputs, output=merged)

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(4000, input_dim=8000, activation='tanh'))
model.add(Dense(2000, input_dim=8000, activation='relu'))
model.add(Dense(500, activation='relu'))
model.add(Dense(300, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
print (model.summary())
model = make_parallel(model, 4)
optimizer = keras.optimizers.Adam(lr=0.0001)
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
x = np.random.rand(131072, 8000)
y = np.random.randint(0, 2, (131072, 1))
model.fit(x, y, batch_size=2048*4)
 <REPLACE_END> <|endoftext|> # Multi-GPU Keras training.
# Captured from https://github.com/kuza55/keras-extras and
# https://medium.com/@kuza55/transparent-multi-gpu-training-on-tensorflow-with-keras-8b0016fd9012.

from keras.layers import merge
from keras.layers.core import Lambda
from keras.models import Model

import tensorflow as tf

def make_parallel(model, gpu_count):
    def get_slice(data, idx, parts):
        shape = tf.shape(data)
        size = tf.concat([ shape[:1] // parts, shape[1:] ],axis=0)
        stride = tf.concat([ shape[:1] // parts, shape[1:]*0 ],axis=0)
        start = stride * idx
        return tf.slice(data, start, size)

    outputs_all = []
    for i in range(len(model.outputs)):
        outputs_all.append([])

    #Place a copy of the model on each GPU, each getting a slice of the batch
    for i in range(gpu_count):
        with tf.device('/gpu:%d' % i):
            with tf.name_scope('tower_%d' % i) as scope:

                inputs = []
                #Slice each input into a piece for processing on this GPU
                for x in model.inputs:
                    input_shape = tuple(x.get_shape().as_list())[1:]
                    slice_n = Lambda(get_slice, output_shape=input_shape, arguments={'idx':i,'parts':gpu_count})(x)
                    inputs.append(slice_n)

                outputs = model(inputs)

                if not isinstance(outputs, list):
                    outputs = [outputs]

                #Save all the outputs for merging back together later
                for l in range(len(outputs)):
                    outputs_all[l].append(outputs[l])

    # merge outputs on CPU
    with tf.device('/cpu:0'):
        merged = []
        for outputs in outputs_all:
            merged.append(merge(outputs, mode='concat', concat_axis=0))

        return Model(input=model.inputs, output=merged)

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(4000, input_dim=8000, activation='tanh'))
model.add(Dense(2000, input_dim=8000, activation='relu'))
model.add(Dense(500, activation='relu'))
model.add(Dense(300, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
print (model.summary())
model = make_parallel(model, 4)
optimizer = keras.optimizers.Adam(lr=0.0001)
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
x = np.random.rand(131072, 8000)
y = np.random.randint(0, 2, (131072, 1))
model.fit(x, y, batch_size=2048*4)

Prompt: Add an example from kuza55.


# Simple function for primeness test
def is_prime(number):

  import math

  limit = int(math.sqrt(number)) + 1

  if number == 2:
    return True
  if number % 2 == 0:
    return False
  for i in range(3, limit, 2):
    if number % i == 0:
      return False

  return True

import math

large_number = 600851475143
limit_large_number = int(math.sqrt(large_number))
answer = -1

for i in range(2, limit_large_number):
  if large_number % i == 0 and is_prime(i):  # short circuit evaluation FTW!
    answer = i

print("Result:", answer)

Solve problem 3 in Python

Solve problem 3 in Python

Response:
<INSERT> # Simple function for primeness test
def is_prime(number):

 <INSERT_END> <INSERT> import math

  limit = int(math.sqrt(number)) + 1

  if number == 2:
    return True
  if number % 2 == 0:
    return False
  for i in range(3, limit, 2):
    if number % i == 0:
      return False

  return True

import math

large_number = 600851475143
limit_large_number = int(math.sqrt(large_number))
answer = -1

for i in range(2, limit_large_number):
  if large_number % i == 0 and is_prime(i):  # short circuit evaluation FTW!
    answer = i

print("Result:", answer)
 <INSERT_END> <|endoftext|> # Simple function for primeness test
def is_prime(number):

  import math

  limit = int(math.sqrt(number)) + 1

  if number == 2:
    return True
  if number % 2 == 0:
    return False
  for i in range(3, limit, 2):
    if number % i == 0:
      return False

  return True

import math

large_number = 600851475143
limit_large_number = int(math.sqrt(large_number))
answer = -1

for i in range(2, limit_large_number):
  if large_number % i == 0 and is_prime(i):  # short circuit evaluation FTW!
    answer = i

print("Result:", answer)

Prompt: Solve problem 3 in Python


import json

import django.test

from .user import UserMixin

class Client(django.test.Client):

    def patch_json(self, path, data=None, **kwargs):
        return self.patch(path, **self._json_kwargs(data, kwargs))

    def post_json(self, path, data=None, **kwargs):
        return self.post(path, **self._json_kwargs(data, kwargs))

    def put_json(self, path, data=None, **kwargs):
        return self.put(path, **self._json_kwargs(data, kwargs))

    def patch_json(self, path, data=None, **kwargs):
        return self.patch(path, **self._json_kwargs(data, kwargs))

    def _json_kwargs(self, data, kwargs):
        if data is not None:
            data = json.dumps(data)
        kwargs['data'] = data
        kwargs['content_type'] = 'application/json'
        return kwargs

class FunctionalTestCase(django.test.TestCase, UserMixin):

    """Base class for view tests.

    It adds the following to Django's `TestCase`:

        - Convenient user creation & login
        - Convenient POSTs, PUTs, and PATCHes with a JSON body

    """

    client_class = Client

Response:
<INSERT> **self._json_kwargs(data, kwargs))

    def patch_json(self, path, data=None, **kwargs):
        return self.patch(path, <INSERT_END> <|endoftext|> import json

import django.test

from .user import UserMixin

class Client(django.test.Client):

    def patch_json(self, path, data=None, **kwargs):
        return self.patch(path, **self._json_kwargs(data, kwargs))

    def post_json(self, path, data=None, **kwargs):
        return self.post(path, **self._json_kwargs(data, kwargs))

    def put_json(self, path, data=None, **kwargs):
        return self.put(path, **self._json_kwargs(data, kwargs))

    def patch_json(self, path, data=None, **kwargs):
        return self.patch(path, **self._json_kwargs(data, kwargs))

    def _json_kwargs(self, data, kwargs):
        if data is not None:
            data = json.dumps(data)
        kwargs['data'] = data
        kwargs['content_type'] = 'application/json'
        return kwargs

class FunctionalTestCase(django.test.TestCase, UserMixin):

    """Base class for view tests.

    It adds the following to Django's `TestCase`:

        - Convenient user creation & login
        - Convenient POSTs, PUTs, and PATCHes with a JSON body

    """

    client_class = Client

Prompt:
Add patch_json method to our test Client

import json

import django.test

from .user import UserMixin

class Client(django.test.Client):

    def patch_json(self, path, data=None, **kwargs):
        return self.patch(path, **self._json_kwargs(data, kwargs))

    def post_json(self, path, data=None, **kwargs):
        return self.post(path, **self._json_kwargs(data, kwargs))

    def put_json(self, path, data=None, **kwargs):
        return self.put(path, **self._json_kwargs(data, kwargs))

    def _json_kwargs(self, data, kwargs):
        if data is not None:
            data = json.dumps(data)
        kwargs['data'] = data
        kwargs['content_type'] = 'application/json'
        return kwargs

class FunctionalTestCase(django.test.TestCase, UserMixin):

    """Base class for view tests.

    It adds the following to Django's `TestCase`:

        - Convenient user creation & login
        - Convenient POSTs, PUTs, and PATCHes with a JSON body

    """

    client_class = Client


from rest_framework import permissions

class CustomUserAPIPermission(permissions.BasePermission):
    """
    Define custom permission for UserAPI and UserDetailAPI

    GET : only accessible by admin
    POST: public
    """

    def has_permission(self, request, view):
        if request.method == 'GET':
            return request.user and request.user.is_staff

        elif request.method == 'POST':
            return True
        return True

Write permission for user api, make POST public

Response:
<INSERT> from rest_framework import permissions

class CustomUserAPIPermission(permissions.BasePermission):
 <INSERT_END> <INSERT>   """
    Define custom permission for UserAPI and UserDetailAPI

    GET : only accessible by admin
    POST: public
    """

    def has_permission(self, request, view):
        if request.method == 'GET':
            return request.user and request.user.is_staff

        elif request.method == 'POST':
            return True
        return True
 <INSERT_END> <|endoftext|> from rest_framework import permissions

class CustomUserAPIPermission(permissions.BasePermission):
    """
    Define custom permission for UserAPI and UserDetailAPI

    GET : only accessible by admin
    POST: public
    """

    def has_permission(self, request, view):
        if request.method == 'GET':
            return request.user and request.user.is_staff

        elif request.method == 'POST':
            return True
        return True

Prompt: Write permission for user api, make POST public


"""Typer, build great CLIs. Easy to code. Based on Python type hints."""

__version__ = "0.0.4"

from click.exceptions import (  # noqa
    Abort,
    Exit,
)
from click.termui import (  # noqa
    clear,
    confirm,
    echo_via_pager,
    edit,
    get_terminal_size,
    getchar,
    launch,
    pause,
    progressbar,
    prompt,
    secho,
    style,
    unstyle,
)
from click.utils import (  # noqa
    echo,
    format_filename,
    get_app_dir,
    get_binary_stream,
    get_text_stream,
    open_file,
)

from .main import Typer, run  # noqa
from .models import BinaryFileRead, BinaryFileWrite, Context, TextFile  # noqa
from .params import Argument, Option  # noqa

Response:
<REPLACE_OLD> BadArgumentUsage,
    BadOptionUsage,
    BadParameter,
    ClickException,
    FileError,
    MissingParameter,
    NoSuchOption,
    UsageError,
)
from <REPLACE_NEW> Exit,
)
from <REPLACE_END> <DELETE> get_os_args,
    <DELETE_END> <|endoftext|> """Typer, build great CLIs. Easy to code. Based on Python type hints."""

__version__ = "0.0.4"

from click.exceptions import (  # noqa
    Abort,
    Exit,
)
from click.termui import (  # noqa
    clear,
    confirm,
    echo_via_pager,
    edit,
    get_terminal_size,
    getchar,
    launch,
    pause,
    progressbar,
    prompt,
    secho,
    style,
    unstyle,
)
from click.utils import (  # noqa
    echo,
    format_filename,
    get_app_dir,
    get_binary_stream,
    get_text_stream,
    open_file,
)

from .main import Typer, run  # noqa
from .models import BinaryFileRead, BinaryFileWrite, Context, TextFile  # noqa
from .params import Argument, Option  # noqa

Prompt:
:fire: Clean exports from typer, remove unneeded Click components

and add Exit exception

"""Typer, build great CLIs. Easy to code. Based on Python type hints."""

__version__ = "0.0.4"

from click.exceptions import (  # noqa
    Abort,
    BadArgumentUsage,
    BadOptionUsage,
    BadParameter,
    ClickException,
    FileError,
    MissingParameter,
    NoSuchOption,
    UsageError,
)
from click.termui import (  # noqa
    clear,
    confirm,
    echo_via_pager,
    edit,
    get_terminal_size,
    getchar,
    launch,
    pause,
    progressbar,
    prompt,
    secho,
    style,
    unstyle,
)
from click.utils import (  # noqa
    echo,
    format_filename,
    get_app_dir,
    get_binary_stream,
    get_os_args,
    get_text_stream,
    open_file,
)

from .main import Typer, run  # noqa
from .models import BinaryFileRead, BinaryFileWrite, Context, TextFile  # noqa
from .params import Argument, Option  # noqa


import time
from twisted.internet import reactor

from .http_protocol import HTTPFactory

class Server(object):

    def __init__(self, channel_layer, host="127.0.0.1", port=8000, signal_handlers=True):
        self.channel_layer = channel_layer
        self.host = host
        self.port = port
        self.signal_handlers = signal_handlers

    def run(self):
        self.factory = HTTPFactory(self.channel_layer)
        reactor.listenTCP(self.port, self.factory, interface=self.host)
        reactor.callInThread(self.backend_reader)
        reactor.run(installSignalHandlers=self.signal_handlers)

    def backend_reader(self):
        """
        Run in a separate thread; reads messages from the backend.
        """
        while True:
            channels = self.factory.reply_channels()
            # Quit if reactor is stopping
            if not reactor.running:
                return
            # Don't do anything if there's no channels to listen on
            if channels:
                channel, message = self.channel_layer.receive_many(channels, block=True)
            else:
                time.sleep(0.1)
                continue
            # Wait around if there's nothing received
            if channel is None:
                time.sleep(0.05)
                continue
            # Deal with the message
            self.factory.dispatch_reply(channel, message)

Response:
<REPLACE_OLD> port=8000):
 <REPLACE_NEW> port=8000, signal_handlers=True):
 <REPLACE_END> <REPLACE_OLD> port

 <REPLACE_NEW> port
        self.signal_handlers = signal_handlers

 <REPLACE_END> <REPLACE_OLD> reactor.run()

 <REPLACE_NEW> reactor.run(installSignalHandlers=self.signal_handlers)

 <REPLACE_END> <|endoftext|> import time
from twisted.internet import reactor

from .http_protocol import HTTPFactory

class Server(object):

    def __init__(self, channel_layer, host="127.0.0.1", port=8000, signal_handlers=True):
        self.channel_layer = channel_layer
        self.host = host
        self.port = port
        self.signal_handlers = signal_handlers

    def run(self):
        self.factory = HTTPFactory(self.channel_layer)
        reactor.listenTCP(self.port, self.factory, interface=self.host)
        reactor.callInThread(self.backend_reader)
        reactor.run(installSignalHandlers=self.signal_handlers)

    def backend_reader(self):
        """
        Run in a separate thread; reads messages from the backend.
        """
        while True:
            channels = self.factory.reply_channels()
            # Quit if reactor is stopping
            if not reactor.running:
                return
            # Don't do anything if there's no channels to listen on
            if channels:
                channel, message = self.channel_layer.receive_many(channels, block=True)
            else:
                time.sleep(0.1)
                continue
            # Wait around if there's nothing received
            if channel is None:
                time.sleep(0.05)
                continue
            # Deal with the message
            self.factory.dispatch_reply(channel, message)

Prompt:
Allow signal handlers to be disabled to run in subthread

import time
from twisted.internet import reactor

from .http_protocol import HTTPFactory

class Server(object):

    def __init__(self, channel_layer, host="127.0.0.1", port=8000):
        self.channel_layer = channel_layer
        self.host = host
        self.port = port

    def run(self):
        self.factory = HTTPFactory(self.channel_layer)
        reactor.listenTCP(self.port, self.factory, interface=self.host)
        reactor.callInThread(self.backend_reader)
        reactor.run()

    def backend_reader(self):
        """
        Run in a separate thread; reads messages from the backend.
        """
        while True:
            channels = self.factory.reply_channels()
            # Quit if reactor is stopping
            if not reactor.running:
                return
            # Don't do anything if there's no channels to listen on
            if channels:
                channel, message = self.channel_layer.receive_many(channels, block=True)
            else:
                time.sleep(0.1)
                continue
            # Wait around if there's nothing received
            if channel is None:
                time.sleep(0.05)
                continue
            # Deal with the message
            self.factory.dispatch_reply(channel, message)