from __future__ import unicode_literals
try:
    from itertools import zip_longest
except ImportError:
    from itertools import izip_longest as zip_longest

from django.template import Library
from django.utils.http import urlencode

register = Library()

@register.filter
def slice(items, group_size=1):
    args = [iter(items)] * group_size
    return (filter(None, group)
            for group in zip_longest(*args, fillvalue=None))

@register.simple_tag(takes_context=True)
def get_sort_by_url(context, field, descending=False):
    request = context['request']
    request_get = request.GET.dict()
    if descending:
        request_get['sort_by'] = '-' + field
    else:
        request_get['sort_by'] = field
    return '%s?%s' % (request.path, urlencode(request_get))

@register.simple_tag(takes_context=True)
def get_sort_by_url_toggle(context, field):
    request = context['request']
    request_get = request.GET.copy()
    if field == request_get.get('sort_by'):
        new_sort_by = u'-%s' % field  # descending sort
    else:
        new_sort_by = field  # ascending sort
    request_get['sort_by'] = new_sort_by
    return '%s?%s' % (request.path, request_get.urlencode())

Response:
<REPLACE_OLD> request.GET.dict()
 <REPLACE_NEW> request.GET.copy()
 <REPLACE_END> <REPLACE_OLD> '-%s' <REPLACE_NEW> u'-%s' <REPLACE_END> <REPLACE_OLD> urlencode(request_get))
 <REPLACE_NEW> request_get.urlencode())
 <REPLACE_END> <|endoftext|> from __future__ import unicode_literals
try:
    from itertools import zip_longest
except ImportError:
    from itertools import izip_longest as zip_longest

from django.template import Library
from django.utils.http import urlencode

register = Library()

@register.filter
def slice(items, group_size=1):
    args = [iter(items)] * group_size
    return (filter(None, group)
            for group in zip_longest(*args, fillvalue=None))

@register.simple_tag(takes_context=True)
def get_sort_by_url(context, field, descending=False):
    request = context['request']
    request_get = request.GET.dict()
    if descending:
        request_get['sort_by'] = '-' + field
    else:
        request_get['sort_by'] = field
    return '%s?%s' % (request.path, urlencode(request_get))

@register.simple_tag(takes_context=True)
def get_sort_by_url_toggle(context, field):
    request = context['request']
    request_get = request.GET.copy()
    if field == request_get.get('sort_by'):
        new_sort_by = u'-%s' % field  # descending sort
    else:
        new_sort_by = field  # ascending sort
    request_get['sort_by'] = new_sort_by
    return '%s?%s' % (request.path, request_get.urlencode())

Prompt:
Fix get_sort_by_toggle to work with QueryDicts with multiple values

from __future__ import unicode_literals
try:
    from itertools import zip_longest
except ImportError:
    from itertools import izip_longest as zip_longest

from django.template import Library
from django.utils.http import urlencode

register = Library()

@register.filter
def slice(items, group_size=1):
    args = [iter(items)] * group_size
    return (filter(None, group)
            for group in zip_longest(*args, fillvalue=None))

@register.simple_tag(takes_context=True)
def get_sort_by_url(context, field, descending=False):
    request = context['request']
    request_get = request.GET.dict()
    if descending:
        request_get['sort_by'] = '-' + field
    else:
        request_get['sort_by'] = field
    return '%s?%s' % (request.path, urlencode(request_get))

@register.simple_tag(takes_context=True)
def get_sort_by_url_toggle(context, field):
    request = context['request']
    request_get = request.GET.dict()
    if field == request_get.get('sort_by'):
        new_sort_by = '-%s' % field  # descending sort
    else:
        new_sort_by = field  # ascending sort
    request_get['sort_by'] = new_sort_by
    return '%s?%s' % (request.path, urlencode(request_get))


import warnings

from django.utils.deprecation import MiddlewareMixin
from wagtail.core.models import Site
from wagtail.utils.deprecation import RemovedInWagtail211Warning

class SiteMiddleware(MiddlewareMixin):
    def process_request(self, request):
        """
        Set request.site to contain the Site object responsible for handling this request,
        according to hostname matching rules
        """
        warnings.warn(
            'Wagtail SiteMiddleware and the use of request.site is deprecated '
            'and will be removed in Wagtail 2.11. Update your middleware settings.',
            RemovedInWagtail211Warning, stacklevel=2
        )

        try:
            request.site = Site.find_for_request(request)
        except Site.DoesNotExist:
            request.site = None

Response:
<REPLACE_OLD> RemovedInWagtail28Warning

class <REPLACE_NEW> RemovedInWagtail211Warning

class <REPLACE_END> <REPLACE_OLD> 'wagtail <REPLACE_NEW> 'Wagtail <REPLACE_END> <REPLACE_OLD> wagtail 2.8. <REPLACE_NEW> Wagtail 2.11. <REPLACE_END> <REPLACE_OLD> RemovedInWagtail28Warning, <REPLACE_NEW> RemovedInWagtail211Warning, <REPLACE_END> <REPLACE_OLD> request._wagtail_site <REPLACE_NEW> request.site <REPLACE_END> <REPLACE_OLD> request._wagtail_site <REPLACE_NEW> request.site <REPLACE_END> <|endoftext|> import warnings

from django.utils.deprecation import MiddlewareMixin
from wagtail.core.models import Site
from wagtail.utils.deprecation import RemovedInWagtail211Warning

class SiteMiddleware(MiddlewareMixin):
    def process_request(self, request):
        """
        Set request.site to contain the Site object responsible for handling this request,
        according to hostname matching rules
        """
        warnings.warn(
            'Wagtail SiteMiddleware and the use of request.site is deprecated '
            'and will be removed in Wagtail 2.11. Update your middleware settings.',
            RemovedInWagtail211Warning, stacklevel=2
        )

        try:
            request.site = Site.find_for_request(request)
        except Site.DoesNotExist:
            request.site = None

Prompt:
Revert SiteMiddleware to setting request.site

This way, SiteMiddleware continues to support existing user / third-party code that has not yet been migrated from request.site to Site.find_for_request

import warnings

from django.utils.deprecation import MiddlewareMixin
from wagtail.core.models import Site
from wagtail.utils.deprecation import RemovedInWagtail28Warning

class SiteMiddleware(MiddlewareMixin):
    def process_request(self, request):
        """
        Set request.site to contain the Site object responsible for handling this request,
        according to hostname matching rules
        """
        warnings.warn(
            'wagtail SiteMiddleware and the use of request.site is deprecated '
            'and will be removed in wagtail 2.8. Update your middleware settings.',
            RemovedInWagtail28Warning, stacklevel=2
        )

        try:
            request._wagtail_site = Site.find_for_request(request)
        except Site.DoesNotExist:
            request._wagtail_site = None


import os
import sys
import xml.etree.ElementTree as ET
from rdflib import Graph, Namespace, Literal

trips_ns = Namespace('http://trips.ihmc.us/concepts/')
isa_rel = Namespace('http://trips.ihmc.us/relations/').term('isa')

def save_hierarchy(g, path):
    with open(path, 'wb') as out_file:
        g_bytes = g.serialize(format='nt')
        # Replace extra new lines in string and get rid of empty line at end
        g_bytes = g_bytes.replace(b'\n\n', b'\n').strip()
        # Split into rows and sort
        rows = g_bytes.split(b'\n')
        rows.sort()
        g_bytes = b'\n'.join(rows)
        out_file.write(g_bytes)

def make_hierarchy(tree):
    g = Graph()
    concepts = tree.findall('concept')
    for concept in concepts:
        name = concept.attrib['name'].replace('ont::', '')
        if name == 'root':
            continue
        term = trips_ns.term(name)
        relations = concept.find("relation[@label='inherit']")
        related_names = [rr.strip().replace('ont::', '') for rr
                        in relations.text.strip().split('\n')]
        for related_name in related_names:
            related_term = trips_ns.term(related_name)
            g.add((term, isa_rel, related_term))
    return g

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('Usage: python make_trips_ontology.py /path/to/trips-ont-dsl.xml')
        sys.exit()
    fname = sys.argv[1]
    tree = ET.parse(fname)
    g = make_hierarchy(tree)
    save_hierarchy(g, 'trips_ontology.rdf')

Implement script to make TRIPS ontology

Implement script to make TRIPS ontology

Response:
<REPLACE_OLD>  <REPLACE_NEW> import os
import sys
import xml.etree.ElementTree as ET
from rdflib import Graph, Namespace, Literal

trips_ns = Namespace('http://trips.ihmc.us/concepts/')
isa_rel = Namespace('http://trips.ihmc.us/relations/').term('isa')

def save_hierarchy(g, path):
    with open(path, 'wb') as out_file:
        g_bytes = g.serialize(format='nt')
        # Replace extra new lines in string and get rid of empty line at end
        g_bytes = g_bytes.replace(b'\n\n', b'\n').strip()
        # Split into rows and sort
        rows = g_bytes.split(b'\n')
        rows.sort()
        g_bytes = b'\n'.join(rows)
        out_file.write(g_bytes)

def make_hierarchy(tree):
    g = Graph()
    concepts = tree.findall('concept')
    for concept in concepts:
        name = concept.attrib['name'].replace('ont::', '')
        if name == 'root':
            continue
        term = trips_ns.term(name)
        relations = concept.find("relation[@label='inherit']")
        related_names = [rr.strip().replace('ont::', '') for rr
                        in relations.text.strip().split('\n')]
        for related_name in related_names:
            related_term = trips_ns.term(related_name)
            g.add((term, isa_rel, related_term))
    return g

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('Usage: python make_trips_ontology.py /path/to/trips-ont-dsl.xml')
        sys.exit()
    fname = sys.argv[1]
    tree = ET.parse(fname)
    g = make_hierarchy(tree)
    save_hierarchy(g, 'trips_ontology.rdf')
 <REPLACE_END> <|endoftext|> import os
import sys
import xml.etree.ElementTree as ET
from rdflib import Graph, Namespace, Literal

trips_ns = Namespace('http://trips.ihmc.us/concepts/')
isa_rel = Namespace('http://trips.ihmc.us/relations/').term('isa')

def save_hierarchy(g, path):
    with open(path, 'wb') as out_file:
        g_bytes = g.serialize(format='nt')
        # Replace extra new lines in string and get rid of empty line at end
        g_bytes = g_bytes.replace(b'\n\n', b'\n').strip()
        # Split into rows and sort
        rows = g_bytes.split(b'\n')
        rows.sort()
        g_bytes = b'\n'.join(rows)
        out_file.write(g_bytes)

def make_hierarchy(tree):
    g = Graph()
    concepts = tree.findall('concept')
    for concept in concepts:
        name = concept.attrib['name'].replace('ont::', '')
        if name == 'root':
            continue
        term = trips_ns.term(name)
        relations = concept.find("relation[@label='inherit']")
        related_names = [rr.strip().replace('ont::', '') for rr
                        in relations.text.strip().split('\n')]
        for related_name in related_names:
            related_term = trips_ns.term(related_name)
            g.add((term, isa_rel, related_term))
    return g

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('Usage: python make_trips_ontology.py /path/to/trips-ont-dsl.xml')
        sys.exit()
    fname = sys.argv[1]
    tree = ET.parse(fname)
    g = make_hierarchy(tree)
    save_hierarchy(g, 'trips_ontology.rdf')

Prompt: Implement script to make TRIPS ontology


"""
Validator functions
"""
from urllib import request
from urllib.error import HTTPError

from django.core.exceptions import ValidationError

import PyPDF2

def online_document(url):
    """Check if online document is available."""
    try:
        online_resource = request.urlopen(url)
    except HTTPError as exception:
        if exception.code == 410:
            raise ValidationError("Online document was removed.")  # This is the code returned by Google Drive
        elif exception.code == 403:
            req = request.Request(url, headers={'User-Agent' : "lowFAT"})
            online_resource = request.urlopen(req)
        else:
            raise ValidationError("Error! HTTP status code is {}.".format(exception.code))

    # Need to test if website didn't redirect the request to another resource.
    if url != online_resource.geturl() or online_resource.getcode() != 200:
        raise ValidationError("Can't access online document.")

