# -*- coding: utf-8 -*-
# Copyright 2016 Ionuț Arțăriși <ionut@artarisi.eu>

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = '0.1.1'

def undecorated(o):
    """Remove all decorators from a function, method or class"""
    # class decorator
    if type(o) is type:
        return o

    try:
        # python2
        closure = o.func_closure
    except AttributeError:
        pass

    try:
        # python3
        closure = o.__closure__
    except AttributeError:
        return

    if closure:
        for cell in closure:
            # avoid infinite recursion
            if cell.cell_contents is o:
                continue

            undecd = undecorated(cell.cell_contents)
            if undecd:
                return undecd
    else:
        return o

Response:
# -*- coding: utf-8 -*-
# Copyright 2016 Ionuț Arțăriși <ionut@artarisi.eu>

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = '0.1.1'

def undecorated(o):
    """Remove all decorators from a function, method or class"""
    # class decorator
    if type(o) is type:
        return o

    try:
        # python2
        closure = o.func_closure
    except AttributeError:
        pass

    try:
        # python3
        closure = o.__closure__
    except AttributeError:
        return

    if closure:
        for cell in closure:
            # avoid infinite recursion
            if cell.cell_contents is o:
                continue

            undecd = undecorated(cell.cell_contents)
            if undecd:
                return undecd
    else:
        return o

Prompt:
Remove module docstring as we have it on the func

