# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""

from __future__ import absolute_import

class NetworkInterfaceNotFoundError(Exception):
    """
    Exception raised when network interface not found.
    """

class ModuleNotFoundError(Exception):
    """
    Exception raised when mandatory kernel module not found.
    """

class TcCommandExecutionError(Exception):
    """
    Exception raised when failed to execute a ``tc`` command.
    """

class TcAlreadyExist(TcCommandExecutionError):
    """
    Exception raised when a traffic shaping rule already exist.
    """

class EmptyParameterError(ValueError):
    """
    Exception raised when a parameter value is empty value.
    """

class InvalidParameterError(ValueError):
    """
    Exception raised when an invalid parameter specified for
    a traffic shaping rule.
    """

    def __init__(self, *args, **kwargs):
        self.__value = kwargs.pop("value", None)
        self.__expected = kwargs.pop("expected", None)

        super(ValueError, self).__init__(*args)

    def __str__(self, *args, **kwargs):
        item_list = [ValueError.__str__(self, *args, **kwargs)]
        extra_item_list = []

        if self.__expected:
            extra_item_list.append("expected={}".format(self.__expected))

        if self.__value:
            extra_item_list.append("value={}".format(self.__value))

        if extra_item_list:
            item_list.extend([":", ", ".join(extra_item_list)])

        return " ".join(item_list)

    def __repr__(self, *args, **kwargs):
        return self.__str__(*args, **kwargs)

class UnitNotFoundError(InvalidParameterError):
    """
    """

Response:
# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""

from __future__ import absolute_import

class NetworkInterfaceNotFoundError(Exception):
    """
    Exception raised when network interface not found.
    """

class ModuleNotFoundError(Exception):
    """
    Exception raised when mandatory kernel module not found.
    """

class TcCommandExecutionError(Exception):
    """
    Exception raised when failed to execute a ``tc`` command.
    """

class TcAlreadyExist(TcCommandExecutionError):
    """
    Exception raised when a traffic shaping rule already exist.
    """

class EmptyParameterError(ValueError):
    """
    Exception raised when a parameter value is empty value.
    """

class InvalidParameterError(ValueError):
    """
    Exception raised when an invalid parameter specified for
    a traffic shaping rule.
    """

    def __init__(self, *args, **kwargs):
        self.__value = kwargs.pop("value", None)
        self.__expected = kwargs.pop("expected", None)

        super(ValueError, self).__init__(*args)

    def __str__(self, *args, **kwargs):
        item_list = [ValueError.__str__(self, *args, **kwargs)]
        extra_item_list = []

        if self.__expected:
            extra_item_list.append("expected={}".format(self.__expected))

        if self.__value:
            extra_item_list.append("value={}".format(self.__value))

        if extra_item_list:
            item_list.extend([":", ", ".join(extra_item_list)])

        return " ".join(item_list)

    def __repr__(self, *args, **kwargs):
        return self.__str__(*args, **kwargs)

class UnitNotFoundError(InvalidParameterError):
    """
    """

Prompt:
Add custom arguments for InvalidParameterError

# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""

from __future__ import absolute_import

class NetworkInterfaceNotFoundError(Exception):
    """
    Exception raised when network interface not found.
    """

class ModuleNotFoundError(Exception):
    """
    Exception raised when mandatory kernel module not found.
    """

class TcCommandExecutionError(Exception):
    """
    Exception raised when failed to execute a ``tc`` command.
    """

class TcAlreadyExist(TcCommandExecutionError):
    """
    Exception raised when a traffic shaping rule already exist.
    """

class EmptyParameterError(ValueError):
    """
    Exception raised when a parameter value is empty value.
    """

class InvalidParameterError(ValueError):
    """
    Exception raised when an invalid parameter specified for
    a traffic shaping rule.
    """

class UnitNotFoundError(InvalidParameterError):
    """
    """


import unittest2

from cfn_sphere.stack_configuration import Config, StackConfig, NoConfigException

class ConfigTests(unittest2.TestCase):
    def test_properties_parsing(self):
        config = Config(config_dict={'region': 'eu-west-1', 'stacks': {'any-stack': {'template-url': 'foo.json', 'tags': {'any-tag': 'any-tag-value'}, 'parameters': {'any-parameter': 'any-value'}}}})
        self.assertEqual('eu-west-1', config.region)
        self.assertEqual(1, len(config.stacks.keys()))
        self.assertTrue(isinstance(config.stacks['any-stack'], StackConfig))
        self.assertEqual('foo.json', config.stacks['any-stack'].template_url)
        self.assertEqual({'any-tag': 'any-tag-value'}, config.stacks['any-stack'].tags)
        self.assertEqual({'any-parameter': 'any-value'}, config.stacks['any-stack'].parameters)

    def test_raises_exception_if_no_region_key(self):
        with self.assertRaises(NoConfigException):
            Config(config_dict={'foo': '', 'stacks': {'any-stack': {'template': 'foo.json'}}})

    def test_raises_exception_if_no_stacks_key(self):
        with self.assertRaises(NoConfigException):
            Config(config_dict={'region': 'eu-west-1'})

Make test variables more descriptive

Response:
import unittest2

from cfn_sphere.stack_configuration import Config, StackConfig, NoConfigException

class ConfigTests(unittest2.TestCase):
    def test_properties_parsing(self):
        config = Config(config_dict={'region': 'eu-west-1', 'stacks': {'any-stack': {'template-url': 'foo.json', 'tags': {'any-tag': 'any-tag-value'}, 'parameters': {'any-parameter': 'any-value'}}}})
        self.assertEqual('eu-west-1', config.region)
        self.assertEqual(1, len(config.stacks.keys()))
        self.assertTrue(isinstance(config.stacks['any-stack'], StackConfig))
        self.assertEqual('foo.json', config.stacks['any-stack'].template_url)
        self.assertEqual({'any-tag': 'any-tag-value'}, config.stacks['any-stack'].tags)
        self.assertEqual({'any-parameter': 'any-value'}, config.stacks['any-stack'].parameters)

    def test_raises_exception_if_no_region_key(self):
        with self.assertRaises(NoConfigException):
            Config(config_dict={'foo': '', 'stacks': {'any-stack': {'template': 'foo.json'}}})

    def test_raises_exception_if_no_stacks_key(self):
        with self.assertRaises(NoConfigException):
            Config(config_dict={'region': 'eu-west-1'})

Prompt:
refactor: Make test variables more descriptive

import unittest2

from cfn_sphere.stack_configuration import Config, StackConfig, NoConfigException

class ConfigTests(unittest2.TestCase):
    def test_properties_parsing(self):
        config = Config(config_dict={'region': 'eu-west-1', 'stacks': {'foo': {'template-url': 'foo.json'}}})
        self.assertEqual('eu-west-1', config.region)
        self.assertEqual(1, len(config.stacks.keys()))
        self.assertTrue(isinstance(config.stacks['foo'], StackConfig))
        self.assertEqual('foo.json', config.stacks['foo'].template_url)

    def test_raises_exception_if_no_region_key(self):
        with self.assertRaises(NoConfigException):
            Config(config_dict={'foo': '', 'stacks': {'foo': {'template': 'foo.json'}}})

    def test_raises_exception_if_no_stacks_key(self):
        with self.assertRaises(NoConfigException):
            Config(config_dict={'region': 'eu-west-1'})


"""
Copyright (c) 2015 Red Hat, Inc
All rights reserved.

This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.

constants
"""

import logging

def set_logging(name="dock", level=logging.DEBUG, handler=None):
    # create logger
    logger = logging.getLogger(name)
    logger.handlers = []
    logger.setLevel(level)

    if not handler:
        # create console handler and set level to debug
        handler = logging.StreamHandler()
        handler.setLevel(logging.DEBUG)

        # create formatter
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

        # add formatter to ch
        handler.setFormatter(formatter)

    # add ch to logger
    logger.addHandler(handler)

set_logging(level=logging.WARNING)  # override this however you want

Response:
"""
Copyright (c) 2015 Red Hat, Inc
All rights reserved.

This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.

constants
"""

import logging

def set_logging(name="dock", level=logging.DEBUG, handler=None):
    # create logger
    logger = logging.getLogger(name)
    logger.handlers = []
    logger.setLevel(level)

    if not handler:
        # create console handler and set level to debug
        handler = logging.StreamHandler()
        handler.setLevel(logging.DEBUG)

        # create formatter
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

        # add formatter to ch
        handler.setFormatter(formatter)

    # add ch to logger
    logger.addHandler(handler)

set_logging(level=logging.WARNING)  # override this however you want

Prompt:
Allow redirecting dock's logs to a given handler only

"""
Copyright (c) 2015 Red Hat, Inc
All rights reserved.

This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.

constants
"""

import logging

def set_logging(name="dock", level=logging.DEBUG):
    # create logger
    logger = logging.getLogger(name)
    logger.handlers = []
    logger.setLevel(level)

    # create console handler and set level to debug
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)

    # create formatter
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    # add formatter to ch
    ch.setFormatter(formatter)

    # add ch to logger
    logger.addHandler(ch)

set_logging(level=logging.WARNING)  # override this however you want


import zephyr.util
from zephyr.collector import MeasurementCollector
from zephyr.bioharness import BioHarnessSignalAnalysis, BioHarnessPacketHandler
from zephyr.message import MessagePayloadParser
from zephyr.testing import visualize_measurements, test_data_dir, VirtualSerial
from zephyr.protocol import Protocol, MessageFrameParser

def main():
    zephyr.util.DISABLE_CLOCK_DIFFERENCE_ESTIMATION = True

    collector = MeasurementCollector()
    rr_signal_analysis = BioHarnessSignalAnalysis([], [collector.handle_event])
    signal_packet_handlers = [collector.handle_signal, rr_signal_analysis.handle_signal]

    signal_packet_handler = BioHarnessPacketHandler(signal_packet_handlers, [collector.handle_event])

    payload_parser = MessagePayloadParser([signal_packet_handler.handle_packet])

    message_parser = MessageFrameParser(payload_parser.handle_message)

    ser = VirtualSerial(test_data_dir + "/120-second-bt-stream.dat")

    protocol = Protocol(ser, [message_parser.parse_data])

    try:
        protocol.run()
    except EOFError:
        pass

    visualize_measurements(collector)

if __name__ == "__main__":
    main()

Response:
import zephyr.util
from zephyr.collector import MeasurementCollector
from zephyr.bioharness import BioHarnessSignalAnalysis, BioHarnessPacketHandler
from zephyr.message import MessagePayloadParser
from zephyr.testing import visualize_measurements, test_data_dir, VirtualSerial
from zephyr.protocol import Protocol, MessageFrameParser

def main():
    zephyr.util.DISABLE_CLOCK_DIFFERENCE_ESTIMATION = True

    collector = MeasurementCollector()
    rr_signal_analysis = BioHarnessSignalAnalysis([], [collector.handle_event])
    signal_packet_handlers = [collector.handle_signal, rr_signal_analysis.handle_signal]

    signal_packet_handler = BioHarnessPacketHandler(signal_packet_handlers, [collector.handle_event])

    payload_parser = MessagePayloadParser([signal_packet_handler.handle_packet])

    message_parser = MessageFrameParser(payload_parser.handle_message)

    ser = VirtualSerial(test_data_dir + "/120-second-bt-stream.dat")

    protocol = Protocol(ser, [message_parser.parse_data])

    try:
        protocol.run()
    except EOFError:
        pass

    visualize_measurements(collector)

