def result(return_code, output, stderr_output, allow_error=False):
    result = ExecutionResult(return_code, output, stderr_output)
    if allow_error or return_code == 0:
        return result
    else:
        raise result.to_error()

class RunProcessError(RuntimeError):
    def __init__(self, return_code, output, stderr_output):
        message = "return code: {0}\noutput: {1}\nstderr output: {2}".format(
            return_code, output, stderr_output)
        super(type(self), self).__init__(message)
        self.return_code = return_code
        self.output = output
        self.stderr_output = stderr_output

class ExecutionResult(object):
    def __init__(self, return_code, output, stderr_output):
        self.return_code = return_code
        self.output = output
        self.stderr_output = stderr_output

    def to_error(self):
        return RunProcessError(
            self.return_code,
            self.output,
            self.stderr_output
        )

Response:
<INSERT>    result = ExecutionResult(return_code, output, stderr_output)
 <INSERT_END> <REPLACE_OLD> ExecutionResult(return_code, output, stderr_output)
 <REPLACE_NEW> result
 <REPLACE_END> <REPLACE_OLD> RunProcessError(return_code, output, stderr_output)

class <REPLACE_NEW> result.to_error()

class <REPLACE_END> <INSERT>
    def to_error(self):
        return RunProcessError(
            self.return_code,
            self.output,
            self.stderr_output
        )
 <INSERT_END> <|endoftext|> def result(return_code, output, stderr_output, allow_error=False):
    result = ExecutionResult(return_code, output, stderr_output)
    if allow_error or return_code == 0:
        return result
    else:
        raise result.to_error()

class RunProcessError(RuntimeError):
    def __init__(self, return_code, output, stderr_output):
        message = "return code: {0}\noutput: {1}\nstderr output: {2}".format(
            return_code, output, stderr_output)
        super(type(self), self).__init__(message)
        self.return_code = return_code
        self.output = output
        self.stderr_output = stderr_output

class ExecutionResult(object):
    def __init__(self, return_code, output, stderr_output):
        self.return_code = return_code
        self.output = output
        self.stderr_output = stderr_output

    def to_error(self):
        return RunProcessError(
            self.return_code,
            self.output,
            self.stderr_output
        )

Prompt:
Move logic for creating RunProcessError to ExecutionResult.to_error

def result(return_code, output, stderr_output, allow_error=False):
    if allow_error or return_code == 0:
        return ExecutionResult(return_code, output, stderr_output)
    else:
        raise RunProcessError(return_code, output, stderr_output)

class RunProcessError(RuntimeError):
    def __init__(self, return_code, output, stderr_output):
        message = "return code: {0}\noutput: {1}\nstderr output: {2}".format(
            return_code, output, stderr_output)
        super(type(self), self).__init__(message)
        self.return_code = return_code
        self.output = output
        self.stderr_output = stderr_output

class ExecutionResult(object):
    def __init__(self, return_code, output, stderr_output):
        self.return_code = return_code
        self.output = output
        self.stderr_output = stderr_output


import simpledaemon

class clio_daemon(simpledaemon.Daemon):
    default_conf = 'clio_daemon.conf'
    section = 'clio'

    def run(self):
        import eventlet
        from clio.store import app
        eventlet.serve(eventlet.listen((app.config['HOST'], app.config['PORT']), backlog=2048), app)

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

Response:
<DELETE> logging

import <DELETE_END> <DELETE> logger = logging.getLogger()
        if logger.handlers:
            [app.logger.addHandler(h) for h in logger.handlers]
        app.logger.setLevel(logger.level)
        <DELETE_END> <|endoftext|>
import simpledaemon

class clio_daemon(simpledaemon.Daemon):
    default_conf = 'clio_daemon.conf'
    section = 'clio'

    def run(self):
        import eventlet
        from clio.store import app
        eventlet.serve(eventlet.listen((app.config['HOST'], app.config['PORT']), backlog=2048), app)

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

Prompt:
Revert "output flask logging into simpledaemon's log file."

This is completely superfluous - logging does this already
automatically.

This reverts commit 18091efef351ecddb1d29ee7d01d0a7fb567a7b7.

import logging

import simpledaemon

class clio_daemon(simpledaemon.Daemon):
    default_conf = 'clio_daemon.conf'
    section = 'clio'

    def run(self):
        import eventlet
        from clio.store import app
        logger = logging.getLogger()
        if logger.handlers:
            [app.logger.addHandler(h) for h in logger.handlers]
        app.logger.setLevel(logger.level)
        eventlet.serve(eventlet.listen((app.config['HOST'], app.config['PORT']), backlog=2048), app)

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


# Invoked by: CloudFormation
# Returns: A `Data` object to a pre-signed URL
#
# Deploys the contents of a versioned zip file object from one bucket in S3
# to a another bucket

import sys
import boto3
from botocore.client import Config
import io
import zipfile
import os
import urllib.request
import json
import traceback

s3 = boto3.client('s3', config=Config(signature_version='s3v4'))

STATUS_SUCCESS = 'SUCCESS'
STATUS_FAILED = 'FAILED'

def send_response(event, context, res_status, res_reason='Done', res_data={}):
    res_data = json.dumps({
        'Status': res_status,
        'Reason': res_reason,
        'PhysicalResourceId': context.log_stream_name,
        'StackId': event['StackId'],
        'RequestId': event['RequestId'],
        'LogicalResourceId': event['LogicalResourceId'],
        'Data': res_data
    }).encode()

    headers = {'content-type': ''}

    url = event['ResponseURL']
    req = urllib.request.Request(url, data=res_data, method='PUT', headers=headers)
    urllib.request.urlopen(req)

def lambda_handler(event, context):
    try:
        print(event)

        if event['RequestType'] == 'Create' or event['RequestType'] == 'Update':
            # The location of the built static site archive file in S3
            bucket = event['ResourceProperties']['StaticSiteArchiveS3Bucket']
            key = event['ResourceProperties']['StaticSiteArchiveS3Object']
            version = event['ResourceProperties']['StaticSiteArchiveS3ObjectVersion']

            # Get the archive object
            s3_obj = s3.get_object(Bucket=bucket, Key=key, VersionId=version)

            unzip_dir = f"/tmp/unzip-{event['RequestId']}"

            # Unzip the archive, to disk
            with zipfile.ZipFile(io.BytesIO(s3_obj['Body'].read()), 'r') as zip:
                zip.extractall(unzip_dir)

            # The bucket to deploy the static to
            deploy_bucket = event['ResourceProperties']['StaticSiteS3DeployBucket']

            # Upload everything from the unzipped archive
            for root, dirs, files in os.walk(unzip_dir):
                for filename in files:

                    local_path = os.path.join(root, filename)
                    s3_key = os.path.relpath(local_path, unzip_dir)

                    print(f"Uploading {s3_key} to {deploy_bucket}")
                    s3.upload_file(local_path, deploy_bucket, s3_key)

            send_response(event, context, STATUS_SUCCESS)
        else:
            send_response(event, context, STATUS_SUCCESS)

    except Exception as e:
        print('Function failed due to exception.')
        print(e)
        traceback.print_exc()
        send_response(event, context, STATUS_FAILED, res_reason=str(e))

Response:
<INSERT> Invoked by: CloudFormation
# Returns: A `Data` object to a pre-signed URL
#
# Deploys the contents of a versioned zip file object from one bucket in S3
# to a another bucket

import sys
import boto3
from botocore.client <INSERT_END> <REPLACE_OLD> boto3

def <REPLACE_NEW> Config
import io
import zipfile
import os
import urllib.request
import json
import traceback

s3 = boto3.client('s3', config=Config(signature_version='s3v4'))

STATUS_SUCCESS = 'SUCCESS'
STATUS_FAILED = 'FAILED'

def send_response(event, context, res_status, res_reason='Done', res_data={}):
    res_data = json.dumps({
        'Status': res_status,
        'Reason': res_reason,
        'PhysicalResourceId': context.log_stream_name,
        'StackId': event['StackId'],
        'RequestId': event['RequestId'],
        'LogicalResourceId': event['LogicalResourceId'],
        'Data': res_data
    }).encode()

    headers = {'content-type': ''}

    url = event['ResponseURL']
    req = urllib.request.Request(url, data=res_data, method='PUT', headers=headers)
    urllib.request.urlopen(req)

def <REPLACE_END> <REPLACE_OLD> pass
 <REPLACE_NEW> try:
        print(event)

        if event['RequestType'] == 'Create' or event['RequestType'] == 'Update':
            # The location of the built static site archive file in S3
            bucket = event['ResourceProperties']['StaticSiteArchiveS3Bucket']
            key = event['ResourceProperties']['StaticSiteArchiveS3Object']
            version = event['ResourceProperties']['StaticSiteArchiveS3ObjectVersion']

            # Get the archive object
            s3_obj = s3.get_object(Bucket=bucket, Key=key, VersionId=version)

            unzip_dir = f"/tmp/unzip-{event['RequestId']}"

            # Unzip the archive, to disk
            with zipfile.ZipFile(io.BytesIO(s3_obj['Body'].read()), 'r') as zip:
                zip.extractall(unzip_dir)

            # The bucket to deploy the static to
            deploy_bucket = event['ResourceProperties']['StaticSiteS3DeployBucket']

            # Upload everything from the unzipped archive
            for root, dirs, files in os.walk(unzip_dir):
                for filename in files:

                    local_path = os.path.join(root, filename)
                    s3_key = os.path.relpath(local_path, unzip_dir)

                    print(f"Uploading {s3_key} to {deploy_bucket}")
                    s3.upload_file(local_path, deploy_bucket, s3_key)

            send_response(event, context, STATUS_SUCCESS)
        else:
            send_response(event, context, STATUS_SUCCESS)

    except Exception as e:
        print('Function failed due to exception.')
        print(e)
        traceback.print_exc()
        send_response(event, context, STATUS_FAILED, res_reason=str(e))
 <REPLACE_END> <|endoftext|> # Invoked by: CloudFormation
# Returns: A `Data` object to a pre-signed URL
#
# Deploys the contents of a versioned zip file object from one bucket in S3
# to a another bucket

import sys
import boto3
from botocore.client import Config
import io
import zipfile
import os
import urllib.request
import json
import traceback

s3 = boto3.client('s3', config=Config(signature_version='s3v4'))

STATUS_SUCCESS = 'SUCCESS'
STATUS_FAILED = 'FAILED'

def send_response(event, context, res_status, res_reason='Done', res_data={}):
    res_data = json.dumps({
        'Status': res_status,
        'Reason': res_reason,
        'PhysicalResourceId': context.log_stream_name,
        'StackId': event['StackId'],
        'RequestId': event['RequestId'],
        'LogicalResourceId': event['LogicalResourceId'],
        'Data': res_data
    }).encode()

    headers = {'content-type': ''}

    url = event['ResponseURL']
    req = urllib.request.Request(url, data=res_data, method='PUT', headers=headers)
    urllib.request.urlopen(req)

