from UM.Operations.Operation import Operation
from UM.Operations.AddSceneNodeOperation import AddSceneNodeOperation
from UM.Operations.TranslateOperation import TranslateOperation
from UM.Operations.GroupedOperation import GroupedOperation

##  A specialised operation designed specifically to modify the previous operation.
class PlatformPhysicsOperation(Operation):
    def __init__(self, node, translation):
        super().__init__()
        self._node = node
        self._transform = node.getLocalTransformation()
        self._position = node.getPosition() + translation
        self._always_merge = True

    def undo(self):
        self._node.setLocalTransformation(self._transform)

    def redo(self):
        self._node.setPosition(self._position)

    def mergeWith(self, other):
        group = GroupedOperation()

        group.addOperation(self)
        group.addOperation(other)

        return group

    def __repr__(self):
        return 'PlatformPhysicsOperation(t = {0})'.format(self._position)

Response:
from UM.Operations.Operation import Operation
from UM.Operations.AddSceneNodeOperation import AddSceneNodeOperation
from UM.Operations.TranslateOperation import TranslateOperation
from UM.Operations.GroupedOperation import GroupedOperation

##  A specialised operation designed specifically to modify the previous operation.
class PlatformPhysicsOperation(Operation):
    def __init__(self, node, translation):
        super().__init__()
        self._node = node
        self._transform = node.getLocalTransformation()
        self._position = node.getPosition() + translation
        self._always_merge = True

    def undo(self):
        self._node.setLocalTransformation(self._transform)

    def redo(self):
        self._node.setPosition(self._position)

    def mergeWith(self, other):
        group = GroupedOperation()

        group.addOperation(self)
        group.addOperation(other)

        return group

    def __repr__(self):
        return 'PlatformPhysicsOperation(t = {0})'.format(self._position)

Prompt:
Use GroupedOperation for merging PlatformPhyisicsOperation

from UM.Operations.Operation import Operation
from UM.Operations.AddSceneNodeOperation import AddSceneNodeOperation
from UM.Operations.TranslateOperation import TranslateOperation

##  A specialised operation designed specifically to modify the previous operation.
class PlatformPhysicsOperation(Operation):
    def __init__(self, node, translation):
        super().__init__()
        self._node = node
        self._translation = translation

    def undo(self):
        pass

    def redo(self):
        pass

    def mergeWith(self, other):
        if type(other) is AddSceneNodeOperation:
            other._node.translate(self._translation)
            return other
        elif type(other) is TranslateOperation:
            other._translation += self._translation
            return other
        else:
            return False


from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.views.generic.simple import direct_to_template
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^i4p/', include('i4p.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
    url(r'^$', direct_to_template, {'template' : 'base.html'}, name="i4p-index"),
    (r'^accounts/', include('registration.backends.default.urls')),
)

Add a name to the index URL

Add a name to the index URL

Response:
from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.views.generic.simple import direct_to_template
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^i4p/', include('i4p.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
    url(r'^$', direct_to_template, {'template' : 'base.html'}, name="i4p-index"),
    (r'^accounts/', include('registration.backends.default.urls')),
)

Prompt:
Add a name to the index URL

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.views.generic.simple import direct_to_template
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^i4p/', include('i4p.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
    (r'^$', direct_to_template, {'template' : 'base.html'}),
    (r'^accounts/', include('registration.backends.default.urls')),
)


 # Module of Gaussian Mixture models for python
import megamix.batch
import megamix.online

__all__ = ['batch','online']

Response:
 # Module of Gaussian Mixture models for python
import megamix.batch
import megamix.online

__all__ = ['batch','online']

Prompt:
Solve minor import problems with submodules

 # Module of Gaussian Mixture models for python

__all__ = ['batch','online']


from django.core.exceptions import ValidationError as DjangoValidationError
from rest_framework import serializers

from apps.approval.models import CommitteeApplication, CommitteePriority

class CommitteeSerializer(serializers.ModelSerializer):
    group_name = serializers.SerializerMethodField(source='group')

    class Meta(object):
        model = CommitteePriority
        fields = ('group', 'group_name', 'priority')

    def get_group_name(self, instance):
        return instance.group.name

class CommitteeApplicationSerializer(serializers.ModelSerializer):
    committees = CommitteeSerializer(many=True, source='committeepriority_set')

    class Meta(object):
        model = CommitteeApplication
        fields = ('name', 'email', 'application_text', 'prioritized', 'committees')

    def create(self, validated_data):
        committees = validated_data.pop('committeepriority_set')
        application = CommitteeApplication(**validated_data)
        try:
            application.clean()
        except DjangoValidationError as django_error:
            raise serializers.ValidationError(django_error.message)
        application.save()

        for committee in committees:
            CommitteePriority.objects.create(committee_application=application, **committee)

        return CommitteeApplication.objects.get(pk=application.pk)

Response:
from django.core.exceptions import ValidationError as DjangoValidationError
from rest_framework import serializers

from apps.approval.models import CommitteeApplication, CommitteePriority

class CommitteeSerializer(serializers.ModelSerializer):
    group_name = serializers.SerializerMethodField(source='group')

    class Meta(object):
        model = CommitteePriority
        fields = ('group', 'group_name', 'priority')

    def get_group_name(self, instance):
        return instance.group.name

class CommitteeApplicationSerializer(serializers.ModelSerializer):
    committees = CommitteeSerializer(many=True, source='committeepriority_set')

    class Meta(object):
        model = CommitteeApplication
        fields = ('name', 'email', 'application_text', 'prioritized', 'committees')

    def create(self, validated_data):
        committees = validated_data.pop('committeepriority_set')
        application = CommitteeApplication(**validated_data)
        try:
            application.clean()
        except DjangoValidationError as django_error:
            raise serializers.ValidationError(django_error.message)
        application.save()

        for committee in committees:
            CommitteePriority.objects.create(committee_application=application, **committee)

        return CommitteeApplication.objects.get(pk=application.pk)

Prompt:
Raise DRF ValidationError to get APIException base

from rest_framework import serializers

from apps.approval.models import CommitteeApplication, CommitteePriority

class CommitteeSerializer(serializers.ModelSerializer):
    group_name = serializers.SerializerMethodField(source='group')

    class Meta(object):
        model = CommitteePriority
        fields = ('group', 'group_name', 'priority')

    def get_group_name(self, instance):
        return instance.group.name

class CommitteeApplicationSerializer(serializers.ModelSerializer):
    committees = CommitteeSerializer(many=True, source='committeepriority_set')

    class Meta(object):
        model = CommitteeApplication
        fields = ('name', 'email', 'application_text', 'prioritized', 'committees')

    def create(self, validated_data):
        committees = validated_data.pop('committeepriority_set')
        application = CommitteeApplication.objects.create(**validated_data)

        for committee in committees:
            CommitteePriority.objects.create(committee_application=application, **committee)

        return CommitteeApplication.objects.get(pk=application.pk)


"""Family module for Lingua Libre."""
#
# (C) Pywikibot team, 2021
#
# Distributed under the terms of the MIT license.
#
from pywikibot import family

# The Lingua Libre family
class Family(family.WikimediaFamily):

    """Family class for Lingua Libre.

    *New in version 6.5.*
    """

    name = 'lingualibre'

    langs = {
        'lingualibre': 'lingualibre.org'
    }

    interwiki_forward = 'wikipedia'

    def scriptpath(self, code):
        """Return the script path for this family."""
        return ''

    def interface(self, code):
        """Return 'DataSite'."""
        return 'DataSite'

Response:
"""Family module for Lingua Libre."""
#
# (C) Pywikibot team, 2021
#
# Distributed under the terms of the MIT license.
#
from pywikibot import family

# The Lingua Libre family
class Family(family.WikimediaFamily):

    """Family class for Lingua Libre.

    *New in version 6.5.*
    """

    name = 'lingualibre'

    langs = {
        'lingualibre': 'lingualibre.org'
    }

    interwiki_forward = 'wikipedia'

    def scriptpath(self, code):
        """Return the script path for this family."""
        return ''

    def interface(self, code):
        """Return 'DataSite'."""
        return 'DataSite'

Prompt:
Allow to request for item on Lingua Libre

Bug: T286303
Change-Id: Ic0d8824d1bf326f2182fbb64d0cc2ed77f82fd4c

"""Family module for Lingua Libre."""
#
# (C) Pywikibot team, 2021
#
# Distributed under the terms of the MIT license.
#
from pywikibot import family

# The Lingua Libre family
class Family(family.WikimediaFamily):

    """Family class for Lingua Libre.

    *New in version 6.5.*
    """

    name = 'lingualibre'

    langs = {
        'lingualibre': 'lingualibre.org'
    }

    interwiki_forward = 'wikipedia'

    def scriptpath(self, code):
        """Return the script path for this family."""
        return ''


Revert "Change imports to relative."

Response:
__version__ = 0.1

import pydarkstar.logutils
import logging

pydarkstar.logutils.setError()

try:
    import sqlalchemy
except ImportError as e:
    logging.exception(e.__class__.__name__)
    logging.error('pip install sqlalchemy')
    exit(-1)

try:
    import pymysql
except ImportError as e:
    logging.exception(e.__class__.__name__)
    logging.error('pip install pymysql')
    exit(-1)

try:
    import bs4
except ImportError as e:
    logging.exception(e.__class__.__name__)
    logging.error('pip install beautifulsoup4')
    exit(-1)

Prompt:
Revert "Change imports to relative."

This reverts commit 9d0990249b7e0e46e38a665cb8c32a1ee435c291.

__version__ = 0.1

import pydarkstar.logutils
import logging

pydarkstar.logutils.setError()

try:
    import sqlalchemy
except ImportError as e:
    logging.exception(e.__class__.__name__)
    logging.error('pip install sqlalchemy')
    exit(-1)

try:
    import pymysql
except ImportError as e:
    logging.exception(e.__class__.__name__)
    logging.error('pip install pymysql')
    exit(-1)

try:
    import bs4
except ImportError as e:
    logging.exception(e.__class__.__name__)
    logging.error('pip install beautifulsoup4')
    exit(-1)

import scrub


import logging
import colorlog

logging.STRONGINFO = logging.INFO + 5
logging.addLevelName(logging.STRONGINFO, 'STRONGINFO')

def logStrongInfo(msg, *args, **kwargs):
    """Log a message with severity 'STRONGINFO' on the root logger. If the logger has
    no handlers, call basicConfig() to add a console handler with a pre-defined
    format."""
    return logging.log(logging.STRONGINFO, msg, *args, **kwargs)

setattr(logging, 'stronginfo', logStrongInfo)

