"""
API MAPPING FOR Eloqua API V2
"""

mapping_table = {

    'content_type': 'application/json',
    'path_prefix': '/API/REST/2.0',

    # Campaigns
    'get_campaign': {
        'method': 'GET',
        'path': '/assets/campaign/{{campaign_id}}',
        'valid_params': ['depth']
    },
    'list_campaigns': {
        'method': 'GET',
        'path': '/assets/campaigns',
        'valid_params': ['depth','count','page','search','sort','dir','orderBy','lastUpdatedAt']
    },
    'create_campaign': {
        'method': 'POST',
        'path': '/assets/campaign',
        'status': 201
    },
    'update_campaign': {
        'method': 'PUT',
        'path': '/assets/campaign/{{campaign_id}}'
    },
    'activate_campaign': {
        'method': 'POST',
        'path': '/assets/campaign/active/{{campaign_id}}',
        'valid_params': ['activateNow','scheduledFor','runAsUserId'],
        'status': 201
    },

    # Campaign folders - UNDOCUMENTED
    'get_campaign_folder': {
        'method': 'GET',
        'path': '/assets/campaign/folder/{{campaign_folder_id}}',
        'valid_params': ['depth']
    },
    'list_campaign_folders': {
        'method': 'GET',
        'path': '/assets/campaign/folders',
        'valid_params': ['depth','count','page','search','sort','dir','orderBy','lastUpdatedAt']
    },

}

Add operation to update campaign.

Add operation to update campaign.

Response:
"""
API MAPPING FOR Eloqua API V2
"""

mapping_table = {

    'content_type': 'application/json',
    'path_prefix': '/API/REST/2.0',

    # Campaigns
    'get_campaign': {
        'method': 'GET',
        'path': '/assets/campaign/{{campaign_id}}',
        'valid_params': ['depth']
    },
    'list_campaigns': {
        'method': 'GET',
        'path': '/assets/campaigns',
        'valid_params': ['depth','count','page','search','sort','dir','orderBy','lastUpdatedAt']
    },
    'create_campaign': {
        'method': 'POST',
        'path': '/assets/campaign',
        'status': 201
    },
    'update_campaign': {
        'method': 'PUT',
        'path': '/assets/campaign/{{campaign_id}}'
    },
    'activate_campaign': {
        'method': 'POST',
        'path': '/assets/campaign/active/{{campaign_id}}',
        'valid_params': ['activateNow','scheduledFor','runAsUserId'],
        'status': 201
    },

    # Campaign folders - UNDOCUMENTED
    'get_campaign_folder': {
        'method': 'GET',
        'path': '/assets/campaign/folder/{{campaign_folder_id}}',
        'valid_params': ['depth']
    },
    'list_campaign_folders': {
        'method': 'GET',
        'path': '/assets/campaign/folders',
        'valid_params': ['depth','count','page','search','sort','dir','orderBy','lastUpdatedAt']
    },

}

Prompt:
Add operation to update campaign.

"""
API MAPPING FOR Eloqua API V2
"""

mapping_table = {

    'content_type': 'application/json',
    'path_prefix': '/API/REST/2.0',

    # Campaigns
    'get_campaign': {
        'method': 'GET',
        'path': '/assets/campaign/{{campaign_id}}',
        'valid_params': ['depth']
    },
    'list_campaigns': {
        'method': 'GET',
        'path': '/assets/campaigns',
        'valid_params': ['depth','count','page','search','sort','dir','orderBy','lastUpdatedAt']
    },
    'create_campaign': {
        'method': 'POST',
        'path': '/assets/campaign',
        'status': 201
    },
    'update_campaign': {
        'method': 'PUT',
        'path': '/assets/campaign/{{campaign_id}}'
    },
    'activate_campaign': {
        'method': 'POST',
        'path': '/assets/campaign/active/{{campaign_id}}',
        'valid_params': ['activateNow','scheduledFor','runAsUserId']
    },

    # Campaign folders - UNDOCUMENTED
    'get_campaign_folder': {
        'method': 'GET',
        'path': '/assets/campaign/folder/{{campaign_folder_id}}',
        'valid_params': ['depth']
    },
    'list_campaign_folders': {
        'method': 'GET',
        'path': '/assets/campaign/folders',
        'valid_params': ['depth','count','page','search','sort','dir','orderBy','lastUpdatedAt']
    },

}


# Garmin

from decimal import Decimal

from ... import nmea

class GRM(nmea.ProprietarySentence):
    sentence_types = {}

    def __new__(_cls, manufacturer, data):
        name = manufacturer + data[0]
        cls = _cls.sentence_types.get(name, _cls)
        return super(GRM, cls).__new__(cls)

    def __init__(self, manufacturer, data):
        self.sentence_type = manufacturer + data[0]
        super(GRM, self).__init__(manufacturer, data[1:])

class GRME(GRM):
    """ GARMIN Estimated position error
    """
    fields = (
        ("Estimated Horiz. Position Error", "hpe", Decimal),
        ("Estimated Horiz. Position Error Unit (M)", "hpe_unit"),
        ("Estimated Vert. Position Error", "vpe", Decimal),
        ("Estimated Vert. Position Error Unit (M)", "vpe_unit"),
        ("Estimated Horiz. Position Error", "osepe", Decimal),
        ("Overall Spherical Equiv. Position Error", "osepe_unit")
    )

class GRMM(GRM):
    """ GARMIN Map Datum
    """
    fields = (
        ('Currently Active Datum', 'datum'),
    )

class GRMZ(GRM):
    """ GARMIN Altitude Information
    """
    fields = (
        ("Altitude", "altitude"),
        ("Altitude Units (Feet)", "altitude_unit"),
        ("Positional Fix Dimension (2=user, 3=GPS)", "pos_fix_dim")
    )

Response:
# Garmin

from decimal import Decimal

from ... import nmea

class GRM(nmea.ProprietarySentence):
    sentence_types = {}

    def __new__(_cls, manufacturer, data):
        name = manufacturer + data[0]
        cls = _cls.sentence_types.get(name, _cls)
        return super(GRM, cls).__new__(cls)

    def __init__(self, manufacturer, data):
        self.sentence_type = manufacturer + data[0]
        super(GRM, self).__init__(manufacturer, data[1:])

class GRME(GRM):
    """ GARMIN Estimated position error
    """
    fields = (
        ("Estimated Horiz. Position Error", "hpe", Decimal),
        ("Estimated Horiz. Position Error Unit (M)", "hpe_unit"),
        ("Estimated Vert. Position Error", "vpe", Decimal),
        ("Estimated Vert. Position Error Unit (M)", "vpe_unit"),
        ("Estimated Horiz. Position Error", "osepe", Decimal),
        ("Overall Spherical Equiv. Position Error", "osepe_unit")
    )

class GRMM(GRM):
    """ GARMIN Map Datum
    """
    fields = (
        ('Currently Active Datum', 'datum'),
    )

class GRMZ(GRM):
    """ GARMIN Altitude Information
    """
    fields = (
        ("Altitude", "altitude"),
        ("Altitude Units (Feet)", "altitude_unit"),
        ("Positional Fix Dimension (2=user, 3=GPS)", "pos_fix_dim")
    )

Prompt:
Add decimal types to Garmin PGRME fields.

# Garmin

from ... import nmea

class GRM(nmea.ProprietarySentence):
    sentence_types = {}

    def __new__(_cls, manufacturer, data):
        name = manufacturer + data[0]
        cls = _cls.sentence_types.get(name, _cls)
        return super(GRM, cls).__new__(cls)

    def __init__(self, manufacturer, data):
        self.sentence_type = manufacturer + data[0]
        super(GRM, self).__init__(manufacturer, data[1:])

class GRME(GRM):
    """ GARMIN Estimated position error
    """
    fields = (
        ("Estimated Horiz. Position Error", "hpe"),
        ("Estimated Horiz. Position Error Unit (M)", "hpe_unit"),
        ("Estimated Vert. Position Error", "vpe"),
        ("Estimated Vert. Position Error Unit (M)", "vpe_unit"),
        ("Estimated Horiz. Position Error", "osepe"),
        ("Overall Spherical Equiv. Position Error", "osepe_unit")
    )

class GRMM(GRM):
    """ GARMIN Map Datum
    """
    fields = (
        ('Currently Active Datum', 'datum'),
    )

class GRMZ(GRM):
    """ GARMIN Altitude Information
    """
    fields = (
        ("Altitude", "altitude"),
        ("Altitude Units (Feet)", "altitude_unit"),
        ("Positional Fix Dimension (2=user, 3=GPS)", "pos_fix_dim")
    )


"""yolk.

Author: Rob Cakebread <cakebread at gmail>

License  : BSD

"""

__version__ = '0.7'

Increment minor version to 0.7

Increment minor version to 0.7

Response:
"""yolk.

Author: Rob Cakebread <cakebread at gmail>

License  : BSD

"""

__version__ = '0.7'

Prompt:
Increment minor version to 0.7

"""yolk.

Author: Rob Cakebread <cakebread at gmail>

License  : BSD

"""

__version__ = '0.6.2'


# -*- coding: utf-8 -*-
import cv2
import sys

def resize(src, w_ratio, h_ratio):
    height = src.shape[0]
    width = src.shape[1]
    dst = cv2.resize(src,((int)(width/100*w_ratio),(int)(height/100*h_ratio)))
    return dst

if __name__ == '__main__':
    param = sys.argv
    if (len(param) != 4):
        print ("Usage: $ python " + param[0] + " sample.jpg wide_ratio height_ratio")
        quit()

    # open image file
    try:
        input_img = cv2.imread(param[1])
    except:
        print ('faild to load %s' % param[1])
        quit()

    if input_img is None:
        print ('faild to load %s' % param[1])
        quit()

    w_ratio = int(param[2])
    h_ratio = int(param[3])

    output_img = resize(input_img, w_ratio, h_ratio)
    cv2.imwrite(param[1], output_img)

Fix bug and delete unused library

Fix bug and delete unused library

Response:
# -*- coding: utf-8 -*-
import cv2
import sys

def resize(src, w_ratio, h_ratio):
    height = src.shape[0]
    width = src.shape[1]
    dst = cv2.resize(src,((int)(width/100*w_ratio),(int)(height/100*h_ratio)))
    return dst

if __name__ == '__main__':
    param = sys.argv
    if (len(param) != 4):
        print ("Usage: $ python " + param[0] + " sample.jpg wide_ratio height_ratio")
        quit()

    # open image file
    try:
        input_img = cv2.imread(param[1])
    except:
        print ('faild to load %s' % param[1])
        quit()

    if input_img is None:
        print ('faild to load %s' % param[1])
        quit()

    w_ratio = int(param[2])
    h_ratio = int(param[3])

    output_img = resize(input_img, w_ratio, h_ratio)
    cv2.imwrite(param[1], output_img)

Prompt:
Fix bug and delete unused library

# -*- coding: utf-8 -*-
import cv2
import sys
import numpy as np

def resize(src, w_ratio, h_ratio):
    height = src.shape[0]
    width = src.shape[1]
    dst = cv2.resize(src,(width/100*w_ratio,height/100*h_ratio))
    return dst