def pdf(value):
    """Check if filename looks like a PDF file."""

    filename = value.name.lower()

    if not filename.endswith(".pdf"):
        raise ValidationError("File name doesn't look to be a PDF file.")

    try:
        pdf_file = PyPDF2.PdfFileReader(value.file)  # pylint: disable=unused-variable
    except:
        raise ValidationError("File doesn't look to be a PDF file.")

Fix problem with sites that blocks bots

Fix problem with sites that blocks bots

Response:
<REPLACE_OLD> Drive

 <REPLACE_NEW> Drive
        elif exception.code == 403:
            req = request.Request(url, headers={'User-Agent' : "lowFAT"})
            online_resource = request.urlopen(req)
        else:
            raise ValidationError("Error! HTTP status code is {}.".format(exception.code))

 <REPLACE_END> <|endoftext|> """
Validator functions
"""
from urllib import request
from urllib.error import HTTPError

from django.core.exceptions import ValidationError

import PyPDF2

def online_document(url):
    """Check if online document is available."""
    try:
        online_resource = request.urlopen(url)
    except HTTPError as exception:
        if exception.code == 410:
            raise ValidationError("Online document was removed.")  # This is the code returned by Google Drive
        elif exception.code == 403:
            req = request.Request(url, headers={'User-Agent' : "lowFAT"})
            online_resource = request.urlopen(req)
        else:
            raise ValidationError("Error! HTTP status code is {}.".format(exception.code))

    # Need to test if website didn't redirect the request to another resource.
    if url != online_resource.geturl() or online_resource.getcode() != 200:
        raise ValidationError("Can't access online document.")

def pdf(value):
    """Check if filename looks like a PDF file."""

    filename = value.name.lower()

    if not filename.endswith(".pdf"):
        raise ValidationError("File name doesn't look to be a PDF file.")

    try:
        pdf_file = PyPDF2.PdfFileReader(value.file)  # pylint: disable=unused-variable
    except:
        raise ValidationError("File doesn't look to be a PDF file.")

Prompt:
Fix problem with sites that blocks bots

"""
Validator functions
"""
from urllib import request
from urllib.error import HTTPError

from django.core.exceptions import ValidationError

import PyPDF2

def online_document(url):
    """Check if online document is available."""
    try:
        online_resource = request.urlopen(url)
    except HTTPError as exception:
        if exception.code == 410:
            raise ValidationError("Online document was removed.")  # This is the code returned by Google Drive

    # Need to test if website didn't redirect the request to another resource.
    if url != online_resource.geturl() or online_resource.getcode() != 200:
        raise ValidationError("Can't access online document.")

def pdf(value):
    """Check if filename looks like a PDF file."""

    filename = value.name.lower()

    if not filename.endswith(".pdf"):
        raise ValidationError("File name doesn't look to be a PDF file.")

    try:
        pdf_file = PyPDF2.PdfFileReader(value.file)  # pylint: disable=unused-variable
    except:
        raise ValidationError("File doesn't look to be a PDF file.")


"""Add policycondition table, remove unused condition column from policy table

Revision ID: dceb6cd3c41e
Revises: b9131d0686eb
Create Date: 2019-07-02 12:19:19.646528

"""

# revision identifiers, used by Alembic.
revision = 'dceb6cd3c41e'
down_revision = 'b9131d0686eb'

from alembic import op, context
import sqlalchemy as sa
from sqlalchemy.schema import Sequence, CreateSequence

def dialect_supports_sequences():
    migration_context = context.get_context()
    return migration_context.dialect.supports_sequences

def create_seq(seq):
    if dialect_supports_sequences():
        op.execute(CreateSequence(seq))

def upgrade():
    try:
        try:
            create_seq(Sequence('policycondition_seq'))
        except Exception as _e:
            pass
        op.create_table('policycondition',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('policy_id', sa.Integer(), nullable=False),
        sa.Column('section', sa.Unicode(length=255), nullable=False),
        sa.Column('key', sa.Unicode(length=255), nullable=False),
        sa.Column('value', sa.Unicode(length=2000), nullable=True),
        sa.Column('comparator', sa.Unicode(length=255), nullable=True),
        sa.ForeignKeyConstraint(['policy_id'], ['policy.id'], ),
        sa.PrimaryKeyConstraint('id'),
        mysql_row_format='DYNAMIC'
        )
    except Exception as exx:
        print("Could not create table policycondition: {!r}".format(exx))
    try:
        op.drop_column('policy', 'condition')
    except Exception as exx:
        print("Could not drop column policy.condition: {!r}".format(exx))

def downgrade():
    op.add_column('policy', sa.Column('condition', sa.INTEGER(), nullable=False))
    op.drop_table('policycondition')

Add migration script for policycondition table

Response:
<REPLACE_OLD>  <REPLACE_NEW> """Add policycondition table, remove unused condition column from policy table

Revision ID: dceb6cd3c41e
Revises: b9131d0686eb
Create Date: 2019-07-02 12:19:19.646528

"""

# revision identifiers, used by Alembic.
revision = 'dceb6cd3c41e'
down_revision = 'b9131d0686eb'

from alembic import op, context
import sqlalchemy as sa
from sqlalchemy.schema import Sequence, CreateSequence

def dialect_supports_sequences():
    migration_context = context.get_context()
    return migration_context.dialect.supports_sequences

def create_seq(seq):
    if dialect_supports_sequences():
        op.execute(CreateSequence(seq))

def upgrade():
    try:
        try:
            create_seq(Sequence('policycondition_seq'))
        except Exception as _e:
            pass
        op.create_table('policycondition',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('policy_id', sa.Integer(), nullable=False),
        sa.Column('section', sa.Unicode(length=255), nullable=False),
        sa.Column('key', sa.Unicode(length=255), nullable=False),
        sa.Column('value', sa.Unicode(length=2000), nullable=True),
        sa.Column('comparator', sa.Unicode(length=255), nullable=True),
        sa.ForeignKeyConstraint(['policy_id'], ['policy.id'], ),
        sa.PrimaryKeyConstraint('id'),
        mysql_row_format='DYNAMIC'
        )
    except Exception as exx:
        print("Could not create table policycondition: {!r}".format(exx))
    try:
        op.drop_column('policy', 'condition')
    except Exception as exx:
        print("Could not drop column policy.condition: {!r}".format(exx))

def downgrade():
    op.add_column('policy', sa.Column('condition', sa.INTEGER(), nullable=False))
    op.drop_table('policycondition')
 <REPLACE_END> <|endoftext|> """Add policycondition table, remove unused condition column from policy table

Revision ID: dceb6cd3c41e
Revises: b9131d0686eb
Create Date: 2019-07-02 12:19:19.646528

"""

# revision identifiers, used by Alembic.
revision = 'dceb6cd3c41e'
down_revision = 'b9131d0686eb'

from alembic import op, context
import sqlalchemy as sa
from sqlalchemy.schema import Sequence, CreateSequence

def dialect_supports_sequences():
    migration_context = context.get_context()
    return migration_context.dialect.supports_sequences

def create_seq(seq):
    if dialect_supports_sequences():
        op.execute(CreateSequence(seq))

def upgrade():
    try:
        try:
            create_seq(Sequence('policycondition_seq'))
        except Exception as _e:
            pass
        op.create_table('policycondition',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('policy_id', sa.Integer(), nullable=False),
        sa.Column('section', sa.Unicode(length=255), nullable=False),
        sa.Column('key', sa.Unicode(length=255), nullable=False),
        sa.Column('value', sa.Unicode(length=2000), nullable=True),
        sa.Column('comparator', sa.Unicode(length=255), nullable=True),
        sa.ForeignKeyConstraint(['policy_id'], ['policy.id'], ),
        sa.PrimaryKeyConstraint('id'),
        mysql_row_format='DYNAMIC'
        )
    except Exception as exx:
        print("Could not create table policycondition: {!r}".format(exx))
    try:
        op.drop_column('policy', 'condition')
    except Exception as exx:
        print("Could not drop column policy.condition: {!r}".format(exx))

def downgrade():
    op.add_column('policy', sa.Column('condition', sa.INTEGER(), nullable=False))
    op.drop_table('policycondition')

Prompt: Add migration script for policycondition table


from django.conf.urls import url

from bluebottle.organizations.views import (
    OrganizationList, OrganizationDetail,
    OrganizationContactList, OrganizationContactDetail
)

urlpatterns = [
    url(r'^$', OrganizationList.as_view(), name='organization_list'),
    url(r'^/(?P<pk>\d+)$', OrganizationDetail.as_view(),
        name='organization_detail'),
    url(r'^/contacts$', OrganizationContactList.as_view(),
        name='organization_contact_list'),
    url(r'^/contacts/(?P<pk>\d+)$', OrganizationContactDetail.as_view(),
        name='organization_contact_detail')
]