def logStrongInfoMethod(self, msg, *args, **kwargs):
    """Log 'msg % args' with severity 'STRONGINFO'.

    To pass exception information, use the keyword argument exc_info with
    a true value, e.g.

    logger.stronginfo("Houston, we have a %s", "interesting problem", exc_info=1)
    """
    return self.log(logging.STRONGINFO, msg, *args, **kwargs)

setattr(logging.getLoggerClass(), 'stronginfo', logStrongInfoMethod)

colorlog.default_log_colors['STRONGINFO'] = 'bold_green'

Response:
import logging
import colorlog

logging.STRONGINFO = logging.INFO + 5
logging.addLevelName(logging.STRONGINFO, 'STRONGINFO')

def logStrongInfo(msg, *args, **kwargs):
    """Log a message with severity 'STRONGINFO' on the root logger. If the logger has
    no handlers, call basicConfig() to add a console handler with a pre-defined
    format."""
    return logging.log(logging.STRONGINFO, msg, *args, **kwargs)

setattr(logging, 'stronginfo', logStrongInfo)

def logStrongInfoMethod(self, msg, *args, **kwargs):
    """Log 'msg % args' with severity 'STRONGINFO'.

    To pass exception information, use the keyword argument exc_info with
    a true value, e.g.

    logger.stronginfo("Houston, we have a %s", "interesting problem", exc_info=1)
    """
    return self.log(logging.STRONGINFO, msg, *args, **kwargs)

setattr(logging.getLoggerClass(), 'stronginfo', logStrongInfoMethod)

colorlog.default_log_colors['STRONGINFO'] = 'bold_green'

Prompt:
Correct priority for STRONGINFO log level

import logging
import colorlog

logging.STRONGINFO = logging.DEBUG + 5
logging.addLevelName(logging.STRONGINFO, 'STRONGINFO')

def logStrongInfo(msg, *args, **kwargs):
    """Log a message with severity 'STRONGINFO' on the root logger. If the logger has
    no handlers, call basicConfig() to add a console handler with a pre-defined
    format."""
    return logging.log(logging.STRONGINFO, msg, *args, **kwargs)

setattr(logging, 'stronginfo', logStrongInfo)

def logStrongInfoMethod(self, msg, *args, **kwargs):
    """Log 'msg % args' with severity 'STRONGINFO'.

    To pass exception information, use the keyword argument exc_info with
    a true value, e.g.

    logger.stronginfo("Houston, we have a %s", "interesting problem", exc_info=1)
    """
    return self.log(logging.STRONGINFO, msg, *args, **kwargs)

setattr(logging.getLoggerClass(), 'stronginfo', logStrongInfoMethod)

colorlog.default_log_colors['STRONGINFO'] = 'bold_green'


import os
import json
import socket

import requests

from flask import request, jsonify, Response, abort

from dadd.worker import app
from dadd.worker.proc import ChildProcess

@app.route('/run/', methods=['POST'])
def run_process():
    proc = ChildProcess(request.json)
    proc.run()
    return jsonify(proc.info())

@app.route('/register/', methods=['POST'])
def register_with_master():
    register(app)
    return jsonify({'message': 'ok'})

def register(host, port):
    sess = requests.Session()

    if 'USERNAME' in app.config and 'PASSWORD' in app.config:
        sess.auth = (app.config['USERNAME'], app.config['PASSWORD'])
    sess.headers = {'content-type': 'application/json'}

    try:
        url = app.config['MASTER_URL'] + '/api/hosts/'
        resp = sess.post(url, data=json.dumps({
            'host': app.config.get('HOSTNAME', socket.getfqdn()),
            'port': port
        }))
        if not resp.ok:
            app.logger.warning('Error registering with master: %s' %
                               app.config['MASTER_URL'])
    except Exception as e:
        app.logger.warning('Connection Error: %s' % e)

@app.route('/logs/<path>', methods=['GET'])
def tail_log(path):
    if os.path.exists(path) and path.startswith('/tmp/'):
        return Response(open(path), content_type='text/plain')
    abort(404)

Response:
import os
import json
import socket

import requests

from flask import request, jsonify, Response, abort

from dadd.worker import app
from dadd.worker.proc import ChildProcess

@app.route('/run/', methods=['POST'])
def run_process():
    proc = ChildProcess(request.json)
    proc.run()
    return jsonify(proc.info())

@app.route('/register/', methods=['POST'])
def register_with_master():
    register(app)
    return jsonify({'message': 'ok'})

def register(host, port):
    sess = requests.Session()

    if 'USERNAME' in app.config and 'PASSWORD' in app.config:
        sess.auth = (app.config['USERNAME'], app.config['PASSWORD'])
    sess.headers = {'content-type': 'application/json'}

    try:
        url = app.config['MASTER_URL'] + '/api/hosts/'
        resp = sess.post(url, data=json.dumps({
            'host': app.config.get('HOSTNAME', socket.getfqdn()),
            'port': port
        }))
        if not resp.ok:
            app.logger.warning('Error registering with master: %s' %
                               app.config['MASTER_URL'])
    except Exception as e:
        app.logger.warning('Connection Error: %s' % e)

@app.route('/logs/<path>', methods=['GET'])
def tail_log(path):
    if os.path.exists(path) and path.startswith('/tmp/'):
        return Response(open(path), content_type='text/plain')
    abort(404)

Prompt:
Allow setting the HOSTNAME reported to the master via an env var; Stub and endpoint for tailing the log of a process assuming you know the path.

I'd like to get this log processing a bit more automatic but I need to
spend more time in Dadd's API.

import json
import socket

import requests

from flask import request, jsonify

from dadd.worker import app
from dadd.worker.proc import ChildProcess

@app.route('/run/', methods=['POST'])
def run_process():
    proc = ChildProcess(request.json)
    proc.run()
    return jsonify(proc.info())

@app.route('/register/', methods=['POST'])
def register_with_master():
    register(app)
    return jsonify({'message': 'ok'})

def register(host, port):
    sess = requests.Session()

    if 'USERNAME' in app.config and 'PASSWORD' in app.config:
        sess.auth = (app.config['USERNAME'], app.config['PASSWORD'])
    sess.headers = {'content-type': 'application/json'}

    try:
        url = app.config['MASTER_URL'] + '/api/hosts/'
        resp = sess.post(url, data=json.dumps({
            'host': socket.getfqdn(), 'port': port
        }))
        if not resp.ok:
            app.logger.warning('Error registering with master: %s' %
                               app.config['MASTER_URL'])
    except Exception as e:
        app.logger.warning('Connection Error: %s' % e)


# Generated by Django 3.1.14 on 2022-03-11 15:06

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

def forward(apps, schema_editor):
    with schema_editor.connection.cursor() as cursor:
        cursor.execute(
            f"SELECT 1 FROM information_schema.columns WHERE table_name='infrastructure_infrastructure' AND column_name='published'"
        )
        if cursor.fetchone():
            for lang in settings.MODELTRANSLATION_LANGUAGES:
                cursor.execute(
                    f"SELECT 1 FROM information_schema.columns WHERE table_name='infrastructure_infrastructure' AND column_name='published_{lang}'"
                )
                if not cursor.fetchone():
                    cursor.execute(
                        f"ALTER TABLE infrastructure_infrastructure ADD published_{lang} Boolean DEFAULT True;"
                    )

def backward(apps, schema_editor):
    pass

class Migration(migrations.Migration):

    dependencies = [
        ('infrastructure', '0027_infrastructure_accessibility'),
    ]

    operations = [
        migrations.RunPython(forward, backward),
    ]

Add migration fix published language

Add migration fix published language

Response:
# Generated by Django 3.1.14 on 2022-03-11 15:06

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

def forward(apps, schema_editor):
    with schema_editor.connection.cursor() as cursor:
        cursor.execute(
            f"SELECT 1 FROM information_schema.columns WHERE table_name='infrastructure_infrastructure' AND column_name='published'"
        )
        if cursor.fetchone():
            for lang in settings.MODELTRANSLATION_LANGUAGES:
                cursor.execute(
                    f"SELECT 1 FROM information_schema.columns WHERE table_name='infrastructure_infrastructure' AND column_name='published_{lang}'"
                )
                if not cursor.fetchone():
                    cursor.execute(
                        f"ALTER TABLE infrastructure_infrastructure ADD published_{lang} Boolean DEFAULT True;"
                    )

def backward(apps, schema_editor):
    pass

class Migration(migrations.Migration):

    dependencies = [
        ('infrastructure', '0027_infrastructure_accessibility'),
    ]

    operations = [
        migrations.RunPython(forward, backward),
    ]

Prompt: Add migration fix published language


"""An interface to report the status of emulator launches."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import json
import logging
import os
import uuid

class NoOpReporter(object):
  """Captures all device and failure data and throws it away."""

  def __init__(self):
    pass

  def ReportDeviceProperties(self, unused_emu_type, unused_props):
    pass

  def ReportFailure(self, unused_component, unused_details):
    pass

  def ReportToolsUsage(self, unused_namespace, unused_tool_name,
                       unused_runtime_ms, unused_success,
                       unused_total_runtime):
    pass

  def Emit(self):
    pass

def MakeReporter():
  """Creates a reporter instance."""
  return NoOpReporter()

Response:
"""An interface to report the status of emulator launches."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import json
import logging
import os
import uuid

class NoOpReporter(object):
  """Captures all device and failure data and throws it away."""

  def __init__(self):
    pass

  def ReportDeviceProperties(self, unused_emu_type, unused_props):
    pass

  def ReportFailure(self, unused_component, unused_details):
    pass

  def ReportToolsUsage(self, unused_namespace, unused_tool_name,
                       unused_runtime_ms, unused_success,
                       unused_total_runtime):
    pass

  def Emit(self):
    pass

def MakeReporter():
  """Creates a reporter instance."""
  return NoOpReporter()

Prompt:
Update the reporter interface to even track the total runtime

PiperOrigin-RevId: 160982468