if __name__ == '__main__':
    param = sys.argv
    if (len(param) != 4):
        print ("Usage: $ python " + param[0] + " sample.jpg wide_ratio height_ratio")
        quit()

    # open image file
    try:
        input_img = cv2.imread(param[1])
    except:
        print ('faild to load %s' % param[1])
        quit()

    if input_img is None:
        print ('faild to load %s' % param[1])
        quit()

    w_ratio = int(param[2])
    h_ratio = int(param[3])

    output_img = resize(input_img, w_ratio, h_ratio)
    cv2.imwrite(param[1], output_img)


import os
import sys
import time
import math
import sqlite3
from contextlib import contextmanager

import meta

@contextmanager
def timeit_msg(msg):
	print '{}...'.format(msg),
	sys.stdout.flush()

	start = time.time()
	yield
	stop = time.time()

	print ' ({:1.3f} s)'.format((stop-start))

def get_file_size(fname):
	fstats = os.stat(fname)
	size = fstats.st_size
	exponent = int(math.log(size, 10))
	divisor = {'B': 0, 'kB': 3, 'MB': 6, 'GB': 9}
	if exponent < 3:
		res = 'B'
	if exponent >= 3 and exponent < 6:
		res = 'kB'
	elif exponent >= 6 and exponent < 9:
		res = 'MB'
	else:
		res = 'GB'
	return '{:1.2f} {}'.format(float(size) / 10**divisor[res], res)

def get_ncbi_db_conn():
	dbfile = os.path.join(meta.__path__[0], 'data', 'NCBI.db')
	conn = sqlite3.connect(dbfile)
	conn.text_factory = str
	return conn

Response:
import os
import sys
import time
import math
import sqlite3
from contextlib import contextmanager

import meta

@contextmanager
def timeit_msg(msg):
	print '{}...'.format(msg),
	sys.stdout.flush()

	start = time.time()
	yield
	stop = time.time()

	print ' ({:1.3f} s)'.format((stop-start))

def get_file_size(fname):
	fstats = os.stat(fname)
	size = fstats.st_size
	exponent = int(math.log(size, 10))
	divisor = {'B': 0, 'kB': 3, 'MB': 6, 'GB': 9}
	if exponent < 3:
		res = 'B'
	if exponent >= 3 and exponent < 6:
		res = 'kB'
	elif exponent >= 6 and exponent < 9:
		res = 'MB'
	else:
		res = 'GB'
	return '{:1.2f} {}'.format(float(size) / 10**divisor[res], res)

def get_ncbi_db_conn():
	dbfile = os.path.join(meta.__path__[0], 'data', 'NCBI.db')
	conn = sqlite3.connect(dbfile)
	conn.text_factory = str
	return conn

Prompt:
Add support for connecting to NCBI database

import os
import sys
import time
import math
from contextlib import contextmanager

@contextmanager
def timeit_msg(msg):
	print '{}...'.format(msg),
	sys.stdout.flush()

	start = time.time()
	yield
	stop = time.time()

	print ' ({:1.3f} s)'.format((stop-start))

def get_file_size(fname):
	fstats = os.stat(fname)
	size = fstats.st_size
	exponent = int(math.log(size, 10))
	divisor = {'B': 0, 'kB': 3, 'MB': 6, 'GB': 9}
	if exponent < 3:
		res = 'B'
	if exponent >= 3 and exponent < 6:
		res = 'kB'
	elif exponent >= 6 and exponent < 9:
		res = 'MB'
	else:
		res = 'GB'
	return '{:1.2f} {}'.format(float(size) / 10**divisor[res], res)


from setuptools import setup

setup(
    name='kibana',
    version='0.1',
    description='Kibana configuration index (.kibana in v4) command line interface and python API (visualization import/export and mappings refresh)',
    author='Ryan Farley',
    author_email='rfarley@mitre.org',
    packages=['kibana'],
    install_requires=(
        'elasticsearch',
        'requests>=2.5.0',
        'argparse',
        'requests',
    )
)

Add argparse, requests install reqs

Add argparse, requests install reqs

Response:
from setuptools import setup

setup(
    name='kibana',
    version='0.1',
    description='Kibana configuration index (.kibana in v4) command line interface and python API (visualization import/export and mappings refresh)',
    author='Ryan Farley',
    author_email='rfarley@mitre.org',
    packages=['kibana'],
    install_requires=(
        'elasticsearch',
        'requests>=2.5.0',
        'argparse',
        'requests',
    )
)

Prompt:
Add argparse, requests install reqs

from setuptools import setup

setup(
    name='kibana',
    version='0.1',
    description='Kibana configuration index (.kibana in v4) command line interface and python API (visualization import/export and mappings refresh)',
    author='Ryan Farley',
    author_email='rfarley@mitre.org',
    packages=['kibana'],
    install_requires=(
        'elasticsearch',
        'requests>=2.5.0',
    )
)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import PyV8
import urllib
from urlparse import urlparse

try:
    import json
except ImportError:
    import simplejson as json

class Wappalyzer(object):

    def __init__(self, url):
        self.file_dir = os.path.dirname(__file__)

        f = open(os.path.join(self.file_dir, '../../share/apps.json'))
        data = json.loads(f.read())
        f.close()

        self.categories = data['categories']
        self.apps = data['apps']
        self.url = url

    def analyze(self):
        ctxt = PyV8.JSContext()
        ctxt.enter()

        f1 = open(os.path.join(self.file_dir, '../php/js/wappalyzer.js'))
        f2 = open(os.path.join(self.file_dir, '../php/js/driver.js'))
        ctxt.eval(f1.read())
        ctxt.eval(f2.read())
        f1.close()
        f2.close()

        host = urlparse(self.url).hostname
        html = urllib.urlopen(self.url).read()

        data = {'host': host, 'url': self.url, 'html': html, 'headers': {}}
        apps = json.dumps(self.apps)
        categories = json.dumps(self.categories)
        return ctxt.eval("w.apps = %s; w.categories = %s; w.driver.data = %s; w.driver.init();" % (apps, categories, json.dumps(data)))

if __name__ == '__main__':
    try:
        w = Wappalyzer(sys.argv[1])
        print w.analyze()
    except IndexError:
        print ('Usage: python %s <url>' % sys.argv[0])

Add python driver (depend on PyV8)

Add python driver (depend on PyV8)

Response:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import PyV8
import urllib
from urlparse import urlparse

try:
    import json
except ImportError:
    import simplejson as json

class Wappalyzer(object):

    def __init__(self, url):
        self.file_dir = os.path.dirname(__file__)

        f = open(os.path.join(self.file_dir, '../../share/apps.json'))
        data = json.loads(f.read())
        f.close()

        self.categories = data['categories']
        self.apps = data['apps']
        self.url = url

    def analyze(self):
        ctxt = PyV8.JSContext()
        ctxt.enter()

        f1 = open(os.path.join(self.file_dir, '../php/js/wappalyzer.js'))
        f2 = open(os.path.join(self.file_dir, '../php/js/driver.js'))
        ctxt.eval(f1.read())
        ctxt.eval(f2.read())
        f1.close()
        f2.close()

        host = urlparse(self.url).hostname
        html = urllib.urlopen(self.url).read()

        data = {'host': host, 'url': self.url, 'html': html, 'headers': {}}
        apps = json.dumps(self.apps)
        categories = json.dumps(self.categories)
        return ctxt.eval("w.apps = %s; w.categories = %s; w.driver.data = %s; w.driver.init();" % (apps, categories, json.dumps(data)))

if __name__ == '__main__':
    try:
        w = Wappalyzer(sys.argv[1])
        print w.analyze()
    except IndexError:
        print ('Usage: python %s <url>' % sys.argv[0])

Prompt: Add python driver (depend on PyV8)


import logging
import os

import tornado.ioloop
import tornado.log
import tornado.options
import tornado.web

tornado.options.define('tornado_log_file',
                       default='/var/log/ipborg/torando.log',
                       type=str)
tornado.options.define('app_log_file',
                       default='/var/log/ipborg/app.log',
                       type=str)
tornado.options.parse_command_line()

def configure_tornado_logging():
    fh = logging.handlers.RotatingFileHandler(
        tornado.options.options.tornado_log_file,
        maxBytes=2**29, backupCount=10)

    fmt = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
    fh.setFormatter(fmt)

    logger = logging.getLogger('tornado')
    logger.setLevel(logging.DEBUG)
    logger.addHandler(fh)

    tornado.log.enable_pretty_logging(logger=logger)
configure_tornado_logging()

settings = {
    'static_path': os.path.join(os.path.dirname(__file__), 'static'),
    'template_path': os.path.join(os.path.dirname(__file__), 'templates'),
    'debug': True,
    'gzip': True
}

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write('{}'.format(os.environ.get('MC_PORT')))

application = tornado.web.Application([
    (r"/", MainHandler),
], **settings)

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

Response:
import logging
import os

import tornado.ioloop
import tornado.log
import tornado.options
import tornado.web

tornado.options.define('tornado_log_file',
                       default='/var/log/ipborg/torando.log',
                       type=str)
tornado.options.define('app_log_file',
                       default='/var/log/ipborg/app.log',
                       type=str)
tornado.options.parse_command_line()

def configure_tornado_logging():
    fh = logging.handlers.RotatingFileHandler(
        tornado.options.options.tornado_log_file,
        maxBytes=2**29, backupCount=10)

    fmt = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
    fh.setFormatter(fmt)

    logger = logging.getLogger('tornado')
    logger.setLevel(logging.DEBUG)
    logger.addHandler(fh)

    tornado.log.enable_pretty_logging(logger=logger)
configure_tornado_logging()

settings = {
    'static_path': os.path.join(os.path.dirname(__file__), 'static'),
    'template_path': os.path.join(os.path.dirname(__file__), 'templates'),
    'debug': True,
    'gzip': True
}

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write('{}'.format(os.environ.get('MC_PORT')))

application = tornado.web.Application([
    (r"/", MainHandler),
], **settings)

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

Prompt:
Add Tornado settings and log location command line options.

import logging
import os

import tornado.ioloop
import tornado.log
import tornado.web

def configure_tornado_logging():
    fh = logging.handlers.RotatingFileHandler(
        '/var/log/ipborg/tornado.log', maxBytes=2**29, backupCount=10)

    fmt = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
    fh.setFormatter(fmt)

    logger = logging.getLogger('tornado')
    logger.setLevel(logging.DEBUG)
    logger.addHandler(fh)

    tornado.log.enable_pretty_logging(logger=logger)
configure_tornado_logging()

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write('{}'.format(os.environ.get('MC_PORT')))

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()


"""

Very basic implementation of an event loop
==========================================

The eventloop is a singleton to schedule and run events.

Exports
-------

- ``EventLoop``: the basic eventloop

"""

import queue
import sched
import threading
import time

from .singleton import Singleton

class EventLoop(object, metaclass=Singleton):
    """A generic event loop object."""

    def __init__(self):
        self.scheduler = sched.scheduler()

    def schedule(self, event):
        """Schedule an event.

        An `event` is a thunk.

        """
        self.scheduler.enter(0, 1, event)

    def stop(self):
        """Stop the loop."""
        pass

    def run(self, block=False):
        self.scheduler.run(blocking=block)

    def run_in_thread(self):
        self.thread = threading.Thread(target=self.run, args=(True,),
                                       name='event_loop')
        self.thread.daemon = True
        self.thread.start()

Response:
"""

Very basic implementation of an event loop
==========================================

The eventloop is a singleton to schedule and run events.

Exports
-------

- ``EventLoop``: the basic eventloop

"""

