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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

from unittest import TestCase

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

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

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

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

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

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


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

For a basic installation just type the command::

  python setup.py install

"""

from setuptools import setup

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

Add PyYAML & numpy dependency

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

For a basic installation just type the command::

  python setup.py install

"""

from setuptools import setup

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

Prompt:
Add PyYAML & numpy dependency

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

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

For a basic installation just type the command::

  python setup.py install

"""

from setuptools import setup

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


from setuptools import setup, find_packages

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

Response:
from setuptools import setup, find_packages

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

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

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

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

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


#!/usr/bin/python

import os
import optparse
import sys
import unittest

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

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

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

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

Response:
#!/usr/bin/python

import os
import optparse
import sys
import unittest

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

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

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

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

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

#!/usr/bin/python

import os
import optparse
import sys
import unittest

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

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

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

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


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

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

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

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

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

if __name__ == '__main__':
    main()

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

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

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

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

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

if __name__ == '__main__':
    main()

Prompt:
Tweak file list script to print obf names

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

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

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

if __name__ == '__main__':
    main()


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

from .network import CausalGraph, Equation

from .measure import MeasureCause, MeasureSuccess

from .payoff import PayoffMatrix

import equations

__version__ = "0.1.0"

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

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

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

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

Fix silly boiler plate copy issue.

Fix silly boiler plate copy issue.

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

from .network import CausalGraph, Equation

from .measure import MeasureCause, MeasureSuccess

from .payoff import PayoffMatrix

import equations

__version__ = "0.1.0"

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

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

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

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

Prompt:
Fix silly boiler plate copy issue.

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

from .network import CausalGraph, Equation

from .measure import MeasureCause, MeasureSuccess

from .payoff import PayoffMatrix

import equations

__version__ = "0.1.0"

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

def to_python(self, value):
  return value

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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


#!/usr/bin/env python3

from sphinx.ext.intersphinx import fetch_inventory

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

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

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

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

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

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

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

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

Response:
#!/usr/bin/env python3

from sphinx.ext.intersphinx import fetch_inventory

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

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

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

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

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

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

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

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

Prompt:
Update script to include empty user agent

#!/usr/bin/env python3

from sphinx.ext.intersphinx import fetch_inventory

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

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

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

class FakeConfig:
    intersphinx_timeout = None
    tls_verify = True

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

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

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

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


from django.conf import settings

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

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

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

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

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

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

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

Response:
from django.conf import settings

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

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

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

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

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

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

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

Prompt:
Fix USE_X_FORWARDED_FOR for proxied environments

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

from django.conf import settings

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

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

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

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

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

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


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

register = template.Library()

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

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

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

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

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

register = template.Library()

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

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

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

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

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

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

register = template.Library()

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

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

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

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


import json
from indra.statements import Activation, Agent

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

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

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

Add initial mention processing code

Add initial mention processing code

Response:
import json
from indra.statements import Activation, Agent

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

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

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

Prompt: Add initial mention processing code


import datetime
from urlparse import urljoin
import requests

# project
from checks import AgentCheck

class Veneur(AgentCheck):

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

    def check(self, instance):
        success = 0

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

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

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

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

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

Response:
import datetime
from urlparse import urljoin
import requests

# project
from checks import AgentCheck

class Veneur(AgentCheck):

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

    def check(self, instance):
        success = 0

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

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

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

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

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

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

import datetime
from urlparse import urljoin
import requests

# project
from checks import AgentCheck

class Veneur(AgentCheck):

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

    def check(self, instance):
        success = 0

        host = instance['host']

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

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

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

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


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

import html_handlers
import models

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

if __name__ == '__main__':
  main()

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

import html_handlers
import models

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

if __name__ == '__main__':
  main()

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

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

import html_handlers
import models

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

if __name__ == '__main__':
  main()


from dateutil.parser import parse

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

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

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

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

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

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

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

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

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

Add crawler for KAL's cartoon

Add crawler for KAL's cartoon

Response:
from dateutil.parser import parse

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

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

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

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

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

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

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

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

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

Prompt: Add crawler for KAL's cartoon


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

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

    _numba_parallel_test_ = False

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

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

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

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

    _numba_parallel_test_ = False

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

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

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

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

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

    _numba_parallel_test_ = False

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

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


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

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

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

from lava_results_app.models import (
    Query
)

class QueryAdmin(admin.ModelAdmin):
    save_as = True

admin.site.register(Query, QueryAdmin)

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

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

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

from lava_results_app.models import (
    Query
)

class QueryAdmin(admin.ModelAdmin):
    save_as = True

admin.site.register(Query, QueryAdmin)

Prompt:
Fix deprecation warnings - RemovedInDjango19Warning

Extend fix in review #9160 for subsequent changes.

Change-Id: I25fbe759cfd28ac683ef94b58a8da098141e8d48

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

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

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

from lava_results_app.models import (
    Query
)

class QueryAdmin(admin.ModelAdmin):
    save_as = True

admin.site.register(Query, QueryAdmin)


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

import json
import re

import lxml.etree
import lxml.html

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

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

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

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

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

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

import json
import re

import lxml.etree
import lxml.html

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

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

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

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

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

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

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

import json
import re

import lxml.etree
import lxml.html

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

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

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

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

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


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

import io
import os
from notebook.utils import to_api_path

_script_exporter = None
_html_exporter = None

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

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

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

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

c.FileContentsManager.post_save_hook = script_post_save

Add script to automatically save py file in jupyter.

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

import io
import os
from notebook.utils import to_api_path

_script_exporter = None
_html_exporter = None

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

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

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

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

c.FileContentsManager.post_save_hook = script_post_save

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


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

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

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

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

Add dummy wrapper for correlation filters

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

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

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

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

Prompt: Add dummy wrapper for correlation filters


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

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

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

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

Add collapse to star import

Add collapse to star import

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

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

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

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

Prompt:
Add collapse to star import

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

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

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

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


from __future__ import unicode_literals
from django.contrib import admin
from django.db import models
import reversion
from stagecraft.apps.datasets.models.backdrop_user import BackdropUser
from stagecraft.apps.datasets.models.data_set import DataSet

class DataSetInline(admin.StackedInline):
    model = DataSet
    fields = ('name',)
    extra = 0

class BackdropUserAdmin(reversion.VersionAdmin):
    search_fields = ['email', 'data_sets']
    list_display = ('email', 'numer_of_datasets_user_has_access_to',)
    list_per_page = 30
    filter_horizontal = ('data_sets',)

    def queryset(self, request):
        return BackdropUser.objects.annotate(
            dataset_count=models.Count('data_sets')
        )

    def numer_of_datasets_user_has_access_to(self, obj):
        return obj.dataset_count

    numer_of_datasets_user_has_access_to.admin_order_field = 'dataset_count'

admin.site.register(BackdropUser, BackdropUserAdmin)

Response:
from __future__ import unicode_literals
from django.contrib import admin
from django.db import models
import reversion
from stagecraft.apps.datasets.models.backdrop_user import BackdropUser
from stagecraft.apps.datasets.models.data_set import DataSet

class DataSetInline(admin.StackedInline):
    model = DataSet
    fields = ('name',)
    extra = 0

class BackdropUserAdmin(reversion.VersionAdmin):
    search_fields = ['email', 'data_sets']
    list_display = ('email', 'numer_of_datasets_user_has_access_to',)
    list_per_page = 30
    filter_horizontal = ('data_sets',)

    def queryset(self, request):
        return BackdropUser.objects.annotate(
            dataset_count=models.Count('data_sets')
        )

    def numer_of_datasets_user_has_access_to(self, obj):
        return obj.dataset_count

    numer_of_datasets_user_has_access_to.admin_order_field = 'dataset_count'

admin.site.register(BackdropUser, BackdropUserAdmin)

Prompt:
Add filter_horizontal to backdrop user admin

- Provides a much more usable interface to filter and add data_sets for backdrop admin users

from __future__ import unicode_literals
from django.contrib import admin
from django.db import models
import reversion
from stagecraft.apps.datasets.models.backdrop_user import BackdropUser
from stagecraft.apps.datasets.models.data_set import DataSet

class DataSetInline(admin.StackedInline):
    model = DataSet
    fields = ('name',)
    extra = 0

class BackdropUserAdmin(reversion.VersionAdmin):
    search_fields = ['email', 'data_sets']
    list_display = ('email', 'numer_of_datasets_user_has_access_to',)
    list_per_page = 30

    def queryset(self, request):
        return BackdropUser.objects.annotate(
            dataset_count=models.Count('data_sets')
        )

    def numer_of_datasets_user_has_access_to(self, obj):
        return obj.dataset_count

    numer_of_datasets_user_has_access_to.admin_order_field = 'dataset_count'

admin.site.register(BackdropUser, BackdropUserAdmin)


from django.shortcuts import render, get_object_or_404

from config.utils import get_active_event

from .models import Talk

def list_talks(request):
    event = get_active_event()
    talks = event.talks.prefetch_related(
        'applicants',
        'applicants__user',
        'skill_level',
        'sponsor',
    ).order_by('-keynote', 'title')

    return render(request, 'talks/list_talks.html', {
        "talks": talks,
    })

def view_talk(request, slug):
    event = get_active_event()
    talk = get_object_or_404(Talk, slug=slug, event=event)

    return render(request, 'talks/view_talk.html', {
        'talk': talk})

Response:
from django.shortcuts import render, get_object_or_404

from config.utils import get_active_event

from .models import Talk

def list_talks(request):
    event = get_active_event()
    talks = event.talks.prefetch_related(
        'applicants',
        'applicants__user',
        'skill_level',
        'sponsor',
    ).order_by('-keynote', 'title')

    return render(request, 'talks/list_talks.html', {
        "talks": talks,
    })

def view_talk(request, slug):
    event = get_active_event()
    talk = get_object_or_404(Talk, slug=slug, event=event)

    return render(request, 'talks/view_talk.html', {
        'talk': talk})

Prompt:
Revert "Temporarily make talks visible only to committee"

This reverts commit 57050b7025acb3de66024fe01255849a5ba5f1fc.

from django.shortcuts import render, get_object_or_404

from config.utils import get_active_event

from .models import Talk

def list_talks(request):
    event = get_active_event()
    talks = event.talks.prefetch_related(
        'applicants',
        'applicants__user',
        'skill_level',
        'sponsor',
    ).order_by('-keynote', 'title')

    # Temporary hack to let only admins & committee members see the talks
    user = request.user
    permitted = user.is_authenticated and (user.is_superuser or user.is_talk_committee_member())
    if not permitted:
        talks = event.talks.none()

    return render(request, 'talks/list_talks.html', {
        "talks": talks,
    })

def view_talk(request, slug):
    event = get_active_event()
    talk = get_object_or_404(Talk, slug=slug, event=event)

    return render(request, 'talks/view_talk.html', {
        'talk': talk})


import unittest
from neo.rawio.nixrawio import NIXRawIO
from neo.rawio.tests.common_rawio_test import BaseTestRawIO

testfname = "nixrawio-1.5.nix"

class TestNixRawIO(BaseTestRawIO, unittest.TestCase):
    rawioclass = NIXRawIO
    entities_to_test = [testfname]
    files_to_download = [testfname]

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

Change filename for NIXRawIO tests

Response:
import unittest
from neo.rawio.nixrawio import NIXRawIO
from neo.rawio.tests.common_rawio_test import BaseTestRawIO

testfname = "nixrawio-1.5.nix"

class TestNixRawIO(BaseTestRawIO, unittest.TestCase):
    rawioclass = NIXRawIO
    entities_to_test = [testfname]
    files_to_download = [testfname]

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

Prompt:
[nixio] Change filename for NIXRawIO tests

import unittest
from neo.rawio.nixrawio import NIXRawIO
from neo.rawio.tests.common_rawio_test import BaseTestRawIO

testfname = "neoraw.nix"

class TestNixRawIO(BaseTestRawIO, unittest.TestCase, ):
    rawioclass = NIXRawIO
    entities_to_test = [testfname]
    files_to_download = [testfname]

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


from models import User
from forms import LoginForm

def load_user(user_id):
    """Returns a user from the database based on their id"""
    return User.query.filter_by(id=user_id).first()

def login(request):
    """Handle a login request from a user."""
    form = LoginForm.from_json(request.form)
    if request.method == 'POST' and form.validate():
        return True

Response:
from models import User
from forms import LoginForm

def load_user(user_id):
    """Returns a user from the database based on their id"""
    return User.query.filter_by(id=user_id).first()

def login(request):
    """Handle a login request from a user."""
    form = LoginForm.from_json(request.form)
    if request.method == 'POST' and form.validate():
        return True

Prompt:
Clean up unused imports and add docstrings

from app_factory.create_app import db
from models import User
from forms import RegistrationForm, LoginForm

def load_user(user_id):
    return User.query.filter_by(id=user_id).first()

def login(request):
    form = LoginForm.from_json(request.form)
    if request.method == 'POST' and form.validate():
        return True


# Copyright (C) 2020 - Gabriel Cardoso de Faria <gabriel.cardoso@kmee.com.br>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import api, tools, SUPERUSER_ID

def post_init_hook(cr, registry):
    env = api.Environment(cr, SUPERUSER_ID, {})
    coa_generic_tmpl = env.ref(
        'l10n_br_coa_generic.l10n_br_coa_generic_template')
    if env['ir.module.module'].search_count([
        ('name', '=', 'l10n_br_account'),
        ('state', '=', 'installed'),
    ]):
        from odoo.addons.l10n_br_account.hooks import load_fiscal_taxes
        # Relate fiscal taxes to account taxes.
        load_fiscal_taxes(env, coa_generic_tmpl)

    # Load COA to Demo Company
    if not tools.config.get('without_demo'):
        user_admin = env.ref('base.user_admin')
        user_admin.company_id = env.ref(
            'l10n_br_base.empresa_lucro_presumido')
        coa_generic_tmpl.sudo(
            user=user_admin.id).try_loading_for_current_company()
        user_admin.company_id = env.ref('base.main_company')

Use admin user to create COA

Response:
# Copyright (C) 2020 - Gabriel Cardoso de Faria <gabriel.cardoso@kmee.com.br>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import api, tools, SUPERUSER_ID

def post_init_hook(cr, registry):
    env = api.Environment(cr, SUPERUSER_ID, {})
    coa_generic_tmpl = env.ref(
        'l10n_br_coa_generic.l10n_br_coa_generic_template')
    if env['ir.module.module'].search_count([
        ('name', '=', 'l10n_br_account'),
        ('state', '=', 'installed'),
    ]):
        from odoo.addons.l10n_br_account.hooks import load_fiscal_taxes
        # Relate fiscal taxes to account taxes.
        load_fiscal_taxes(env, coa_generic_tmpl)

    # Load COA to Demo Company
    if not tools.config.get('without_demo'):
        user_admin = env.ref('base.user_admin')
        user_admin.company_id = env.ref(
            'l10n_br_base.empresa_lucro_presumido')
        coa_generic_tmpl.sudo(
            user=user_admin.id).try_loading_for_current_company()
        user_admin.company_id = env.ref('base.main_company')

Prompt:
[FIX] l10n_br_coa_generic: Use admin user to create COA

# Copyright (C) 2020 - Gabriel Cardoso de Faria <gabriel.cardoso@kmee.com.br>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import api, tools, SUPERUSER_ID

def post_init_hook(cr, registry):
    env = api.Environment(cr, SUPERUSER_ID, {})
    coa_generic_tmpl = env.ref(
        'l10n_br_coa_generic.l10n_br_coa_generic_template')
    if env['ir.module.module'].search_count([
        ('name', '=', 'l10n_br_account'),
        ('state', '=', 'installed'),
    ]):
        from odoo.addons.l10n_br_account.hooks import load_fiscal_taxes
        # Relate fiscal taxes to account taxes.
        load_fiscal_taxes(env, coa_generic_tmpl)

    # Load COA to Demo Company
    if not tools.config.get('without_demo'):
        env.user.company_id = env.ref(
            'l10n_br_fiscal.empresa_lucro_presumido')
        coa_generic_tmpl.try_loading_for_current_company()


"""
Downgrade to pip < 20.2 when running on PyPy.

