# coding: utf-8
import os
import pytest
from mock import patch
from decouple import AutoConfig

def test_autoconfig_env():
    config = AutoConfig()
    path = os.path.join(os.path.dirname(__file__), 'autoconfig', 'env', 'project')
    with patch.object(config, '_caller_path', return_value=path):
        assert 'ENV' == config('KEY')

def test_autoconfig_ini():
    config = AutoConfig()
    path = os.path.join(os.path.dirname(__file__), 'autoconfig', 'ini', 'project')
    with patch.object(config, '_caller_path', return_value=path):
        assert 'INI' == config('KEY')

def test_autoconfig_none():
    config = AutoConfig()
    with patch('os.path.exists', return_value=False):
        with patch.object(config, '_caller_path', return_value="/"):
            with pytest.raises(RuntimeError):
                config('KEY')

Replace cwd with current module's path

Replace cwd with current module's path

Response:
# coding: utf-8
import os
import pytest
from mock import patch
from decouple import AutoConfig

def test_autoconfig_env():
    config = AutoConfig()
    path = os.path.join(os.path.dirname(__file__), 'autoconfig', 'env', 'project')
    with patch.object(config, '_caller_path', return_value=path):
        assert 'ENV' == config('KEY')

def test_autoconfig_ini():
    config = AutoConfig()
    path = os.path.join(os.path.dirname(__file__), 'autoconfig', 'ini', 'project')
    with patch.object(config, '_caller_path', return_value=path):
        assert 'INI' == config('KEY')

def test_autoconfig_none():
    config = AutoConfig()
    with patch('os.path.exists', return_value=False):
        with patch.object(config, '_caller_path', return_value="/"):
            with pytest.raises(RuntimeError):
                config('KEY')

Prompt:
Replace cwd with current module's path
# coding: utf-8
import os
import pytest
from mock import patch
from decouple import AutoConfig

def test_autoconfig_env():
    config = AutoConfig()
    path = os.path.join(os.getcwd(), 'autoconfig', 'env', 'project')
    with patch.object(config, '_caller_path', return_value=path):
        assert 'ENV' == config('KEY')

def test_autoconfig_ini():
    config = AutoConfig()
    path = os.path.join(os.getcwd(), 'autoconfig', 'ini', 'project')
    with patch.object(config, '_caller_path', return_value=path):
        assert 'INI' == config('KEY')

def test_autoconfig_none():
    config = AutoConfig()
    with patch('os.path.exists', return_value=False):
        with patch.object(config, '_caller_path', return_value="/"):
            with pytest.raises(RuntimeError):
                config('KEY')


import openface
import numpy as np
import os
import cv2
import pickle

id_name = ["Alec", "Greg", "Phong", "Emil"]

def classify(aligned_face, net, clf, le):
    rep = net.forward(aligned_face)
    predictions = clf.predict_proba(rep.reshape((1, len(rep)))).ravel()
    maxI = np.argmax(predictions)
    person = le.inverse_transform(maxI)
    confidence = predictions[maxI]
    print("Predict {} with {:.2f} confidence.".format(person, confidence))
    return person

# path to the face alignment model
dLib_predictor = "../../resource/shaqpe_predictor_68_face_landmarks.dat"
# construct the face alignment model
align = openface.AlignDlib(dLib_predictor)
# path to deep neural network for feature representation
network_model = "../../resource/nn4.small2.v1.t7"
# construct the network for feature represenation
net = openface.TorchNeuralNet(network_model, 96)

classifier_model = "../../resource/svm.pkl"