if __name__ == "__main__":
    main()

Prompt:
Fix test data plotting to use the changed interfaces

import zephyr.util
from zephyr.collector import MeasurementCollector
from zephyr.bioharness import BioHarnessSignalAnalysis, BioHarnessPacketHandler
from zephyr.message import MessagePayloadParser
from zephyr.testing import visualize_measurements, test_data_dir, VirtualSerial
from zephyr.protocol import Protocol

def main():
    zephyr.util.DISABLE_CLOCK_DIFFERENCE_ESTIMATION = True

    collector = MeasurementCollector()
    rr_signal_analysis = BioHarnessSignalAnalysis([], [collector.handle_event])
    signal_packet_handlers = [collector.handle_signal, rr_signal_analysis.handle_signal]

    signal_packet_handler = BioHarnessPacketHandler(signal_packet_handlers, [collector.handle_event])

    payload_parser = MessagePayloadParser([signal_packet_handler.handle_packet])

    ser = VirtualSerial(test_data_dir + "/120-second-bt-stream.dat")

    protocol = Protocol(ser, payload_parser.handle_message)

    try:
        protocol.run()
    except EOFError:
        pass

    visualize_measurements(collector)

if __name__ == "__main__":
    main()


import guzzle_sphinx_theme

project = "argcomplete"
copyright = "Andrey Kislyuk and argcomplete contributors"
author = "Andrey Kislyuk"
version = ""
release = ""
language = None
master_doc = "index"
extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"]
source_suffix = [".rst", ".md"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
pygments_style = "sphinx"
html_theme_path = guzzle_sphinx_theme.html_theme_path()
html_theme = "guzzle_sphinx_theme"
html_theme_options = {
    "project_nav_name": project,
    "projectlink": "https://github.com/kislyuk/" + project,
}
html_sidebars = {
    "**": [
        "logo-text.html",
        # "globaltoc.html",
        "localtoc.html",
        "searchbox.html"
    ]
}

Adjust copyright line in docs

Adjust copyright line in docs

Response:
import guzzle_sphinx_theme

project = "argcomplete"
copyright = "Andrey Kislyuk and argcomplete contributors"
author = "Andrey Kislyuk"
version = ""
release = ""
language = None
master_doc = "index"
extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"]
source_suffix = [".rst", ".md"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
pygments_style = "sphinx"
html_theme_path = guzzle_sphinx_theme.html_theme_path()
html_theme = "guzzle_sphinx_theme"
html_theme_options = {
    "project_nav_name": project,
    "projectlink": "https://github.com/kislyuk/" + project,
}
html_sidebars = {
    "**": [
        "logo-text.html",
        # "globaltoc.html",
        "localtoc.html",
        "searchbox.html"
    ]
}

Prompt:
Adjust copyright line in docs

import guzzle_sphinx_theme

project = "argcomplete"
copyright = "Andrey Kislyuk"
author = "Andrey Kislyuk"
version = ""
release = ""
language = None
master_doc = "index"
extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"]
source_suffix = [".rst", ".md"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
pygments_style = "sphinx"
html_theme_path = guzzle_sphinx_theme.html_theme_path()
html_theme = "guzzle_sphinx_theme"
html_theme_options = {
    "project_nav_name": project,
    "projectlink": "https://github.com/kislyuk/" + project,
}
html_sidebars = {
    "**": [
        "logo-text.html",
        # "globaltoc.html",
        "localtoc.html",
        "searchbox.html"
    ]
}


import functools

from django.contrib.auth import REDIRECT_FIELD_NAME

from account.utils import handle_redirect_to_login

def login_required(func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
    """
    Decorator for views that checks that the user is logged in, redirecting
    to the log in page if necessary.
    """
    def decorator(view_func):
        @functools.wraps(view_func, assigned=functools.WRAPPER_ASSIGNMENTS)
        def _wrapped_view(request, *args, **kwargs):
            if request.user.is_authenticated:
                return view_func(request, *args, **kwargs)
            return handle_redirect_to_login(
                request,
                redirect_field_name=redirect_field_name,
                login_url=login_url
            )
        return _wrapped_view
    if func:
        return decorator(func)
    return decorator

Fix a 3.0 compat issue

Fix a 3.0 compat issue

Response:
import functools

from django.contrib.auth import REDIRECT_FIELD_NAME

from account.utils import handle_redirect_to_login

def login_required(func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
    """
    Decorator for views that checks that the user is logged in, redirecting
    to the log in page if necessary.
    """
    def decorator(view_func):
        @functools.wraps(view_func, assigned=functools.WRAPPER_ASSIGNMENTS)
        def _wrapped_view(request, *args, **kwargs):
            if request.user.is_authenticated:
                return view_func(request, *args, **kwargs)
            return handle_redirect_to_login(
                request,
                redirect_field_name=redirect_field_name,
                login_url=login_url
            )
        return _wrapped_view
    if func:
        return decorator(func)
    return decorator

Prompt:
Fix a 3.0 compat issue

import functools

from django.contrib.auth import REDIRECT_FIELD_NAME
from django.utils.decorators import available_attrs

from account.utils import handle_redirect_to_login

def login_required(func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
    """
    Decorator for views that checks that the user is logged in, redirecting
    to the log in page if necessary.
    """
    def decorator(view_func):
        @functools.wraps(view_func, assigned=available_attrs(view_func))
        def _wrapped_view(request, *args, **kwargs):
            if request.user.is_authenticated:
                return view_func(request, *args, **kwargs)
            return handle_redirect_to_login(
                request,
                redirect_field_name=redirect_field_name,
                login_url=login_url
            )
        return _wrapped_view
    if func:
        return decorator(func)
    return decorator


from django_evolution.db import evolver

def write_sql(sql):
    "Output a list of SQL statements, unrolling parameters as required"
    for statement in sql:
        if isinstance(statement, tuple):
            print unicode(statement[0] % tuple(evolver.quote_sql_param(s) for s in statement[1]))
        else:
            print unicode(statement)

def execute_sql(cursor, sql):
    """
    Execute a list of SQL statements on the provided cursor, unrolling
    parameters as required
    """
    for statement in sql:
        if isinstance(statement, tuple):
            if not statement[0].startswith('--'):
                cursor.execute(*statement)
        else:
            if not statement.startswith('--'):
                cursor.execute(statement)

Response:
from django_evolution.db import evolver

def write_sql(sql):
    "Output a list of SQL statements, unrolling parameters as required"
    for statement in sql:
        if isinstance(statement, tuple):
            print unicode(statement[0] % tuple(evolver.quote_sql_param(s) for s in statement[1]))
        else:
            print unicode(statement)

def execute_sql(cursor, sql):
    """
    Execute a list of SQL statements on the provided cursor, unrolling
    parameters as required
    """
    for statement in sql:
        if isinstance(statement, tuple):
            if not statement[0].startswith('--'):
                cursor.execute(*statement)
        else:
            if not statement.startswith('--'):
                cursor.execute(statement)

Prompt:
Revert a debugging change that slipped in.

git-svn-id: 48f3d5eb0141859d8d7d81547b6bd7b3dde885f8@186 8655a95f-0638-0410-abc2-2f1ed958ef3d

from django_evolution.db import evolver

def write_sql(sql):
    "Output a list of SQL statements, unrolling parameters as required"
    for statement in sql:
        if isinstance(statement, tuple):
            print unicode(statement[0] % tuple(evolver.quote_sql_param(s) for s in statement[1]))
        else:
            print unicode(statement)

def execute_sql(cursor, sql):
    """
    Execute a list of SQL statements on the provided cursor, unrolling
    parameters as required
    """
    for statement in sql:
        if isinstance(statement, tuple):
            if not statement[0].startswith('--'):
                cursor.execute(*statement)
        else:
            if not statement.startswith('--'):
                try:
                    cursor.execute(statement)
                except:
                    print statement
                    print sql
                    raise Exception(statement)


#!/usr/bin/env python3
import os
from setuptools import setup, find_packages

def get_readme():
    return open(os.path.join(os.path.dirname(__file__), 'README.rst')).read()

setup(
    author="Julio Gonzalez Altamirano",
    author_email='devjga@gmail.com',
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 3 :: Only',
    ],
    description="ETL for CapMetro raw data.",
    entry_points={
        'console_scripts': [
            'capmetrics=capmetrics_etl.cli:run',
            'capmetrics-tables=capmetrics_etl.cli.tables'
        ],
    },
    install_requires=['click', 'pytz', 'sqlalchemy', 'xlrd'],
    keywords="python etl transit",
    license="MIT",
    long_description=get_readme(),
    name='capmetrics-etl',
    package_data={
        'capmetrics_etl': ['templates/*.html'],
    },
    packages=find_packages(include=['capmetrics_etl', 'capmetrics_etl.*'],
                           exclude=['tests', 'tests.*']),
    platforms=['any'],
    url='https://github.com/jga/capmetrics-etl',
    version='0.1.0'
)

Add table generation console script.

Add table generation console script.

Response:
#!/usr/bin/env python3
import os
from setuptools import setup, find_packages

def get_readme():
    return open(os.path.join(os.path.dirname(__file__), 'README.rst')).read()

setup(
    author="Julio Gonzalez Altamirano",
    author_email='devjga@gmail.com',
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 3 :: Only',
    ],
    description="ETL for CapMetro raw data.",
    entry_points={
        'console_scripts': [
            'capmetrics=capmetrics_etl.cli:run',
            'capmetrics-tables=capmetrics_etl.cli.tables'
        ],
    },
    install_requires=['click', 'pytz', 'sqlalchemy', 'xlrd'],
    keywords="python etl transit",
    license="MIT",
    long_description=get_readme(),
    name='capmetrics-etl',
    package_data={
        'capmetrics_etl': ['templates/*.html'],
    },
    packages=find_packages(include=['capmetrics_etl', 'capmetrics_etl.*'],
                           exclude=['tests', 'tests.*']),
    platforms=['any'],
    url='https://github.com/jga/capmetrics-etl',
    version='0.1.0'
)

Prompt:
Add table generation console script.

#!/usr/bin/env python3
import os
from setuptools import setup, find_packages

def get_readme():
    return open(os.path.join(os.path.dirname(__file__), 'README.rst')).read()

setup(
    author="Julio Gonzalez Altamirano",
    author_email='devjga@gmail.com',
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 3 :: Only',
    ],
    description="ETL for CapMetro raw data.",
    entry_points={
        'console_scripts': [
            'capmetrics=capmetrics_etl.cli:run',
        ],
    },
    install_requires=['click', 'pytz', 'sqlalchemy', 'xlrd'],
    keywords="python etl transit",
    license="MIT",
    long_description=get_readme(),
    name='capmetrics-etl',
    package_data={
        'capmetrics_etl': ['templates/*.html'],
    },
    packages=find_packages(include=['capmetrics_etl', 'capmetrics_etl.*'],
                           exclude=['tests', 'tests.*']),
    platforms=['any'],
    url='https://github.com/jga/capmetrics-etl',
    version='0.1.0'
)


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

import struct
from math import cos, asin, sqrt

def distance(lat1, lon1, lat2, lon2):
    p = 0.017453292519943295
    a = 0.5 - cos((lat2 - lat1) * p)/2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
    return 12742 * asin(sqrt(a)) * 1000

def i2f(int):
    return struct.unpack('<d', struct.pack('<Q', int))[0]

def print_green(message):
    print(u'\033[92m' + message.decode('utf-8') + '\033[0m');

def print_yellow(message):
    print(u'\033[93m' + message.decode('utf-8') + '\033[0m');

def print_red(message):
    print(u'\033[91m' + message.decode('utf-8') + '\033[0m');

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

import struct
from math import cos, asin, sqrt

def distance(lat1, lon1, lat2, lon2):
    p = 0.017453292519943295
    a = 0.5 - cos((lat2 - lat1) * p)/2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
    return 12742 * asin(sqrt(a)) * 1000

def i2f(int):
    return struct.unpack('<d', struct.pack('<Q', int))[0]

def print_green(message):
    print(u'\033[92m' + message.decode('utf-8') + '\033[0m');

def print_yellow(message):
    print(u'\033[93m' + message.decode('utf-8') + '\033[0m');

def print_red(message):
    print(u'\033[91m' + message.decode('utf-8') + '\033[0m');

Prompt:
Fix encoding error when printing messages

Some messages that will be printed will contain utf-8 chars, e.g. Pokestops in European locations.
# -*- coding: utf-8 -*-

import struct
from math import cos, asin, sqrt

def distance(lat1, lon1, lat2, lon2):
    p = 0.017453292519943295
    a = 0.5 - cos((lat2 - lat1) * p)/2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
    return 12742 * asin(sqrt(a)) * 1000

def i2f(int):
    return struct.unpack('<d', struct.pack('<Q', int))[0]

def print_green(message):
    print('\033[92m' + message + '\033[0m');

def print_yellow(message):
    print('\033[93m' + message + '\033[0m');

def print_red(message):
    print('\033[91m' + message + '\033[0m');


import requests

class AlgoliaEndpoint(object):
  """Class used to call the Algolia API and parse the response."""

  URL = "http://hn.algolia.com/api/v1/search_by_date"

  @staticmethod
  def get(tag, since, until=None, page=0):
    """Send a GET request to the endpoint.

    Since Algolia only returns JSON, parse it into a dict.

    See http://hn.algolia.com/api for more details.

    Params:
      tag: Can be "story" or "comment".
      since: timestamp representing how old the news should be.

    Optional params:
      until: timestamp representing how new the news should be.
      page: The number of the page to get.

    Returns:
      A python dict representing the response.

    Raises:
      requests.exceptions.RequestException.
    """

    numericFilters = ["created_at_i<%d" % since]
    if until is not None:
      numericFilters += ["created_at_i>%d" % until]

    params = {
        "numericFilters": ",".join(numericFilters),
        "tags": tag,
        "page": page
    }

    url = AlgoliaEndpoint.URL
    url += "?" + "&".join(["%s=%s" for k,v in params.items()])
    response = requests.get(url)

    return response.json()

Add Algolia API website in docstring.

Add Algolia API website in docstring.

Response:
import requests

class AlgoliaEndpoint(object):
  """Class used to call the Algolia API and parse the response."""

  URL = "http://hn.algolia.com/api/v1/search_by_date"

  @staticmethod
  def get(tag, since, until=None, page=0):
    """Send a GET request to the endpoint.

    Since Algolia only returns JSON, parse it into a dict.

    See http://hn.algolia.com/api for more details.

    Params:
      tag: Can be "story" or "comment".
      since: timestamp representing how old the news should be.

    Optional params:
      until: timestamp representing how new the news should be.
      page: The number of the page to get.

    Returns:
      A python dict representing the response.

    Raises:
      requests.exceptions.RequestException.
    """

    numericFilters = ["created_at_i<%d" % since]
    if until is not None:
      numericFilters += ["created_at_i>%d" % until]

    params = {
        "numericFilters": ",".join(numericFilters),
        "tags": tag,
        "page": page
    }

    url = AlgoliaEndpoint.URL
    url += "?" + "&".join(["%s=%s" for k,v in params.items()])
    response = requests.get(url)

    return response.json()

Prompt:
Add Algolia API website in docstring.

import requests

class AlgoliaEndpoint(object):
  """Class used to call the Algolia API and parse the response."""

  URL = "http://hn.algolia.com/api/v1/search_by_date"

  @staticmethod
  def get(tag, since, until=None, page=0):
    """Send a GET request to the endpoint.

    Since Algolia only returns JSON, parse it into a dict.

    Params:
      tag: Can be "story" or "comment".
      since: timestamp representing how old the news should be.

    Optional params:
      until: timestamp representing how new the news should be.
      page: The number of the page to get.

    Returns:
      A python dict representing the response.

    Raises:
      requests.exceptions.RequestException.
    """

    numericFilters = ["created_at_i<%d" % since]
    if until is not None:
      numericFilters += ["created_at_i>%d" % until]

    params = {
        "numericFilters": ",".join(numericFilters),
        "tags": tag,
        "page": page
    }

    url = AlgoliaEndpoint.URL
    url += "?" + "&".join(["%s=%s" for k,v in params.items()])
    response = requests.get(url)

    return response.json()


# coding: utf-8

from django.conf import settings

DEFAULTS = {
    "SPEEDINFO_TESTS": False,
    "SPEEDINFO_CACHED_RESPONSE_ATTR_NAME": "_is_cached",
    "SPEEDINFO_STORAGE": None,
    "SPEEDINFO_CACHE_STORAGE_CACHE_ALIAS": "default",
    "SPEEDINFO_PROFILING_CONDITIONS": [],
    "SPEEDINFO_EXCLUDE_URLS": [],
    "SPEEDINFO_ADMIN_COLUMNS": (
        ("View name", "{}", "view_name"),
        ("HTTP method", "{}", "method"),
        ("Anonymous calls", "{:.1f}%", "anon_calls_ratio"),
        ("Cache hits", "{:.1f}%", "cache_hits_ratio"),
        ("SQL queries per call", "{}", "sql_count_per_call"),
        ("SQL time", "{:.1f}%", "sql_time_ratio"),
        ("Total calls", "{}", "total_calls"),
        ("Time per call", "{:.8f}", "time_per_call"),
        ("Total time", "{:.4f}", "total_time"),
    ),
}

class SpeedinfoSettings:
    def __init__(self, defaults=None):
        self.defaults = defaults or DEFAULTS

    def __getattr__(self, name):
        if name not in self.defaults:
            raise AttributeError("Invalid setting: '{}'".format(name))

        return getattr(settings, name, self.defaults.get(name))

speedinfo_settings = SpeedinfoSettings()

Response:
# coding: utf-8

from django.conf import settings

DEFAULTS = {
    "SPEEDINFO_TESTS": False,
    "SPEEDINFO_CACHED_RESPONSE_ATTR_NAME": "_is_cached",
    "SPEEDINFO_STORAGE": None,
    "SPEEDINFO_CACHE_STORAGE_CACHE_ALIAS": "default",
    "SPEEDINFO_PROFILING_CONDITIONS": [],
    "SPEEDINFO_EXCLUDE_URLS": [],
    "SPEEDINFO_ADMIN_COLUMNS": (
        ("View name", "{}", "view_name"),
        ("HTTP method", "{}", "method"),
        ("Anonymous calls", "{:.1f}%", "anon_calls_ratio"),
        ("Cache hits", "{:.1f}%", "cache_hits_ratio"),
        ("SQL queries per call", "{}", "sql_count_per_call"),
        ("SQL time", "{:.1f}%", "sql_time_ratio"),
        ("Total calls", "{}", "total_calls"),
        ("Time per call", "{:.8f}", "time_per_call"),
        ("Total time", "{:.4f}", "total_time"),
    ),
}

class SpeedinfoSettings:
    def __init__(self, defaults=None):
        self.defaults = defaults or DEFAULTS

    def __getattr__(self, name):
        if name not in self.defaults:
            raise AttributeError("Invalid setting: '{}'".format(name))

        return getattr(settings, name, self.defaults.get(name))

speedinfo_settings = SpeedinfoSettings()

Prompt:
Change SPEEDINFO_CACHED_RESPONSE_ATTR_NAME default value to `_is_cached`

# coding: utf-8

from django.conf import settings

DEFAULTS = {
    "SPEEDINFO_TESTS": False,
    "SPEEDINFO_CACHED_RESPONSE_ATTR_NAME": "is_cached",
    "SPEEDINFO_STORAGE": None,
    "SPEEDINFO_CACHE_STORAGE_CACHE_ALIAS": "default",
    "SPEEDINFO_PROFILING_CONDITIONS": [],
    "SPEEDINFO_EXCLUDE_URLS": [],
    "SPEEDINFO_ADMIN_COLUMNS": (
        ("View name", "{}", "view_name"),
        ("HTTP method", "{}", "method"),
        ("Anonymous calls", "{:.1f}%", "anon_calls_ratio"),
        ("Cache hits", "{:.1f}%", "cache_hits_ratio"),
        ("SQL queries per call", "{}", "sql_count_per_call"),
        ("SQL time", "{:.1f}%", "sql_time_ratio"),
        ("Total calls", "{}", "total_calls"),
        ("Time per call", "{:.8f}", "time_per_call"),
        ("Total time", "{:.4f}", "total_time"),
    ),
}

class SpeedinfoSettings:
    def __init__(self, defaults=None):
        self.defaults = defaults or DEFAULTS

    def __getattr__(self, name):
        if name not in self.defaults:
            raise AttributeError("Invalid setting: '{}'".format(name))

        return getattr(settings, name, self.defaults.get(name))

speedinfo_settings = SpeedinfoSettings()


import bottle
import waitress
import controller
import breathe
from pytz import timezone
from apscheduler.schedulers.background import BackgroundScheduler

bottle_app = bottle.app()
scheduler = BackgroundScheduler()
scheduler.configure(timezone=timezone('US/Pacific'))
breather = breathe.Breathe()
my_controller = controller.Controller(bottle_app, None)

@scheduler.scheduled_job(trigger='cron', hour=21, minute=0)
def on_job():
    """Start at 9:00pm PT"""
    print('STARTING BREATHER')
    breather.restart()

@scheduler.scheduled_job(trigger='cron', hour=23, minute=0)
def off_job():
    """End at 11:00pm PT"""
    print("STOPPING BREATHER")
    breather.shutdown()

if __name__ == '__main__':
    scheduler.start()
    waitress.serve(bottle_app, host='0.0.0.0', port=7000)

Response:
import bottle
import waitress
import controller
import breathe
from pytz import timezone
from apscheduler.schedulers.background import BackgroundScheduler

bottle_app = bottle.app()
scheduler = BackgroundScheduler()
scheduler.configure(timezone=timezone('US/Pacific'))
breather = breathe.Breathe()
my_controller = controller.Controller(bottle_app, None)

@scheduler.scheduled_job(trigger='cron', hour=21, minute=0)
def on_job():
    """Start at 9:00pm PT"""
    print('STARTING BREATHER')
    breather.restart()

@scheduler.scheduled_job(trigger='cron', hour=23, minute=0)
def off_job():
    """End at 11:00pm PT"""
    print("STOPPING BREATHER")
    breather.shutdown()

if __name__ == '__main__':
    scheduler.start()
    waitress.serve(bottle_app, host='0.0.0.0', port=7000)

Prompt:
Add scheduler. Schedule lights on for 9:00pm and lights off for 11:00pm

import bottle
import waitress
import controller
import breathe

if __name__ == '__main__':
    bottle_app = bottle.app()
    breather = breathe.Breathe()
    my_controller = controller.Controller(bottle_app, breather)
    waitress.serve(bottle_app, host='0.0.0.0', port=7000)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations
from django.db import models

class Migration(migrations.Migration):
    atomic=False

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

    operations = [
        migrations.RenameModel(
            old_name='Document',
            new_name='Chapter',
        ),
        migrations.RenameField(
            model_name='paragraph',
            old_name='document',
            new_name='chapter',
        ),
    ]

Work around a migration issue in sqlite

Response:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations
from django.db import models

class Migration(migrations.Migration):
    atomic=False

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

    operations = [
        migrations.RenameModel(
            old_name='Document',
            new_name='Chapter',
        ),
        migrations.RenameField(
            model_name='paragraph',
            old_name='document',
            new_name='chapter',
        ),
    ]

Prompt:
apps/documents: Work around a migration issue in sqlite

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations
from django.db import models

class Migration(migrations.Migration):

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

    operations = [
        migrations.RenameModel(
            old_name='Document',
            new_name='Chapter',
        ),
        migrations.RenameField(
            model_name='paragraph',
            old_name='document',
            new_name='chapter',
        ),
    ]


"""Python Bindings for Psynteract.

"""

# This is built on the demo package as described at
# https://packaging.python.org/en/latest/distributing.html

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='Psynteract',

    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/single_source_version.html
    version='0.6.1',

    description='Psynteract Python bindings',
    long_description=long_description,

    # The project's main homepage.
    url='https://github.com/psynteract/psynteract-py',

    # Author details
    author='Felix Henninger & Pascal Kieslich',
    author_email='mailbox@felixhenninger.com',

    # Choose your license
    license='Apache 2.0',

    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        'Development Status :: 4 - Beta',

        # Indicate who your project is intended for
        'Intended Audience :: Developers',

        # Pick your license as you wish (should match "license" above)
        'License :: OSI Approved :: Apache 2.0',

        # Specify the Python versions you support here. In particular, ensure
        # that you indicate whether you support Python 2, Python 3 or both.
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
    ],

    # List run-time dependencies here.  These will be installed by pip when
    # your project is installed. For an analysis of "install_requires" vs pip's
    # requirements files see:
    # https://packaging.python.org/en/latest/requirements.html
    install_requires=['requests', 'pycouchdb'],
)

Add tentative support for installation via pip

Response:
"""Python Bindings for Psynteract.

"""

# This is built on the demo package as described at
# https://packaging.python.org/en/latest/distributing.html

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='Psynteract',

    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/single_source_version.html
    version='0.6.1',

    description='Psynteract Python bindings',
    long_description=long_description,

    # The project's main homepage.
    url='https://github.com/psynteract/psynteract-py',

    # Author details
    author='Felix Henninger & Pascal Kieslich',
    author_email='mailbox@felixhenninger.com',

    # Choose your license
    license='Apache 2.0',

    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        'Development Status :: 4 - Beta',

        # Indicate who your project is intended for
        'Intended Audience :: Developers',

        # Pick your license as you wish (should match "license" above)
        'License :: OSI Approved :: Apache 2.0',

        # Specify the Python versions you support here. In particular, ensure
        # that you indicate whether you support Python 2, Python 3 or both.
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
    ],

    # List run-time dependencies here.  These will be installed by pip when
    # your project is installed. For an analysis of "install_requires" vs pip's
    # requirements files see:
    # https://packaging.python.org/en/latest/requirements.html
    install_requires=['requests', 'pycouchdb'],
)