def lambda_handler(event, context):
    try:
        print(event)

        if event['RequestType'] == 'Create' or event['RequestType'] == 'Update':
            # The location of the built static site archive file in S3
            bucket = event['ResourceProperties']['StaticSiteArchiveS3Bucket']
            key = event['ResourceProperties']['StaticSiteArchiveS3Object']
            version = event['ResourceProperties']['StaticSiteArchiveS3ObjectVersion']

            # Get the archive object
            s3_obj = s3.get_object(Bucket=bucket, Key=key, VersionId=version)

            unzip_dir = f"/tmp/unzip-{event['RequestId']}"

            # Unzip the archive, to disk
            with zipfile.ZipFile(io.BytesIO(s3_obj['Body'].read()), 'r') as zip:
                zip.extractall(unzip_dir)

            # The bucket to deploy the static to
            deploy_bucket = event['ResourceProperties']['StaticSiteS3DeployBucket']

            # Upload everything from the unzipped archive
            for root, dirs, files in os.walk(unzip_dir):
                for filename in files:

                    local_path = os.path.join(root, filename)
                    s3_key = os.path.relpath(local_path, unzip_dir)

                    print(f"Uploading {s3_key} to {deploy_bucket}")
                    s3.upload_file(local_path, deploy_bucket, s3_key)

            send_response(event, context, STATUS_SUCCESS)
        else:
            send_response(event, context, STATUS_SUCCESS)

    except Exception as e:
        print('Function failed due to exception.')
        print(e)
        traceback.print_exc()
        send_response(event, context, STATUS_FAILED, res_reason=str(e))

Prompt:
Add S3 static deploy custom resource Lambda function

# import boto3

def lambda_handler(event, context):
    pass


import json
from lxml import html

class XPathObject(object):
    input_properties = {}
    """Dict of keys (property names) and XPaths (to read vals from)"""

    @classmethod
    def FromHTML(cls, html_contents):
        inst = cls()
        print("Reading through {b} bytes for {c} properties...".format(
            b=len(html_contents),
            c=len(cls.input_properties)))

        tree = html.fromstring(html_contents)

        for attr_name, xpath in cls.input_properties.items():
            print("Searching for '{n}': {x}".format(
                n=attr_name,
                x=xpath))
            elements = tree.xpath(xpath)

            if not len(elements):
                print("Failed to find '{n}': {x}".format(
                    n=attr_name,
                    x=xpath))
                continue

            setattr(
                inst,
                attr_name,
                elements[0].text)

        return inst

    def __repr__(self):
        return json.dumps(
            self.__dict__,
            indent=4,
            separators=(',', ': '))

Add json library for repr() calls

Add json library for repr() calls

Response:
<REPLACE_OLD> from <REPLACE_NEW> import json
from <REPLACE_END> <|endoftext|> import json
from lxml import html

class XPathObject(object):
    input_properties = {}
    """Dict of keys (property names) and XPaths (to read vals from)"""

    @classmethod
    def FromHTML(cls, html_contents):
        inst = cls()
        print("Reading through {b} bytes for {c} properties...".format(
            b=len(html_contents),
            c=len(cls.input_properties)))

        tree = html.fromstring(html_contents)

        for attr_name, xpath in cls.input_properties.items():
            print("Searching for '{n}': {x}".format(
                n=attr_name,
                x=xpath))
            elements = tree.xpath(xpath)

            if not len(elements):
                print("Failed to find '{n}': {x}".format(
                    n=attr_name,
                    x=xpath))
                continue

            setattr(
                inst,
                attr_name,
                elements[0].text)

        return inst

    def __repr__(self):
        return json.dumps(
            self.__dict__,
            indent=4,
            separators=(',', ': '))

Prompt:
Add json library for repr() calls

from lxml import html

class XPathObject(object):
    input_properties = {}
    """Dict of keys (property names) and XPaths (to read vals from)"""

    @classmethod
    def FromHTML(cls, html_contents):
        inst = cls()
        print("Reading through {b} bytes for {c} properties...".format(
            b=len(html_contents),
            c=len(cls.input_properties)))

        tree = html.fromstring(html_contents)

        for attr_name, xpath in cls.input_properties.items():
            print("Searching for '{n}': {x}".format(
                n=attr_name,
                x=xpath))
            elements = tree.xpath(xpath)

            if not len(elements):
                print("Failed to find '{n}': {x}".format(
                    n=attr_name,
                    x=xpath))
                continue

            setattr(
                inst,
                attr_name,
                elements[0].text)

        return inst

    def __repr__(self):
        return json.dumps(
            self.__dict__,
            indent=4,
            separators=(',', ': '))


# Copyright 2022 Google LLC
#
# 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
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.import proto

from .query_editor import QuerySpecification
from .query_executor import AdsReportFetcher

def get_customer_ids(ads_client, customer_id):
    query = """
    SELECT customer_client.id FROM customer_client
    WHERE customer_client.manager = FALSE
    """
    query_specification = QuerySpecification(query).generate()
    report_fetcher = AdsReportFetcher(ads_client, customer_id)
    return report_fetcher.fetch(query_specification)

Response:
<REPLACE_OLD> AdsReportFetcher(ads_client)
 <REPLACE_NEW> AdsReportFetcher(ads_client, customer_id)
 <REPLACE_END> <REPLACE_OLD> report_fetcher.fetch(query_specification, customer_id)
 <REPLACE_NEW> report_fetcher.fetch(query_specification)
 <REPLACE_END> <|endoftext|> # Copyright 2022 Google LLC
#
# 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
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.import proto

from .query_editor import QuerySpecification
from .query_executor import AdsReportFetcher

def get_customer_ids(ads_client, customer_id):
    query = """
    SELECT customer_client.id FROM customer_client
    WHERE customer_client.manager = FALSE
    """
    query_specification = QuerySpecification(query).generate()
    report_fetcher = AdsReportFetcher(ads_client, customer_id)
    return report_fetcher.fetch(query_specification)

Prompt:
Fix incorrect signature for get_customer_ids function

Change-Id: Ib44af3ac6437ad9fa4cbfd9fda9b055b7eff4547

# Copyright 2022 Google LLC
#
# 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
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.import proto

from .query_editor import QuerySpecification
from .query_executor import AdsReportFetcher

def get_customer_ids(ads_client, customer_id):
    query = """
    SELECT customer_client.id FROM customer_client
    WHERE customer_client.manager = FALSE
    """
    query_specification = QuerySpecification(query).generate()
    report_fetcher = AdsReportFetcher(ads_client)
    return report_fetcher.fetch(query_specification, customer_id)


# -*- coding: utf-8 -*-
"""
Created on Fri Aug  7 12:04:18 2015

@author: wirkert
"""

import numpy as np
from msi.io.reader import Reader
from msi.msi import Msi

class SpectrometerReader(Reader):

    def __init__(self):
        pass

    def read(self, file_to_read):
        # our spectrometer like to follow german standards in files, we need
        # to switch to english ones
        transformed=""
        replacements = {',': '.', '\r\n': ''}
        with open(file_to_read) as infile:
            for line in infile:
                for src, target in replacements.iteritems():
                    line = line.replace(src, target)
                transformed = "\n".join([transformed, line])

        for num, line in enumerate(transformed.splitlines(), 1):
            if ">>>>>Begin" in line:
                break

        for num_end, line in enumerate(transformed.splitlines(), 1):
            if ">>>>>End" in line:
                num_end -= 1
                break
        string_only_spectrum = "\n".join(transformed.splitlines()[num:num_end])
        data_vector = np.fromstring(string_only_spectrum,
                                    sep="\t").reshape(-1, 2)
        msi = Msi(data_vector[:, 1],
                  {'wavelengths': data_vector[:, 0] * 10 ** -9})
        return msi

Response:
<REPLACE_OLD> ">>>>>Begin Spectral Data<<<<<" <REPLACE_NEW> ">>>>>Begin" <REPLACE_END> <INSERT> break

        for num_end, line in enumerate(transformed.splitlines(), 1):
            if ">>>>>End" in line:
                num_end -= 1
                <INSERT_END> <REPLACE_OLD> "\n".join(transformed.splitlines()[num:])
 <REPLACE_NEW> "\n".join(transformed.splitlines()[num:num_end])
 <REPLACE_END> <|endoftext|> # -*- coding: utf-8 -*-
"""
Created on Fri Aug  7 12:04:18 2015

@author: wirkert
"""

import numpy as np
from msi.io.reader import Reader
from msi.msi import Msi

class SpectrometerReader(Reader):

    def __init__(self):
        pass

    def read(self, file_to_read):
        # our spectrometer like to follow german standards in files, we need
        # to switch to english ones
        transformed=""
        replacements = {',': '.', '\r\n': ''}
        with open(file_to_read) as infile:
            for line in infile:
                for src, target in replacements.iteritems():
                    line = line.replace(src, target)
                transformed = "\n".join([transformed, line])

        for num, line in enumerate(transformed.splitlines(), 1):
            if ">>>>>Begin" in line:
                break

        for num_end, line in enumerate(transformed.splitlines(), 1):
            if ">>>>>End" in line:
                num_end -= 1
                break
        string_only_spectrum = "\n".join(transformed.splitlines()[num:num_end])
        data_vector = np.fromstring(string_only_spectrum,
                                    sep="\t").reshape(-1, 2)
        msi = Msi(data_vector[:, 1],
                  {'wavelengths': data_vector[:, 0] * 10 ** -9})
        return msi

Prompt:
Change SpectrometerReader a little so it can handle more data formats.

# -*- coding: utf-8 -*-
"""
Created on Fri Aug  7 12:04:18 2015

@author: wirkert
"""

import numpy as np
from msi.io.reader import Reader
from msi.msi import Msi

class SpectrometerReader(Reader):

    def __init__(self):
        pass

    def read(self, file_to_read):
        # our spectrometer like to follow german standards in files, we need
        # to switch to english ones
        transformed=""
        replacements = {',': '.', '\r\n': ''}
        with open(file_to_read) as infile:
            for line in infile:
                for src, target in replacements.iteritems():
                    line = line.replace(src, target)
                transformed = "\n".join([transformed, line])

        for num, line in enumerate(transformed.splitlines(), 1):
            if ">>>>>Begin Spectral Data<<<<<" in line:
                break
        string_only_spectrum = "\n".join(transformed.splitlines()[num:])
        data_vector = np.fromstring(string_only_spectrum,
                                    sep="\t").reshape(-1, 2)
        msi = Msi(data_vector[:, 1],
                  {'wavelengths': data_vector[:, 0] * 10 ** -9})
        return msi


# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Chromium presubmit script for chrome/browser/policy.