# -*- coding: utf-8 -*-
# Copyright 2016 Ionuț Arțăriși <ionut@artarisi.eu>

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Return a function with any decorators removed """

__version__ = '0.1.1'

def undecorated(o):
    """Remove all decorators from a function, method or class"""
    # class decorator
    if type(o) is type:
        return o

    try:
        # python2
        closure = o.func_closure
    except AttributeError:
        pass

    try:
        # python3
        closure = o.__closure__
    except AttributeError:
        return

    if closure:
        for cell in closure:
            # avoid infinite recursion
            if cell.cell_contents is o:
                continue

            undecd = undecorated(cell.cell_contents)
            if undecd:
                return undecd
    else:
        return o


#!/usr/bin/env python

'''
Script: fy_print_seq_len_in_fasta.py
Function: Print sequence length to STDOUT in fasta file
Note: Python3 is not default installed for most computer,
      and the extra-installed module like Biopython could
      not be directly used by python3. So, it's not the
      righ time to use Python3 now.
Date: 2014/11/11
'''

import sys
if len(sys.argv) < 2:
    print('Usage: ' + sys.argv[0] + ' <FASTA>')
    sys.exit()

from Bio import SeqIO
seqlen     = []
num_of_seq = 0
total_len  = 0
for record in SeqIO.parse(sys.argv[1], 'fasta'):
    print("%s %i" % (record.id, len(record)))
    num_of_seq += 1
    total_len += len(record)
    seqlen.append(len(record))

seqlen.sort()
min_len = seqlen[0]
max_len = seqlen[-1]

print('Number of sequences: ' + str(num_of_seq))
print('Total length: ' + str(total_len))
print('Max length: ' + str(max_len))
print('Min length: ' + str(min_len))

Response:
#!/usr/bin/env python

'''
Script: fy_print_seq_len_in_fasta.py
Function: Print sequence length to STDOUT in fasta file
Note: Python3 is not default installed for most computer,
      and the extra-installed module like Biopython could
      not be directly used by python3. So, it's not the
      righ time to use Python3 now.
Date: 2014/11/11
'''

import sys
if len(sys.argv) < 2:
    print('Usage: ' + sys.argv[0] + ' <FASTA>')
    sys.exit()

from Bio import SeqIO
seqlen     = []
num_of_seq = 0
total_len  = 0
for record in SeqIO.parse(sys.argv[1], 'fasta'):
    print("%s %i" % (record.id, len(record)))
    num_of_seq += 1
    total_len += len(record)
    seqlen.append(len(record))

seqlen.sort()
min_len = seqlen[0]
max_len = seqlen[-1]

print('Number of sequences: ' + str(num_of_seq))
print('Total length: ' + str(total_len))
print('Max length: ' + str(max_len))
print('Min length: ' + str(min_len))

Prompt:
Use single quotes instead of double quotes

#!/usr/bin/env python

'''
Script: fy_print_seq_len_in_fasta.py
Function: Print sequence length to STDOUT in fasta file
Note: Python3 is not default installed for most computer,
      and the extra-installed module like Biopython could
      not be directly used by python3. So, it's not the
      righ time to use Python3 now.
Date: 2014/11/11
'''

import sys
if len(sys.argv) < 2:
    print('Usage: ' + sys.argv[0] + ' <FASTA>')
    sys.exit()

from Bio import SeqIO
seqlen     = []
num_of_seq = 0
total_len  = 0
for record in SeqIO.parse(sys.argv[1], 'fasta'):
    print("%s %i" % (record.id, len(record)))
    num_of_seq += 1
    total_len += len(record)
    seqlen.append(len(record))

seqlen.sort()
min_len = seqlen[0]
max_len = seqlen[-1]

print("Number of sequences: " + str(num_of_seq))
print("Total length: " + str(total_len))
print("Max length: " + str(max_len))
print("Min length: " + str(min_len))


Use clear cart method from satchless

Response:
from __future__ import unicode_literals

from django.utils.translation import pgettext
from satchless import cart
from satchless.item import ItemList, ClassifyingPartitioner

from ..product.models import DigitalShip

class ShippedGroup(ItemList):
    '''
    Group for shippable products.
    '''
    pass

class DigitalGroup(ItemList):
    '''
    Group for digital products.
    '''
    pass

class CartPartitioner(ClassifyingPartitioner):
    '''
    Dividing cart into groups.
    '''
    def classify(self, item):
        if isinstance(item.product, DigitalShip):
            return 'digital'
        return 'shippable'

    def get_partition(self, classifier, items):
        if classifier == 'digital':
            return DigitalGroup(items)
        return ShippedGroup(items)

class Cart(cart.Cart):
    '''
    Contains cart items. Serialized instance of cart is saved into django
    session.
    '''
    timestamp = None
    billing_address = None

    def __unicode__(self):
        return pgettext(
            'Shopping cart',
            'Your cart (%(cart_count)s)') % {'cart_count': self.count()}

Prompt:
Use clear cart method from satchless

https://github.com/mirumee/satchless/commit/3acaa8f6a27d9ab259a2d66fc3f7416a18fab1ad

This reverts commit 2ad16c44adb20e9ba023e873149d67068504c34c.

from __future__ import unicode_literals

from django.utils.translation import pgettext
from satchless import cart
from satchless.item import ItemList, ClassifyingPartitioner

from ..product.models import DigitalShip

class ShippedGroup(ItemList):
    '''
    Group for shippable products.
    '''
    pass

class DigitalGroup(ItemList):
    '''
    Group for digital products.
    '''
    pass

class CartPartitioner(ClassifyingPartitioner):
    '''
    Dividing cart into groups.
    '''
    def classify(self, item):
        if isinstance(item.product, DigitalShip):
            return 'digital'
        return 'shippable'

    def get_partition(self, classifier, items):
        if classifier == 'digital':
            return DigitalGroup(items)
        return ShippedGroup(items)

class Cart(cart.Cart):
    '''
    Contains cart items. Serialized instance of cart is saved into django
    session.
    '''
    timestamp = None
    billing_address = None

    def __unicode__(self):
        return pgettext(
            'Shopping cart',
            'Your cart (%(cart_count)s)') % {'cart_count': self.count()}

    def clear(self):
        self._state = []


from sqlalchemy import *
from migrate import *

meta = MetaData()

def upgrade(migrate_engine):
    meta.bind = migrate_engine
    dataset = Table('dataset', meta, autoload=True)

    serp_title = Column('serp_title', Unicode())
    serp_title.create(dataset)

    serp_teaser = Column('serp_teaser', Unicode())
    serp_teaser.create(dataset)

Migrate in domain model changes

Migrate in domain model changes

Response:
from sqlalchemy import *
from migrate import *

meta = MetaData()

def upgrade(migrate_engine):
    meta.bind = migrate_engine
    dataset = Table('dataset', meta, autoload=True)

    serp_title = Column('serp_title', Unicode())
    serp_title.create(dataset)

    serp_teaser = Column('serp_teaser', Unicode())
    serp_teaser.create(dataset)

Prompt: Migrate in domain model changes


from setuptools import setup

setup(
    name='polygraph',
    version='0.1.0',
    description='Python library for defining GraphQL schemas',
    url='https://github.com/yen223/polygraph/',
    author='Wei Yen, Lee',
    author_email='hello@weiyen.net',
    license='MIT',
    install_requires=[
        'marshmallow>=3.0.0b2',
        'graphql-core>=1.0.1',
    ],
    extras_require={
        'dev': [
            'flake8',
            'ipython',
            'autopep8',
            'isort',
        ]
    }
)

Add isort as a development requirement

Add isort as a development requirement

Response:
from setuptools import setup

setup(
    name='polygraph',
    version='0.1.0',
    description='Python library for defining GraphQL schemas',
    url='https://github.com/yen223/polygraph/',
    author='Wei Yen, Lee',
    author_email='hello@weiyen.net',
    license='MIT',
    install_requires=[
        'marshmallow>=3.0.0b2',
        'graphql-core>=1.0.1',
    ],
    extras_require={
        'dev': [
            'flake8',
            'ipython',
            'autopep8',
            'isort',
        ]
    }
)

Prompt:
Add isort as a development requirement

from setuptools import setup

setup(
    name='polygraph',
    version='0.1.0',
    description='Python library for defining GraphQL schemas',
    url='https://github.com/yen223/polygraph/',
    author='Wei Yen, Lee',
    author_email='hello@weiyen.net',
    license='MIT',
    install_requires=[
        'marshmallow>=3.0.0b2',
        'graphql-core>=1.0.1',
    ],
    extras_require={
        'dev': [
            'flake8',
            'ipython',
            'autopep8',
        ]
    }
)


class Collector(object):
    def __init__(self):
        self._groups = []
        self._current_group = None

    def add_input(self, new_input):
        if self._current_group is None:
            self._current_group = []
            self._groups.append(self._current_group)
        self._current_group.append(new_input)

    def finalise_group(self):
        self._current_group = None

    @property
    def groups(self):
        return self._groups

    @property
    def current_group(self):
        return self._current_group

    def __str__(self):
        temp = ""
        for group in self._groups:
            for text in group:
                temp += text + "\n"
            temp += "\n"
        return temp

Response:
class Collector(object):
    def __init__(self):
        self._groups = []
        self._current_group = None

    def add_input(self, new_input):
        if self._current_group is None:
            self._current_group = []
            self._groups.append(self._current_group)
        self._current_group.append(new_input)

    def finalise_group(self):
        self._current_group = None

    @property
    def groups(self):
        return self._groups

    @property
    def current_group(self):
        return self._current_group

    def __str__(self):
        temp = ""
        for group in self._groups:
            for text in group:
                temp += text + "\n"
            temp += "\n"
        return temp

Prompt:
Fix bug with collector losing last group of input

This means the script runner doesn't have to manually finalise the last
input, which was always a bit silly. In fact, the whole metaphor is
rather silly. I should change it to be "start new group" instead.

class Collector(object):
    def __init__(self):
        self._groups = []
        self._current_group = None

    def add_input(self, new_input):
        if self._current_group is None:
            self._current_group = []
        self._current_group.append(new_input)

    def finalise_group(self):
        self._groups.append(self._current_group)
        self._current_group = None

    @property
    def groups(self):
        return self._groups

    @property
    def current_group(self):
        return self._current_group

    def __str__(self):
        temp = ""
        for group in self._groups:
            for text in group:
                temp += text + "\n"
            temp += "\n"
        return temp


from django.conf import settings
from django.conf.urls import static
from django.contrib import admin
from django.urls import path

admin.autodiscover()

urlpatterns = [
    path("admin/", admin.site.urls),
]
urlpatterns += static.static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Fix correction to comply with black

Fix correction to comply with black

Response:
from django.conf import settings
from django.conf.urls import static
from django.contrib import admin
from django.urls import path

admin.autodiscover()

urlpatterns = [
    path("admin/", admin.site.urls),
]
urlpatterns += static.static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Prompt:
Fix correction to comply with black

from django.conf import settings
from django.conf.urls import static
from django.contrib import admin
from django.urls import path

admin.autodiscover()

urlpatterns = [
    path('admin/', admin.site.urls),
]
urlpatterns += static.static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


from branca.element import Figure, Element, JavascriptLink, CssLink

class JSCSSMixin(Element):
    """Render links to external Javascript and CSS resources."""

    default_js = []
    default_css = []

    def render(self, **kwargs):
        figure = self.get_root()
        assert isinstance(figure, Figure), ('You cannot render this Element '
                                            'if it is not in a Figure.')

        for name, url in self.default_js:
            figure.header.add_child(JavascriptLink(url), name=name)

        for name, url in self.default_css:
            figure.header.add_child(CssLink(url), name=name)

        super().render(**kwargs)

Add mixin to render JS and CSS links

Add mixin to render JS and CSS links

Response:
from branca.element import Figure, Element, JavascriptLink, CssLink

class JSCSSMixin(Element):
    """Render links to external Javascript and CSS resources."""

    default_js = []
    default_css = []

    def render(self, **kwargs):
        figure = self.get_root()
        assert isinstance(figure, Figure), ('You cannot render this Element '
                                            'if it is not in a Figure.')

        for name, url in self.default_js:
            figure.header.add_child(JavascriptLink(url), name=name)

        for name, url in self.default_css:
            figure.header.add_child(CssLink(url), name=name)

        super().render(**kwargs)

Prompt: Add mixin to render JS and CSS links


import sys
import os.path
import pprint
sys.path.append(os.path.abspath(__file__ + "\..\.."))

import windows
import windows.test
import windows.debug

import windows.native_exec.simple_x86 as x86
from windows.generated_def.winstructs import *

class MyDebugger(windows.debug.Debugger):
    def __init__(self, *args, **kwargs):
        super(MyDebugger, self).__init__(*args, **kwargs)
        self.single_step_counter = 0

    def on_exception(self, exception):
        code = exception.ExceptionRecord.ExceptionCode
        addr = exception.ExceptionRecord.ExceptionAddress
        print("Got exception {0} at 0x{1:x}".format(code, addr))

    def on_single_step(self, exception):
        code = exception.ExceptionRecord.ExceptionCode
        addr = exception.ExceptionRecord.ExceptionAddress
        print("Got single_step {0} at 0x{1:x}".format(code, addr))
        self.single_step_counter -= 1
        if self.single_step_counter > 0:
            return self.single_step()

class SingleStepOnWrite(windows.debug.MemoryBreakpoint):
    DEFAULT_PROTECT = PAGE_READONLY
    """Check that BP/dbg can trigger single step and that instruction follows"""
    def trigger(self, dbg, exc):
        fault_addr = exc.ExceptionRecord.ExceptionInformation[1]
        eip = dbg.current_thread.context.pc
        print("Instruction at <{0:#x}> wrote at <{1:#x}>".format(eip, fault_addr))
        dbg.single_step_counter = 4
        return dbg.single_step()

calc = windows.test.pop_calc_32(dwCreationFlags=DEBUG_PROCESS)
d = MyDebugger(calc)

code = calc.virtual_alloc(0x1000)
data = calc.virtual_alloc(0x1000)

injected = x86.MultipleInstr()
injected += x86.Mov("EAX", 0)
injected += x86.Mov(x86.deref(data), "EAX")
injected += x86.Add("EAX", 4)
injected += x86.Mov(x86.deref(data + 4), "EAX")
injected += x86.Nop()
injected += x86.Nop()
injected += x86.Ret()

calc.write_memory(code, injected.get_code())
d.add_bp(SingleStepOnWrite(data, size=0x1000))
calc.create_thread(code, 0)
d.loop()

Add a sample on memoryBP + singlestep (cc Heurs :D)

Response:
import sys
import os.path
import pprint
sys.path.append(os.path.abspath(__file__ + "\..\.."))

import windows
import windows.test
import windows.debug

import windows.native_exec.simple_x86 as x86
from windows.generated_def.winstructs import *

class MyDebugger(windows.debug.Debugger):
    def __init__(self, *args, **kwargs):
        super(MyDebugger, self).__init__(*args, **kwargs)
        self.single_step_counter = 0

    def on_exception(self, exception):
        code = exception.ExceptionRecord.ExceptionCode
        addr = exception.ExceptionRecord.ExceptionAddress
        print("Got exception {0} at 0x{1:x}".format(code, addr))

    def on_single_step(self, exception):
        code = exception.ExceptionRecord.ExceptionCode
        addr = exception.ExceptionRecord.ExceptionAddress
        print("Got single_step {0} at 0x{1:x}".format(code, addr))
        self.single_step_counter -= 1
        if self.single_step_counter > 0:
            return self.single_step()

class SingleStepOnWrite(windows.debug.MemoryBreakpoint):
    DEFAULT_PROTECT = PAGE_READONLY
    """Check that BP/dbg can trigger single step and that instruction follows"""
    def trigger(self, dbg, exc):
        fault_addr = exc.ExceptionRecord.ExceptionInformation[1]
        eip = dbg.current_thread.context.pc
        print("Instruction at <{0:#x}> wrote at <{1:#x}>".format(eip, fault_addr))
        dbg.single_step_counter = 4
        return dbg.single_step()

calc = windows.test.pop_calc_32(dwCreationFlags=DEBUG_PROCESS)
d = MyDebugger(calc)

code = calc.virtual_alloc(0x1000)
data = calc.virtual_alloc(0x1000)

injected = x86.MultipleInstr()
injected += x86.Mov("EAX", 0)
injected += x86.Mov(x86.deref(data), "EAX")
injected += x86.Add("EAX", 4)
injected += x86.Mov(x86.deref(data + 4), "EAX")
injected += x86.Nop()
injected += x86.Nop()
injected += x86.Ret()

calc.write_memory(code, injected.get_code())
d.add_bp(SingleStepOnWrite(data, size=0x1000))
calc.create_thread(code, 0)
d.loop()

Prompt: Add a sample on memoryBP + singlestep (cc Heurs :D)


class Calendar:
    def __init__(self, year=2015):
        self.year = year  # TODO get current year
        self.dates = []
        for month in range(1, 13):
            self.insert_dates(month)

    def insert_dates(self, month):
        days = 28
        if month in [1, 4, 6, 9, 11]:
            days = 30
        if month in [3, 5, 7, 8, 10, 12]:
            days = 31
        print days
        self.create_dates(days, month)

    def create_dates(self, days, month):
        for day in range(1, days + 1):
            date = Date(day=day, month=month)
            date.write_into(self.dates)

class Person:
    def __init__(self, name, birth_date):
        self.name = name
        self.birth_date = birth_date

class BirthDate:
    def __init__(self, year, date):
        self.year = year
        self.date = date

class Date:
    def __init__(self, day, month):
        self.day = day
        self.month = month

    def write_into(self, list):
        list.append(self)

Create Calender with list of all dates

Create Calender with list of all dates

Response:
class Calendar:
    def __init__(self, year=2015):
        self.year = year  # TODO get current year
        self.dates = []
        for month in range(1, 13):
            self.insert_dates(month)

    def insert_dates(self, month):
        days = 28
        if month in [1, 4, 6, 9, 11]:
            days = 30
        if month in [3, 5, 7, 8, 10, 12]:
            days = 31
        print days
        self.create_dates(days, month)

    def create_dates(self, days, month):
        for day in range(1, days + 1):
            date = Date(day=day, month=month)
            date.write_into(self.dates)

class Person:
    def __init__(self, name, birth_date):
        self.name = name
        self.birth_date = birth_date

class BirthDate:
    def __init__(self, year, date):
        self.year = year
        self.date = date

class Date:
    def __init__(self, day, month):
        self.day = day
        self.month = month

    def write_into(self, list):
        list.append(self)

Prompt:
Create Calender with list of all dates

class Person:
    def __init__(self, name, birth_date):
        self.name = name
        self.birth_date = birth_date

class BirthDate:
    def __init__(self, year, date):
        self.year = year
        self.date = date

class Date:
    def __init__(self, day, month):
        self.day = day
        self.month = month

    def write_into(self, list):
        list.append(self)


Remove blank line at end of file.

Remove blank line at end of file.

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

"""
    Property Test: orchard.app
"""

import hypothesis
import hypothesis.strategies as st
import unittest

import orchard

class AppPropertyTest(unittest.TestCase):

    def setUp(self):
        self.app_context = orchard.app.app_context()
        self.app_context.push()
        self.client = orchard.app.test_client(use_cookies = True)

    def tearDown(self):
        self.app_context.pop()

    @hypothesis.given(name = st.text(alphabet = ['a', 'b', 'c', 'A', 'B', 'C']))
    def test_index(self, name):
        response = self.client.get('/{name}'.format(name = name))
        data = response.get_data(as_text = True)
        self.assertEqual(response.status_code, 200)
        self.assertTrue(name in data)

Prompt:
Remove blank line at end of file.

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

"""
    Property Test: orchard.app