Prompt: Add tentative support for installation via pip


from flask import render_template
from app.main import main

@main.app_errorhandler(400)
def page_not_found(e):
    print(e.message)
    return _render_error_page(500)

@main.app_errorhandler(404)
def page_not_found(e):
    print(e.message)
    return _render_error_page(404)

@main.app_errorhandler(500)
def exception(e):
    print(e.message)
    return _render_error_page(500)

@main.app_errorhandler(503)
def service_unavailable(e):
    print(e.message)
    return _render_error_page(503)

def _render_error_page(status_code):
    template_map = {
        404: "errors/404.html",
        500: "errors/500.html",
        503: "errors/500.html",
    }
    if status_code not in template_map:
        status_code = 500
    template_data = main.config['BASE_TEMPLATE_DATA']
    return render_template(template_map[status_code], **template_data), status_code

Response:
from flask import render_template
from app.main import main

@main.app_errorhandler(400)
def page_not_found(e):
    print(e.message)
    return _render_error_page(500)

@main.app_errorhandler(404)
def page_not_found(e):
    print(e.message)
    return _render_error_page(404)

@main.app_errorhandler(500)
def exception(e):
    print(e.message)
    return _render_error_page(500)

@main.app_errorhandler(503)
def service_unavailable(e):
    print(e.message)
    return _render_error_page(503)