See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details on the presubmit API built into gcl.
"""

def GetPreferredTrySlaves():
  return [
    'linux_chromeos',
    'linux_chromeos_clang:compile',
  ]

Send try jobs that touch policy code to the linux_chromeos bot too.

Review URL: https://chromiumcodereview.appspot.com/10473015

git-svn-id: de016e52bd170d2d4f2344f9bf92d50478b649e0@140268 0039d316-1c4b-4281-b951-d872f2087c98

Response:
<INSERT> # Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Chromium presubmit script for chrome/browser/policy.

See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details on the presubmit API built into gcl.
"""

def GetPreferredTrySlaves():
 <INSERT_END> <INSERT> return [
    'linux_chromeos',
    'linux_chromeos_clang:compile',
  ]
 <INSERT_END> <|endoftext|> # Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Chromium presubmit script for chrome/browser/policy.

See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details on the presubmit API built into gcl.
"""

def GetPreferredTrySlaves():
  return [
    'linux_chromeos',
    'linux_chromeos_clang:compile',
  ]

Prompt:
Send try jobs that touch policy code to the linux_chromeos bot too.

Review URL: https://chromiumcodereview.appspot.com/10473015

git-svn-id: de016e52bd170d2d4f2344f9bf92d50478b649e0@140268 0039d316-1c4b-4281-b951-d872f2087c98


from .find_contours import find_contours
from ._regionprops import regionprops, perimeter
from ._structural_similarity import structural_similarity
from ._polygon import approximate_polygon, subdivide_polygon

__all__ = ['find_contours',
           'regionprops',
           'perimeter',
           'structural_similarity',
           'approximate_polygon',
           'subdivide_polygon']

Add __all__ to measure package

Add __all__ to measure package

Response:
<REPLACE_OLD> subdivide_polygon <REPLACE_NEW> subdivide_polygon

__all__ = ['find_contours',
           'regionprops',
           'perimeter',
           'structural_similarity',
           'approximate_polygon',
           'subdivide_polygon']
 <REPLACE_END> <|endoftext|> from .find_contours import find_contours
from ._regionprops import regionprops, perimeter
from ._structural_similarity import structural_similarity
from ._polygon import approximate_polygon, subdivide_polygon

__all__ = ['find_contours',
           'regionprops',
           'perimeter',
           'structural_similarity',
           'approximate_polygon',
           'subdivide_polygon']

Prompt:
Add __all__ to measure package

from .find_contours import find_contours
from ._regionprops import regionprops, perimeter
from ._structural_similarity import structural_similarity
from ._polygon import approximate_polygon, subdivide_polygon


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

# Hack to allow us to run tests before installing.
import sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..')))

from unittest import main

from test_core import *
from test_lazy import *

if __name__ == '__main__':
    main()

Make running unit tests more friendly

Make running unit tests more friendly

Response:
<REPLACE_OLD> -*-

from <REPLACE_NEW> -*-

# Hack to allow us to run tests before installing.
import sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..')))

from <REPLACE_END> <|endoftext|> #!/usr/bin/env python
# -*- coding: utf-8 -*-

# Hack to allow us to run tests before installing.
import sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..')))

from unittest import main

from test_core import *
from test_lazy import *

if __name__ == '__main__':
    main()

Prompt:
Make running unit tests more friendly

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

from unittest import main

from test_core import *
from test_lazy import *

if __name__ == '__main__':
    main()


from .base_settings import *

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'KEY_PREFIX': 'driver27-cache-app'

    }
}

Add settings for django tests.

Add settings for django tests.

Response:
<INSERT> from .base_settings import *

CACHES = {
 <INSERT_END> <INSERT>   'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'KEY_PREFIX': 'driver27-cache-app'

    }
} <INSERT_END> <|endoftext|> from .base_settings import *

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'KEY_PREFIX': 'driver27-cache-app'

    }
}

Prompt: Add settings for django tests.


from setuptools import setup, find_packages

setup(
    name='pyhaproxy',
    version='0.2.4',
    keywords=('haproxy', 'parse'),
    description='A Python library to parse haproxy configuration file',
    license='MIT License',
    install_requires=[],

    include_package_data=True,
    package_data={
        'pyhaproxy': ['*.peg'],
    },

    author='Joey',
    author_email='majunjiev@gmail.com',

    url='https://github.com/imjoey/pyhaproxy',

    packages=find_packages(),
    platforms='any',
)

Response:
<REPLACE_OLD> version='0.2.2',
 <REPLACE_NEW> version='0.2.4',
 <REPLACE_END> <|endoftext|> from setuptools import setup, find_packages

setup(
    name='pyhaproxy',
    version='0.2.4',
    keywords=('haproxy', 'parse'),
    description='A Python library to parse haproxy configuration file',
    license='MIT License',
    install_requires=[],

    include_package_data=True,
    package_data={
        'pyhaproxy': ['*.peg'],
    },

    author='Joey',
    author_email='majunjiev@gmail.com',

    url='https://github.com/imjoey/pyhaproxy',

    packages=find_packages(),
    platforms='any',
)

Prompt:
version2.4: Add additional filestring param for parser.Parse method.

from setuptools import setup, find_packages

setup(
    name='pyhaproxy',
    version='0.2.2',
    keywords=('haproxy', 'parse'),
    description='A Python library to parse haproxy configuration file',
    license='MIT License',
    install_requires=[],

    include_package_data=True,
    package_data={
        'pyhaproxy': ['*.peg'],
    },

    author='Joey',
    author_email='majunjiev@gmail.com',

    url='https://github.com/imjoey/pyhaproxy',

    packages=find_packages(),
    platforms='any',
)


#!/usr/bin/python

import logging
import re
import subprocess
import sys
import os

import calibre_config
from calibre.library.database2 import LibraryDatabase2
from calibre.utils.config import prefs

import args

TODO_DIR = os.path.join(os.environ['HOME'], 'Dropbox', 'todo')
ISSUE_PATTERN = re.compile('(\d+) (.*)$')

args.add_argument('--todobin', '-t', help='path to todo.txt script',
                  type=str, default='todo.sh')
args.add_argument('--todofile', '-f', help='path to todo.txt file',
                  type=str, default=os.path.join(TODO_DIR, 'todo.txt'))
args.add_argument('--verbose', '-v', help='Enable verbose logging',
                  action='store_true')

ARGS={}

def get_issues():
  for line in open(ARGS.todofile, 'r'):
    issueid = ISSUE_PATTERN.match(line)
    if issueid:
      yield int(issueid.group(1)), issueid.group(2)

def get_issue_details():
  db = LibraryDatabase2(prefs['library_path'])
  for issue, title in get_issues():
    mi = db.get_metadata(issue, index_is_id=True)
    yield mi.pubdate, title, issue

def main():
  logger = logging.getLogger()
  if ARGS.verbose:
    logger.setLevel(logging.DEBUG)
  # archive todo list
  try:
    output = subprocess.check_output([ARGS.todobin, 'archive'])
  except CalledProcessError as e:
    logging.error('Unable to archive old items: %s', e.output)
  issues = sorted(get_issue_details())

  # Write out sorted list
  for pubdate, title, idx in issues:
    print "%d %s" % (idx, title)

if __name__ == '__main__':
  ARGS = args.parse_args()
  main()

Sort to-read list by publication date

Sort to-read list by publication date

Response:
<REPLACE_OLD>  <REPLACE_NEW> #!/usr/bin/python

import logging
import re
import subprocess
import sys
import os

import calibre_config
from calibre.library.database2 import LibraryDatabase2
from calibre.utils.config import prefs

import args

TODO_DIR = os.path.join(os.environ['HOME'], 'Dropbox', 'todo')
ISSUE_PATTERN = re.compile('(\d+) (.*)$')

args.add_argument('--todobin', '-t', help='path to todo.txt script',
                  type=str, default='todo.sh')
args.add_argument('--todofile', '-f', help='path to todo.txt file',
                  type=str, default=os.path.join(TODO_DIR, 'todo.txt'))
args.add_argument('--verbose', '-v', help='Enable verbose logging',
                  action='store_true')

ARGS={}

def get_issues():
  for line in open(ARGS.todofile, 'r'):
    issueid = ISSUE_PATTERN.match(line)
    if issueid:
      yield int(issueid.group(1)), issueid.group(2)

def get_issue_details():
  db = LibraryDatabase2(prefs['library_path'])
  for issue, title in get_issues():
    mi = db.get_metadata(issue, index_is_id=True)
    yield mi.pubdate, title, issue

def main():
  logger = logging.getLogger()
  if ARGS.verbose:
    logger.setLevel(logging.DEBUG)
  # archive todo list
  try:
    output = subprocess.check_output([ARGS.todobin, 'archive'])
  except CalledProcessError as e:
    logging.error('Unable to archive old items: %s', e.output)
  issues = sorted(get_issue_details())

  # Write out sorted list
  for pubdate, title, idx in issues:
    print "%d %s" % (idx, title)

if __name__ == '__main__':
  ARGS = args.parse_args()
  main()

 <REPLACE_END> <|endoftext|> #!/usr/bin/python

import logging
import re
import subprocess
import sys
import os

import calibre_config
from calibre.library.database2 import LibraryDatabase2
from calibre.utils.config import prefs

import args

TODO_DIR = os.path.join(os.environ['HOME'], 'Dropbox', 'todo')
ISSUE_PATTERN = re.compile('(\d+) (.*)$')

args.add_argument('--todobin', '-t', help='path to todo.txt script',
                  type=str, default='todo.sh')
args.add_argument('--todofile', '-f', help='path to todo.txt file',
                  type=str, default=os.path.join(TODO_DIR, 'todo.txt'))
args.add_argument('--verbose', '-v', help='Enable verbose logging',
                  action='store_true')

ARGS={}

def get_issues():
  for line in open(ARGS.todofile, 'r'):
    issueid = ISSUE_PATTERN.match(line)
    if issueid:
      yield int(issueid.group(1)), issueid.group(2)

def get_issue_details():
  db = LibraryDatabase2(prefs['library_path'])
  for issue, title in get_issues():
    mi = db.get_metadata(issue, index_is_id=True)
    yield mi.pubdate, title, issue

def main():
  logger = logging.getLogger()
  if ARGS.verbose:
    logger.setLevel(logging.DEBUG)
  # archive todo list
  try:
    output = subprocess.check_output([ARGS.todobin, 'archive'])
  except CalledProcessError as e:
    logging.error('Unable to archive old items: %s', e.output)
  issues = sorted(get_issue_details())

  # Write out sorted list
  for pubdate, title, idx in issues:
    print "%d %s" % (idx, title)

if __name__ == '__main__':
  ARGS = args.parse_args()
  main()

Prompt: Sort to-read list by publication date


# Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details.

import requests
from platformio.util import get_api_result

def pytest_generate_tests(metafunc):
    if "package_data" not in metafunc.fixturenames:
        return
    pkgs_manifest = get_api_result("/packages")
    assert isinstance(pkgs_manifest, dict)
    packages = []
    for _, variants in pkgs_manifest.iteritems():
        for item in variants:
            packages.append(item)
    metafunc.parametrize("package_data", packages)

def validate_response(req):
    assert req.status_code == 200
    assert int(req.headers['Content-Length']) > 0

def validate_package(url):
    r = requests.head(url, allow_redirects=True)
    validate_response(r)
    assert r.headers['Content-Type'] in ("application/x-gzip",
                                         "application/octet-stream")

def test_package(package_data):
    assert package_data['url'].endswith("%d.tar.gz" % package_data['version'])
    validate_package(package_data['url'])

Response:
<REPLACE_OLD> == "application/x-gzip"

def <REPLACE_NEW> in ("application/x-gzip",
                                         "application/octet-stream")

def <REPLACE_END> <|endoftext|> # Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details.

import requests
from platformio.util import get_api_result

def pytest_generate_tests(metafunc):
    if "package_data" not in metafunc.fixturenames:
        return
    pkgs_manifest = get_api_result("/packages")
    assert isinstance(pkgs_manifest, dict)
    packages = []
    for _, variants in pkgs_manifest.iteritems():
        for item in variants:
            packages.append(item)
    metafunc.parametrize("package_data", packages)

def validate_response(req):
    assert req.status_code == 200
    assert int(req.headers['Content-Length']) > 0

def validate_package(url):
    r = requests.head(url, allow_redirects=True)
    validate_response(r)
    assert r.headers['Content-Type'] in ("application/x-gzip",
                                         "application/octet-stream")

def test_package(package_data):
    assert package_data['url'].endswith("%d.tar.gz" % package_data['version'])
    validate_package(package_data['url'])

Prompt:
Add "application/octet-stream" mime type for package

# Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details.

import requests
from platformio.util import get_api_result

def pytest_generate_tests(metafunc):
    if "package_data" not in metafunc.fixturenames:
        return
    pkgs_manifest = get_api_result("/packages")
    assert isinstance(pkgs_manifest, dict)
    packages = []
    for _, variants in pkgs_manifest.iteritems():
        for item in variants:
            packages.append(item)
    metafunc.parametrize("package_data", packages)

def validate_response(req):
    assert req.status_code == 200
    assert int(req.headers['Content-Length']) > 0

def validate_package(url):
    r = requests.head(url, allow_redirects=True)
    validate_response(r)
    assert r.headers['Content-Type'] == "application/x-gzip"

def test_package(package_data):
    assert package_data['url'].endswith("%d.tar.gz" % package_data['version'])
    validate_package(package_data['url'])


import functools

from flask import jsonify, request
from flask.ext.sqlalchemy import Pagination
from .request_utils import dictify_model, error_abort

def paginate_query(query, default_page_size=100, renderer=dictify_model):
    try:
        page_size = int(request.args.get("page_size", default_page_size))
        page = int(request.args.get("page", 1))
    except ValueError:
        error_abort(httplib.BAD_REQUEST, "Invalid integer value")

    num_objects = query.count()

    return {
            "metadata": {
                "total_num_objects": num_objects,
                "total_num_pages": _ceil_div(num_objects, page_size) or 1,
                "page": page,
            },
            "result": [renderer(obj) for obj in query.offset((page-1)*page_size).limit(page_size)],
        }

def _ceil_div(value, divisor):
    returned = float(value) / divisor
    if int(returned) != returned:
        return int(returned) + 1
    return int(returned)

def paginated_view(func=None, renderer=dictify_model):
    if func is None:
        return functools.partial(paginated_view, renderer=renderer)
    @functools.wraps(func)
    def new_func(*args, **kwargs):
        returned = func(*args, **kwargs)
        return jsonify(paginate_query(returned, renderer=renderer))

    return new_func

Response:
<REPLACE_OLD> paginated_view(func):
 <REPLACE_NEW> paginated_view(func=None, renderer=dictify_model):
    if func is None:
        return functools.partial(paginated_view, renderer=renderer)
 <REPLACE_END> <REPLACE_OLD> jsonify(paginate_query(returned))

 <REPLACE_NEW> jsonify(paginate_query(returned, renderer=renderer))

 <REPLACE_END> <|endoftext|> import functools

from flask import jsonify, request
from flask.ext.sqlalchemy import Pagination
from .request_utils import dictify_model, error_abort

def paginate_query(query, default_page_size=100, renderer=dictify_model):
    try:
        page_size = int(request.args.get("page_size", default_page_size))
        page = int(request.args.get("page", 1))
    except ValueError:
        error_abort(httplib.BAD_REQUEST, "Invalid integer value")

    num_objects = query.count()

    return {
            "metadata": {
                "total_num_objects": num_objects,
                "total_num_pages": _ceil_div(num_objects, page_size) or 1,
                "page": page,
            },
            "result": [renderer(obj) for obj in query.offset((page-1)*page_size).limit(page_size)],
        }

def _ceil_div(value, divisor):
    returned = float(value) / divisor
    if int(returned) != returned:
        return int(returned) + 1
    return int(returned)

def paginated_view(func=None, renderer=dictify_model):
    if func is None:
        return functools.partial(paginated_view, renderer=renderer)
    @functools.wraps(func)
    def new_func(*args, **kwargs):
        returned = func(*args, **kwargs)
        return jsonify(paginate_query(returned, renderer=renderer))

    return new_func

Prompt:
Allow renderer argument to paginated_view decorator

import functools

from flask import jsonify, request
from flask.ext.sqlalchemy import Pagination
from .request_utils import dictify_model, error_abort

def paginate_query(query, default_page_size=100, renderer=dictify_model):
    try:
        page_size = int(request.args.get("page_size", default_page_size))
        page = int(request.args.get("page", 1))
    except ValueError:
        error_abort(httplib.BAD_REQUEST, "Invalid integer value")

    num_objects = query.count()

    return {
            "metadata": {
                "total_num_objects": num_objects,
                "total_num_pages": _ceil_div(num_objects, page_size) or 1,
                "page": page,
            },
            "result": [renderer(obj) for obj in query.offset((page-1)*page_size).limit(page_size)],
        }

def _ceil_div(value, divisor):
    returned = float(value) / divisor
    if int(returned) != returned:
        return int(returned) + 1
    return int(returned)

def paginated_view(func):
    @functools.wraps(func)
    def new_func(*args, **kwargs):
        returned = func(*args, **kwargs)
        return jsonify(paginate_query(returned))

    return new_func


# Test that sos kernel is installed

import jupyter_client

try:
    jupyter_client.kernelspec.get_kernel_spec('sos')
except jupyter_client.kernelspec.NoSuchKernel:
    print('sos kernel was not installed')
    print('The following kernels are installed:')
    print('jupyter_client.kernelspec.find_kernel_specs()')
    print(jupyter_client.kernelspec.find_kernel_specs())

# Test that sos kernel is functional

import unittest

from ipykernel.tests.utils import execute, wait_for_idle, assemble_output
from sos_notebook.test_utils import sos_kernel

class TestSoSKernel(unittest.TestCase):
    def testKernel(self):
        with sos_kernel() as kc:
            execute(kc=kc, code='a = 1\n')
            wait_for_idle(kc)
            execute(kc=kc, code='%use Python3\n%get a\nb = a + 1')
            wait_for_idle(kc)
            execute(kc=kc, code='%use SoS\n%get b --from Python3\nprint(b)')
            stdout, stderr = assemble_output(kc.iopub_channel)
            self.assertEqual(stderr, '')
            self.assertEqual(stdout.strip(), '2')

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

Add test of kernel switch.

Add test of kernel switch.

Response:
<REPLACE_OLD> print(jupyter_client.kernelspec.find_kernel_specs())
 <REPLACE_NEW> print(jupyter_client.kernelspec.find_kernel_specs())

# Test that sos kernel is functional

import unittest

from ipykernel.tests.utils import execute, wait_for_idle, assemble_output
from sos_notebook.test_utils import sos_kernel

class TestSoSKernel(unittest.TestCase):
    def testKernel(self):
        with sos_kernel() as kc:
            execute(kc=kc, code='a = 1\n')
            wait_for_idle(kc)
            execute(kc=kc, code='%use Python3\n%get a\nb = a + 1')
            wait_for_idle(kc)
            execute(kc=kc, code='%use SoS\n%get b --from Python3\nprint(b)')
            stdout, stderr = assemble_output(kc.iopub_channel)
            self.assertEqual(stderr, '')
            self.assertEqual(stdout.strip(), '2')

if __name__ == '__main__':
    unittest.main()
 <REPLACE_END> <|endoftext|> # Test that sos kernel is installed

import jupyter_client

try:
    jupyter_client.kernelspec.get_kernel_spec('sos')
except jupyter_client.kernelspec.NoSuchKernel:
    print('sos kernel was not installed')
    print('The following kernels are installed:')
    print('jupyter_client.kernelspec.find_kernel_specs()')
    print(jupyter_client.kernelspec.find_kernel_specs())

# Test that sos kernel is functional

import unittest

from ipykernel.tests.utils import execute, wait_for_idle, assemble_output
from sos_notebook.test_utils import sos_kernel

class TestSoSKernel(unittest.TestCase):
    def testKernel(self):
        with sos_kernel() as kc:
            execute(kc=kc, code='a = 1\n')
            wait_for_idle(kc)
            execute(kc=kc, code='%use Python3\n%get a\nb = a + 1')
            wait_for_idle(kc)
            execute(kc=kc, code='%use SoS\n%get b --from Python3\nprint(b)')
            stdout, stderr = assemble_output(kc.iopub_channel)
            self.assertEqual(stderr, '')
            self.assertEqual(stdout.strip(), '2')

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

Prompt:
Add test of kernel switch.

# Test that sos kernel is installed

import jupyter_client

try:
    jupyter_client.kernelspec.get_kernel_spec('sos')
except jupyter_client.kernelspec.NoSuchKernel:
    print('sos kernel was not installed')
    print('The following kernels are installed:')
    print('jupyter_client.kernelspec.find_kernel_specs()')
    print(jupyter_client.kernelspec.find_kernel_specs())


# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# 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 platformio.compat import IS_WINDOWS
from platformio.debug.config.base import DebugConfigBase

class NativeDebugConfig(DebugConfigBase):

    GDB_INIT_SCRIPT = """