"""An interface to report the status of emulator launches."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import json
import logging
import os
import uuid

class NoOpReporter(object):
  """Captures all device and failure data and throws it away."""

  def __init__(self):
    pass

  def ReportDeviceProperties(self, unused_emu_type, unused_props):
    pass

  def ReportFailure(self, unused_component, unused_details):
    pass

  def ReportToolsUsage(self, unused_namespace, unused_tool_name,
                       unused_runtime_ms, unused_success):
    pass

  def Emit(self):
    pass

def MakeReporter():
  """Creates a reporter instance."""
  return NoOpReporter()


from functools import wraps
from bs4 import BeautifulSoup as bs

from .errors import (
    XMLError, PhoneError, StartTimeError,
    EndTimeError, LifetimeError, SpeedError,
    AlphanameError, TextError, InsufficientFundsError
)

ERROR_MAP = {
    'XMLERROR': XMLError,
    'ERRPHONES': PhoneError,
    'ERRSTARTTIME': StartTimeError,
    'ERRENDTIME': EndTimeError,
    'ERRLIFETIME': LifetimeError,
    'ERRSPEED': SpeedError,
    'ERRALFANAME': AlphanameError,
    'ERRTEXT': TextError,
    'INSUFFICIENTFUNDS': InsufficientFundsError
}

def parse_xml_response(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        res_xml = bs(f(*args, **kwargs), features='lxml-xml')
        state_code = res_xml.message.state['code']
        try:
            raise ERROR_MAP[state_code]
        except KeyError:
            return res_xml
    return wrapper

Fix import of error classes

Fix import of error classes

Response:
from functools import wraps
from bs4 import BeautifulSoup as bs

from .errors import (
    XMLError, PhoneError, StartTimeError,
    EndTimeError, LifetimeError, SpeedError,
    AlphanameError, TextError, InsufficientFundsError
)

ERROR_MAP = {
    'XMLERROR': XMLError,
    'ERRPHONES': PhoneError,
    'ERRSTARTTIME': StartTimeError,
    'ERRENDTIME': EndTimeError,
    'ERRLIFETIME': LifetimeError,
    'ERRSPEED': SpeedError,
    'ERRALFANAME': AlphanameError,
    'ERRTEXT': TextError,
    'INSUFFICIENTFUNDS': InsufficientFundsError
}

def parse_xml_response(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        res_xml = bs(f(*args, **kwargs), features='lxml-xml')
        state_code = res_xml.message.state['code']
        try:
            raise ERROR_MAP[state_code]
        except KeyError:
            return res_xml
    return wrapper

Prompt:
Fix import of error classes

from functools import wraps
from bs4 import BeautifulSoup as bs

from errors import XMLError, PhoneError, StartTimeError, EndTimeError, LifetimeError, SpeedError,
       AlphanameError, TextError, InsufficientFundsError

ERROR_MAP = {
    'XMLERROR': XMLError,
    'ERRPHONES': PhoneError,
    'ERRSTARTTIME': StartTimeError,
    'ERRENDTIME': EndTimeError,
    'ERRLIFETIME': LifetimeError,
    'ERRSPEED': SpeedError,
    'ERRALFANAME': AlphanameError,
    'ERRTEXT': TextError,
    'INSUFFICIENTFUNDS': InsufficientFundsError
}

def parse_xml_response(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        res_xml = bs(f(*args, **kwargs), features='lxml-xml')
        state_code = res_xml.message.state['code']
        try:
            raise ERROR_MAP[state_code]
        except KeyError:
            return res_xml
    return wrapper


import os
import json
from framework.mongo import set_up_storage

from website import settings
from website.project.taxonomies import Subject

from modularodm import Q, storage
from modularodm.exceptions import NoResultsFound, MultipleResultsFound

def update_taxonomies():
    # Flat taxonomy is stored locally, read in here
    with open(
        os.path.join(
            settings.APP_PATH,
            'website', 'static', 'plos_taxonomy.json'
        )
    ) as fp:
        taxonomy = json.load(fp)
        # For now, only PLOS taxonomy is loaded, other types possibly considered in the future
        type = 'plos'
        for subject_path in taxonomy.get('data'):
            subjects = subject_path.split('_')
            text = subjects[-1]

            # Search for parent subject, get id if it exists
            _parent = None
            if len(subjects) > 1:
                try:
                    _parent = Subject.find_one(
                        Q('text', 'eq', subjects[-2]) &
                        Q('type', 'eq', type)
                    )
                except Exception:
                    _parent = None

            parent_id = None
            if _parent:
                parent_id = _parent._id

            try:
                subject = Subject.find_one(
                    Q('text', 'eq', text) &
                    Q('type', 'eq', type)
                )
            except (NoResultsFound, MultipleResultsFound):
                # If subject does not yet exist, create it
                if parent_id:
                    subject = Subject(
                        type=type,
                        text=text,
                        parent_ids=[parent_id],
                    )
                else:
                    subject = Subject(
                        type=type,
                        text=text,
                        parent_ids=[],
                    )
            else:
                # If subject does exist, append parent_id if not already added
                subject.text = text
                subject.type = type
                if parent_id not in subject.parent_ids:
                    subject.parent_ids.append(parent_id)

            subject.save()

if __name__ == '__main__':
    set_up_storage([Subject], storage.MongoStorage)
    update_taxonomies()

Add a script that loads the taxonomies as a standalone thing

Response:
import os
import json
from framework.mongo import set_up_storage

from website import settings
from website.project.taxonomies import Subject

from modularodm import Q, storage
from modularodm.exceptions import NoResultsFound, MultipleResultsFound

def update_taxonomies():
    # Flat taxonomy is stored locally, read in here
    with open(
        os.path.join(
            settings.APP_PATH,
            'website', 'static', 'plos_taxonomy.json'
        )
    ) as fp:
        taxonomy = json.load(fp)
        # For now, only PLOS taxonomy is loaded, other types possibly considered in the future
        type = 'plos'
        for subject_path in taxonomy.get('data'):
            subjects = subject_path.split('_')
            text = subjects[-1]

            # Search for parent subject, get id if it exists
            _parent = None
            if len(subjects) > 1:
                try:
                    _parent = Subject.find_one(
                        Q('text', 'eq', subjects[-2]) &
                        Q('type', 'eq', type)
                    )
                except Exception:
                    _parent = None

            parent_id = None
            if _parent:
                parent_id = _parent._id

            try:
                subject = Subject.find_one(
                    Q('text', 'eq', text) &
                    Q('type', 'eq', type)
                )
            except (NoResultsFound, MultipleResultsFound):
                # If subject does not yet exist, create it
                if parent_id:
                    subject = Subject(
                        type=type,
                        text=text,
                        parent_ids=[parent_id],
                    )
                else:
                    subject = Subject(
                        type=type,
                        text=text,
                        parent_ids=[],
                    )
            else:
                # If subject does exist, append parent_id if not already added
                subject.text = text
                subject.type = type
                if parent_id not in subject.parent_ids:
                    subject.parent_ids.append(parent_id)

            subject.save()

if __name__ == '__main__':
    set_up_storage([Subject], storage.MongoStorage)
    update_taxonomies()

Prompt: Add a script that loads the taxonomies as a standalone thing


Change to Unix line endings

REF: Change to Unix line endings

Response:
"""Provides convenience utilities for assorted data files.

This package provides a means of organizing the code developed
at UKZN for handling the dataflow and processing of information
for the WRC funded research project K5-1683 "Soil Moisture from
Space".

The interface isn't stable yet so be prepared to update your code
on a regular basis...

"""

Prompt:
REF: Change to Unix line endings

"""Provides convenience utilities for assorted data files.

This package provides a means of organizing the code developed
at UKZN for handling the dataflow and processing of information
for the WRC funded research project K5-1683 "Soil Moisture from
Space".

The interface isn't stable yet so be prepared to update your code
on a regular basis...