import queue
import sched
import threading
import time

from .singleton import Singleton

class EventLoop(object, metaclass=Singleton):
    """A generic event loop object."""

    def __init__(self):
        self.scheduler = sched.scheduler()

    def schedule(self, event):
        """Schedule an event.

        An `event` is a thunk.

        """
        self.scheduler.enter(0, 1, event)

    def stop(self):
        """Stop the loop."""
        pass

    def run(self, block=False):
        self.scheduler.run(blocking=block)

    def run_in_thread(self):
        self.thread = threading.Thread(target=self.run, args=(True,),
                                       name='event_loop')
        self.thread.daemon = True
        self.thread.start()

Prompt:
Use Python scheduler rather than the home-made one
"""

Very basic implementation of an event loop
==========================================

The eventloop is a singleton to schedule and run events.

Exports
-------

- ``EventLoop``: the basic eventloop

"""

import queue
import threading
import time

from .singleton import Singleton

class EventLoop(object, metaclass=Singleton):
    """A generic event loop object."""

    def __init__(self):
        self.queue = queue.Queue()

    def schedule(self, event):
        """Schedule an event.

        An `event` is a thunk.

        """
        self.queue.put(event)

    def stop(self):
        """Stop the loop."""
        pass

    def run_step(self, block=True):
        """Process one event."""
        ev = self.queue.get(block=block)
        ev()

    def run(self, block=False):
        """Process all events in the queue."""
        try:
            while True:
                self.run_step(block=block)
        except queue.Empty:
            return

    def run_in_thread(self):
        self.thread = threading.Thread(target=self.run, args=(True,),
                                       name='event_loop')
        self.thread.daemon = True
        self.thread.start()


#!/usr/bin/env python

import docutils.core as dc

from writer import writer

import os.path
import sys
import glob

preamble = r'''
% These preamble commands are from build_paper.py

% PDF Standard Fonts
\usepackage{mathptmx}
\usepackage[scaled=.90]{helvet}
\usepackage{courier}

% Make verbatim environment smaller
\makeatletter
\g@addto@macro\@verbatim\footnotesize
\makeatother

\renewcommand{\quote}{}
'''

settings = {'documentclass': 'IEEEtran',
            'use_verbatim_when_possible': True,
            'use_latex_citations': True,
            'latex_preamble': preamble}

if len(sys.argv) != 2:
    print "Usage: build_paper.py paper_directory"
    sys.exit(-1)

path = sys.argv[1]
if not os.path.isdir(path):
    print("Cannot open directory: %s" % path)
    sys.exit(-1)

rst = glob.glob(os.path.join(path, '*.rst'))[0]

content = open(rst, 'r').read()
content = '''
.. role:: math(raw)
   :format: latex

''' + content

tex = dc.publish_string(source=content, writer=writer,
                        settings_overrides=settings)

out = open('/tmp/paper.tex', 'w')
out.write(tex)
out.close()

Response:
#!/usr/bin/env python

import docutils.core as dc

from writer import writer

import os.path
import sys
import glob

preamble = r'''
% These preamble commands are from build_paper.py

% PDF Standard Fonts
\usepackage{mathptmx}
\usepackage[scaled=.90]{helvet}
\usepackage{courier}

% Make verbatim environment smaller
\makeatletter
\g@addto@macro\@verbatim\footnotesize
\makeatother

\renewcommand{\quote}{}
'''

settings = {'documentclass': 'IEEEtran',
            'use_verbatim_when_possible': True,
            'use_latex_citations': True,
            'latex_preamble': preamble}

if len(sys.argv) != 2:
    print "Usage: build_paper.py paper_directory"
    sys.exit(-1)

path = sys.argv[1]
if not os.path.isdir(path):
    print("Cannot open directory: %s" % path)
    sys.exit(-1)

rst = glob.glob(os.path.join(path, '*.rst'))[0]

content = open(rst, 'r').read()
content = '''
.. role:: math(raw)
   :format: latex

''' + content

tex = dc.publish_string(source=content, writer=writer,
                        settings_overrides=settings)

out = open('/tmp/paper.tex', 'w')
out.write(tex)
out.close()

Prompt:
Update preamble: no indent on quote, smaller verbatim font size.

#!/usr/bin/env python

import docutils.core as dc

from writer import writer

import os.path
import sys
import glob

preamble = '\n'.join([r'% PDF Standard Fonts',
                      r'\usepackage{mathptmx}',
                      r'\usepackage[scaled=.80]{helvet}',
                      r'\usepackage{courier}'],)

settings = {'documentclass': 'IEEEtran',
            'use_verbatim_when_possible': True,
            'use_latex_citations': True,
            'latex_preamble': preamble}

if len(sys.argv) != 2:
    print "Usage: build_paper.py paper_directory"
    sys.exit(-1)

path = sys.argv[1]
if not os.path.isdir(path):
    print("Cannot open directory: %s" % path)
    sys.exit(-1)

rst = glob.glob(os.path.join(path, '*.rst'))[0]

content = open(rst, 'r').read()
content = '''
.. role:: math(raw)
   :format: latex

''' + content

tex = dc.publish_string(source=content, writer=writer,
                        settings_overrides=settings)

out = open('/tmp/paper.tex', 'w')
out.write(tex)
out.close()


import numpy
import cupy
import scipy.special
import cupyx.scipy.special

from cupy import testing
import pytest

scipy_ufuncs = {
    f
    for f in scipy.special.__all__
    if isinstance(getattr(scipy.special, f), numpy.ufunc)
}
cupyx_scipy_ufuncs = {
    f
    for f in dir(cupyx.scipy.special)
    if isinstance(getattr(cupyx.scipy.special, f), cupy.ufunc)
}

@testing.gpu
@testing.with_requires("scipy")
@pytest.mark.parametrize("ufunc", sorted(cupyx_scipy_ufuncs & scipy_ufuncs))
class TestUfunc:
    @testing.numpy_cupy_allclose(atol=1e-5)
    def test_dispatch(self, xp, ufunc):
        ufunc = getattr(scipy.special, ufunc)
        # some ufunc (like sph_harm) do not work with float inputs
        # therefore we retrieve the types from the ufunc itself
        types = ufunc.types[0]
        args = [
            cupy.testing.shaped_random((5,), xp, dtype=types[i])
            for i in range(ufunc.nargs - 1)
        ]
        res = ufunc(*args)
        assert type(res) == xp.ndarray
        return res

Response:
import numpy
import cupy
import scipy.special
import cupyx.scipy.special

from cupy import testing
import pytest

scipy_ufuncs = {
    f
    for f in scipy.special.__all__
    if isinstance(getattr(scipy.special, f), numpy.ufunc)
}
cupyx_scipy_ufuncs = {
    f
    for f in dir(cupyx.scipy.special)
    if isinstance(getattr(cupyx.scipy.special, f), cupy.ufunc)
}

@testing.gpu
@testing.with_requires("scipy")
@pytest.mark.parametrize("ufunc", sorted(cupyx_scipy_ufuncs & scipy_ufuncs))
class TestUfunc:
    @testing.numpy_cupy_allclose(atol=1e-5)
    def test_dispatch(self, xp, ufunc):
        ufunc = getattr(scipy.special, ufunc)
        # some ufunc (like sph_harm) do not work with float inputs
        # therefore we retrieve the types from the ufunc itself
        types = ufunc.types[0]
        args = [
            cupy.testing.shaped_random((5,), xp, dtype=types[i])
            for i in range(ufunc.nargs - 1)
        ]
        res = ufunc(*args)
        assert type(res) == xp.ndarray
        return res

Prompt:
Use sorted on the set to parametrize tests so that pytest-xdist works

import numpy
import cupy
import scipy.special
import cupyx.scipy.special

from cupy import testing
import pytest

scipy_ufuncs = {
    f
    for f in scipy.special.__all__
    if isinstance(getattr(scipy.special, f), numpy.ufunc)
}
cupyx_scipy_ufuncs = {
    f
    for f in dir(cupyx.scipy.special)
    if isinstance(getattr(cupyx.scipy.special, f), cupy.ufunc)
}

@testing.gpu
@testing.with_requires("scipy")
@pytest.mark.parametrize("ufunc", cupyx_scipy_ufuncs & scipy_ufuncs)
class TestUfunc:
    @testing.numpy_cupy_allclose(atol=1e-5)
    def test_dispatch(self, xp, ufunc):
        ufunc = getattr(scipy.special, ufunc)
        # some ufunc (like sph_harm) do not work with float inputs
        # therefore we retrieve the types from the ufunc itself
        types = ufunc.types[0]
        args = [
            cupy.testing.shaped_random((5,), xp, dtype=types[i])
            for i in range(ufunc.nargs - 1)
        ]
        res = ufunc(*args)
        assert type(res) == xp.ndarray
        return res


import json
from relay_api.core.relay import relay
from relay_api.conf.config import relays

def init_relays():
    for r in relays:
        relays[r]["object"] = relay(relays[r]["gpio"])
        relays[r]["state"] = relays[r]["object"].get_state()

def get_all_relays():
    relays_dict = __get_relay_dict()
    return json.dumps(relays_dict, indent=4)

def get_relay(relay_name):
    if relay_name not in relays:
        return None
    relay_dict = __get_relay_dict(relay_name)
    return json.dumps(relay_dict, indent=4)

def __get_relay_dict(relay_name=None):
    if relay_name:
        relay_dict = dict.copy(relays["relay_name"])
        del(relay_dict["object"])
        return relay_dict
    relays_dict = dict.copy(relays)
    for r in relays_dict:
        del(relays_dict[r]["object"])
    return relays_dict

Add indent in json to improve debugging

Add indent in json to improve debugging

Response:
import json
from relay_api.core.relay import relay
from relay_api.conf.config import relays

def init_relays():
    for r in relays:
        relays[r]["object"] = relay(relays[r]["gpio"])
        relays[r]["state"] = relays[r]["object"].get_state()

def get_all_relays():
    relays_dict = __get_relay_dict()
    return json.dumps(relays_dict, indent=4)

def get_relay(relay_name):
    if relay_name not in relays:
        return None
    relay_dict = __get_relay_dict(relay_name)
    return json.dumps(relay_dict, indent=4)

def __get_relay_dict(relay_name=None):
    if relay_name:
        relay_dict = dict.copy(relays["relay_name"])
        del(relay_dict["object"])
        return relay_dict
    relays_dict = dict.copy(relays)
    for r in relays_dict:
        del(relays_dict[r]["object"])
    return relays_dict

Prompt:
Add indent in json to improve debugging

import json
from relay_api.core.relay import relay
from relay_api.conf.config import relays

def init_relays():
    for r in relays:
        relays[r]["object"] = relay(relays[r]["gpio"])
        relays[r]["state"] = relays[r]["object"].get_state()

def get_all_relays():
    relays_dict = __get_relay_dict()
    return json.dumps(relays_dict)

def get_relay(relay_name):
    if relay_name not in relays:
        return None
    relay_dict = __get_relay_dict(relay_name)
    return json.dumps(relay_dict)

def __get_relay_dict(relay_name=None):
    if relay_name:
        relay_dict = dict.copy(relays["relay_name"])
        del(relay_dict["object"])
        return relay_dict
    relays_dict = dict.copy(relays)
    for r in relays_dict:
        del(relays_dict[r]["object"])
    return relays_dict