with open(classifier_model, 'r') as f:
    (le, clf) = pickle.load(f)
    print("Successfully loaded SVM model")
    video = cv2.VideoCapture(0)
    if video is None:
        exit()

    while True:
        # grab image
        ret, cameraFrame = video.read()
        if not ret:
            exit()
        try:
            bbs = align.getAllFaceBoundingBoxes(cameraFrame)
            print("Found {} face".format(len(bbs)))
            for bb2 in bbs:
                alignedFace = align.align(96, cameraFrame, bb2,
                                            landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)
                id = classify(alignedFace, net, clf, le)
                person_name = id_name[id]
                print(person_name)

                rectColor = (0,255, 0)
                textColor = (255, 0, 0)
                face_top_left = (bb2.left(), bb2.top())
                face_bottom_right = (bb2.right(), bb2.bottom())
                cv2.rectangle(cameraFrame, face_top_left, face_bottom_right, rectColor)
                cv2.putText(cameraFrame, person_name, face_top_left,
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=textColor, thickness=2)

            cv2.imshow('FaceRecognizer', cameraFrame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        except:
            cv2.imshow('FaceRecognize', cameraFrame)
            continue

Use the model to classify face in local camera.

Response:
import openface
import numpy as np
import os
import cv2
import pickle

id_name = ["Alec", "Greg", "Phong", "Emil"]

def classify(aligned_face, net, clf, le):
    rep = net.forward(aligned_face)
    predictions = clf.predict_proba(rep.reshape((1, len(rep)))).ravel()
    maxI = np.argmax(predictions)
    person = le.inverse_transform(maxI)
    confidence = predictions[maxI]
    print("Predict {} with {:.2f} confidence.".format(person, confidence))
    return person

# path to the face alignment model
dLib_predictor = "../../resource/shaqpe_predictor_68_face_landmarks.dat"
# construct the face alignment model
align = openface.AlignDlib(dLib_predictor)
# path to deep neural network for feature representation
network_model = "../../resource/nn4.small2.v1.t7"
# construct the network for feature represenation
net = openface.TorchNeuralNet(network_model, 96)

classifier_model = "../../resource/svm.pkl"

with open(classifier_model, 'r') as f:
    (le, clf) = pickle.load(f)
    print("Successfully loaded SVM model")
    video = cv2.VideoCapture(0)
    if video is None:
        exit()

    while True:
        # grab image
        ret, cameraFrame = video.read()
        if not ret:
            exit()
        try:
            bbs = align.getAllFaceBoundingBoxes(cameraFrame)
            print("Found {} face".format(len(bbs)))
            for bb2 in bbs:
                alignedFace = align.align(96, cameraFrame, bb2,
                                            landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)
                id = classify(alignedFace, net, clf, le)
                person_name = id_name[id]
                print(person_name)

                rectColor = (0,255, 0)
                textColor = (255, 0, 0)
                face_top_left = (bb2.left(), bb2.top())
                face_bottom_right = (bb2.right(), bb2.bottom())
                cv2.rectangle(cameraFrame, face_top_left, face_bottom_right, rectColor)
                cv2.putText(cameraFrame, person_name, face_top_left,
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=textColor, thickness=2)

            cv2.imshow('FaceRecognizer', cameraFrame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        except:
            cv2.imshow('FaceRecognize', cameraFrame)
            continue

Prompt: Use the model to classify face in local camera.


# Add a manual scheduler for building release candidates
rc_scheduler = ForceScheduler(
    name="rc build",
    builderNames=["package_osx10.9-x64", "package_win6.2-x64", "package_win6.2-x86", "package_tarball64", "package_tarball32", "package_tarballarm", "package_tarballppc64le"],
    reason=FixedParameter(name="reason", default=""),
    branch=FixedParameter(name="branch", default=""),
    repository=FixedParameter(name="repository", default=""),
    project=FixedParameter(name="project", default="Packaging"),
    properties=[
    ]
)
c['schedulers'].append(rc_scheduler)

Add ppc64le tarball rc force builder

Add ppc64le tarball rc force builder

Response:
# Add a manual scheduler for building release candidates
rc_scheduler = ForceScheduler(
    name="rc build",
    builderNames=["package_osx10.9-x64", "package_win6.2-x64", "package_win6.2-x86", "package_tarball64", "package_tarball32", "package_tarballarm", "package_tarballppc64le"],
    reason=FixedParameter(name="reason", default=""),
    branch=FixedParameter(name="branch", default=""),
    repository=FixedParameter(name="repository", default=""),
    project=FixedParameter(name="project", default="Packaging"),
    properties=[
    ]
)
c['schedulers'].append(rc_scheduler)

Prompt:
Add ppc64le tarball rc force builder

# Add a manual scheduler for building release candidates
rc_scheduler = ForceScheduler(
    name="rc build",
    builderNames=["package_osx10.9-x64", "package_win6.2-x64", "package_win6.2-x86", "package_tarball64", "package_tarball32", "package_tarballarm"],
    reason=FixedParameter(name="reason", default=""),
    branch=FixedParameter(name="branch", default=""),
    repository=FixedParameter(name="repository", default=""),
    project=FixedParameter(name="project", default="Packaging"),
    properties=[
    ]
)
c['schedulers'].append(rc_scheduler)


#!/usr/bin/env python
import sys
from os.path import dirname, abspath

from django.conf import settings

if not settings.configured:
    settings.configure(
        DATABASE_ENGINE='django.db.backends.postgresql_psycopg2',
        DATABASE_NAME='bitfield_test',
        INSTALLED_APPS=[
            'django.contrib.contenttypes',
            'bitfield',
            'bitfield.tests',
        ],
        ROOT_URLCONF='',
        DEBUG=False,
    )

from django.test.simple import run_tests

def runtests(*test_args):
    if not test_args:
        test_args = ['bitfield']
    parent = dirname(abspath(__file__))
    sys.path.insert(0, parent)
    failures = run_tests(test_args, verbosity=1, interactive=True)
    sys.exit(failures)

if __name__ == '__main__':
    runtests(*sys.argv[1:])

Response:
#!/usr/bin/env python
import sys
from os.path import dirname, abspath

from django.conf import settings

if not settings.configured:
    settings.configure(
        DATABASE_ENGINE='django.db.backends.postgresql_psycopg2',
        DATABASE_NAME='bitfield_test',
        INSTALLED_APPS=[
            'django.contrib.contenttypes',
            'bitfield',
            'bitfield.tests',
        ],
        ROOT_URLCONF='',
        DEBUG=False,
    )

from django.test.simple import run_tests

def runtests(*test_args):
    if not test_args:
        test_args = ['bitfield']
    parent = dirname(abspath(__file__))
    sys.path.insert(0, parent)
    failures = run_tests(test_args, verbosity=1, interactive=True)
    sys.exit(failures)

if __name__ == '__main__':
    runtests(*sys.argv[1:])

Prompt:
Test suite requires me to spell contenttypes correctly

#!/usr/bin/env python
import sys
from os.path import dirname, abspath

from django.conf import settings

if not settings.configured:
    settings.configure(
        DATABASE_ENGINE='django.db.backends.postgresql_psycopg2',
        DATABASE_NAME='bitfield_test',
        INSTALLED_APPS=[
            'django.contrib.contettypes',
            'bitfield',
            'bitfield.tests',
        ],
        ROOT_URLCONF='',
        DEBUG=False,
    )

from django.test.simple import run_tests

def runtests(*test_args):
    if not test_args:
        test_args = ['bitfield']
    parent = dirname(abspath(__file__))
    sys.path.insert(0, parent)
    failures = run_tests(test_args, verbosity=1, interactive=True)
    sys.exit(failures)

if __name__ == '__main__':
    runtests(*sys.argv[1:])


import logging

from openspending.lib import cubes
from openspending import migration, model, mongo

log = logging.getLogger(__name__)

def up():
    group_args = ({'dataset':1}, {}, {'num': 0},
                  'function (x, acc) { acc.num += 1 }')

    before = mongo.db.dimension.group(*group_args)
    dims = model.dimension.find({'type': {'$nin': ['entity', 'classifier']}})
    for d in dims:
        log.info("Removing dimension: %s", d)
        model.dimension.remove({'_id': d['_id']})
    after = mongo.db.dimension.group(*group_args)

    for bf, af in zip(before, after):
        if int(bf['num']) != int(af['num']):
            log.warn("Number of dimensions for dimension '%s' "
                     "changed. Recomputing cubes.", bf['dataset'])
            ds = model.dataset.find_one({'name': bf['dataset']})
            cubes.Cube.update_all_cubes(ds)

def down():
    raise migration.IrreversibleMigrationError("Can't add back dimension "
                                               "fields that we dropped!")

Add migration to remove non-{entity,classifier} dimensions from the database, and to recompute cubes if necessary

Response:
import logging

from openspending.lib import cubes
from openspending import migration, model, mongo

log = logging.getLogger(__name__)

def up():
    group_args = ({'dataset':1}, {}, {'num': 0},
                  'function (x, acc) { acc.num += 1 }')

    before = mongo.db.dimension.group(*group_args)
    dims = model.dimension.find({'type': {'$nin': ['entity', 'classifier']}})
    for d in dims:
        log.info("Removing dimension: %s", d)
        model.dimension.remove({'_id': d['_id']})
    after = mongo.db.dimension.group(*group_args)

    for bf, af in zip(before, after):
        if int(bf['num']) != int(af['num']):
            log.warn("Number of dimensions for dimension '%s' "
                     "changed. Recomputing cubes.", bf['dataset'])
            ds = model.dataset.find_one({'name': bf['dataset']})
            cubes.Cube.update_all_cubes(ds)

def down():
    raise migration.IrreversibleMigrationError("Can't add back dimension "
                                               "fields that we dropped!")

Prompt: Add migration to remove non-{entity,classifier} dimensions from the database, and to recompute cubes if necessary


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

import ibmcnx.functions

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

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

dblist.sort()

for db in dblist:
      #t1 = ibmcnx.functions.getDSId( db )
      print db
#      AdminConfig.show( t1 )
#      print '\n\n'
#      AdminConfig.showall( t1 )
#      AdminConfig.showAttribute(t1,'statementCacheSize' )
#      AdminConfig.showAttribute(t1,'[statementCacheSize]' )

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

import ibmcnx.functions

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

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

dblist.sort()

for db in dblist:
      #t1 = ibmcnx.functions.getDSId( db )
      print db
#      AdminConfig.show( t1 )
#      print '\n\n'
#      AdminConfig.showall( t1 )
#      AdminConfig.showAttribute(t1,'statementCacheSize' )
#      AdminConfig.showAttribute(t1,'[statementCacheSize]' )

Prompt:
8: Create documentation of DataSource Settings

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

import ibmcnx.functions

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

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

dblist.sort()

for db in dblist:
      t1 = ibmcnx.functions.getDSId( db )
      print t1
#      AdminConfig.show( t1 )
#      print '\n\n'
#      AdminConfig.showall( t1 )
#      AdminConfig.showAttribute(t1,'statementCacheSize' )
#      AdminConfig.showAttribute(t1,'[statementCacheSize]' )


### -*- coding: utf-8 -*-
###
### © 2012 Krux Digital, Inc.
### Author: Paul Lathrop <paul@krux.com>
###

from setuptools import setup, find_packages

setup(name='pysecurity-groups',
      version="1.0.0",
      description='Library for working with EC2 security groups in bulk.',
      author='Paul Lathrop',
      author_email='paul@krux.com',
      url='https://github.com/krux/pysecurity-groups',
      packages=find_packages(),
      install_requires=['boto', 'argparse', 'IPy'],
      entry_points={'console_scripts':
                    ['security-groups = pysecurity_groups.cli:main'] },
      )

Update release version to 1.0.0

Update release version to 1.0.0

Response:
### -*- coding: utf-8 -*-
###
### © 2012 Krux Digital, Inc.
### Author: Paul Lathrop <paul@krux.com>
###

from setuptools import setup, find_packages

setup(name='pysecurity-groups',
      version="1.0.0",
      description='Library for working with EC2 security groups in bulk.',
      author='Paul Lathrop',
      author_email='paul@krux.com',
      url='https://github.com/krux/pysecurity-groups',
      packages=find_packages(),
      install_requires=['boto', 'argparse', 'IPy'],
      entry_points={'console_scripts':
                    ['security-groups = pysecurity_groups.cli:main'] },
      )

Prompt:
Update release version to 1.0.0

### -*- coding: utf-8 -*-
###
### © 2012 Krux Digital, Inc.
### Author: Paul Lathrop <paul@krux.com>
###

from setuptools import setup, find_packages

setup(name='pysecurity-groups',
      version="0.0.1",
      description='Library for working with EC2 security groups in bulk.',
      author='Paul Lathrop',
      author_email='paul@krux.com',
      url='https://github.com/krux/pysecurity-groups',
      packages=find_packages(),
      install_requires=['boto', 'argparse', 'IPy'],
      entry_points={'console_scripts':
                    ['security-groups = pysecurity_groups.cli:main'] },
      )


__version__ = '2.0.0'

def get_account(using=None):
    from django.conf import settings
    accounts = settings.SMSGATEWAY_ACCOUNTS
    if using is not None:
        return accounts[using]
    else:
        return accounts[accounts['__default__']]

def send(to, msg, signature, using=None, reliable=False):
    """
    Send an SMS message immediately.

    *   'to' is a semicolon separated list of phone numbers with an international
        prefix (+32... etc).
    *   'msg' is the message itself as a unicode object (max 160 characters).
    *   'signature' is where the message comes from. Depends on the backend in use.
    *   'using' is an optional parameter where you can specify a specific account
        to send messages from.
    """
    from smsgateway.backends import get_backend
    from smsgateway.sms import SMSRequest
    account_dict = get_account(using)
    backend = get_backend(account_dict['backend'])
    sms_request = SMSRequest(to, msg, signature, reliable=reliable)
    return backend.send(sms_request, account_dict)

def send_queued(to, msg, signature, using=None, reliable=False, priority=None):
    """
    Place SMS message in queue to be sent.
    """
    from smsgateway.models import QueuedSMS
    queued_sms = QueuedSMS(
        to=to,
        content=msg,
        signature=signature,
        using=using if using is not None else '__none__',
        reliable=reliable
    )
    if priority is not None:
        queued_sms.priority = priority
    queued_sms.save()

Add priority option to send_queued

Add priority option to send_queued

Response:
__version__ = '2.0.0'

def get_account(using=None):
    from django.conf import settings
    accounts = settings.SMSGATEWAY_ACCOUNTS
    if using is not None:
        return accounts[using]
    else:
        return accounts[accounts['__default__']]

def send(to, msg, signature, using=None, reliable=False):
    """
    Send an SMS message immediately.

    *   'to' is a semicolon separated list of phone numbers with an international
        prefix (+32... etc).
    *   'msg' is the message itself as a unicode object (max 160 characters).
    *   'signature' is where the message comes from. Depends on the backend in use.
    *   'using' is an optional parameter where you can specify a specific account
        to send messages from.
    """
    from smsgateway.backends import get_backend
    from smsgateway.sms import SMSRequest
    account_dict = get_account(using)
    backend = get_backend(account_dict['backend'])
    sms_request = SMSRequest(to, msg, signature, reliable=reliable)
    return backend.send(sms_request, account_dict)

def send_queued(to, msg, signature, using=None, reliable=False, priority=None):
    """
    Place SMS message in queue to be sent.
    """
    from smsgateway.models import QueuedSMS
    queued_sms = QueuedSMS(
        to=to,
        content=msg,
        signature=signature,
        using=using if using is not None else '__none__',
        reliable=reliable
    )
    if priority is not None:
        queued_sms.priority = priority
    queued_sms.save()

Prompt:
Add priority option to send_queued

__version__ = '2.0.0'

def get_account(using=None):
    from django.conf import settings
    accounts = settings.SMSGATEWAY_ACCOUNTS
    if using is not None:
        return accounts[using]
    else:
        return accounts[accounts['__default__']]

def send(to, msg, signature, using=None, reliable=False):
    """
    Send an SMS message immediately.

    *   'to' is a semicolon separated list of phone numbers with an international
        prefix (+32... etc).
    *   'msg' is the message itself as a unicode object (max 160 characters).
    *   'signature' is where the message comes from. Depends on the backend in use.
    *   'using' is an optional parameter where you can specify a specific account
        to send messages from.
    """
    from smsgateway.backends import get_backend
    from smsgateway.sms import SMSRequest
    account_dict = get_account(using)
    backend = get_backend(account_dict['backend'])
    sms_request = SMSRequest(to, msg, signature, reliable=reliable)
    return backend.send(sms_request, account_dict)

def send_queued(to, msg, signature, using=None, reliable=False):
    """
    Place SMS message in queue to be sent.
    """
    from smsgateway.models import QueuedSMS
    QueuedSMS.objects.create(
        to=to,
        content=msg,
        signature=signature,
        using=using if using is not None else '__none__',
        reliable=reliable
    )


#!/usr/bin/env python

import argparse

from githublist.parser import main as get_data
from githublist.serve import serve_content

parser = argparse.ArgumentParser(description='View repositories for any GitHub account.')
parser.add_argument('user', type=str, nargs='+', help='GitHub user handle')
parser.add_argument('-f', '--format', nargs='+',
                    choices=['json', 'csv', 'md', 'raw.txt', 'tbl.txt', 'all'],
                    help='File output format.')

def main():
    args = parser.parse_args()
    user = args.user
    format_ = ['json', 'csv', 'md', 'raw.txt', 'tbl.txt'] if 'all' in args.format else args.format

    for u in user:
        print('Preparing data for {}...'.format(u))
        d = get_data(u)
        for f in format_:
            if f is not None:
                print(' Writing {}...'.format(f), end='')
                serve_content(d, u, f)
                print(' Done!')
            else:
                serve_content(d, u, f)
    print('Complete!')
    return None

if __name__ == '__main__':
    main()

Response:
#!/usr/bin/env python

import argparse

from githublist.parser import main as get_data
from githublist.serve import serve_content

parser = argparse.ArgumentParser(description='View repositories for any GitHub account.')
parser.add_argument('user', type=str, nargs='+', help='GitHub user handle')
parser.add_argument('-f', '--format', nargs='+',
                    choices=['json', 'csv', 'md', 'raw.txt', 'tbl.txt', 'all'],
                    help='File output format.')

def main():
    args = parser.parse_args()
    user = args.user
    format_ = ['json', 'csv', 'md', 'raw.txt', 'tbl.txt'] if 'all' in args.format else args.format

    for u in user:
        print('Preparing data for {}...'.format(u))
        d = get_data(u)
        for f in format_:
            if f is not None:
                print(' Writing {}...'.format(f), end='')
                serve_content(d, u, f)
                print(' Done!')
            else:
                serve_content(d, u, f)
    print('Complete!')
    return None

if __name__ == '__main__':
    main()

Prompt:
Add support for multiple users, format types

#!/usr/bin/env python

import argparse

from githublist.parser import main as get_data
from githublist.serve import serve_content

parser = argparse.ArgumentParser(description='View repositories for any GitHub account.')
parser.add_argument('user', type=str, help='GitHub user handle')
parser.add_argument('-f', '--format', choices=['json', 'csv', 'md', 'raw.txt', 'tbl.txt'],
                    help='File output format.')

def main():
    args = parser.parse_args()
    user, format_ = args.user, args.format

    return serve_content(get_data(user), user, format_)

if __name__ == '__main__':
    main()


import sys
import bsddb3
from xclib.utf8 import utf8, unutf8

def perform(args):
    domain_db = bsddb3.hashopen(args.domain_db, 'c', 0o600)
    if args.get:
        print(unutf8(domain_db[utf8(args.get)], 'illegal'))
    elif args.put:
        domain_db[utf8(args.put[0])] = args.put[1]
    elif args.delete:
        del domain_db[utf8(args.delete)]
    elif args.unload:
        for k in list(domain_db.keys()):
            print('%s\t%s' % (unutf8(k, 'illegal'), unutf8(domain_db[k], 'illegal')))
        # Should work according to documentation, but doesn't
        # for k, v in DOMAIN_DB.iteritems():
        #     print k, '\t', v
    elif args.load:
        for line in sys.stdin:
            k, v = line.rstrip('\r\n').split('\t', 1)
            domain_db[utf8(k)] = v
    domain_db.close()

# vim: tabstop=8 softtabstop=0 expandtab shiftwidth=4

Allow dumping illegal utf-8 contents

Allow dumping illegal utf-8 contents

Response:
import sys
import bsddb3
from xclib.utf8 import utf8, unutf8

def perform(args):
    domain_db = bsddb3.hashopen(args.domain_db, 'c', 0o600)
    if args.get:
        print(unutf8(domain_db[utf8(args.get)], 'illegal'))
    elif args.put:
        domain_db[utf8(args.put[0])] = args.put[1]
    elif args.delete:
        del domain_db[utf8(args.delete)]
    elif args.unload:
        for k in list(domain_db.keys()):
            print('%s\t%s' % (unutf8(k, 'illegal'), unutf8(domain_db[k], 'illegal')))
        # Should work according to documentation, but doesn't
        # for k, v in DOMAIN_DB.iteritems():
        #     print k, '\t', v
    elif args.load:
        for line in sys.stdin:
            k, v = line.rstrip('\r\n').split('\t', 1)
            domain_db[utf8(k)] = v
    domain_db.close()

# vim: tabstop=8 softtabstop=0 expandtab shiftwidth=4

Prompt:
Allow dumping illegal utf-8 contents

import sys
import bsddb3
from xclib.utf8 import utf8, unutf8

def perform(args):
    domain_db = bsddb3.hashopen(args.domain_db, 'c', 0o600)
    if args.get:
        print(unutf8(domain_db[utf8(args.get)]))
    elif args.put:
        domain_db[utf8(args.put[0])] = args.put[1]
    elif args.delete:
        del domain_db[utf8(args.delete)]
    elif args.unload:
        for k in list(domain_db.keys()):
            print('%s\t%s' % (unutf8(k), unutf8(domain_db[k])))
        # Should work according to documentation, but doesn't
        # for k, v in DOMAIN_DB.iteritems():
        #     print k, '\t', v
    elif args.load:
        for line in sys.stdin:
            k, v = line.rstrip('\r\n').split('\t', 1)
            domain_db[utf8(k)] = v
    domain_db.close()

# vim: tabstop=8 softtabstop=0 expandtab shiftwidth=4


from setuptools import setup, find_packages
import sys

install_reqs = [
    "decorator>=3.3.2"
]
test_reqs = [
     "Pillow>=2.5.0"
]

if sys.version_info[0] == 2:
    # simplejson is not python3 compatible
    install_reqs.append("simplejson>=2.0.9")

if [sys.version_info[0], sys.version_info[1]] < [2, 7]:
    install_reqs.append("argparse>=1.2")

setup(
    name = "dogapi",
    version = "1.9.1",
    packages = find_packages("src"),
    package_dir = {'':'src'},
    author = "Datadog, Inc.",
    author_email = "packages@datadoghq.com",
    description = "Python bindings to Datadog's API and a user-facing command line tool.",
    license = "BSD",
    keywords = "datadog data",
    url = "http://www.datadoghq.com",
    install_requires = install_reqs,
    tests_require = test_reqs,
    entry_points = {
        'console_scripts': [
            'dog = dogshell:main',
            'dogwrap = dogshell.wrap:main',
        ],
    },
)

Move Pillow to tests requirements

Move Pillow to tests requirements

Response:
from setuptools import setup, find_packages
import sys

install_reqs = [
    "decorator>=3.3.2"
]
test_reqs = [
     "Pillow>=2.5.0"
]

if sys.version_info[0] == 2:
    # simplejson is not python3 compatible
    install_reqs.append("simplejson>=2.0.9")

if [sys.version_info[0], sys.version_info[1]] < [2, 7]:
    install_reqs.append("argparse>=1.2")

setup(
    name = "dogapi",
    version = "1.9.1",
    packages = find_packages("src"),
    package_dir = {'':'src'},
    author = "Datadog, Inc.",
    author_email = "packages@datadoghq.com",
    description = "Python bindings to Datadog's API and a user-facing command line tool.",
    license = "BSD",
    keywords = "datadog data",
    url = "http://www.datadoghq.com",
    install_requires = install_reqs,
    tests_require = test_reqs,
    entry_points = {
        'console_scripts': [
            'dog = dogshell:main',
            'dogwrap = dogshell.wrap:main',
        ],
    },
)

Prompt:
Move Pillow to tests requirements

from setuptools import setup, find_packages
import sys

reqs = [
    "decorator>=3.3.2",
    "Pillow>=2.5.0"
]
if sys.version_info[0] == 2:
    # simplejson is not python3 compatible
    reqs.append("simplejson>=2.0.9")

if [sys.version_info[0], sys.version_info[1]] < [2, 7]:
    reqs.append("argparse>=1.2")

setup(
    name = "dogapi",
    version = "1.9.1",
    packages = find_packages("src"),
    package_dir = {'':'src'},
    author = "Datadog, Inc.",
    author_email = "packages@datadoghq.com",
    description = "Python bindings to Datadog's API and a user-facing command line tool.",
    license = "BSD",
    keywords = "datadog data",
    url = "http://www.datadoghq.com",
    install_requires = reqs,
    entry_points={
        'console_scripts': [
            'dog = dogshell:main',
            'dogwrap = dogshell.wrap:main',
        ],
    },
)


#!/usr/bin/env python3

import i3ipc

# make connection to i3 ipc
i3 = i3ipc.Connection()

# check if workspaces are all in order
def workspaces_ordered(i3conn):
    last_workspace_number = 0
    for i in sorted(i3conn.get_workspaces(), key=lambda x: x['num']):
        if i['num'] != last_workspace_number+1:
            return False
        last_workspace_number += 1
    return True

# find all the workspaces that are out of order and
# the least possible valid workspace number that is unassigned
def find_disordered(i3conn):
    last_workspace_number = 0
    disordered = []
    least_number = None
    for i in sorted(i3conn.get_workspaces(), key=lambda x: x['num']):
        if i['num'] != last_workspace_number+1:
            disordered.append(i['num'])
            if least_number is None:
                least_number = last_workspace_number + 1
        last_workspace_number += 1
    return (disordered, least_number)

# renumber all the workspaces that appear out of order from the others
def fix_ordering(i3conn):
    if workspaces_ordered(i3conn):
        return
    else:
        workspaces = i3conn.get_tree().workspaces()
        disordered_workspaces,least_number = find_disordered(i3conn)
        containers = list(filter(lambda x: x.num in disordered_workspaces, workspaces))
        for c in containers:
            for i in c.leaves():
                i.command("move container to workspace %s" % least_number)
            least_number += 1
    return

# callback for when workspace focus changes
def on_workspace_focus(i3conn, e):
    fix_ordering(i3conn)

if __name__ == '__main__':
    i3.on('workspace::focus', on_workspace_focus)
    i3.main()

Add script that makes sure workspace numbers are always in consecutive order by moving windows on out of order workspaces to the appropriate workspace

Response:
#!/usr/bin/env python3

import i3ipc

# make connection to i3 ipc
i3 = i3ipc.Connection()

# check if workspaces are all in order
def workspaces_ordered(i3conn):
    last_workspace_number = 0
    for i in sorted(i3conn.get_workspaces(), key=lambda x: x['num']):
        if i['num'] != last_workspace_number+1:
            return False
        last_workspace_number += 1
    return True

# find all the workspaces that are out of order and
# the least possible valid workspace number that is unassigned
def find_disordered(i3conn):
    last_workspace_number = 0
    disordered = []
    least_number = None
    for i in sorted(i3conn.get_workspaces(), key=lambda x: x['num']):
        if i['num'] != last_workspace_number+1:
            disordered.append(i['num'])
            if least_number is None:
                least_number = last_workspace_number + 1
        last_workspace_number += 1
    return (disordered, least_number)

# renumber all the workspaces that appear out of order from the others
def fix_ordering(i3conn):
    if workspaces_ordered(i3conn):
        return
    else:
        workspaces = i3conn.get_tree().workspaces()
        disordered_workspaces,least_number = find_disordered(i3conn)
        containers = list(filter(lambda x: x.num in disordered_workspaces, workspaces))
        for c in containers:
            for i in c.leaves():
                i.command("move container to workspace %s" % least_number)
            least_number += 1
    return

# callback for when workspace focus changes
def on_workspace_focus(i3conn, e):
    fix_ordering(i3conn)

if __name__ == '__main__':
    i3.on('workspace::focus', on_workspace_focus)
    i3.main()

Prompt:
Add script that makes sure workspace numbers are always in consecutive
order by moving windows on out of order workspaces to the appropriate
workspace


import os

"""
Loader settings:
we use the GMQL scala class CombineTextFileWithPathInputFormat in order to load the region and metadata files with the
same id_sample based on the hash of the file name
"""

inputFormatClass = 'it.polimi.genomics.spark.implementation.loaders.Loaders$CombineTextFileWithPathInputFormat'
keyFormatClass = 'org.apache.hadoop.io.LongWritable'
valueFormatClass = 'org.apache.hadoop.io.Text'
defaultCombineSize = 64

# configuration of the Hadoop loader used by spark
conf = {"textinputformat.record.delimiter": "\n",
        "mapreduce.input.fileinputformat.input.dir.recursive": "true",
        "mapred.max.split.size": str(defaultCombineSize*1024*1024)
        }

"""
    Generation of the index of the pandas dataframe.
    This can be done in different ways:
    - hashing the complete file name
    - using directly the file as index (this is visually appealing :) )
"""

def generateHashKey(filename):
    return hash(filename)

def generateNameKey(filename):
    filename = os.path.basename(filename)
    if filename.endswith(".meta"):
        return filename[:-5]
    else:
        return filename

Use only the base file name for key

Response:
import os

"""
Loader settings:
we use the GMQL scala class CombineTextFileWithPathInputFormat in order to load the region and metadata files with the
same id_sample based on the hash of the file name
"""

inputFormatClass = 'it.polimi.genomics.spark.implementation.loaders.Loaders$CombineTextFileWithPathInputFormat'
keyFormatClass = 'org.apache.hadoop.io.LongWritable'
valueFormatClass = 'org.apache.hadoop.io.Text'
defaultCombineSize = 64

# configuration of the Hadoop loader used by spark
conf = {"textinputformat.record.delimiter": "\n",
        "mapreduce.input.fileinputformat.input.dir.recursive": "true",
        "mapred.max.split.size": str(defaultCombineSize*1024*1024)
        }

"""
    Generation of the index of the pandas dataframe.
    This can be done in different ways:
    - hashing the complete file name
    - using directly the file as index (this is visually appealing :) )
"""

def generateHashKey(filename):
    return hash(filename)

def generateNameKey(filename):
    filename = os.path.basename(filename)
    if filename.endswith(".meta"):
        return filename[:-5]
    else:
        return filename

Prompt:
Use only the base file name for key

Former-commit-id: 3acbf9e93d3d501013e2a1b6aa9631bdcc663c66 [formerly eea949727d3693aa032033d84dcca3790d9072dd] [formerly ca065bc9f78b1416903179418d64b3273d437987]
Former-commit-id: 58c1d18e8960ad3236a34b8080b18ccff5684eaa
Former-commit-id: 6501068cea164df37ec055c3fb8baf8c89fc7823
"""
Loader settings:
we use the GMQL scala class CombineTextFileWithPathInputFormat in order to load the region and metadata files with the
same id_sample based on the hash of the file name
"""

inputFormatClass = 'it.polimi.genomics.spark.implementation.loaders.Loaders$CombineTextFileWithPathInputFormat'
keyFormatClass = 'org.apache.hadoop.io.LongWritable'
valueFormatClass = 'org.apache.hadoop.io.Text'
defaultCombineSize = 64

# configuration of the Hadoop loader used by spark
conf = {"textinputformat.record.delimiter": "\n",
        "mapreduce.input.fileinputformat.input.dir.recursive": "true",
        "mapred.max.split.size": str(defaultCombineSize*1024*1024)
        }

def generateHashKey(filename):
    return hash(filename)

def generateNameKey(filename):
    if filename.endswith(".meta"):
        return filename[:-5]
    else:
        return filename


def prime_factors(n):
    factors = []
    factor = 2
    while n != 1:
        while n % factor == 0:
            factors += [factor]
            n //= factor
        factor += 1
    return factors

Response:
def prime_factors(n):
    factors = []
    factor = 2
    while n != 1:
        while n % factor == 0:
            factors += [factor]
            n //= factor
        factor += 1
    return factors

Prompt:
Fix memory issues by just trying every number

import sieve

def prime_factors(n):
    primes = sieve.sieve(n)
    factors = []
    for p in primes:
        while n % p == 0:
            factors += [p]
            n //= p
    return factors


import unittest

class ExampleTest(unittest.TestCase):
    def test_example(self):
        self.assertEqual(1, 1)

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

Fix typo in method name

Fix typo in method name

Response:
import unittest

class ExampleTest(unittest.TestCase):
    def test_example(self):
        self.assertEqual(1, 1)

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

Prompt:
Fix typo in method name
import unittest

class ExampleTest(unittest.TestCase):
    def test_xample(self):
        self.assertEqual(1, 1)

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


"""Use native postgres JSON type for Entry.content

Revision ID: 588336e02ca
Revises: 2b7f5e38dd73
Create Date: 2014-01-09 22:40:07.690000

"""

# revision identifiers, used by Alembic.
revision = '588336e02ca'
down_revision = '2b7f5e38dd73'

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    # Couldn't find a way to specify a USING clause with alembic's alter_table
    op.execute('ALTER TABLE entry ALTER COLUMN content TYPE json USING content::json')
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('entry', 'content',
               type_=sa.TEXT(),
               nullable=False)
    ### end Alembic commands ###

Add migration for native postgres JSON type

Response:
"""Use native postgres JSON type for Entry.content

Revision ID: 588336e02ca
Revises: 2b7f5e38dd73
Create Date: 2014-01-09 22:40:07.690000

"""

# revision identifiers, used by Alembic.
revision = '588336e02ca'
down_revision = '2b7f5e38dd73'

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    # Couldn't find a way to specify a USING clause with alembic's alter_table
    op.execute('ALTER TABLE entry ALTER COLUMN content TYPE json USING content::json')
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('entry', 'content',
               type_=sa.TEXT(),
               nullable=False)
    ### end Alembic commands ###

Prompt: Add migration for native postgres JSON type


from setuptools import setup

setup(
    name='tangled.contrib',
    version='0.1a4.dev0',
    description='Tangled namespace for contributed packages',
    long_description=open('README.rst').read(),
    url='http://tangledframework.org/',
    download_url='https://github.com/TangledWeb/tangled.contrib/tags',
    author='Wyatt Baldwin',
    author_email='self@wyattbaldwin.com',
    packages=[
        'tangled',
        'tangled.contrib',
        'tangled.contrib.scaffolds',
        'tangled.contrib.tests',
    ],
    include_package_data=True,
    install_requires=[
        'tangled>=0.1a8',
    ],
    extras_require={
        'dev': [
            'tangled[dev]>=0.1a8',
        ],
    },
    entry_points="""
    [tangled.scaffolds]
    contrib = tangled.contrib.scaffolds:default
    contrib-core = tangled.contrib.scaffolds:core

    """,
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
    ],
)

Upgrade tangled from 0.1a5 to 0.1a8

Upgrade tangled from 0.1a5 to 0.1a8

Response:
from setuptools import setup

setup(
    name='tangled.contrib',
    version='0.1a4.dev0',
    description='Tangled namespace for contributed packages',
    long_description=open('README.rst').read(),
    url='http://tangledframework.org/',
    download_url='https://github.com/TangledWeb/tangled.contrib/tags',
    author='Wyatt Baldwin',
    author_email='self@wyattbaldwin.com',
    packages=[
        'tangled',
        'tangled.contrib',
        'tangled.contrib.scaffolds',
        'tangled.contrib.tests',
    ],
    include_package_data=True,
    install_requires=[
        'tangled>=0.1a8',
    ],
    extras_require={
        'dev': [
            'tangled[dev]>=0.1a8',
        ],
    },
    entry_points="""
    [tangled.scaffolds]
    contrib = tangled.contrib.scaffolds:default
    contrib-core = tangled.contrib.scaffolds:core

    """,
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
    ],
)