"""


import logging

from autostew_back.tests.test_assets import prl_s4_r2_zolder_casual

logging.getLogger().setLevel(logging.INFO)
logging.getLogger('django.db.backends').setLevel(logging.INFO)
logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(logging.INFO)

class SettingsWithoutPlugins:
    host_name = "TestHost"
    server_name = "TestServer"
    config_file = "server.cfg"
    url = "http://localhost:9000"
    event_poll_period = 0
    full_update_period = 0

    setup_rotation = [
        prl_s4_r2_zolder_casual
    ]

    plugins = []

Set sleep times to zero for tests

Set sleep times to zero for tests

Response:
import logging

from autostew_back.tests.test_assets import prl_s4_r2_zolder_casual

logging.getLogger().setLevel(logging.INFO)
logging.getLogger('django.db.backends').setLevel(logging.INFO)
logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(logging.INFO)

class SettingsWithoutPlugins:
    host_name = "TestHost"
    server_name = "TestServer"
    config_file = "server.cfg"
    url = "http://localhost:9000"
    event_poll_period = 0
    full_update_period = 0

    setup_rotation = [
        prl_s4_r2_zolder_casual
    ]

    plugins = []

Prompt:
Set sleep times to zero for tests

import logging

from autostew_back.tests.test_assets import prl_s4_r2_zolder_casual

logging.getLogger().setLevel(logging.INFO)
logging.getLogger('django.db.backends').setLevel(logging.INFO)
logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(logging.INFO)

class SettingsWithoutPlugins:
    host_name = "TestHost"
    server_name = "TestServer"
    config_file = "server.cfg"
    url = "http://localhost:9000"
    event_poll_period = 1
    full_update_period = 5

    setup_rotation = [
        prl_s4_r2_zolder_casual
    ]

    plugins = []


from .. import int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64
# Note: This name is changed
from .. import bool_ as bool

__all__ = ['int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'float32', 'float64', 'bool']

Response:
from .. import int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64
# Note: This name is changed
from .. import bool_ as bool

__all__ = ['int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'float32', 'float64', 'bool']

Prompt:
Fix the bool name in the array API namespace

from .. import int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64, bool

__all__ = ['int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'float32', 'float64', 'bool']


#!/usr/bin/env python

# NOTE: setup.py does NOT install the contents of the library dir
# for you, you should go through "make install" or "make RPMs"
# for that, or manually copy modules over.

import os
import sys

sys.path.insert(0, os.path.abspath('lib'))
from ansible import __version__, __author__
from distutils.core import setup

setup(name='ansible',
      version=__version__,
      description='Minimal SSH command and control',
      author=__author__,
      author_email='michael.dehaan@gmail.com',
      url='http://ansible.github.com/',
      license='GPLv3',
      install_requires=['paramiko', 'jinja2', "PyYAML"],
      package_dir={ 'ansible': 'lib/ansible' },
      packages=[
         'ansible',
         'ansible.inventory',
         'ansible.playbook',
         'ansible.runner',
         'ansible.runner.connection',
      ],
      scripts=[
         'bin/ansible',
         'bin/ansible-playbook',
         'bin/ansible-pull'
      ]
)

Response:
#!/usr/bin/env python

# NOTE: setup.py does NOT install the contents of the library dir
# for you, you should go through "make install" or "make RPMs"
# for that, or manually copy modules over.

import os
import sys

sys.path.insert(0, os.path.abspath('lib'))
from ansible import __version__, __author__
from distutils.core import setup

setup(name='ansible',
      version=__version__,
      description='Minimal SSH command and control',
      author=__author__,
      author_email='michael.dehaan@gmail.com',
      url='http://ansible.github.com/',
      license='GPLv3',
      install_requires=['paramiko', 'jinja2', "PyYAML"],
      package_dir={ 'ansible': 'lib/ansible' },
      packages=[
         'ansible',
         'ansible.inventory',
         'ansible.playbook',
         'ansible.runner',
         'ansible.runner.connection',
      ],
      scripts=[
         'bin/ansible',
         'bin/ansible-playbook',
         'bin/ansible-pull'
      ]
)

Prompt:
Include bin/ansible-pull as part of the sdist in distutils.

#!/usr/bin/env python

# NOTE: setup.py does NOT install the contents of the library dir
# for you, you should go through "make install" or "make RPMs"
# for that, or manually copy modules over.

import os
import sys

sys.path.insert(0, os.path.abspath('lib'))
from ansible import __version__, __author__
from distutils.core import setup

setup(name='ansible',
      version=__version__,
      description='Minimal SSH command and control',
      author=__author__,
      author_email='michael.dehaan@gmail.com',
      url='http://ansible.github.com/',
      license='GPLv3',
      install_requires=['paramiko', 'jinja2', "PyYAML"],
      package_dir={ 'ansible': 'lib/ansible' },
      packages=[
         'ansible',
         'ansible.inventory',
         'ansible.playbook',
         'ansible.runner',
         'ansible.runner.connection',
      ],
      scripts=[
         'bin/ansible',
         'bin/ansible-playbook'
      ]
)


import os
import xlrd
import xlwt

# Assumes the directory with the workbook is relative to the script's location.
directory = 'workbooks/'

workbook = ''
for dirpath, dirnames, filenames in os.walk(directory):
    for files in filenames:
        workbook = (dirpath + files)

'''
Test films include:
Repulsion
The Crying Game
Saint Joan
My LIfe as a Zucchini
'''
# Collects the names of all the films for which individual workbooks need to be
# created.
films = []
wbs = int(input('Number of workbooks to create: '))
for wb in range(0, wbs):
    film = input('Name the film: ')
    films.append(film)

def create_workbook():
    '''Creates a workbook.

    Arguments:
        none

    Returns:
        book = The workbook that was opened.
    '''
    headers = ['First Name', 'Last Name', 'Email', 'Phone', 'Full address']
    book = xlwt.Workbook()
    sheet = book.add_sheet('Contacts')
    column_number = 0
    for header in headers:
        sheet.write(0, column_number, header)
        column_number += 1
    return book

def save_workbook(book, film):
    '''Saves the workbook created in create_workbook().

    Arguments:
        book = The object returned by calling create_workbook().
        film = The name of the film that will part of the workbook's name.

    Returns:
        none
    '''
    book.save('{0} contacts.xls'.format(film))

for film in films:
    save_workbook(create_workbook(), film)

Response:
import os
import xlrd
import xlwt

# Assumes the directory with the workbook is relative to the script's location.
directory = 'workbooks/'

workbook = ''
for dirpath, dirnames, filenames in os.walk(directory):
    for files in filenames:
        workbook = (dirpath + files)

'''
Test films include:
Repulsion
The Crying Game
Saint Joan
My LIfe as a Zucchini
'''
# Collects the names of all the films for which individual workbooks need to be
# created.
films = []
wbs = int(input('Number of workbooks to create: '))
for wb in range(0, wbs):
    film = input('Name the film: ')
    films.append(film)

def create_workbook():
    '''Creates a workbook.

    Arguments:
        none

    Returns:
        book = The workbook that was opened.
    '''
    headers = ['First Name', 'Last Name', 'Email', 'Phone', 'Full address']
    book = xlwt.Workbook()
    sheet = book.add_sheet('Contacts')
    column_number = 0
    for header in headers:
        sheet.write(0, column_number, header)
        column_number += 1
    return book

def save_workbook(book, film):
    '''Saves the workbook created in create_workbook().

    Arguments:
        book = The object returned by calling create_workbook().
        film = The name of the film that will part of the workbook's name.

    Returns:
        none
    '''
    book.save('{0} contacts.xls'.format(film))

for film in films:
    save_workbook(create_workbook(), film)

Prompt:
Add header row to worksheet in workbooks

import os
import xlrd
import xlwt

# Assumes the directory with the workbook is relative to the script's location.
directory = 'workbooks/'

workbook = ''
for dirpath, dirnames, filenames in os.walk(directory):
    for files in filenames:
        workbook = (dirpath + files)

'''
Test films include:
Repulsion
The Crying Game
Saint Joan
My LIfe as a Zucchini
'''
# Collects the names of all the films for which individual workbooks need to be
# created.
films = []
wbs = int(input('Number of workbooks to create: '))
for wb in range(0, wbs):
    film = input('Name the film: ')
    films.append(film)

def create_workbook():
    '''Creates a workbook.

    Arguments:
        none

    Returns:
        book = The workbook that was opened.
    '''
    book = xlwt.Workbook()
    sheet = book.add_sheet('Contacts')
    return book

def save_workbook(book, film):
    '''Saves the workbook created in create_workbook().

    Arguments:
        book = The object returned by calling create_workbook().
        film = The name of the film that will part of the workbook's name.

    Returns:
        none
    '''
    book.save('{0} contacts'.format(film))

for film in films:
    save_workbook(create_workbook(), film)


# -*- coding: utf-8 -*-
# Generated by Django 1.11.22 on 2019-10-31 16:33
from __future__ import unicode_literals

from django.db import migrations, OperationalError, ProgrammingError

def fix_fee_product_index(apps, schema_editor):
    try:
        schema_editor.execute(
            'DROP INDEX idx_16977_product_id;'
            'ALTER TABLE cfpb.ratechecker_fee '
            'DROP CONSTRAINT IF EXISTS idx_16977_product_id;'
            'ALTER TABLE cfpb.ratechecker_fee '
            'ADD CONSTRAINT idx_16977_product_id '
            'UNIQUE (product_id, state_id, lender, single_family, condo, coop);'
        )
    except (ProgrammingError, OperationalError):
        pass

class Migration(migrations.Migration):

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

    operations = [
        #migrations.RunPython(fix_fee_product_index),
        migrations.AlterUniqueTogether(
            name='fee',
            unique_together=set([]),
        ),
        migrations.RemoveField(
            model_name='fee',
            name='plan',
        ),
        migrations.DeleteModel(
            name='Fee',
        ),
    ]

Response:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.22 on 2019-10-31 16:33
from __future__ import unicode_literals

from django.db import migrations, OperationalError, ProgrammingError

def fix_fee_product_index(apps, schema_editor):
    try:
        schema_editor.execute(
            'DROP INDEX idx_16977_product_id;'
            'ALTER TABLE cfpb.ratechecker_fee '
            'DROP CONSTRAINT IF EXISTS idx_16977_product_id;'
            'ALTER TABLE cfpb.ratechecker_fee '
            'ADD CONSTRAINT idx_16977_product_id '
            'UNIQUE (product_id, state_id, lender, single_family, condo, coop);'
        )
    except (ProgrammingError, OperationalError):
        pass

class Migration(migrations.Migration):

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

    operations = [
        #migrations.RunPython(fix_fee_product_index),
        migrations.AlterUniqueTogether(
            name='fee',
            unique_together=set([]),
        ),
        migrations.RemoveField(
            model_name='fee',
            name='plan',
        ),
        migrations.DeleteModel(
            name='Fee',
        ),
    ]

Prompt:
Comment out fix_fee_product_index from migration

# -*- coding: utf-8 -*-
# Generated by Django 1.11.22 on 2019-10-31 16:33
from __future__ import unicode_literals

from django.db import migrations, OperationalError, ProgrammingError

def fix_fee_product_index(apps, schema_editor):
    table_name = 'cfpb.ratechecker_fee'
    index_name = 'idx_16977_product_id'
    try:
        schema_editor.execute(
            'DROP INDEX idx_16977_product_id;'
            'ALTER TABLE cfpb.ratechecker_fee '
            'DROP CONSTRAINT IF EXISTS idx_16977_product_id;'
            'ALTER TABLE cfpb.ratechecker_fee '
            'ADD CONSTRAINT idx_16977_product_id '
            'UNIQUE (product_id, state_id, lender, single_family, condo, coop);'
        )
    except (ProgrammingError, OperationalError):
        pass

class Migration(migrations.Migration):

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

    operations = [
        migrations.RunPython(fix_fee_product_index),
        migrations.AlterUniqueTogether(
            name='fee',
            unique_together=set([]),
        ),
        migrations.RemoveField(
            model_name='fee',
            name='plan',
        ),
        migrations.DeleteModel(
            name='Fee',
        ),
    ]


from setuptools import setup, find_packages
import os

scriptnames = ['runElegant.sh',
               'sddsprintdata.sh',
               'renametolower.sh',
               'file2lower.sh',
               'lte2json',
               'json2lte',
               'latticeviewer',
               'lv']

def readme():
    with open('README.rst') as f:
        return f.read()

requiredpackages = ['pyrpn', 'h5py', 'numpy', 'matplotlib', 'pyepics', 'wxPython']

setup(
        name     = "beamline",
        version  = "1.3.6",
        description = "online model package for electron accelerator",
        long_description = readme() + '\n\n',
        author   = "Tong Zhang",
        author_email = "warriorlance@gmail.com",
        platforms = ["Linux"],
        license  = "MIT",
        packages = find_packages(),
        url = "http://archman.github.io/beamline/",
        scripts  = [os.path.join('scripts',sn) for sn in scriptnames],
        requires = requiredpackages,
        install_requires = requiredpackages,
        extras_require = {'sdds': ['sddswhl']},
)

Add wxPython and use install_requires

Response:
from setuptools import setup, find_packages
import os

scriptnames = ['runElegant.sh',
               'sddsprintdata.sh',
               'renametolower.sh',
               'file2lower.sh',
               'lte2json',
               'json2lte',
               'latticeviewer',
               'lv']

def readme():
    with open('README.rst') as f:
        return f.read()

requiredpackages = ['pyrpn', 'h5py', 'numpy', 'matplotlib', 'pyepics', 'wxPython']

setup(
        name     = "beamline",
        version  = "1.3.6",
        description = "online model package for electron accelerator",
        long_description = readme() + '\n\n',
        author   = "Tong Zhang",
        author_email = "warriorlance@gmail.com",
        platforms = ["Linux"],
        license  = "MIT",
        packages = find_packages(),
        url = "http://archman.github.io/beamline/",
        scripts  = [os.path.join('scripts',sn) for sn in scriptnames],
        requires = requiredpackages,
        install_requires = requiredpackages,
        extras_require = {'sdds': ['sddswhl']},
)

Prompt:
Add wxPython and use install_requires

* Add wxPython as a depenency
* Use `install_requires` so dependencies are installed if one installs this package

from setuptools import setup, find_packages
import os

scriptnames = ['runElegant.sh',
               'sddsprintdata.sh',
               'renametolower.sh',
               'file2lower.sh',
               'lte2json',
               'json2lte',
               'latticeviewer',
               'lv']

def readme():
    with open('README.rst') as f:
        return f.read()

requiredpackages = ['pyrpn', 'h5py', 'numpy', 'matplotlib', 'pyepics']

setup(
        name     = "beamline",
        version  = "1.3.6",
        description = "online model package for electron accelerator",
        long_description = readme() + '\n\n',
        author   = "Tong Zhang",
        author_email = "warriorlance@gmail.com",
        platforms = ["Linux"],
        license  = "MIT",
        packages = find_packages(),
        url = "http://archman.github.io/beamline/",
        scripts  = [os.path.join('scripts',sn) for sn in scriptnames],
        requires = requiredpackages,
        extras_require = {'sdds': ['sddswhl']},
)


import pyaudio
import wave
import time
import sys
import numpy as np

if len(sys.argv) < 2:
    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
    sys.exit(-1)

wf = wave.open(sys.argv[1], 'rb')

p = pyaudio.PyAudio()

n_bytes_to_test = 1024 * 2 * 6

DEVICE_ID=2

def callback(in_data, frame_count, time_info, status):
    data = wf.readframes(frame_count)
    # npdata = np.frombuffer(data, dtype=np.int16)
    # print("len(data): {}, frame_count: {}".format(len(data), frame_count))
    if len(data) < n_bytes_to_test:
        wf.rewind()
        data = wf.readframes(frame_count)
	print("Rewinding")
    return (data, pyaudio.paContinue)

print("Device parameters: {}".format(p.get_device_info_by_index(DEVICE_ID)))

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=48000,
                output_device_index=DEVICE_ID,
                output=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():
    time.sleep(0.1)

stream.stop_stream()
stream.close()
wf.close()

p.terminate()

Add support for looping sample

Add support for looping sample

Response:
import pyaudio
import wave
import time
import sys
import numpy as np

if len(sys.argv) < 2:
    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
    sys.exit(-1)

wf = wave.open(sys.argv[1], 'rb')

p = pyaudio.PyAudio()

n_bytes_to_test = 1024 * 2 * 6

DEVICE_ID=2

def callback(in_data, frame_count, time_info, status):
    data = wf.readframes(frame_count)
    # npdata = np.frombuffer(data, dtype=np.int16)
    # print("len(data): {}, frame_count: {}".format(len(data), frame_count))
    if len(data) < n_bytes_to_test:
        wf.rewind()
        data = wf.readframes(frame_count)
	print("Rewinding")
    return (data, pyaudio.paContinue)

print("Device parameters: {}".format(p.get_device_info_by_index(DEVICE_ID)))

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=48000,
                output_device_index=DEVICE_ID,
                output=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():
    time.sleep(0.1)

stream.stop_stream()
stream.close()
wf.close()

p.terminate()

Prompt:
Add support for looping sample

import pyaudio
import wave
import time
import sys
import numpy as np

if len(sys.argv) < 2:
    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
    sys.exit(-1)

wf = wave.open(sys.argv[1], 'rb')

p = pyaudio.PyAudio()

DEVICE_ID=2

def callback(in_data, frame_count, time_info, status):
    data = wf.readframes(frame_count)
    # npdata = np.frombuffer(data, dtype=np.int16)
    return (data, pyaudio.paContinue)

print("Device parameters: {}".format(p.get_default_output_device_info()))

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output_device_index=DEVICE_ID,
                output=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():
    time.sleep(0.1)

stream.stop_stream()
stream.close()
wf.close()

p.terminate()


from django.contrib import admin
from models import MuseumObject,FunctionalCategory

class MOAdmin(admin.ModelAdmin):
    fields = ('registration_number','country','description','comment')

    list_display = ('registration_number','country','description','comment')

    list_filter = ('country','functional_category')

    search_fields = ['description','comment']

admin.site.register(MuseumObject, MOAdmin)

admin.site.register(FunctionalCategory)

Enable Django Admin for some of our data

Response:
from django.contrib import admin
from models import MuseumObject,FunctionalCategory

class MOAdmin(admin.ModelAdmin):
    fields = ('registration_number','country','description','comment')

    list_display = ('registration_number','country','description','comment')

    list_filter = ('country','functional_category')

    search_fields = ['description','comment']

admin.site.register(MuseumObject, MOAdmin)

admin.site.register(FunctionalCategory)

Prompt: Enable Django Admin for some of our data


Indent return in identify class.

Indent return in identify class.

Response:
#!/usr/bin/env python
#
# A class for auto identifying bioinformatics file formats.
# By Lee & Matt

import re
import json
import mmap

class BioID:
	defs = None

	def __init__(self, defpath):
		with open(defpath, "r") as deffile:
			conts = deffile.read()
		self.defs = json.loads(conts)["formats"]

	@classmethod
	def identify(cls, files):
		recog = {}
		for file in files:
			with open(file, "r") as infile:
				buff = infile.read()
				mem_map = mmap.mmap(infile.fileno(), 0, mmap.MAP_PRIVATE, mmap.PROT_READ)

			if len(buff) == 0:
				recog[file] = "empty"  # Empty files have no format :)
				continue

			for fdef in cls.defs:
				matched = True
				if "regexen" in fdef:
					for regex in fdef["regexen"]:
						if not re.findall(regex.replace("\\n", "\n"), buff, re.IGNORECASE):
							matched = False
							break
			if "bytes" in fdef:
				for bytes in fdef["bytes"]:
					if mem_map.find(bytes.decode("string_escape")) == -1:
						matched = False
						break
			if matched:
				recog[file] = fdef["name"]
				break

		mem_map.close()
		if file not in recog:
			recog[file] = "unrecognized"

		return recog

Prompt:
Indent return in identify class.

#!/usr/bin/env python
#
# A class for auto identifying bioinformatics file formats.
# By Lee & Matt

import re
import json
import mmap

class BioID:
    defs = None

    def __init__(self, defpath):
        with open(defpath, "r") as deffile:
            conts = deffile.read()
        self.defs = json.loads(conts)["formats"]

    @classmethod
    def identify(cls, files):
        recog = {}
        for file in files:
            with open(file, "r") as infile:
                buff = infile.read()
                mem_map = mmap.mmap(infile.fileno(), 0, mmap.MAP_PRIVATE, mmap.PROT_READ)

            if len(buff) == 0:
                recog[file] = "empty"  # Empty files have no format :)
                continue

            for fdef in cls.defs:
                matched = True
                if "regexen" in fdef:
                    for regex in fdef["regexen"]:
						if not re.findall(regex.replace("\\n", "\n"), buff, re.IGNORECASE):
                            matched = False
                            break
                if "bytes" in fdef:
                    for bytes in fdef["bytes"]:
                        if mem_map.find(bytes.decode("string_escape")) == -1:
                            matched = False
                            break
                if matched:
                    recog[file] = fdef["name"]
                    break

            mem_map.close()
            if file not in recog:
                recog[file] = "unrecognized"

        return recog


from __future__ import print_function

import numpy as np
np.random.seed(1337)

import keras.backend as K
from keras.models import Sequential
from keras.layers.core import Dense
from keras.layers.wrappers import TimeDistributed
from keras.layers.recurrent import LSTM
from keras.layers.normalization import BatchNormalization
from keras.optimizers import SGD

# wrapper of ctc_cost
def ctc_cost(y_true, y_pred):
    '''
    CTC cost:
    a theano wrapper for warp-ctc
    Arguments:
        y_true : label
        y_pred : acts
    '''
    from theano_ctc import ctc_cost as warp_ctc_cost

    # convert (batch size, timestep, target) to (timestep, batch size, target)
    acts = K.permute_dimensions(y_pred, (1, 0, 2))
    labels = K.cast(K.squeeze(y_true, axis=2), 'int32')
    return warp_ctc_cost(acts, labels)

batch_size = 16
frame_len = 80
nb_feat = 120
nb_class = 36
nb_output = nb_class + 1  # add output for blank
inner_dim = 512
nb_cell = 1024

print("Building model...")
model = Sequential()
model.add(LSTM(inner_dim, input_shape = (frame_len, nb_feat), return_sequences = True))
model.add(BatchNormalization())
model.add(TimeDistributed(Dense(nb_output)))

model.summary()

# Compiling
opt = SGD(lr = 1e-5, momentum = 0.9, nesterov = True)
model.compile(optimizer = opt, loss = ctc_cost, sample_weight_mode = None)

# Generate dummy data
data = np.random.uniform(low = -5, high = 5, size = (batch_size, frame_len, nb_feat))
# Dummy labels in range [1,nb_class]. 0 = <blank>
label = 1 + np.random.randint(nb_class, size = (batch_size, frame_len, 1))

# Training
model.fit(data, label, nb_epoch = 5, batch_size = batch_size)

Add simple test for Keras

Add simple test for Keras

Response:
from __future__ import print_function

import numpy as np
np.random.seed(1337)

import keras.backend as K
from keras.models import Sequential
from keras.layers.core import Dense
from keras.layers.wrappers import TimeDistributed
from keras.layers.recurrent import LSTM
from keras.layers.normalization import BatchNormalization
from keras.optimizers import SGD

# wrapper of ctc_cost
def ctc_cost(y_true, y_pred):
    '''
    CTC cost:
    a theano wrapper for warp-ctc
    Arguments:
        y_true : label
        y_pred : acts
    '''
    from theano_ctc import ctc_cost as warp_ctc_cost

    # convert (batch size, timestep, target) to (timestep, batch size, target)
    acts = K.permute_dimensions(y_pred, (1, 0, 2))
    labels = K.cast(K.squeeze(y_true, axis=2), 'int32')
    return warp_ctc_cost(acts, labels)

batch_size = 16
frame_len = 80
nb_feat = 120
nb_class = 36
nb_output = nb_class + 1  # add output for blank
inner_dim = 512
nb_cell = 1024

print("Building model...")
model = Sequential()
model.add(LSTM(inner_dim, input_shape = (frame_len, nb_feat), return_sequences = True))
model.add(BatchNormalization())
model.add(TimeDistributed(Dense(nb_output)))

model.summary()

# Compiling
opt = SGD(lr = 1e-5, momentum = 0.9, nesterov = True)
model.compile(optimizer = opt, loss = ctc_cost, sample_weight_mode = None)

# Generate dummy data
data = np.random.uniform(low = -5, high = 5, size = (batch_size, frame_len, nb_feat))
# Dummy labels in range [1,nb_class]. 0 = <blank>
label = 1 + np.random.randint(nb_class, size = (batch_size, frame_len, 1))

# Training
model.fit(data, label, nb_epoch = 5, batch_size = batch_size)

Prompt: Add simple test for Keras


# Author: Milos Buncic
# Date: 2017/10/14
# Description: Convert YAML to JSON and vice versa (OpenFaaS function)

import os
import sys
import json
import yaml

def yaml2json(data):
  """
  Convert YAML to JSON (output: JSON)
  """
  try:
    d = yaml.load(data, Loader=yaml.BaseLoader)
  except Exception as e:
    d = {'error': '{}'.format(e)}

  return json.dumps(d)

def json2yaml(data):
  """
  Convert JSON to YAML (output: YAML)
  """
  try:
    d = json.loads(data)
  except Exception as e:
    d = {'error': '{}'.format(e)}

  return yaml.dump(d, default_flow_style=False)

def handle(data, **parms):
  if parms.get('reverse') == 'true':
    print(json2yaml(data))
  else:
    print(yaml2json(data))

Response:
# Author: Milos Buncic
# Date: 2017/10/14
# Description: Convert YAML to JSON and vice versa (OpenFaaS function)

import os
import sys
import json
import yaml

def yaml2json(data):
  """
  Convert YAML to JSON (output: JSON)
  """
  try:
    d = yaml.load(data, Loader=yaml.BaseLoader)
  except Exception as e:
    d = {'error': '{}'.format(e)}

  return json.dumps(d)

def json2yaml(data):
  """
  Convert JSON to YAML (output: YAML)
  """
  try:
    d = json.loads(data)
  except Exception as e:
    d = {'error': '{}'.format(e)}

  return yaml.dump(d, default_flow_style=False)

def handle(data, **parms):
  if parms.get('reverse') == 'true':
    print(json2yaml(data))
  else:
    print(yaml2json(data))

Prompt:
Make handle function to behave similar as main function

# Author: Milos Buncic
# Date: 2017/10/14
# Description: Convert YAML to JSON and vice versa (OpenFaaS function)

import os
import sys
import json
import yaml

def handle(data, **parms):
  def yaml2json(ydata):
    """
    Convert YAML to JSON (output: JSON)
    """
    try:
      d = yaml.load(ydata, Loader=yaml.BaseLoader)
    except Exception as e:
      d = {'error': '{}'.format(e)}

    return json.dumps(d)

  def json2yaml(jdata):
    """
    Convert JSON to YAML (output: YAML)
    """
    try:
      d = json.loads(jdata)
    except Exception as e:
      d = {'error': '{}'.format(e)}

    return yaml.dump(d, default_flow_style=False)

  if parms.get('reverse') == 'true':
    print(json2yaml(data))
  else:
    print(yaml2json(data))


from django.core.management.base import BaseCommand

from topics.models import Topic

class Command(BaseCommand):
    help = u'Bootstrap the topic lists in the database.'

    def handle(self, *args, **kwargs):
        self.load_topics()

    def load_topics(self):
        self.stdout.write(u'Loading hot list topics...')

        topics = [
            u'Budget & Taxes',
            u'Business & Technology',
            u'Criminal Justice',
            u'Energy',
            u'Environment',
            u'Ethics',
            u'Health & Human Services',
            u'Higher Education',
            u'Immigration & Border Security',
            u'Public Education',
            u'Social Justice',
            u'Transportation',
        ]

        for topic in topics:
            Topic.objects.get_or_create(name=topic)

Response:
from django.core.management.base import BaseCommand

from topics.models import Topic

class Command(BaseCommand):
    help = u'Bootstrap the topic lists in the database.'

    def handle(self, *args, **kwargs):
        self.load_topics()

    def load_topics(self):
        self.stdout.write(u'Loading hot list topics...')

        topics = [
            u'Budget & Taxes',
            u'Business & Technology',
            u'Criminal Justice',
            u'Energy',
            u'Environment',
            u'Ethics',
            u'Health & Human Services',
            u'Higher Education',
            u'Immigration & Border Security',
            u'Public Education',
            u'Social Justice',
            u'Transportation',
        ]

        for topic in topics:
            Topic.objects.get_or_create(name=topic)

Prompt:
Split Energy and Environment, change Civil Liberties to Social Justice

from django.core.management.base import BaseCommand

from topics.models import Topic

class Command(BaseCommand):
    help = u'Bootstrap the topic lists in the database.'

    def handle(self, *args, **kwargs):
        self.load_topics()

    def load_topics(self):
        self.stdout.write(u'Loading hot list topics...')

        topics = [
            u'Budget & Taxes',
            u'Business & Technology',
            u'Civil Liberties',
            u'Criminal Justice',
            u'Energy & Environment',
            u'Ethics',
            u'Health & Human Services',
            u'Higher Education',
            u'Immigration & Border Security',
            u'Public Education',
            u'Transportation',
        ]

        for topic in topics:
            Topic.objects.get_or_create(name=topic)


import datetime
import json
import sys

import ffs
import re

from publish.lib.helpers import filename_for_resource, download_file
from publish.lib.upload import Uploader

def main(workspace):
    DATA_DIR = ffs.Path(workspace)
    datasets = json.load(open(DATA_DIR / 'ccgois_indicators.json'))

    u = Uploader("ccgois")
    for dataset in datasets:
        resources = []
        for resource in dataset['sources']:
            resource['format'] = resource['filetype']
            resource['name'] = resource['url'].split('/')[-1]

            filename = filename_for_resource(resource)
            path = DATA_DIR / filename

            download_file(resource['url'], path)
            print "Uploading to S3"
            url = u.upload(path)
            resource['url'] = url
            resources.append(resource)
        dataset['resources'] = resources
    u.close()

    json.dump(datasets, open(DATA_DIR / 'ccgois_indicators.json', 'w'))

Response:
import datetime
import json
import sys

import ffs
import re

from publish.lib.helpers import filename_for_resource, download_file
from publish.lib.upload import Uploader

def main(workspace):
    DATA_DIR = ffs.Path(workspace)
    datasets = json.load(open(DATA_DIR / 'ccgois_indicators.json'))

    u = Uploader("ccgois")
    for dataset in datasets:
        resources = []
        for resource in dataset['sources']:
            resource['format'] = resource['filetype']
            resource['name'] = resource['url'].split('/')[-1]

            filename = filename_for_resource(resource)
            path = DATA_DIR / filename

            download_file(resource['url'], path)
            print "Uploading to S3"
            url = u.upload(path)
            resource['url'] = url
            resources.append(resource)
        dataset['resources'] = resources
    u.close()

    json.dump(datasets, open(DATA_DIR / 'ccgois_indicators.json', 'w'))

Prompt:
Fix the naming of the newly created resources

import datetime
import json
import sys

import ffs
import re

from publish.lib.helpers import filename_for_resource, download_file
from publish.lib.upload import Uploader

def main(workspace):
    DATA_DIR = ffs.Path(workspace)
    datasets = json.load(open(DATA_DIR / 'ccgois_indicators.json'))

    u = Uploader("ccgois")
    for dataset in datasets:
        resources = []
        for resource in dataset['sources']:
            resource['format'] = resource['filetype']
            resource['name'] = resource['url'].split('/')[-1],

            filename = filename_for_resource(resource)
            path = DATA_DIR / filename

            download_file(resource['url'], path)
            print "Uploading to S3"
            url = u.upload(path)
            resource['url'] = url
            resources.append(resource)
        dataset['resources'] = resources
    u.close()

    print datasets[0]['resources']

    json.dump(datasets, open(DATA_DIR / 'ccgois_indicators.json', 'w'))


#!/usr/bin/python

import os
import sqlite3
import sys
import time

if len(sys.argv) < 3:
    raise ValueError('Usage: %s "Firstnam Lastname" email@example.com' % sys.argv[0])

db = sqlite3.connect('/var/lib/zon-api/data.db')
api_key = str(os.urandom(26).encode('hex'))
tier = 'free'
name = sys.argv[1]
email = sys.argv[2]
requests = 0
reset = int(time.time())
query = 'INSERT INTO client VALUES (?, ?, ?, ?, ?, ?)'
db.execute(query, (api_key, tier, name, email, requests, reset))
db.commit()
db.close()
print api_key

Add helper script to generate API keys.

Add helper script to generate API keys.

Response:
#!/usr/bin/python

import os
import sqlite3
import sys
import time

if len(sys.argv) < 3:
    raise ValueError('Usage: %s "Firstnam Lastname" email@example.com' % sys.argv[0])

db = sqlite3.connect('/var/lib/zon-api/data.db')
api_key = str(os.urandom(26).encode('hex'))
tier = 'free'
name = sys.argv[1]
email = sys.argv[2]
requests = 0
reset = int(time.time())
query = 'INSERT INTO client VALUES (?, ?, ?, ?, ?, ?)'
db.execute(query, (api_key, tier, name, email, requests, reset))
db.commit()
db.close()
print api_key

Prompt: Add helper script to generate API keys.


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

from django.shortcuts import render_to_response
from django.template import RequestContext

def home_page(request):
    return render_to_response('home_page/index.html', RequestContext(request))

Add RequestContext to Micro site

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

from django.shortcuts import render_to_response
from django.template import RequestContext

def home_page(request):
    return render_to_response('home_page/index.html', RequestContext(request))

Prompt:
Add RequestContext to Micro site

This allows us to populate settings variables such as Google Analytics
codes. See original work done for #769.

Refs #920

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

from django.shortcuts import render_to_response

def home_page(request):
    return render_to_response('home_page/index.html')


#!/usr/bin/env python

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

d = generate_distutils_setup(
    packages=['xacro'],
    package_dir={'': 'src'}
)

setup(**d)

Remove bin copy of xacro.py

Remove bin copy of xacro.py

Response:
#!/usr/bin/env python

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

d = generate_distutils_setup(
    packages=['xacro'],
    package_dir={'': 'src'}
)

setup(**d)

Prompt:
Remove bin copy of xacro.py
#!/usr/bin/env python

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

d = generate_distutils_setup(
    packages=['xacro'],
    package_dir={'': 'src'},
    scripts=['scripts/xacro.py']
)

setup(**d)


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [('auvsi_suas', '0012_missionclockevent'), ]

    operations = [
        migrations.AlterField(
            model_name='target',
            name='target_type',
            field=models.IntegerField(choices=[(1, b'standard'), (2, b'qrc'), (
                3, b'off_axis'), (4, b'emergent')]), ),
    ]

Remove the IR target type in migration.

Remove the IR target type in migration.

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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [('auvsi_suas', '0012_missionclockevent'), ]

    operations = [
        migrations.AlterField(
            model_name='target',
            name='target_type',
            field=models.IntegerField(choices=[(1, b'standard'), (2, b'qrc'), (
                3, b'off_axis'), (4, b'emergent')]), ),
    ]

Prompt: Remove the IR target type in migration.


from string import ascii_lowercase
import django
import os
import string

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
django.setup()

from breach.models import Target, Victim

endpoint = 'https://dionyziz.com/breach-test/reflect.php?ref=%s'
prefix = 'imper'
alphabet = ascii_lowercase
secretlength = 9

target_1 = Target(
    endpoint=endpoint,
    prefix=prefix,
    alphabet=alphabet,
    secretlength=secretlength,
    alignmentalphabet=string.ascii_uppercase
)
target_1.save()

print 'Created Target:\n\tendpoint: {}\n\tprefix: {}\n\talphabet: {}\n\tsecretlength: {}'.format(endpoint, prefix, alphabet, secretlength)

snifferendpoint = 'http://127.0.0.1:9000'
sourceip = '192.168.1.22'

victim_1 = Victim(
    target=target_1,
    snifferendpoint=snifferendpoint,
    sourceip=sourceip,
    method='serial'
)
victim_1.save()

print 'Created Victim:\n\tvictim_id: {}\n\tsnifferendpoint: {}\n\tsourceip: {}'.format(victim_1.id, snifferendpoint, sourceip)

Add population script for dionyziz

Add population script for dionyziz

Response:
from string import ascii_lowercase
import django
import os
import string

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
django.setup()

from breach.models import Target, Victim

endpoint = 'https://dionyziz.com/breach-test/reflect.php?ref=%s'
prefix = 'imper'
alphabet = ascii_lowercase
secretlength = 9

target_1 = Target(
    endpoint=endpoint,
    prefix=prefix,
    alphabet=alphabet,
    secretlength=secretlength,
    alignmentalphabet=string.ascii_uppercase
)
target_1.save()

print 'Created Target:\n\tendpoint: {}\n\tprefix: {}\n\talphabet: {}\n\tsecretlength: {}'.format(endpoint, prefix, alphabet, secretlength)

snifferendpoint = 'http://127.0.0.1:9000'
sourceip = '192.168.1.22'

victim_1 = Victim(
    target=target_1,
    snifferendpoint=snifferendpoint,
    sourceip=sourceip,
    method='serial'
)
victim_1.save()

print 'Created Victim:\n\tvictim_id: {}\n\tsnifferendpoint: {}\n\tsourceip: {}'.format(victim_1.id, snifferendpoint, sourceip)

Prompt: Add population script for dionyziz


from twisted.plugin import IPlugin
from txircd.module_interface import IModuleData, ModuleData
from txircd.utils import ircLower
from zope.interface import implements
from fnmatch import fnmatchcase

class AccountExtban(ModuleData):
	implements(IPlugin, IModuleData)

	name = "AccountExtban"

	def actions(self):
		return [ ("usermatchban-R", 1, self.matchBan),
		  ("usermetadataupdate", 10, self.updateBansOnAccountChange) ]

	def matchBan(self, user, matchNegated, mask):
		if not user.metadataKeyExists("account"):
			return matchNegated
		userAccount = ircLower(user.metadataValue("account"))
		lowerMask = ircLower(mask)
		if fnmatchcase(userAccount, lowerMask):
			return not matchNegated
		return matchNegated

	def updateBansOnAccountChange(self, user, key, oldValue, value, visibility, setByUser, fromServer = None):
		if key != "account":
			return
		self.ircd.runActionStandard("updateuserbancache", user)

matchExtban = AccountExtban()

Fix matching users against R: extbans

Fix matching users against R: extbans

Response:
from twisted.plugin import IPlugin
from txircd.module_interface import IModuleData, ModuleData
from txircd.utils import ircLower
from zope.interface import implements
from fnmatch import fnmatchcase

class AccountExtban(ModuleData):
	implements(IPlugin, IModuleData)

	name = "AccountExtban"

	def actions(self):
		return [ ("usermatchban-R", 1, self.matchBan),
		  ("usermetadataupdate", 10, self.updateBansOnAccountChange) ]

	def matchBan(self, user, matchNegated, mask):
		if not user.metadataKeyExists("account"):
			return matchNegated
		userAccount = ircLower(user.metadataValue("account"))
		lowerMask = ircLower(mask)
		if fnmatchcase(userAccount, lowerMask):
			return not matchNegated
		return matchNegated

	def updateBansOnAccountChange(self, user, key, oldValue, value, visibility, setByUser, fromServer = None):
		if key != "account":
			return
		self.ircd.runActionStandard("updateuserbancache", user)

matchExtban = AccountExtban()

Prompt:
Fix matching users against R: extbans

from twisted.plugin import IPlugin
from txircd.module_interface import IModuleData, ModuleData
from txircd.utils import ircLower
from zope.interface import implements
from fnmatch import fnmatchcase

class AccountExtban(ModuleData):
	implements(IPlugin, IModuleData)

	name = "AccountExtban"

	def actions(self):
		return [ ("usermatchban-R", 1, self.matchBan),
		  ("usermetadataupdate", 10, self.updateBansOnAccountChange) ]

	def matchBan(self, user, matchNegated, mask):
		if not user.metadataKeyExists("account"):
			return matchNegated
		userAccount = ircLower(user.metadataValue("account"))
		if fnmatchcase(userAccount, mask):
			return not matchNegated
		return matchNegated

	def updateBansOnAccountChange(self, user, key, oldValue, value, visibility, setByUser, fromServer = None):
		if key != "account":
			return
		self.ircd.runActionStandard("updateuserbancache", user)

matchExtban = AccountExtban()


import json
import jsonpointer

class Schema(dict):
    """
    Lightweight encapsulation of a JSON Schema.
    """

    @classmethod
    def from_file(cls, path_or_stream):
        """
        Create a schema from a file name or stream.
        """
        if hasattr(path_or_stream, 'read'):
            return cls(json.load(path_or_stream))
        else:
            with open(path_or_stream) as fp:
                return cls(json.load(fp))

    def resolve_ref(self, ref):
        if not ref.startswith('#'):
            raise ValueError("non-fragment references are not supported (got: %s)" % ref)
        return jsonpointer.resolve_pointer(self, ref.lstrip('#'))

Response:
import json
import jsonpointer

class Schema(dict):
    """
    Lightweight encapsulation of a JSON Schema.
    """

    @classmethod
    def from_file(cls, path_or_stream):
        """
        Create a schema from a file name or stream.
        """
        if hasattr(path_or_stream, 'read'):
            return cls(json.load(path_or_stream))
        else:
            with open(path_or_stream) as fp:
                return cls(json.load(fp))

    def resolve_ref(self, ref):
        if not ref.startswith('#'):
            raise ValueError("non-fragment references are not supported (got: %s)" % ref)
        return jsonpointer.resolve_pointer(self, ref.lstrip('#'))

Prompt:
Use jsonpointer instead of my own terrible Reference class.

import json

class Schema(dict):
    """
    Lightweight encapsulation of a JSON Schema.
    """

    @classmethod
    def from_file(cls, path_or_stream):
        """
        Create a schema from a file name or stream.
        """
        if hasattr(path_or_stream, 'read'):
            return cls(json.load(path_or_stream))
        else:
            with open(path_or_stream) as fp:
                return cls(json.load(fp))

    def resolve_ref(self, ref):
        return Reference(ref).resolve(self)

class Reference(object):
    def __init__(self, ref):
        if not ref.startswith('#'):
            raise ValueError("non-fragment references are not supported (got: %s)" % ref)
        self.ref = ref

    def resolve(self, schema):
        # Very overly simplisitic - doesn't handle array indexes, etc. However,
        # works with Heroku's schema, so good enough for a prototype.
        node = schema
        for bit in self.ref.split('/')[1:]:
            node = node[bit]
        return node


# __init__.py - collection of Brazilian numbers
# coding: utf-8
#
# Copyright (C) 2012 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""Collection of Brazilian numbers."""
from stdnum.br import cnpj as vat  # noqa: F401