"""

import hypothesis
import hypothesis.strategies as st
import unittest

import orchard

class AppPropertyTest(unittest.TestCase):

    def setUp(self):
        self.app_context = orchard.app.app_context()
        self.app_context.push()
        self.client = orchard.app.test_client(use_cookies = True)

    def tearDown(self):
        self.app_context.pop()

    @hypothesis.given(name = st.text(alphabet = ['a', 'b', 'c', 'A', 'B', 'C']))
    def test_index(self, name):
        response = self.client.get('/{name}'.format(name = name))
        data = response.get_data(as_text = True)
        self.assertEqual(response.status_code, 200)
        self.assertTrue(name in data)


from django.contrib import admin
from game.models import *

class LocationAdmin(admin.ModelAdmin):
    model = Location

admin.site.register(Location, LocationAdmin)

class DetailAdmin(admin.ModelAdmin):

    model = Detail

admin.site.register(Detail, DetailAdmin)

Add models to Admin Panel

Add models to Admin Panel

Response:
from django.contrib import admin
from game.models import *

class LocationAdmin(admin.ModelAdmin):
    model = Location

admin.site.register(Location, LocationAdmin)

class DetailAdmin(admin.ModelAdmin):

    model = Detail

admin.site.register(Detail, DetailAdmin)

Prompt:
Add models to Admin Panel

from django.contrib import admin

# Register your models here.


# Copyright (c) 2021 elParaguayo
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from libqtile import widget

RESPONSE = "London: +17°C"

def test_wttr_methods():
    wttr = widget.Wttr(location={"London": "Home"})

    assert wttr._get_url() == "https://wttr.in/London?m&format=3&lang=en"
    assert wttr.parse(RESPONSE) == "Home: +17°C"

def test_wttr_no_location():
    wttr = widget.Wttr()
    assert wttr._get_url() is None

Add test for wttr widget

Add test for wttr widget

Response:
# Copyright (c) 2021 elParaguayo
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from libqtile import widget

RESPONSE = "London: +17°C"

def test_wttr_methods():
    wttr = widget.Wttr(location={"London": "Home"})

    assert wttr._get_url() == "https://wttr.in/London?m&format=3&lang=en"
    assert wttr.parse(RESPONSE) == "Home: +17°C"

def test_wttr_no_location():
    wttr = widget.Wttr()
    assert wttr._get_url() is None

Prompt: Add test for wttr widget


# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Test testing utilities
"""

import os
import warnings
from nipype.testing.utils import TempFATFS
from nose.tools import assert_true

def test_tempfatfs():
    try:
        fatfs = TempFATFS()
    except IOError:
        warnings.warn("Cannot mount FAT filesystems with FUSE")
    else:
        with fatfs as tmpdir:
            yield assert_true, os.path.exists(tmpdir)

Add warning for TempFATFS test

TEST: Add warning for TempFATFS test

Response:
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Test testing utilities
"""

import os
import warnings
from nipype.testing.utils import TempFATFS
from nose.tools import assert_true

def test_tempfatfs():
    try:
        fatfs = TempFATFS()
    except IOError:
        warnings.warn("Cannot mount FAT filesystems with FUSE")
    else:
        with fatfs as tmpdir:
            yield assert_true, os.path.exists(tmpdir)

Prompt:
TEST: Add warning for TempFATFS test

# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Test testing utilities
"""

from nipype.testing.utils import TempFATFS
from nose.tools import assert_true

def test_tempfatfs():
    with TempFATFS() as tmpdir:
        yield assert_true, tmpdir is not None


from distutils.core import setup
import sslserver

setup(name="django-sslserver",
      version=sslserver.__version__,
      author="Ted Dziuba",
      author_email="tjdziuba@gmail.com",
      description="An SSL-enabled development server for Django",
      url="https://github.com/teddziuba/django-sslserver",
      packages=["sslserver",
                "sslserver.management",
                "sslserver.management.commands"],
      package_dir={"sslserver": "sslserver"},
      package_data={"sslserver": ["certs/development.crt",
                                  "certs/development.key",
                                  "certs/server.csr"]},
      install_requires=["setuptools",
                        "Django >= 1.8"],
      license="MIT"
      )

Response:
from distutils.core import setup
import sslserver

setup(name="django-sslserver",
      version=sslserver.__version__,
      author="Ted Dziuba",
      author_email="tjdziuba@gmail.com",
      description="An SSL-enabled development server for Django",
      url="https://github.com/teddziuba/django-sslserver",
      packages=["sslserver",
                "sslserver.management",
                "sslserver.management.commands"],
      package_dir={"sslserver": "sslserver"},
      package_data={"sslserver": ["certs/development.crt",
                                  "certs/development.key",
                                  "certs/server.csr"]},
      install_requires=["setuptools",
                        "Django >= 1.8"],
      license="MIT"
      )

Prompt:
Fix Django dependency: update Django version from 1.4 to 1.8

from distutils.core import setup
import sslserver

setup(name="django-sslserver",
      version=sslserver.__version__,
      author="Ted Dziuba",
      author_email="tjdziuba@gmail.com",
      description="An SSL-enabled development server for Django",
      url="https://github.com/teddziuba/django-sslserver",
      packages=["sslserver",
                "sslserver.management",
                "sslserver.management.commands"],
      package_dir={"sslserver": "sslserver"},
      package_data={"sslserver": ["certs/development.crt",
                                  "certs/development.key",
                                  "certs/server.csr"]},
      install_requires=["setuptools",
                        "Django >= 1.4"],
      license="MIT"
      )


import six

from autotranslate.compat import importlib
from django.conf import settings

def perform_import(val, setting_name):
    """
    If the given setting is a string import notation,
    then perform the necessary import or imports.
    Credits: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/settings.py#L138
    """
    if val is None:
        return None
    elif isinstance(val, six.string_types):
        return import_from_string(val, setting_name)
    elif isinstance(val, (list, tuple)):
        return [import_from_string(item, setting_name) for item in val]
    return val

def import_from_string(val, setting_name):
    """
    Attempt to import a class from a string representation.
    """
    try:
        parts = val.split('.')
        module_path, class_name = '.'.join(parts[:-1]), parts[-1]
        module = importlib.import_module(module_path)
        return getattr(module, class_name)
    except (ImportError, AttributeError) as e:
        raise ImportError('Could not import {} for API setting {}. {}: {}.'
                          .format(val, setting_name, e.__class__.__name__, e))

def get_translator():
    """
    Returns the default translator.
    """
    TranslatorService = getattr(settings, 'AUTOTRANSLATE_TRANSLATOR_SERVICE',
                                'autotranslate.services.GoSlateTranslatorService')
    translator = perform_import(TranslatorService, 'AUTOTRANSLATE_TRANSLATOR_SERVICE')()
    return translator

Response:
import six

from autotranslate.compat import importlib
from django.conf import settings

def perform_import(val, setting_name):
    """
    If the given setting is a string import notation,
    then perform the necessary import or imports.
    Credits: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/settings.py#L138
    """
    if val is None:
        return None
    elif isinstance(val, six.string_types):
        return import_from_string(val, setting_name)
    elif isinstance(val, (list, tuple)):
        return [import_from_string(item, setting_name) for item in val]
    return val

def import_from_string(val, setting_name):
    """
    Attempt to import a class from a string representation.
    """
    try:
        parts = val.split('.')
        module_path, class_name = '.'.join(parts[:-1]), parts[-1]
        module = importlib.import_module(module_path)
        return getattr(module, class_name)
    except (ImportError, AttributeError) as e:
        raise ImportError('Could not import {} for API setting {}. {}: {}.'
                          .format(val, setting_name, e.__class__.__name__, e))

def get_translator():
    """
    Returns the default translator.
    """
    TranslatorService = getattr(settings, 'AUTOTRANSLATE_TRANSLATOR_SERVICE',
                                'autotranslate.services.GoSlateTranslatorService')
    translator = perform_import(TranslatorService, 'AUTOTRANSLATE_TRANSLATOR_SERVICE')()
    return translator

Prompt:
Make sure we don't expose translator as global
import six

from autotranslate.compat import importlib
from django.conf import settings

def perform_import(val, setting_name):
    """
    If the given setting is a string import notation,
    then perform the necessary import or imports.
    Credits: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/settings.py#L138
    """
    if val is None:
        return None
    elif isinstance(val, six.string_types):
        return import_from_string(val, setting_name)
    elif isinstance(val, (list, tuple)):
        return [import_from_string(item, setting_name) for item in val]
    return val

def import_from_string(val, setting_name):
    """
    Attempt to import a class from a string representation.
    """
    try:
        parts = val.split('.')
        module_path, class_name = '.'.join(parts[:-1]), parts[-1]
        module = importlib.import_module(module_path)
        return getattr(module, class_name)
    except (ImportError, AttributeError) as e:
        raise ImportError('Could not import {} for API setting {}. {}: {}.'
                          .format(val, setting_name, e.__class__.__name__, e))

TranslatorService = getattr(settings, 'AUTOTRANSLATE_TRANSLATOR_SERVICE',
                            'autotranslate.services.GoSlateTranslatorService')
translator = perform_import(TranslatorService, 'AUTOTRANSLATE_TRANSLATOR_SERVICE')()

translate_string = translator.translate_string
translate_strings = translator.translate_strings


import angr

import logging
l = logging.getLogger("angr.tests")