Prompt:
Upgrade tangled from 0.1a5 to 0.1a8

from setuptools import setup

setup(
    name='tangled.contrib',
    version='0.1a4.dev0',
    description='Tangled namespace for contributed packages',
    long_description=open('README.rst').read(),
    url='http://tangledframework.org/',
    download_url='https://github.com/TangledWeb/tangled.contrib/tags',
    author='Wyatt Baldwin',
    author_email='self@wyattbaldwin.com',
    packages=[
        'tangled',
        'tangled.contrib',
        'tangled.contrib.scaffolds',
        'tangled.contrib.tests',
    ],
    include_package_data=True,
    install_requires=[
        'tangled>=0.1a5',
    ],
    extras_require={
        'dev': [
            'tangled[dev]>=0.1a5',
        ],
    },
    entry_points="""
    [tangled.scaffolds]
    contrib = tangled.contrib.scaffolds:default
    contrib-core = tangled.contrib.scaffolds:core

    """,
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
    ],
)


import pytest
from gi.repository import Gtk

from gaphas.item import Element
from gaphas.tool.placement import PlacementState, on_drag_begin, placement_tool

@pytest.fixture
def tool_factory(connections):
    def tool_factory():
        return Element(connections)

    return tool_factory

def test_can_create_placement_tool(view, tool_factory):
    tool = placement_tool(view, tool_factory, 2)

    assert isinstance(tool, Gtk.Gesture)