define pio_reset_halt_target
end

define pio_reset_run_target
end

define pio_restart_target
end

$INIT_BREAK
""" + (
        "set startup-with-shell off" if not IS_WINDOWS else ""
    )

Response:
<INSERT> platformio.compat import IS_WINDOWS
from <INSERT_END> <REPLACE_OLD> pio_restart_target
end

$INIT_BREAK
set <REPLACE_NEW> pio_restart_target
end

$INIT_BREAK
""" + (
        "set <REPLACE_END> <REPLACE_OLD> off
"""
 <REPLACE_NEW> off" if not IS_WINDOWS else ""
    )
 <REPLACE_END> <|endoftext|> # Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# 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 platformio.compat import IS_WINDOWS
from platformio.debug.config.base import DebugConfigBase

class NativeDebugConfig(DebugConfigBase):

    GDB_INIT_SCRIPT = """
define pio_reset_halt_target
end

define pio_reset_run_target
end

define pio_restart_target
end

$INIT_BREAK
""" + (
        "set startup-with-shell off" if not IS_WINDOWS else ""
    )

Prompt:
Disable GDB "startup-with-shell" only on Unix platform

# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# 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 platformio.debug.config.base import DebugConfigBase

class NativeDebugConfig(DebugConfigBase):

    GDB_INIT_SCRIPT = """
define pio_reset_halt_target
end

define pio_reset_run_target
end

define pio_restart_target
end

$INIT_BREAK
set startup-with-shell off
"""


"""set submitted at for old brief responses

Revision ID: 770
Revises: 760
Create Date: 2016-10-25 11:10:53.245586

"""

# revision identifiers, used by Alembic.
revision = '770'
down_revision = '760'

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

def upgrade():
	op.execute("""
        UPDATE brief_responses
		SET submitted_at = created_at
		WHERE submitted_at IS NULL
    """)

def downgrade():
	# No downgrade
	pass

Add migration that sets `submitted_at` for old brief responses

For any brief response that does not have a `submitted_at` time,
we add one based on the `created_at` time. As we will now be looking
at `submitted_at` rather than `created_at` to indicate a submitted
brief response, we need to migrate all older brief responses to have
a `submitted_at` time.

Response:
<INSERT> """set submitted at for old brief responses

Revision ID: 770
Revises: 760
Create Date: 2016-10-25 11:10:53.245586

"""

# revision identifiers, used by Alembic.
revision = '770'
down_revision = '760'

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

def upgrade():
	op.execute("""
 <INSERT_END> <INSERT>       UPDATE brief_responses
		SET submitted_at = created_at
		WHERE submitted_at IS NULL
    """)

def downgrade():
	# No downgrade
	pass
 <INSERT_END> <|endoftext|> """set submitted at for old brief responses

Revision ID: 770
Revises: 760
Create Date: 2016-10-25 11:10:53.245586

"""

# revision identifiers, used by Alembic.
revision = '770'
down_revision = '760'

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

def upgrade():
	op.execute("""
        UPDATE brief_responses
		SET submitted_at = created_at
		WHERE submitted_at IS NULL
    """)

def downgrade():
	# No downgrade
	pass

Prompt:
Add migration that sets `submitted_at` for old brief responses

For any brief response that does not have a `submitted_at` time,
we add one based on the `created_at` time. As we will now be looking
at `submitted_at` rather than `created_at` to indicate a submitted
brief response, we need to migrate all older brief responses to have
a `submitted_at` time.


from .base import Generator
from .vars import parse_vars

class Select(Generator):

    def get_arguments(self, table=None, where=None, pk='id', **kwargs):
        super(Select, self).get_arguments(**kwargs)

        self.table = table
        self.where = parse_vars(where)
        self.pk = pk

    def generate(self):
        backend = self.blueprint.backend

        while True:
            where = self.evaluate(self.where)
            values = backend.select_random(self.table, fields=(self.pk,),
                                           where=where, max_rows=10000)
            for value in values:
                if self.evaluate(self.where) != where:
                    break
                yield value

Response:
<REPLACE_OLD> Generator

class <REPLACE_NEW> Generator
from .vars import parse_vars

class <REPLACE_END> <REPLACE_OLD> where
 <REPLACE_NEW> parse_vars(where)
 <REPLACE_END> <INSERT> where = self.evaluate(self.where)
            <INSERT_END> <REPLACE_OLD> where=self.where, <REPLACE_NEW> where=where, <REPLACE_END> <INSERT> if self.evaluate(self.where) != where:
                    break
                <INSERT_END> <|endoftext|> from .base import Generator
from .vars import parse_vars

class Select(Generator):

    def get_arguments(self, table=None, where=None, pk='id', **kwargs):
        super(Select, self).get_arguments(**kwargs)

        self.table = table
        self.where = parse_vars(where)
        self.pk = pk

    def generate(self):
        backend = self.blueprint.backend

        while True:
            where = self.evaluate(self.where)
            values = backend.select_random(self.table, fields=(self.pk,),
                                           where=where, max_rows=10000)
            for value in values:
                if self.evaluate(self.where) != where:
                    break
                yield value

Prompt:
Handle where with variables in Select generator

from .base import Generator

class Select(Generator):

    def get_arguments(self, table=None, where=None, pk='id', **kwargs):
        super(Select, self).get_arguments(**kwargs)

        self.table = table
        self.where = where
        self.pk = pk

    def generate(self):
        backend = self.blueprint.backend

        while True:
            values = backend.select_random(self.table, fields=(self.pk,),
                                           where=self.where, max_rows=10000)
            for value in values:
                yield value


from setuptools import setup, find_packages
from sys import version_info

assert version_info >= (2,7)

setup(
    name='molly',
    version='2.0dev',
    packages=find_packages(exclude=['tests']),
    include_package_data=True,
    url='http://mollyproject.org/',
    author='The Molly Project',
    setup_requires=['setuptools'],
    tests_require=['unittest2', 'mock'],
    test_suite='unittest2.collector',
    install_requires=[
        'celery',
        'cssmin',
        'Flask',
        'Flask-Assets',
        'Flask-Babel',
        'Flask-Cache',
        'Flask-PyMongo',
        'Flask-Script',
        'Flask-StatsD',
        'geojson',
        'gunicorn',
        'imposm.parser',
        'phonenumbers==5.7b2',
        'python-dateutil',
        'python-memcached',
        'raven',
        'requests',
        'Shapely',
        'supervisor'
    ],
    entry_points={
        'console_scripts': [
            'mollyui = molly.command:ui_main',
            'mollyrest = molly.command:rest_main',
            'mollyd = molly.command:mollyd',
            'mollydebugd = molly.command:mollydebugd',
            'mollyctl = molly.command:mollyctl',
            'mollydebugctl = molly.command:mollydebugctl'
        ]
    }
)

Include templates, etc, with install

Include templates, etc, with install

Response:
<INSERT> include_package_data=True,
    <INSERT_END> <|endoftext|> from setuptools import setup, find_packages
from sys import version_info

assert version_info >= (2,7)

setup(
    name='molly',
    version='2.0dev',
    packages=find_packages(exclude=['tests']),
    include_package_data=True,
    url='http://mollyproject.org/',
    author='The Molly Project',
    setup_requires=['setuptools'],
    tests_require=['unittest2', 'mock'],
    test_suite='unittest2.collector',
    install_requires=[
        'celery',
        'cssmin',
        'Flask',
        'Flask-Assets',
        'Flask-Babel',
        'Flask-Cache',
        'Flask-PyMongo',
        'Flask-Script',
        'Flask-StatsD',
        'geojson',
        'gunicorn',
        'imposm.parser',
        'phonenumbers==5.7b2',
        'python-dateutil',
        'python-memcached',
        'raven',
        'requests',
        'Shapely',
        'supervisor'
    ],
    entry_points={
        'console_scripts': [
            'mollyui = molly.command:ui_main',
            'mollyrest = molly.command:rest_main',
            'mollyd = molly.command:mollyd',
            'mollydebugd = molly.command:mollydebugd',
            'mollyctl = molly.command:mollyctl',
            'mollydebugctl = molly.command:mollydebugctl'
        ]
    }
)

Prompt:
Include templates, etc, with install

from setuptools import setup, find_packages
from sys import version_info

assert version_info >= (2,7)

setup(
    name='molly',
    version='2.0dev',
    packages=find_packages(exclude=['tests']),
    url='http://mollyproject.org/',
    author='The Molly Project',
    setup_requires=['setuptools'],
    tests_require=['unittest2', 'mock'],
    test_suite='unittest2.collector',
    install_requires=[
        'celery',
        'cssmin',
        'Flask',
        'Flask-Assets',
        'Flask-Babel',
        'Flask-Cache',
        'Flask-PyMongo',
        'Flask-Script',
        'Flask-StatsD',
        'geojson',
        'gunicorn',
        'imposm.parser',
        'phonenumbers==5.7b2',
        'python-dateutil',
        'python-memcached',
        'raven',
        'requests',
        'Shapely',
        'supervisor'
    ],
    entry_points={
        'console_scripts': [
            'mollyui = molly.command:ui_main',
            'mollyrest = molly.command:rest_main',
            'mollyd = molly.command:mollyd',
            'mollydebugd = molly.command:mollydebugd',
            'mollyctl = molly.command:mollyctl',
            'mollydebugctl = molly.command:mollydebugctl'
        ]
    }
)


from flask import Flask, render_template
from redis import StrictRedis
import random
import config

app = Flask(__name__)
app.debug = config.DEBUG

db = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT)

@app.route('/')
def main():
    available_quizzes = db.smembers('quizzes')
    return render_template('main.html', quizzes=available_quizzes)

@app.route('/quiz/start/<quiz_url_name>')
def start_quiz(quiz_url_name):
    description, created_by, quiz_name = db.hmget(quiz_url_name,
            'description', 'created_by', 'quiz_name')
    return render_template('quiz-start.html', quiz_name=quiz_name,
            description=description, created_by=created_by,
            quiz_url_name=quiz_url_name)

@app.route('/quiz/<quiz_url_name>')
def quiz(quiz_url_name):
    #FIXME: naive random implementation, needs to be score-weighted
    hash_name = "{0}:questions".format(quiz_url_name)
    all_questions = db.hkeys(hash_name)
    question = random.choice(all_questions)
    answer = db.hget(hash_name, question)
    quiz_name = db.hget(quiz_url_name, 'quiz_name')

    return render_template('quiz.html', quiz_name=quiz_name,
            question=question, answer=answer,
            quiz_url_name=quiz_url_name)

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

Change key lookups and fix typos

Response:
<REPLACE_OLD> render_template('templates/main.html', quizzes=available_quizzes)

@app.route('/quiz/start/<quiz_name>')
def start_quiz(quiz_name):
 <REPLACE_NEW> render_template('main.html', quizzes=available_quizzes)

@app.route('/quiz/start/<quiz_url_name>')
def start_quiz(quiz_url_name):
    description, created_by, quiz_name = db.hmget(quiz_url_name,
            'description', 'created_by', 'quiz_name')
 <REPLACE_END> <REPLACE_OLD> render_template('templates/quiz-start.html', quiz_name=quiz_name)

@app.route('/quiz/<quiz_name>')
def quiz(quiz_name):
 <REPLACE_NEW> render_template('quiz-start.html', quiz_name=quiz_name,
            description=description, created_by=created_by,
            quiz_url_name=quiz_url_name)

@app.route('/quiz/<quiz_url_name>')
def quiz(quiz_url_name):
 <REPLACE_END> <INSERT> hash_name = "{0}:questions".format(quiz_url_name)
    <INSERT_END> <REPLACE_OLD> db.hkeys(quiz_name)
 <REPLACE_NEW> db.hkeys(hash_name)
 <REPLACE_END> <REPLACE_OLD> db.hget(quiz_name, question)

 <REPLACE_NEW> db.hget(hash_name, question)
    quiz_name = db.hget(quiz_url_name, 'quiz_name')

 <REPLACE_END> <REPLACE_OLD> render_template('templates/quiz.html', <REPLACE_NEW> render_template('quiz.html', <REPLACE_END> <REPLACE_OLD> answer=answer)
 <REPLACE_NEW> answer=answer,
            quiz_url_name=quiz_url_name)

if __name__ == '__main__':
    app.run()
 <REPLACE_END> <|endoftext|> from flask import Flask, render_template
from redis import StrictRedis
import random
import config

app = Flask(__name__)
app.debug = config.DEBUG

db = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT)

@app.route('/')
def main():
    available_quizzes = db.smembers('quizzes')
    return render_template('main.html', quizzes=available_quizzes)

@app.route('/quiz/start/<quiz_url_name>')
def start_quiz(quiz_url_name):
    description, created_by, quiz_name = db.hmget(quiz_url_name,
            'description', 'created_by', 'quiz_name')
    return render_template('quiz-start.html', quiz_name=quiz_name,
            description=description, created_by=created_by,
            quiz_url_name=quiz_url_name)

@app.route('/quiz/<quiz_url_name>')
def quiz(quiz_url_name):
    #FIXME: naive random implementation, needs to be score-weighted
    hash_name = "{0}:questions".format(quiz_url_name)
    all_questions = db.hkeys(hash_name)
    question = random.choice(all_questions)
    answer = db.hget(hash_name, question)
    quiz_name = db.hget(quiz_url_name, 'quiz_name')

    return render_template('quiz.html', quiz_name=quiz_name,
            question=question, answer=answer,
            quiz_url_name=quiz_url_name)

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

Prompt:
Change key lookups and fix typos

Revamped the way URLs were handled from Redis by differentiating the URL
friendly name from the actual name. Fixed bad paths in render_template
for all routes.

from flask import Flask, render_template
from redis import StrictRedis
import random
import config

app = Flask(__name__)
app.debug = config.DEBUG

db = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT)

@app.route('/')
def main():
    available_quizzes = db.smembers('quizzes')
    return render_template('templates/main.html', quizzes=available_quizzes)

@app.route('/quiz/start/<quiz_name>')
def start_quiz(quiz_name):
    return render_template('templates/quiz-start.html', quiz_name=quiz_name)

@app.route('/quiz/<quiz_name>')
def quiz(quiz_name):
    #FIXME: naive random implementation, needs to be score-weighted
    all_questions = db.hkeys(quiz_name)
    question = random.choice(all_questions)
    answer = db.hget(quiz_name, question)

    return render_template('templates/quiz.html', quiz_name=quiz_name,
            question=question, answer=answer)


"""Command to import PMG's constitiency office data

PMG are undertaking an exercise to visit all the constituency
offices and collect accurate locations and photographs.
The data from this is available at

https://app.m4jam.com/app/campaigns/2298/export/

This script parses the resulting CSV file and updates our data
on constituency offices to have the correct location.
"""

import csv
import re

from django.core.management.base import BaseCommand, CommandError
from django.contrib.gis.geos import Point

from pombola.core.models import (
    Organisation,
    )

# Matches the format of the locations in the CSV we get from PMG.
location_re = re.compile(
    r'SRID=4326;POINT\((-?[0-9]+\.[0-9]+) (-?[0-9]+\.[0-9]+)\)')

class Command(BaseCommand):
    args = '<path to csv file>'
    help = 'Updates constituency offices based on the supplied CSV file.'

    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError(
                "You must provide the path to the CSV file as an argument."
                )

        constituency_offices = Organisation.objects.filter(
            kind__slug='constituency-office')

        with open(args[0]) as csvfile:
            reader = csv.DictReader(csvfile)

            for row in reader:
                poi_ref = row['POI_REFERENCE']
                party, name = (
                    re.match(
                        '(.*) Constituency Office (.*)', poi_ref
                        ).groups()
                    )
                name = re.sub('\s+', ' ', name)

                qs = constituency_offices.filter(
                    name__regex=r'^{}.+: {}$'.format(party, name))

                try:
                    org = qs.get()
                except Organisation.MultipleObjectsReturned:
                    print "Skipping {} as multiple orgs returned: {}".format(
                        poi_ref,
                        repr(qs),
                        )
                    continue
                except Organisation.DoesNotExist:
                    # Fall back to searching for the name and the party in the
                    # constituency office name
                    qs = (
                        Organisation.objects
                        .filter(kind__slug='constituency-office')
                        .filter(name__contains=name)
                        .filter(name__contains=party)
                        )

                    org = qs.get()

                place = (
                    org.place_set
                    .filter(name__contains='Approximate position of')
                    .filter(kind__slug='constituency-office')
                    .get()  # There should be only one.
                    )

                lon, lat = location_re.match(row['Constituency_Pin']).groups()

                place.location = Point(float(lon), float(lat))
                place.save()

Add a script to import new constituency office locations.

PMG are conducting an exercise to go to all the constituency offices
and get accurate locations for them and photographs. The data they
collect is available as a CSV, and this new management command
processes that CSV and updates our data with the improved locations.

Response:
<REPLACE_OLD>  <REPLACE_NEW> """Command to import PMG's constitiency office data

PMG are undertaking an exercise to visit all the constituency
offices and collect accurate locations and photographs.
The data from this is available at

https://app.m4jam.com/app/campaigns/2298/export/

This script parses the resulting CSV file and updates our data
on constituency offices to have the correct location.
"""

import csv
import re

from django.core.management.base import BaseCommand, CommandError
from django.contrib.gis.geos import Point

from pombola.core.models import (
    Organisation,
    )

# Matches the format of the locations in the CSV we get from PMG.
location_re = re.compile(
    r'SRID=4326;POINT\((-?[0-9]+\.[0-9]+) (-?[0-9]+\.[0-9]+)\)')

class Command(BaseCommand):
    args = '<path to csv file>'
    help = 'Updates constituency offices based on the supplied CSV file.'

    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError(
                "You must provide the path to the CSV file as an argument."
                )

        constituency_offices = Organisation.objects.filter(
            kind__slug='constituency-office')

        with open(args[0]) as csvfile:
            reader = csv.DictReader(csvfile)

            for row in reader:
                poi_ref = row['POI_REFERENCE']
                party, name = (
                    re.match(
                        '(.*) Constituency Office (.*)', poi_ref
                        ).groups()
                    )
                name = re.sub('\s+', ' ', name)

                qs = constituency_offices.filter(
                    name__regex=r'^{}.+: {}$'.format(party, name))

                try:
                    org = qs.get()
                except Organisation.MultipleObjectsReturned:
                    print "Skipping {} as multiple orgs returned: {}".format(
                        poi_ref,
                        repr(qs),
                        )
                    continue
                except Organisation.DoesNotExist:
                    # Fall back to searching for the name and the party in the
                    # constituency office name
                    qs = (
                        Organisation.objects
                        .filter(kind__slug='constituency-office')
                        .filter(name__contains=name)
                        .filter(name__contains=party)
                        )

                    org = qs.get()

                place = (
                    org.place_set
                    .filter(name__contains='Approximate position of')
                    .filter(kind__slug='constituency-office')
                    .get()  # There should be only one.
                    )

                lon, lat = location_re.match(row['Constituency_Pin']).groups()

                place.location = Point(float(lon), float(lat))
                place.save()
 <REPLACE_END> <|endoftext|> """Command to import PMG's constitiency office data

PMG are undertaking an exercise to visit all the constituency
offices and collect accurate locations and photographs.
The data from this is available at

https://app.m4jam.com/app/campaigns/2298/export/

This script parses the resulting CSV file and updates our data
on constituency offices to have the correct location.
"""

import csv
import re

from django.core.management.base import BaseCommand, CommandError
from django.contrib.gis.geos import Point

from pombola.core.models import (
    Organisation,
    )

# Matches the format of the locations in the CSV we get from PMG.
location_re = re.compile(
    r'SRID=4326;POINT\((-?[0-9]+\.[0-9]+) (-?[0-9]+\.[0-9]+)\)')

class Command(BaseCommand):
    args = '<path to csv file>'
    help = 'Updates constituency offices based on the supplied CSV file.'

    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError(
                "You must provide the path to the CSV file as an argument."
                )

        constituency_offices = Organisation.objects.filter(
            kind__slug='constituency-office')

        with open(args[0]) as csvfile:
            reader = csv.DictReader(csvfile)

            for row in reader:
                poi_ref = row['POI_REFERENCE']
                party, name = (
                    re.match(
                        '(.*) Constituency Office (.*)', poi_ref
                        ).groups()
                    )
                name = re.sub('\s+', ' ', name)

                qs = constituency_offices.filter(
                    name__regex=r'^{}.+: {}$'.format(party, name))

                try:
                    org = qs.get()
                except Organisation.MultipleObjectsReturned:
                    print "Skipping {} as multiple orgs returned: {}".format(
                        poi_ref,
                        repr(qs),
                        )
                    continue
                except Organisation.DoesNotExist:
                    # Fall back to searching for the name and the party in the
                    # constituency office name
                    qs = (
                        Organisation.objects
                        .filter(kind__slug='constituency-office')
                        .filter(name__contains=name)
                        .filter(name__contains=party)
                        )

                    org = qs.get()

                place = (
                    org.place_set
                    .filter(name__contains='Approximate position of')
                    .filter(kind__slug='constituency-office')
                    .get()  # There should be only one.
                    )

                lon, lat = location_re.match(row['Constituency_Pin']).groups()

                place.location = Point(float(lon), float(lat))
                place.save()

Prompt:
Add a script to import new constituency office locations.

PMG are conducting an exercise to go to all the constituency offices
and get accurate locations for them and photographs. The data they
collect is available as a CSV, and this new management command
processes that CSV and updates our data with the improved locations.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Server used for tests
"""
import sys
import os
# ensure sys knows about pyqode.core in the test env
sys.path.insert(0, os.getcwd())
sys.path.insert(0, os.path.abspath(".."))
from pyqode.core import backend

if __name__ == '__main__':
    print('Server started')
    print(sys.path)
    print(os.getcwd())
    backend.CodeCompletionWorker.providers.append(
        backend.DocumentWordsProvider())
    backend.serve_forever()

Response:
<REPLACE_OLD> os.getcwd())
from <REPLACE_NEW> os.getcwd())
sys.path.insert(0, os.path.abspath(".."))
from <REPLACE_END> <|endoftext|> #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Server used for tests
"""
import sys
import os
# ensure sys knows about pyqode.core in the test env
sys.path.insert(0, os.getcwd())
sys.path.insert(0, os.path.abspath(".."))
from pyqode.core import backend

if __name__ == '__main__':
    print('Server started')
    print(sys.path)
    print(os.getcwd())
    backend.CodeCompletionWorker.providers.append(
        backend.DocumentWordsProvider())
    backend.serve_forever()

Prompt:
Fix test suite on travis (restore previous path config)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Server used for tests
"""
import sys
import os
# ensure sys knows about pyqode.core in the test env
sys.path.insert(0, os.getcwd())
from pyqode.core import backend