def _render_error_page(status_code):
    template_map = {
        404: "errors/404.html",
        500: "errors/500.html",
        503: "errors/500.html",
    }
    if status_code not in template_map:
        status_code = 500
    template_data = main.config['BASE_TEMPLATE_DATA']
    return render_template(template_map[status_code], **template_data), status_code

Prompt:
Add print statements for all error types

from flask import render_template
from app.main import main

@main.app_errorhandler(400)
def page_not_found(e):
    return _render_error_page(500)

@main.app_errorhandler(404)
def page_not_found(e):
    return _render_error_page(404)

@main.app_errorhandler(500)
def exception(e):
    return _render_error_page(500)

@main.app_errorhandler(503)
def service_unavailable(e):
    return _render_error_page(503)

def _render_error_page(status_code):
    template_map = {
        404: "errors/404.html",
        500: "errors/500.html",
        503: "errors/500.html",
    }
    if status_code not in template_map:
        status_code = 500
    template_data = main.config['BASE_TEMPLATE_DATA']
    return render_template(template_map[status_code], **template_data), status_code


from setuptools import setup

PACKAGE_VERSION = '1.0.1'
deps = []

setup(name='wptserve',
      version=PACKAGE_VERSION,
      description="Python webserver intended for in web browser testing",
      long_description=open("README.md").read(),
      # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
      classifiers=["Development Status :: 5 - Production/Stable",
                   "License :: OSI Approved :: BSD License",
                   "Topic :: Internet :: WWW/HTTP :: HTTP Servers"],
      keywords='',
      author='James Graham',
      author_email='james@hoppipolla.co.uk',
      url='http://wptserve.readthedocs.org/',
      license='BSD',
      packages=['wptserve'],
      include_package_data=True,
      zip_safe=False,
      install_requires=deps
      )

Update the version for manifest update

Update the version for manifest update

Response:
from setuptools import setup

PACKAGE_VERSION = '1.0.1'
deps = []

setup(name='wptserve',
      version=PACKAGE_VERSION,
      description="Python webserver intended for in web browser testing",
      long_description=open("README.md").read(),
      # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
      classifiers=["Development Status :: 5 - Production/Stable",
                   "License :: OSI Approved :: BSD License",
                   "Topic :: Internet :: WWW/HTTP :: HTTP Servers"],
      keywords='',
      author='James Graham',
      author_email='james@hoppipolla.co.uk',
      url='http://wptserve.readthedocs.org/',
      license='BSD',
      packages=['wptserve'],
      include_package_data=True,
      zip_safe=False,
      install_requires=deps
      )

Prompt:
Update the version for manifest update

from setuptools import setup

PACKAGE_VERSION = '1.0'
deps = []

setup(name='wptserve',
      version=PACKAGE_VERSION,
      description="Python webserver intended for in web browser testing",
      long_description=open("README.md").read(),
      # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
      classifiers=["Development Status :: 5 - Production/Stable",
                   "License :: OSI Approved :: BSD License",
                   "Topic :: Internet :: WWW/HTTP :: HTTP Servers"],
      keywords='',
      author='James Graham',
      author_email='james@hoppipolla.co.uk',
      url='http://wptserve.readthedocs.org/',
      license='BSD',
      packages=['wptserve'],
      include_package_data=True,
      zip_safe=False,
      install_requires=deps
      )


"""
A collection of Django extensions that add content-management facilities to Django projects.

Developed by Dave Hall.

<http://etianen.com/>
"""

VERSION = (1, 8)

Bump version number for release.

Bump version number for release.

Response:
"""
A collection of Django extensions that add content-management facilities to Django projects.

Developed by Dave Hall.

<http://etianen.com/>
"""

VERSION = (1, 8)

Prompt:
Bump version number for release.

"""
A collection of Django extensions that add content-management facilities to Django projects.

Developed by Dave Hall.

<http://etianen.com/>
"""

VERSION = (1, 7, 11)


from flask.ext.restful import Resource, marshal, reqparse

class BasicResource(Resource):

    def __init__(self):
        super(BasicResource, self).__init__()
        self.parser = reqparse.RequestParser()

    def response(self, data, code):
        return marshal(data, self.fields), code

Add parser to basic resource

Add parser to basic resource

Response:
from flask.ext.restful import Resource, marshal, reqparse

class BasicResource(Resource):

    def __init__(self):
        super(BasicResource, self).__init__()
        self.parser = reqparse.RequestParser()

    def response(self, data, code):
        return marshal(data, self.fields), code

Prompt:
Add parser to basic resource

from flask.ext.restful import Resource, marshal

class BasicResource(Resource):

    def __init__(self):
        super(BasicResource, self).__init__()

    def response(self, data, code):
        return marshal(data, self.fields), code


from django.http import HttpResponse
from django.template import loader
from django.utils.crypto import salted_hmac, constant_time_compare

from django.contrib.auth.models import User

class HMACAuth(object):

    def __init__(self, realm='API'):
        self.realm = realm

    def process_request(self, request):
        user_id = request.META.get('HTTP_X_LA_USER_ID', None)
        signature = request.META.get('HTTP_X_LA_HASH', None)
        return user_id, signature

    def is_authenticated(self, request):
        user_id, signature = self.process_request(request)

        if user_id and signature:
            check_digest = salted_hmac("linked_accounts.views.login", str(user_id)).hexdigest()
            if not constant_time_compare(signature, check_digest):
                return False

            try:
                user = User.objects.get(id=user_id)
                if user.is_active:
                    request.user = user
                    return True
            except User.DoesNotExist:
                pass

        return False

    def challenge(self):
        response = HttpResponse()
        response.status_code = 401

        tmpl = loader.render_to_string('linked_accounts/api_challenge.html')

        response.content = tmpl

        return response

    def __repr__(self):
        return u'<HMACAuth: realm=%s>' % self.realm

Response:
from django.http import HttpResponse
from django.template import loader
from django.utils.crypto import salted_hmac, constant_time_compare

from django.contrib.auth.models import User