def test_create_new_element(view, tool_factory, window):
    state = PlacementState(tool_factory, 2)
    tool = placement_tool(view, tool_factory, 2)

    on_drag_begin(tool, 0, 0, state)

    assert state.moving

Add some extra tests for placement

Add some extra tests for placement

Response:
import pytest
from gi.repository import Gtk

from gaphas.item import Element
from gaphas.tool.placement import PlacementState, on_drag_begin, placement_tool

@pytest.fixture
def tool_factory(connections):
    def tool_factory():
        return Element(connections)

    return tool_factory

def test_can_create_placement_tool(view, tool_factory):
    tool = placement_tool(view, tool_factory, 2)

    assert isinstance(tool, Gtk.Gesture)

def test_create_new_element(view, tool_factory, window):
    state = PlacementState(tool_factory, 2)
    tool = placement_tool(view, tool_factory, 2)

    on_drag_begin(tool, 0, 0, state)

    assert state.moving

Prompt: Add some extra tests for placement


import logging
import os

import yaml

import monsetup.agent_config
import monsetup.detection

log = logging.getLogger(__name__)

class Zookeeper(monsetup.detection.Plugin):

    """Detect Zookeeper daemons and setup configuration to monitor them.

    """

    def _detect(self):
        """Run detection, set self.available True if the service is detected.

        """
        if monsetup.detection.find_process_cmdline('org.apache.zookeeper') is not None:
            self.available = True

    def build_config(self):
        """Build the config as a Plugins object and return.

        """
        config = monsetup.agent_config.Plugins()
        # First watch the process
        log.info("\tWatching the zookeeper process.")
        config.merge(monsetup.detection.watch_process(['zookeeper']))

        log.info("\tEnabling the zookeeper plugin")
        with open(os.path.join(self.template_dir, 'conf.d/zk.yaml.example'), 'r') as zk_template:
            zk_config = yaml.load(zk_template.read())
        config['zk'] = zk_config

        return config

    def dependencies_installed(self):
        # The current plugin just does a simple socket connection to zookeeper and
        # parses the stat command
        return True