if __name__ == '__main__':
    print('Server started')
    print(sys.path)
    print(os.getcwd())
    backend.CodeCompletionWorker.providers.append(
        backend.DocumentWordsProvider())
    backend.serve_forever()


"""
Example of a manual_report() function that can be used either as a context manager
(with statement), or a decorator, to manually add entries to Locust's statistics.

Usage as a context manager:

    with manual_report("stats entry name"):
        # Run time of this block will be reported under a stats entry called "stats entry name"
        # do stuff here, if an Exception is raised, it'll be reported as a failure

Usage as a decorator:

    @task
    @manual_report
    def my_task(self):
       # The run time of this task will be reported under a stats entry called "my task" (type "manual").
       # If an Exception is raised, it'll be reported as a failure

"""

import random
from contextlib import contextmanager, ContextDecorator
from time import time, sleep

from locust import User, task, constant, events

@contextmanager
def _manual_report(name):
    start_time = time()
    try:
        yield
    except Exception as e:
        events.request_failure.fire(
            request_type="manual",
            name=name,
            response_time=(time() - start_time) * 1000,
            response_length=0,
            exception=e,
        )
        raise
    else:
        events.request_success.fire(
            request_type="manual",
            name=name,
            response_time=(time() - start_time) * 1000,
            response_length=0,
        )