import os
test_location = str(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../binaries/tests'))

def test_block_cache():
    p = angr.Project(os.path.join(test_location, "x86_64", "fauxware"), translation_cache=True)
    b = p.factory.block(p.entry)
    assert p.factory.block(p.entry).vex is b.vex

    p = angr.Project(os.path.join(test_location, "x86_64", "fauxware"), translation_cache=False)
    b = p.factory.block(p.entry)
    assert p.factory.block(p.entry).vex is not b.vex

if __name__ == "__main__":
    test_block_cache()

Fix the test case for block cache.

Fix the test case for block cache.

Response:
import angr

import logging
l = logging.getLogger("angr.tests")

import os
test_location = str(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../binaries/tests'))

def test_block_cache():
    p = angr.Project(os.path.join(test_location, "x86_64", "fauxware"), translation_cache=True)
    b = p.factory.block(p.entry)
    assert p.factory.block(p.entry).vex is b.vex

    p = angr.Project(os.path.join(test_location, "x86_64", "fauxware"), translation_cache=False)
    b = p.factory.block(p.entry)
    assert p.factory.block(p.entry).vex is not b.vex

if __name__ == "__main__":
    test_block_cache()

Prompt:
Fix the test case for block cache.

import angr

import logging
l = logging.getLogger("angr.tests")

import os
test_location = str(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../binaries/tests'))

def test_block_cache():
    p = angr.Project(os.path.join(test_location, "x86_64", "fauxware"), translation_cache=True)
    b = p.factory.block(p.entry)
    assert p.factory.block(p.entry) is b

    p = angr.Project(os.path.join(test_location, "x86_64", "fauxware"), translation_cache=False)
    b = p.factory.block(p.entry)
    assert p.factory.block(p.entry) is not b

if __name__ == "__main__":
    test_block_cache()


from subprocess import Popen, PIPE
from plyer.facades import UniqueID
from plyer.utils import whereis_exe

from os import environ

class OSXUniqueID(UniqueID):
    def _get_uid(self):
        old_lang = environ.get('LANG')
        environ['LANG'] = 'C'

        ioreg_process = Popen(["ioreg", "-l"], stdout=PIPE)
        grep_process = Popen(["grep", "IOPlatformSerialNumber"],
            stdin=ioreg_process.stdout, stdout=PIPE)
        ioreg_process.stdout.close()
        output = grep_process.communicate()[0]

        if old_lang is None:
            environ.pop('LANG')
        else:
            environ['LANG'] = old_lang

        if output:
            return output.split()[3][1:-1]
        else:
            return None

def instance():
    import sys
    if whereis_exe('ioreg'):
        return OSXUniqueID()
    sys.stderr.write("ioreg not found.")
    return UniqueID()

Response:
from subprocess import Popen, PIPE
from plyer.facades import UniqueID
from plyer.utils import whereis_exe

from os import environ

class OSXUniqueID(UniqueID):
    def _get_uid(self):
        old_lang = environ.get('LANG')
        environ['LANG'] = 'C'

        ioreg_process = Popen(["ioreg", "-l"], stdout=PIPE)
        grep_process = Popen(["grep", "IOPlatformSerialNumber"],
            stdin=ioreg_process.stdout, stdout=PIPE)
        ioreg_process.stdout.close()
        output = grep_process.communicate()[0]

        if old_lang is None:
            environ.pop('LANG')
        else:
            environ['LANG'] = old_lang

        if output:
            return output.split()[3][1:-1]
        else:
            return None

def instance():
    import sys
    if whereis_exe('ioreg'):
        return OSXUniqueID()
    sys.stderr.write("ioreg not found.")
    return UniqueID()

Prompt:
Fix TypeError if `LANG` is not set in on osx

In macosx.uniqueid, `$LANG` is retrieved with `old_lang = environ.get('LANG')`. This means that if `LANG` is not set, `old_lang` will be none. When plyer later tries to restore the original value of lang, `putenv` will complain that we're not supplying a string.  I've corrected this by popping the inserted `LANG` value if `old_lang` is none.
from subprocess import Popen, PIPE
from plyer.facades import UniqueID
from plyer.utils import whereis_exe

from os import environ

class OSXUniqueID(UniqueID):
    def _get_uid(self):
        old_lang = environ.get('LANG')
        environ['LANG'] = 'C'

        ioreg_process = Popen(["ioreg", "-l"], stdout=PIPE)
        grep_process = Popen(["grep", "IOPlatformSerialNumber"],
            stdin=ioreg_process.stdout, stdout=PIPE)
        ioreg_process.stdout.close()
        output = grep_process.communicate()[0]

        environ['LANG'] = old_lang

        if output:
            return output.split()[3][1:-1]
        else:
            return None

def instance():
    import sys
    if whereis_exe('ioreg'):
        return OSXUniqueID()
    sys.stderr.write("ioreg not found.")
    return UniqueID()


import chainer
import chainer.functions as F
import chainer.links as L

class SEBlock(chainer.Chain):

    """A squeeze-and-excitation block.

    This block is part of squeeze-and-excitation networks. Channel-wise
    multiplication weights are inferred from and applied to input feature map.
    Please refer to `the original paper
    <https://arxiv.org/pdf/1709.01507.pdf>`_ for more details.

    .. seealso::
        :class:`chainercv.links.model.senet.SEResNet`

    Args:
        n_channel (int): The number of channels of the input and output array.
        ratio (int): Reduction ratio of :obj:`n_channel` to the number of
            hidden layer units.

    """

    def __init__(self, n_channel, ratio=16):

        super(SEBlock, self).__init__()
        reduction_size = n_channel // ratio

        with self.init_scope():
            self.down = L.Linear(n_channel, reduction_size)
            self.up = L.Linear(reduction_size, n_channel)

    def forward(self, u):
        B, C, H, W = u.shape

        z = F.average(u, axis=(2, 3))
        x = F.relu(self.down(z))
        x = F.sigmoid(self.up(x))
        x = F.reshape(x, x.shape[:2] + (1, 1))
        # Spatial axes of `x` will be broadcasted.
        return u * x

Response:
import chainer
import chainer.functions as F
import chainer.links as L

class SEBlock(chainer.Chain):

    """A squeeze-and-excitation block.

    This block is part of squeeze-and-excitation networks. Channel-wise
    multiplication weights are inferred from and applied to input feature map.
    Please refer to `the original paper
    <https://arxiv.org/pdf/1709.01507.pdf>`_ for more details.

    .. seealso::
        :class:`chainercv.links.model.senet.SEResNet`

    Args:
        n_channel (int): The number of channels of the input and output array.
        ratio (int): Reduction ratio of :obj:`n_channel` to the number of
            hidden layer units.

    """

    def __init__(self, n_channel, ratio=16):

        super(SEBlock, self).__init__()
        reduction_size = n_channel // ratio

        with self.init_scope():
            self.down = L.Linear(n_channel, reduction_size)
            self.up = L.Linear(reduction_size, n_channel)

    def forward(self, u):
        B, C, H, W = u.shape

        z = F.average(u, axis=(2, 3))
        x = F.relu(self.down(z))
        x = F.sigmoid(self.up(x))
        x = F.reshape(x, x.shape[:2] + (1, 1))
        # Spatial axes of `x` will be broadcasted.
        return u * x

Prompt:
Simplify SEBlock by broadcast of binary op

instead of explicit broadcast_to. The main motivation of this
change is to simplify the exported ONNX, but this would also
improve performance.

import chainer
import chainer.functions as F
import chainer.links as L

class SEBlock(chainer.Chain):

    """A squeeze-and-excitation block.

    This block is part of squeeze-and-excitation networks. Channel-wise
    multiplication weights are inferred from and applied to input feature map.
    Please refer to `the original paper
    <https://arxiv.org/pdf/1709.01507.pdf>`_ for more details.

    .. seealso::
        :class:`chainercv.links.model.senet.SEResNet`

    Args:
        n_channel (int): The number of channels of the input and output array.
        ratio (int): Reduction ratio of :obj:`n_channel` to the number of
            hidden layer units.

    """

    def __init__(self, n_channel, ratio=16):

        super(SEBlock, self).__init__()
        reduction_size = n_channel // ratio

        with self.init_scope():
            self.down = L.Linear(n_channel, reduction_size)
            self.up = L.Linear(reduction_size, n_channel)

    def forward(self, u):
        B, C, H, W = u.shape

        z = F.average(u, axis=(2, 3))
        x = F.relu(self.down(z))
        x = F.sigmoid(self.up(x))

        x = F.broadcast_to(x, (H, W, B, C))
        x = x.transpose((2, 3, 0, 1))

        return u * x


import sympy

x1, x2 = sympy.symbols('x1 x2')
f = 8*x1 + 12*x2 + x1**2 -2*x2**2

df_dx1 = sympy.diff(f,x1)
df_dx2 = sympy.diff(f,x2)
H = sympy.hessian(f, (x1, x2))

xs = sympy.solve([df_dx1, df_dx2], [x1, x2])

H_xs = H.subs([(x1,xs[x1]), (x2,xs[x2])])
lambda_xs = H_xs.eigenvals()

count = 0
for i in lambda_xs.keys():
    if i.evalf() <= 0:
        count += 1

if count == 0:
    print 'Local minima'
elif count == len(lambda_xs.keys()):
    print 'Lacal maxima'
else:
    print 'Saddle point'

Add solution for exercise 2b of hw3

Add solution for exercise 2b of hw3

Response:
import sympy

x1, x2 = sympy.symbols('x1 x2')
f = 8*x1 + 12*x2 + x1**2 -2*x2**2

df_dx1 = sympy.diff(f,x1)
df_dx2 = sympy.diff(f,x2)
H = sympy.hessian(f, (x1, x2))

xs = sympy.solve([df_dx1, df_dx2], [x1, x2])

H_xs = H.subs([(x1,xs[x1]), (x2,xs[x2])])
lambda_xs = H_xs.eigenvals()

count = 0
for i in lambda_xs.keys():
    if i.evalf() <= 0:
        count += 1

if count == 0:
    print 'Local minima'
elif count == len(lambda_xs.keys()):
    print 'Lacal maxima'
else:
    print 'Saddle point'

Prompt: Add solution for exercise 2b of hw3


#!/usr/bin/env python
import setuptools
import os

setuptools.setup(
    name='endpoints-proto-datastore',
    version='0.9.0',
    description='Google Cloud Endpoints Proto Datastore Library',
    long_description=open(os.path.join(os.path.dirname(__file__), 'README.md')).read(),
    url='https://github.com/GoogleCloudPlatform/endpoints-proto-datastore',
    license='Apache',
    author='Danny Hermes',
    author_email='daniel.j.hermes@gmail.com',
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: Apache License',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python'
    ],
    packages=setuptools.find_packages(exclude=['examples*', 'docs*']),
)

Response:
#!/usr/bin/env python
import setuptools
import os

setuptools.setup(
    name='endpoints-proto-datastore',
    version='0.9.0',
    description='Google Cloud Endpoints Proto Datastore Library',
    long_description=open(os.path.join(os.path.dirname(__file__), 'README.md')).read(),
    url='https://github.com/GoogleCloudPlatform/endpoints-proto-datastore',
    license='Apache',
    author='Danny Hermes',
    author_email='daniel.j.hermes@gmail.com',
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: Apache License',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python'
    ],
    packages=setuptools.find_packages(exclude=['examples*', 'docs*']),
)

Prompt:
Change desc to clearly advertise that this is a library to work with
Google Cloud Endpoints

#!/usr/bin/env python
import setuptools
import os

setuptools.setup(
    name='endpoints-proto-datastore',
    version='0.9.0',
    description='Endpoints Proto Datastore API',
    long_description=open(os.path.join(os.path.dirname(__file__), 'README.md')).read(),
    url='https://github.com/GoogleCloudPlatform/endpoints-proto-datastore',
    license='Apache',
    author='Danny Hermes',
    author_email='daniel.j.hermes@gmail.com',
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: Apache License',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python'
    ],
    packages=setuptools.find_packages(exclude=['examples*', 'docs*']),
)