Response:
import logging
import os

import yaml

import monsetup.agent_config
import monsetup.detection

log = logging.getLogger(__name__)

class Zookeeper(monsetup.detection.Plugin):

    """Detect Zookeeper daemons and setup configuration to monitor them.

    """

    def _detect(self):
        """Run detection, set self.available True if the service is detected.

        """
        if monsetup.detection.find_process_cmdline('org.apache.zookeeper') is not None:
            self.available = True

    def build_config(self):
        """Build the config as a Plugins object and return.

        """
        config = monsetup.agent_config.Plugins()
        # First watch the process
        log.info("\tWatching the zookeeper process.")
        config.merge(monsetup.detection.watch_process(['zookeeper']))

        log.info("\tEnabling the zookeeper plugin")
        with open(os.path.join(self.template_dir, 'conf.d/zk.yaml.example'), 'r') as zk_template:
            zk_config = yaml.load(zk_template.read())
        config['zk'] = zk_config

        return config

    def dependencies_installed(self):
        # The current plugin just does a simple socket connection to zookeeper and
        # parses the stat command
        return True

Prompt:
Fix detection of Zookeeper in monasca-setup

The Zookeeper detection plugin was looking for zookeeper in the process
command-line.  This was producing false positives in the detection
process because storm uses the zookeeper library and it shows up
the command-line for storm.

Change-Id: I764a3064003beec55f0e589272855dadfa0997e7

import logging
import os

import yaml

import monsetup.agent_config
import monsetup.detection

log = logging.getLogger(__name__)

class Zookeeper(monsetup.detection.Plugin):

    """Detect Zookeeper daemons and setup configuration to monitor them.

    """

    def _detect(self):
        """Run detection, set self.available True if the service is detected.

        """
        if monsetup.detection.find_process_cmdline('zookeeper') is not None:
            self.available = True

    def build_config(self):
        """Build the config as a Plugins object and return.

        """
        config = monsetup.agent_config.Plugins()
        # First watch the process
        log.info("\tWatching the zookeeper process.")
        config.merge(monsetup.detection.watch_process(['zookeeper']))

        log.info("\tEnabling the zookeeper plugin")
        with open(os.path.join(self.template_dir, 'conf.d/zk.yaml.example'), 'r') as zk_template:
            zk_config = yaml.load(zk_template.read())
        config['zk'] = zk_config

        return config

    def dependencies_installed(self):
        # The current plugin just does a simple socket connection to zookeeper and
        # parses the stat command
        return True


import django_filters
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _

try:
    from django_filters import rest_framework as filters
except ImportError:  # Back-ward compatible for django-rest-framework<3.7
    from rest_framework import filters
from rest_framework import viewsets
from teryt_tree.models import JednostkaAdministracyjna
from teryt_tree.rest_framework_ext.serializers import \
    JednostkaAdministracyjnaSerializer

def custom_area_filter(queryset, _, value):
    if not value:
        return queryset
    return queryset.area(get_object_or_404(JednostkaAdministracyjna, pk=value))

class JednostkaAdministracyjnaFilter(filters.FilterSet):
    area = django_filters.CharFilter(
        method=custom_area_filter,
        label=_("Area")
    )

    class Meta:
        model = JednostkaAdministracyjna
        fields = ['name', 'category', 'category__level', 'area']

class JednostkaAdministracyjnaViewSet(viewsets.ModelViewSet):
    queryset = (JednostkaAdministracyjna.objects.
                select_related('category').
                prefetch_related('children').
                all())
    serializer_class = JednostkaAdministracyjnaSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_class = JednostkaAdministracyjnaFilter

Response:
import django_filters
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _

try:
    from django_filters import rest_framework as filters
except ImportError:  # Back-ward compatible for django-rest-framework<3.7
    from rest_framework import filters
from rest_framework import viewsets
from teryt_tree.models import JednostkaAdministracyjna
from teryt_tree.rest_framework_ext.serializers import \
    JednostkaAdministracyjnaSerializer

def custom_area_filter(queryset, _, value):
    if not value:
        return queryset
    return queryset.area(get_object_or_404(JednostkaAdministracyjna, pk=value))

class JednostkaAdministracyjnaFilter(filters.FilterSet):
    area = django_filters.CharFilter(
        method=custom_area_filter,
        label=_("Area")
    )

    class Meta:
        model = JednostkaAdministracyjna
        fields = ['name', 'category', 'category__level', 'area']

class JednostkaAdministracyjnaViewSet(viewsets.ModelViewSet):
    queryset = (JednostkaAdministracyjna.objects.
                select_related('category').
                prefetch_related('children').
                all())
    serializer_class = JednostkaAdministracyjnaSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_class = JednostkaAdministracyjnaFilter

Prompt:
Update JednostkaAdministracyjnaFilter for new django-filters

import django_filters
from django.shortcuts import get_object_or_404
try:
    from django_filters import rest_framework as filters
except ImportError:  # Back-ward compatible for django-rest-framework<3.7
    from rest_framework import filters
from rest_framework import viewsets
from teryt_tree.models import JednostkaAdministracyjna
from teryt_tree.rest_framework_ext.serializers import JednostkaAdministracyjnaSerializer

def custom_area_filter(queryset, _, value):
    if not value:
        return queryset
    return queryset.area(get_object_or_404(JednostkaAdministracyjna, pk=value))

class JednostkaAdministracyjnaFilter(filters.FilterSet):
    area = django_filters.CharFilter(action=custom_area_filter)

    class Meta:
        model = JednostkaAdministracyjna
        fields = ['name', 'category', 'category__level', 'area']

class JednostkaAdministracyjnaViewSet(viewsets.ModelViewSet):
    queryset = (JednostkaAdministracyjna.objects.
                select_related('category').
                prefetch_related('children').
                all())
    serializer_class = JednostkaAdministracyjnaSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_class = JednostkaAdministracyjnaFilter


 # -*- coding: utf-8 -*-
##############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.openupgrade import openupgrade

@openupgrade.migrate()
def migrate(cr, version):
    # Drop view that inhibits changing field types. It will be recreated BTW
    cr.execute('drop view if exists report_document_user cascade')

[IMP] document: Remove annoying SQL view that prevents some operations

Response:
 # -*- coding: utf-8 -*-
##############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.openupgrade import openupgrade

@openupgrade.migrate()
def migrate(cr, version):
    # Drop view that inhibits changing field types. It will be recreated BTW
    cr.execute('drop view if exists report_document_user cascade')

Prompt: [IMP] document: Remove annoying SQL view that prevents some operations


"""
sentry.web.frontend.generic
~~~~~~~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2010-2014 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import

from django.views.generic import TemplateView as BaseTemplateView

from sentry.web.helpers import render_to_response

def static_media(request, **kwargs):
    """
    Serve static files below a given point in the directory structure.
    """
    from django.contrib.staticfiles.views import serve

    module = kwargs.get('module')
    path = kwargs.get('path', '')

    if module:
        path = '%s/%s' % (module, path)

    response = serve(request, path, insecure=True)

    # We need CORS for font files
    if path.endswith(('.eot', '.ttf', '.woff', '.js')):
        response['Access-Control-Allow-Origin'] = '*'
    return response

class TemplateView(BaseTemplateView):
    def render_to_response(self, context, **response_kwargs):
        return render_to_response(
            request=self.request,
            template=self.get_template_names(),
            context=context,
            **response_kwargs
        )

Response:
"""
sentry.web.frontend.generic
~~~~~~~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2010-2014 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import

from django.views.generic import TemplateView as BaseTemplateView

from sentry.web.helpers import render_to_response

def static_media(request, **kwargs):
    """
    Serve static files below a given point in the directory structure.
    """
    from django.contrib.staticfiles.views import serve

    module = kwargs.get('module')
    path = kwargs.get('path', '')

    if module:
        path = '%s/%s' % (module, path)

    response = serve(request, path, insecure=True)

    # We need CORS for font files
    if path.endswith(('.eot', '.ttf', '.woff', '.js')):
        response['Access-Control-Allow-Origin'] = '*'
    return response

class TemplateView(BaseTemplateView):
    def render_to_response(self, context, **response_kwargs):
        return render_to_response(
            request=self.request,
            template=self.get_template_names(),
            context=context,
            **response_kwargs
        )

Prompt:
Add CORS headers to js files for local dev