Response:
<REPLACE_OLD> url(r'^/contacts/$', <REPLACE_NEW> url(r'^/contacts$', <REPLACE_END> <|endoftext|> from django.conf.urls import url

from bluebottle.organizations.views import (
    OrganizationList, OrganizationDetail,
    OrganizationContactList, OrganizationContactDetail
)

urlpatterns = [
    url(r'^$', OrganizationList.as_view(), name='organization_list'),
    url(r'^/(?P<pk>\d+)$', OrganizationDetail.as_view(),
        name='organization_detail'),
    url(r'^/contacts$', OrganizationContactList.as_view(),
        name='organization_contact_list'),
    url(r'^/contacts/(?P<pk>\d+)$', OrganizationContactDetail.as_view(),
        name='organization_contact_detail')
]

Prompt:
Fix organization-contact url having an extra slash

from django.conf.urls import url

from bluebottle.organizations.views import (
    OrganizationList, OrganizationDetail,
    OrganizationContactList, OrganizationContactDetail
)

urlpatterns = [
    url(r'^$', OrganizationList.as_view(), name='organization_list'),
    url(r'^/(?P<pk>\d+)$', OrganizationDetail.as_view(),
        name='organization_detail'),
    url(r'^/contacts/$', OrganizationContactList.as_view(),
        name='organization_contact_list'),
    url(r'^/contacts/(?P<pk>\d+)$', OrganizationContactDetail.as_view(),
        name='organization_contact_detail')
]


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

"""
@author: Seva Zhidkov
@contact: zhidkovseva@gmail.com
@license: The MIT license

Copyright (C) 2015
"""

from sheldon.adapter import *
from sheldon.config import *
from sheldon.exceptions import *
from sheldon.manager import *
from sheldon.storage import *
from sheldon.utils import logger

class Sheldon:
    """
    Main class of the bot.
    Run script creating new instance of this class and run it.
    """

    def __init__(self, command_line_arguments):
        """
        Function for loading bot.

        :param command_line_arguments: dict, arguments for start script
        :return:
        """

        self._load_config(command_line_arguments)

    def _load_config(self, command_line_arguments):
        """
        Сreate and load bot config.

        :param command_line_arguments: dict, arguments for creating config:
                                       config-prefix - prefix of environment
                                                       variables.
                                                       Default - 'SHELDON_'
        :return:
        """
        # Config class is imported from sheldon.config
        try:
            if 'config-prefix' in command_line_arguments:
                self.config = Config(prefix=command_line_arguments['config-prefix'])
            else:
                self.config = Config()
        except Exception as error:
            logger.critical_message('Error with loading config:')
            logger.critical_message(str(error.__traceback__))

Response:
<REPLACE_OLD> logger.error_message('Error <REPLACE_NEW> logger.critical_message('Error <REPLACE_END> <REPLACE_OLD> logger.error_message(str(error.__traceback__))
 <REPLACE_NEW> logger.critical_message(str(error.__traceback__))
 <REPLACE_END> <|endoftext|> # -*- coding: utf-8 -*-

"""
@author: Seva Zhidkov
@contact: zhidkovseva@gmail.com
@license: The MIT license

Copyright (C) 2015
"""

from sheldon.adapter import *
from sheldon.config import *
from sheldon.exceptions import *
from sheldon.manager import *
from sheldon.storage import *
from sheldon.utils import logger

class Sheldon:
    """
    Main class of the bot.
    Run script creating new instance of this class and run it.
    """

    def __init__(self, command_line_arguments):
        """
        Function for loading bot.

        :param command_line_arguments: dict, arguments for start script
        :return:
        """

        self._load_config(command_line_arguments)

    def _load_config(self, command_line_arguments):
        """
        Сreate and load bot config.

        :param command_line_arguments: dict, arguments for creating config:
                                       config-prefix - prefix of environment
                                                       variables.
                                                       Default - 'SHELDON_'
        :return:
        """
        # Config class is imported from sheldon.config
        try:
            if 'config-prefix' in command_line_arguments:
                self.config = Config(prefix=command_line_arguments['config-prefix'])
            else:
                self.config = Config()
        except Exception as error:
            logger.critical_message('Error with loading config:')
            logger.critical_message(str(error.__traceback__))

Prompt:
Change error status with config problems to critical

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

"""
@author: Seva Zhidkov
@contact: zhidkovseva@gmail.com
@license: The MIT license

Copyright (C) 2015
"""

from sheldon.adapter import *
from sheldon.config import *
from sheldon.exceptions import *
from sheldon.manager import *
from sheldon.storage import *
from sheldon.utils import logger

class Sheldon:
    """
    Main class of the bot.
    Run script creating new instance of this class and run it.
    """

    def __init__(self, command_line_arguments):
        """
        Function for loading bot.

        :param command_line_arguments: dict, arguments for start script
        :return:
        """

        self._load_config(command_line_arguments)

    def _load_config(self, command_line_arguments):
        """
        Сreate and load bot config.

        :param command_line_arguments: dict, arguments for creating config:
                                       config-prefix - prefix of environment
                                                       variables.
                                                       Default - 'SHELDON_'
        :return:
        """
        # Config class is imported from sheldon.config
        try:
            if 'config-prefix' in command_line_arguments:
                self.config = Config(prefix=command_line_arguments['config-prefix'])
            else:
                self.config = Config()
        except Exception as error:
            logger.error_message('Error with loading config:')
            logger.error_message(str(error.__traceback__))


# test nested whiles within a try-except

while 1:
    print(1)
    try:
        print(2)
        while 1:
            print(3)
            break
    except:
        print(4)
    print(5)
    break

tests: Add test for nested while with exc and break.

Response:
<INSERT> # test nested whiles within a try-except

while 1:
 <INSERT_END> <INSERT>   print(1)
    try:
        print(2)
        while 1:
            print(3)
            break
    except:
        print(4)
    print(5)
    break
 <INSERT_END> <|endoftext|> # test nested whiles within a try-except

while 1:
    print(1)
    try:
        print(2)
        while 1:
            print(3)
            break
    except:
        print(4)
    print(5)
    break

Prompt: tests: Add test for nested while with exc and break.


from . import TestCase
from memopol.search.templatetags.search_tags import simple_search_shortcut

class TestSearchTemplateTags(TestCase):

    def test_simple_search_shortcut(self):
        url = simple_search_shortcut('country:FR or country:BR')
        self.assertEqual(url, "/search/?q=country%3AFR%20or%20country%3ABR")

[enh] Add a basic test for simple_search_shortcut templatetag

Response:
<INSERT> from . import TestCase
from memopol.search.templatetags.search_tags import simple_search_shortcut

class TestSearchTemplateTags(TestCase):

 <INSERT_END> <INSERT>   def test_simple_search_shortcut(self):
        url = simple_search_shortcut('country:FR or country:BR')
        self.assertEqual(url, "/search/?q=country%3AFR%20or%20country%3ABR")
 <INSERT_END> <|endoftext|> from . import TestCase
from memopol.search.templatetags.search_tags import simple_search_shortcut

class TestSearchTemplateTags(TestCase):

    def test_simple_search_shortcut(self):
        url = simple_search_shortcut('country:FR or country:BR')
        self.assertEqual(url, "/search/?q=country%3AFR%20or%20country%3ABR")

Prompt: [enh] Add a basic test for simple_search_shortcut templatetag


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

from unittest import TestCase
from semver import compare
from semver import match
from semver import parse

class TestSemver(TestCase):
    def test_should_parse_version(self):
        self.assertEquals(
            parse("1.2.3-alpha.1.2+build.11.e0f985a"),
            {'major': 1,
             'minor': 2,
             'patch': 3,
             'prerelease': 'alpha.1.2',
             'build': 'build.11.e0f985a'})

    def test_should_get_less(self):
        self.assertEquals(
            compare("1.0.0", "2.0.0"),
            -1)

    def test_should_get_greater(self):
        self.assertEquals(
            compare("2.0.0", "1.0.0"),
            1)

    def test_should_match_simple(self):
        self.assertEquals(
            match("2.3.7", ">=2.3.6"),
            True)

    def test_should_no_match_simple(self):
        self.assertEquals(
            match("2.3.7", ">=2.3.8"),
            False)

    def test_should_raise_value_error_for_invalid_value(self):
        self.assertRaises(ValueError, compare, 'foo', 'bar')
        self.assertRaises(ValueError, compare, '1.0', '1.0.0')
        self.assertRaises(ValueError, compare, '1.x', '1.0.0')

    def test_should_raise_value_error_for_invalid_match_expression(self):
        self.assertRaises(ValueError, match, '1.0.0', '')
        self.assertRaises(ValueError, match, '1.0.0', '!')
        self.assertRaises(ValueError, match, '1.0.0', '1.0.0')

Response:
<REPLACE_OLD> TestCase

from <REPLACE_NEW> TestCase
from <REPLACE_END> <REPLACE_OLD> False)
 <REPLACE_NEW> False)

    def test_should_raise_value_error_for_invalid_value(self):
        self.assertRaises(ValueError, compare, 'foo', 'bar')
        self.assertRaises(ValueError, compare, '1.0', '1.0.0')
        self.assertRaises(ValueError, compare, '1.x', '1.0.0')

    def test_should_raise_value_error_for_invalid_match_expression(self):
        self.assertRaises(ValueError, match, '1.0.0', '')
        self.assertRaises(ValueError, match, '1.0.0', '!')
        self.assertRaises(ValueError, match, '1.0.0', '1.0.0')
 <REPLACE_END> <|endoftext|> # -*- coding: utf-8 -*-

from unittest import TestCase
from semver import compare
from semver import match
from semver import parse

class TestSemver(TestCase):
    def test_should_parse_version(self):
        self.assertEquals(
            parse("1.2.3-alpha.1.2+build.11.e0f985a"),
            {'major': 1,
             'minor': 2,
             'patch': 3,
             'prerelease': 'alpha.1.2',
             'build': 'build.11.e0f985a'})

    def test_should_get_less(self):
        self.assertEquals(
            compare("1.0.0", "2.0.0"),
            -1)

    def test_should_get_greater(self):
        self.assertEquals(
            compare("2.0.0", "1.0.0"),
            1)

    def test_should_match_simple(self):
        self.assertEquals(
            match("2.3.7", ">=2.3.6"),
            True)

    def test_should_no_match_simple(self):
        self.assertEquals(
            match("2.3.7", ">=2.3.8"),
            False)

    def test_should_raise_value_error_for_invalid_value(self):
        self.assertRaises(ValueError, compare, 'foo', 'bar')
        self.assertRaises(ValueError, compare, '1.0', '1.0.0')
        self.assertRaises(ValueError, compare, '1.x', '1.0.0')

    def test_should_raise_value_error_for_invalid_match_expression(self):
        self.assertRaises(ValueError, match, '1.0.0', '')
        self.assertRaises(ValueError, match, '1.0.0', '!')
        self.assertRaises(ValueError, match, '1.0.0', '1.0.0')

Prompt:
Add tests for error cases that proves incompatibility with Python 2.5 and early versions.

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

from unittest import TestCase

from semver import compare
from semver import match
from semver import parse

class TestSemver(TestCase):
    def test_should_parse_version(self):
        self.assertEquals(
            parse("1.2.3-alpha.1.2+build.11.e0f985a"),
            {'major': 1,
             'minor': 2,
             'patch': 3,
             'prerelease': 'alpha.1.2',
             'build': 'build.11.e0f985a'})

    def test_should_get_less(self):
        self.assertEquals(
            compare("1.0.0", "2.0.0"),
            -1)

    def test_should_get_greater(self):
        self.assertEquals(
            compare("2.0.0", "1.0.0"),
            1)

    def test_should_match_simple(self):
        self.assertEquals(
            match("2.3.7", ">=2.3.6"),
            True)

    def test_should_no_match_simple(self):
        self.assertEquals(
            match("2.3.7", ">=2.3.8"),
            False)


#! /usr/bin/python
"""Setuptools-based setup script for datreant.

For a basic installation just type the command::

  python setup.py install

"""

from setuptools import setup

setup(name='datreant',
      version='0.5.1',
      author='David Dotson',
      author_email='dotsdl@gmail.com',
      packages=['datreant', 'datreant.tests'],
      scripts=[],
      license='BSD',
      long_description=open('README.rst').read(),
      install_requires=[
          'numpy',
          'pandas',
          'tables',
          'h5py',
          'scandir',
          'PyYAML'
          ]
      )

Add PyYAML & numpy dependency

Response:
<REPLACE_OLD> Dotson',
 <REPLACE_NEW> Dotson',
 <REPLACE_END> <REPLACE_OLD> install_requires=['pandas', 'tables', 'h5py', 'scandir']
 <REPLACE_NEW> install_requires=[
          'numpy',
          'pandas',
          'tables',
          'h5py',
          'scandir',
          'PyYAML'
          ]
  <REPLACE_END> <|endoftext|> #! /usr/bin/python
"""Setuptools-based setup script for datreant.

For a basic installation just type the command::

  python setup.py install

"""

from setuptools import setup

setup(name='datreant',
      version='0.5.1',
      author='David Dotson',
      author_email='dotsdl@gmail.com',
      packages=['datreant', 'datreant.tests'],
      scripts=[],
      license='BSD',
      long_description=open('README.rst').read(),
      install_requires=[
          'numpy',
          'pandas',
          'tables',
          'h5py',
          'scandir',
          'PyYAML'
          ]
      )

Prompt:
Add PyYAML & numpy dependency

I'm adding numpy too because we import it directly.

#! /usr/bin/python
"""Setuptools-based setup script for datreant.

For a basic installation just type the command::

  python setup.py install

"""

from setuptools import setup

setup(name='datreant',
      version='0.5.1',
      author='David Dotson',
      author_email='dotsdl@gmail.com',
      packages=['datreant', 'datreant.tests'],
      scripts=[],
      license='BSD',
      long_description=open('README.rst').read(),
      install_requires=['pandas', 'tables', 'h5py', 'scandir']
     )


from setuptools import setup, find_packages

setup(
    name='panoptes_client',
    url='https://github.com/zooniverse/panoptes-python-client',
    author='Adam McMaster',
    author_email='adam@zooniverse.org',
    version='1.1.1',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'requests>=2.4.2,<2.25',
        'future>=0.16,<0.19',
        'python-magic>=0.4,<0.5',
        'redo>=1.7',
        'six>=1.9',
    ],
    extras_require={
        'testing': [
            'mock>=2.0,<4.1',
        ],
        'docs': [
            'sphinx',
        ],
        ':python_version == "2.7"': ['futures'],
    }
)

Response:
<REPLACE_OLD> 'requests>=2.4.2,<2.24',
 <REPLACE_NEW> 'requests>=2.4.2,<2.25',
 <REPLACE_END> <|endoftext|> from setuptools import setup, find_packages