#!/bin/env python
#
# cat_StarterLog.py
#
# Print out the StarterLog for a glidein output file
#
# Usage: cat_StarterLog.py logname
#

import os.path
import sys
STARTUP_DIR=sys.path[0]
sys.path.append(os.path.join(STARTUP_DIR,"lib"))
import gWftLogParser

USAGE="Usage: cat_StarterLog.py <logname>"

def main():
    try:
        print gWftLogParser.get_CondorLog(sys.argv[1],"((StarterLog)|(StarterLog.vm2))")
    except:
        sys.stderr.write("%s\n"%USAGE)
        sys.exit(1)

if __name__ == '__main__':
    main()

Support both old and new format

Support both old and new format

Response:
#!/bin/env python
#
# cat_StarterLog.py
#
# Print out the StarterLog for a glidein output file
#
# Usage: cat_StarterLog.py logname
#

import os.path
import sys
STARTUP_DIR=sys.path[0]
sys.path.append(os.path.join(STARTUP_DIR,"lib"))
import gWftLogParser

USAGE="Usage: cat_StarterLog.py <logname>"

def main():
    try:
        print gWftLogParser.get_CondorLog(sys.argv[1],"((StarterLog)|(StarterLog.vm2))")
    except:
        sys.stderr.write("%s\n"%USAGE)
        sys.exit(1)

if __name__ == '__main__':
    main()

Prompt:
Support both old and new format

#!/bin/env python
#
# cat_StarterLog.py
#
# Print out the StarterLog for a glidein output file
#
# Usage: cat_StarterLog.py logname
#

import os.path
import sys
STARTUP_DIR=sys.path[0]
sys.path.append(os.path.join(STARTUP_DIR,"lib"))
import gWftLogParser

USAGE="Usage: cat_StarterLog.py <logname>"

def main():
    try:
        print gWftLogParser.get_CondorLog(sys.argv[1],"StarterLog.vm2")
    except:
        sys.stderr.write("%s\n"%USAGE)
        sys.exit(1)

if __name__ == '__main__':
    main()


import sys
from pathlib import Path
from fabric.api import local, task, lcd, env
from fabric.contrib.console import confirm
from fabric.utils import abort

src_p = Path(env.real_fabfile).parent / 'src'

@task
def start_db():
    if sys.platform.startswith('darwin'):
        # Mac OSX
        local('postgres -D /usr/local/var/postgres -s')

@task
def backup():
    cmd_dumpdata = 'python manage.py dumpdata '
    with lcd(src_p):
        local(
            cmd_dumpdata + 'users.EmailUser data_sources.DataSource | '
            'tee ../db_dump/user_sources.json'
        )
        local(
            cmd_dumpdata + 'experiments | '
            'tee ../db_dump/experiments.json'
        )
        local(
            cmd_dumpdata + 'analyses.GenomeReference | '
            'tee ../db_dump/genome_reference.json'
        )

@task
def reborn():
    with lcd(src_p.as_posix()):
        db_dump_dir = Path(env.cwd, '../db_dump')
        if not (
            db_dump_dir.joinpath('user_sources.json').exists() and
            db_dump_dir.joinpath('genome_reference.json').exists() and
            db_dump_dir.joinpath('experiments.json').exists()
        ):
            abort('Backup the import database content first!')
        confirm('Destory and re-create the current database?', False)

        local('dropdb biocloud')
        local('createdb biocloud')
        local('python manage.py migrate')
        local('python manage.py loaddata ../db_dump/user_sources.json')
        local('python manage.py loaddata ../db_dump/genome_reference.json')
        local('python manage.py loaddata ../db_dump/experiments.json')

Response:
import sys
from pathlib import Path
from fabric.api import local, task, lcd, env
from fabric.contrib.console import confirm
from fabric.utils import abort

src_p = Path(env.real_fabfile).parent / 'src'

@task
def start_db():
    if sys.platform.startswith('darwin'):
        # Mac OSX
        local('postgres -D /usr/local/var/postgres -s')

@task
def backup():
    cmd_dumpdata = 'python manage.py dumpdata '
    with lcd(src_p):
        local(
            cmd_dumpdata + 'users.EmailUser data_sources.DataSource | '
            'tee ../db_dump/user_sources.json'
        )
        local(
            cmd_dumpdata + 'experiments | '
            'tee ../db_dump/experiments.json'
        )
        local(
            cmd_dumpdata + 'analyses.GenomeReference | '
            'tee ../db_dump/genome_reference.json'
        )

@task
def reborn():
    with lcd(src_p.as_posix()):
        db_dump_dir = Path(env.cwd, '../db_dump')
        if not (
            db_dump_dir.joinpath('user_sources.json').exists() and
            db_dump_dir.joinpath('genome_reference.json').exists() and
            db_dump_dir.joinpath('experiments.json').exists()
        ):
            abort('Backup the import database content first!')
        confirm('Destory and re-create the current database?', False)

        local('dropdb biocloud')
        local('createdb biocloud')
        local('python manage.py migrate')
        local('python manage.py loaddata ../db_dump/user_sources.json')
        local('python manage.py loaddata ../db_dump/genome_reference.json')
        local('python manage.py loaddata ../db_dump/experiments.json')

Prompt:
Add fab command to backup and destroy database

import sys
from fabric.api import local, task

@task
def start_db():
    if sys.platform.startswith('darwin'):
        # Mac OSX
        local('postgres -D /usr/local/var/postgres -s')


from comics.aggregator.crawler import CrawlerBase, CrawlerImage
from comics.core.comic_data import ComicDataBase

class ComicData(ComicDataBase):
    name = "Basic Instructions"
    language = "en"
    url = "http://www.basicinstructions.net/"
    start_date = "2006-07-01"
    rights = "Scott Meyer"

class Crawler(CrawlerBase):
    history_capable_days = 100
    schedule = "Tu,Th,Su"
    time_zone = "US/Eastern"

    def crawl(self, pub_date):
        feed = self.parse_feed(
            "http://basicinstructions.net/basic-instructions/rss.xml"
        )
        for entry in feed.for_date(pub_date):
            url = entry.summary.src("img")
            title = entry.title
            return CrawlerImage(url, title)

Response:
from comics.aggregator.crawler import CrawlerBase, CrawlerImage
from comics.core.comic_data import ComicDataBase

class ComicData(ComicDataBase):
    name = "Basic Instructions"
    language = "en"
    url = "http://www.basicinstructions.net/"
    start_date = "2006-07-01"
    rights = "Scott Meyer"

class Crawler(CrawlerBase):
    history_capable_days = 100
    schedule = "Tu,Th,Su"
    time_zone = "US/Eastern"

    def crawl(self, pub_date):
        feed = self.parse_feed(
            "http://basicinstructions.net/basic-instructions/rss.xml"
        )
        for entry in feed.for_date(pub_date):
            url = entry.summary.src("img")
            title = entry.title
            return CrawlerImage(url, title)

Prompt:
Update "Basic Instructions" after feed change

from comics.aggregator.crawler import CrawlerBase, CrawlerImage
from comics.core.comic_data import ComicDataBase

class ComicData(ComicDataBase):
    name = "Basic Instructions"
    language = "en"
    url = "http://www.basicinstructions.net/"
    start_date = "2006-07-01"
    rights = "Scott Meyer"

class Crawler(CrawlerBase):
    history_capable_days = 100
    schedule = "Tu,Th,Su"
    time_zone = "US/Eastern"

    def crawl(self, pub_date):
        feed = self.parse_feed(
            "http://basicinstructions.net/basic-instructions/rss.xml"
        )
        for entry in feed.for_date(pub_date):
            url = entry.summary.src('img[src*="/storage/"][src*=".gif"]')
            title = entry.title
            return CrawlerImage(url, title)


import pytest
from unittest import TestCase, mock

import core.config
import core.widget
import modules.contrib.network_traffic

from types import SimpleNamespace

pytest.importorskip("psutil")
pytest.importorskip("netifaces")

def io_counters_mock(recv, sent):
    return {
        'lo': SimpleNamespace(
            bytes_sent = sent,
            bytes_recv = recv
        )
    }

def gateways_response():
    return {
        'default': {
            1: ('10.0.0.10', 'lo')
        }
    }

def build_module():
    return modules.contrib.network_traffic.Module(config=core.config.Config([]), theme=None)

class TestNetworkTrafficUnit(TestCase):
    def test_load_module(self):
        __import__("modules.contrib.network_traffic")

    @mock.patch('psutil.net_io_counters')
    @mock.patch('netifaces.gateways')
    @mock.patch('netifaces.AF_INET', 1)
    def test_update_rates(self, gateways_mock, net_io_counters_mock):
        net_io_counters_mock.return_value = io_counters_mock(0, 0)
        gateways_mock.return_value = gateways_response()

        module = build_module()

        net_io_counters_mock.return_value = io_counters_mock(2842135, 1932215)
        module.update()

        assert module.widgets()[1].full_text() == '1.84MiB/s'
        assert module.widgets()[0].full_text() == '2.71MiB/s'

    def test_initial_download_rate(self):
        module = build_module()
        assert module.widgets()[0].full_text() == '0.00B/s'

    def test_initial_upload_rate(self):
        module = build_module()
        assert module.widgets()[1].full_text() == '0.00B/s'

Add Network Traffic module tests

Add Network Traffic module tests

Response:
import pytest
from unittest import TestCase, mock

import core.config
import core.widget
import modules.contrib.network_traffic

from types import SimpleNamespace

pytest.importorskip("psutil")
pytest.importorskip("netifaces")

def io_counters_mock(recv, sent):
    return {
        'lo': SimpleNamespace(
            bytes_sent = sent,
            bytes_recv = recv
        )
    }

def gateways_response():
    return {
        'default': {
            1: ('10.0.0.10', 'lo')
        }
    }