"""
sentry.web.frontend.generic
~~~~~~~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2010-2014 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import

from django.views.generic import TemplateView as BaseTemplateView

from sentry.web.helpers import render_to_response

def static_media(request, **kwargs):
    """
    Serve static files below a given point in the directory structure.
    """
    from django.contrib.staticfiles.views import serve

    module = kwargs.get('module')
    path = kwargs.get('path', '')

    if module:
        path = '%s/%s' % (module, path)

    response = serve(request, path, insecure=True)

    # We need CORS for font files
    if path.endswith(('.eot', '.ttf', '.woff')):
        response['Access-Control-Allow-Origin'] = '*'
    return response

class TemplateView(BaseTemplateView):
    def render_to_response(self, context, **response_kwargs):
        return render_to_response(
            request=self.request,
            template=self.get_template_names(),
            context=context,
            **response_kwargs
        )


# -*- coding: utf-8 -*-
import click

from jungle import __version__

class JungleCLI(click.MultiCommand):

    """Jangle CLI main class"""

    def list_commands(self, ctx):
        """return available modules"""
        return ['ec2', 'elb']

    def get_command(self, ctx, name):
        """get command"""
        mod = __import__('jungle.' + name, None, None, ['cli'])
        return mod.cli

cli = JungleCLI(help="aws operation cli (v{})".format(__version__))

if __name__ == '__main__':
    cli()

Add version number to help text

Add version number to help text

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

from jungle import __version__

class JungleCLI(click.MultiCommand):

    """Jangle CLI main class"""

    def list_commands(self, ctx):
        """return available modules"""
        return ['ec2', 'elb']

    def get_command(self, ctx, name):
        """get command"""
        mod = __import__('jungle.' + name, None, None, ['cli'])
        return mod.cli

cli = JungleCLI(help="aws operation cli (v{})".format(__version__))

if __name__ == '__main__':
    cli()

Prompt:
Add version number to help text

# -*- coding: utf-8 -*-
import click

class JungleCLI(click.MultiCommand):

    """Jangle CLI main class"""

    def list_commands(self, ctx):
        """return available modules"""
        return ['ec2', 'elb']

    def get_command(self, ctx, name):
        """get command"""
        mod = __import__('jungle.' + name, None, None, ['cli'])
        return mod.cli

cli = JungleCLI(help='aws operation cli')

if __name__ == '__main__':
    cli()


import functools

DEFAULT_HEADERS = frozenset(["origin", "x-requested-with", "content-type", "accept"])):

def wrap_cors(func=None, *, allow_origin='*', allow_headers=DEFAULT_HEADERS):
    """
    A middleware that allow CORS calls, by adding the
    headers Access-Control-Allow-Origin and Access-Control-Allow-Headers.
    This middlware accepts two optional parameters `allow_origin` and
    `allow_headers` for customization of the headers values. By default
    will be `*` and a set of `[Origin, X-Requested-With, Content-Type, Accept]`
    respectively.
    """

    @functools.wraps(func)
    def wrapper(request, *args, **kwargs):
        response = func(request, *args, **kwargs)

        if not response.headers:
            response.headers = {}

        response.headers['Access-Control-Allow-Origin'] = allow_origin
        response.headers['Access-Control-Allow-Headers'] = ', '.join(allow_headers)
        return response

    return wrapper

Response:
import functools

DEFAULT_HEADERS = frozenset(["origin", "x-requested-with", "content-type", "accept"])):

def wrap_cors(func=None, *, allow_origin='*', allow_headers=DEFAULT_HEADERS):
    """
    A middleware that allow CORS calls, by adding the
    headers Access-Control-Allow-Origin and Access-Control-Allow-Headers.
    This middlware accepts two optional parameters `allow_origin` and
    `allow_headers` for customization of the headers values. By default
    will be `*` and a set of `[Origin, X-Requested-With, Content-Type, Accept]`
    respectively.
    """

    @functools.wraps(func)
    def wrapper(request, *args, **kwargs):
        response = func(request, *args, **kwargs)

        if not response.headers:
            response.headers = {}

        response.headers['Access-Control-Allow-Origin'] = allow_origin
        response.headers['Access-Control-Allow-Headers'] = ', '.join(allow_headers)
        return response

    return wrapper

Prompt:
Put default headers as constant and make it immutable.

With additional cosmetic fixes.

import functools

def wrap_cors(
    func=None,
    *,
    allow_origin='*',
    allow_headers=set(["Origin", "X-Requested-With", "Content-Type", "Accept"])):
    """
    A middleware that allow CORS calls, by adding the
    headers Access-Control-Allow-Origin and Access-Control-Allow-Headers.
    This middlware accepts two optional parameters `allow_origin` and
    `allow_headers` for customization of the headers values. By default
    will be `*` and a set of `[Origin, X-Requested-With, Content-Type, Accept]`
    respectively.
    """

    @functools.wraps(func)
    def wrapper(request, *args, **kwargs):
        response = func(request, *args, **kwargs)

        if not response.headers:
            response.headers = {}

        response.headers['Access-Control-Allow-Origin'] = allow_origin
        response.headers['Access-Control-Allow-Headers'] = ', '.join(allow_headers)
        return response

    return wrapper


#! /usr/bin/env python3
""" Helper functions to make our life easier.

Originally obtained from the 'pharm' repository, but modified.
"""

import fileinput
import json
import os.path
import sys

from dstruct.Sentence import Sentence

## BASE_DIR denotes the application directory
BASE_DIR, throwaway = os.path.split(os.path.realpath(__file__))
BASE_DIR = os.path.realpath(BASE_DIR + "/../..")

## Return the start and end indexes of all subsets of words in the sentence
## sent, with size at most max_phrase_length
def get_all_phrases_in_sentence(sent, max_phrase_length):
    for start in range(len(sent.words)):
        for end in reversed(range(start + 1, min(len(sent.words), start + 1 + max_phrase_length))):
            yield (start, end)

## Return Sentence objects from input lines
def get_input_sentences(input_files=sys.argv[1:]):
    with fileinput.input(files=input_files) as f:
        for line in f:
            sent_dict = json.loads(line)
            yield Sentence(sent_dict["doc_id"], sent_dict["sent_id"],
                    sent_dict["wordidxs"], sent_dict["words"],
                    sent_dict["poses"], sent_dict["ners"], sent_dict["lemmas"],
                    sent_dict["dep_paths"], sent_dict["dep_parents"],
                    sent_dict["bounding_boxes"])

Response:
#! /usr/bin/env python3
""" Helper functions to make our life easier.

Originally obtained from the 'pharm' repository, but modified.
"""

import fileinput
import json
import os.path
import sys

from dstruct.Sentence import Sentence

## BASE_DIR denotes the application directory
BASE_DIR, throwaway = os.path.split(os.path.realpath(__file__))
BASE_DIR = os.path.realpath(BASE_DIR + "/../..")

## Return the start and end indexes of all subsets of words in the sentence
## sent, with size at most max_phrase_length
def get_all_phrases_in_sentence(sent, max_phrase_length):
    for start in range(len(sent.words)):
        for end in reversed(range(start + 1, min(len(sent.words), start + 1 + max_phrase_length))):
            yield (start, end)

## Return Sentence objects from input lines
def get_input_sentences(input_files=sys.argv[1:]):
    with fileinput.input(files=input_files) as f:
        for line in f:
            sent_dict = json.loads(line)
            yield Sentence(sent_dict["doc_id"], sent_dict["sent_id"],
                    sent_dict["wordidxs"], sent_dict["words"],
                    sent_dict["poses"], sent_dict["ners"], sent_dict["lemmas"],
                    sent_dict["dep_paths"], sent_dict["dep_parents"],
                    sent_dict["bounding_boxes"])

Prompt:
Fix import, use fileinput.iput as context, and fix its argument

#! /usr/bin/env python3
""" Helper functions to make our life easier.

Originally obtained from the 'pharm' repository, but modified.
"""

import fileinput
import json
import os.path

from dstruct import Sentence

## BASE_DIR denotes the application directory
BASE_DIR, throwaway = os.path.split(os.path.realpath(__file__))
BASE_DIR = os.path.realpath(BASE_DIR + "/../..")

## Return the start and end indexes of all subsets of words in the sentence
## sent, with size at most max_phrase_length
def get_all_phrases_in_sentence(sent, max_phrase_length):
    for start in range(len(sent.words)):
        for end in reversed(range(start + 1, min(len(sent.words), start + 1 + max_phrase_length))):
            yield (start, end)

## Return Sentence objects from input lines
def get_input_sentences(input_files=[]):
    for line in fileinput.input(input_files):
        sent_dict = json.loads(line)
        yield Sentence(sent_dict["doc_id"], sent_dict["sent_id"],
                sent_dict["wordidxs"], sent_dict["words"],
                sent_dict["poses"], sent_dict["ners"], sent_dict["lemmas"],
                sent_dict["dep_paths"], sent_dict["dep_parents"],
                sent_dict["bounding_boxes"])


"""Leetcode 246. Strobogrammatic Number
Easy

URL: https://leetcode.com/problems/strobogrammatic-number/A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Write a function to determine if a number is strobogrammatic. The number is
represented as a string.

Example 1:
Input:  "69"
Output: true

Example 2:
Input:  "88"
Output: true

Example 3:
Input:  "962"
Output: false
"""

class SolutionMapDictIter(object):
    def isStrobogrammatic(self, num):
        """
        :type num: str
        :rtype: bool

        Time complexity: O(n).
        Space complexity: O(n).
        """
        # Reverse num.
        rev_num = num[::-1]

        # Convert to mapped number or empty string.
        map_d = {
            '0': '0',
            '1': '1',
            '6': '9',
            '8': '8',
            '9': '6'
        }

        mapped_num_ls = []
        for n in rev_num:
            if n in map_d:
                mapped_num_ls.append(map_d[n])
            else:
                mapped_num_ls.append(' ')
        mapped_num = ''.join(mapped_num_ls)

        # Check if strobogrammatic.
        return mapped_num == num

def main():
    # Output: true
    num = "69"
    print SolutionMapDictIter().isStrobogrammatic(num)

    # Output: true
    num = "88"
    print SolutionMapDictIter().isStrobogrammatic(num)

    # Output: false
    num = "962"
    print SolutionMapDictIter().isStrobogrammatic(num)

    # Output: false
    num = "2"
    print SolutionMapDictIter().isStrobogrammatic(num)

if __name__ == '__main__':
    main()

Response:
"""Leetcode 246. Strobogrammatic Number
Easy

URL: https://leetcode.com/problems/strobogrammatic-number/A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Write a function to determine if a number is strobogrammatic. The number is
represented as a string.

Example 1:
Input:  "69"
Output: true

Example 2:
Input:  "88"
Output: true

Example 3:
Input:  "962"
Output: false
"""

class SolutionMapDictIter(object):
    def isStrobogrammatic(self, num):
        """
        :type num: str
        :rtype: bool

        Time complexity: O(n).
        Space complexity: O(n).
        """
        # Reverse num.
        rev_num = num[::-1]

        # Convert to mapped number or empty string.
        map_d = {
            '0': '0',
            '1': '1',
            '6': '9',
            '8': '8',
            '9': '6'
        }

        mapped_num_ls = []
        for n in rev_num:
            if n in map_d:
                mapped_num_ls.append(map_d[n])
            else:
                mapped_num_ls.append(' ')
        mapped_num = ''.join(mapped_num_ls)

        # Check if strobogrammatic.
        return mapped_num == num

def main():
    # Output: true
    num = "69"
    print SolutionMapDictIter().isStrobogrammatic(num)

    # Output: true
    num = "88"
    print SolutionMapDictIter().isStrobogrammatic(num)

    # Output: false
    num = "962"
    print SolutionMapDictIter().isStrobogrammatic(num)

    # Output: false
    num = "2"
    print SolutionMapDictIter().isStrobogrammatic(num)

if __name__ == '__main__':
    main()

Prompt:
Complete map dict sol w/ time/space complexity

"""Leetcode 246. Strobogrammatic Number
Easy

URL: https://leetcode.com/problems/strobogrammatic-number/A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Write a function to determine if a number is strobogrammatic. The number is
represented as a string.

Example 1:
Input:  "69"
Output: true

Example 2:
Input:  "88"
Output: true