setup(
    name='panoptes_client',
    url='https://github.com/zooniverse/panoptes-python-client',
    author='Adam McMaster',
    author_email='adam@zooniverse.org',
    version='1.1.1',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'requests>=2.4.2,<2.25',
        'future>=0.16,<0.19',
        'python-magic>=0.4,<0.5',
        'redo>=1.7',
        'six>=1.9',
    ],
    extras_require={
        'testing': [
            'mock>=2.0,<4.1',
        ],
        'docs': [
            'sphinx',
        ],
        ':python_version == "2.7"': ['futures'],
    }
)

Prompt:
Update requests requirement from <2.24,>=2.4.2 to >=2.4.2,<2.25

Updates the requirements on [requests](https://github.com/psf/requests) to permit the latest version.
- [Release notes](https://github.com/psf/requests/releases)
- [Changelog](https://github.com/psf/requests/blob/master/HISTORY.md)
- [Commits](https://github.com/psf/requests/compare/v2.4.2...v2.24.0)

Signed-off-by: dependabot-preview[bot] <5bdcd3c0d4d24ae3e71b3b452a024c6324c7e4bb@dependabot.com>
from setuptools import setup, find_packages

setup(
    name='panoptes_client',
    url='https://github.com/zooniverse/panoptes-python-client',
    author='Adam McMaster',
    author_email='adam@zooniverse.org',
    version='1.1.1',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'requests>=2.4.2,<2.24',
        'future>=0.16,<0.19',
        'python-magic>=0.4,<0.5',
        'redo>=1.7',
        'six>=1.9',
    ],
    extras_require={
        'testing': [
            'mock>=2.0,<4.1',
        ],
        'docs': [
            'sphinx',
        ],
        ':python_version == "2.7"': ['futures'],
    }
)


#!/usr/bin/python

import os
import optparse
import sys
import unittest

USAGE = """%prog SDK_PATH TEST_PATH
Run unit tests for App Engine apps.

SDK_PATH     Path to the SDK installation.
TEST_PATH    Path to package containing test modules.
WEBTEST_PATH Path to the webtest library."""

def main(sdk_path, test_path, webtest_path):
    sys.path.insert(0, sdk_path)
    import dev_appserver
    dev_appserver.fix_sys_path()
    sys.path.append(webtest_path)
    suite = unittest.loader.TestLoader().discover(test_path,
                                                  pattern="*test.py")
    unittest.TextTestRunner(verbosity=2).run(suite)

if __name__ == '__main__':
    parser = optparse.OptionParser(USAGE)
    options, args = parser.parse_args()
    if len(args) != 3:
        print 'Error: Exactly 3 arguments required.'
        parser.print_help()
        sys.exit(1)
    SDK_PATH = args[0]
    TEST_PATH = args[1]
    WEBTEST_PATH = args[2]
    main(SDK_PATH, TEST_PATH, WEBTEST_PATH)

Response:
<REPLACE_OLD> return unittest.TextTestRunner(verbosity=2).run(suite).wasSuccessful()

if <REPLACE_NEW> unittest.TextTestRunner(verbosity=2).run(suite)

if <REPLACE_END> <DELETE> sys.exit(not <DELETE_END> <REPLACE_OLD> WEBTEST_PATH))
 <REPLACE_NEW> WEBTEST_PATH)
 <REPLACE_END> <|endoftext|> #!/usr/bin/python

import os
import optparse
import sys
import unittest

USAGE = """%prog SDK_PATH TEST_PATH
Run unit tests for App Engine apps.

SDK_PATH     Path to the SDK installation.
TEST_PATH    Path to package containing test modules.
WEBTEST_PATH Path to the webtest library."""

def main(sdk_path, test_path, webtest_path):
    sys.path.insert(0, sdk_path)
    import dev_appserver
    dev_appserver.fix_sys_path()
    sys.path.append(webtest_path)
    suite = unittest.loader.TestLoader().discover(test_path,
                                                  pattern="*test.py")
    unittest.TextTestRunner(verbosity=2).run(suite)

if __name__ == '__main__':
    parser = optparse.OptionParser(USAGE)
    options, args = parser.parse_args()
    if len(args) != 3:
        print 'Error: Exactly 3 arguments required.'
        parser.print_help()
        sys.exit(1)
    SDK_PATH = args[0]
    TEST_PATH = args[1]
    WEBTEST_PATH = args[2]
    main(SDK_PATH, TEST_PATH, WEBTEST_PATH)

Prompt:
Revert "Python tests now return an error code on fail."

#!/usr/bin/python

import os
import optparse
import sys
import unittest

USAGE = """%prog SDK_PATH TEST_PATH
Run unit tests for App Engine apps.

SDK_PATH     Path to the SDK installation.
TEST_PATH    Path to package containing test modules.
WEBTEST_PATH Path to the webtest library."""

def main(sdk_path, test_path, webtest_path):
    sys.path.insert(0, sdk_path)
    import dev_appserver
    dev_appserver.fix_sys_path()
    sys.path.append(webtest_path)
    suite = unittest.loader.TestLoader().discover(test_path,
                                                  pattern="*test.py")
    return unittest.TextTestRunner(verbosity=2).run(suite).wasSuccessful()

if __name__ == '__main__':
    parser = optparse.OptionParser(USAGE)
    options, args = parser.parse_args()
    if len(args) != 3:
        print 'Error: Exactly 3 arguments required.'
        parser.print_help()
        sys.exit(1)
    SDK_PATH = args[0]
    TEST_PATH = args[1]
    WEBTEST_PATH = args[2]
    sys.exit(not main(SDK_PATH, TEST_PATH, WEBTEST_PATH))


import sys
import os
import commands
import fnmatch
import re
import subprocess, shlex

mcp_root = os.path.abspath(sys.argv[1])
sys.path.append(os.path.join(mcp_root,"runtime"))
from filehandling.srgshandler import parse_srg

def cmdsplit(args):
    if os.sep == '\\':
        args = args.replace('\\', '\\\\')
    return shlex.split(args)

def main():
    list_file = os.path.abspath(sys.argv[2])
    prelist = os.path.join(mcp_root,"temp","server.md5")
    postlist = os.path.join(mcp_root,"temp","server_reobf.md5")
    cmd = 'diff --unchanged-group-format='' --old-group-format='' --new-group-format=\'%%>\' --changed-group-format=\'%%>\' %s %s' % (prelist, postlist)
    process = subprocess.Popen(cmdsplit(cmd), stdout=subprocess.PIPE, bufsize=-1)
    difflist,_= process.communicate()
    srg_data = parse_srg(os.path.join(mcp_root,"temp","server_rg.srg")
    classes=dict()
    for row in srg_data['CL']:
      classes[row['deobf_name']] = row['obf_name']

    with open(list_file, 'w') as fh:
      for diff in difflist:
        (clazz,md5)=diff.strip().split()
        if clazz in classes:
          clazz=classes[clazz]
        fh.write("%s\n" %(clazz))

if __name__ == '__main__':
    main()

Response:
<REPLACE_OLD> shlex

def <REPLACE_NEW> shlex

mcp_root = os.path.abspath(sys.argv[1])
sys.path.append(os.path.join(mcp_root,"runtime"))
from filehandling.srgshandler import parse_srg

def <REPLACE_END> <DELETE> md5dir = os.path.abspath(sys.argv[1])
    <DELETE_END> <REPLACE_OLD> os.path.join(md5dir,"temp","server.md5")
 <REPLACE_NEW> os.path.join(mcp_root,"temp","server.md5")
 <REPLACE_END> <REPLACE_OLD> os.path.join(md5dir,"temp","server_reobf.md5")
 <REPLACE_NEW> os.path.join(mcp_root,"temp","server_reobf.md5")
 <REPLACE_END> <INSERT> srg_data = parse_srg(os.path.join(mcp_root,"temp","server_rg.srg")
    classes=dict()
    for row in srg_data['CL']:
      classes[row['deobf_name']] = row['obf_name']

    <INSERT_END> <REPLACE_OLD> fh.write(difflist)

 <REPLACE_NEW> for diff in difflist:
        (clazz,md5)=diff.strip().split()
        if clazz in classes:
          clazz=classes[clazz]
        fh.write("%s\n" %(clazz))

 <REPLACE_END> <|endoftext|> import sys
import os
import commands
import fnmatch
import re
import subprocess, shlex

mcp_root = os.path.abspath(sys.argv[1])
sys.path.append(os.path.join(mcp_root,"runtime"))
from filehandling.srgshandler import parse_srg

def cmdsplit(args):
    if os.sep == '\\':
        args = args.replace('\\', '\\\\')
    return shlex.split(args)

def main():
    list_file = os.path.abspath(sys.argv[2])
    prelist = os.path.join(mcp_root,"temp","server.md5")
    postlist = os.path.join(mcp_root,"temp","server_reobf.md5")
    cmd = 'diff --unchanged-group-format='' --old-group-format='' --new-group-format=\'%%>\' --changed-group-format=\'%%>\' %s %s' % (prelist, postlist)
    process = subprocess.Popen(cmdsplit(cmd), stdout=subprocess.PIPE, bufsize=-1)
    difflist,_= process.communicate()
    srg_data = parse_srg(os.path.join(mcp_root,"temp","server_rg.srg")
    classes=dict()
    for row in srg_data['CL']:
      classes[row['deobf_name']] = row['obf_name']

    with open(list_file, 'w') as fh:
      for diff in difflist:
        (clazz,md5)=diff.strip().split()
        if clazz in classes:
          clazz=classes[clazz]
        fh.write("%s\n" %(clazz))

if __name__ == '__main__':
    main()

Prompt:
Tweak file list script to print obf names

import sys
import os
import commands
import fnmatch
import re
import subprocess, shlex

def cmdsplit(args):
    if os.sep == '\\':
        args = args.replace('\\', '\\\\')
    return shlex.split(args)

def main():
    md5dir = os.path.abspath(sys.argv[1])
    list_file = os.path.abspath(sys.argv[2])
    prelist = os.path.join(md5dir,"temp","server.md5")
    postlist = os.path.join(md5dir,"temp","server_reobf.md5")
    cmd = 'diff --unchanged-group-format='' --old-group-format='' --new-group-format=\'%%>\' --changed-group-format=\'%%>\' %s %s' % (prelist, postlist)
    process = subprocess.Popen(cmdsplit(cmd), stdout=subprocess.PIPE, bufsize=-1)
    difflist,_= process.communicate()
    with open(list_file, 'w') as fh:
      fh.write(difflist)

if __name__ == '__main__':
    main()


from .probability import (
    vs,
    Variable,
    make_variables,
    UniformDist,
    JointDist,
    JointDistByState
)

from .network import CausalGraph, Equation

from .measure import MeasureCause, MeasureSuccess

from .payoff import PayoffMatrix

import equations

__version__ = "0.1.0"

__title__ = "causalinfo"
__description__ = "Information Measures on Causal Graphs."
__uri__ = "http://github/brettc/causalinfo/"

__author__ = "Brett Calcott"
__email__ = "brett.calcott@gmail.com"

__license__ = "MIT"
__copyright__ = "Copyright (c) 2015 Brett Calcott"

__all__ = [
    "CausalGraph",
    "Equation",
    "vs",
    "Variable",
    "make_variables",
    "UniformDist",
    "JointDist",
    "JointDistByState",
    "MeasureCause",
    "MeasureSuccess",
    "PayoffMatrix",
    "equations",
]

Fix silly boiler plate copy issue.

Fix silly boiler plate copy issue.

Response:
<REPLACE_OLD> "Attributes without boilerplate."
__uri__ <REPLACE_NEW> "Information Measures on Causal Graphs."
__uri__ <REPLACE_END> <|endoftext|> from .probability import (
    vs,
    Variable,
    make_variables,
    UniformDist,
    JointDist,
    JointDistByState
)

from .network import CausalGraph, Equation

from .measure import MeasureCause, MeasureSuccess

from .payoff import PayoffMatrix

import equations

__version__ = "0.1.0"

__title__ = "causalinfo"
__description__ = "Information Measures on Causal Graphs."
__uri__ = "http://github/brettc/causalinfo/"

__author__ = "Brett Calcott"
__email__ = "brett.calcott@gmail.com"

__license__ = "MIT"
__copyright__ = "Copyright (c) 2015 Brett Calcott"

__all__ = [
    "CausalGraph",
    "Equation",
    "vs",
    "Variable",
    "make_variables",
    "UniformDist",
    "JointDist",
    "JointDistByState",
    "MeasureCause",
    "MeasureSuccess",
    "PayoffMatrix",
    "equations",
]

Prompt:
Fix silly boiler plate copy issue.

from .probability import (
    vs,
    Variable,
    make_variables,
    UniformDist,
    JointDist,
    JointDistByState
)

from .network import CausalGraph, Equation

from .measure import MeasureCause, MeasureSuccess

from .payoff import PayoffMatrix

import equations

__version__ = "0.1.0"

__title__ = "causalinfo"
__description__ = "Attributes without boilerplate."
__uri__ = "http://github/brettc/causalinfo/"

__author__ = "Brett Calcott"
__email__ = "brett.calcott@gmail.com"

__license__ = "MIT"
__copyright__ = "Copyright (c) 2015 Brett Calcott"

__all__ = [
    "CausalGraph",
    "Equation",
    "vs",
    "Variable",
    "make_variables",
    "UniformDist",
    "JointDist",
    "JointDistByState",
    "MeasureCause",
    "MeasureSuccess",
    "PayoffMatrix",
    "equations",
]


# Adapted from http://stackoverflow.com/questions/110803/dirty-fields-in-django
from django.db.models.signals import post_save

class DirtyFieldsMixin(object):
    def __init__(self, *args, **kwargs):
        super(DirtyFieldsMixin, self).__init__(*args, **kwargs)
        post_save.connect(reset_state, sender=self.__class__,
                            dispatch_uid='%s-DirtyFieldsMixin-sweeper' % self.__class__.__name__)
        reset_state(sender=self.__class__, instance=self)

    def _as_dict(self):
        return dict([(f.name, f.to_python(getattr(self, f.name))) for f in self._meta.local_fields if not f.rel])

    def get_dirty_fields(self):
        new_state = self._as_dict()
        return dict([(key, value) for key, value in self._original_state.iteritems() if value != new_state[key]])

    def is_dirty(self):
        # in order to be dirty we need to have been saved at least once, so we
        # check for a primary key and we need our dirty fields to not be empty
        if not self.pk:
            return True
        return {} != self.get_dirty_fields()

def reset_state(sender, instance, **kwargs):
    instance._original_state = instance._as_dict()

Response:
<REPLACE_OLD> getattr(self, f.name)) <REPLACE_NEW> f.to_python(getattr(self, f.name))) <REPLACE_END> <|endoftext|> # Adapted from http://stackoverflow.com/questions/110803/dirty-fields-in-django
from django.db.models.signals import post_save

class DirtyFieldsMixin(object):
    def __init__(self, *args, **kwargs):
        super(DirtyFieldsMixin, self).__init__(*args, **kwargs)
        post_save.connect(reset_state, sender=self.__class__,
                            dispatch_uid='%s-DirtyFieldsMixin-sweeper' % self.__class__.__name__)
        reset_state(sender=self.__class__, instance=self)

    def _as_dict(self):
        return dict([(f.name, f.to_python(getattr(self, f.name))) for f in self._meta.local_fields if not f.rel])

    def get_dirty_fields(self):
        new_state = self._as_dict()
        return dict([(key, value) for key, value in self._original_state.iteritems() if value != new_state[key]])

    def is_dirty(self):
        # in order to be dirty we need to have been saved at least once, so we
        # check for a primary key and we need our dirty fields to not be empty
        if not self.pk:
            return True
        return {} != self.get_dirty_fields()

def reset_state(sender, instance, **kwargs):
    instance._original_state = instance._as_dict()

Prompt:
Use field.to_python to do django type conversions on the field before checking if dirty.

This solves issues where you might have a decimal field that you write a string to, eg:
>>> m = MyModel.objects.get(id=1)
>>> m.my_decimal_field
Decimal('1.00')
>>> m.my_decimal_field = u'1.00' # from a form or something
>>> m.is_dirty() # currently evaluates to True, should evaluate to False
False

This pull request could probably use some unit testing, but it should be safe as the base class for django fields defines to_python as:

def to_python(self, value):
  return value

So, any field type that does not have an explicit to_python method will behave as before this change.

# Adapted from http://stackoverflow.com/questions/110803/dirty-fields-in-django
from django.db.models.signals import post_save

class DirtyFieldsMixin(object):
    def __init__(self, *args, **kwargs):
        super(DirtyFieldsMixin, self).__init__(*args, **kwargs)
        post_save.connect(reset_state, sender=self.__class__,
                            dispatch_uid='%s-DirtyFieldsMixin-sweeper' % self.__class__.__name__)
        reset_state(sender=self.__class__, instance=self)

    def _as_dict(self):
        return dict([(f.name, getattr(self, f.name)) for f in self._meta.local_fields if not f.rel])

    def get_dirty_fields(self):
        new_state = self._as_dict()
        return dict([(key, value) for key, value in self._original_state.iteritems() if value != new_state[key]])

    def is_dirty(self):
        # in order to be dirty we need to have been saved at least once, so we
        # check for a primary key and we need our dirty fields to not be empty
        if not self.pk:
            return True
        return {} != self.get_dirty_fields()

def reset_state(sender, instance, **kwargs):
    instance._original_state = instance._as_dict()


from bot.action.core.action import IntermediateAction
from bot.multithreading.work import Work

class AsynchronousAction(IntermediateAction):
    def __init__(self, name: str, min_workers: int = 0, max_workers: int = 4, max_seconds_idle: int = 60):
        super().__init__()
        self.name = name
        self.min_workers = min_workers
        self.max_workers = max_workers
        self.max_seconds_idle = max_seconds_idle
        self.worker_pool = None  # initialized in post_setup where we have access to scheduler

    def post_setup(self):
        self.worker_pool = self.scheduler.new_worker_pool(
            self.name,
            self.min_workers,
            self.max_workers,
            self.max_seconds_idle
        )

    def process(self, event):
        self.worker_pool.post(Work(lambda: self._continue(event), "asynchronous_action"))

Response:
<REPLACE_OLD> 1, <REPLACE_NEW> 0, <REPLACE_END> <REPLACE_OLD> 15):
 <REPLACE_NEW> 60):
 <REPLACE_END> <|endoftext|> from bot.action.core.action import IntermediateAction