from locust import HttpLocust, TaskSet, task

class HttpPingTasks(TaskSet):
    @task
    def ping(self):
        self.client.get("/")

class SayHelloLocust(HttpLocust):
    task_set = HttpPingTasks
    min_wait = 100
    max_wait = 500

Add simple pinger for basic locust demo

Add simple pinger for basic locust demo

Response:
from locust import HttpLocust, TaskSet, task

class HttpPingTasks(TaskSet):
    @task
    def ping(self):
        self.client.get("/")

class SayHelloLocust(HttpLocust):
    task_set = HttpPingTasks
    min_wait = 100
    max_wait = 500

Prompt: Add simple pinger for basic locust demo


from setuptools import setup, find_packages

setup(
    name='django-waffle',
    version='0.7.1',
    description='A feature flipper for Django.',
    long_description=open('README.rst').read(),
    author='James Socol',
    author_email='james.socol@gmail.com',
    url='http://github.com/jsocol/django-waffle',
    license='BSD',
    packages=find_packages(exclude=['test_app']),
    include_package_data=True,
    package_data={'': ['README.rst']},
    zip_safe=False,
    classifiers=[
        'Development Status :: 4 - Beta',
        'Environment :: Web Environment',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ]
)

Switch to find_packages. Bump for PyPI.

Switch to find_packages. Bump for PyPI.

Response:
from setuptools import setup, find_packages

setup(
    name='django-waffle',
    version='0.7.1',
    description='A feature flipper for Django.',
    long_description=open('README.rst').read(),
    author='James Socol',
    author_email='james.socol@gmail.com',
    url='http://github.com/jsocol/django-waffle',
    license='BSD',
    packages=find_packages(exclude=['test_app']),
    include_package_data=True,
    package_data={'': ['README.rst']},
    zip_safe=False,
    classifiers=[
        'Development Status :: 4 - Beta',
        'Environment :: Web Environment',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ]
)

Prompt:
Switch to find_packages. Bump for PyPI.

from setuptools import setup

setup(
    name='django-waffle',
    version='0.7',
    description='A feature flipper for Django.',
    long_description=open('README.rst').read(),
    author='James Socol',
    author_email='james.socol@gmail.com',
    url='http://github.com/jsocol/django-waffle',
    license='BSD',
    packages=['waffle', 'waffle.templatetags'],
    include_package_data=True,
    package_data={'': ['README.rst']},
    zip_safe=False,
    classifiers=[
        'Development Status :: 4 - Beta',
        'Environment :: Web Environment',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ]
)


""" Test utilities for ensuring the correctness of products
"""
import arrow
import six

from tilezilla.core import BoundingBox, Band

MAPPING = {
    'timeseries_id': six.string_types,
    'acquired': arrow.Arrow,
    'processed': arrow.Arrow,
    'platform': six.string_types,
    'instrument': six.string_types,
    'bounds': BoundingBox,
    'bands': [Band],
    'metadata': dict,
    'metadata_files': dict
}

def check_attributes(product):
    for attr, _type in six.iteritems(MAPPING):
        assert hasattr(product, attr)
        value = getattr(product, attr)

        if isinstance(_type, (type, tuple)):
            # Type declaration one or more types
            assert isinstance(value, _type)
        else:
            # Type declaration list of types
            assert isinstance(value, type(_type))
            for item in value:
                assert isinstance(item, tuple(_type))

Allow str type comparison in py2/3

Allow str type comparison in py2/3

Response:
""" Test utilities for ensuring the correctness of products
"""
import arrow
import six

from tilezilla.core import BoundingBox, Band

MAPPING = {
    'timeseries_id': six.string_types,
    'acquired': arrow.Arrow,
    'processed': arrow.Arrow,
    'platform': six.string_types,
    'instrument': six.string_types,
    'bounds': BoundingBox,
    'bands': [Band],
    'metadata': dict,
    'metadata_files': dict
}

def check_attributes(product):
    for attr, _type in six.iteritems(MAPPING):
        assert hasattr(product, attr)
        value = getattr(product, attr)

        if isinstance(_type, (type, tuple)):
            # Type declaration one or more types
            assert isinstance(value, _type)
        else:
            # Type declaration list of types
            assert isinstance(value, type(_type))
            for item in value:
                assert isinstance(item, tuple(_type))

Prompt:
Allow str type comparison in py2/3

""" Test utilities for ensuring the correctness of products
"""
import arrow
import six

from tilezilla.core import BoundingBox, Band

MAPPING = {
    'timeseries_id': str,
    'acquired': arrow.Arrow,
    'processed': arrow.Arrow,
    'platform': str,
    'instrument': str,
    'bounds': BoundingBox,
    'bands': [Band],
    'metadata': dict,
    'metadata_files': dict
}

def check_attributes(product):
    for attr, _type in six.iteritems(MAPPING):
        assert hasattr(product, attr)
        value = getattr(product, attr)

        if isinstance(_type, type):
            assert isinstance(value, _type)
        else:
            assert isinstance(value, type(_type))
            for item in value:
                assert isinstance(item, tuple(_type))


#!/usr/bin/env python
#   -*- encoding: UTF-8 -*-

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

from turberfield.dialogue.types import Player

async def default(folder, ensemble:list, log=None, loop=None):
    if log is not None:
        log.debug("No activity during interlude")
    return folder

async def stop(folder, ensemble:list, log=None, loop=None):
    if log is not None:
        log.debug("Interlude stops dialogue")
    for entity in ensemble[:]:
        if not isinstance(entity, Player):
            ensemble.remove(entity)
    return None

Response:
#!/usr/bin/env python
#   -*- encoding: UTF-8 -*-

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

from turberfield.dialogue.types import Player

async def default(folder, ensemble:list, log=None, loop=None):
    if log is not None:
        log.debug("No activity during interlude")
    return folder

async def stop(folder, ensemble:list, log=None, loop=None):
    if log is not None:
        log.debug("Interlude stops dialogue")
    for entity in ensemble[:]:
        if not isinstance(entity, Player):
            ensemble.remove(entity)
    return None

Prompt:
Stop interlude removes non-player characters.

#!/usr/bin/env python
#   -*- encoding: UTF-8 -*-

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

async def default(folder, ensemble, log=None, loop=None):
    if log is not None:
        log.debug("No activity during interlude")
    return folder

async def stop(folder, ensemble, log=None, loop=None):
    if log is not None:
        log.debug("Interlude stops dialogue")
    return None


from . import uuid

def task_crud(self, shotgun, trigger_poll=lambda: None):

    shot_name = uuid(8)
    shot = shotgun.create('Shot', {'code': shot_name})

    name = uuid(8)
    task = shotgun.create('Task', {'content': name, 'entity': shot})
    trigger_poll()

    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['content'])
    self.assertSameEntity(task, x)

    # entity field
    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['entity'])
    self.assertSameEntity(shot, x['entity'])

    # return through entity field
    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['entity.Shot.code'])
    self.assertEqual(shot_name, x['entity.Shot.code'])

    # Updates
    name += '-2'
    shotgun.update('Task', task['id'], {'content': name})
    trigger_poll()
    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['content'])
    self.assertEqual(x['content'], name)

    # Delete
    shotgun.delete('Task', task['id'])
    trigger_poll()
    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['content'])
    self.assertIs(x, None)
    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['content'], retired_only=True)
    self.assertSameEntity(task, x)

Add entity link to basic crud tests

Add entity link to basic crud tests

Response:
from . import uuid

def task_crud(self, shotgun, trigger_poll=lambda: None):

    shot_name = uuid(8)
    shot = shotgun.create('Shot', {'code': shot_name})

    name = uuid(8)
    task = shotgun.create('Task', {'content': name, 'entity': shot})
    trigger_poll()

    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['content'])
    self.assertSameEntity(task, x)

    # entity field
    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['entity'])
    self.assertSameEntity(shot, x['entity'])

    # return through entity field
    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['entity.Shot.code'])
    self.assertEqual(shot_name, x['entity.Shot.code'])

    # Updates
    name += '-2'
    shotgun.update('Task', task['id'], {'content': name})
    trigger_poll()
    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['content'])
    self.assertEqual(x['content'], name)

    # Delete
    shotgun.delete('Task', task['id'])
    trigger_poll()
    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['content'])
    self.assertIs(x, None)
    x = self.cached.find_one('Task', [('id', 'is', task['id'])], ['content'], retired_only=True)
    self.assertSameEntity(task, x)

Prompt:
Add entity link to basic crud tests

from . import uuid

def task_crud(self, shotgun, trigger_poll=lambda: None):

    name = uuid(8)
    a = shotgun.create('Task', {'content': name})
    trigger_poll()
    b = self.cached.find_one('Task', [('id', 'is', a['id'])], ['content'])
    self.assertSameEntity(a, b)

    name += '-2'
    shotgun.update('Task', a['id'], {'content': name})
    trigger_poll()
    c = self.cached.find_one('Task', [('id', 'is', a['id'])], ['content'])
    self.assertEqual(c['content'], name)

    shotgun.delete('Task', a['id'])
    trigger_poll()
    d = self.cached.find_one('Task', [('id', 'is', a['id'])], ['content'])
    self.assertIs(d, None)


DEFAULT_URL = 'https://s3-us-west-2.amazonaws.com/openpolicingdata/PIALog_16-0806.zip'  # noqa
DATASET_BASENAME = 'PIALog_16-0806'
# DATASET_BASENAME = 'Small-0806'

Fix URL to current MD dataset on S3

Fix URL to current MD dataset on S3

Response:
DEFAULT_URL = 'https://s3-us-west-2.amazonaws.com/openpolicingdata/PIALog_16-0806.zip'  # noqa
DATASET_BASENAME = 'PIALog_16-0806'
# DATASET_BASENAME = 'Small-0806'

Prompt:
Fix URL to current MD dataset on S3

DEFAULT_URL = "https://s3-us-west-2.amazonaws.com/openpolicingdata/Maryland-Traffic-Stop-Data-2013.zip"  # noqa
DATASET_BASENAME = 'PIALog_16-0806'
# DATASET_BASENAME = 'Small-0806'


import marshmallow

class StorySchema(marshmallow.Schema):
    id = marshmallow.fields.Int(dump_only=True)
    title = marshmallow.fields.Str(required=True)
    created = marshmallow.fields.DateTime(dump_only=True)
    event_time = marshmallow.fields.DateTime(allow_none=True)
    location = marshmallow.fields.Str(allow_none=True)
    section = marshmallow.fields.Str(allow_none=True)

    @marshmallow.post_dump(pass_many=True)
    def wrap(self, data, many):
        key = 'stories' if many else 'story'
        return {key: data}

class PersonSchema(marshmallow.Schema):
    id = marshmallow.fields.Int(dump_only=True)
    name = marshmallow.fields.Str(required=True)

    @marshmallow.post_dump(pass_many=True)
    def wrap(self, data, many):
        key = 'people' if many else 'person'
        return {key: data}

class AddStoryPersonSchema(marshmallow.Schema):
    id = marshmallow.fields.Int()

    @marshmallow.post_dump(pass_many=True)
    def wrap(self, data, many):
        key = 'story_people' if many else 'story_person'
        return {key: data}

Response:
import marshmallow