Unfortunately pip 20.2 (the most recent release at the time of writing) breaks
compatibility with PyPy, thereby causing Travis CI builds of py2deb to fail as
well. For details please refer to https://github.com/pypa/pip/issues/8653.
"""

import pip
import platform
import subprocess

from distutils.version import LooseVersion

if platform.python_implementation() == 'PyPy':
    installed_release = LooseVersion(pip.__version__)
    known_bad_release = LooseVersion('20.2')
    if installed_release >= known_bad_release:
        # Given that pip is broken, we can't use it to downgrade itself!
        # Fortunately setuptools provides easy_install which works fine.
        subprocess.check_call(['easy_install', 'pip < 20.2'])

Response:
"""
Downgrade to pip < 20.2 when running on PyPy.

Unfortunately pip 20.2 (the most recent release at the time of writing) breaks
compatibility with PyPy, thereby causing Travis CI builds of py2deb to fail as
well. For details please refer to https://github.com/pypa/pip/issues/8653.
"""

import pip
import platform
import subprocess

from distutils.version import LooseVersion

if platform.python_implementation() == 'PyPy':
    installed_release = LooseVersion(pip.__version__)
    known_bad_release = LooseVersion('20.2')
    if installed_release >= known_bad_release:
        # Given that pip is broken, we can't use it to downgrade itself!
        # Fortunately setuptools provides easy_install which works fine.
        subprocess.check_call(['easy_install', 'pip < 20.2'])

Prompt:
Fix flake8 "violations" in new PyPy compensation script

"""
Downgrade to pip < 20.2 when running on PyPy because pip 20.2 (the most recent
release at the time of writing) breaks compatibility with PyPy, thereby causing
Travis CI builds of py2deb to fail as well. For more details please refer to
https://github.com/pypa/pip/issues/8653.
"""

import pip
import platform
import subprocess

from distutils.version import LooseVersion

if platform.python_implementation() == 'PyPy':
    installed_release = LooseVersion(pip.__version__)
    known_bad_release = LooseVersion('20.2')
    if installed_release >= known_bad_release:
        # Given that pip is broken, we can't use it to downgrade itself!
        # Fortunately setuptools provides easy_install which works fine.
        subprocess.check_call(['easy_install', 'pip < 20.2'])


from distutils.core import setup
setup(name = "bitey",
            description="Bitcode Import Tool",
            long_description = """