from bot.multithreading.work import Work

class AsynchronousAction(IntermediateAction):
    def __init__(self, name: str, min_workers: int = 0, max_workers: int = 4, max_seconds_idle: int = 60):
        super().__init__()
        self.name = name
        self.min_workers = min_workers
        self.max_workers = max_workers
        self.max_seconds_idle = max_seconds_idle
        self.worker_pool = None  # initialized in post_setup where we have access to scheduler

    def post_setup(self):
        self.worker_pool = self.scheduler.new_worker_pool(
            self.name,
            self.min_workers,
            self.max_workers,
            self.max_seconds_idle
        )

    def process(self, event):
        self.worker_pool.post(Work(lambda: self._continue(event), "asynchronous_action"))

Prompt:
Update AsynchronousAction default values of min_workers to 0 and max_seconds_idle to 60

from bot.action.core.action import IntermediateAction
from bot.multithreading.work import Work

class AsynchronousAction(IntermediateAction):
    def __init__(self, name: str, min_workers: int = 1, max_workers: int = 4, max_seconds_idle: int = 15):
        super().__init__()
        self.name = name
        self.min_workers = min_workers
        self.max_workers = max_workers
        self.max_seconds_idle = max_seconds_idle
        self.worker_pool = None  # initialized in post_setup where we have access to scheduler

    def post_setup(self):
        self.worker_pool = self.scheduler.new_worker_pool(
            self.name,
            self.min_workers,
            self.max_workers,
            self.max_seconds_idle
        )

    def process(self, event):
        self.worker_pool.post(Work(lambda: self._continue(event), "asynchronous_action"))


#!/usr/bin/env python3

from sphinx.ext.intersphinx import fetch_inventory

URL = "https://docs.python.org/{}/objects.inv"
PATH = "isort/stdlibs/py{}.py"
VERSIONS = [("2", "7"), ("3", "5"), ("3", "6"), ("3", "7"), ("3", "8"), ("3", "9")]

DOCSTRING = """
File contains the standard library of Python {}.

DO NOT EDIT. If the standard library changes, a new list should be created
using the mkstdlibs.py script.
"""

class FakeConfig:
    intersphinx_timeout = None
    tls_verify = True
    user_agent = ""

class FakeApp:
    srcdir = ""
    config = FakeConfig()

for version_info in VERSIONS:
    version = ".".join(version_info)
    url = URL.format(version)
    invdata = fetch_inventory(FakeApp(), "", url)

    modules = set()
    for module in invdata["py:module"]:
        root, *_ = module.split(".")
        if root not in ["__future__", "__main__"]:
            modules.add(root)

    path = PATH.format("".join(version_info))
    with open(path, "w") as stdlib_file:
        docstring = DOCSTRING.format(version)
        stdlib_file.write(f'"""{docstring}"""\n\n')
        stdlib_file.write("stdlib = {\n")
        for module in sorted(modules):
            stdlib_file.write(f'    "{module}",\n')
        stdlib_file.write("}\n")

Response:
<REPLACE_OLD> "8")]

DOCSTRING <REPLACE_NEW> "8"), ("3", "9")]

DOCSTRING <REPLACE_END> <REPLACE_OLD> True

class <REPLACE_NEW> True
    user_agent = ""

class <REPLACE_END> <|endoftext|> #!/usr/bin/env python3

from sphinx.ext.intersphinx import fetch_inventory

URL = "https://docs.python.org/{}/objects.inv"
PATH = "isort/stdlibs/py{}.py"
VERSIONS = [("2", "7"), ("3", "5"), ("3", "6"), ("3", "7"), ("3", "8"), ("3", "9")]

DOCSTRING = """
File contains the standard library of Python {}.

DO NOT EDIT. If the standard library changes, a new list should be created
using the mkstdlibs.py script.
"""

class FakeConfig:
    intersphinx_timeout = None
    tls_verify = True
    user_agent = ""

class FakeApp:
    srcdir = ""
    config = FakeConfig()

for version_info in VERSIONS:
    version = ".".join(version_info)
    url = URL.format(version)
    invdata = fetch_inventory(FakeApp(), "", url)

    modules = set()
    for module in invdata["py:module"]:
        root, *_ = module.split(".")
        if root not in ["__future__", "__main__"]:
            modules.add(root)

    path = PATH.format("".join(version_info))
    with open(path, "w") as stdlib_file:
        docstring = DOCSTRING.format(version)
        stdlib_file.write(f'"""{docstring}"""\n\n')
        stdlib_file.write("stdlib = {\n")
        for module in sorted(modules):
            stdlib_file.write(f'    "{module}",\n')
        stdlib_file.write("}\n")

Prompt:
Update script to include empty user agent

#!/usr/bin/env python3

from sphinx.ext.intersphinx import fetch_inventory

URL = "https://docs.python.org/{}/objects.inv"
PATH = "isort/stdlibs/py{}.py"
VERSIONS = [("2", "7"), ("3", "5"), ("3", "6"), ("3", "7"), ("3", "8")]

DOCSTRING = """
File contains the standard library of Python {}.

DO NOT EDIT. If the standard library changes, a new list should be created
using the mkstdlibs.py script.
"""