class HMACAuth(object):

    def __init__(self, realm='API'):
        self.realm = realm

    def process_request(self, request):
        user_id = request.META.get('HTTP_X_LA_USER_ID', None)
        signature = request.META.get('HTTP_X_LA_HASH', None)
        return user_id, signature

    def is_authenticated(self, request):
        user_id, signature = self.process_request(request)

        if user_id and signature:
            check_digest = salted_hmac("linked_accounts.views.login", str(user_id)).hexdigest()
            if not constant_time_compare(signature, check_digest):
                return False

            try:
                user = User.objects.get(id=user_id)
                if user.is_active:
                    request.user = user
                    return True
            except User.DoesNotExist:
                pass

        return False

    def challenge(self):
        response = HttpResponse()
        response.status_code = 401

        tmpl = loader.render_to_string('linked_accounts/api_challenge.html')

        response.content = tmpl

        return response

    def __repr__(self):
        return u'<HMACAuth: realm=%s>' % self.realm

Prompt:
Return is_authenticated true if we got user and it is active

from django.http import HttpResponse
from django.template import loader
from django.utils.crypto import salted_hmac, constant_time_compare

from django.contrib.auth.models import User

class HMACAuth(object):

    def __init__(self, realm='API'):
        self.realm = realm

    def process_request(self, request):
        user_id = request.META.get('HTTP_X_LA_USER_ID', None)
        signature = request.META.get('HTTP_X_LA_HASH', None)
        return user_id, signature

    def is_authenticated(self, request):
        user_id, signature = self.process_request(request)

        if user_id and signature:
            check_digest = salted_hmac("linked_accounts.views.login", str(user_id)).hexdigest()
            if not constant_time_compare(signature, check_digest):
                return False

            try:
                user = User.objects.get(id=user_id)
                if user.is_active:
                    request.user = user
            except User.DoesNotExist:
                pass

        return False

    def challenge(self):
        response = HttpResponse()
        response.status_code = 401

        tmpl = loader.render_to_string('linked_accounts/api_challenge.html')

        response.content = tmpl

        return response

    def __repr__(self):
        return u'<HMACAuth: realm=%s>' % self.realm


from sys import argv
from operator import itemgetter

if __name__ == "__main__":
    if (len(argv) != 3):
        print("Usage: " + argv[0] + " <input.svm> <output.svm>")
        print("Example:")
        print("input.svm:")
        print("1 24:1 12:1 55:1")
        print("0 84:1 82:1 15:1")
        print("...")
        print("output.svm:")
        print("1 12:1 24:1 55:1")
        print("0 15:1 82:1 84:1")
        print("...")
        exit(1)

    input = argv[1]
    output = argv[2]
    with open(input, "r") as f:
        for line in f:
            splits = line.split()
            target = splits[0]
            features = splits[1:]
            d = {int(a[0]):int(a[1]) for a in [feat.split(':') for feat in features]}
            dsorted = sorted(d.iteritems(), key=itemgetter(0), reverse=False)
            with open(output, "a") as g:
                g.write(target + ' ')
                for (i,j) in dsorted:
                    g.write(str(i)+':'+str(j)+' ')
                g.write('\n')

Add helper script to sort svmlight files by column keys.

Response:
from sys import argv
from operator import itemgetter

if __name__ == "__main__":
    if (len(argv) != 3):
        print("Usage: " + argv[0] + " <input.svm> <output.svm>")
        print("Example:")
        print("input.svm:")
        print("1 24:1 12:1 55:1")
        print("0 84:1 82:1 15:1")
        print("...")
        print("output.svm:")
        print("1 12:1 24:1 55:1")
        print("0 15:1 82:1 84:1")
        print("...")
        exit(1)

    input = argv[1]
    output = argv[2]
    with open(input, "r") as f:
        for line in f:
            splits = line.split()
            target = splits[0]
            features = splits[1:]
            d = {int(a[0]):int(a[1]) for a in [feat.split(':') for feat in features]}
            dsorted = sorted(d.iteritems(), key=itemgetter(0), reverse=False)
            with open(output, "a") as g:
                g.write(target + ' ')
                for (i,j) in dsorted:
                    g.write(str(i)+':'+str(j)+' ')
                g.write('\n')

Prompt: Add helper script to sort svmlight files by column keys.


from logger import logger
from seriesly import Seriesly

from perfrunner.settings import StatsSettings

def main():
    s = Seriesly(StatsSettings.SERIESLY['host'])
    for db in s.list_dbs():
        logger.info('Compacting {}'.format(db))
        result = s[db].compact()
        logger.info('Compaction finished: {}'.format(result))

if __name__ == '__main__':
    main()

CBPS-210: Add a script for periodic compaction of Seriesly databases

Change-Id: I95123c116c0dcdce4b4df02974d2a9fdeaef55dc
Reviewed-on: http://review.couchbase.org/69249
Tested-by: buildbot <80754af91bfb6d1073585b046fe0a474ce868509@couchbase.com>
Reviewed-by: Pavel Paulau <dd88eded64e90046a680e3a6c0828ceb8fe8a0e7@gmail.com>

Response:
from logger import logger
from seriesly import Seriesly

from perfrunner.settings import StatsSettings

def main():
    s = Seriesly(StatsSettings.SERIESLY['host'])
    for db in s.list_dbs():
        logger.info('Compacting {}'.format(db))
        result = s[db].compact()
        logger.info('Compaction finished: {}'.format(result))

if __name__ == '__main__':
    main()

Prompt:
CBPS-210: Add a script for periodic compaction of Seriesly databases

Change-Id: I95123c116c0dcdce4b4df02974d2a9fdeaef55dc
Reviewed-on: http://review.couchbase.org/69249
Tested-by: buildbot <80754af91bfb6d1073585b046fe0a474ce868509@couchbase.com>
Reviewed-by: Pavel Paulau <dd88eded64e90046a680e3a6c0828ceb8fe8a0e7@gmail.com>


from mythril.laser.ethereum.transaction import execute_contract_creation
from mythril.ether import util
import mythril.laser.ethereum.svm as svm
from mythril.disassembler.disassembly import Disassembly
from datetime import datetime
from mythril.ether.soliditycontract import SolidityContract
import tests
from mythril.analysis.security import fire_lasers
from mythril.analysis.symbolic import SymExecWrapper

def test_create():
    contract = SolidityContract(str(tests.TESTDATA_INPUTS_CONTRACTS / 'calls.sol'))

    laser_evm = svm.LaserEVM({})

    laser_evm.time = datetime.now()
    execute_contract_creation(laser_evm, contract.creation_code)

    resulting_final_state = laser_evm.open_states[0]

    for address, created_account in resulting_final_state.accounts.items():
        created_account_code = created_account.code
        actual_code = Disassembly(contract.code)

        for i in range(len(created_account_code.instruction_list)):
            found_instruction = created_account_code.instruction_list[i]
            actual_instruction = actual_code.instruction_list[i]

            assert found_instruction['opcode'] == actual_instruction['opcode']

def test_sym_exec():
    contract = SolidityContract(str(tests.TESTDATA_INPUTS_CONTRACTS / 'calls.sol'))

    sym = SymExecWrapper(contract, address=(util.get_indexed_address(0)), strategy="dfs")
    issues = fire_lasers(sym)

    assert len(issues) != 0

Update test to reflect the refactor

Update test to reflect the refactor

Response:
from mythril.laser.ethereum.transaction import execute_contract_creation
from mythril.ether import util
import mythril.laser.ethereum.svm as svm
from mythril.disassembler.disassembly import Disassembly
from datetime import datetime
from mythril.ether.soliditycontract import SolidityContract
import tests
from mythril.analysis.security import fire_lasers
from mythril.analysis.symbolic import SymExecWrapper

def test_create():
    contract = SolidityContract(str(tests.TESTDATA_INPUTS_CONTRACTS / 'calls.sol'))

    laser_evm = svm.LaserEVM({})

    laser_evm.time = datetime.now()
    execute_contract_creation(laser_evm, contract.creation_code)

    resulting_final_state = laser_evm.open_states[0]

    for address, created_account in resulting_final_state.accounts.items():
        created_account_code = created_account.code
        actual_code = Disassembly(contract.code)

        for i in range(len(created_account_code.instruction_list)):
            found_instruction = created_account_code.instruction_list[i]
            actual_instruction = actual_code.instruction_list[i]

            assert found_instruction['opcode'] == actual_instruction['opcode']

def test_sym_exec():
    contract = SolidityContract(str(tests.TESTDATA_INPUTS_CONTRACTS / 'calls.sol'))

    sym = SymExecWrapper(contract, address=(util.get_indexed_address(0)), strategy="dfs")
    issues = fire_lasers(sym)

    assert len(issues) != 0

Prompt:
Update test to reflect the refactor

import mythril.laser.ethereum.transaction as transaction
from mythril.ether import util
import mythril.laser.ethereum.svm as svm
from mythril.disassembler.disassembly import Disassembly
from datetime import datetime
from mythril.ether.soliditycontract import SolidityContract
import tests
from mythril.analysis.security import fire_lasers
from mythril.analysis.symbolic import SymExecWrapper

def test_create():
    contract = SolidityContract(str(tests.TESTDATA_INPUTS_CONTRACTS / 'calls.sol'))

    laser_evm = svm.LaserEVM({})

    laser_evm.time = datetime.now()
    laser_evm.execute_contract_creation(contract.creation_code)

    resulting_final_state = laser_evm.open_states[0]

    for address, created_account in resulting_final_state.accounts.items():
        created_account_code = created_account.code
        actual_code = Disassembly(contract.code)

        for i in range(len(created_account_code.instruction_list)):
            found_instruction = created_account_code.instruction_list[i]
            actual_instruction = actual_code.instruction_list[i]

            assert found_instruction['opcode'] == actual_instruction['opcode']

def test_sym_exec():
    contract = SolidityContract(str(tests.TESTDATA_INPUTS_CONTRACTS / 'calls.sol'))

    sym = SymExecWrapper(contract, address=(util.get_indexed_address(0)), strategy="dfs")
    issues = fire_lasers(sym)

    assert len(issues) != 0


Response:
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab
# pylint: disable=invalid-name
"""
csv2ofx.mappings.stripe
~~~~~~~~~~~~~~~~~~~~~~~~

Provides a mapping for transactions obtained via Stripe card processing

Note that Stripe provides a Default set of columns or you can download
All columns. (as well as custom). The Default set does not include card
information, so provides no appropriate value for the PAYEE field for
an anonymous transaction (missing a customer).
It's suggested the All Columns format be used if not all transactions
identify a customer. This mapping sets PAYEE to Customer Name if it
exists, otherwise Card Name (if provided)
"""
from __future__ import (
    absolute_import, division, print_function, unicode_literals)

from operator import itemgetter

mapping = {
    'has_header': True,
    'account': 'Stripe',
    'id': itemgetter('id'),
    'date': itemgetter('created'),
    'amount': itemgetter('amount'),
    'currency': itemgetter('currency'),
    'payee': lambda tr: tr.get('customer_description')
        if len(tr.get('customer_description')) > 0
        else tr.get('card_name', ""),
    'desc': itemgetter("description")
}

Prompt:
Fix lint line length warnings (blocking manage checks)

# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab
# pylint: disable=invalid-name
"""
csv2ofx.mappings.stripe
~~~~~~~~~~~~~~~~~~~~~~~~

Provides a mapping for transactions obtained via Stripe card processing

Note that Stripe provides a Default set of columns or you can download All columns. (as well as custom).
The Default set does not include card information, so provides no appropriate value for the
PAYEE field for an anonymous transaction (missing a customer).
It's suggested the All Columns format be used if not all transactions identify a customer.
This mapping sets PAYEE to Customer Name if it exists, otherwise Card Name (if provided)
"""
from __future__ import (
    absolute_import, division, print_function, unicode_literals)

from operator import itemgetter

mapping = {
    'has_header': True,
    'account': 'Stripe',
    'id': itemgetter('id'),
    'date': itemgetter('created'),
    'amount': itemgetter('amount'),
    'currency': itemgetter('currency'),
    'payee': lambda tr: tr.get('customer_description') if len(tr.get('customer_description')) > 0 else tr.get('card_name', ""),
    'desc': itemgetter("description")
}


Response:
"""
This class is used in kaggle competitions
"""

import json

class Settings():
    train_path = None
    test_path = None
    model_path = None
    submission_path = None
    string_train_path = "TRAIN_DATA_PATH"
    string_test_path = "TEST_DATA_PATH"
    string_model_path = "MODEL_PATH"
    string_submission_path = "SUBMISSION_PATH"

    def __init__(self):
        self._load_settings()
    def __str__(self):
        to_print = "\n".join([self.string_train_path,
                              self.train_path,
                              self.string_test_path,
                              self.test_path,
                              self.string_model_path,
                              self.model_path,
                              self.string_submission_path,
                              self.submission_path])
        return to_print
    def _load_settings(self):
        with open('SETTINGS.json') as json_data:
            settings = json.load(json_data)
            self.train_path=settings[self.string_train_path]
            self.test_path=settings[self.string_test_path]
            self.model_path=settings[self.string_model_path]
            self.submission_path=settings[self.string_submission_path]

Prompt:
Remove not necessary code in Setting class

"""
This class is used in kaggle competitions
"""

import json

class Settings():
    train_path = None
    test_path = None
    model_path = None
    submission_path = None
    string_train_path = "TRAIN_DATA_PATH"
    string_test_path = "TEST_DATA_PATH"
    string_model_path = "MODEL_PATH"
    string_submission_path = "SUBMISSION_PATH"

    def __init__(self):
        self._load_settings()
    def __str__(self):
        to_print = "\n".join([self.string_train_path,
                              self.train_path,
                              self.string_test_path,
                              self.test_path,
                              self.string_model_path,
                              self.model_path,
                              self.string_submission_path,
                              self.submission_path])
        return to_print
    def _load_settings(self):
        with open('SETTINGS.json') as json_data:
            settings = json.load(json_data)
            self.train_path=settings[self.string_train_path]
            self.test_path=settings[self.string_test_path]
            self.model_path=settings[self.string_model_path]
            self.submission_path=settings[self.string_submission_path]

s = Settings()


import time

from . import config

# expects Cozify devices type json data
def getMultisensorData(data):
    out = []
    for device in data:
        state=data[device]['state']
        devtype = state['type']

        if devtype == 'STATE_MULTI_SENSOR':
            name=data[device]['name']
            if 'lastSeen' in state:
                timestamp=state['lastSeen']
            else:
                # if no time of measurement is known we must make a reasonable assumption
                # Stored here in milliseconds to match accuracy of what the hub will give you
                timestamp = time.time() * 1000
            if 'temperature' in state:
                temperature=state['temperature']
            else:
                temperature=None
            if 'humidity' in state:
                humidity=state['humidity']
            else:
                humidity=None

            out.append({
                    'name': name,
                    'time': timestamp,
                    'temperature': temperature,
                    'humidity': humidity
                    })
    return out

Remove outdated imports, oops sorry.

Remove outdated imports, oops sorry.

Response:
import time

from . import config

# expects Cozify devices type json data
def getMultisensorData(data):
    out = []
    for device in data:
        state=data[device]['state']
        devtype = state['type']

        if devtype == 'STATE_MULTI_SENSOR':
            name=data[device]['name']
            if 'lastSeen' in state:
                timestamp=state['lastSeen']
            else:
                # if no time of measurement is known we must make a reasonable assumption
                # Stored here in milliseconds to match accuracy of what the hub will give you
                timestamp = time.time() * 1000
            if 'temperature' in state:
                temperature=state['temperature']
            else:
                temperature=None
            if 'humidity' in state:
                humidity=state['humidity']
            else:
                humidity=None

            out.append({
                    'name': name,
                    'time': timestamp,
                    'temperature': temperature,
                    'humidity': humidity
                    })
    return out

Prompt:
Remove outdated imports, oops sorry.

import time

from influxdb import InfluxDBClient
from influxdb import SeriesHelper

from . import config

# expects Cozify devices type json data
def getMultisensorData(data):
    out = []
    for device in data:
        state=data[device]['state']
        devtype = state['type']

        if devtype == 'STATE_MULTI_SENSOR':
            name=data[device]['name']
            if 'lastSeen' in state:
                timestamp=state['lastSeen']
            else:
                # if no time of measurement is known we must make a reasonable assumption
                # Stored here in milliseconds to match accuracy of what the hub will give you
                timestamp = time.time() * 1000
            if 'temperature' in state:
                temperature=state['temperature']
            else:
                temperature=None
            if 'humidity' in state:
                humidity=state['humidity']
            else:
                humidity=None

            out.append({
                    'name': name,
                    'time': timestamp,
                    'temperature': temperature,
                    'humidity': humidity
                    })
    return out


# -*- coding: utf-8 -*-
from django.conf import settings
from django.db import migrations
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps

def rename_zulip_realm_to_zulipinternal(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
    if not settings.PRODUCTION:
        return

    Realm = apps.get_model('zerver', 'Realm')
    UserProfile = apps.get_model('zerver', 'UserProfile')

    if Realm.objects.count() == 0:
        # Database not yet populated, do nothing:
        return

    if Realm.objects.filter(string_id="zulipinternal").exists():
        return
    if not Realm.objects.filter(string_id="zulip").exists():
        # If the user renamed the `zulip` system bot realm (or deleted
        # it), there's nothing for us to do.
        return

    internal_realm = Realm.objects.get(string_id="zulip")

    # For safety, as a sanity check, verify that "internal_realm" is indeed the realm for system bots:
    welcome_bot = UserProfile.objects.get(email="welcome-bot@zulip.com")
    assert welcome_bot.realm.id == internal_realm.id

    internal_realm.string_id = "zulipinternal"
    internal_realm.name = "System use only"
    internal_realm.save()

class Migration(migrations.Migration):

    dependencies = [
        ('zerver', '0236_remove_illegal_characters_email_full'),
    ]

    operations = [
        migrations.RunPython(rename_zulip_realm_to_zulipinternal)
    ]

Response:
# -*- coding: utf-8 -*-
from django.conf import settings
from django.db import migrations
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps

def rename_zulip_realm_to_zulipinternal(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
    if not settings.PRODUCTION:
        return

    Realm = apps.get_model('zerver', 'Realm')
    UserProfile = apps.get_model('zerver', 'UserProfile')

    if Realm.objects.count() == 0:
        # Database not yet populated, do nothing:
        return

    if Realm.objects.filter(string_id="zulipinternal").exists():
        return
    if not Realm.objects.filter(string_id="zulip").exists():
        # If the user renamed the `zulip` system bot realm (or deleted
        # it), there's nothing for us to do.
        return

    internal_realm = Realm.objects.get(string_id="zulip")

    # For safety, as a sanity check, verify that "internal_realm" is indeed the realm for system bots:
    welcome_bot = UserProfile.objects.get(email="welcome-bot@zulip.com")
    assert welcome_bot.realm.id == internal_realm.id

    internal_realm.string_id = "zulipinternal"
    internal_realm.name = "System use only"
    internal_realm.save()

class Migration(migrations.Migration):

    dependencies = [
        ('zerver', '0236_remove_illegal_characters_email_full'),
    ]

    operations = [
        migrations.RunPython(rename_zulip_realm_to_zulipinternal)
    ]

Prompt:
migrations: Fix zulipinternal migration corner case.

It's theoretically possible to have configured a Zulip server where
the system bots live in the same realm as normal users (and may have
in fact been the default in early Zulip releases?  Unclear.).  We
should handle these without the migration intended to clean up naming
for the system bot realm crashing.

Fixes #13660.

# -*- coding: utf-8 -*-
from django.conf import settings
from django.db import migrations
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps

def rename_zulip_realm_to_zulipinternal(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
    if not settings.PRODUCTION:
        return

    Realm = apps.get_model('zerver', 'Realm')
    UserProfile = apps.get_model('zerver', 'UserProfile')

    if Realm.objects.count() == 0:
        # Database not yet populated, do nothing:
        return

    if Realm.objects.filter(string_id="zulipinternal").exists():
        return

    internal_realm = Realm.objects.get(string_id="zulip")

    # For safety, as a sanity check, verify that "internal_realm" is indeed the realm for system bots:
    welcome_bot = UserProfile.objects.get(email="welcome-bot@zulip.com")
    assert welcome_bot.realm.id == internal_realm.id

    internal_realm.string_id = "zulipinternal"
    internal_realm.name = "System use only"
    internal_realm.save()

class Migration(migrations.Migration):

    dependencies = [
        ('zerver', '0236_remove_illegal_characters_email_full'),
    ]

    operations = [
        migrations.RunPython(rename_zulip_realm_to_zulipinternal)
    ]


######
#  Check ExId (GUID) by Email through JDBC
#
#  Author:        Christoph Stoettner
#  Mail:          christoph.stoettner@stoeps.de
#  Documentation: http://scripting101.stoeps.de
#
#  Version:       2.0
#  Date:          2014-06-04
#
#  License:       Apache 2.0
#
#  Check ExId of a User in all Connections Applications

import ibmcnx.functions

cell = AdminControl.getCell()
cellname = "/Cell:" + cell + "/"

# Get a list of all databases except DefaultEJBTimerDataSource and OTiSDataSource
dbs = AdminConfig.list('DataSource',AdminConfig.getid(cellname)).splitlines()
dsidlist = []
# remove unwanted databases
for db in dbs:
    dbname = db.split('(')
    n = 0
    for i in dbname:
        # i is only the name of the DataSource, db is DataSource ID!
        if n == 0 and i != "DefaultEJBTimerDataSource" and i != 'OTiSDataSource':
            dsidlist.append(str(db).replace('"',''))
        n += 1

dsidlist.sort()

for dsid in dsidlist:
    propertySet = AdminConfig.showAttribute(dsid,"propertySet")
    print propertySet
    propertyList = AdminConfig.list("J2EEResourceProperty", propertySet).splitlines()
    print propertyList

Response:
######
#  Check ExId (GUID) by Email through JDBC
#
#  Author:        Christoph Stoettner
#  Mail:          christoph.stoettner@stoeps.de
#  Documentation: http://scripting101.stoeps.de
#
#  Version:       2.0
#  Date:          2014-06-04
#
#  License:       Apache 2.0
#
#  Check ExId of a User in all Connections Applications

import ibmcnx.functions

cell = AdminControl.getCell()
cellname = "/Cell:" + cell + "/"

# Get a list of all databases except DefaultEJBTimerDataSource and OTiSDataSource
dbs = AdminConfig.list('DataSource',AdminConfig.getid(cellname)).splitlines()
dsidlist = []
# remove unwanted databases
for db in dbs:
    dbname = db.split('(')
    n = 0
    for i in dbname:
        # i is only the name of the DataSource, db is DataSource ID!
        if n == 0 and i != "DefaultEJBTimerDataSource" and i != 'OTiSDataSource':
            dsidlist.append(str(db).replace('"',''))
        n += 1

dsidlist.sort()

for dsid in dsidlist:
    propertySet = AdminConfig.showAttribute(dsid,"propertySet")
    print propertySet
    propertyList = AdminConfig.list("J2EEResourceProperty", propertySet).splitlines()
    print propertyList

Prompt:
8: Create documentation of DataSource Settings

Task-Url: http://github.com/stoeps13/ibmcnx2/issues/issue/8
######
#  Check ExId (GUID) by Email through JDBC
#
#  Author:        Christoph Stoettner
#  Mail:          christoph.stoettner@stoeps.de
#  Documentation: http://scripting101.stoeps.de
#
#  Version:       2.0
#  Date:          2014-06-04
#
#  License:       Apache 2.0
#
#  Check ExId of a User in all Connections Applications

import ibmcnx.functions

cell = AdminControl.getCell()
cellname = "/Cell:" + cell + "/"

# Get a list of all databases except DefaultEJBTimerDataSource and OTiSDataSource
dbs = AdminConfig.list('DataSource',AdminConfig.getid(cellname)).splitlines()
dsidlist = []
# remove unwanted databases
for db in dbs:
    dbname = db.split('(')
    n = 0
    for i in dbname:
        # i is only the name of the DataSource, db is DataSource ID!
        if n == 0 and i != "DefaultEJBTimerDataSource" and i != 'OTiSDataSource':
            dsidlist.append(str(db).replace('"',''))
        n += 1

dsidlist.sort()

for dsid in dsidlist:
    propertySet = AdminConfig.showAttribute(dsid,"propertySet")
    propertyList = AdminConfig.list("J2EEResourceProperty", propertySet).splitlines()
    print propertyList


from subprocess import call, check_output
import sys
import os

isPython3 = sys.version_info.major == 3

# https://stackoverflow.com/a/3357357
command = 'git log --format=%B -n 1'.split()
out = check_output(command)

if b'build wheels' not in out.lower() or not isPython3:
    exit(0)

path = os.path.abspath(sys.argv[1])
call('pip install cibuildwheel==0.7.0'.split())
call('cibuildwheel --output-dir {}'.format(sys.argv[1]).split())

call('pip install dropbox'.split())
from dropboxUpload import uploadAll

uploadAll(path)

Fix build wheels and upload 5.

Fix build wheels and upload 5.

Response:
from subprocess import call, check_output
import sys
import os

isPython3 = sys.version_info.major == 3

# https://stackoverflow.com/a/3357357
command = 'git log --format=%B -n 1'.split()
out = check_output(command)

if b'build wheels' not in out.lower() or not isPython3:
    exit(0)

path = os.path.abspath(sys.argv[1])
call('pip install cibuildwheel==0.7.0'.split())
call('cibuildwheel --output-dir {}'.format(sys.argv[1]).split())

call('pip install dropbox'.split())
from dropboxUpload import uploadAll

uploadAll(path)

Prompt:
Fix build wheels and upload 5.

from subprocess import call, check_output
import sys
import os

isPython3 = sys.version_info.major == 3

# https://stackoverflow.com/a/3357357
command = 'git log --format=%B -n 1'.split()
out = check_output(command)

if b'build wheels' not in out.lower() or not isPython3:
    exit(0)

path = os.path.abspath(sys.argv[1])
call('pip install cibuildwheel==0.7.0'.split())
call('cibuildwheel --output-dir {}'.format(sys.argv[1]).split())

from dropboxUpload import uploadAll

uploadAll(path)


# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class SchoolYear(models.Model):
    name = models.CharField(max_length=9)

    def __str__(self):
        return self.name

@python_2_unicode_compatible
class StatsBase(models.Model):
    """
    An abstract model representing stats commonly tracked across all entities
    in TEA data. Meant to be the base used by other apps for establishing
    their stats models.

    Example:

    class CampusStats(StatsBase):
        ...

    """

    # Student counts
    all_students_count = models.IntegerField('Number of students')
    african_american_count = models.IntegerField(
        'Number of African American students')
    asian_count = models.IntegerField('Number of Asian students')
    hispanic_count = models.IntegerField('Number of Hispanic students')
    pacific_islander_count = models.IntegerField(
        'Number of Pacific Islander students')
    two_or_more_races_count = models.IntegerField(
        'Number of Two or More Races students')
    white_count = models.IntegerField('Number of White students')

    class Meta:
        abstract = True

Add two more fields to StatsBase

Add two more fields to StatsBase

Response:
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class SchoolYear(models.Model):
    name = models.CharField(max_length=9)

    def __str__(self):
        return self.name

@python_2_unicode_compatible
class StatsBase(models.Model):
    """
    An abstract model representing stats commonly tracked across all entities
    in TEA data. Meant to be the base used by other apps for establishing
    their stats models.

    Example:

    class CampusStats(StatsBase):
        ...

    """

    # Student counts
    all_students_count = models.IntegerField('Number of students')
    african_american_count = models.IntegerField(
        'Number of African American students')
    asian_count = models.IntegerField('Number of Asian students')
    hispanic_count = models.IntegerField('Number of Hispanic students')
    pacific_islander_count = models.IntegerField(
        'Number of Pacific Islander students')
    two_or_more_races_count = models.IntegerField(
        'Number of Two or More Races students')
    white_count = models.IntegerField('Number of White students')

    class Meta:
        abstract = True

Prompt:
Add two more fields to StatsBase

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class SchoolYear(models.Model):
    name = models.CharField(max_length=9)

    def __str__(self):
        return self.name

@python_2_unicode_compatible
class StatsBase(models.Model):
    """
    An abstract model representing stats commonly tracked across all entities
    in TEA data. Meant to be the base used by other apps for establishing
    their stats models.

    Example:

    class CampusStats(StatsBase):
        ...

    """

    # Student counts
    all_students_count = models.IntegerField('Number of students')
    asian_count = models.IntegerField('Number of Asian students')
    hispanic_count = models.IntegerField('Number of Hispanic students')
    pacific_islander_count = models.IntegerField(
        'Number of Pacific Islander students')
    white_count = models.IntegerField('Number of White students')

    class Meta:
        abstract = True


# gaia_tools.query: some helper functions for querying the Gaia database
import numpy
from astropy.table import Table
from astroquery.gaia import Gaia
import psycopg2

def query(sql_query,local=False,dbname='catalogs',user='postgres'):
    """
    NAME:
       query
    PURPOSE:
       perform a query, either on a local server or on the Gaia archive
    INPUT:
       sql_query - the text of the query
    OUTPUT:
       result
    HISTORY:
       2018-05-02 - Written - Bovy (UofT)
    """
    if local and 'gaiadr2.' in sql_query:
        sql_query= sql_query.replace('gaiadr2.','gdr2_')
    elif not local and 'gdr2_' in sql_query:
        sql_query= sql_query.replace('gdr2_','gaiadr2.')
    if local:
        conn= psycopg2.connect("dbname={} user={}".format(dbname,user))
        cur= conn.cursor()
        cur.execute(sql_query)
        out= cur.fetchall()
        names= [desc[0] for desc in cur.description]
        cur.close()
        conn.close()
        out= Table(numpy.array(out),names=names)
    else:
        job= Gaia.launch_job_async(sql_query)
        out= job.get_results()
    return out

Add function to run a local or remote query

Response:
# gaia_tools.query: some helper functions for querying the Gaia database
import numpy
from astropy.table import Table
from astroquery.gaia import Gaia
import psycopg2

def query(sql_query,local=False,dbname='catalogs',user='postgres'):
    """
    NAME:
       query
    PURPOSE:
       perform a query, either on a local server or on the Gaia archive
    INPUT:
       sql_query - the text of the query
    OUTPUT:
       result
    HISTORY:
       2018-05-02 - Written - Bovy (UofT)
    """
    if local and 'gaiadr2.' in sql_query:
        sql_query= sql_query.replace('gaiadr2.','gdr2_')
    elif not local and 'gdr2_' in sql_query:
        sql_query= sql_query.replace('gdr2_','gaiadr2.')
    if local:
        conn= psycopg2.connect("dbname={} user={}".format(dbname,user))
        cur= conn.cursor()
        cur.execute(sql_query)
        out= cur.fetchall()
        names= [desc[0] for desc in cur.description]
        cur.close()
        conn.close()
        out= Table(numpy.array(out),names=names)
    else:
        job= Gaia.launch_job_async(sql_query)
        out= job.get_results()
    return out

Prompt: Add function to run a local or remote query


import pickle

from flask import Flask, abort
app = Flask(__name__)

import petstore_impl
import petstore_server

store = petstore_impl.PetStore()
server = petstore_server.PetStore_server(store)

@app.route("/" + petstore_server.service_name + "/<args>")
def run_service(args):
    try:
        command, args, kwargs = pickle.loads(args.decode("base64"))
    except Exception:
        abort(400)

    try:
        method = getattr(server, command)
    except AttributeError:
        abort(404)

    try:
        output = True, method(*args, **kwargs)
    except Exception as exc:
        output = False, exc

    return pickle.dumps(output)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080)

Add Flask equivalent for psserver and sample_rpc

Response:
import pickle

from flask import Flask, abort
app = Flask(__name__)

import petstore_impl
import petstore_server

store = petstore_impl.PetStore()
server = petstore_server.PetStore_server(store)

@app.route("/" + petstore_server.service_name + "/<args>")
def run_service(args):
    try:
        command, args, kwargs = pickle.loads(args.decode("base64"))
    except Exception:
        abort(400)

    try:
        method = getattr(server, command)
    except AttributeError:
        abort(404)

    try:
        output = True, method(*args, **kwargs)
    except Exception as exc:
        output = False, exc

    return pickle.dumps(output)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080)

Prompt: Add Flask equivalent for psserver and sample_rpc


"""Computes the least-squares optimal Keplerian elements for a sequence of
   cartesian position observations.
"""

import math
import numpy as np
import matplotlib.pyplot as plt

# convention:
# a: semi-major axis
# e: eccentricity
# eps: mean longitude at epoch
# Euler angles:
# I: inclination
# Omega: longitude of ascending node
# omega: argument of pericenter

#rotation about the z-axis about an angle `ang`
def rotz(ang):
    cos_ang = math.cos(ang)
    sin_ang = math.sin(ang)
    return np.array(((cos_ang,-sin_ang,0.0), (sin_ang, cos_ang,0.0), (0.0,0.0,1.0)))

#rotation about the x-axis about an angle `ang`
def rotx(ang):
    cos_ang = math.cos(ang)
    sin_ang = math.sin(ang)
    return np.array(((1.0,0.0,0.0), (0.0,cos_ang,-sin_ang), (0.0,sin_ang,cos_ang)))

#rotation from the orbital plane to the inertial frame
#it is composed of the following rotations, in that order:
#1) rotation about the z axis about an angle `omega` (argument of pericenter)
#2) rotation about the x axis about an angle `I` (inclination)
#3) rotation about the z axis about an angle `Omega` (longitude of ascending node)
def op2if(omega,I,Omega):
    P2_mul_P3 = np.matmul(rotx(I),rotz(omega))
    return np.matmul(rotz(Omega),P2_mul_P3)

omega = math.radians(31.124)
I = math.radians(75.0)
Omega = math.radians(60.0)

# rotation matrix from orbital plane to inertial frame
# two ways to compute it; result should be the same
P_1 = rotz(omega) #rotation about z axis by an angle `omega`
P_2 = rotx(I) #rotation about x axis by an angle `I`
P_3 = rotz(Omega) #rotation about z axis by an angle `Omega`

Rot1 = np.matmul(P_3,np.matmul(P_2,P_1))
Rot2 = op2if(omega,I,Omega)

v = np.array((3.0,-2.0,1.0))

print(I)
print(omega)
print(Omega)

print(Rot1)

print(np.matmul(Rot1,v))

print(Rot2)

Response:
"""Computes the least-squares optimal Keplerian elements for a sequence of
   cartesian position observations.
"""

import math
import numpy as np
import matplotlib.pyplot as plt

# convention:
# a: semi-major axis
# e: eccentricity
# eps: mean longitude at epoch
# Euler angles:
# I: inclination
# Omega: longitude of ascending node
# omega: argument of pericenter

#rotation about the z-axis about an angle `ang`
def rotz(ang):
    cos_ang = math.cos(ang)
    sin_ang = math.sin(ang)
    return np.array(((cos_ang,-sin_ang,0.0), (sin_ang, cos_ang,0.0), (0.0,0.0,1.0)))

#rotation about the x-axis about an angle `ang`
def rotx(ang):
    cos_ang = math.cos(ang)
    sin_ang = math.sin(ang)
    return np.array(((1.0,0.0,0.0), (0.0,cos_ang,-sin_ang), (0.0,sin_ang,cos_ang)))

#rotation from the orbital plane to the inertial frame
#it is composed of the following rotations, in that order:
#1) rotation about the z axis about an angle `omega` (argument of pericenter)
#2) rotation about the x axis about an angle `I` (inclination)
#3) rotation about the z axis about an angle `Omega` (longitude of ascending node)
def op2if(omega,I,Omega):
    P2_mul_P3 = np.matmul(rotx(I),rotz(omega))
    return np.matmul(rotz(Omega),P2_mul_P3)

omega = math.radians(31.124)
I = math.radians(75.0)
Omega = math.radians(60.0)

# rotation matrix from orbital plane to inertial frame
# two ways to compute it; result should be the same
P_1 = rotz(omega) #rotation about z axis by an angle `omega`
P_2 = rotx(I) #rotation about x axis by an angle `I`
P_3 = rotz(Omega) #rotation about z axis by an angle `Omega`

Rot1 = np.matmul(P_3,np.matmul(P_2,P_1))
Rot2 = op2if(omega,I,Omega)

v = np.array((3.0,-2.0,1.0))

print(I)
print(omega)
print(Omega)

print(Rot1)

print(np.matmul(Rot1,v))

print(Rot2)

Prompt:
Add rotation matrix, from orbital plane to inertial frame

"""Computes the least-squares optimal Keplerian elements for a sequence of
   cartesian position observations.
"""

import numpy as np
import matplotlib.pyplot as plt

# convention:
# a: semi-major axis
# e: eccentricity
# eps: mean longitude at epoch
# Euler angles:
# I: inclination
# Omega: longitude of ascending node
# omega: argument of pericenter


''' imports from installed packages '''
from django.core.management.base import BaseCommand, CommandError

from django_mongokit import get_database

try:
    from bson import ObjectId
except ImportError:  # old pymongo
    from pymongo.objectid import ObjectId

''' imports from application folders/files '''
from gnowsys_ndf.ndf.models import Node, Triple, HistoryManager
from gnowsys_ndf.ndf.rcslib import RCS

####################################################################################################################

class Command(BaseCommand):
    """This update-script updates attribute_type field's value (of all GAttribute instances) from ObjectId to it's corresponding DBRef; and also creates it's version-file(history) in rcs-repo.
    """

    help = "\tThis update-script updates attribute_type field's value (of all GAttribute instances) from ObjectId to it's corresponding DBRef; and also creates it's version-file(history) in rcs-repo."

    def handle(self, *args, **options):

        history_manager = HistoryManager()
        rcs_obj = RCS()

        collection = get_database()[Triple.collection_name]
        cur = collection.Triple.find( {'_type': 'GAttribute'} )

        for n in cur:
            if type(n.attribute_type) == ObjectId:
                # Retrieving attribute-type document(or object) from it's ObjectId stored in GAttribute's attribute_type field
                attr_type = collection.Node.one( {'_id': n.attribute_type} )

                # Replacing GAttribute's attribute_type field value from ObjectId to DBRef
                n.attribute_type = attr_type

                subject_doc = collection.Node.one({'_id': n.subject})

                n.name = subject_doc.name + " -- " + n.attribute_type['name'] + " -- " + n.object_value

                if history_manager.create_or_replace_json_file(n):
                    fp = history_manager.get_file_path(n)
                    message = "This document (" + n.name + ") is created on " + subject_doc.created_at.strftime("%d %B %Y")
                    rcs_obj.checkin(fp, 1, message.encode('utf-8'), "-i")

                n.save()

        # --- End of handle() ---

Update script for updating attribute_type field of GAttribute instances from ObjectId to DBRef (AttributeType)

Response:
''' imports from installed packages '''
from django.core.management.base import BaseCommand, CommandError

from django_mongokit import get_database

try:
    from bson import ObjectId
except ImportError:  # old pymongo
    from pymongo.objectid import ObjectId

''' imports from application folders/files '''
from gnowsys_ndf.ndf.models import Node, Triple, HistoryManager
from gnowsys_ndf.ndf.rcslib import RCS

####################################################################################################################

class Command(BaseCommand):
    """This update-script updates attribute_type field's value (of all GAttribute instances) from ObjectId to it's corresponding DBRef; and also creates it's version-file(history) in rcs-repo.
    """

    help = "\tThis update-script updates attribute_type field's value (of all GAttribute instances) from ObjectId to it's corresponding DBRef; and also creates it's version-file(history) in rcs-repo."

    def handle(self, *args, **options):

        history_manager = HistoryManager()
        rcs_obj = RCS()

        collection = get_database()[Triple.collection_name]
        cur = collection.Triple.find( {'_type': 'GAttribute'} )

        for n in cur:
            if type(n.attribute_type) == ObjectId:
                # Retrieving attribute-type document(or object) from it's ObjectId stored in GAttribute's attribute_type field
                attr_type = collection.Node.one( {'_id': n.attribute_type} )

                # Replacing GAttribute's attribute_type field value from ObjectId to DBRef
                n.attribute_type = attr_type

                subject_doc = collection.Node.one({'_id': n.subject})

                n.name = subject_doc.name + " -- " + n.attribute_type['name'] + " -- " + n.object_value

                if history_manager.create_or_replace_json_file(n):
                    fp = history_manager.get_file_path(n)
                    message = "This document (" + n.name + ") is created on " + subject_doc.created_at.strftime("%d %B %Y")
                    rcs_obj.checkin(fp, 1, message.encode('utf-8'), "-i")

                n.save()

        # --- End of handle() ---

Prompt: Update script for updating attribute_type field of GAttribute instances from ObjectId to DBRef (AttributeType)


import json
import sys
import re

def parse(version, endpoint_path):
    """Map an HTTP endpoint path to an API path"""

    with open('opendc/api/{}/paths.json'.format(version)) as paths_file:
        paths = json.load(paths_file)

    endpoint_path_parts = endpoint_path.strip('/').split('/')
    paths_parts = [x.split('/') for x in paths if len(x.split('/')) == len(endpoint_path_parts)]

    for path_parts in paths_parts:
        found = True

        for (endpoint_part, part) in zip(endpoint_path_parts, path_parts):
            if not part.startswith('{') and endpoint_part != part:
                found = False
                break

        if found:
            sys.stdout.flush()
            return '{}/{}'.format(version, '/'.join(path_parts))

    return None

Make path parser robust to trailing /

Make path parser robust to trailing /

Response:
import json
import sys
import re

def parse(version, endpoint_path):
    """Map an HTTP endpoint path to an API path"""

    with open('opendc/api/{}/paths.json'.format(version)) as paths_file:
        paths = json.load(paths_file)

    endpoint_path_parts = endpoint_path.strip('/').split('/')
    paths_parts = [x.split('/') for x in paths if len(x.split('/')) == len(endpoint_path_parts)]

    for path_parts in paths_parts:
        found = True

        for (endpoint_part, part) in zip(endpoint_path_parts, path_parts):
            if not part.startswith('{') and endpoint_part != part:
                found = False
                break

        if found:
            sys.stdout.flush()
            return '{}/{}'.format(version, '/'.join(path_parts))

    return None

Prompt:
Make path parser robust to trailing /

import json
import sys
import re

def parse(version, endpoint_path):
    """Map an HTTP call to an API path"""

    with open('opendc/api/{}/paths.json'.format(version)) as paths_file:
        paths = json.load(paths_file)

    endpoint_path_parts = endpoint_path.split('/')
    paths_parts = [x.split('/') for x in paths if len(x.split('/')) == len(endpoint_path_parts)]

    for path_parts in paths_parts:
        found = True

        for (endpoint_part, part) in zip(endpoint_path_parts, path_parts):
            print endpoint_part, part
            if not part.startswith('{') and endpoint_part != part:
                found = False
                break

        if found:
            sys.stdout.flush()
            return '{}/{}'.format(version, '/'.join(path_parts))

    return None


import os

from appconf import AppConf
from django.conf import settings

class DjExperimentAppConf(AppConf):
    DATA_DIR = "./"
    BASE_DATA_DIR = os.path.join(settings.BASE_DIR, 'data')
    SEPARATOR = "."
    OUTPUT_PREFIX = ""
    OUTPUT_SUFFIX = ".nc"
    CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672//'
    CELERY_RESULT_BACKEND = 'rpc://'

    class Meta:
        prefix = 'dj_experiment'
        holder = 'dj_experiment.conf.settings'

Response:
import os

from appconf import AppConf
from django.conf import settings

class DjExperimentAppConf(AppConf):
    DATA_DIR = "./"
    BASE_DATA_DIR = os.path.join(settings.BASE_DIR, 'data')
    SEPARATOR = "."
    OUTPUT_PREFIX = ""
    OUTPUT_SUFFIX = ".nc"
    CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672//'
    CELERY_RESULT_BACKEND = 'rpc://'

    class Meta:
        prefix = 'dj_experiment'
        holder = 'dj_experiment.conf.settings'

Prompt:
Add default base data dir for experiments

from appconf import AppConf
from django.conf import settings

class DjExperimentAppConf(AppConf):
    DATA_DIR = "./"
    SEPARATOR = "."
    OUTPUT_PREFIX = ""
    OUTPUT_SUFFIX = ".nc"
    CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672//'
    CELERY_RESULT_BACKEND = 'rpc://'

    class Meta:
        prefix = 'dj_experiment'
        holder = 'dj_experiment.conf.settings'