def manual_report(name_or_func):
    if callable(name_or_func):
        # used as decorator without name argument specified
        return _manual_report(name_or_func.__name__)(name_or_func)
    else:
        return _manual_report(name_or_func)

class MyUser(User):
    wait_time = constant(1)

    @task
    def successful_task(self):
        with manual_report("successful_task"):
            sleep(random.random())

    @task
    @manual_report
    def decorator_test(self):
        if random.random() > 0.5:
            raise Exception("decorator_task failed")
        sleep(random.random())

    @task
    def failing_task(self):
        with manual_report("failing_task"):
            sleep(random.random())
            raise Exception("Oh nooes!")

Add example with a manual_report decorator/context manager that can be used to easily measure the time of functions/code blocks and manually report them as stats entries to the Locust statistics

Response:
<REPLACE_OLD>  <REPLACE_NEW> """
Example of a manual_report() function that can be used either as a context manager
(with statement), or a decorator, to manually add entries to Locust's statistics.

Usage as a context manager:

    with manual_report("stats entry name"):
        # Run time of this block will be reported under a stats entry called "stats entry name"
        # do stuff here, if an Exception is raised, it'll be reported as a failure

Usage as a decorator:

    @task
    @manual_report
    def my_task(self):
       # The run time of this task will be reported under a stats entry called "my task" (type "manual").
       # If an Exception is raised, it'll be reported as a failure

"""