class FakeConfig:
    intersphinx_timeout = None
    tls_verify = True

class FakeApp:
    srcdir = ""
    config = FakeConfig()

for version_info in VERSIONS:
    version = ".".join(version_info)
    url = URL.format(version)
    invdata = fetch_inventory(FakeApp(), "", url)

    modules = set()
    for module in invdata["py:module"]:
        root, *_ = module.split(".")
        if root not in ["__future__", "__main__"]:
            modules.add(root)

    path = PATH.format("".join(version_info))
    with open(path, "w") as stdlib_file:
        docstring = DOCSTRING.format(version)
        stdlib_file.write(f'"""{docstring}"""\n\n')
        stdlib_file.write("stdlib = {\n")
        for module in sorted(modules):
            stdlib_file.write(f'    "{module}",\n')
        stdlib_file.write("}\n")


from django.conf import settings

DEFAULT_REALM = getattr(settings, 'RATED_DEFAULT_REALM', 'default')
DEFAULT_LIMIT = getattr(settings, 'RATED_DEFAULT_LIMIT', 100)
DEFAULT_DURATION = getattr(settings, 'RATED_DEFAULT_DURATION', 60 * 60)

RESPONSE_CODE = getattr(settings, 'RATED_RESPONSE_CODE', 429)
RESPONSE_MESSAGE = getattr(settings, 'RATED_RESPONSE_MESSAGE', '')

DEFAULT_WHITELIST = getattr(settings, 'RATED_DEFAULT_WHITELIST', [])

REALMS = getattr(settings, 'RATED_REALMS', {})

REALM_MAP = getattr(settings, 'RATED_REALM_MAP', {})

# Redis config parameters
REDIS = getattr(settings, 'RATED_REDIS', {})

USE_X_FORWARDED_FOR = getattr(settings, 'USE_X_FORWARDED_FOR', False)

Response:
<REPLACE_OLD> {})

 <REPLACE_NEW> {})

USE_X_FORWARDED_FOR = getattr(settings, 'USE_X_FORWARDED_FOR', False)
 <REPLACE_END> <|endoftext|>
from django.conf import settings

DEFAULT_REALM = getattr(settings, 'RATED_DEFAULT_REALM', 'default')
DEFAULT_LIMIT = getattr(settings, 'RATED_DEFAULT_LIMIT', 100)
DEFAULT_DURATION = getattr(settings, 'RATED_DEFAULT_DURATION', 60 * 60)

RESPONSE_CODE = getattr(settings, 'RATED_RESPONSE_CODE', 429)
RESPONSE_MESSAGE = getattr(settings, 'RATED_RESPONSE_MESSAGE', '')

DEFAULT_WHITELIST = getattr(settings, 'RATED_DEFAULT_WHITELIST', [])

REALMS = getattr(settings, 'RATED_REALMS', {})

REALM_MAP = getattr(settings, 'RATED_REALM_MAP', {})

# Redis config parameters
REDIS = getattr(settings, 'RATED_REDIS', {})

USE_X_FORWARDED_FOR = getattr(settings, 'USE_X_FORWARDED_FOR', False)

Prompt:
Fix USE_X_FORWARDED_FOR for proxied environments

The settings for USE_X_FORWARDED_FOR were not being respected because the settings were not being passed through.

from django.conf import settings

DEFAULT_REALM = getattr(settings, 'RATED_DEFAULT_REALM', 'default')
DEFAULT_LIMIT = getattr(settings, 'RATED_DEFAULT_LIMIT', 100)
DEFAULT_DURATION = getattr(settings, 'RATED_DEFAULT_DURATION', 60 * 60)

RESPONSE_CODE = getattr(settings, 'RATED_RESPONSE_CODE', 429)
RESPONSE_MESSAGE = getattr(settings, 'RATED_RESPONSE_MESSAGE', '')

DEFAULT_WHITELIST = getattr(settings, 'RATED_DEFAULT_WHITELIST', [])

REALMS = getattr(settings, 'RATED_REALMS', {})

REALM_MAP = getattr(settings, 'RATED_REALM_MAP', {})

# Redis config parameters
REDIS = getattr(settings, 'RATED_REDIS', {})


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django import template
from ..generate import image_url as url

register = template.Library()

@register.simple_tag
def image_url(image_url, **kwargs):
    return url(image_url=image_url, **kwargs)

@register.simple_tag
def image_obj(image, **kwargs):
    new = {}
    new['flip'] = image.flip
    new['flop'] = image.flop
    if image.halign != "":
        new['halign'] = image.halign
    if image.valign != "":
        new['valign'] = image.valign
    new['fit_in'] = image.fit_in
    new['smart'] = image.smart

    if image.crop_x1 > 0 or image.crop_x2 > 0 or image.crop_y1 > 0 or \
       image.crop_y2 > 0:
        new['crop'] = ((image.crop_x1, image.crop_y1),
                       (image.crop_x2, image.crop_y2))

    kwargs = dict(new, **kwargs)
    return url(image_url=image.image.url, **kwargs)

Response:
<REPLACE_OLD> ((image.crop_x1,image.crop_y1),
 <REPLACE_NEW> ((image.crop_x1, image.crop_y1),
 <REPLACE_END> <REPLACE_OLD> (image.crop_x2,image.crop_y2))

 <REPLACE_NEW> (image.crop_x2, image.crop_y2))

 <REPLACE_END> <|endoftext|> #!/usr/bin/env python
# -*- coding: utf-8 -*-
from django import template
from ..generate import image_url as url

register = template.Library()

@register.simple_tag
def image_url(image_url, **kwargs):
    return url(image_url=image_url, **kwargs)

@register.simple_tag
def image_obj(image, **kwargs):
    new = {}
    new['flip'] = image.flip
    new['flop'] = image.flop
    if image.halign != "":
        new['halign'] = image.halign
    if image.valign != "":
        new['valign'] = image.valign
    new['fit_in'] = image.fit_in
    new['smart'] = image.smart

    if image.crop_x1 > 0 or image.crop_x2 > 0 or image.crop_y1 > 0 or \
       image.crop_y2 > 0:
        new['crop'] = ((image.crop_x1, image.crop_y1),
                       (image.crop_x2, image.crop_y2))

    kwargs = dict(new, **kwargs)
    return url(image_url=image.image.url, **kwargs)

Prompt:
Fix pep8, E231 missing whitespace after ','

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django import template
from ..generate import image_url as url

register = template.Library()

@register.simple_tag
def image_url(image_url, **kwargs):
    return url(image_url=image_url, **kwargs)

@register.simple_tag
def image_obj(image, **kwargs):
    new = {}
    new['flip'] = image.flip
    new['flop'] = image.flop
    if image.halign != "":
        new['halign'] = image.halign
    if image.valign != "":
        new['valign'] = image.valign
    new['fit_in'] = image.fit_in
    new['smart'] = image.smart

    if image.crop_x1 > 0 or image.crop_x2 > 0 or image.crop_y1 > 0 or \
       image.crop_y2 > 0:
        new['crop'] = ((image.crop_x1,image.crop_y1),
                       (image.crop_x2,image.crop_y2))

    kwargs = dict(new, **kwargs)
    return url(image_url=image.image.url, **kwargs)


import json
from indra.statements import Activation, Agent

def process_mentions(fname):
    with open(fname, 'r') as fh:
        jd = json.load(fh)
        mentions = jd['mentions']

    events = [m for m in mentions if m['type'] == 'EventMention']
    events = [e for e in events if len(e['arguments']) == 2]
    events = [e for e in events if 'cause' in e['arguments']]

    stmts = []
    for event in events:
        cause = event['arguments']['cause'][0]['text']
        effect = event['arguments']['effect'][0]['text']
        st = Activation(Agent(cause), Agent(effect))
        stmts.append(st)
    return stmts

Add initial mention processing code

Add initial mention processing code

Response:
<INSERT> import json
from indra.statements import Activation, Agent

def process_mentions(fname):
 <INSERT_END> <INSERT>   with open(fname, 'r') as fh:
        jd = json.load(fh)
        mentions = jd['mentions']

    events = [m for m in mentions if m['type'] == 'EventMention']
    events = [e for e in events if len(e['arguments']) == 2]
    events = [e for e in events if 'cause' in e['arguments']]

    stmts = []
    for event in events:
        cause = event['arguments']['cause'][0]['text']
        effect = event['arguments']['effect'][0]['text']
        st = Activation(Agent(cause), Agent(effect))
        stmts.append(st)
    return stmts

 <INSERT_END> <|endoftext|> import json
from indra.statements import Activation, Agent

def process_mentions(fname):
    with open(fname, 'r') as fh:
        jd = json.load(fh)
        mentions = jd['mentions']

    events = [m for m in mentions if m['type'] == 'EventMention']
    events = [e for e in events if len(e['arguments']) == 2]
    events = [e for e in events if 'cause' in e['arguments']]

    stmts = []
    for event in events:
        cause = event['arguments']['cause'][0]['text']
        effect = event['arguments']['effect'][0]['text']
        st = Activation(Agent(cause), Agent(effect))
        stmts.append(st)
    return stmts

Prompt: Add initial mention processing code


import datetime
from urlparse import urljoin
import requests

# project
from checks import AgentCheck

class Veneur(AgentCheck):

    VERSION_METRIC_NAME = 'veneur.deployed_version'
    BUILDAGE_METRIC_NAME = 'veneur.build_age'
    ERROR_METRIC_NAME = 'veneur.agent_check.errors_total'

    def check(self, instance):
        success = 0

        host = instance['host']
        tags = instance.get('tags', [])

        try:
            r = requests.get(urljoin(host, '/version'))
            tags.extend(['sha:{0}'.format(r.text)])
            success = 1

            r = requests.get(urljoin(host, '/builddate'))
            builddate = datetime.datetime.fromtimestamp(int(r.text))

            tdelta = datetime.datetime.now() - builddate
            self.histogram(self.BUILDAGE_METRIC_NAME, tdelta.total_seconds())

        except:
            success = 0
            self.increment(self.ERROR_METRIC_NAME)
            raise
        finally:
            self.gauge(self.VERSION_METRIC_NAME, success, tags = tags)

Response:
<REPLACE_OLD> instance['host']

 <REPLACE_NEW> instance['host']
        tags = instance.get('tags', [])

 <REPLACE_END> <REPLACE_OLD> sha = r.text
 <REPLACE_NEW> tags.extend(['sha:{0}'.format(r.text)])
 <REPLACE_END> <DELETE> tags = instance.get('tags', [])
            tags.extend(['sha:{0}'.format(sha)])
            <DELETE_END> <|endoftext|> import datetime
from urlparse import urljoin
import requests

# project
from checks import AgentCheck

class Veneur(AgentCheck):

    VERSION_METRIC_NAME = 'veneur.deployed_version'
    BUILDAGE_METRIC_NAME = 'veneur.build_age'
    ERROR_METRIC_NAME = 'veneur.agent_check.errors_total'

    def check(self, instance):
        success = 0

        host = instance['host']
        tags = instance.get('tags', [])

        try:
            r = requests.get(urljoin(host, '/version'))
            tags.extend(['sha:{0}'.format(r.text)])
            success = 1

            r = requests.get(urljoin(host, '/builddate'))
            builddate = datetime.datetime.fromtimestamp(int(r.text))

            tdelta = datetime.datetime.now() - builddate
            self.histogram(self.BUILDAGE_METRIC_NAME, tdelta.total_seconds())

        except:
            success = 0
            self.increment(self.ERROR_METRIC_NAME)
            raise
        finally:
            self.gauge(self.VERSION_METRIC_NAME, success, tags = tags)

Prompt:
Add the sha to the tags in a way that won't cause an error

import datetime
from urlparse import urljoin
import requests

# project
from checks import AgentCheck