Bitey allows LLVM bitcode to be directly imported into Python as
an high performance extension module without the need for writing wrappers.
""",
            license="""BSD""",
            version = "0.0",
            author = "David Beazley",
            author_email = "dave@dabeaz.com",
            maintainer = "David Beazley",
            maintainer_email = "dave@dabeaz.com",
            url = "https://github.com/dabeaz/bitey/",
            packages = ['bitey'],
            classifiers = [
              'Programming Language :: Python :: 2',
              'Programming Language :: Python :: 3',
              ]
            )

Add trove classifier for Python 3

Add trove classifier for Python 3

Response:
from distutils.core import setup
setup(name = "bitey",
            description="Bitcode Import Tool",
            long_description = """
Bitey allows LLVM bitcode to be directly imported into Python as
an high performance extension module without the need for writing wrappers.
""",
            license="""BSD""",
            version = "0.0",
            author = "David Beazley",
            author_email = "dave@dabeaz.com",
            maintainer = "David Beazley",
            maintainer_email = "dave@dabeaz.com",
            url = "https://github.com/dabeaz/bitey/",
            packages = ['bitey'],
            classifiers = [
              'Programming Language :: Python :: 2',
              'Programming Language :: Python :: 3',
              ]
            )

Prompt:
Add trove classifier for Python 3

from distutils.core import setup
setup(name = "bitey",
            description="Bitcode Import Tool",
            long_description = """