Add missing vat alias for Brazil

Add missing vat alias for Brazil

Response:
# __init__.py - collection of Brazilian numbers
# coding: utf-8
#
# Copyright (C) 2012 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""Collection of Brazilian numbers."""
from stdnum.br import cnpj as vat  # noqa: F401

Prompt:
Add missing vat alias for Brazil

# __init__.py - collection of Brazilian numbers
# coding: utf-8
#
# Copyright (C) 2012 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""Collection of Brazilian numbers."""


from rest_framework.permissions import BasePermission

from course_discovery.apps.publisher.mixins import check_course_organization_permission, check_roles_access
from course_discovery.apps.publisher.models import OrganizationExtension
from course_discovery.apps.publisher.utils import is_internal_user, is_publisher_user

class CanViewAssociatedCourse(BasePermission):
    """ Permission class to check user can view a publisher course or also if
    user has view permission on organization.
    """

    def has_object_permission(self, request, view, obj):
        user = request.user
        return (
            check_roles_access(user) or
            check_course_organization_permission(user, obj.course, OrganizationExtension.VIEW_COURSE)
        )

class InternalUserPermission(BasePermission):
    """ Permission class to check user is an internal user. """

    def has_object_permission(self, request, view, obj):
        return request.user.is_staff or is_internal_user(request.user)