class Veneur(AgentCheck):

    VERSION_METRIC_NAME = 'veneur.deployed_version'
    BUILDAGE_METRIC_NAME = 'veneur.build_age'
    ERROR_METRIC_NAME = 'veneur.agent_check.errors_total'

    def check(self, instance):
        success = 0

        host = instance['host']

        try:
            r = requests.get(urljoin(host, '/version'))
            sha = r.text
            success = 1

            r = requests.get(urljoin(host, '/builddate'))
            builddate = datetime.datetime.fromtimestamp(int(r.text))

            tdelta = datetime.datetime.now() - builddate
            self.histogram(self.BUILDAGE_METRIC_NAME, tdelta.total_seconds())

        except:
            success = 0
            self.increment(self.ERROR_METRIC_NAME)
            raise
        finally:
            tags = instance.get('tags', [])
            tags.extend(['sha:{0}'.format(sha)])
            self.gauge(self.VERSION_METRIC_NAME, success, tags = tags)


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

import html_handlers
import models

def main():
  application = webapp.WSGIApplication([
      ("/", html_handlers.MainPageHandler),
      ("/about", html_handlers.AboutHandler),
      ("/make", html_handlers.MakeHandler),
      ("/make.do", html_handlers.MakeSubmitHandler),
      ("/mymfks", html_handlers.MyMfksHandler),
      ("/vote/(.*)", html_handlers.VoteHandler),
      ("/vote.do", html_handlers.VoteSubmitHandler),
      ("/i/(.*)", html_handlers.EntityImageHandler),
      ("/.*", html_handlers.CatchAllHandler),
    ])
  util.run_wsgi_app(application)

if __name__ == '__main__':
  main()

Response:
<DELETE> # TODO(mjkelly): Clean up these handlers.
  <DELETE_END> <|endoftext|> #!/usr/bin/env python
#
# Copyright 2010 Hunter Freyer and Michael Kelly
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

import html_handlers
import models

def main():
  application = webapp.WSGIApplication([
      ("/", html_handlers.MainPageHandler),
      ("/about", html_handlers.AboutHandler),
      ("/make", html_handlers.MakeHandler),
      ("/make.do", html_handlers.MakeSubmitHandler),
      ("/mymfks", html_handlers.MyMfksHandler),
      ("/vote/(.*)", html_handlers.VoteHandler),
      ("/vote.do", html_handlers.VoteSubmitHandler),
      ("/i/(.*)", html_handlers.EntityImageHandler),
      ("/.*", html_handlers.CatchAllHandler),
    ])
  util.run_wsgi_app(application)

if __name__ == '__main__':
  main()

Prompt:
Remove TODO -- handlers have been cleaned up.

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

import html_handlers
import models

def main():
  # TODO(mjkelly): Clean up these handlers.
  application = webapp.WSGIApplication([
      ("/", html_handlers.MainPageHandler),
      ("/about", html_handlers.AboutHandler),
      ("/make", html_handlers.MakeHandler),
      ("/make.do", html_handlers.MakeSubmitHandler),
      ("/mymfks", html_handlers.MyMfksHandler),
      ("/vote/(.*)", html_handlers.VoteHandler),
      ("/vote.do", html_handlers.VoteSubmitHandler),
      ("/i/(.*)", html_handlers.EntityImageHandler),
      ("/.*", html_handlers.CatchAllHandler),
    ])
  util.run_wsgi_app(application)

if __name__ == '__main__':
  main()


from dateutil.parser import parse

from comics.aggregator.crawler import CrawlerBase, CrawlerResult
from comics.meta.base import MetaBase

class Meta(MetaBase):
    name = "KAL's Cartoon"
    language = 'en'
    url = 'http://www.economist.com'
    start_date = '2006-01-05'
    rights = 'Kevin Kallaugher'

class Crawler(CrawlerBase):
    history_capable_days = 1000
    schedule = 'Th'

    def crawl(self, pub_date):
        article_list = self.parse_page('http://www.economist.com/research/articlesBySubject/display.cfm?id=8717275&startRow=1&endrow=500')
        article_list.remove('.web-only')

        for block in article_list.root.cssselect('.article-list .block'):
            date = block.cssselect('.date')[0]

            if pub_date != parse(date.text_content()).date():
                continue

            anchor = blockdate.cssselect('h2 a')[0]

            if "KAL's cartoon" not in anchor.text_content():
                continue

            page = self.parse_page(anchor.get('href'))
            return CrawlerResult(page.src('.content-image-full img'))

Add crawler for KAL's cartoon

Add crawler for KAL's cartoon

Response:
<REPLACE_OLD>  <REPLACE_NEW> from dateutil.parser import parse

from comics.aggregator.crawler import CrawlerBase, CrawlerResult
from comics.meta.base import MetaBase

class Meta(MetaBase):
    name = "KAL's Cartoon"
    language = 'en'
    url = 'http://www.economist.com'
    start_date = '2006-01-05'
    rights = 'Kevin Kallaugher'

class Crawler(CrawlerBase):
    history_capable_days = 1000
    schedule = 'Th'

    def crawl(self, pub_date):
        article_list = self.parse_page('http://www.economist.com/research/articlesBySubject/display.cfm?id=8717275&startRow=1&endrow=500')
        article_list.remove('.web-only')

        for block in article_list.root.cssselect('.article-list .block'):
            date = block.cssselect('.date')[0]

            if pub_date != parse(date.text_content()).date():
                continue

            anchor = blockdate.cssselect('h2 a')[0]

            if "KAL's cartoon" not in anchor.text_content():
                continue

            page = self.parse_page(anchor.get('href'))
            return CrawlerResult(page.src('.content-image-full img'))
 <REPLACE_END> <|endoftext|> from dateutil.parser import parse

from comics.aggregator.crawler import CrawlerBase, CrawlerResult
from comics.meta.base import MetaBase

class Meta(MetaBase):
    name = "KAL's Cartoon"
    language = 'en'
    url = 'http://www.economist.com'
    start_date = '2006-01-05'
    rights = 'Kevin Kallaugher'

class Crawler(CrawlerBase):
    history_capable_days = 1000
    schedule = 'Th'

    def crawl(self, pub_date):
        article_list = self.parse_page('http://www.economist.com/research/articlesBySubject/display.cfm?id=8717275&startRow=1&endrow=500')
        article_list.remove('.web-only')

        for block in article_list.root.cssselect('.article-list .block'):
            date = block.cssselect('.date')[0]

            if pub_date != parse(date.text_content()).date():
                continue

            anchor = blockdate.cssselect('h2 a')[0]

            if "KAL's cartoon" not in anchor.text_content():
                continue

            page = self.parse_page(anchor.get('href'))
            return CrawlerResult(page.src('.content-image-full img'))

Prompt: Add crawler for KAL's cartoon


from numba.np.ufunc.parallel import get_thread_count
from os import environ as env
from numba.core import config
import unittest

class TestParallelEnvVariable(unittest.TestCase):
    """
    Tests environment variables related to the underlying "parallel"
    functions for npyufuncs.
    """

    _numba_parallel_test_ = False

    def test_num_threads_variable(self):
        """
        Tests the NUMBA_NUM_THREADS env variable behaves as expected.
        """
        key = 'NUMBA_NUM_THREADS'
        current = str(getattr(env, key, config.NUMBA_NUM_THREADS))
        threads = "3154"
        env[key] = threads
        try:
            config.reload_config()
        except RuntimeError as e:
            # This test should fail if threads have already been launched
            self.assertIn("Cannot set NUMBA_NUM_THREADS", e.args[0])
        else:
            self.assertEqual(threads, str(get_thread_count()))
            self.assertEqual(threads, str(config.NUMBA_NUM_THREADS))
        finally:
            # reset the env variable/set to default. Should not fail even if
            # threads are launched because the value is the same.
            env[key] = current
            config.reload_config()

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

Response:
<REPLACE_OLD> config.NUMBA_DEFAULT_NUM_THREADS))
 <REPLACE_NEW> config.NUMBA_NUM_THREADS))
 <REPLACE_END> <|endoftext|> from numba.np.ufunc.parallel import get_thread_count
from os import environ as env
from numba.core import config
import unittest

class TestParallelEnvVariable(unittest.TestCase):
    """
    Tests environment variables related to the underlying "parallel"
    functions for npyufuncs.
    """

    _numba_parallel_test_ = False

    def test_num_threads_variable(self):
        """
        Tests the NUMBA_NUM_THREADS env variable behaves as expected.
        """
        key = 'NUMBA_NUM_THREADS'
        current = str(getattr(env, key, config.NUMBA_NUM_THREADS))
        threads = "3154"
        env[key] = threads
        try:
            config.reload_config()
        except RuntimeError as e:
            # This test should fail if threads have already been launched
            self.assertIn("Cannot set NUMBA_NUM_THREADS", e.args[0])
        else:
            self.assertEqual(threads, str(get_thread_count()))
            self.assertEqual(threads, str(config.NUMBA_NUM_THREADS))
        finally:
            # reset the env variable/set to default. Should not fail even if
            # threads are launched because the value is the same.
            env[key] = current
            config.reload_config()

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

Prompt:
Reset the num threads to the env variable, not the default

from numba.np.ufunc.parallel import get_thread_count
from os import environ as env
from numba.core import config
import unittest

class TestParallelEnvVariable(unittest.TestCase):
    """
    Tests environment variables related to the underlying "parallel"
    functions for npyufuncs.
    """

    _numba_parallel_test_ = False

    def test_num_threads_variable(self):
        """
        Tests the NUMBA_NUM_THREADS env variable behaves as expected.
        """
        key = 'NUMBA_NUM_THREADS'
        current = str(getattr(env, key, config.NUMBA_DEFAULT_NUM_THREADS))
        threads = "3154"
        env[key] = threads
        try:
            config.reload_config()
        except RuntimeError as e:
            # This test should fail if threads have already been launched
            self.assertIn("Cannot set NUMBA_NUM_THREADS", e.args[0])
        else:
            self.assertEqual(threads, str(get_thread_count()))
            self.assertEqual(threads, str(config.NUMBA_NUM_THREADS))
        finally:
            # reset the env variable/set to default. Should not fail even if
            # threads are launched because the value is the same.
            env[key] = current
            config.reload_config()

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


# Copyright (C) 2015 Linaro Limited
#
# Author: Stevan Radakovic <stevan.radakovic@linaro.org>
#
# This file is part of Lava Server.
#
# Lava Server is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License version 3
# as published by the Free Software Foundation
#
# Lava Server is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Lava Dashboard.  If not, see <http://www.gnu.org/licenses/>.

"""
Administration interface of the LAVA Results application.
"""

from django import forms
from django.contrib import admin
from django.contrib.admin.actions import delete_selected
from django.contrib.contenttypes import fields
from django.utils.translation import ugettext_lazy as _

from lava_results_app.models import (
    Query
)

class QueryAdmin(admin.ModelAdmin):
    save_as = True

admin.site.register(Query, QueryAdmin)

Response:
<REPLACE_OLD> generic
from <REPLACE_NEW> fields
from <REPLACE_END> <|endoftext|> # Copyright (C) 2015 Linaro Limited
#
# Author: Stevan Radakovic <stevan.radakovic@linaro.org>
#
# This file is part of Lava Server.
#
# Lava Server is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License version 3
# as published by the Free Software Foundation
#
# Lava Server is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Lava Dashboard.  If not, see <http://www.gnu.org/licenses/>.

"""
Administration interface of the LAVA Results application.
"""

from django import forms
from django.contrib import admin
from django.contrib.admin.actions import delete_selected
from django.contrib.contenttypes import fields
from django.utils.translation import ugettext_lazy as _

from lava_results_app.models import (
    Query
)

class QueryAdmin(admin.ModelAdmin):
    save_as = True

admin.site.register(Query, QueryAdmin)

Prompt:
Fix deprecation warnings - RemovedInDjango19Warning

Extend fix in review #9160 for subsequent changes.

Change-Id: I25fbe759cfd28ac683ef94b58a8da098141e8d48