import random
from contextlib import contextmanager, ContextDecorator
from time import time, sleep

from locust import User, task, constant, events

@contextmanager
def _manual_report(name):
    start_time = time()
    try:
        yield
    except Exception as e:
        events.request_failure.fire(
            request_type="manual",
            name=name,
            response_time=(time() - start_time) * 1000,
            response_length=0,
            exception=e,
        )
        raise
    else:
        events.request_success.fire(
            request_type="manual",
            name=name,
            response_time=(time() - start_time) * 1000,
            response_length=0,
        )

def manual_report(name_or_func):
    if callable(name_or_func):
        # used as decorator without name argument specified
        return _manual_report(name_or_func.__name__)(name_or_func)
    else:
        return _manual_report(name_or_func)

class MyUser(User):
    wait_time = constant(1)

    @task
    def successful_task(self):
        with manual_report("successful_task"):
            sleep(random.random())

    @task
    @manual_report
    def decorator_test(self):
        if random.random() > 0.5:
            raise Exception("decorator_task failed")
        sleep(random.random())

    @task
    def failing_task(self):
        with manual_report("failing_task"):
            sleep(random.random())
            raise Exception("Oh nooes!")
 <REPLACE_END> <|endoftext|> """
Example of a manual_report() function that can be used either as a context manager
(with statement), or a decorator, to manually add entries to Locust's statistics.

Usage as a context manager:

    with manual_report("stats entry name"):
        # Run time of this block will be reported under a stats entry called "stats entry name"
        # do stuff here, if an Exception is raised, it'll be reported as a failure

Usage as a decorator:

    @task
    @manual_report
    def my_task(self):
       # The run time of this task will be reported under a stats entry called "my task" (type "manual").
       # If an Exception is raised, it'll be reported as a failure

"""

import random
from contextlib import contextmanager, ContextDecorator
from time import time, sleep

from locust import User, task, constant, events

@contextmanager
def _manual_report(name):
    start_time = time()
    try:
        yield
    except Exception as e:
        events.request_failure.fire(
            request_type="manual",
            name=name,
            response_time=(time() - start_time) * 1000,
            response_length=0,
            exception=e,
        )
        raise
    else:
        events.request_success.fire(
            request_type="manual",
            name=name,
            response_time=(time() - start_time) * 1000,
            response_length=0,
        )

def manual_report(name_or_func):
    if callable(name_or_func):
        # used as decorator without name argument specified
        return _manual_report(name_or_func.__name__)(name_or_func)
    else:
        return _manual_report(name_or_func)

class MyUser(User):
    wait_time = constant(1)

    @task
    def successful_task(self):
        with manual_report("successful_task"):
            sleep(random.random())

    @task
    @manual_report
    def decorator_test(self):
        if random.random() > 0.5:
            raise Exception("decorator_task failed")
        sleep(random.random())

    @task
    def failing_task(self):
        with manual_report("failing_task"):
            sleep(random.random())
            raise Exception("Oh nooes!")

Prompt: Add example with a manual_report decorator/context manager that can be used to easily measure the time of functions/code blocks and manually report them as stats entries to the Locust statistics


from unittest import TestCase

from pymemcache.serde import (python_memcache_serializer,
                              python_memcache_deserializer, FLAG_PICKLE,
                              FLAG_INTEGER, FLAG_LONG, FLAG_TEXT)
import pytest
import six

@pytest.mark.unit()
class TestSerde(TestCase):

    def check(self, value, expected_flags=0):
        serialized, flags = python_memcache_serializer(b'key', value)
        assert flags == expected_flags

        # pymemcache stores values as byte strings, so we immediately the value
        # if needed so deserialized works as it would with a real server
        if not isinstance(serialized, six.binary_type):
            serialized = six.text_type(serialized).encode('ascii')

        deserialized = python_memcache_deserializer(b'key', serialized, flags)
        assert deserialized == value

    def test_bytes(self):
        self.check(b'value')

    def test_unicode(self):
        self.check(u'value', FLAG_TEXT)

    def test_int(self):
        self.check(1, FLAG_INTEGER)

    def test_long(self):
        # long only exists with Python 2, so we're just testing for another
        # integer with Python 3
        if six.PY2:
            expected_flags = FLAG_LONG
        else:
            expected_flags = FLAG_INTEGER
        self.check(123123123123123123123, expected_flags)

    def test_pickleable(self):
        self.check({'a': 'dict'}, FLAG_PICKLE)

Response:
<REPLACE_OLD> python_memcache_deserializer)
import <REPLACE_NEW> python_memcache_deserializer, FLAG_PICKLE,
                              FLAG_INTEGER, FLAG_LONG, FLAG_TEXT)
import <REPLACE_END> <REPLACE_OLD> value):
 <REPLACE_NEW> value, expected_flags=0):
 <REPLACE_END> <REPLACE_OLD> value)

 <REPLACE_NEW> value)
        assert flags == expected_flags

 <REPLACE_END> <REPLACE_OLD> self.check(u'value')

 <REPLACE_NEW> self.check(u'value', FLAG_TEXT)

 <REPLACE_END> <REPLACE_OLD> self.check(1)

 <REPLACE_NEW> self.check(1, FLAG_INTEGER)

 <REPLACE_END> <REPLACE_OLD> self.check(123123123123123123123)

 <REPLACE_NEW> # long only exists with Python 2, so we're just testing for another
        # integer with Python 3
        if six.PY2:
            expected_flags = FLAG_LONG
        else:
            expected_flags = FLAG_INTEGER
        self.check(123123123123123123123, expected_flags)

 <REPLACE_END> <REPLACE_OLD> 'dict'})
 <REPLACE_NEW> 'dict'}, FLAG_PICKLE)
 <REPLACE_END> <|endoftext|> from unittest import TestCase

from pymemcache.serde import (python_memcache_serializer,
                              python_memcache_deserializer, FLAG_PICKLE,
                              FLAG_INTEGER, FLAG_LONG, FLAG_TEXT)
import pytest
import six

@pytest.mark.unit()
class TestSerde(TestCase):

    def check(self, value, expected_flags=0):
        serialized, flags = python_memcache_serializer(b'key', value)
        assert flags == expected_flags

        # pymemcache stores values as byte strings, so we immediately the value
        # if needed so deserialized works as it would with a real server
        if not isinstance(serialized, six.binary_type):
            serialized = six.text_type(serialized).encode('ascii')

        deserialized = python_memcache_deserializer(b'key', serialized, flags)
        assert deserialized == value

    def test_bytes(self):
        self.check(b'value')

    def test_unicode(self):
        self.check(u'value', FLAG_TEXT)

    def test_int(self):
        self.check(1, FLAG_INTEGER)

    def test_long(self):
        # long only exists with Python 2, so we're just testing for another
        # integer with Python 3
        if six.PY2:
            expected_flags = FLAG_LONG
        else:
            expected_flags = FLAG_INTEGER
        self.check(123123123123123123123, expected_flags)

    def test_pickleable(self):
        self.check({'a': 'dict'}, FLAG_PICKLE)

Prompt:
Test for expected flags with serde tests

from unittest import TestCase

from pymemcache.serde import (python_memcache_serializer,
                              python_memcache_deserializer)