class PublisherUserPermission(BasePermission):
    """ Permission class to check user is a publisher user. """

    def has_object_permission(self, request, view, obj):
        return request.user.is_staff or is_publisher_user(request.user)

    def has_permission(self, request, view):
        return request.user.is_staff or is_publisher_user(request.user)

Allow staff access to Publisher APIs

Response:
from rest_framework.permissions import BasePermission

from course_discovery.apps.publisher.mixins import check_course_organization_permission, check_roles_access
from course_discovery.apps.publisher.models import OrganizationExtension
from course_discovery.apps.publisher.utils import is_internal_user, is_publisher_user

class CanViewAssociatedCourse(BasePermission):
    """ Permission class to check user can view a publisher course or also if
    user has view permission on organization.
    """

    def has_object_permission(self, request, view, obj):
        user = request.user
        return (
            check_roles_access(user) or
            check_course_organization_permission(user, obj.course, OrganizationExtension.VIEW_COURSE)
        )

class InternalUserPermission(BasePermission):
    """ Permission class to check user is an internal user. """

    def has_object_permission(self, request, view, obj):
        return request.user.is_staff or is_internal_user(request.user)

class PublisherUserPermission(BasePermission):
    """ Permission class to check user is a publisher user. """

    def has_object_permission(self, request, view, obj):
        return request.user.is_staff or is_publisher_user(request.user)

    def has_permission(self, request, view):
        return request.user.is_staff or is_publisher_user(request.user)