class StorySchema(marshmallow.Schema):
    id = marshmallow.fields.Int(dump_only=True)
    title = marshmallow.fields.Str(required=True)
    created = marshmallow.fields.DateTime(dump_only=True)
    event_time = marshmallow.fields.DateTime(allow_none=True)
    location = marshmallow.fields.Str(allow_none=True)
    section = marshmallow.fields.Str(allow_none=True)

    @marshmallow.post_dump(pass_many=True)
    def wrap(self, data, many):
        key = 'stories' if many else 'story'
        return {key: data}

class PersonSchema(marshmallow.Schema):
    id = marshmallow.fields.Int(dump_only=True)
    name = marshmallow.fields.Str(required=True)

    @marshmallow.post_dump(pass_many=True)
    def wrap(self, data, many):
        key = 'people' if many else 'person'
        return {key: data}

class AddStoryPersonSchema(marshmallow.Schema):
    id = marshmallow.fields.Int()

    @marshmallow.post_dump(pass_many=True)
    def wrap(self, data, many):
        key = 'story_people' if many else 'story_person'
        return {key: data}

Prompt:
Allow story location and section to be null

import marshmallow

class StorySchema(marshmallow.Schema):
    id = marshmallow.fields.Int(dump_only=True)
    title = marshmallow.fields.Str(required=True)
    created = marshmallow.fields.DateTime(dump_only=True)
    event_time = marshmallow.fields.DateTime(allow_none=True)
    location = marshmallow.fields.Str()
    section = marshmallow.fields.Str()

    @marshmallow.post_dump(pass_many=True)
    def wrap(self, data, many):
        key = 'stories' if many else 'story'
        return {key: data}

class PersonSchema(marshmallow.Schema):
    id = marshmallow.fields.Int(dump_only=True)
    name = marshmallow.fields.Str(required=True)

    @marshmallow.post_dump(pass_many=True)
    def wrap(self, data, many):
        key = 'people' if many else 'person'
        return {key: data}

class AddStoryPersonSchema(marshmallow.Schema):
    id = marshmallow.fields.Int()

    @marshmallow.post_dump(pass_many=True)
    def wrap(self, data, many):
        key = 'story_people' if many else 'story_person'
        return {key: data}


# -*- coding: utf-8 -*-
# Copyright 2017 Stein & Gabelgaard ApS
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
    'name': 'Campos Checkin',
    'description': """
        CampOS Check In functionality""",
    'version': '8.0.1.0.0',
    'license': 'AGPL-3',
    'author': 'Stein & Gabelgaard ApS',
    'website': 'www.steingabelgaard.dk',
    'depends': [
        'campos_jobber_final',
        'campos_transportation',
        'campos_crewnet',
        'web_ir_actions_act_window_message',
        #'web_tree_dynamic_colored_field',
    ],
    'data': [
        'wizards/campos_checkin_wiz.xml',
        'security/campos_checkin.xml',
        'views/campos_event_participant.xml',
        'views/campos_mat_report.xml',
        'wizards/campos_checkin_grp_wiz.xml',
        'views/event_registration.xml',
    ],
    'demo': [
    ],
}

Fix order for menu ref

Fix order for menu ref

Response:
# -*- coding: utf-8 -*-
# Copyright 2017 Stein & Gabelgaard ApS
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
    'name': 'Campos Checkin',
    'description': """
        CampOS Check In functionality""",
    'version': '8.0.1.0.0',
    'license': 'AGPL-3',
    'author': 'Stein & Gabelgaard ApS',
    'website': 'www.steingabelgaard.dk',
    'depends': [
        'campos_jobber_final',
        'campos_transportation',
        'campos_crewnet',
        'web_ir_actions_act_window_message',
        #'web_tree_dynamic_colored_field',
    ],
    'data': [
        'wizards/campos_checkin_wiz.xml',
        'security/campos_checkin.xml',
        'views/campos_event_participant.xml',
        'views/campos_mat_report.xml',
        'wizards/campos_checkin_grp_wiz.xml',
        'views/event_registration.xml',
    ],
    'demo': [
    ],
}

Prompt:
Fix order for menu ref
# -*- coding: utf-8 -*-
# Copyright 2017 Stein & Gabelgaard ApS
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
    'name': 'Campos Checkin',
    'description': """
        CampOS Check In functionality""",
    'version': '8.0.1.0.0',
    'license': 'AGPL-3',
    'author': 'Stein & Gabelgaard ApS',
    'website': 'www.steingabelgaard.dk',
    'depends': [
        'campos_jobber_final',
        'campos_transportation',
        'campos_crewnet',
        'web_ir_actions_act_window_message',
        #'web_tree_dynamic_colored_field',
    ],
    'data': [
        'wizards/campos_checkin_grp_wiz.xml',
        'views/event_registration.xml',
        'wizards/campos_checkin_wiz.xml',
        'security/campos_checkin.xml',
        'views/campos_event_participant.xml',
        'views/campos_mat_report.xml',
    ],
    'demo': [
    ],
}


from .base import Base

class List(Base):
    current = None

    def __init__(self, result, object_type):
        Base.__init__(self, result)
        self.object_type = object_type

    def get_object_name(self):
        return self.object_type.__name__.lower() + 's'

    def __iter__(self):
        """Implement iterator logic."""
        self.current = None
        return self

    def __next__(self):
        """Implement iterator logic."""
        if self.current is None:
            self.current = 0
        else:
            self.current += 1
        try:
            item = self['_embedded'][self.get_object_name()][self.current]
            return self.object_type(item)
        except IndexError:
            raise StopIteration

    next = __next__  # support python2 iterator interface

    @property
    def count(self):
        if 'count' not in self:
            return None
        return int(self['count'])

    def get_offset(self):
        if 'offset' not in self:
            return None
        return self['offset']

Response:
from .base import Base

class List(Base):
    current = None

    def __init__(self, result, object_type):
        Base.__init__(self, result)
        self.object_type = object_type

    def get_object_name(self):
        return self.object_type.__name__.lower() + 's'

    def __iter__(self):
        """Implement iterator logic."""
        self.current = None
        return self

    def __next__(self):
        """Implement iterator logic."""
        if self.current is None:
            self.current = 0
        else:
            self.current += 1
        try:
            item = self['_embedded'][self.get_object_name()][self.current]
            return self.object_type(item)
        except IndexError:
            raise StopIteration

    next = __next__  # support python2 iterator interface

    @property
    def count(self):
        if 'count' not in self:
            return None
        return int(self['count'])

    def get_offset(self):
        if 'offset' not in self:
            return None
        return self['offset']

Prompt:
Add proxy method for python2 iterator support

from .base import Base

class List(Base):
    current = None

    def __init__(self, result, object_type):
        Base.__init__(self, result)
        self.object_type = object_type

    def get_object_name(self):
        return self.object_type.__name__.lower() + 's'

    def __iter__(self):
        """Implement iterator logic."""
        self.current = None
        return self

    def __next__(self):
        """Implement iterator logic."""
        if self.current is None:
            self.current = 0
        else:
            self.current += 1
        try:
            item = self['_embedded'][self.get_object_name()][self.current]
            return self.object_type(item)
        except IndexError:
            raise StopIteration

    @property
    def count(self):
        if 'count' not in self:
            return None
        return int(self['count'])

    def get_offset(self):
        if 'offset' not in self:
            return None
        return self['offset']


import sys

def import_attribute(import_path, exception_handler=None):
    try:
        from importlib import import_module
    except ImportError:
        from django.utils.importlib import import_module
    module_name, object_name = import_path.rsplit('.', 1)
    try:
        module = import_module(module_name)
    except:  # pragma: no cover
        if callable(exception_handler):
            exctype, excvalue, tb = sys.exc_info()
            return exception_handler(import_path, exctype, excvalue, tb)
        else:
            raise
    try:
        return getattr(module, object_name)
    except:  # pragma: no cover
        if callable(exception_handler):
            exctype, excvalue, tb = sys.exc_info()
            return exception_handler(import_path, exctype, excvalue, tb)
        else:
            raise

Response:
import sys

def import_attribute(import_path, exception_handler=None):
    try:
        from importlib import import_module
    except ImportError:
        from django.utils.importlib import import_module
    module_name, object_name = import_path.rsplit('.', 1)
    try:
        module = import_module(module_name)
    except:  # pragma: no cover
        if callable(exception_handler):
            exctype, excvalue, tb = sys.exc_info()
            return exception_handler(import_path, exctype, excvalue, tb)
        else:
            raise
    try:
        return getattr(module, object_name)
    except:  # pragma: no cover
        if callable(exception_handler):
            exctype, excvalue, tb = sys.exc_info()
            return exception_handler(import_path, exctype, excvalue, tb)
        else:
            raise

Prompt:
Use import_module from standard library if exists

Django 1.8+ drops `django.utils.importlib`. I imagine because that is because an older version of Python (either 2.5 and/or 2.6) is being dropped. I haven't checked older versions but `importlib` exists in Python 2.7.
import sys

def import_attribute(import_path, exception_handler=None):
    from django.utils.importlib import import_module
    module_name, object_name = import_path.rsplit('.', 1)
    try:
        module = import_module(module_name)
    except:  # pragma: no cover
        if callable(exception_handler):
            exctype, excvalue, tb = sys.exc_info()
            return exception_handler(import_path, exctype, excvalue, tb)
        else:
            raise
    try:
        return getattr(module, object_name)
    except:  # pragma: no cover
        if callable(exception_handler):
            exctype, excvalue, tb = sys.exc_info()
            return exception_handler(import_path, exctype, excvalue, tb)
        else:
            raise


# -*- coding: utf-8 -*-
# MIT Licensed, Copyright (c) 2016 Ryan Scott Brown <sb@ryansb.com>

from dotenv import load_dotenv, get_key, set_key, unset_key

def load_envs(path):
    import os
    path, _ = os.path.split(path)
    if path == '/':
        # bail out when you reach top of the FS
        load_dotenv(os.path.join(path, '.env'))
        return
    # load higher envs first
    # closer-to-base environments need higher precedence.
    load_envs(path)
    load_dotenv(os.path.join(path, '.env'))

Response:
# -*- coding: utf-8 -*-
# MIT Licensed, Copyright (c) 2016 Ryan Scott Brown <sb@ryansb.com>

from dotenv import load_dotenv, get_key, set_key, unset_key

def load_envs(path):
    import os
    path, _ = os.path.split(path)
    if path == '/':
        # bail out when you reach top of the FS
        load_dotenv(os.path.join(path, '.env'))
        return
    # load higher envs first
    # closer-to-base environments need higher precedence.
    load_envs(path)
    load_dotenv(os.path.join(path, '.env'))

Prompt:
Add `load_envs` to take a base path and recurse up, looking for .envs

# -*- coding: utf-8 -*-
# MIT Licensed, Copyright (c) 2016 Ryan Scott Brown <sb@ryansb.com>

import dotenv


import re