import pytest
import six

@pytest.mark.unit()
class TestSerde(TestCase):

    def check(self, value):
        serialized, flags = python_memcache_serializer(b'key', value)

        # pymemcache stores values as byte strings, so we immediately the value
        # if needed so deserialized works as it would with a real server
        if not isinstance(serialized, six.binary_type):
            serialized = six.text_type(serialized).encode('ascii')

        deserialized = python_memcache_deserializer(b'key', serialized, flags)
        assert deserialized == value

    def test_bytes(self):
        self.check(b'value')

    def test_unicode(self):
        self.check(u'value')

    def test_int(self):
        self.check(1)

    def test_long(self):
        self.check(123123123123123123123)

    def test_pickleable(self):
        self.check({'a': 'dict'})


import os
import sysctl
from sysctl.tests import SysctlTestBase

class TestSysctlANum(SysctlTestBase):

    def test_sysctl_setvalue(self):
        dummy = sysctl.filter('kern.dummy')[0]
        try:
            self.command("/sbin/sysctl kern.dummy=0")
        except:
            if os.getuid() == 0:
                raise

        try:
            dummy.value = 1
        except TypeError:
            if os.getuid() == 0:
                raise

        if os.getuid() == 0:
            value = int(self.command("/sbin/sysctl -n kern.dummy"))
            if value != 1:
                raise ValueError("Failed to set kern.dummy")

Add test to set a sysctl value

Add test to set a sysctl value

Response:
<REPLACE_OLD>  <REPLACE_NEW> import os
import sysctl
from sysctl.tests import SysctlTestBase

class TestSysctlANum(SysctlTestBase):

    def test_sysctl_setvalue(self):
        dummy = sysctl.filter('kern.dummy')[0]
        try:
            self.command("/sbin/sysctl kern.dummy=0")
        except:
            if os.getuid() == 0:
                raise

        try:
            dummy.value = 1
        except TypeError:
            if os.getuid() == 0:
                raise

        if os.getuid() == 0:
            value = int(self.command("/sbin/sysctl -n kern.dummy"))
            if value != 1:
                raise ValueError("Failed to set kern.dummy")
 <REPLACE_END> <|endoftext|> import os
import sysctl
from sysctl.tests import SysctlTestBase

class TestSysctlANum(SysctlTestBase):

    def test_sysctl_setvalue(self):
        dummy = sysctl.filter('kern.dummy')[0]
        try:
            self.command("/sbin/sysctl kern.dummy=0")
        except:
            if os.getuid() == 0:
                raise

        try:
            dummy.value = 1
        except TypeError:
            if os.getuid() == 0:
                raise

        if os.getuid() == 0:
            value = int(self.command("/sbin/sysctl -n kern.dummy"))
            if value != 1:
                raise ValueError("Failed to set kern.dummy")

Prompt: Add test to set a sysctl value


"""Adaptive configuration dialogs.

Attributes:
    __version__: The current version string.
"""

import os
import subprocess

def _get_version(version=None):  # overwritten by setup.py
    if version is None:
        pkg_dir = os.path.dirname(__file__)
        src_dir = os.path.abspath(os.path.join(pkg_dir, os.pardir))
        git_dir = os.path.join(src_dir, ".git")
        git_args = ("git", "--work-tree", src_dir, "--git-dir",
                    git_dir, "describe", "--tags", "--always")
        output = subprocess.check_output(git_args)
        version = output.decode("utf-8").strip()
        if version.rfind("-") >= 0:
            version = version[:version.rfind("-")]  # strip SHA1 hash
            version = version.replace("-", ".post")  # PEP 440 compatible
    return version

__version__ = _get_version()

Response:
<REPLACE_OLD> "--tags")
 <REPLACE_NEW> "--tags", "--always")
 <REPLACE_END> <|endoftext|> """Adaptive configuration dialogs.

Attributes:
    __version__: The current version string.
"""

import os
import subprocess

def _get_version(version=None):  # overwritten by setup.py
    if version is None:
        pkg_dir = os.path.dirname(__file__)
        src_dir = os.path.abspath(os.path.join(pkg_dir, os.pardir))
        git_dir = os.path.join(src_dir, ".git")
        git_args = ("git", "--work-tree", src_dir, "--git-dir",
                    git_dir, "describe", "--tags", "--always")
        output = subprocess.check_output(git_args)
        version = output.decode("utf-8").strip()
        if version.rfind("-") >= 0:
            version = version[:version.rfind("-")]  # strip SHA1 hash
            version = version.replace("-", ".post")  # PEP 440 compatible
    return version

__version__ = _get_version()

Prompt:
Use git describe --always in _get_version()

Travis CI does a truncated clone and causes 'git describe' to fail if
the version tag is not part of the truncated history.

"""Adaptive configuration dialogs.

Attributes:
    __version__: The current version string.
"""

import os
import subprocess

def _get_version(version=None):  # overwritten by setup.py
    if version is None:
        pkg_dir = os.path.dirname(__file__)
        src_dir = os.path.abspath(os.path.join(pkg_dir, os.pardir))
        git_dir = os.path.join(src_dir, ".git")
        git_args = ("git", "--work-tree", src_dir, "--git-dir",
                    git_dir, "describe", "--tags")
        output = subprocess.check_output(git_args)
        version = output.decode("utf-8").strip()
        if version.rfind("-") >= 0:
            version = version[:version.rfind("-")]  # strip SHA1 hash
            version = version.replace("-", ".post")  # PEP 440 compatible
    return version

__version__ = _get_version()


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import random

import pytest

from ..pages.home import HomePage

@pytest.mark.nondestructive
def test_change_language(base_url, selenium):
    page = HomePage(base_url, selenium).open()
    initial = page.footer.language
    # avoid selecting the same language or locales that have homepage redirects
    excluded = [initial, 'ja', 'ja-JP-mac', 'zh-TW', 'zh-CN']
    available = [l for l in page.footer.languages if l not in excluded]
    new = random.choice(available)
    page.footer.select_language(new)
    assert '/{0}/'.format(new) in selenium.current_url, 'Language is not in URL'
    assert new == page.footer.language, 'Language has not been selected'

Response:
<REPLACE_OLD> new <REPLACE_NEW> '/{0}/'.format(new) <REPLACE_END> <|endoftext|> # This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import random

import pytest

from ..pages.home import HomePage

@pytest.mark.nondestructive
def test_change_language(base_url, selenium):
    page = HomePage(base_url, selenium).open()
    initial = page.footer.language
    # avoid selecting the same language or locales that have homepage redirects
    excluded = [initial, 'ja', 'ja-JP-mac', 'zh-TW', 'zh-CN']
    available = [l for l in page.footer.languages if l not in excluded]
    new = random.choice(available)
    page.footer.select_language(new)
    assert '/{0}/'.format(new) in selenium.current_url, 'Language is not in URL'
    assert new == page.footer.language, 'Language has not been selected'

Prompt:
Fix intermittent failure in l10n language selector test

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import random

import pytest

from ..pages.home import HomePage

@pytest.mark.nondestructive
def test_change_language(base_url, selenium):
    page = HomePage(base_url, selenium).open()
    initial = page.footer.language
    # avoid selecting the same language or locales that have homepage redirects
    excluded = [initial, 'ja', 'ja-JP-mac', 'zh-TW', 'zh-CN']
    available = [l for l in page.footer.languages if l not in excluded]
    new = random.choice(available)
    page.footer.select_language(new)
    assert new in selenium.current_url, 'Language is not in URL'
    assert new == page.footer.language, 'Language has not been selected'


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

# Configuration file for ipython-notebook.

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

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

# Supply overrides for the tornado.web.Application that the IPython notebook
# uses.
c.NotebookApp.tornado_settings = {
    'template_path':['/srv/ga/', '/srv/ipython/IPython/html',
                     '/srv/ipython/IPython/html/templates']
}

Configure the IPython notebook user settings

Response:
<INSERT> #!/usr/bin/env python
# -*- coding: utf-8 -*-

# Configuration file for ipython-notebook.

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

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

# Supply overrides for the tornado.web.Application that the IPython notebook
# uses.
c.NotebookApp.tornado_settings = {
 <INSERT_END> <INSERT>   'template_path':['/srv/ga/', '/srv/ipython/IPython/html',
                     '/srv/ipython/IPython/html/templates']
}
 <INSERT_END> <|endoftext|> #!/usr/bin/env python
# -*- coding: utf-8 -*-

# Configuration file for ipython-notebook.

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

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

# Supply overrides for the tornado.web.Application that the IPython notebook
# uses.
c.NotebookApp.tornado_settings = {
    'template_path':['/srv/ga/', '/srv/ipython/IPython/html',
                     '/srv/ipython/IPython/html/templates']
}

Prompt: Configure the IPython notebook user settings


import plata

def plata_context(request):
    """
    Adds a few variables from Plata to the context if they are available:

    * ``plata.shop``: The current :class:`plata.shop.views.Shop` instance
    * ``plata.order``: The current order
    * ``plata.contact``: The current contact instance
    * ``plata.price_includes_tax``: Whether prices include tax or not
    """

    shop = plata.shop_instance()
    if not shop:
        return {}

    return {'plata': {
        'shop': shop,
        'order': shop.order_from_request(request),
        'contact': shop.contact_from_user(request.user),
        'price_includes_tax': plata.settings.PLATA_PRICE_INCLUDES_TAX,
        }}

Response:
<INSERT>    * ``plata.price_includes_tax``: Whether prices include tax or not
 <INSERT_END> <INSERT> 'price_includes_tax': plata.settings.PLATA_PRICE_INCLUDES_TAX,
        <INSERT_END> <|endoftext|> import plata

def plata_context(request):
    """
    Adds a few variables from Plata to the context if they are available:

    * ``plata.shop``: The current :class:`plata.shop.views.Shop` instance
    * ``plata.order``: The current order
    * ``plata.contact``: The current contact instance
    * ``plata.price_includes_tax``: Whether prices include tax or not
    """

    shop = plata.shop_instance()
    if not shop:
        return {}

    return {'plata': {
        'shop': shop,
        'order': shop.order_from_request(request),
        'contact': shop.contact_from_user(request.user),
        'price_includes_tax': plata.settings.PLATA_PRICE_INCLUDES_TAX,
        }}

Prompt:
Add the variable `plata.price_includes_tax` to the template context

import plata

def plata_context(request):
    """
    Adds a few variables from Plata to the context if they are available:

    * ``plata.shop``: The current :class:`plata.shop.views.Shop` instance
    * ``plata.order``: The current order
    * ``plata.contact``: The current contact instance
    """

    shop = plata.shop_instance()
    if not shop:
        return {}

    return {'plata': {
        'shop': shop,
        'order': shop.order_from_request(request),
        'contact': shop.contact_from_user(request.user),
        }}