Prompt:
Allow staff access to Publisher APIs

A few Publisher APIs were marked as only allowing publisher users.
We should also let staff in.

DISCO-1365

from rest_framework.permissions import BasePermission

from course_discovery.apps.publisher.mixins import check_course_organization_permission, check_roles_access
from course_discovery.apps.publisher.models import OrganizationExtension
from course_discovery.apps.publisher.utils import is_internal_user, is_publisher_user

class CanViewAssociatedCourse(BasePermission):
    """ Permission class to check user can view a publisher course or also if
    user has view permission on organization.
    """

    def has_object_permission(self, request, view, obj):
        user = request.user
        return (
            check_roles_access(user) or
            check_course_organization_permission(user, obj.course, OrganizationExtension.VIEW_COURSE)
        )

class InternalUserPermission(BasePermission):
    """ Permission class to check user is an internal user. """

    def has_object_permission(self, request, view, obj):
        return is_internal_user(request.user)

class PublisherUserPermission(BasePermission):
    """ Permission class to check user is a publisher user. """

    def has_object_permission(self, request, view, obj):
        return is_publisher_user(request.user)

    def has_permission(self, request, view):
        return is_publisher_user(request.user)


import abc
from decimal import Decimal

from core import Key, Keyboard

class StandardKeyboard(Keyboard):
    """ A StandardKeyboard is a keyboard with standard Cherry MX key sizes and spacings. (see: http://www.fentek-ind.com/images/CHERRY_MX_keyswitch.pdf)

    """
    def __init__(self):
        self.unit_height = 19.05
        self.unit_width = 19.05
        self.separator = '|'
        self.comment = '-'

    @property
    def keys(self):
        keys = set()
        offset_y = Decimal(0)
        for line in self.schema.splitlines():
            # don't process lines starting with the comment prefix
            if line.startswith(self.comment):
                continue
            offset_x = Decimal(0)
            for key in line.strip(self.separator).split(self.separator):
                keys.add(Key(x=offset_x, y=offset_y))
                unit = len(key) * 0.25 + 1
                offset_x = offset_x.add(Decimal(self.unit_width * unit))
            offset_y = offset_y.add(Decimal(self.unit_height))
        return keys

    @abc.abstractproperty
    def schema(self):
        pass