def build_module():
    return modules.contrib.network_traffic.Module(config=core.config.Config([]), theme=None)

class TestNetworkTrafficUnit(TestCase):
    def test_load_module(self):
        __import__("modules.contrib.network_traffic")

    @mock.patch('psutil.net_io_counters')
    @mock.patch('netifaces.gateways')
    @mock.patch('netifaces.AF_INET', 1)
    def test_update_rates(self, gateways_mock, net_io_counters_mock):
        net_io_counters_mock.return_value = io_counters_mock(0, 0)
        gateways_mock.return_value = gateways_response()

        module = build_module()

        net_io_counters_mock.return_value = io_counters_mock(2842135, 1932215)
        module.update()

        assert module.widgets()[1].full_text() == '1.84MiB/s'
        assert module.widgets()[0].full_text() == '2.71MiB/s'

    def test_initial_download_rate(self):
        module = build_module()
        assert module.widgets()[0].full_text() == '0.00B/s'

    def test_initial_upload_rate(self):
        module = build_module()
        assert module.widgets()[1].full_text() == '0.00B/s'

Prompt:
Add Network Traffic module tests

import pytest

pytest.importorskip("psutil")

pytest.importorskip("netifaces")

def test_load_module():
    __import__("modules.contrib.network_traffic")


import ucilib.Sim as Sim
import ucilib.BorisUpdater as BorisUpdater
import numpy as np

# Some helpful constants.
fund_charge = 1.602176565e-19

# Mass of Be^+ ions.
ion_mass = 8.9465 * 1.673e-27

# Create a simulation with n particles.
n = 10000
s = Sim.Sim()
s.ptcls.set_nptcls(n)

# 1/e radius of cloud.
s.ptcls.rmax = 2.0e-4
s.ptcls.init_ptcls(charge = fund_charge, mass = ion_mass)

# Turn the first n/2 particles into electrons by setting their mass and
# charge.
s.ptcls.q()[:(n/2)] = -fund_charge * np.ones(n/2)
s.ptcls.m()[:(n/2)] = 9.1e-31 * np.ones(n/2)

# Finally we set the updater.
s.updater = BorisUpdater.BorisUpdater(s.ctx, s.queue)

Add an example simulation setup for an ultracold neutral plasma.

Response:
import ucilib.Sim as Sim
import ucilib.BorisUpdater as BorisUpdater
import numpy as np

# Some helpful constants.
fund_charge = 1.602176565e-19

# Mass of Be^+ ions.
ion_mass = 8.9465 * 1.673e-27

# Create a simulation with n particles.
n = 10000
s = Sim.Sim()
s.ptcls.set_nptcls(n)

# 1/e radius of cloud.
s.ptcls.rmax = 2.0e-4
s.ptcls.init_ptcls(charge = fund_charge, mass = ion_mass)

# Turn the first n/2 particles into electrons by setting their mass and
# charge.
s.ptcls.q()[:(n/2)] = -fund_charge * np.ones(n/2)
s.ptcls.m()[:(n/2)] = 9.1e-31 * np.ones(n/2)

# Finally we set the updater.
s.updater = BorisUpdater.BorisUpdater(s.ctx, s.queue)

Prompt: Add an example simulation setup for an ultracold neutral plasma.


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

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        ('msgs', '0033_exportmessagestask_uuid'),
    ]

    def move_recording_domains(apps, schema_editor):
        Msg = apps.get_model('msgs', 'Msg')

        # this is our new bucket name
        bucket_name = settings.AWS_STORAGE_BUCKET_NAME

        # our old bucket name had periods instead of dashes
        old_bucket_domain = 'http://' + bucket_name.replace('-', '.')

        # our new domain is more specific
        new_bucket_domain = 'https://' + settings.AWS_BUCKET_DOMAIN

        for msg in Msg.objects.filter(direction='I', msg_type='V').exclude(recording_url=None):
            # if our recording URL is on our old bucket
            if msg.recording_url.find(old_bucket_domain) >= 0:
                # rename it to our new bucket
                old_recording_url = msg.recording_url
                msg.recording_url = msg.recording_url.replace(old_bucket_domain,
                                                              new_bucket_domain)
                print "[%d] %s to %s" % (msg.id, old_recording_url, msg.recording_url)
                msg.save(update_fields=['recording_url'])

    operations = [
        migrations.RunPython(move_recording_domains)
    ]

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

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        ('msgs', '0033_exportmessagestask_uuid'),
    ]

    def move_recording_domains(apps, schema_editor):
        Msg = apps.get_model('msgs', 'Msg')

        # this is our new bucket name
        bucket_name = settings.AWS_STORAGE_BUCKET_NAME

        # our old bucket name had periods instead of dashes
        old_bucket_domain = 'http://' + bucket_name.replace('-', '.')

        # our new domain is more specific
        new_bucket_domain = 'https://' + settings.AWS_BUCKET_DOMAIN

        for msg in Msg.objects.filter(direction='I', msg_type='V').exclude(recording_url=None):
            # if our recording URL is on our old bucket
            if msg.recording_url.find(old_bucket_domain) >= 0:
                # rename it to our new bucket
                old_recording_url = msg.recording_url
                msg.recording_url = msg.recording_url.replace(old_bucket_domain,
                                                              new_bucket_domain)
                print "[%d] %s to %s" % (msg.id, old_recording_url, msg.recording_url)
                msg.save(update_fields=['recording_url'])

    operations = [
        migrations.RunPython(move_recording_domains)
    ]

Prompt:
Tweak to migration so it is a bit faster for future migraters

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

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        ('msgs', '0033_exportmessagestask_uuid'),
    ]

    def move_recording_domains(apps, schema_editor):
        Msg = apps.get_model('msgs', 'Msg')

        # this is our new bucket name
        bucket_name = settings.AWS_STORAGE_BUCKET_NAME

        # our old bucket name had periods instead of dashes
        old_bucket_domain = 'http://' + bucket_name.replace('-', '.')

        # our new domain is more specific
        new_bucket_domain = 'https://' + settings.AWS_BUCKET_DOMAIN

        for msg in Msg.objects.filter(msg_type='V').exclude(recording_url=None):
            # if our recording URL is on our old bucket
            if msg.recording_url.find(old_bucket_domain) >= 0:
                # rename it to our new bucket
                old_recording_url = msg.recording_url
                msg.recording_url = msg.recording_url.replace(old_bucket_domain,
                                                              new_bucket_domain)
                print "[%d] %s to %s" % (msg.id, old_recording_url, msg.recording_url)
                msg.save(update_fields=['recording_url'])

    operations = [
        migrations.RunPython(move_recording_domains)
    ]


import os
from setuptools import setup, find_packages

with open(os.path.join(os.path.dirname(__file__), 'README.md')) as readme:
    README = readme.read()

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
    name='pybbm-private-messages',
    version='0.2.2',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'pybbm',
    ],
    test_suite='runtests.runtests',
    license='MIT License',
    description='A private messaging plugin for the pybbm forum.',
    long_description=README,
    url='https://github.com/skolsuper/pybbm_private_messages',
    author='James Keys',
    author_email='skolsuper@gmail.com',
    classifiers=[
        'Environment :: Web Environment',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
)

Response:
import os
from setuptools import setup, find_packages

with open(os.path.join(os.path.dirname(__file__), 'README.md')) as readme:
    README = readme.read()

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
    name='pybbm-private-messages',
    version='0.2.2',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'pybbm',
    ],
    test_suite='runtests.runtests',
    license='MIT License',
    description='A private messaging plugin for the pybbm forum.',
    long_description=README,
    url='https://github.com/skolsuper/pybbm_private_messages',
    author='James Keys',
    author_email='skolsuper@gmail.com',
    classifiers=[
        'Environment :: Web Environment',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
)

Prompt:
Include custom templatetags in package build.

import os
from setuptools import setup

with open(os.path.join(os.path.dirname(__file__), 'README.md')) as readme:
    README = readme.read()

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
    name='pybbm-private-messages',
    version='0.2.1',
    packages=['private_messages', 'private_messages.migrations'],
    include_package_data=True,
    install_requires=[
        'pybbm',
    ],
    test_suite='runtests.runtests',
    license='MIT License',
    description='A private messaging plugin for the pybbm forum.',
    long_description=README,
    url='https://github.com/skolsuper/pybbm_private_messages',
    author='James Keys',
    author_email='skolsuper@gmail.com',
    classifiers=[
        'Environment :: Web Environment',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
)


from django.core.management.base import BaseCommand
from thezombies.models import Agency
from thezombies.tasks.main import crawl_agency_datasets

class Command(BaseCommand):
    """Start a task that crawl the datasets from an agency data catalog. This command will exit, but the task will run in the background"""
    args = '<agency_id ...>'

    def handle(self, *args, **options):
        if len(args) > 0:
            agency_id = args[0]
            if agency_id:
                task = crawl_agency_datasets.delay(agency_id)
                self.stdout.write(u'Running task with id {0}'.format(task.id))
                self.stdout.write(u'This can take many minutes...')
            else:
                self.stderr.write(u"Didn't get an agency_id!")
        else:
            self.stdout.write(u'Please provide an agency id:\n')
            agency_list = u'\n'.join(['{0:2d}: {1}'.format(a.id, a.name) for a in Agency.objects.all()])
            self.stdout.write(agency_list)
            self.stdout.write(u'\n')

Response:
from django.core.management.base import BaseCommand
from thezombies.models import Agency
from thezombies.tasks.main import crawl_agency_datasets

class Command(BaseCommand):
    """Start a task that crawl the datasets from an agency data catalog. This command will exit, but the task will run in the background"""
    args = '<agency_id ...>'

    def handle(self, *args, **options):
        if len(args) > 0:
            agency_id = args[0]
            if agency_id:
                task = crawl_agency_datasets.delay(agency_id)
                self.stdout.write(u'Running task with id {0}'.format(task.id))
                self.stdout.write(u'This can take many minutes...')
            else:
                self.stderr.write(u"Didn't get an agency_id!")
        else:
            self.stdout.write(u'Please provide an agency id:\n')
            agency_list = u'\n'.join(['{0:2d}: {1}'.format(a.id, a.name) for a in Agency.objects.all()])
            self.stdout.write(agency_list)
            self.stdout.write(u'\n')

Prompt:
Add message for when command is not supplied any arguments.

from django.core.management.base import BaseCommand
from thezombies.tasks.main import crawl_agency_datasets