class ArtifactAnnotator:

    excluded_types = set(['heading', 'code'])

    def linkify_artifacts(marked_tree, artifacts):
        big_string = ArtifactAnnotator._marked_tree_to_big_string(marked_tree)

        for artifact in artifacts:
            link = '(%s "GHOSTDOC-TOKEN")' % artifact['_id']
            for token in artifact['tokens']:
                reg = ArtifactAnnotator._token_reg(token)
                repl = r'[\1]%s' % link
                big_string = reg.sub(repl, big_string)

        ArtifactAnnotator._big_string_to_marked_tree(marked_tree, big_string)
        return marked_tree

    def _token_reg(token):
        reg = r'(\b%s)' % token
        return re.compile(reg, re.IGNORECASE)

    def _marked_tree_to_big_string(marked_tree):
        strings = []
        for item in marked_tree:
            if 'text' in item and item['type'] not in ArtifactAnnotator.excluded_types:
                strings.append(item['text'])
        big_string = u'\u1394'.join(strings)
        return big_string

    def _big_string_to_marked_tree(marked_tree, big_string):
        strings = big_string.split(u'\u1394')
        i = 0
        for item in marked_tree:
            if 'text' in item and item['type'] not in ArtifactAnnotator.excluded_types
                item['text'] = strings[i]
                i = i + 1

Improve annotating of code segements

Response:
import re

class ArtifactAnnotator:

    excluded_types = set(['heading', 'code'])

    def linkify_artifacts(marked_tree, artifacts):
        big_string = ArtifactAnnotator._marked_tree_to_big_string(marked_tree)

        for artifact in artifacts:
            link = '(%s "GHOSTDOC-TOKEN")' % artifact['_id']
            for token in artifact['tokens']:
                reg = ArtifactAnnotator._token_reg(token)
                repl = r'[\1]%s' % link
                big_string = reg.sub(repl, big_string)

        ArtifactAnnotator._big_string_to_marked_tree(marked_tree, big_string)
        return marked_tree

    def _token_reg(token):
        reg = r'(\b%s)' % token
        return re.compile(reg, re.IGNORECASE)

    def _marked_tree_to_big_string(marked_tree):
        strings = []
        for item in marked_tree:
            if 'text' in item and item['type'] not in ArtifactAnnotator.excluded_types:
                strings.append(item['text'])
        big_string = u'\u1394'.join(strings)
        return big_string

    def _big_string_to_marked_tree(marked_tree, big_string):
        strings = big_string.split(u'\u1394')
        i = 0
        for item in marked_tree:
            if 'text' in item and item['type'] not in ArtifactAnnotator.excluded_types
                item['text'] = strings[i]
                i = i + 1

Prompt:
feat: Improve annotating of code segements

import re

class ArtifactAnnotator:
    def linkify_artifacts(marked_tree, artifacts):
        big_string = ArtifactAnnotator._marked_tree_to_big_string(marked_tree)

        for artifact in artifacts:
            link = '(%s "GHOSTDOC-TOKEN")' % artifact['_id']
            for token in artifact['tokens']:
                reg = ArtifactAnnotator._token_reg(token)
                repl = r'[\1]%s' % link
                big_string = reg.sub(repl, big_string)

        ArtifactAnnotator._big_string_to_marked_tree(marked_tree, big_string)
        return marked_tree

    def _token_reg(token):
        reg = r'(\b%s)' % token
        return re.compile(reg, re.IGNORECASE)

    def _marked_tree_to_big_string(marked_tree):
        strings = []
        for item in marked_tree:
            if 'text' in item and item['type'] != 'heading' and item[
                    'type'] != 'code':
                strings.append(item['text'])
        big_string = u'\u1394'.join(strings)
        return big_string

    def _big_string_to_marked_tree(marked_tree, big_string):
        strings = big_string.split(u'\u1394')
        i = 0
        for item in marked_tree:
            if 'text' in item and item['type'] != 'heading' and item[
                    'type'] != 'code':
                item['text'] = strings[i]
                i = i + 1


from pathlib import Path