Implement parsing in StandardKeyboard class

Response:
import abc
from decimal import Decimal

from core import Key, Keyboard

class StandardKeyboard(Keyboard):
    """ A StandardKeyboard is a keyboard with standard Cherry MX key sizes and spacings. (see: http://www.fentek-ind.com/images/CHERRY_MX_keyswitch.pdf)

    """
    def __init__(self):
        self.unit_height = 19.05
        self.unit_width = 19.05
        self.separator = '|'
        self.comment = '-'

    @property
    def keys(self):
        keys = set()
        offset_y = Decimal(0)
        for line in self.schema.splitlines():
            # don't process lines starting with the comment prefix
            if line.startswith(self.comment):
                continue
            offset_x = Decimal(0)
            for key in line.strip(self.separator).split(self.separator):
                keys.add(Key(x=offset_x, y=offset_y))
                unit = len(key) * 0.25 + 1
                offset_x = offset_x.add(Decimal(self.unit_width * unit))
            offset_y = offset_y.add(Decimal(self.unit_height))
        return keys

    @abc.abstractproperty
    def schema(self):
        pass

Prompt: Implement parsing in StandardKeyboard class


from __future__ import unicode_literals

import csv

from django.core.management.base import BaseCommand

from candidatebot.helpers import CandidateBot
from popolo.models import Person

class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument(
            'filename',
            help='Path to the file with the email addresses'
        )
        parser.add_argument(
            '--source',
            help='Source of the data. The source CSV column takes precedence'
        )

    def handle(self, **options):
        with open(options['filename'], 'r') as fh:
            reader = csv.DictReader(fh)
            for row in reader:
                source = row.get('source', options.get('source'))
                if not row['democlub_id']:
                    continue
                if not source:
                    raise ValueError("A source is required")

                try:
                    bot = CandidateBot(row['democlub_id'])
                    try:
                        bot.add_email(row['email'])
                        bot.save(source)
                    except ValueError:
                        #Email exists, move on
                        pass
                except Person.DoesNotExist:
                    print("Person ID {} not found".format(
                        row['democlub_id']))
                # print(row)

Move on if email exists

Move on if email exists

Response:
from __future__ import unicode_literals

import csv

from django.core.management.base import BaseCommand

from candidatebot.helpers import CandidateBot
from popolo.models import Person

class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument(
            'filename',
            help='Path to the file with the email addresses'
        )
        parser.add_argument(
            '--source',
            help='Source of the data. The source CSV column takes precedence'
        )

    def handle(self, **options):
        with open(options['filename'], 'r') as fh:
            reader = csv.DictReader(fh)
            for row in reader:
                source = row.get('source', options.get('source'))
                if not row['democlub_id']:
                    continue
                if not source:
                    raise ValueError("A source is required")

                try:
                    bot = CandidateBot(row['democlub_id'])
                    try:
                        bot.add_email(row['email'])
                        bot.save(source)
                    except ValueError:
                        #Email exists, move on
                        pass
                except Person.DoesNotExist:
                    print("Person ID {} not found".format(
                        row['democlub_id']))
                # print(row)

Prompt:
Move on if email exists

from __future__ import unicode_literals

import csv

from django.core.management.base import BaseCommand

from candidatebot.helpers import CandidateBot
from popolo.models import Person

class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument(
            'filename',
            help='Path to the file with the email addresses'
        )
        parser.add_argument(
            '--source',
            help='Source of the data. The source CSV column takes precedence'
        )

    def handle(self, **options):
        with open(options['filename'], 'r') as fh:
            reader = csv.DictReader(fh)
            for row in reader:
                source = row.get('source', options.get('source'))
                if not row['democlub_id']:
                    continue
                if not source:
                    raise ValueError("A source is required")

                try:
                    bot = CandidateBot(row['democlub_id'])
                    bot.add_email(row['email'])
                    bot.save(source)
                    # print(person)
                except Person.DoesNotExist:
                    print("Person ID {} not found".format(
                        row['democlub_id']))
                # print(row)


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

import unittest
import os
from mathdeck import loadproblem

class TestMathdeckLoadProblem(unittest.TestCase):

    def test_loadproblem_has_answers_attribute(self):
        file_name = 'has_answers_attribute.py'
        file = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                'fixtures','loadproblem', file_name)
        problem = loadproblem.load_file_as_module(file)
        self.assertTrue(hasattr(problem,'answers'))

    def test_loadproblem_has_no_answers_attribute(self):
        file_name = 'has_no_answers_attribute.py'
        file = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                'fixtures','loadproblem', file_name)
        self.assertRaises(Exception, loadproblem.load_file_as_module(file))

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

Fix parameter values for load function

Fix parameter values for load function

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

import unittest
import os
from mathdeck import loadproblem

class TestMathdeckLoadProblem(unittest.TestCase):

    def test_loadproblem_has_answers_attribute(self):
        file_name = 'has_answers_attribute.py'
        file = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                'fixtures','loadproblem', file_name)
        problem = loadproblem.load_file_as_module(file)
        self.assertTrue(hasattr(problem,'answers'))

    def test_loadproblem_has_no_answers_attribute(self):
        file_name = 'has_no_answers_attribute.py'
        file = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                'fixtures','loadproblem', file_name)
        self.assertRaises(Exception, loadproblem.load_file_as_module(file))

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

Prompt:
Fix parameter values for load function

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

import unittest
import os
from mathdeck import loadproblem

class TestMathdeckLoadProblem(unittest.TestCase):

    def test_loadproblem_has_answers_attribute(self):
        file_name = 'has_answers_attribute.py'
        problem_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                'fixtures','loadproblem')
        problem = loadproblem.load_file_as_module(problem_dir,file_name)
        self.assertTrue(hasattr(problem,'answers'))

    def test_loadproblem_has_no_answers_attribute(self):
        file_name = 'has_no_answers_attribute.py'
        problem_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                'fixtures','loadproblem')
        self.assertRaises(Exception, loadproblem. \
            load_file_as_module(problem_dir,file_name))

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


from django.conf.urls import patterns, url

urlpatterns = patterns(
    'buysafe.views',
    url(r'^entry/(?P<order_id>\d+)/$', 'entry', name='buysafe_pay'),
    (r'^start/$', 'start'),
    (r'^success/(?P<payment_type>[01])/$', 'success'),
    (r'^fail/(?P<payment_type>[01])/$', 'fail'),
    (r'^check/(?P<payment_type>[01])/$', 'check')
)

Add name for entry view URL

Add name for entry view URL

Response:
from django.conf.urls import patterns, url

urlpatterns = patterns(
    'buysafe.views',
    url(r'^entry/(?P<order_id>\d+)/$', 'entry', name='buysafe_pay'),
    (r'^start/$', 'start'),
    (r'^success/(?P<payment_type>[01])/$', 'success'),
    (r'^fail/(?P<payment_type>[01])/$', 'fail'),
    (r'^check/(?P<payment_type>[01])/$', 'check')
)

Prompt:
Add name for entry view URL

from django.conf.urls import patterns

urlpatterns = patterns(
    'buysafe.views',
    (r'^entry/(?P<order_id>\d+)/$', 'entry'),
    (r'^start/$', 'start'),
    (r'^success/(?P<payment_type>[01])/$', 'success'),
    (r'^fail/(?P<payment_type>[01])/$', 'fail'),
    (r'^check/(?P<payment_type>[01])/$', 'check')
)


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

from . import areas

class FailedRequest(Exception):
    def __init__(self, error):
        self.error = error

    def __str__(self):
        return repr(self.error)

class Client(object):
    """
    The Podio API client. Callers should use the factory method OAuthClient to create instances.
    """

    def __init__(self, transport):
        self.transport = transport

    def __getattr__(self, name):
        new_trans = self.transport
        area = getattr(areas, name)
        return area(new_trans)

    def __dir__(self):
        """
        Should return list of attribute names.
        Since __getattr__ looks in areas, we simply list the content of the areas module
        """
        return dir(areas)

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

from . import areas

class FailedRequest(Exception):
    def __init__(self, error):
        self.error = error

    def __str__(self):
        return repr(self.error)

class Client(object):
    """
    The Podio API client. Callers should use the factory method OAuthClient to create instances.
    """

    def __init__(self, transport):
        self.transport = transport

    def __getattr__(self, name):
        new_trans = self.transport
        area = getattr(areas, name)
        return area(new_trans)

    def __dir__(self):
        """
        Should return list of attribute names.
        Since __getattr__ looks in areas, we simply list the content of the areas module
        """
        return dir(areas)

Prompt:
Add __dir__ method to Client in order to allow autocompletion in interactive terminals, etc.

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

from . import areas

class FailedRequest(Exception):
    def __init__(self, error):
        self.error = error

    def __str__(self):
        return repr(self.error)

class Client(object):
    """
    The Podio API client. Callers should use the factory method OAuthClient to create instances.
    """

    def __init__(self, transport):
        self.transport = transport

    def __getattr__(self, name):
        new_trans = self.transport
        area = getattr(areas, name)
        return area(new_trans)