class Command(BaseCommand):
    """Start a task that crawl the datasets from an agency data catalog. This command will exit, but the task will run in the background"""
    args = '<agency_id ...>'

    def handle(self, *args, **options):
        if len(args) > 0:
            agency_id = args[0]
            if agency_id:
                task = crawl_agency_datasets.delay(agency_id)
                self.stdout.write(u'Running task with id {0}'.format(task.id))
                self.stdout.write(u'This can take many minutes...')
            else:
                self.stderr.write(u"Didn't get an agency_id!")


from numpy.testing import assert_almost_equal

from statsmodels.datasets import statecrime, get_rdataset
from statsmodels.regression.linear_model import OLS
from statsmodels.stats.outliers_influence import reset_ramsey
from statsmodels.stats.outliers_influence import variance_inflation_factor
from statsmodels.tools import add_constant

import numpy as np

data = statecrime.load_pandas().data

def test_reset_stata():
    mod = OLS(data.violent, add_constant(data[['murder', 'hs_grad']]))
    res = mod.fit()
    stat = reset_ramsey(res, degree=4)
    assert_almost_equal(stat.fvalue[0, 0], 1.52, decimal=2)
    assert_almost_equal(stat.pvalue, 0.2221, decimal=4)

    exog_idx = list(data.columns).index('urban')
    X_arr = np.asarray(data)
    vif = variance_inflation_factor(X_arr, exog_idx)
    assert_almost_equal(vif, 16.4394, decimal=4)

Response:
from numpy.testing import assert_almost_equal

from statsmodels.datasets import statecrime, get_rdataset
from statsmodels.regression.linear_model import OLS
from statsmodels.stats.outliers_influence import reset_ramsey
from statsmodels.stats.outliers_influence import variance_inflation_factor
from statsmodels.tools import add_constant

import numpy as np

data = statecrime.load_pandas().data

def test_reset_stata():
    mod = OLS(data.violent, add_constant(data[['murder', 'hs_grad']]))
    res = mod.fit()
    stat = reset_ramsey(res, degree=4)
    assert_almost_equal(stat.fvalue[0, 0], 1.52, decimal=2)
    assert_almost_equal(stat.pvalue, 0.2221, decimal=4)

    exog_idx = list(data.columns).index('urban')
    X_arr = np.asarray(data)
    vif = variance_inflation_factor(X_arr, exog_idx)
    assert_almost_equal(vif, 16.4394, decimal=4)

Prompt:
ENH: Add pandas dataframe capability in variance_inflation_factor

from numpy.testing import assert_almost_equal

from statsmodels.datasets import statecrime
from statsmodels.regression.linear_model import OLS
from statsmodels.stats.outliers_influence import reset_ramsey
from statsmodels.tools import add_constant

data = statecrime.load_pandas().data

def test_reset_stata():
    mod = OLS(data.violent, add_constant(data[['murder', 'hs_grad']]))
    res = mod.fit()
    stat = reset_ramsey(res, degree=4)
    assert_almost_equal(stat.fvalue[0, 0], 1.52, decimal=2)
    assert_almost_equal(stat.pvalue, 0.2221, decimal=4)


import resource

import pytest

from .models import TestModel as DirtyMixinModel

pytestmark = pytest.mark.django_db

def test_rss_usage():
    DirtyMixinModel()
    rss_1 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    for _ in range(1000):
        DirtyMixinModel()
    rss_2 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    assert rss_2 == rss_1, 'There is a memory leak!'

Add test for detecting memory leaks

Add test for detecting memory leaks

Response:
import resource

import pytest

from .models import TestModel as DirtyMixinModel

pytestmark = pytest.mark.django_db

def test_rss_usage():
    DirtyMixinModel()
    rss_1 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    for _ in range(1000):
        DirtyMixinModel()
    rss_2 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    assert rss_2 == rss_1, 'There is a memory leak!'

Prompt: Add test for detecting memory leaks


from __future__ import absolute_import

from rest_framework.response import Response

from sentry.app import tsdb
from sentry.api.base import BaseStatsEndpoint, DocSection
from sentry.api.permissions import assert_perm
from sentry.models import Project

class ProjectStatsEndpoint(BaseStatsEndpoint):
    doc_section = DocSection.PROJECTS

    def get(self, request, project_id):
        """
        Retrieve event counts for a project

        **Draft:** This endpoint may change in the future without notice.

        Return a set of points representing a normalized timestamp and the
        number of events seen in the period.

            {method} {path}?since=1421092384.822244&until=1434052399.443363

        Query ranges are limited to Sentry's configured time-series resolutions.

        Parameters:

        - since: a timestamp to set the start of the query
        - until: a timestamp to set the end of the query
        - resolution: an explicit resolution to search for

        **Note:** resolution should not be used unless you're familiar with Sentry
        internals as it's restricted to pre-defined values.
        """
        project = Project.objects.get_from_cache(
            id=project_id,
        )

        assert_perm(project, request.user, request.auth)

        data = tsdb.get_range(
            model=tsdb.models.project,
            keys=[project.id],
            **self._parse_args(request)
        )[project.id]

        return Response(data)

Add project stats to docs

Add project stats to docs

Response:
from __future__ import absolute_import

from rest_framework.response import Response

from sentry.app import tsdb
from sentry.api.base import BaseStatsEndpoint, DocSection
from sentry.api.permissions import assert_perm
from sentry.models import Project

class ProjectStatsEndpoint(BaseStatsEndpoint):
    doc_section = DocSection.PROJECTS

    def get(self, request, project_id):
        """
        Retrieve event counts for a project

        **Draft:** This endpoint may change in the future without notice.

        Return a set of points representing a normalized timestamp and the
        number of events seen in the period.

            {method} {path}?since=1421092384.822244&until=1434052399.443363

        Query ranges are limited to Sentry's configured time-series resolutions.

        Parameters:

        - since: a timestamp to set the start of the query
        - until: a timestamp to set the end of the query
        - resolution: an explicit resolution to search for

        **Note:** resolution should not be used unless you're familiar with Sentry
        internals as it's restricted to pre-defined values.
        """
        project = Project.objects.get_from_cache(
            id=project_id,
        )

        assert_perm(project, request.user, request.auth)

        data = tsdb.get_range(
            model=tsdb.models.project,
            keys=[project.id],
            **self._parse_args(request)
        )[project.id]

        return Response(data)

Prompt:
Add project stats to docs

from __future__ import absolute_import

from rest_framework.response import Response

from sentry.app import tsdb
from sentry.api.base import BaseStatsEndpoint
from sentry.api.permissions import assert_perm
from sentry.models import Project

class ProjectStatsEndpoint(BaseStatsEndpoint):
    def get(self, request, project_id):
        project = Project.objects.get_from_cache(
            id=project_id,
        )

        assert_perm(project, request.user, request.auth)

        data = tsdb.get_range(
            model=tsdb.models.project,
            keys=[project.id],
            **self._parse_args(request)
        )[project.id]

        return Response(data)


from django.core import management
import pytest
from model_mommy import mommy
import time

from example.app.models import SimpleObject, RelatableObject

@pytest.mark.django_db
def test_simple_get(es_client):

    management.call_command("sync_es")

    test_object = mommy.make(SimpleObject)
    time.sleep(1)  # Let the index refresh

    from_es = SimpleObject.search_objects.get(id=test_object.id)
    assert from_es.foo == test_object.foo
    assert from_es.bar == test_object.bar
    assert from_es.baz == test_object.baz
    assert from_es.__class__.__name__ == "SimpleObject_ElasticSearchResult"
    assert from_es.save is None

    with pytest.raises(RelatableObject.DoesNotExist):
        RelatableObject.search_objects.get(id=test_object.id)

@pytest.mark.django_db
def test_related_get(es_client):

    management.call_command("sync_es")

    test_object = mommy.make(RelatableObject)
    time.sleep(1)  # Let the index refresh

    from_es = RelatableObject.search_objects.get(id=test_object.id)
    assert from_es.foo == test_object.foo
    assert from_es.bar == test_object.bar
    assert from_es.baz == test_object.baz
    assert from_es.__class__.__name__ == "SimpleObject_ElasticSearchResult"
    assert from_es.save is None

Work on testing, bulk indexing, etc

Work on testing, bulk indexing, etc

Response:
from django.core import management
import pytest
from model_mommy import mommy
import time

from example.app.models import SimpleObject, RelatableObject

@pytest.mark.django_db
def test_simple_get(es_client):

    management.call_command("sync_es")

    test_object = mommy.make(SimpleObject)
    time.sleep(1)  # Let the index refresh

    from_es = SimpleObject.search_objects.get(id=test_object.id)
    assert from_es.foo == test_object.foo
    assert from_es.bar == test_object.bar
    assert from_es.baz == test_object.baz
    assert from_es.__class__.__name__ == "SimpleObject_ElasticSearchResult"
    assert from_es.save is None

    with pytest.raises(RelatableObject.DoesNotExist):
        RelatableObject.search_objects.get(id=test_object.id)

@pytest.mark.django_db
def test_related_get(es_client):

    management.call_command("sync_es")

    test_object = mommy.make(RelatableObject)
    time.sleep(1)  # Let the index refresh

    from_es = RelatableObject.search_objects.get(id=test_object.id)
    assert from_es.foo == test_object.foo
    assert from_es.bar == test_object.bar
    assert from_es.baz == test_object.baz
    assert from_es.__class__.__name__ == "SimpleObject_ElasticSearchResult"
    assert from_es.save is None

Prompt:
Work on testing, bulk indexing, etc

from django.core import management
import pytest
from model_mommy import mommy
import time

from example.app.models import SimpleObject

@pytest.mark.django_db
def test_get(es_client):

    management.call_command("sync_es")

    test_object = mommy.make(SimpleObject)
    time.sleep(1)  # Let the index refresh

    from_es = SimpleObject.search_objects.get(id=test_object.id)
    assert from_es.foo == test_object.foo
    assert from_es.bar == test_object.bar
    assert from_es.baz == test_object.baz
    assert from_es.__class__.__name__ == "SimpleObject_ElasticSearchResult"
    assert from_es.save is None


from ..attribute_processor import AttributeProcessorError, AttributeProcessorWarning
from .base_processor import BaseProcessor

CONFIG_KEY_MAPPEDATTRIBUTE = 'mapped_attribute'
CONFIG_DEFAULT_MAPPEDATTRIBUTE = ''