Bitey allows LLVM bitcode to be directly imported into Python as
an high performance extension module without the need for writing wrappers.
""",
            license="""BSD""",
            version = "0.0",
            author = "David Beazley",
            author_email = "dave@dabeaz.com",
            maintainer = "David Beazley",
            maintainer_email = "dave@dabeaz.com",
            url = "https://github.com/dabeaz/bitey/",
            packages = ['bitey'],
            classifiers = [
              'Programming Language :: Python :: 2',
              ]
            )


import requests
import os
from .endpoints import *

class UnauthorizedToken(Exception):
    pass

class ReviewsAPI:

    def __init__(self):
        token = os.environ.get('UDACITY_AUTH_TOKEN')
        self.headers = {'Authorization': token, 'Content-Length': '0'}

    def certifications(self):
        try:
            raw_response = requests.get(CERTIFICATIONS_URL, headers=self.headers)
            response = raw_response.json()

            raw_response.raise_for_status()

            certifications_list = [item['project_id'] for item in response if item['status'] == 'certified']
            return certifications_list
        except requests.exceptions.HTTPError:
            raise UnauthorizedToken('Maybe it\'s time to change you token!')

    def request_reviews(self, certifications_list):
        projects = self.__projects(certifications_list)
        return requests.post(SUBMISSION_REQUESTS, json=projects, headers=self.headers)

    # TODO Add support to multi language
    def __projects(self, certifications_list):
        projects_list = []
        for certification in certifications_list:
            projects_list.append({'project_id': certification, 'language': 'pt-br'})
        return {'projects': projects_list}

Response:
import requests
import os
from .endpoints import *

class UnauthorizedToken(Exception):
    pass

class ReviewsAPI:

    def __init__(self):
        token = os.environ.get('UDACITY_AUTH_TOKEN')
        self.headers = {'Authorization': token, 'Content-Length': '0'}

    def certifications(self):
        try:
            raw_response = requests.get(CERTIFICATIONS_URL, headers=self.headers)
            response = raw_response.json()

            raw_response.raise_for_status()

            certifications_list = [item['project_id'] for item in response if item['status'] == 'certified']
            return certifications_list
        except requests.exceptions.HTTPError:
            raise UnauthorizedToken('Maybe it\'s time to change you token!')

    def request_reviews(self, certifications_list):
        projects = self.__projects(certifications_list)
        return requests.post(SUBMISSION_REQUESTS, json=projects, headers=self.headers)

    # TODO Add support to multi language
    def __projects(self, certifications_list):
        projects_list = []
        for certification in certifications_list:
            projects_list.append({'project_id': certification, 'language': 'pt-br'})
        return {'projects': projects_list}

Prompt:
Raise exception if connection not succeed and customize error message

import requests
import os
from .endpoints import *

class UnauthorizedToken(Exception):
    pass

class ReviewsAPI:

    def __init__(self):
        token = os.environ.get('UDACITY_AUTH_TOKEN')
        self.headers = {'Authorization': token, 'Content-Length': '0'}

    def certifications(self):
        try:
            raw_response = requests.get(CERTIFICATIONS_URL, headers=self.headers)
            response = raw_response.json()
            certifications_list = [item['project_id'] for item in response if item['status'] == 'certified']
            return certifications_list
        except requests.exceptions.HTTPError:
            raise UnauthorizedToken

    def request_reviews(self, certifications_list):
        projects = self.__projects(certifications_list)
        return requests.post(SUBMISSION_REQUESTS, json=projects, headers=self.headers)

    # TODO Add support to multi language
    def __projects(self, certifications_list):
        projects_list = []
        for certification in certifications_list:
            projects_list.append({'project_id': certification, 'language': 'pt-br'})
        return {'projects': projects_list}


"""
Definition of the plugin.
"""
from django.apps import apps
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from fluent_contents.extensions import ContentPlugin, plugin_pool

default_blog_model = 'blog_tools.BlogPost'
icekit_blog_model = getattr(settings, 'ICEKIT_BLOG_MODEL', default_blog_model)
BLOG_MODEL = apps.get_model(*icekit_blog_model.rsplit('.', 1))

if icekit_blog_model != default_blog_model:
    @plugin_pool.register
    class BlogPostPlugin(ContentPlugin):
        model = apps.get_model(getattr(settings, 'ICEKIT_BLOG_CONTENT_ITEM', 'blog_post.BlogPostItem'))
        category = _('Blog')
        render_template = 'icekit/plugins/post/default.html'
        raw_id_fields = ['post', ]

Response:
"""
Definition of the plugin.
"""
from django.apps import apps
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from fluent_contents.extensions import ContentPlugin, plugin_pool

default_blog_model = 'blog_tools.BlogPost'
icekit_blog_model = getattr(settings, 'ICEKIT_BLOG_MODEL', default_blog_model)
BLOG_MODEL = apps.get_model(*icekit_blog_model.rsplit('.', 1))

if icekit_blog_model != default_blog_model:
    @plugin_pool.register
    class BlogPostPlugin(ContentPlugin):
        model = apps.get_model(getattr(settings, 'ICEKIT_BLOG_CONTENT_ITEM', 'blog_post.BlogPostItem'))
        category = _('Blog')
        render_template = 'icekit/plugins/post/default.html'
        raw_id_fields = ['post', ]

Prompt:
Update Blog model and content item matching

"""
Definition of the plugin.
"""
from django.apps import apps
from django.conf import settings
from django.db.models.loading import get_model
from django.utils.translation import ugettext_lazy as _
from fluent_contents.extensions import ContentPlugin, plugin_pool

default_blog_model = 'blog_tools.BlogPost'
icekit_blog_model = getattr(settings, 'ICEKIT_BLOG_MODEL', default_blog_model)
BLOG_MODEL = apps.get_model(*icekit_blog_model.rsplit('.', 1))

if icekit_blog_model != default_blog_model:
    @plugin_pool.register
    class BlogPostPlugin(ContentPlugin):
        model = get_model(getattr(settings, 'ICEKIT_BLOG_CONTENT_ITEM', 'blog_post.PostItem'))
        category = _('Blog')
        render_template = 'icekit/plugins/post/default.html'
        raw_id_fields = ['post', ]


"""Development automation."""

import nox

def _install_this_editable(session, *, extras=None):
    if extras is None:
        extras = []

    session.install("flit")
    session.run(
        "flit",
        "install",
        "-s",
        "--deps=production",
        "--extras",
        ",".join(extras),
        silent=True,
    )

@nox.session
def lint(session):
    session.install("pre-commit")
    session.run("pre-commit", "run", "--all-files", *session.posargs)

@nox.session(python=["3.6", "3.7", "3.8"])
def test(session):
    _install_this_editable(session, extras=["test"])

    default_args = ["--cov-report", "term", "--cov", "sphinx_autobuild"]
    args = session.posargs or default_args

    session.run("pytest", *args)

@nox.session
def docs(session):
    _install_this_editable(session, extras=["docs"])
    session.run("sphinx-build", "-b", "html", "docs/", "build/docs")

@nox.session(name="docs-live")
def docs_live(session):
    _install_this_editable(session, extras=["docs"])
    session.run("sphinx-autobuild", "-b", "html", "docs/", "build/docs")

Add docs-live to perform demo-runs

Add docs-live to perform demo-runs

Response:
"""Development automation."""

import nox

def _install_this_editable(session, *, extras=None):
    if extras is None:
        extras = []

    session.install("flit")
    session.run(
        "flit",
        "install",
        "-s",
        "--deps=production",
        "--extras",
        ",".join(extras),
        silent=True,
    )

@nox.session
def lint(session):
    session.install("pre-commit")
    session.run("pre-commit", "run", "--all-files", *session.posargs)

@nox.session(python=["3.6", "3.7", "3.8"])
def test(session):
    _install_this_editable(session, extras=["test"])

    default_args = ["--cov-report", "term", "--cov", "sphinx_autobuild"]
    args = session.posargs or default_args

    session.run("pytest", *args)

@nox.session
def docs(session):
    _install_this_editable(session, extras=["docs"])
    session.run("sphinx-build", "-b", "html", "docs/", "build/docs")

@nox.session(name="docs-live")
def docs_live(session):
    _install_this_editable(session, extras=["docs"])
    session.run("sphinx-autobuild", "-b", "html", "docs/", "build/docs")

Prompt:
Add docs-live to perform demo-runs

"""Development automation."""

import nox

def _install_this_editable(session, *, extras=None):
    if extras is None:
        extras = []

    session.install("flit")
    session.run(
        "flit",
        "install",
        "-s",
        "--deps=production",
        "--extras",
        ",".join(extras),
        silent=True,
    )

@nox.session
def lint(session):
    session.install("pre-commit")
    session.run("pre-commit", "run", "--all-files", *session.posargs)

@nox.session(python=["3.6", "3.7", "3.8"])
def test(session):
    _install_this_editable(session, extras=["test"])

    default_args = ["--cov-report", "term", "--cov", "sphinx_autobuild"]
    args = session.posargs or default_args

    session.run("pytest", *args)

@nox.session
def docs(session):
    _install_this_editable(session, extras=["docs"])
    session.run("sphinx-build", "-b", "html", "docs/", "build/docs")


from __future__ import print_function

import difflib
import pprint

import sys

def p(*args):
    """A helper for printing to stderr"""
    print(file=sys.stderr, *args)

def equal_arg(arg1, arg2):
    """Return True if the args are equal, False otherwise

    If the arguments aren't equal under ==, return True, otherwise
    return False and try to output to stderr a diff of the
    pretty-printed objects."""

    if arg1 == arg2:
        return True

    # This is more or less taken from assertDictEqual in
    # django/utils/unittest/case.py:
    args1_lines = pprint.pformat(arg1).splitlines()
    args2_lines = pprint.pformat(arg2).splitlines()
    diff = difflib.ndiff(args1_lines, args2_lines)

    p("Found the following differences: ====================================")
    for line in diff:
        p(line)
    p("=====================================================================")

    return False

def equal_call_args(args1, args2):
    """Return True if two sequences of arguments are equal

    Otherwise return False and output a diff of the first non-equal
    arguments to stderr."""
    if len(args1) != len(args2):
        message = "The argument lists were different lengths: {0} and {1}"
        p(message.format(len(args1), len(args2)))

    for i, arg1 in enumerate(args1):
        if not equal_arg(arg1, args2[i]):
            return False

    return True

Add equal_call_args for helpful diffs of args

This is particularly useful because mock's assert_called_with produces
really unhelpful output if the the expectation doesn't match.

Response:
from __future__ import print_function

import difflib
import pprint

import sys

def p(*args):
    """A helper for printing to stderr"""
    print(file=sys.stderr, *args)

def equal_arg(arg1, arg2):
    """Return True if the args are equal, False otherwise

    If the arguments aren't equal under ==, return True, otherwise
    return False and try to output to stderr a diff of the
    pretty-printed objects."""

    if arg1 == arg2:
        return True

    # This is more or less taken from assertDictEqual in
    # django/utils/unittest/case.py:
    args1_lines = pprint.pformat(arg1).splitlines()
    args2_lines = pprint.pformat(arg2).splitlines()
    diff = difflib.ndiff(args1_lines, args2_lines)

    p("Found the following differences: ====================================")
    for line in diff:
        p(line)
    p("=====================================================================")

    return False

def equal_call_args(args1, args2):
    """Return True if two sequences of arguments are equal

    Otherwise return False and output a diff of the first non-equal
    arguments to stderr."""
    if len(args1) != len(args2):
        message = "The argument lists were different lengths: {0} and {1}"
        p(message.format(len(args1), len(args2)))

    for i, arg1 in enumerate(args1):
        if not equal_arg(arg1, args2[i]):
            return False

    return True

Prompt:
Add equal_call_args for helpful diffs of args

This is particularly useful because mock's assert_called_with produces
really unhelpful output if the the expectation doesn't match.


import sys
sys.path.insert(1, "../../../")
import h2o

def cupMediumGBM(ip,port):
  # Connect to h2o
  h2o.init(ip,port)

  train = h2o.import_frame(path=h2o.locate("bigdata/laptop/usecases/cup98LRN_z.csv"))
  test = h2o.import_frame(path=h2o.locate("bigdata/laptop/usecases/cup98VAL_z.csv"))

  train["TARGET_B"] = train["TARGET_B"].asfactor()

  # Train H2O GBM Model:
  train_cols = train.names()
  for c in ['C1', "TARGET_D", "TARGET_B", "CONTROLN"]:
    train_cols.remove(c)
  model = h2o.gbm(x=train[train_cols], y=train["TARGET_B"], distribution = "bernoulli", ntrees = 5)

if __name__ == "__main__":
  h2o.run_test(sys.argv, cupMediumGBM)

Response:
import sys
sys.path.insert(1, "../../../")
import h2o

def cupMediumGBM(ip,port):
  # Connect to h2o
  h2o.init(ip,port)

  train = h2o.import_frame(path=h2o.locate("bigdata/laptop/usecases/cup98LRN_z.csv"))
  test = h2o.import_frame(path=h2o.locate("bigdata/laptop/usecases/cup98VAL_z.csv"))

  train["TARGET_B"] = train["TARGET_B"].asfactor()

  # Train H2O GBM Model:
  train_cols = train.names()
  for c in ['C1', "TARGET_D", "TARGET_B", "CONTROLN"]:
    train_cols.remove(c)
  model = h2o.gbm(x=train[train_cols], y=train["TARGET_B"], distribution = "bernoulli", ntrees = 5)

if __name__ == "__main__":
  h2o.run_test(sys.argv, cupMediumGBM)

Prompt:
Update test for new column naming rules.  Remove '' as a name and call column 'C1'

import sys
sys.path.insert(1, "../../../")
import h2o

def cupMediumGBM(ip,port):
  # Connect to h2o
  h2o.init(ip,port)

  train = h2o.import_frame(path=h2o.locate("bigdata/laptop/usecases/cup98LRN_z.csv"))
  test = h2o.import_frame(path=h2o.locate("bigdata/laptop/usecases/cup98VAL_z.csv"))

  train["TARGET_B"] = train["TARGET_B"].asfactor()

  # Train H2O GBM Model:
  train_cols = train.names()
  for c in ['', "TARGET_D", "TARGET_B", "CONTROLN"]:
    train_cols.remove(c)
  model = h2o.gbm(x=train[train_cols], y=train["TARGET_B"], distribution = "bernoulli", ntrees = 5)

if __name__ == "__main__":
  h2o.run_test(sys.argv, cupMediumGBM)


import numpy as np
from ..centroid_resid import get_obs_slot_residuals

def test_multi_ai():
    # obsid 15175 has two aspect intervals
    dyags, yt, dzags, zt = get_obs_slot_residuals(15175, 4)
    assert np.all(np.abs(dyags) < 3)
    assert np.all(np.abs(dzags) < 6)

def test_obc_centroids():
    dyags, yt, dzags, zt = get_obs_slot_residuals(15175, 5, centroid_source='obc')
    assert np.all(np.abs(dyags) < 6)
    assert np.all(np.abs(dzags) < 3)

def test_obc():
    dyags, yt, dzags, zt = get_obs_slot_residuals(15175, 6, att_source='obc', centroid_source='obc')
    assert np.all(np.abs(dyags) < 4.5)
    assert np.all(np.abs(dzags) < 5.5)

Response:
import numpy as np
from ..centroid_resid import get_obs_slot_residuals

def test_multi_ai():
    # obsid 15175 has two aspect intervals
    dyags, yt, dzags, zt = get_obs_slot_residuals(15175, 4)
    assert np.all(np.abs(dyags) < 3)
    assert np.all(np.abs(dzags) < 6)

def test_obc_centroids():
    dyags, yt, dzags, zt = get_obs_slot_residuals(15175, 5, centroid_source='obc')
    assert np.all(np.abs(dyags) < 6)
    assert np.all(np.abs(dzags) < 3)

def test_obc():
    dyags, yt, dzags, zt = get_obs_slot_residuals(15175, 6, att_source='obc', centroid_source='obc')
    assert np.all(np.abs(dyags) < 4.5)
    assert np.all(np.abs(dzags) < 5.5)

Prompt:
Update tests using helper method for new API

import numpy as np
from ..centroid_resid import get_obs_slot_residuals

def test_multi_ai():
    # obsid 15175 has two aspect intervals
    dyags, dzags = get_obs_slot_residuals(15175, 4)
    assert np.all(np.abs(dyags) < 3)
    assert np.all(np.abs(dzags) < 6)

def test_obc_centroids():
    dyags, dzags = get_obs_slot_residuals(15175, 5, centroid_source='obc')
    assert np.all(np.abs(dyags) < 6)
    assert np.all(np.abs(dzags) < 3)

def test_obc():
    dyags, dzags = get_obs_slot_residuals(15175, 6, att_source='obc', centroid_source='obc')
    assert np.all(np.abs(dyags) < 4.5)
    assert np.all(np.abs(dzags) < 5.5)


from django.conf import settings
import os
import os.path
import time
import re
from celery.task import task

@task
def cleanup_temp_tif_files(age=7200):
    temp_dir = settings.DATASET_DOWNLOAD_DIR
    cutoff = time.time() - age
    t_files = os.listdir(temp_dir)
    for t_file in t_files:
        if re.search('.zip$', t_file):
            path = os.path.join(temp_dir, t_file)
            if os.path.getctime(path) < cutoff:
                try:
                    os.remove(path)
                except OSError:
                    pass

Update GeoTIFF cleanup to cleanup .zip

Update GeoTIFF cleanup to cleanup .zip

Response:
from django.conf import settings
import os
import os.path
import time
import re
from celery.task import task

@task
def cleanup_temp_tif_files(age=7200):
    temp_dir = settings.DATASET_DOWNLOAD_DIR
    cutoff = time.time() - age
    t_files = os.listdir(temp_dir)
    for t_file in t_files:
        if re.search('.zip$', t_file):
            path = os.path.join(temp_dir, t_file)
            if os.path.getctime(path) < cutoff:
                try:
                    os.remove(path)
                except OSError:
                    pass

Prompt:
Update GeoTIFF cleanup to cleanup .zip

from django.conf import settings
import os
import os.path
import time
import re
from celery.task import task

@task
def cleanup_temp_tif_files(age=7200):
    temp_dir = settings.DATASET_DOWNLOAD_DIR
    cutoff = time.time() - age
    t_files = os.listdir(temp_dir)
    for t_file in t_files:
        if re.search('.tif$', t_file):
            path = os.path.join(temp_dir, t_file)
            if os.path.getctime(path) < cutoff:
                try:
                    os.remove(path)
                except OSError:
                    pass


import os, socket, sys, struct, getopt

def sendfile(filename, ip):
	statinfo = os.stat(filename)
	fbiinfo = struct.pack('!q', statinfo.st_size)
	with open(filename, 'rb') as f:
		sock = socket.socket()
		sock.connect((ip, 5000))
		sock.send(fbiinfo)
		while True:
			chunk = f.read(16384)
			if not chunk:
				break  # EOF
			sock.sendall(chunk)
		sock.close()

def show_usage_exit():
	print('sendfile.py -f <inputfile> -i <ip address>')
	sys.exit(2)

def main(argv):
	filename = None
	ip = None
	try:
		opts, args = getopt.getopt(argv, "hf:i:")
	except getopt.GetoptError:
		show_usage_exit()
	for opt, arg in opts:
		if opt == '-h':
			show_usage_exit()
		elif opt in ("-f", "--file"):
			filename = arg
		elif opt in ("-i", "--ip"):
			ip = arg
	if not (filename and ip):
		show_usage_exit()
	sendfile(filename, ip)

if __name__ == "__main__":
	main(sys.argv[1:])

Add script for network CIA installation using FBI

Response:
import os, socket, sys, struct, getopt

def sendfile(filename, ip):
	statinfo = os.stat(filename)
	fbiinfo = struct.pack('!q', statinfo.st_size)
	with open(filename, 'rb') as f:
		sock = socket.socket()
		sock.connect((ip, 5000))
		sock.send(fbiinfo)
		while True:
			chunk = f.read(16384)
			if not chunk:
				break  # EOF
			sock.sendall(chunk)
		sock.close()

def show_usage_exit():
	print('sendfile.py -f <inputfile> -i <ip address>')
	sys.exit(2)

def main(argv):
	filename = None
	ip = None
	try:
		opts, args = getopt.getopt(argv, "hf:i:")
	except getopt.GetoptError:
		show_usage_exit()
	for opt, arg in opts:
		if opt == '-h':
			show_usage_exit()
		elif opt in ("-f", "--file"):
			filename = arg
		elif opt in ("-i", "--ip"):
			ip = arg
	if not (filename and ip):
		show_usage_exit()
	sendfile(filename, ip)

if __name__ == "__main__":
	main(sys.argv[1:])

Prompt: Add script for network CIA installation using FBI


import staticconf

CLUSTERMAN_YAML_FILE_PATH = '/nail/srv/configs/clusterman.yaml'
CLUSTERMAN_METRICS_YAML_FILE_PATH = '/nail/srv/configs/clusterman_metrics.yaml'

def get_clusterman_metrics():
    try:
        import clusterman_metrics
        clusterman_yaml = CLUSTERMAN_YAML_FILE_PATH
        staticconf.YamlConfiguration(CLUSTERMAN_METRICS_YAML_FILE_PATH, namespace='clusterman_metrics')
    except (ImportError, FileNotFoundError):
        # our cluster autoscaler is not currently open source, sorry!
        clusterman_metrics = None
        clusterman_yaml = None

    return clusterman_metrics, clusterman_yaml

Fix regression in manpages build

Fix regression in manpages build

Response:
import staticconf

CLUSTERMAN_YAML_FILE_PATH = '/nail/srv/configs/clusterman.yaml'
CLUSTERMAN_METRICS_YAML_FILE_PATH = '/nail/srv/configs/clusterman_metrics.yaml'

def get_clusterman_metrics():
    try:
        import clusterman_metrics
        clusterman_yaml = CLUSTERMAN_YAML_FILE_PATH
        staticconf.YamlConfiguration(CLUSTERMAN_METRICS_YAML_FILE_PATH, namespace='clusterman_metrics')
    except (ImportError, FileNotFoundError):
        # our cluster autoscaler is not currently open source, sorry!
        clusterman_metrics = None
        clusterman_yaml = None

    return clusterman_metrics, clusterman_yaml

Prompt:
Fix regression in manpages build

import staticconf

CLUSTERMAN_YAML_FILE_PATH = '/nail/srv/configs/clusterman.yaml'
CLUSTERMAN_METRICS_YAML_FILE_PATH = '/nail/srv/configs/clusterman_metrics.yaml'

def get_clusterman_metrics():
    try:
        import clusterman_metrics
        clusterman_yaml = CLUSTERMAN_YAML_FILE_PATH
        staticconf.YamlConfiguration(CLUSTERMAN_METRICS_YAML_FILE_PATH, namespace='clusterman_metrics')
    except ImportError:
        # our cluster autoscaler is not currently open source, sorry!
        clusterman_metrics = None
        clusterman_yaml = None

    return clusterman_metrics, clusterman_yaml