# Copyright (C) 2015 Linaro Limited
#
# Author: Stevan Radakovic <stevan.radakovic@linaro.org>
#
# This file is part of Lava Server.
#
# Lava Server is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License version 3
# as published by the Free Software Foundation
#
# Lava Server is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Lava Dashboard.  If not, see <http://www.gnu.org/licenses/>.

"""
Administration interface of the LAVA Results application.
"""

from django import forms
from django.contrib import admin
from django.contrib.admin.actions import delete_selected
from django.contrib.contenttypes import generic
from django.utils.translation import ugettext_lazy as _

from lava_results_app.models import (
    Query
)

class QueryAdmin(admin.ModelAdmin):
    save_as = True

admin.site.register(Query, QueryAdmin)


# -*- coding: utf-8 -*-
"""
JSON-LD extractor
"""

import json
import re

import lxml.etree
import lxml.html

HTML_OR_JS_COMMENTLINE = re.compile('^\s*(//.*|<!--.*-->)')

class JsonLdExtractor(object):
    _xp_jsonld = lxml.etree.XPath('descendant-or-self::script[@type="application/ld+json"]')

    def extract(self, htmlstring, base_url=None, encoding="UTF-8"):
        parser = lxml.html.HTMLParser(encoding=encoding)
        lxmldoc = lxml.html.fromstring(htmlstring, parser=parser)
        return self.extract_items(lxmldoc, base_url=base_url)

    def extract_items(self, document, base_url=None):
        return [item for items in map(self._extract_items,
                                      self._xp_jsonld(document))
                     for item in items
                         if item]

    def _extract_items(self, node):
        script = node.xpath('string()')
        # now do remove possible leading HTML/JavaScript comment first, allow control characters to be loaded
        # TODO: `strict=False` can be configurable if needed
        data = json.loads(HTML_OR_JS_COMMENTLINE.sub('', script), strict=False)
        if isinstance(data, list):
            return data
        elif isinstance(data, dict):
            return [data]

Response:
<REPLACE_OLD> try:
     <REPLACE_NEW> # now do remove possible leading HTML/JavaScript comment first, allow control characters to be loaded
        # TODO: `strict=False` can be configurable if needed
 <REPLACE_END> <DELETE> json.loads(script)
        except ValueError:
            # sometimes JSON-decoding errors are due to leading HTML or JavaScript comments
            try:
                data = <DELETE_END> <REPLACE_OLD> script))
            except ValueError: # ValueError again because json.JSONDecodeError(bases from ValueError) appears since Python 3.5
                # some pages have JSON-LD data with control characters, json.loads should use strict=False
                data = json.loads(script, <REPLACE_NEW> script), <REPLACE_END> <|endoftext|> # -*- coding: utf-8 -*-
"""
JSON-LD extractor
"""

import json
import re

import lxml.etree
import lxml.html

HTML_OR_JS_COMMENTLINE = re.compile('^\s*(//.*|<!--.*-->)')

class JsonLdExtractor(object):
    _xp_jsonld = lxml.etree.XPath('descendant-or-self::script[@type="application/ld+json"]')

    def extract(self, htmlstring, base_url=None, encoding="UTF-8"):
        parser = lxml.html.HTMLParser(encoding=encoding)
        lxmldoc = lxml.html.fromstring(htmlstring, parser=parser)
        return self.extract_items(lxmldoc, base_url=base_url)

    def extract_items(self, document, base_url=None):
        return [item for items in map(self._extract_items,
                                      self._xp_jsonld(document))
                     for item in items
                         if item]

    def _extract_items(self, node):
        script = node.xpath('string()')
        # now do remove possible leading HTML/JavaScript comment first, allow control characters to be loaded
        # TODO: `strict=False` can be configurable if needed
        data = json.loads(HTML_OR_JS_COMMENTLINE.sub('', script), strict=False)
        if isinstance(data, list):
            return data
        elif isinstance(data, dict):
            return [data]

Prompt:
Mod: Remove leading comments and allow control characters directly.

# -*- coding: utf-8 -*-
"""
JSON-LD extractor
"""

import json
import re

import lxml.etree
import lxml.html

HTML_OR_JS_COMMENTLINE = re.compile('^\s*(//.*|<!--.*-->)')

class JsonLdExtractor(object):
    _xp_jsonld = lxml.etree.XPath('descendant-or-self::script[@type="application/ld+json"]')

    def extract(self, htmlstring, base_url=None, encoding="UTF-8"):
        parser = lxml.html.HTMLParser(encoding=encoding)
        lxmldoc = lxml.html.fromstring(htmlstring, parser=parser)
        return self.extract_items(lxmldoc, base_url=base_url)

    def extract_items(self, document, base_url=None):
        return [item for items in map(self._extract_items,
                                      self._xp_jsonld(document))
                     for item in items
                         if item]

    def _extract_items(self, node):
        script = node.xpath('string()')
        try:
            data = json.loads(script)
        except ValueError:
            # sometimes JSON-decoding errors are due to leading HTML or JavaScript comments
            try:
                data = json.loads(HTML_OR_JS_COMMENTLINE.sub('', script))
            except ValueError: # ValueError again because json.JSONDecodeError(bases from ValueError) appears since Python 3.5
                # some pages have JSON-LD data with control characters, json.loads should use strict=False
                data = json.loads(script, strict=False)
        if isinstance(data, list):
            return data
        elif isinstance(data, dict):
            return [data]


# Based off of https://github.com/jupyter/notebook/blob/master/docs/source/extending/savehooks.rst

import io
import os
from notebook.utils import to_api_path

_script_exporter = None
_html_exporter = None

def script_post_save(model, os_path, contents_manager, **kwargs):
    """convert notebooks to Python script after save with nbconvert
    replaces `ipython notebook --script`
    """
    from nbconvert.exporters.script import ScriptExporter

    if model['type'] != 'notebook':
        return

    global _script_exporter
    if _script_exporter is None:
        _script_exporter = ScriptExporter(parent=contents_manager)
    log = contents_manager.log

    # save .py file
    base, ext = os.path.splitext(os_path)
    script, resources = _script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)

c.FileContentsManager.post_save_hook = script_post_save

Add script to automatically save py file in jupyter.

Response:
<INSERT> # Based off of https://github.com/jupyter/notebook/blob/master/docs/source/extending/savehooks.rst

import io
import os
from notebook.utils import to_api_path

_script_exporter = None
_html_exporter = None

def script_post_save(model, os_path, contents_manager, **kwargs):
 <INSERT_END> <INSERT>   """convert notebooks to Python script after save with nbconvert
    replaces `ipython notebook --script`
    """
    from nbconvert.exporters.script import ScriptExporter

    if model['type'] != 'notebook':
        return

    global _script_exporter
    if _script_exporter is None:
        _script_exporter = ScriptExporter(parent=contents_manager)
    log = contents_manager.log

    # save .py file
    base, ext = os.path.splitext(os_path)
    script, resources = _script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)

c.FileContentsManager.post_save_hook = script_post_save
 <INSERT_END> <|endoftext|> # Based off of https://github.com/jupyter/notebook/blob/master/docs/source/extending/savehooks.rst

import io
import os
from notebook.utils import to_api_path

_script_exporter = None
_html_exporter = None

def script_post_save(model, os_path, contents_manager, **kwargs):
    """convert notebooks to Python script after save with nbconvert
    replaces `ipython notebook --script`
    """
    from nbconvert.exporters.script import ScriptExporter

    if model['type'] != 'notebook':
        return

    global _script_exporter
    if _script_exporter is None:
        _script_exporter = ScriptExporter(parent=contents_manager)
    log = contents_manager.log

    # save .py file
    base, ext = os.path.splitext(os_path)
    script, resources = _script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)

c.FileContentsManager.post_save_hook = script_post_save

Prompt: Add script to automatically save py file in jupyter.


import numpy as np
from menpofit.math.correlationfilter import mccf, imccf

# TODO: document me!
class IncrementalCorrelationFilterThinWrapper(object):
    r"""
    """
    def __init__(self, cf_callable=mccf, icf_callable=imccf):
        self.cf_callable = cf_callable
        self.icf_callable = icf_callable

    def increment(self, A, B, n_x, Z, t):
        r"""
        """
        # Turn list of X into ndarray
        if isinstance(Z, list):
            Z = np.asarray(Z)
        return self.icf_callable(A, B, n_x, Z, t)

    def train(self, X, t):
        r"""
        """
        # Turn list of X into ndarray
        if isinstance(X, list):
            X = np.asarray(X)
        # Return linear svm filter and bias
        return self.cf_callable(X, t)

Add dummy wrapper for correlation filters

Response:
<REPLACE_OLD>  <REPLACE_NEW> import numpy as np
from menpofit.math.correlationfilter import mccf, imccf

# TODO: document me!
class IncrementalCorrelationFilterThinWrapper(object):
    r"""
    """
    def __init__(self, cf_callable=mccf, icf_callable=imccf):
        self.cf_callable = cf_callable
        self.icf_callable = icf_callable

    def increment(self, A, B, n_x, Z, t):
        r"""
        """
        # Turn list of X into ndarray
        if isinstance(Z, list):
            Z = np.asarray(Z)
        return self.icf_callable(A, B, n_x, Z, t)

    def train(self, X, t):
        r"""
        """
        # Turn list of X into ndarray
        if isinstance(X, list):
            X = np.asarray(X)
        # Return linear svm filter and bias
        return self.cf_callable(X, t)
 <REPLACE_END> <|endoftext|> import numpy as np
from menpofit.math.correlationfilter import mccf, imccf

# TODO: document me!
class IncrementalCorrelationFilterThinWrapper(object):
    r"""
    """
    def __init__(self, cf_callable=mccf, icf_callable=imccf):
        self.cf_callable = cf_callable
        self.icf_callable = icf_callable

    def increment(self, A, B, n_x, Z, t):
        r"""
        """
        # Turn list of X into ndarray
        if isinstance(Z, list):
            Z = np.asarray(Z)
        return self.icf_callable(A, B, n_x, Z, t)

    def train(self, X, t):
        r"""
        """
        # Turn list of X into ndarray
        if isinstance(X, list):
            X = np.asarray(X)
        # Return linear svm filter and bias
        return self.cf_callable(X, t)

Prompt: Add dummy wrapper for correlation filters


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

"""This module contains functions that mutate or make transformations on a network."""

from . import collapse, deletion, expansion, induction, inference, metadata, transfer
from .collapse import *
from .deletion import *
from .expansion import *
from .induction import *
from .inference import *
from .metadata import *
from .transfer import *

__all__ = (
        collapse.__all__ +
        deletion.__all__ +
        expansion.__all__ +
        induction.__all__ +
        inference.__all__ +
        metadata.__all__ +
        transfer.__all__
)

Add collapse to star import

Add collapse to star import

Response:
<INSERT> collapse, <INSERT_END> <INSERT> .collapse import *
from <INSERT_END> <INSERT>        collapse.__all__ +
 <INSERT_END> <|endoftext|> # -*- coding: utf-8 -*-

"""This module contains functions that mutate or make transformations on a network."""

from . import collapse, deletion, expansion, induction, inference, metadata, transfer
from .collapse import *
from .deletion import *
from .expansion import *
from .induction import *
from .inference import *
from .metadata import *
from .transfer import *

__all__ = (
        collapse.__all__ +
        deletion.__all__ +
        expansion.__all__ +
        induction.__all__ +
        inference.__all__ +
        metadata.__all__ +
        transfer.__all__
)

Prompt:
Add collapse to star import

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

"""This module contains functions that mutate or make transformations on a network."""

from . import deletion, expansion, induction, inference, metadata, transfer
from .deletion import *
from .expansion import *
from .induction import *
from .inference import *
from .metadata import *
from .transfer import *

__all__ = (
        deletion.__all__ +
        expansion.__all__ +
        induction.__all__ +
        inference.__all__ +
        metadata.__all__ +
        transfer.__all__
)