class ScopeExtractorProcessor(BaseProcessor):
    """
    Extracts the scope from a scoped attribute and maps that to
    another attribute

    Example configuration:
    module: satosa.micro_services.attribute_processor.AttributeProcessor
    name: AttributeProcessor
    config:
      process:
      - attribute: scoped_affiliation
        processors:
        - name: ScopeExtractorProcessor
          module: satosa.micro_services.processors.scope_extractor_processor
          mapped_attribute: domain
    """
    def process(self, internal_data, attribute, **kwargs):
        mapped_attribute = kwargs.get(CONFIG_KEY_MAPPEDATTRIBUTE, CONFIG_DEFAULT_MAPPEDATTRIBUTE)
        if mapped_attribute is None or mapped_attribute == '':
            raise AttributeProcessorError("The mapped_attribute needs to be set")

        attributes = internal_data.attributes
        values = attributes.get(attribute, [])
        if not values:
            raise AttributeProcessorWarning("Cannot apply scope_extractor to {}, it has no values".format(attribute))
        if not isinstance(values, list):
            values = [values]
        if not any('@' in val for val in values):
            raise AttributeProcessorWarning("Cannot apply scope_extractor to {}, it's values are not scoped".format(attribute))
        for value in values:
            if '@' in value:
                scope = value.split('@')[1]
                attributes[mapped_attribute] = [scope]
                break

Response:
from ..attribute_processor import AttributeProcessorError, AttributeProcessorWarning
from .base_processor import BaseProcessor

CONFIG_KEY_MAPPEDATTRIBUTE = 'mapped_attribute'
CONFIG_DEFAULT_MAPPEDATTRIBUTE = ''

class ScopeExtractorProcessor(BaseProcessor):
    """
    Extracts the scope from a scoped attribute and maps that to
    another attribute

    Example configuration:
    module: satosa.micro_services.attribute_processor.AttributeProcessor
    name: AttributeProcessor
    config:
      process:
      - attribute: scoped_affiliation
        processors:
        - name: ScopeExtractorProcessor
          module: satosa.micro_services.processors.scope_extractor_processor
          mapped_attribute: domain
    """
    def process(self, internal_data, attribute, **kwargs):
        mapped_attribute = kwargs.get(CONFIG_KEY_MAPPEDATTRIBUTE, CONFIG_DEFAULT_MAPPEDATTRIBUTE)
        if mapped_attribute is None or mapped_attribute == '':
            raise AttributeProcessorError("The mapped_attribute needs to be set")

        attributes = internal_data.attributes
        values = attributes.get(attribute, [])
        if not values:
            raise AttributeProcessorWarning("Cannot apply scope_extractor to {}, it has no values".format(attribute))
        if not isinstance(values, list):
            values = [values]
        if not any('@' in val for val in values):
            raise AttributeProcessorWarning("Cannot apply scope_extractor to {}, it's values are not scoped".format(attribute))
        for value in values:
            if '@' in value:
                scope = value.split('@')[1]
                attributes[mapped_attribute] = [scope]
                break

Prompt:
Make the ScopeExtractorProcessor usable for the Primary Identifier

This patch adds support to use the ScopeExtractorProcessor on the Primary
Identifiert which is, in contrast to the other values, a string.

Closes #348

from ..attribute_processor import AttributeProcessorError, AttributeProcessorWarning
from .base_processor import BaseProcessor

CONFIG_KEY_MAPPEDATTRIBUTE = 'mapped_attribute'
CONFIG_DEFAULT_MAPPEDATTRIBUTE = ''

class ScopeExtractorProcessor(BaseProcessor):
    """
    Extracts the scope from a scoped attribute and maps that to
    another attribute

    Example configuration:
    module: satosa.micro_services.attribute_processor.AttributeProcessor
    name: AttributeProcessor
    config:
      process:
      - attribute: scoped_affiliation
        processors:
        - name: ScopeExtractorProcessor
          module: satosa.micro_services.processors.scope_extractor_processor
          mapped_attribute: domain
    """
    def process(self, internal_data, attribute, **kwargs):
        mapped_attribute = kwargs.get(CONFIG_KEY_MAPPEDATTRIBUTE, CONFIG_DEFAULT_MAPPEDATTRIBUTE)
        if mapped_attribute is None or mapped_attribute == '':
            raise AttributeProcessorError("The mapped_attribute needs to be set")

        attributes = internal_data.attributes
        values = attributes.get(attribute, [])
        if not values:
            raise AttributeProcessorWarning("Cannot apply scope_extractor to {}, it has no values".format(attribute))
        if not any('@' in val for val in values):
            raise AttributeProcessorWarning("Cannot apply scope_extractor to {}, it's values are not scoped".format(attribute))
        for value in values:
            if '@' in value:
                scope = value.split('@')[1]
                attributes[mapped_attribute] = [scope]
                break


from errors import WebHDFSError
from client import WebHDFSClient
from attrib import WebHDFSObject

__version__ = '1.0'

Response:
from errors import WebHDFSError
from client import WebHDFSClient
from attrib import WebHDFSObject

__version__ = '1.0'

Prompt:
Make other API classes available from base module.

from client import WebHDFSClient

__version__ = '1.0'


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

@app.route('/')
def index():
    project_list = projects.get_projects()
    return render_template('index.html', projects=project_list)

@app.route('/blog')
def blog():
    return "Flasktopress isn't quite ready yet, but we're stoked that it's coming."

@app.route('/<project>')
def project(project):
    project_list = projects.get_projects()
    if project in project_list:
        project_data = project_list[project]
        return "Contact %s to join the %s project!" % (project_data['project_leaders'][0]['name'], project_data['project_title'])
    else:
        abort(404)

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

Response:
import projects
from flask import Flask, render_template, abort
app = Flask(__name__)

@app.route('/')
def index():
    project_list = projects.get_projects()
    return render_template('index.html', projects=project_list)

@app.route('/blog')
def blog():
    return "Flasktopress isn't quite ready yet, but we're stoked that it's coming."

@app.route('/<project>')
def project(project):
    project_list = projects.get_projects()
    if project in project_list:
        project_data = project_list[project]
        return "Contact %s to join the %s project!" % (project_data['project_leaders'][0]['name'], project_data['project_title'])
    else:
        abort(404)

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

Prompt:
Test if a project exists, load or 404 accordingly

import projects
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
    project_list = projects.get_projects()
    return render_template('index.html', projects=project_list)

@app.route('/blog')
def blog():
    return "Flasktopress isn't quite ready yet, but we're stoked that it's coming."

@app.route('/<project>')
def project(project):
    return "Bet you can't wait to join %s, huh?" % project

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


# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

from __future__ import absolute_import
import abc

import six

from .acceptor import LoaderAcceptor
from .error import InvalidDataError

@six.add_metaclass(abc.ABCMeta)
class TableFormatterInterface(object):
    """
    Abstract class of table data validator.
    """

    @abc.abstractmethod
    def to_table_data(self):  # pragma: no cover
        pass

class TableFormatter(LoaderAcceptor, TableFormatterInterface):
    """
    Abstract class of |TableData| formatter.
    """

    def _validate_source_data(self):
        if len(self._source_data) == 0:
            raise InvalidDataError("souce data is empty")

    def __init__(self, source_data):
        self._source_data = source_data

Delete a private method from interface

Delete a private method from interface

Response:
# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

from __future__ import absolute_import
import abc

import six

from .acceptor import LoaderAcceptor
from .error import InvalidDataError

@six.add_metaclass(abc.ABCMeta)
class TableFormatterInterface(object):
    """
    Abstract class of table data validator.
    """

    @abc.abstractmethod
    def to_table_data(self):  # pragma: no cover
        pass

class TableFormatter(LoaderAcceptor, TableFormatterInterface):
    """
    Abstract class of |TableData| formatter.
    """

    def _validate_source_data(self):
        if len(self._source_data) == 0:
            raise InvalidDataError("souce data is empty")

    def __init__(self, source_data):
        self._source_data = source_data

Prompt:
Delete a private method from interface

# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

from __future__ import absolute_import
import abc

import six

from .acceptor import LoaderAcceptor
from .error import InvalidDataError

@six.add_metaclass(abc.ABCMeta)
class TableFormatterInterface(object):
    """
    Abstract class of table data validator.
    """

    @abc.abstractmethod
    def to_table_data(self):  # pragma: no cover
        pass

    @abc.abstractmethod
    def _validate_source_data(self):  # pragma: no cover
        pass

class TableFormatter(LoaderAcceptor, TableFormatterInterface):
    """
    Abstract class of |TableData| formatter.
    """

    def _validate_source_data(self):
        if len(self._source_data) == 0:
            raise InvalidDataError("souce data is empty")

    def __init__(self, source_data):
        self._source_data = source_data


import loaders
loader = loaders.Lazy(
    '%s.models' % __name__,
    ('Upload',)
)

import extensions
from .functions import (
    delete,
    save,
    save_file,
    save_images,
)
from .models import Upload

def init(db, Storage, resizer=None):
    if 'upload' in db.metadata.tables:
        return  # Already registered the model.

    extensions.db = db
    extensions.resizer = resizer
    extensions.Storage = Storage
    loader.ready()

__all__ = (
    delete,
    init,
    save,
    save_file,
    save_images,
    Upload,
)

Response:
import loaders
loader = loaders.Lazy(
    '%s.models' % __name__,
    ('Upload',)
)

import extensions
from .functions import (
    delete,
    save,
    save_file,
    save_images,
)
from .models import Upload

def init(db, Storage, resizer=None):
    if 'upload' in db.metadata.tables:
        return  # Already registered the model.

    extensions.db = db
    extensions.resizer = resizer
    extensions.Storage = Storage
    loader.ready()

__all__ = (
    delete,
    init,
    save,
    save_file,
    save_images,
    Upload,
)

Prompt:
Make sure model isn't added several times.

import loaders
loader = loaders.Lazy(
    '%s.models' % __name__,
    ('Upload',)
)

import extensions
from .functions import (
    delete,
    save,
    save_file,
    save_images,
)
from .models import Upload

def init(db, Storage, resizer=None):
    extensions.db = db
    extensions.resizer = resizer
    extensions.Storage = Storage
    loader.ready()

__all__ = (
    delete,
    init,
    save,
    save_file,
    save_images,
    Upload,
)


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

__import__('pkg_resources').declare_namespace(__name__)

Prompt:
Remove extraneous vim editor configuration comments

Change-Id: Id34b3ed02b6ef34b92d0cae9791f6e1b2d6cd4d8
Partial-Bug: #1229324

# vim: tabstop=4 shiftwidth=4 softtabstop=4

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

__import__('pkg_resources').declare_namespace(__name__)