extensions = [
    'sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'sphinx.ext.coverage']
master_doc = 'index'
project = 'cairocffi'
copyright = '2013-2019, Simon Sapin'
release = (
    Path(__file__).parent.parent / 'cairocffi' / 'VERSION').read_text().strip()
version = '.'.join(release.split('.')[:2])
exclude_patterns = ['_build']
autodoc_member_order = 'bysource'
autodoc_default_flags = ['members']
intersphinx_mapping = {
    'http://docs.python.org/': None,
    'http://cairographics.org/documentation/pycairo/2/': None}

Fix the VERSION path for doc

Fix the VERSION path for doc

Response:
from pathlib import Path

extensions = [
    'sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'sphinx.ext.coverage']
master_doc = 'index'
project = 'cairocffi'
copyright = '2013-2019, Simon Sapin'
release = (
    Path(__file__).parent.parent / 'cairocffi' / 'VERSION').read_text().strip()
version = '.'.join(release.split('.')[:2])
exclude_patterns = ['_build']
autodoc_member_order = 'bysource'
autodoc_default_flags = ['members']
intersphinx_mapping = {
    'http://docs.python.org/': None,
    'http://cairographics.org/documentation/pycairo/2/': None}

Prompt:
Fix the VERSION path for doc

from pathlib import Path

extensions = [
    'sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'sphinx.ext.coverage']
master_doc = 'index'
project = 'cairocffi'
copyright = '2013-2019, Simon Sapin'
release = (Path(__file__).parent / 'cairocffi' / 'VERSION').read_text().strip()
version = '.'.join(release.split('.')[:2])
exclude_patterns = ['_build']
autodoc_member_order = 'bysource'
autodoc_default_flags = ['members']
intersphinx_mapping = {
    'http://docs.python.org/': None,
    'http://cairographics.org/documentation/pycairo/2/': None}


#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Tests for koji_sign module.
"""

import unittest

import os
import sys

# HACK: inject empty koji module to silence failing tests.
# We need to add koji to deps (currently not possible)
# or create a mock object for testing.
import imp
sys.modules["koji"] = imp.new_module("koji")

DIR = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(DIR, ".."))

from releng_sop.common import Environment  # noqa: E402
from releng_sop.koji_sign import get_rpmsign_class, LocalRPMSign  # noqa: E402

RELEASES_DIR = os.path.join(DIR, "releases")
ENVIRONMENTS_DIR = os.path.join(DIR, "environments")

class TestRPMSignClass(unittest.TestCase):
    """
    Tests related to RPMSign classes.
    """

    longMessage = True

    def test_get_rpmsign_class(self):
        """Test if a RPMSign class can be properly loaded based on env settings."""
        env = Environment("test-env", config_dirs=[ENVIRONMENTS_DIR])
        cls = get_rpmsign_class(env)
        self.assertEqual(cls, LocalRPMSign)

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

Response:
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Tests for koji_sign module.
"""

import unittest

import os
import sys

# HACK: inject empty koji module to silence failing tests.
# We need to add koji to deps (currently not possible)
# or create a mock object for testing.
import imp
sys.modules["koji"] = imp.new_module("koji")

DIR = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(DIR, ".."))

from releng_sop.common import Environment  # noqa: E402
from releng_sop.koji_sign import get_rpmsign_class, LocalRPMSign  # noqa: E402

RELEASES_DIR = os.path.join(DIR, "releases")
ENVIRONMENTS_DIR = os.path.join(DIR, "environments")

class TestRPMSignClass(unittest.TestCase):
    """
    Tests related to RPMSign classes.
    """

    longMessage = True

    def test_get_rpmsign_class(self):
        """Test if a RPMSign class can be properly loaded based on env settings."""
        env = Environment("test-env", config_dirs=[ENVIRONMENTS_DIR])
        cls = get_rpmsign_class(env)
        self.assertEqual(cls, LocalRPMSign)

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

Prompt:
Fix failing tests by injecting an empty koji module.

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Tests for koji_sign module.
"""

import unittest

import os
import sys

DIR = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(DIR, ".."))

from releng_sop.common import Environment  # noqa: E402
from releng_sop.koji_sign import get_rpmsign_class, LocalRPMSign  # noqa: E402

RELEASES_DIR = os.path.join(DIR, "releases")
ENVIRONMENTS_DIR = os.path.join(DIR, "environments")

class TestRPMSignClass(unittest.TestCase):
    """
    Tests related to RPMSign classes.
    """

    longMessage = True

    def test_get_rpmsign_class(self):
        """Test if a RPMSign class can be properly loaded based on env settings."""
        env = Environment("test-env", config_dirs=[ENVIRONMENTS_DIR])
        cls = get_rpmsign_class(env)
        self.assertEqual(cls, LocalRPMSign)

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


import dumper.utils

def invalidate_paths(paths):
    '''
    Invalidate all pages for a certain path.
    '''
    for path in paths:
        for key in all_cache_keys_from_path(path):
            dumper.utils.cache.delete(key)

def all_cache_keys_from_path(path):
    '''
    Each path can actually have multiple cached entries, varying based on different HTTP
    methods. So a GET request will have a different cached response from a HEAD request.

    In order to invalidate a path, we must first know all the different cache keys that the
    path might have been cached at. This returns those keys
    '''
    return [dumper.utils.cache_key(path, method) for method in dumper.settings.CACHABLE_METHODS]

Response:
import dumper.utils

def invalidate_paths(paths):
    '''
    Invalidate all pages for a certain path.
    '''
    for path in paths:
        for key in all_cache_keys_from_path(path):
            dumper.utils.cache.delete(key)

def all_cache_keys_from_path(path):
    '''
    Each path can actually have multiple cached entries, varying based on different HTTP
    methods. So a GET request will have a different cached response from a HEAD request.

    In order to invalidate a path, we must first know all the different cache keys that the
    path might have been cached at. This returns those keys
    '''
    return [dumper.utils.cache_key(path, method) for method in dumper.settings.CACHABLE_METHODS]

Prompt:
Comment concerning differences in keys per path
import dumper.utils

def invalidate_paths(paths):
    '''
    Invalidate all pages for a certain path.
    '''
    for path in paths:
        for key in all_cache_keys_from_path(path):
            dumper.utils.cache.delete(key)

def all_cache_keys_from_path(path):
    return [dumper.utils.cache_key(path, method) for method in dumper.settings.CACHABLE_METHODS]


import urllib
urllib.urlretrieve('http://192.168.0.13:8080/photoaf.jpg', 'img.jpg')

Change save path to local path

Change save path to local path

Response:
import urllib
urllib.urlretrieve('http://192.168.0.13:8080/photoaf.jpg', 'img.jpg')

Prompt:
Change save path to local path

import urllib
urllib.urlretrieve('http://192.168.0.13:8080/photoaf.jpg', '/home/pi/img/img.jpg')


import unittest

from werkzeug.datastructures import MultiDict

from cla_public.app import create_app
from cla_public.apps.contact.forms import ContactForm

def submit(**kwargs):
    return ContactForm(MultiDict(kwargs), csrf_enabled=False)

class NotesTest(unittest.TestCase):

    def setUp(self):
        app = create_app('config/testing.py')
        app.test_request_context().push()

    def validate_notes(self, notes):
        form = submit(
            extra_notes=notes)
        form.validate()
        return u'Your notes must be 4000 characters or less' not in \
            form.extra_notes.errors

    def test_notes_max_length(self):
        longest_allowed = 'x' * 4000
        self.assertTrue(self.validate_notes(longest_allowed))

        too_long = longest_allowed + 'x'
        self.assertFalse(self.validate_notes(too_long))

Add python test for notes length validation

Response:
import unittest

from werkzeug.datastructures import MultiDict

from cla_public.app import create_app
from cla_public.apps.contact.forms import ContactForm

def submit(**kwargs):
    return ContactForm(MultiDict(kwargs), csrf_enabled=False)

class NotesTest(unittest.TestCase):

    def setUp(self):
        app = create_app('config/testing.py')
        app.test_request_context().push()

    def validate_notes(self, notes):
        form = submit(
            extra_notes=notes)
        form.validate()
        return u'Your notes must be 4000 characters or less' not in \
            form.extra_notes.errors

    def test_notes_max_length(self):
        longest_allowed = 'x' * 4000
        self.assertTrue(self.validate_notes(longest_allowed))

        too_long = longest_allowed + 'x'
        self.assertFalse(self.validate_notes(too_long))

Prompt: Add python test for notes length validation


#! /usr/bin/env python

from tools import check_cmd

def run_cyclus(cyclus, cwd, sim_files):
    """Runs cyclus with various inputs and creates output databases
       """

    for sim_input, sim_output in sim_files:
        holdsrtn = [1]  # needed because nose does not send() to test generator
        cmd = [cyclus, "-o", sim_output, "--input-file", sim_input]
        check_cmd(cmd, cwd, holdsrtn)
        rtn = holdsrtn[0]
        if rtn != 0:
            return  # don"t execute further commands

Response:
#! /usr/bin/env python

from tools import check_cmd

def run_cyclus(cyclus, cwd, sim_files):
    """Runs cyclus with various inputs and creates output databases
       """

    for sim_input, sim_output in sim_files:
        holdsrtn = [1]  # needed because nose does not send() to test generator
        cmd = [cyclus, "-o", sim_output, "--input-file", sim_input]
        check_cmd(cmd, cwd, holdsrtn)
        rtn = holdsrtn[0]
        if rtn != 0:
            return  # don"t execute further commands

Prompt:
Correct zip() error in run_cyclus function

#! /usr/bin/env python

from tools import check_cmd

def run_cyclus(cyclus, cwd, sim_files):
    """Runs cyclus with various inputs and creates output databases
       """

    for sim_input, sim_output in zip(sim_files):
        holdsrtn = [1]  # needed because nose does not send() to test generator
        cmd = [cyclus, "-o", sim_output, "--input-file", sim_input]
        check_cmd(cmd, cwd, holdsrtn)
        rtn = holdsrtn[0]
        if rtn != 0:
            return  # don"t execute further commands


import discord
from stickord.registry import Command

@Command(['tellen', 'tel'])
async def counting(cont, mesg, client, *args, **kwargs):
    count, auth, when = (0, discord.User, None)
    if cont:
        try:
            num = int(cont[0])
            if num != count + 1:
                return f'Whoops, you done goof! you should have entered "{cnt+1}" but you entered "{num}"'
            else if mesg.author.id == auth.id:
                return f'You can\'t submit a number twice in a row! Shame on you {mesg.author.mention}!'
            else:
                #save num
                await client.add_reaction(mesg, '\U0001f44c')
        except ValueError:
            return 'Entered number was not valid.'

Add framework for counting command

Add framework for counting command

Response:
import discord
from stickord.registry import Command

@Command(['tellen', 'tel'])
async def counting(cont, mesg, client, *args, **kwargs):
    count, auth, when = (0, discord.User, None)
    if cont:
        try:
            num = int(cont[0])
            if num != count + 1:
                return f'Whoops, you done goof! you should have entered "{cnt+1}" but you entered "{num}"'
            else if mesg.author.id == auth.id:
                return f'You can\'t submit a number twice in a row! Shame on you {mesg.author.mention}!'
            else:
                #save num
                await client.add_reaction(mesg, '\U0001f44c')
        except ValueError:
            return 'Entered number was not valid.'

Prompt: Add framework for counting command


"""
Export course metadata for all courses
"""

from django.core.management.base import BaseCommand

from xmodule.modulestore.django import modulestore

from cms.djangoapps.export_course_metadata.signals import export_course_metadata
from cms.djangoapps.export_course_metadata.tasks import export_course_metadata_task

class Command(BaseCommand):
    """
    Export course metadata for all courses
    """
    help = 'Export course metadata for all courses'

    def handle(self, *args, **options):
        """
        Execute the command
        """
        export_course_metadata_for_all_courses()

def export_course_metadata_for_all_courses():
    """
    Export course metadata for all courses
    """
    courses = modulestore().get_course_summaries()
    for course in courses:
        export_course_metadata_task.delay(str(course.id))

Response:
"""
Export course metadata for all courses
"""

from django.core.management.base import BaseCommand

from xmodule.modulestore.django import modulestore

from cms.djangoapps.export_course_metadata.signals import export_course_metadata
from cms.djangoapps.export_course_metadata.tasks import export_course_metadata_task

class Command(BaseCommand):
    """
    Export course metadata for all courses
    """
    help = 'Export course metadata for all courses'

    def handle(self, *args, **options):
        """
        Execute the command
        """
        export_course_metadata_for_all_courses()

def export_course_metadata_for_all_courses():
    """
    Export course metadata for all courses
    """
    courses = modulestore().get_course_summaries()
    for course in courses:
        export_course_metadata_task.delay(str(course.id))

Prompt:
Change how we get course ids to avoid memory issues

"""
Export course metadata for all courses
"""

from django.core.management.base import BaseCommand

from xmodule.modulestore.django import modulestore

from cms.djangoapps.export_course_metadata.signals import export_course_metadata
from cms.djangoapps.export_course_metadata.tasks import export_course_metadata_task

class Command(BaseCommand):
    """
    Export course metadata for all courses
    """
    help = 'Export course metadata for all courses'

    def handle(self, *args, **options):
        """
        Execute the command
        """
        export_course_metadata_for_all_courses()

def export_course_metadata_for_all_courses():
    """
    Export course metadata for all courses
    """
    module_store = modulestore()
    courses = module_store.get_courses()
    for course in courses:
        export_course_metadata_task.delay(str(course.id))


#!/usr/bin/env python
"""
:Author Patrik Valkovic
:Created 23.06.2017 16:39
:Licence GNUv3
Part of grammpy

"""

Create file for Nonterminal have method

Create file for Nonterminal have method

Response:
#!/usr/bin/env python
"""
:Author Patrik Valkovic
:Created 23.06.2017 16:39
:Licence GNUv3
Part of grammpy

"""

Prompt: Create file for Nonterminal have method


# -*- coding: utf-8 -*-
from django.shortcuts import render

# Create your views here.

from rest_framework import viewsets
from rest_framework import filters

from porchlightapi.models import Repository, ValueDataPoint
from porchlightapi.serializers import RepositorySerializer, ValueDataPointSerializer

class RepositoryViewSet(viewsets.ReadOnlyModelViewSet):
    """
    A REST view of repositories. Provides GET listing of Repositories,
    filtering on name and project, and individual repsositories on id.
    """
    queryset = Repository.objects.all()
    serializer_class = RepositorySerializer
    filter_backends = (filters.SearchFilter,)
    search_fields = ('name', 'project', 'url')

class ValueDataPointViewSet(viewsets.ReadOnlyModelViewSet):
    """
    A REST view of data points.
    """
    queryset = ValueDataPoint.objects.all()
    serializer_class = ValueDataPointSerializer

Use DRF's built-in search filter

Use DRF's built-in search filter

Response:
# -*- coding: utf-8 -*-
from django.shortcuts import render

# Create your views here.

from rest_framework import viewsets
from rest_framework import filters

from porchlightapi.models import Repository, ValueDataPoint
from porchlightapi.serializers import RepositorySerializer, ValueDataPointSerializer

class RepositoryViewSet(viewsets.ReadOnlyModelViewSet):
    """
    A REST view of repositories. Provides GET listing of Repositories,
    filtering on name and project, and individual repsositories on id.
    """
    queryset = Repository.objects.all()
    serializer_class = RepositorySerializer
    filter_backends = (filters.SearchFilter,)
    search_fields = ('name', 'project', 'url')

class ValueDataPointViewSet(viewsets.ReadOnlyModelViewSet):
    """
    A REST view of data points.
    """
    queryset = ValueDataPoint.objects.all()
    serializer_class = ValueDataPointSerializer

Prompt:
Use DRF's built-in search filter

# -*- coding: utf-8 -*-
from django.shortcuts import render

# Create your views here.

import django_filters

from rest_framework import viewsets
from rest_framework import filters

from porchlightapi.models import Repository, ValueDataPoint
from porchlightapi.serializers import RepositorySerializer, ValueDataPointSerializer

class RepositoryFilter(django_filters.FilterSet):
    """
    Provide filtering of repository objects based on name or project.
    This is 'icontains' filtering, so a repo with the name "Porchlight"
    will match 'por', 'Por', etc.
    """
    name = django_filters.CharFilter(name="name", lookup_type='icontains')
    project = django_filters.CharFilter(name="project", lookup_type='icontains')
    class Meta:
        model = Repository
        fields = ['name', 'project',]

class RepositoryViewSet(viewsets.ReadOnlyModelViewSet):
    """
    A REST view of repositories. Provides GET listing of Repositories,
    filtering on name and project, and individual repsositories on id.
    """
    queryset = Repository.objects.all()
    serializer_class = RepositorySerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_class = RepositoryFilter
    # filter_fields = ('name', 'project')

class ValueDataPointViewSet(viewsets.ReadOnlyModelViewSet):
    """
    A REST view of data points.
    """
    queryset = ValueDataPoint.objects.all()
    serializer_class = ValueDataPointSerializer


from django.db import models
from django.db.models import Max
from django.utils.translation import pgettext_lazy
from mptt.managers import TreeManager
from mptt.models import MPTTModel

class Menu(models.Model):
    slug = models.SlugField(max_length=50)

    class Meta:
        permissions = (
            ('view_menu',
             pgettext_lazy('Permission description', 'Can view menus')),
            ('edit_menu',
             pgettext_lazy('Permission description', 'Can edit menus')))

    def __str__(self):
        return self.slug

class MenuItem(MPTTModel):
    menu = models.ForeignKey(
        Menu, related_name='items', on_delete=models.CASCADE)
    name = models.CharField(max_length=128)
    sort_order = models.PositiveIntegerField(editable=False)
    url = models.URLField(max_length=256)
    parent = models.ForeignKey(
        'self', null=True, blank=True, related_name='children',
        on_delete=models.CASCADE)

    objects = models.Manager()
    tree = TreeManager()

    class Meta:
        ordering = ('sort_order',)
        app_label = 'menu'

    def __str__(self):
        return self.name

    def get_ordering_queryset(self):
        return (
            self.menu.items.all() if not self.parent
            else self.parent.children.all())

    def save(self, *args, **kwargs):
        if self.sort_order is None:
            qs = self.get_ordering_queryset()
            existing_max = qs.aggregate(Max('sort_order'))
            existing_max = existing_max.get('sort_order__max')
            self.sort_order = 0 if existing_max is None else existing_max + 1
        super().save(*args, **kwargs)

Save sorting order on MenuItem

Save sorting order on MenuItem

Response:
from django.db import models
from django.db.models import Max
from django.utils.translation import pgettext_lazy
from mptt.managers import TreeManager
from mptt.models import MPTTModel

class Menu(models.Model):
    slug = models.SlugField(max_length=50)

    class Meta:
        permissions = (
            ('view_menu',
             pgettext_lazy('Permission description', 'Can view menus')),
            ('edit_menu',
             pgettext_lazy('Permission description', 'Can edit menus')))

    def __str__(self):
        return self.slug

class MenuItem(MPTTModel):
    menu = models.ForeignKey(
        Menu, related_name='items', on_delete=models.CASCADE)
    name = models.CharField(max_length=128)
    sort_order = models.PositiveIntegerField(editable=False)
    url = models.URLField(max_length=256)
    parent = models.ForeignKey(
        'self', null=True, blank=True, related_name='children',
        on_delete=models.CASCADE)

    objects = models.Manager()
    tree = TreeManager()

    class Meta:
        ordering = ('sort_order',)
        app_label = 'menu'

    def __str__(self):
        return self.name

    def get_ordering_queryset(self):
        return (
            self.menu.items.all() if not self.parent
            else self.parent.children.all())

    def save(self, *args, **kwargs):
        if self.sort_order is None:
            qs = self.get_ordering_queryset()
            existing_max = qs.aggregate(Max('sort_order'))
            existing_max = existing_max.get('sort_order__max')
            self.sort_order = 0 if existing_max is None else existing_max + 1
        super().save(*args, **kwargs)

Prompt:
Save sorting order on MenuItem

from django.db import models
from django.utils.translation import pgettext_lazy
from mptt.managers import TreeManager
from mptt.models import MPTTModel

class Menu(models.Model):
    slug = models.SlugField(max_length=50)

    class Meta:
        permissions = (
            ('view_menu',
             pgettext_lazy('Permission description', 'Can view menus')),
            ('edit_menu',
             pgettext_lazy('Permission description', 'Can edit menus')))

    def __str__(self):
        return self.slug

class MenuItem(MPTTModel):
    menu = models.ForeignKey(
        Menu, related_name='items', on_delete=models.CASCADE)
    name = models.CharField(max_length=128)
    sort_order = models.PositiveIntegerField(editable=False)
    url = models.URLField(max_length=256)
    parent = models.ForeignKey(
        'self', null=True, blank=True, related_name='children',
        on_delete=models.CASCADE)

    objects = models.Manager()
    tree = TreeManager()

    class Meta:
        ordering = ('sort_order',)
        app_label = 'menu'

    def __str__(self):
        return self.name


from ..Module import Module
from ..TagData import TagData

class rgp(Module):
    opmap = {
        'infData':      'descend',
    }

    def parse_rgpStatus(self, response, tag):
        status = tag.attrib['s']
        response.set(status, tag.text)

    def render_default(self, request, data):
        ext = self.render_extension(request, 'update')
        request.add_subtag(ext, 'rgp:restore', { 'op': 'request'})

    def render_request(self, request, data):
        return self.render_default(request, data)

    def render_report(self, request, data):
        ext = self.render_extension(request, 'update')
        restore = request.add_subtag(ext, 'rgp:restore', { 'op': 'report'})
        report = request.add_subtag(restore, 'rgp:report')
        request.add_subtag(report, 'rgp:preData', {}, data.get('preData'))
        request.add_subtag(report, 'rgp:postData', {}, data.get('postData'))
        request.add_subtag(report, 'rgp:delTime', {}, data.get('delTime'))
        request.add_subtag(report, 'rgp:resTime', {}, data.get('resTime'))
        request.add_subtag(report, 'rgp:resReason', {}, data.get('resReason', 'Registrant error'))
        request.add_subtag(report, 'rgp:statement', {}, data.get('statement', 'This registrar has not restored the Registered Name in order to assume the rights to use or sell the Registered Name for itself or for any third party'))
        request.add_subtag(report, 'rgp:statement', {}, data.get('statement', 'The information in this report is true to best of this registrar\'s knowledge, and this registrar acknowledges that intentionally supplying false information in this report shall constitute an incurable material breach of the Registry-Registrar Agreement'))

Add RGP request and report

Add RGP request and report

Response:
from ..Module import Module
from ..TagData import TagData

class rgp(Module):
    opmap = {
        'infData':      'descend',
    }

    def parse_rgpStatus(self, response, tag):
        status = tag.attrib['s']
        response.set(status, tag.text)

    def render_default(self, request, data):
        ext = self.render_extension(request, 'update')
        request.add_subtag(ext, 'rgp:restore', { 'op': 'request'})

    def render_request(self, request, data):
        return self.render_default(request, data)

    def render_report(self, request, data):
        ext = self.render_extension(request, 'update')
        restore = request.add_subtag(ext, 'rgp:restore', { 'op': 'report'})
        report = request.add_subtag(restore, 'rgp:report')
        request.add_subtag(report, 'rgp:preData', {}, data.get('preData'))
        request.add_subtag(report, 'rgp:postData', {}, data.get('postData'))
        request.add_subtag(report, 'rgp:delTime', {}, data.get('delTime'))
        request.add_subtag(report, 'rgp:resTime', {}, data.get('resTime'))
        request.add_subtag(report, 'rgp:resReason', {}, data.get('resReason', 'Registrant error'))
        request.add_subtag(report, 'rgp:statement', {}, data.get('statement', 'This registrar has not restored the Registered Name in order to assume the rights to use or sell the Registered Name for itself or for any third party'))
        request.add_subtag(report, 'rgp:statement', {}, data.get('statement', 'The information in this report is true to best of this registrar\'s knowledge, and this registrar acknowledges that intentionally supplying false information in this report shall constitute an incurable material breach of the Registry-Registrar Agreement'))

Prompt:
Add RGP request and report

from ..Module import Module
from ..TagData import TagData

class rgp(Module):
    opmap = {
        'infData':      'descend',
    }

    def parse_rgpStatus(self, response, tag):
        status = tag.attrib['s']
        response.set(status, tag.text)

    def render_default(self, request, data):
        ext = self.render_extension(request, 'update')
        request.add_subtag(ext, 'rgp:restore', { 'op': 'request'})


"""
Various parser-related exceptions.
"""
from emds.exceptions import EMDSError

class ParseError(EMDSError):
    """
    Raise this when some unrecoverable error happens while parsing serialized
    market data.
    """
    pass

Make ParseError a child of EMDSError.

Make ParseError a child of EMDSError.

Response:
"""
Various parser-related exceptions.
"""
from emds.exceptions import EMDSError

class ParseError(EMDSError):
    """
    Raise this when some unrecoverable error happens while parsing serialized
    market data.
    """
    pass

Prompt:
Make ParseError a child of EMDSError.

"""
Various parser-related exceptions.
"""

class ParseError(Exception):
    """
    Raise this when some unrecoverable error happens while parsing serialized
    market data.
    """
    pass


import git
import os

from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render

class SaveContentMixin():

    """Save exam or question content to a git repository and to a database."""

#    object = None
#    request = None
#    template_name = None

    def write_content(self, directory, form, inlines=None):
        try:
            repo = git.Repo(settings.GLOBAL_SETTINGS['REPO_PATH'])
            os.environ['GIT_AUTHOR_NAME'] = 'Numbas'
            os.environ['GIT_AUTHOR_EMAIL'] = 'numbas@ncl.ac.uk'
            path_to_file = os.path.join(settings.GLOBAL_SETTINGS['REPO_PATH'],
                                        directory, self.object.filename)
            fh = open(path_to_file, 'w')
            fh.write(self.object.content)
            fh.close()
            repo.index.add([os.path.join(directory, self.object.filename)])
            repo.index.commit('Made some changes to %s' % self.object.name)
        except IOError:
            error = "Could not save file."
            return render(self.request, self.template_name,
                          {'form': form, 'inlines': inlines, 'error': error,
                           'object': self.object})
        self.object = form.save()
        if inlines is not None:
            for formset in inlines:
                formset.save()
        return HttpResponseRedirect(self.get_success_url())

Set the git author name and e-mail

Set the git author name and e-mail

Response:
import git
import os

from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render

class SaveContentMixin():

    """Save exam or question content to a git repository and to a database."""

#    object = None
#    request = None
#    template_name = None

    def write_content(self, directory, form, inlines=None):
        try:
            repo = git.Repo(settings.GLOBAL_SETTINGS['REPO_PATH'])
            os.environ['GIT_AUTHOR_NAME'] = 'Numbas'
            os.environ['GIT_AUTHOR_EMAIL'] = 'numbas@ncl.ac.uk'
            path_to_file = os.path.join(settings.GLOBAL_SETTINGS['REPO_PATH'],
                                        directory, self.object.filename)
            fh = open(path_to_file, 'w')
            fh.write(self.object.content)
            fh.close()
            repo.index.add([os.path.join(directory, self.object.filename)])
            repo.index.commit('Made some changes to %s' % self.object.name)
        except IOError:
            error = "Could not save file."
            return render(self.request, self.template_name,
                          {'form': form, 'inlines': inlines, 'error': error,
                           'object': self.object})
        self.object = form.save()
        if inlines is not None:
            for formset in inlines:
                formset.save()
        return HttpResponseRedirect(self.get_success_url())

Prompt:
Set the git author name and e-mail

import git
import os

from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render

class SaveContentMixin():

    """Save exam or question content to a git repository and to a database."""

#    object = None
#    request = None
#    template_name = None

    def write_content(self, directory, form, inlines=None):
        try:
            repo = git.Repo(settings.GLOBAL_SETTINGS['REPO_PATH'])
            path_to_file = os.path.join(settings.GLOBAL_SETTINGS['REPO_PATH'],
                                        directory, self.object.filename)
            fh = open(path_to_file, 'w')
            fh.write(self.object.content)
            fh.close()
            repo.index.add([os.path.join(directory, self.object.filename)])
            repo.index.commit('Made some changes to %s' % self.object.name)
        except IOError:
            error = "Could not save file."
            return render(self.request, self.template_name,
                          {'form': form, 'inlines': inlines, 'error': error,
                           'object': self.object})
        self.object = form.save()
        if inlines is not None:
            for formset in inlines:
                formset.save()
        return HttpResponseRedirect(self.get_success_url())


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

from feedback.forms import FeedbackForm

def leave_feedback(request, template_name='feedback/feedback_form.html'):
    form = FeedbackForm(request.POST or None)
    if form.is_valid():
        feedback = form.save(commit=False)
        feedback.user = request.user
        feedback.save()
        request.user.message_set.create(message="Your feedback has been saved successfully.")
        return HttpResponseRedirect(request.POST.get('next', request.META.get('HTTP_REFERER', '/')))
    return render_to_response(template_name, {'form': form}, context_instance=RequestContext(request))

Allow passing of template_name to view

Allow passing of template_name to view

Response:
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext

from feedback.forms import FeedbackForm

def leave_feedback(request, template_name='feedback/feedback_form.html'):
    form = FeedbackForm(request.POST or None)
    if form.is_valid():
        feedback = form.save(commit=False)
        feedback.user = request.user
        feedback.save()
        request.user.message_set.create(message="Your feedback has been saved successfully.")
        return HttpResponseRedirect(request.POST.get('next', request.META.get('HTTP_REFERER', '/')))
    return render_to_response(template_name, {'form': form}, context_instance=RequestContext(request))

Prompt:
Allow passing of template_name to view

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

from feedback.forms import FeedbackForm

def leave_feedback(request):
    form = FeedbackForm(request.POST or None)
    if form.is_valid():
        feedback = form.save(commit=False)
        feedback.user = request.user
        feedback.save()
        request.user.message_set.create(message="Your feedback has been saved successfully.")
        return HttpResponseRedirect(request.POST.get('next', request.META.get('HTTP_REFERER', '/')))
    return render_to_response('feedback/feedback_form.html', {'form': form}, context_instance=RequestContext(request))