Example 3:
Input:  "962"
Output: false
"""

class Solution(object):
    def isStrobogrammatic(self, num):
        """
        :type num: str
        :rtype: bool
        """
        pass

def main():
    # Output: true
    num = "69"
    print Solution().isStrobogrammatic(num)

    # Output: true
    num = "88"
    print Solution().isStrobogrammatic(num)

    # Output: false
    num = "962"
    print Solution().isStrobogrammatic(num)

if __name__ == '__main__':
    main()


import logging
logger = logging.getLogger('djpg')

from django.dispatch import Signal
from .codes import codes

notification_received = Signal(providing_args=['notification'])
transaction_received = Signal(providing_args=['transaction'])

transaction_waiting = Signal()
transaction_analysis = Signal()
transaction_paid = Signal()
transaction_available = Signal()
transaction_dispute = Signal()
transaction_returned = Signal()
transaction_canceled = Signal()
transaction_unknown = Signal()

def dispatch_transaction(sender, **kwargs):
    transaction = kwargs.pop('transaction')
    code = transaction['code']
    status = int(transaction['status'])

    signals = {
        codes.waiting: transaction_waiting,
        codes.analysis: transaction_analysis,
        codes.paid: transaction_paid,
        codes.available: transaction_available,
        codes.dispute: transaction_dispute,
        codes.returned: transaction_returned,
        codes.canceled: transaction_canceled
    }

    signals \
        .get(status, transaction_unknown) \
        .send(sender=None, transaction=transaction)

    logger.info('Transaction with status "%s" and code "%s" dispatched'
                % (status, code))

transaction_received.connect(dispatch_transaction)

Response:
import logging
logger = logging.getLogger('djpg')

from django.dispatch import Signal
from .codes import codes

notification_received = Signal(providing_args=['notification'])
transaction_received = Signal(providing_args=['transaction'])

transaction_waiting = Signal()
transaction_analysis = Signal()
transaction_paid = Signal()
transaction_available = Signal()
transaction_dispute = Signal()
transaction_returned = Signal()
transaction_canceled = Signal()
transaction_unknown = Signal()

def dispatch_transaction(sender, **kwargs):
    transaction = kwargs.pop('transaction')
    code = transaction['code']
    status = int(transaction['status'])

    signals = {
        codes.waiting: transaction_waiting,
        codes.analysis: transaction_analysis,
        codes.paid: transaction_paid,
        codes.available: transaction_available,
        codes.dispute: transaction_dispute,
        codes.returned: transaction_returned,
        codes.canceled: transaction_canceled
    }

    signals \
        .get(status, transaction_unknown) \
        .send(sender=None, transaction=transaction)

    logger.info('Transaction with status "%s" and code "%s" dispatched'
                % (status, code))

transaction_received.connect(dispatch_transaction)

Prompt:
Fix bug when trying to convert transaction code to int

import logging
logger = logging.getLogger('djpg')

from django.dispatch import Signal
from .codes import codes

notification_received = Signal(providing_args=['notification'])
transaction_received = Signal(providing_args=['transaction'])

transaction_waiting = Signal()
transaction_analysis = Signal()
transaction_paid = Signal()
transaction_available = Signal()
transaction_dispute = Signal()
transaction_returned = Signal()
transaction_canceled = Signal()
transaction_unknown = Signal()

def dispatch_transaction(sender, **kwargs):
    transaction = kwargs.pop('transaction')
    code = int(transaction['code'])
    status = int(transaction['status'])

    signals = {
        codes.waiting: transaction_waiting,
        codes.analysis: transaction_analysis,
        codes.paid: transaction_paid,
        codes.available: transaction_available,
        codes.dispute: transaction_dispute,
        codes.returned: transaction_returned,
        codes.canceled: transaction_canceled
    }

    signals \
        .get(status, transaction_unknown) \
        .send(sender=None, transaction=transaction)

    logger.info('Transaction with status "%s" and code "%s" dispatched'
                % (status, code))

transaction_received.connect(dispatch_transaction)


"""Test Provider Health Check setting."""
from foremast.pipeline.construct_pipeline_block import check_provider_healthcheck

TEST_SETTINGS = {'app': {'eureka_enabled': False}, 'asg': {'provider_healthcheck': {}}}

def test_provider_healthcheck():
    """Make sure default Provider Health Check works."""
    health_checks = check_provider_healthcheck(settings=TEST_SETTINGS)
    assert health_checks.providers == []
    assert health_checks.has_healthcheck == False

Update Provider Health Check sanity

Response:
"""Test Provider Health Check setting."""
from foremast.pipeline.construct_pipeline_block import check_provider_healthcheck

TEST_SETTINGS = {'app': {'eureka_enabled': False}, 'asg': {'provider_healthcheck': {}}}

def test_provider_healthcheck():
    """Make sure default Provider Health Check works."""
    health_checks = check_provider_healthcheck(settings=TEST_SETTINGS)
    assert health_checks.providers == []
    assert health_checks.has_healthcheck == False

Prompt:
test: Update Provider Health Check sanity

See also: PSOBAT-2465

"""Test Provider Health Check setting."""
from foremast.pipeline.construct_pipeline_block import check_provider_healthcheck

TEST_SETTINGS = {'app': {'eureka_enabled': False}, 'asg': {'provider_healthcheck': {}}}

def test_provider_healthcheck():
    """Make sure default Provider Health Check works."""
    provider_healthcheck, has_provider_healthcheck = check_provider_healthcheck(settings=TEST_SETTINGS)
    assert provider_healthcheck == []
    assert has_provider_healthcheck == False


from sure import expect

from simpleflow import activity
from simpleflow.swf.task import ActivityTask

@activity.with_attributes()
def show_context_func():
    return show_context_func.context

@activity.with_attributes()
class ShowContextCls(object):
    def execute(self):
        return self.context

def test_task_has_an_empty_context_by_default():
    expect(ActivityTask(show_context_func).execute()).to.be.none
    expect(ActivityTask(ShowContextCls).execute()).to.be.none

def test_task_attaches_context_to_functions():
    ctx = {'foo': 'bar'}
    expect(ActivityTask(show_context_func, context=ctx).execute()).to.equal(ctx)
    expect(show_context_func.context).to.equal(ctx)

def test_task_attaches_context_to_object_instances():
    ctx = {'foo': 'bar'}
    expect(ActivityTask(ShowContextCls, context=ctx).execute()).to.equal(ctx)
    expect(ShowContextCls.context).to.be.none

Add tests to ensure ActivityTask attaches the context

Response:
from sure import expect

from simpleflow import activity
from simpleflow.swf.task import ActivityTask

@activity.with_attributes()
def show_context_func():
    return show_context_func.context

@activity.with_attributes()
class ShowContextCls(object):
    def execute(self):
        return self.context

def test_task_has_an_empty_context_by_default():
    expect(ActivityTask(show_context_func).execute()).to.be.none
    expect(ActivityTask(ShowContextCls).execute()).to.be.none

def test_task_attaches_context_to_functions():
    ctx = {'foo': 'bar'}
    expect(ActivityTask(show_context_func, context=ctx).execute()).to.equal(ctx)
    expect(show_context_func.context).to.equal(ctx)

def test_task_attaches_context_to_object_instances():
    ctx = {'foo': 'bar'}
    expect(ActivityTask(ShowContextCls, context=ctx).execute()).to.equal(ctx)
    expect(ShowContextCls.context).to.be.none

Prompt: Add tests to ensure ActivityTask attaches the context


import asyncio
import logging

import os

from warreport import battle_monitor, battle_reporting

logger = logging.getLogger("warreport")

def main():
    logger.info("Starting main loop.")
    logger.debug("Debug logging enabled.")
    loop = asyncio.get_event_loop()
    gathered_future = asyncio.gather(
        battle_monitor.grab_new_battles(loop),
        battle_monitor.process_battles(loop),
        battle_reporting.report_battles(loop),
        loop=loop,
    )
    try:
        loop.run_until_complete(gathered_future)
    except KeyboardInterrupt:
        logger.info("Caught KeyboardInterrupt: Ending main loop.")
        gathered_future.cancel()
        loop.run_forever()
        gathered_future.exception()
    finally:
        loop.close()
    logger.info("Ended successfully.")
    # TODO: this is basically so I don't have to Ctrl+C twice to exit. I don't know what holds up the process, and it
    # would definitely be a good idea to find out!
    os._exit(0)

if __name__ == '__main__':
    main()

Make another try at graceful exiting.

Response:
import asyncio
import logging

import os

from warreport import battle_monitor, battle_reporting

logger = logging.getLogger("warreport")

def main():
    logger.info("Starting main loop.")
    logger.debug("Debug logging enabled.")
    loop = asyncio.get_event_loop()
    gathered_future = asyncio.gather(
        battle_monitor.grab_new_battles(loop),
        battle_monitor.process_battles(loop),
        battle_reporting.report_battles(loop),
        loop=loop,
    )
    try:
        loop.run_until_complete(gathered_future)
    except KeyboardInterrupt:
        logger.info("Caught KeyboardInterrupt: Ending main loop.")
        gathered_future.cancel()
        loop.run_forever()
        gathered_future.exception()
    finally:
        loop.close()
    logger.info("Ended successfully.")
    # TODO: this is basically so I don't have to Ctrl+C twice to exit. I don't know what holds up the process, and it
    # would definitely be a good idea to find out!
    os._exit(0)

if __name__ == '__main__':
    main()

Prompt:
Make another try at graceful exiting.

This still does use os._exit() after all coroutines have finished, but it's significantly less spaghetti code, and it makes it nicer to exit the program.

import asyncio
import logging

from warreport import battle_monitor, battle_reporting

logger = logging.getLogger("warreport")

def main():
    logger.info("Starting main loop.")
    logger.debug("Debug logging enabled.")
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.gather(
        battle_monitor.grab_new_battles(loop),
        battle_monitor.process_battles(loop),
        battle_reporting.report_battles(loop),
    ))

if __name__ == '__main__':
    main()


from django.core.management.base import NoArgsCommand
import os
import shutil

class Command(NoArgsCommand):
    help = "Copy the satchmo static directory and files to the local project."

    def handle_noargs(self, **options):
        import satchmo
        static_src = os.path.join(satchmo.__path__[0],'static')
        static_dest = os.path.join(os.getcwd(), 'static')
        if os.path.exists(static_dest):
            print "Static directory exists. You must manually copy the files you need."
        else:
            shutil.copytree(static_src, static_dest)
            print "Copied %s to %s" % (static_src, static_dest)

Add an error check to static copying

Add an error check to static copying

Response:
from django.core.management.base import NoArgsCommand
import os
import shutil

class Command(NoArgsCommand):
    help = "Copy the satchmo static directory and files to the local project."

    def handle_noargs(self, **options):
        import satchmo
        static_src = os.path.join(satchmo.__path__[0],'static')
        static_dest = os.path.join(os.getcwd(), 'static')
        if os.path.exists(static_dest):
            print "Static directory exists. You must manually copy the files you need."
        else:
            shutil.copytree(static_src, static_dest)
            print "Copied %s to %s" % (static_src, static_dest)

Prompt:
Add an error check to static copying

from django.core.management.base import NoArgsCommand
import os
import shutil

class Command(NoArgsCommand):
    help = "Copy the satchmo static directory and files to the local project."

    def handle_noargs(self, **options):
        import satchmo
        static_src = os.path.join(satchmo.__path__[0],'static')
        static_dest = os.path.join(os.getcwd(), 'static')
        shutil.copytree(static_src, static_dest)
        print "Copied %s to %s" % (static_src, static_dest)


from conans import ConanFile, CMake

class ArgsConan(ConanFile):
    name = "cfgfile"
    version = "0.2.8.2"
    url = "https://github.com/igormironchik/cfgfile.git"
    license = "MIT"
    description = "Header-only library for reading/saving configuration files with schema defined in sources."
    exports = "cfgfile/*", "COPYING", "generator/*", "3rdparty/Args/Args/*.hpp"

    def build(self):
        cmake = CMake(self)
        cmake.configure(source_folder="generator")
        cmake.build()

    def package(self):
        self.copy("COPYING", src=".", dst=".")
        self.copy("*", src="cfgfile", dst="cfgfile")
        self.copy("*", src="generator", dst="generator")
        self.copy("*.hpp", src="3rdparty/Args/Args", dst="3rdparty/Args/Args")

    def package_info(self):
        self.cpp_info.includedirs = ["."]

Add build step into Conan recipe.

Add build step into Conan recipe.

Response:
from conans import ConanFile, CMake

class ArgsConan(ConanFile):
    name = "cfgfile"
    version = "0.2.8.2"
    url = "https://github.com/igormironchik/cfgfile.git"
    license = "MIT"
    description = "Header-only library for reading/saving configuration files with schema defined in sources."
    exports = "cfgfile/*", "COPYING", "generator/*", "3rdparty/Args/Args/*.hpp"

    def build(self):
        cmake = CMake(self)
        cmake.configure(source_folder="generator")
        cmake.build()

    def package(self):
        self.copy("COPYING", src=".", dst=".")
        self.copy("*", src="cfgfile", dst="cfgfile")
        self.copy("*", src="generator", dst="generator")
        self.copy("*.hpp", src="3rdparty/Args/Args", dst="3rdparty/Args/Args")

    def package_info(self):
        self.cpp_info.includedirs = ["."]

Prompt:
Add build step into Conan recipe.

from conans import ConanFile

class ArgsConan(ConanFile):
    name = "cfgfile"
    version = "0.2.8.2"
    url = "https://github.com/igormironchik/cfgfile.git"
    license = "MIT"
    description = "Header-only library for reading/saving configuration files with schema defined in sources."
    exports = "cfgfile/*", "COPYING", "generator/*", "3rdparty/Args/Args/*.hpp"

    def package(self):
        self.copy("COPYING", src=".", dst=".")
        self.copy("*", src="cfgfile", dst="cfgfile")
        self.copy("*", src="generator", dst="generator")
        self.copy("*.hpp", src="3rdparty/Args/Args", dst="3rdparty/Args/Args")

    def package_info(self):
        self.cpp_info.includedirs = ["."]


from setuptools import setup, find_packages

setup(
    name='django-force-logout',

    url="https://chris-lamb.co.uk/projects/django-force-logout",
    version='2.0.0',
    description="Framework to be able to forcibly log users out of Django projects",

    author="Chris Lamb",
    author_email='chris@chris-lamb.co.uk',
    license="BSD",

    packages=find_packages(),

    install_requires=(
        'Django>=1.8',
    ),
)

Update Django requirement to latest LTS

Update Django requirement to latest LTS

Response:
from setuptools import setup, find_packages

setup(
    name='django-force-logout',

    url="https://chris-lamb.co.uk/projects/django-force-logout",
    version='2.0.0',
    description="Framework to be able to forcibly log users out of Django projects",

    author="Chris Lamb",
    author_email='chris@chris-lamb.co.uk',
    license="BSD",

    packages=find_packages(),

    install_requires=(
        'Django>=1.8',
    ),
)

Prompt:
Update Django requirement to latest LTS

from setuptools import setup, find_packages

setup(
    name='django-force-logout',

    url="https://chris-lamb.co.uk/projects/django-force-logout",
    version='2.0.0',
    description="Framework to be able to forcibly log users out of Django projects",

    author="Chris Lamb",
    author_email='chris@chris-lamb.co.uk',
    license="BSD",

    packages=find_packages(),
)


import codecs
from lxml import etree
from bs4 import BeautifulSoup
from emotools.bs4_helpers import act, sentence, word, speaker_turn, note
import argparse
import os
import re
import sys

if __name__ == '__main__':
    folia = '/home/jvdzwaan/data/embem-annotatie/vinc001pefr02_01.xml'
    tag = '/home/jvdzwaan/data/embem-annotatie-tag-fix/vinc001pefr02/vinc001pefr02_01__act-01.tag'
    #tag = '/home/jvdzwaan/data/kaf2folia/vinc001pefr02/vinc001pefr02_01__act-01.tag'
    out_dir = '/home/jvdzwaan/data/embem-annotatie-tag-fix/'

    d, file_name = os.path.split(tag)
    out_file = os.path.join(out_dir, file_name)

    print 'Saving file to', out_file

    # lees tag file in geheugen
    with codecs.open(tag, 'rb', 'utf-8') as f:
        tag_lines = f.readlines()
    print 'Found {} tags'.format(len(tag_lines))
    tags = iter(tag_lines)

    curr_tag = None
    # word ids that start with lg-added must be replaced
    reg = r'^lg-added-\d+'

    word_tag = '{http://ilk.uvt.nl/folia}w'

    # Load folia document
    context = etree.iterparse(folia, events=('end',), tag=word_tag)

    # open file for output
    with codecs.open(out_file, 'wb', 'utf-8') as f:
        for event, elem in context:
            if not curr_tag:
                try:
                    curr_tag = tags.next()

                    parts = curr_tag.split('\t')
                    tag_id = parts[0]
                    tag_word = parts[1]

                    print 'looking for', tag_id
                except StopIteration:
                    sys.exit()

            if event == 'end':
                word_xml = BeautifulSoup(etree.tostring(elem), 'xml')
                w = word_xml.find(word)
                w_id = w.attrs.get('xml:id')
                w_word = w.t.string

                match_part = re.sub(reg, '', tag_id)
                #if re.match(reg, tag_id):
                #    print 'tag_id matches regex', tag_id, w_id
                if w_id.endswith(match_part) and w_word == tag_word:
                    print 'found match:'
                    print w_id, w_word
                    print tag_id, tag_word
                    #print '{}\t{}'.format(w_id, '\t'.join(parts[1:]).encode('utf-8'))
                    f.write('{}\t{}'.format(w_id, '\t'.join(parts[1:]).encode('utf-8')).decode('utf-8'))
                    curr_tag = None

Add a script that fixes tag files

Add a script that fixes tag files

The kaf files used for annotation were generated using folia files
contain strange ids (starting with lg-added). Unfortunately, these
folia files were not saved. Therefore, the word ids in the kaf files
must be updated to the word ids in the new folia files. This script does
that for hardcoded input.
The next step is to turn these parameters into command line arguments
and to call this script with a bash script that runs the fix_tags script
for all tag files in a directory.

Response:
import codecs
from lxml import etree
from bs4 import BeautifulSoup
from emotools.bs4_helpers import act, sentence, word, speaker_turn, note
import argparse
import os
import re
import sys

if __name__ == '__main__':
    folia = '/home/jvdzwaan/data/embem-annotatie/vinc001pefr02_01.xml'
    tag = '/home/jvdzwaan/data/embem-annotatie-tag-fix/vinc001pefr02/vinc001pefr02_01__act-01.tag'
    #tag = '/home/jvdzwaan/data/kaf2folia/vinc001pefr02/vinc001pefr02_01__act-01.tag'
    out_dir = '/home/jvdzwaan/data/embem-annotatie-tag-fix/'

    d, file_name = os.path.split(tag)
    out_file = os.path.join(out_dir, file_name)

    print 'Saving file to', out_file

    # lees tag file in geheugen
    with codecs.open(tag, 'rb', 'utf-8') as f:
        tag_lines = f.readlines()
    print 'Found {} tags'.format(len(tag_lines))
    tags = iter(tag_lines)

    curr_tag = None
    # word ids that start with lg-added must be replaced
    reg = r'^lg-added-\d+'

    word_tag = '{http://ilk.uvt.nl/folia}w'

    # Load folia document
    context = etree.iterparse(folia, events=('end',), tag=word_tag)

    # open file for output
    with codecs.open(out_file, 'wb', 'utf-8') as f:
        for event, elem in context:
            if not curr_tag:
                try:
                    curr_tag = tags.next()

                    parts = curr_tag.split('\t')
                    tag_id = parts[0]
                    tag_word = parts[1]

                    print 'looking for', tag_id
                except StopIteration:
                    sys.exit()

            if event == 'end':
                word_xml = BeautifulSoup(etree.tostring(elem), 'xml')
                w = word_xml.find(word)
                w_id = w.attrs.get('xml:id')
                w_word = w.t.string

                match_part = re.sub(reg, '', tag_id)
                #if re.match(reg, tag_id):
                #    print 'tag_id matches regex', tag_id, w_id
                if w_id.endswith(match_part) and w_word == tag_word:
                    print 'found match:'
                    print w_id, w_word
                    print tag_id, tag_word
                    #print '{}\t{}'.format(w_id, '\t'.join(parts[1:]).encode('utf-8'))
                    f.write('{}\t{}'.format(w_id, '\t'.join(parts[1:]).encode('utf-8')).decode('utf-8'))
                    curr_tag = None

Prompt:
Add a script that fixes tag files

The kaf files used for annotation were generated using folia files
contain strange ids (starting with lg-added). Unfortunately, these
folia files were not saved. Therefore, the word ids in the kaf files
must be updated to the word ids in the new folia files. This script does
that for hardcoded input.
The next step is to turn these parameters into command line arguments
and to call this script with a bash script that runs the fix_tags script
for all tag files in a directory.


import igraph

def packing_graph(tets, vx, vy, vz, independent = 0):
    N = len(tets)
    for i in range(N-1, independent-1, -1):
      for s in tets[i].get_symetry(vx, vy, vz, include_self=False):
          if tets[i].collision(s):
              del tets[i]
              break

    N = len(tets)
    g = igraph.Graph()
    g.add_vertices(N)
    for i in range(N):
        for j in range(max(i, independent),N):
            for s in tets[j].get_symetry(vx, vy, vz, include_self=(i != j)):
                if tets[i].collision(s):
                    g.add_edge(i,j)
                    break

    return g

Response:
import igraph

def packing_graph(tets, vx, vy, vz, independent = 0):
    N = len(tets)
    for i in range(N-1, independent-1, -1):
      for s in tets[i].get_symetry(vx, vy, vz, include_self=False):
          if tets[i].collision(s):
              del tets[i]
              break

    N = len(tets)
    g = igraph.Graph()
    g.add_vertices(N)
    for i in range(N):
        for j in range(max(i, independent),N):
            for s in tets[j].get_symetry(vx, vy, vz, include_self=(i != j)):
                if tets[i].collision(s):
                    g.add_edge(i,j)
                    break

    return g

Prompt:
Remove self-touching tets first to reduce collision load.

import igraph

def packing_graph(tets, vx, vy, vz, independent = 0):
    N = len(tets)
    g = igraph.Graph()
    g.add_vertices(N)
    for i in range(N):
        for j in range(max(i, independent),N):
            for s in tets[j].get_symetry(vx, vy, vz, include_self=(i != j)):
                if tets[i].collision(s):
                    g.add_edge(i,j)
                    break
    edges = g.get_edgelist()
    for i in range(N, -1, -1):
        if (i,i) in edges:
            g.delete_vertices(g.vs[i])
            del tets[i]
    return g