"""A set of Resource objects used for Capability List
Indexes and ResourceSync Description documents.

Ordinging is currently alphanumeric (using sorted(..)) on the
uri which is the key.
"""

class ResourceSet(dict):
    """Implementation of class to store resources in Capability List
    Indexes and ResourceSync Description documents.

    Key properties of this class are:
    - has add(resource) method
    - is iterable and results given in alphanumeric order by resource.uri
    """

    def __iter__(self):
        """Iterator over all the resources in this resource_list"""
        self._iter_next_list = sorted(self.keys())
        self._iter_next_list.reverse()
        return(iter(self._iter_next, None))

    def _iter_next(self):
        if (len(self._iter_next_list)>0):
            return(self[self._iter_next_list.pop()])
        else:
            return(None)

    def add(self, resource, replace=False):
        """Add just a single resource"""
        uri = resource.uri
        if (uri in self and not replace):
            raise ResourceSetDupeError("Attempt to add resource already in this set")
        self[uri]=resource

class ResourceSetDupeError(Exception):
    pass

Response:
"""A set of Resource objects used for Capability List
Indexes and ResourceSync Description documents.

Ordinging is currently alphanumeric (using sorted(..)) on the
uri which is the key.
"""

class ResourceSet(dict):
    """Implementation of class to store resources in Capability List
    Indexes and ResourceSync Description documents.

    Key properties of this class are:
    - has add(resource) method
    - is iterable and results given in alphanumeric order by resource.uri
    """

    def __iter__(self):
        """Iterator over all the resources in this resource_list"""
        self._iter_next_list = sorted(self.keys())
        self._iter_next_list.reverse()
        return(iter(self._iter_next, None))

    def _iter_next(self):
        if (len(self._iter_next_list)>0):
            return(self[self._iter_next_list.pop()])
        else:
            return(None)

    def add(self, resource, replace=False):
        """Add just a single resource"""
        uri = resource.uri
        if (uri in self and not replace):
            raise ResourceSetDupeError("Attempt to add resource already in this set")
        self[uri]=resource

class ResourceSetDupeError(Exception):
    pass

Prompt:
Change comment to indicate choice of alphanum order by uri

"""A set of Resource objects used for Capability List
Indexes and ResourceSync Description documents.

FIXME - what should the ordering be?
"""

class ResourceSet(dict):
    """Implementation of class to store resources in Capability List
    Indexes and ResourceSync Description documents.

    Key properties of this class are:
    - has add(resource) method
    - is iterable and results given in alphanumeric order by resource.uri
    """

    def __iter__(self):
        """Iterator over all the resources in this resource_list"""
        self._iter_next_list = sorted(self.keys())
        self._iter_next_list.reverse()
        return(iter(self._iter_next, None))

    def _iter_next(self):
        if (len(self._iter_next_list)>0):
            return(self[self._iter_next_list.pop()])
        else:
            return(None)

    def add(self, resource, replace=False):
        """Add just a single resource"""
        uri = resource.uri
        if (uri in self and not replace):
            raise ResourceSetDupeError("Attempt to add resource already in this set")
        self[uri]=resource

class ResourceSetDupeError(Exception):
    pass


from __future__ import absolute_import

import itertools
from hashlib import md5

from rest_framework.response import Response

from sentry import status_checks
from sentry.api.base import Endpoint
from sentry.api.permissions import SuperuserPermission

class SystemHealthEndpoint(Endpoint):
    permission_classes = (SuperuserPermission,)

    def get(self, request):
        results = status_checks.check_all()
        return Response({
            'problems': map(
                lambda problem: {
                    'id': md5(problem.message).hexdigest(),
                    'message': problem.message,
                    'severity': problem.severity,
                    'url': problem.url,
                },
                sorted(itertools.chain.from_iterable(results.values()), reverse=True),
            ),
            'healthy': {type(check).__name__: not problems for check, problems in results.items()},
        })

Response:
from __future__ import absolute_import

import itertools
from hashlib import md5

from rest_framework.response import Response

from sentry import status_checks
from sentry.api.base import Endpoint
from sentry.api.permissions import SuperuserPermission

class SystemHealthEndpoint(Endpoint):
    permission_classes = (SuperuserPermission,)

    def get(self, request):
        results = status_checks.check_all()
        return Response({
            'problems': map(
                lambda problem: {
                    'id': md5(problem.message).hexdigest(),
                    'message': problem.message,
                    'severity': problem.severity,
                    'url': problem.url,
                },
                sorted(itertools.chain.from_iterable(results.values()), reverse=True),
            ),
            'healthy': {type(check).__name__: not problems for check, problems in results.items()},
        })

Prompt:
Add `id` to `SystemHealthEndpoint` response.

from __future__ import absolute_import

import itertools

from rest_framework.response import Response

from sentry import status_checks
from sentry.api.base import Endpoint
from sentry.api.permissions import SuperuserPermission

class SystemHealthEndpoint(Endpoint):
    permission_classes = (SuperuserPermission,)

    def get(self, request):
        results = status_checks.check_all()
        return Response({
            'problems': map(
                lambda problem: {
                    'message': problem.message,
                    'severity': problem.severity,
                    'url': problem.url,
                },
                sorted(itertools.chain.from_iterable(results.values()), reverse=True),
            ),
            'healthy': {type(check).__name__: not problems for check, problems in results.items()},
        })


from myfedora.widgets.resourceview import ToolWidget
from fedora.client import BaseClient
from myfedora.lib.app_factory import AppFactory

class ProfileInfoToolApp(AppFactory):
    entry_name = "tools.profileinfo"

class ProfileInfoToolWidget(ToolWidget):
    template = 'genshi:myfedora.plugins.apps.tools.templates.profileinfo'
    display_name = "Info"

    def update_params(self, d):
        super(ProfileInfoToolWidget, self).update_params(d)

        return d

Response:
from myfedora.widgets.resourceview import ToolWidget
from fedora.client import BaseClient
from myfedora.lib.app_factory import AppFactory

class ProfileInfoToolApp(AppFactory):
    entry_name = "tools.profileinfo"

class ProfileInfoToolWidget(ToolWidget):
    template = 'genshi:myfedora.plugins.apps.tools.templates.profileinfo'
    display_name = "Info"

    def update_params(self, d):
        super(ProfileInfoToolWidget, self).update_params(d)

        return d

Prompt:
Fix a deprecation warning from python-fedora

from myfedora.widgets.resourceview import ToolWidget
from fedora.tg.client import BaseClient
from myfedora.lib.app_factory import AppFactory

class ProfileInfoToolApp(AppFactory):
    entry_name = "tools.profileinfo"

class ProfileInfoToolWidget(ToolWidget):
    template = 'genshi:myfedora.plugins.apps.tools.templates.profileinfo'
    display_name = "Info"

    def update_params(self, d):
        super(ProfileInfoToolWidget, self).update_params(d)

        return d


from flask import Flask, render_template
import os

app = Flask(__name__)

@app.route('/')
def webprint():
    return 'Hello world!'

if __name__ == "__main__":
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

Return text message on /

Return text message on /

Response:
from flask import Flask, render_template
import os

app = Flask(__name__)

@app.route('/')
def webprint():
    return 'Hello world!'

if __name__ == "__main__":
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

Prompt:
Return text message on /

from flask import Flask, render_template
import os

app = Flask(__name__)

@app.route('/')
def webprint():
    return(render_template('index.html'))

if __name__ == "__main__":
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)


# Copyright (C) 2020 David BEAL @ Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.tests.common import TransactionCase

class MulticompanyTests(TransactionCase):
    def setUp(self):
        super().setUp()

    def test_classif_in_multicompany(self):
        prd = self.env.ref(
            "account_product_fiscal_classification.product_template_all_cpnies"
        )
        self.env.user.write({"company_id": self.env.ref("base.main_company").id})
        classif1 = self.env.ref(
            "account_product_fiscal_classification.global_fiscal_classification_1"
        )
        classif2 = self.env.ref(
            "account_product_fiscal_classification.global_fiscal_classification_2"
        )

        def check_sale_purchase_tax(classif, write_classif=True):
            if write_classif:
                prd.write({"fiscal_classification_id": classif.id})
            self.assertEqual(prd.sudo().taxes_id, classif.sudo().sale_tax_ids)
            self.assertEqual(
                prd.sudo().supplier_taxes_id, classif.sudo().purchase_tax_ids
            )

        # test write with 2 classifications on the same company
        check_sale_purchase_tax(classif1)
        check_sale_purchase_tax(classif2)
        # test the same data but from another company point of view
        self.env.user.write(
            {
                "company_id": self.env.ref(
                    "account_product_fiscal_classification."
                    "cpny_onlyshare_classification"
                ).id
            }
        )
        # check with same data
        check_sale_purchase_tax(classif2, write_classif=False)
        check_sale_purchase_tax(classif1)

FIX prd_classif: set taxes on all companies sharing classif

Response:
# Copyright (C) 2020 David BEAL @ Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.tests.common import TransactionCase

class MulticompanyTests(TransactionCase):
    def setUp(self):
        super().setUp()

    def test_classif_in_multicompany(self):
        prd = self.env.ref(
            "account_product_fiscal_classification.product_template_all_cpnies"
        )
        self.env.user.write({"company_id": self.env.ref("base.main_company").id})
        classif1 = self.env.ref(
            "account_product_fiscal_classification.global_fiscal_classification_1"
        )
        classif2 = self.env.ref(
            "account_product_fiscal_classification.global_fiscal_classification_2"
        )

        def check_sale_purchase_tax(classif, write_classif=True):
            if write_classif:
                prd.write({"fiscal_classification_id": classif.id})
            self.assertEqual(prd.sudo().taxes_id, classif.sudo().sale_tax_ids)
            self.assertEqual(
                prd.sudo().supplier_taxes_id, classif.sudo().purchase_tax_ids
            )

        # test write with 2 classifications on the same company
        check_sale_purchase_tax(classif1)
        check_sale_purchase_tax(classif2)
        # test the same data but from another company point of view
        self.env.user.write(
            {
                "company_id": self.env.ref(
                    "account_product_fiscal_classification."
                    "cpny_onlyshare_classification"
                ).id
            }
        )
        # check with same data
        check_sale_purchase_tax(classif2, write_classif=False)
        check_sale_purchase_tax(classif1)

Prompt: FIX prd_classif: set taxes on all companies sharing classif


# Empty test file just for coverage / syntax-checking purposes
# flake8: noqa

import nbdiff.server.remote_server
import nbdiff.server.database

import nbdiff.server.command.AboutUsCommand
import nbdiff.server.command.ComparisonCommand
import nbdiff.server.command.ContactUsCommand
import nbdiff.server.command.DiffCommand
import nbdiff.server.command.DiffURLCommand
import nbdiff.server.command.FaqCommand
import nbdiff.server.command.MergeCommand
import nbdiff.server.command.MergeURLCommand
import nbdiff.server.command.NotebookRequestCommand
import nbdiff.server.command.ResourceRequestCommand
import nbdiff.server.command.SaveNotebookCommand
import nbdiff.server.command.UploadCommand

Add empty test file that imports all remote command files for syntax-checking/coverage reporting.

Response:
# Empty test file just for coverage / syntax-checking purposes
# flake8: noqa

import nbdiff.server.remote_server
import nbdiff.server.database

import nbdiff.server.command.AboutUsCommand
import nbdiff.server.command.ComparisonCommand
import nbdiff.server.command.ContactUsCommand
import nbdiff.server.command.DiffCommand
import nbdiff.server.command.DiffURLCommand
import nbdiff.server.command.FaqCommand
import nbdiff.server.command.MergeCommand
import nbdiff.server.command.MergeURLCommand
import nbdiff.server.command.NotebookRequestCommand
import nbdiff.server.command.ResourceRequestCommand
import nbdiff.server.command.SaveNotebookCommand
import nbdiff.server.command.UploadCommand

Prompt: Add empty test file that imports all remote command files for syntax-checking/coverage reporting.


def copy_files(src, target_node, parent=None, name=None):
    """Copy the files from src to the target node
    :param Folder src: The source to copy children from
    :param Node target_node: The node to copy files to
    :param Folder parent: The parent of to attach the clone of src to, if applicable
    """
    assert not parent or not parent.is_file, 'Parent must be a folder'

    cloned = src.clone()
    cloned.parent = parent
    cloned.target = target_node
    cloned.name = name or cloned.name
    cloned.copied_from = src

    cloned.save()

    if src.is_file and src.versions.exists():
        fileversions = src.versions.select_related('region').order_by('-created')
        most_recent_fileversion = fileversions.first()
        if most_recent_fileversion.region != target_node.osfstorage_region:
            # add all original version except the most recent
            cloned.versions.add(*fileversions[1:])
            # create a new most recent version and update the region before adding
            new_fileversion = most_recent_fileversion.clone()
            new_fileversion.region = target_node.osfstorage_region
            new_fileversion.save()
            cloned.versions.add(new_fileversion)
        else:
            cloned.versions.add(*src.versions.all())
    if not src.is_file:
        for child in src.children:
            copy_files(child, target_node, parent=cloned)

    return cloned

Response:
def copy_files(src, target_node, parent=None, name=None):
    """Copy the files from src to the target node
    :param Folder src: The source to copy children from
    :param Node target_node: The node to copy files to
    :param Folder parent: The parent of to attach the clone of src to, if applicable
    """
    assert not parent or not parent.is_file, 'Parent must be a folder'

    cloned = src.clone()
    cloned.parent = parent
    cloned.target = target_node
    cloned.name = name or cloned.name
    cloned.copied_from = src

    cloned.save()

    if src.is_file and src.versions.exists():
        fileversions = src.versions.select_related('region').order_by('-created')
        most_recent_fileversion = fileversions.first()
        if most_recent_fileversion.region != target_node.osfstorage_region:
            # add all original version except the most recent
            cloned.versions.add(*fileversions[1:])
            # create a new most recent version and update the region before adding
            new_fileversion = most_recent_fileversion.clone()
            new_fileversion.region = target_node.osfstorage_region
            new_fileversion.save()
            cloned.versions.add(new_fileversion)
        else:
            cloned.versions.add(*src.versions.all())
    if not src.is_file:
        for child in src.children:
            copy_files(child, target_node, parent=cloned)

    return cloned

Prompt:
Use clone() method from BaseModel to copy most recent fileversion

from osf.models.base import generate_object_id

def copy_files(src, target_node, parent=None, name=None):
    """Copy the files from src to the target node
    :param Folder src: The source to copy children from
    :param Node target_node: The node to copy files to
    :param Folder parent: The parent of to attach the clone of src to, if applicable
    """
    assert not parent or not parent.is_file, 'Parent must be a folder'

    cloned = src.clone()
    cloned.parent = parent
    cloned.target = target_node
    cloned.name = name or cloned.name
    cloned.copied_from = src

    cloned.save()

    if src.is_file and src.versions.exists():
        fileversions = src.versions.select_related('region').order_by('-created')
        most_recent_fileversion = fileversions.first()
        if most_recent_fileversion.region != target_node.osfstorage_region:
            # add all original version except the most recent
            cloned.versions.add(*fileversions[1:])
            # setting the id to None and calling save generates a new object
            most_recent_fileversion.id = None
            most_recent_fileversion._id = generate_object_id()
            most_recent_fileversion.region = target_node.osfstorage_region
            most_recent_fileversion.save()
            cloned.versions.add(most_recent_fileversion)
        else:
            cloned.versions.add(*src.versions.all())
    if not src.is_file:
        for child in src.children:
            copy_files(child, target_node, parent=cloned)

    return cloned


from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType

from django.http import HttpResponse
from django.utils import simplejson as json
from django.shortcuts import get_object_or_404, redirect
from django.views.decorators.http import require_POST

from phileo.models import Like
from phileo.signals import object_liked, object_unliked

@require_POST
@login_required
def like_toggle(request, content_type_id, object_id):
    content_type = get_object_or_404(ContentType, pk=content_type_id)

    like, created = Like.objects.get_or_create(
        sender = request.user,
        receiver_content_type = content_type,
        receiver_object_id = object_id
    )

    if created:
        object_liked.send(sender=Like, like=like)
    else:
        like.delete()
        object_unliked.send(
            sender=Like,
            object=content_type.get_object_for_this_type(
                pk=object_id
            )
        )

    if request.is_ajax():
        return HttpResponse(json.dumps({
            "likes_count": Like.objects.filter(
                receiver_content_type = content_type,
                receiver_object_id = object_id
            ).count()
        }), mimetype="application/json")

    return redirect(request.META["HTTP_REFERER"])

Response:
from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType

from django.http import HttpResponse
from django.utils import simplejson as json
from django.shortcuts import get_object_or_404, redirect
from django.views.decorators.http import require_POST

from phileo.models import Like
from phileo.signals import object_liked, object_unliked

@require_POST
@login_required
def like_toggle(request, content_type_id, object_id):
    content_type = get_object_or_404(ContentType, pk=content_type_id)

    like, created = Like.objects.get_or_create(
        sender = request.user,
        receiver_content_type = content_type,
        receiver_object_id = object_id
    )

    if created:
        object_liked.send(sender=Like, like=like)
    else:
        like.delete()
        object_unliked.send(
            sender=Like,
            object=content_type.get_object_for_this_type(
                pk=object_id
            )
        )

    if request.is_ajax():
        return HttpResponse(json.dumps({
            "likes_count": Like.objects.filter(
                receiver_content_type = content_type,
                receiver_object_id = object_id
            ).count()
        }), mimetype="application/json")

    return redirect(request.META["HTTP_REFERER"])

Prompt:
Remove user from count query to show likes count for all users for obj

from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType

from django.http import HttpResponse
from django.utils import simplejson as json
from django.shortcuts import get_object_or_404, redirect
from django.views.decorators.http import require_POST

from phileo.models import Like
from phileo.signals import object_liked, object_unliked

@require_POST
@login_required
def like_toggle(request, content_type_id, object_id):
    content_type = get_object_or_404(ContentType, pk=content_type_id)

    like, created = Like.objects.get_or_create(
        sender = request.user,
        receiver_content_type = content_type,
        receiver_object_id = object_id
    )

    if created:
        object_liked.send(sender=Like, like=like)
    else:
        like.delete()
        object_unliked.send(
            sender=Like,
            object=content_type.get_object_for_this_type(
                pk=object_id
            )
        )

    if request.is_ajax():
        return HttpResponse(json.dumps({
            "likes_count": Like.objects.filter(
                sender = request.user,
                receiver_content_type = content_type,
                receiver_object_id = object_id
            ).count()
        }), mimetype="application/json")

    return redirect(request.META["HTTP_REFERER"])


'''
This script showing you how to use a sliding window
'''

from itertools import islice
def sliding_window(a, n, step):
    '''
    a - sequence
    n - width of the window
    step - window step
    '''
    z = (islice(a, i, None, step) for i in range(n))
    return zip(*z)

##Example
sliding_window(range(10), 2, 1)

Add a script to do the sliding window efficiently

Response:
'''
This script showing you how to use a sliding window
'''

from itertools import islice
def sliding_window(a, n, step):
    '''
    a - sequence
    n - width of the window
    step - window step
    '''
    z = (islice(a, i, None, step) for i in range(n))
    return zip(*z)

##Example
sliding_window(range(10), 2, 1)

Prompt: Add a script to do the sliding window efficiently


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

from django.db import models
from django_countries.fields import CountryField

class GuitarSociety(models.Model):
    """
    Represents a single guitar society.

    .. versionadded:: 0.1
    """

    #: the name of the society
    #: ..versionadded:: 0.1
    name = models.CharField(max_length=1024)

    #: the society's url
    #: ..versionadded:: 0.1
    link = models.URLField(max_length=255)

    #: The country in which the society resides
    #: .. versionadded:: 0.1
    country = CountryField()

    #: A free form "city" or "region" field used to display where
    #: exactly the society is within a country
    #: .. versionadded:: 0.1
    region = models.CharField(max_length=512, null=True, default=None, blank=True)

    def __str__(self):
        return self.name

    def __repr__(self):
        return 'GuitarSociety("{}")'.format(self.name)

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

from django.db import models
from django_countries.fields import CountryField

class GuitarSociety(models.Model):
    """
    Represents a single guitar society.

    .. versionadded:: 0.1
    """

    #: the name of the society
    #: ..versionadded:: 0.1
    name = models.CharField(max_length=1024)

    #: the society's url
    #: ..versionadded:: 0.1
    link = models.URLField(max_length=255)

    #: The country in which the society resides
    #: .. versionadded:: 0.1
    country = CountryField()

    #: A free form "city" or "region" field used to display where
    #: exactly the society is within a country
    #: .. versionadded:: 0.1
    region = models.CharField(max_length=512, null=True, default=None, blank=True)

    def __str__(self):
        return self.name

    def __repr__(self):
        return 'GuitarSociety("{}")'.format(self.name)

Prompt:
Make the Guitar Society __str__ Method a bit more Logical

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

from django.db import models
from django_countries.fields import CountryField

class GuitarSociety(models.Model):
    """
    Represents a single guitar society.

    .. versionadded:: 0.1
    """

    #: the name of the society
    #: ..versionadded:: 0.1
    name = models.CharField(max_length=1024)

    #: the society's url
    #: ..versionadded:: 0.1
    link = models.URLField(max_length=255)

    #: The country in which the society resides
    #: .. versionadded:: 0.1
    country = CountryField()

    #: A free form "city" or "region" field used to display where
    #: exactly the society is within a country
    #: .. versionadded:: 0.1
    region = models.CharField(max_length=512, null=True, default=None, blank=True)

    def __str__(self):
        return 'GuitarSociety(name="{}", link="{}")'.format(self.name, self.link)


class BaseWrapper(object):
    """
    A base class for backend plugins.
    """

    def reset(self, really):
        """
        Resets the training data to a blank slate.
        """
        if really:
            raise NotImplementedError()

    def get_key(self, tag, key, default=None):
        """
        Gets the value held by the tag, key composite key. If it doesn't exist,
        return default.
        """
        raise NotImplementedError()

    def get_key_list(self, tag, keys, default=None):
        """
        Given a list of key, tag tuples get all values.
        If key, tag doesn't exist, return default.
        Subclasses can override this to make more efficient queries for bulk
        requests.
        """
        return [self.get_key(tag, key, default) for tag, key in key_tag_pairs]

    def set_key_list(self, tag_key_value_tuples):
        """
        Given a list of tuples of tag, key, value set them all.
        Subclasses can override this to make more efficient queries for bulk
        requests.
        """
        return [self.set_key(tag, key, value) for tag, key, value in tag_key_value_tuples]

    def set_key(self, tag, key, value):
        """
        Set the value held by the tag, key composite key.
        """
        raise NotImplementedError()

Add a reset function stub

Response:
class BaseWrapper(object):
    """
    A base class for backend plugins.
    """

    def reset(self, really):
        """
        Resets the training data to a blank slate.
        """
        if really:
            raise NotImplementedError()

    def get_key(self, tag, key, default=None):
        """
        Gets the value held by the tag, key composite key. If it doesn't exist,
        return default.
        """
        raise NotImplementedError()

    def get_key_list(self, tag, keys, default=None):
        """
        Given a list of key, tag tuples get all values.
        If key, tag doesn't exist, return default.
        Subclasses can override this to make more efficient queries for bulk
        requests.
        """
        return [self.get_key(tag, key, default) for tag, key in key_tag_pairs]

    def set_key_list(self, tag_key_value_tuples):
        """
        Given a list of tuples of tag, key, value set them all.
        Subclasses can override this to make more efficient queries for bulk
        requests.
        """
        return [self.set_key(tag, key, value) for tag, key, value in tag_key_value_tuples]

    def set_key(self, tag, key, value):
        """
        Set the value held by the tag, key composite key.
        """
        raise NotImplementedError()

Prompt:
Add a reset function stub

Also fix a typo.

class BaseWrapper(object):
    """
    A base class for backend plugins.
    """

    def get_key(self, tag, key, default=None):
        """
        Gets the value held by the tag, key composite key. If it doesn't exist,
        return default.
        """
        raise NotImplementedError()

    def get_key_list(self, tag, keys, default=None):
        """
        Given a list of key, tag tuples get all values.
        If key, tag doesn't exist, return default.
        Subclasses can override this to make more efficient queries for bulk
        requests.
        """
        return [self.get_key(tag, key, default) for tag, key in key_tag_pairs]

    def set_key_list(self, tag, key_value_tuples):
        """
        Given a list of tuples of tag, key, value set them all.
        Subclasses can override this to make more efficient queries for bulk
        requests.
        """
        return [self.set_key(tag, key, value) for tag, key, value in tag_key_value_tuples]

    def set_key(self, tag, key, value):
        """
        Set the value held by the tag, key composite key.
        """
        raise NotImplementedError()


# coding: utf-8
# Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.

from __future__ import unicode_literals

"""
The util package implements various utilities that are commonly used by various
packages.
"""

__author__ = "Shyue"
__date__ = "$Jun 6, 2011 7:30:05 AM$"

try:
  import coord_utils_cython
except ImportError:
  import coord_utils_python as coord_utils_cython  # use pure-python fallback

Fix spelling in coord_utils import.

Response:
# coding: utf-8
# Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.

from __future__ import unicode_literals

"""
The util package implements various utilities that are commonly used by various
packages.
"""

__author__ = "Shyue"
__date__ = "$Jun 6, 2011 7:30:05 AM$"

try:
  import coord_utils_cython
except ImportError:
  import coord_utils_python as coord_utils_cython  # use pure-python fallback

Prompt:
Fix spelling in coord_utils import.

Former-commit-id: 03182f09c5e7cc2d7a5ca8b996baa82e4557e7fa [formerly 4011394beda266f4f43120e951b99bdc0470f93e]
Former-commit-id: 78985fde51ef15145a5ff42d8bd9fe05a8890b22
# coding: utf-8
# Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.

from __future__ import unicode_literals

"""
The util package implements various utilities that are commonly used by various
packages.
"""

__author__ = "Shyue"
__date__ = "$Jun 6, 2011 7:30:05 AM$"

try:
  import coord_util_cython
except ImportError:
  import coord_util_python as coord_util_cython  # use pure-python fallback


"""engine.SCons.Platform.darwin

Platform-specific initialization for Mac OS X systems.

There normally shouldn't be any need to import this module directly.  It
will usually be imported through the generic SCons.Platform.Platform()
selection method.
"""

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

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import posix
import os

def generate(env):
    posix.generate(env)
    env['SHLIBSUFFIX'] = '.dylib'

Response:
"""engine.SCons.Platform.darwin

Platform-specific initialization for Mac OS X systems.

There normally shouldn't be any need to import this module directly.  It
will usually be imported through the generic SCons.Platform.Platform()
selection method.
"""

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

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import posix
import os

def generate(env):
    posix.generate(env)
    env['SHLIBSUFFIX'] = '.dylib'

Prompt:
Fix __COPYRIGHT__ and __REVISION__ in new Darwin module.

"""engine.SCons.Platform.darwin

Platform-specific initialization for Mac OS X systems.

There normally shouldn't be any need to import this module directly.  It
will usually be imported through the generic SCons.Platform.Platform()
selection method.
"""

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

import posix
import os

def generate(env):
    posix.generate(env)
    env['SHLIBSUFFIX'] = '.dylib'


FIX sale stock availa.. description

FIX sale stock availa.. description

Response:
# -*- coding: utf-8 -*-
{
    'name': 'Stock availability in sales order line',
    'version': '0.1',
    'category': 'Tools',
    'description': """
Stock availability in sales order line
======================================
* Add two groups. One for seeing stock on sale orders and other to see only if or not available
* Add an option in warehouse to disable stock warning

IMPORTANT:
----------
    * This module could break some warnings as the ones implemented by "warning" module
    * If you dont disable warning and give a user availbility to see only "true/false" on sale order stock, he can see stock if the warning is raised
    """,
    'author': 'Moldeo Interactive & Ingenieria Adhoc',
    'website': 'http://business.moldeo.coop http://ingadhoc.com/',
    'images': [],
    'depends': [
        'sale_stock'
    ],
    'demo': [],
    'data': [
        'sale_view.xml',
        'stock_view.xml',
        'security/sale_order.xml',
        ],
    'test': [],
    'installable': True,
}

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

Prompt:
FIX sale stock availa.. description

# -*- coding: utf-8 -*-
{
    'name': 'Stock availability in sales order line',
    'version': '0.1',
    'category': 'Tools',
    'description': """
    Stock availability in sales order line
    ======================================
    * Add two groups. One for seeing stock on sale orders and other to see only if or not available
    * Add an option in warehouse to disable stock warning
    IMPORTANT:
    ----------
        * This module could break some warnings as the ones implemented by "warning" module
        * If you dont disable warning and give a user availbility to see only "true/false" on sale order stock, he can see stock if the warning is raised
    """,
    'author': 'Moldeo Interactive & Ingenieria Adhoc',
    'website': 'http://business.moldeo.coop http://ingadhoc.com/',
    'images': [],
    'depends': [
        'sale_stock'
    ],
    'demo': [],
    'data': [
        'sale_view.xml',
        'stock_view.xml',
        'security/sale_order.xml',
        ],
    'test': [],
    'installable': True,
}

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


from django.db.models import ForeignKey, DateTimeField, ManyToManyField
from yunity.models.entities import User, Location, Mappable, Message
from yunity.models.utils import BaseModel, MaxLengthCharField
from yunity.utils.decorators import classproperty

class Chat(BaseModel):
    participants = ManyToManyField(User)
    messages = ManyToManyField(Message)

class MappableLocation(BaseModel):
    mappable = ForeignKey(Mappable)
    location = ForeignKey(Location)

    startTime = DateTimeField(null=True)
    endTime = DateTimeField(null=True)

class MappableResponsibility(BaseModel):
    @classproperty
    def TYPE(cls):
        return cls.create_constants('type', 'OWNER')

    @classproperty
    def STATUS(cls):
        return cls.create_constants('status', 'GRANTED', 'PENDING', 'REQUESTED')

    responsible = ForeignKey(User, null=True)
    mappable = ForeignKey(Mappable)

    status = MaxLengthCharField()
    date = DateTimeField(null=True, auto_now=True)
    type = MaxLengthCharField()

class UserLocation(BaseModel):
    user = ForeignKey(User)
    location = ForeignKey(Location)

    type = MaxLengthCharField()

class ItemRequest(BaseModel):
    @classproperty
    def FEEDBACK(cls):
        return cls.create_constants('feedback', 'OK', 'NO_SHOW', 'NOT_GRANTED')

    requester = ForeignKey(User)
    requested = ForeignKey(Mappable)

    feedback = MaxLengthCharField(null=True, default=None)

Response:
from django.db.models import ForeignKey, DateTimeField, ManyToManyField
from yunity.models.entities import User, Location, Mappable, Message
from yunity.models.utils import BaseModel, MaxLengthCharField
from yunity.utils.decorators import classproperty

class Chat(BaseModel):
    participants = ManyToManyField(User)
    messages = ManyToManyField(Message)

class MappableLocation(BaseModel):
    mappable = ForeignKey(Mappable)
    location = ForeignKey(Location)

    startTime = DateTimeField(null=True)
    endTime = DateTimeField(null=True)

class MappableResponsibility(BaseModel):
    @classproperty
    def TYPE(cls):
        return cls.create_constants('type', 'OWNER')

    @classproperty
    def STATUS(cls):
        return cls.create_constants('status', 'GRANTED', 'PENDING', 'REQUESTED')

    responsible = ForeignKey(User, null=True)
    mappable = ForeignKey(Mappable)

    status = MaxLengthCharField()
    date = DateTimeField(null=True, auto_now=True)
    type = MaxLengthCharField()

class UserLocation(BaseModel):
    user = ForeignKey(User)
    location = ForeignKey(Location)

    type = MaxLengthCharField()

class ItemRequest(BaseModel):
    @classproperty
    def FEEDBACK(cls):
        return cls.create_constants('feedback', 'OK', 'NO_SHOW', 'NOT_GRANTED')

    requester = ForeignKey(User)
    requested = ForeignKey(Mappable)

    feedback = MaxLengthCharField(null=True, default=None)

Prompt:
Add some default feedback types for item requests

from django.db.models import ForeignKey, DateTimeField, ManyToManyField
from yunity.models.entities import User, Location, Mappable, Message
from yunity.models.utils import BaseModel, MaxLengthCharField
from yunity.utils.decorators import classproperty

class Chat(BaseModel):
    participants = ManyToManyField(User)
    messages = ManyToManyField(Message)

class MappableLocation(BaseModel):
    mappable = ForeignKey(Mappable)
    location = ForeignKey(Location)

    startTime = DateTimeField(null=True)
    endTime = DateTimeField(null=True)

class MappableResponsibility(BaseModel):
    @classproperty
    def TYPE(cls):
        return cls.create_constants('type', 'OWNER')

    @classproperty
    def STATUS(cls):
        return cls.create_constants('status', 'GRANTED', 'PENDING', 'REQUESTED')

    responsible = ForeignKey(User, null=True)
    mappable = ForeignKey(Mappable)

    status = MaxLengthCharField()
    date = DateTimeField(null=True, auto_now=True)
    type = MaxLengthCharField()

class UserLocation(BaseModel):
    user = ForeignKey(User)
    location = ForeignKey(Location)

    type = MaxLengthCharField()

class ItemRequest(BaseModel):
    requester = ForeignKey(User)
    requested = ForeignKey(Mappable)

    feedback = MaxLengthCharField(null=True, default=None)


import base64
import os
from distutils.core import Command

class TestCommand(Command):
    description = "Launch all tests under fusion_tables app"
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def create_client_secret_file(self):
        client_secret = open("/tmp/client_secret.json", "w")
        data = os.environ.get("CLIENT_SECRET")
        client_secret.write(base64.b64decode(data))
        client_secret.close()

    def configure_settings(self):
        from django.conf import settings
        settings.configure(
            DATABASES={
                "default": {
                    "NAME": ":memory:",
                    "ENGINE": "django.db.backends.sqlite3",
                    "TEST": {
                        "NAME": ":memory:"
                    }
                }
            },
            INSTALLED_APPS=(
                "django.contrib.contenttypes",
                "fusion_tables",
            ),
            ROOT_URLCONF="tests.urls",
            MODELS_TO_SYNC=("fusion_tables.SampleModel", ),
            CLIENT_SECRET_JSON_FILEPATH="/tmp/client_secret.json",
            LOCATION_FIELDS=("TextField", )
        )

    def run(self):
        import django
        from django.core.management import call_command
        self.create_client_secret_file()
        self.configure_settings()
        django.setup()
        call_command("test", "fusion_tables")

Response:
import base64
import os
from distutils.core import Command

class TestCommand(Command):
    description = "Launch all tests under fusion_tables app"
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def create_client_secret_file(self):
        client_secret = open("/tmp/client_secret.json", "w")
        data = os.environ.get("CLIENT_SECRET")
        client_secret.write(base64.b64decode(data))
        client_secret.close()

    def configure_settings(self):
        from django.conf import settings
        settings.configure(
            DATABASES={
                "default": {
                    "NAME": ":memory:",
                    "ENGINE": "django.db.backends.sqlite3",
                    "TEST": {
                        "NAME": ":memory:"
                    }
                }
            },
            INSTALLED_APPS=(
                "django.contrib.contenttypes",
                "fusion_tables",
            ),
            ROOT_URLCONF="tests.urls",
            MODELS_TO_SYNC=("fusion_tables.SampleModel", ),
            CLIENT_SECRET_JSON_FILEPATH="/tmp/client_secret.json",
            LOCATION_FIELDS=("TextField", )
        )

    def run(self):
        import django
        from django.core.management import call_command
        self.create_client_secret_file()
        self.configure_settings()
        django.setup()
        call_command("test", "fusion_tables")

Prompt:
Revert "Fix unit test python3 compatibility."

This reverts commit 6807e5a5966f1f37f69a54e255a9981918cc8fb6.

import base64
import os
from distutils.core import Command

class TestCommand(Command):
    description = "Launch all tests under fusion_tables app"
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def create_client_secret_file(self):
        client_secret = open("/tmp/client_secret.json", "w")
        data = os.environ.get("CLIENT_SECRET").decode("utf-8")
        client_secret.write(base64.b64decode(data))
        client_secret.close()

    def configure_settings(self):
        from django.conf import settings
        settings.configure(
            DATABASES={
                "default": {
                    "NAME": ":memory:",
                    "ENGINE": "django.db.backends.sqlite3",
                    "TEST": {
                        "NAME": ":memory:"
                    }
                }
            },
            INSTALLED_APPS=(
                "django.contrib.contenttypes",
                "fusion_tables",
            ),
            ROOT_URLCONF="tests.urls",
            MODELS_TO_SYNC=("fusion_tables.SampleModel", ),
            CLIENT_SECRET_JSON_FILEPATH="/tmp/client_secret.json",
            LOCATION_FIELDS=("TextField", )
        )

    def run(self):
        import django
        from django.core.management import call_command
        self.create_client_secret_file()
        self.configure_settings()
        django.setup()
        call_command("test", "fusion_tables")


from django.core.management.base import BaseCommand

from corehq.apps.domain.models import Domain
from dimagi.utils.couch.database import iter_docs

class Command(BaseCommand):

    def _get_domains_without_last_modified_date(self):
        docs = iter_docs(Domain.get_db(), [
            domain['id']
            for domain in Domain.view(
                "domain/domains",
                reduce=False,
                include_docs=False
            )
        ])
        return filter(lambda x: 'last_modified' not in x or not x['last_modified'], docs)

    def handle(self, *args, **options):
        for domain_doc in self._get_domains_without_last_modified_date():
            print "Updating domain {}".format(domain_doc['name'])
            domain = Domain.wrap(domain_doc)
            domain.save()

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

from corehq.apps.domain.models import Domain
from dimagi.utils.couch.database import iter_docs

class Command(BaseCommand):

    def _get_domains_without_last_modified_date(self):
        docs = iter_docs(Domain.get_db(), [
            domain['id']
            for domain in Domain.view(
                "domain/domains",
                reduce=False,
                include_docs=False
            )
        ])
        return filter(lambda x: 'last_modified' not in x or not x['last_modified'], docs)

    def handle(self, *args, **options):
        for domain_doc in self._get_domains_without_last_modified_date():
            print "Updating domain {}".format(domain_doc['name'])
            domain = Domain.wrap(domain_doc)
            domain.save()

Prompt:
Include domains which have last_modified equal to None

from django.core.management.base import BaseCommand

from corehq.apps.domain.models import Domain
from dimagi.utils.couch.database import iter_docs

class Command(BaseCommand):

    def _get_domains_without_last_modified_date(self):
        docs = iter_docs(Domain.get_db(), [
            domain['id']
            for domain in Domain.view(
                "domain/domains",
                reduce=False,
                include_docs=False
            )
        ])
        return filter(lambda x: 'last_modified' not in x, docs)

    def handle(self, *args, **options):
        for domain_doc in self._get_domains_without_last_modified_date():
            print "Updating domain {}".format(domain_doc['name'])
            domain = Domain.wrap(domain_doc)
            domain.save()


from busbus import util

class LazyEntityProperty(object):

    def __init__(self, f, *args, **kwargs):
        self.f = f
        self.args = args
        self.kwargs = kwargs

    def __call__(self):
        return self.f(*self.args, **self.kwargs)

class BaseEntity(object):
    __repr_attrs__ = ('id',)

    def __init__(self, provider, **kwargs):
        self._provider = provider
        self._lazy_properties = {}

        for attr in getattr(self, '__attrs__', []):
            if isinstance(kwargs.get(attr, None), LazyEntityProperty):
                self._lazy_properties[attr] = kwargs[attr]
            else:
                setattr(self, attr, kwargs.get(attr, None))

        provider._new_entity(self)

    def __repr__(self):
        return u'<{0}({1})>'.format(
            util.clsname(self), ','.join(
                '{0}={1!r}'.format(i, getattr(self, i))
                for i in self.__repr_attrs__))

    def __getattr__(self, name):
        if name in self._lazy_properties:
            value = self._lazy_properties[name]()
            del self._lazy_properties[name]
            setattr(self, name, value)
            return value
        else:
            raise AttributeError(name)

    def to_dict(self):
        return dict((attr, getattr(self, attr)) for attr in self.__attrs__
                    if getattr(self, attr))

Response:
from busbus import util

class LazyEntityProperty(object):

    def __init__(self, f, *args, **kwargs):
        self.f = f
        self.args = args
        self.kwargs = kwargs

    def __call__(self):
        return self.f(*self.args, **self.kwargs)

class BaseEntity(object):
    __repr_attrs__ = ('id',)

    def __init__(self, provider, **kwargs):
        self._provider = provider
        self._lazy_properties = {}

        for attr in getattr(self, '__attrs__', []):
            if isinstance(kwargs.get(attr, None), LazyEntityProperty):
                self._lazy_properties[attr] = kwargs[attr]
            else:
                setattr(self, attr, kwargs.get(attr, None))

        provider._new_entity(self)

    def __repr__(self):
        return u'<{0}({1})>'.format(
            util.clsname(self), ','.join(
                '{0}={1!r}'.format(i, getattr(self, i))
                for i in self.__repr_attrs__))

    def __getattr__(self, name):
        if name in self._lazy_properties:
            value = self._lazy_properties[name]()
            del self._lazy_properties[name]
            setattr(self, name, value)
            return value
        else:
            raise AttributeError(name)

    def to_dict(self):
        return dict((attr, getattr(self, attr)) for attr in self.__attrs__
                    if getattr(self, attr))

Prompt:
Use an instance variable instead of a non-standard argument to __repr__

from busbus import util

class LazyEntityProperty(object):

    def __init__(self, f, *args, **kwargs):
        self.f = f
        self.args = args
        self.kwargs = kwargs

    def __call__(self):
        return self.f(*self.args, **self.kwargs)

class BaseEntity(object):

    def __init__(self, provider, **kwargs):
        self._provider = provider
        self._lazy_properties = {}

        for attr in getattr(self, '__attrs__', []):
            if isinstance(kwargs.get(attr, None), LazyEntityProperty):
                self._lazy_properties[attr] = kwargs[attr]
            else:
                setattr(self, attr, kwargs.get(attr, None))

        provider._new_entity(self)

    def __repr__(self, args=['id']):
        return u'<{0}({1})>'.format(
            util.clsname(self),
            ','.join('{0}={1!r}'.format(i, getattr(self, i)) for i in args))

    def __getattr__(self, name):
        if name in self._lazy_properties:
            value = self._lazy_properties[name]()
            del self._lazy_properties[name]
            setattr(self, name, value)
            return value
        else:
            raise AttributeError(name)

    def to_dict(self):
        return dict((attr, getattr(self, attr)) for attr in self.__attrs__
                    if getattr(self, attr))


#!/usr/bin/env python

"""Script to read data files in a directory, compute their md5sums, and output
them to a JSON file."""

import json
import os
import sys
import hashlib

def get_relative_filepaths(base_directory):
    """ Return a list of file paths without the base_directory prefix"""
    file_list = []
    for root, subFolders, files in os.walk('Data'):
        relative_path="/".join(root.split('/')[1:])
        for file in files:
            file_list.append(os.path.join(relative_path,file))
    return file_list

def get_md5sums(base_directory):
    md5sums = []
    for filename in get_relative_filepaths(base_directory):
        md5 = hashlib.md5()
        full_filepath = os.path.join(base_directory, filename)
        with open(full_filepath, 'rb') as fp:
            for chunk in iter(lambda: fp.read(128 * md5.block_size), b''):
                md5.update(chunk)
        md5hash = md5.hexdigest()
        md5sums.append((filename, md5hash))
    return md5sums

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print('Usage: ' + sys.argv[0] + ' input_directory output.json')
        sys.exit(1)
    directory = sys.argv[1]
    if not os.path.exists(directory):
        print('Directory does not exist!')
        sys.exit(1)
    output_json = sys.argv[2]

    md5sums = get_md5sums(directory)
    with open(output_json, 'w') as fp:
        json.dump(md5sums, fp, indent=0)

Allow directory of files to be indexed

Response:
#!/usr/bin/env python

"""Script to read data files in a directory, compute their md5sums, and output
them to a JSON file."""

import json
import os
import sys
import hashlib

def get_relative_filepaths(base_directory):
    """ Return a list of file paths without the base_directory prefix"""
    file_list = []
    for root, subFolders, files in os.walk('Data'):
        relative_path="/".join(root.split('/')[1:])
        for file in files:
            file_list.append(os.path.join(relative_path,file))
    return file_list

def get_md5sums(base_directory):
    md5sums = []
    for filename in get_relative_filepaths(base_directory):
        md5 = hashlib.md5()
        full_filepath = os.path.join(base_directory, filename)
        with open(full_filepath, 'rb') as fp:
            for chunk in iter(lambda: fp.read(128 * md5.block_size), b''):
                md5.update(chunk)
        md5hash = md5.hexdigest()
        md5sums.append((filename, md5hash))
    return md5sums

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print('Usage: ' + sys.argv[0] + ' input_directory output.json')
        sys.exit(1)
    directory = sys.argv[1]
    if not os.path.exists(directory):
        print('Directory does not exist!')
        sys.exit(1)
    output_json = sys.argv[2]

    md5sums = get_md5sums(directory)
    with open(output_json, 'w') as fp:
        json.dump(md5sums, fp, indent=0)

Prompt:
ENH: Allow directory of files to be indexed

In the Data directory, there may be sub-directories of files that need to
be kept separate, but all of them need to be indexed.

#!/usr/bin/env python

"""Script to read data files in a directory, compute their md5sums, and output
them to a JSON file."""

import json
import os
import sys
import hashlib

def get_md5sums(directory):
    md5sums = []
    for filename in os.listdir(directory):
        md5 = hashlib.md5()
        with open(os.path.join(directory, filename), 'rb') as fp:
            for chunk in iter(lambda: fp.read(128 * md5.block_size), b''):
                md5.update(chunk)
        md5hash = md5.hexdigest()
        md5sums.append((filename, md5hash))

    return md5sums

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print('Usage: ' + sys.argv[0] + ' input_directory output.json')
        sys.exit(1)
    directory = sys.argv[1]
    if not os.path.exists(directory):
        print('Directory does not exist!')
        sys.exit(1)
    output_json = sys.argv[2]

    md5sums = get_md5sums(directory)
    with open(output_json, 'w') as fp:
        json.dump(md5sums, fp, indent=0)


from uuid import uuid4

from valohai_cli.utils import get_random_string

def get_project_data(n_projects):
    return {
        'results': [
            {'id': str(uuid4()), 'name': get_random_string()}
            for i in range(n_projects)
        ],
    }

def make_call_stub(retval=None):
    calls = []

    def call_stub(*args, **kwargs):
        calls.append({'args': args, 'kwargs': kwargs})
        return retval

    call_stub.calls = calls
    return call_stub

Add generic monkeypatch call stub

Add generic monkeypatch call stub

Response:
from uuid import uuid4

from valohai_cli.utils import get_random_string

def get_project_data(n_projects):
    return {
        'results': [
            {'id': str(uuid4()), 'name': get_random_string()}
            for i in range(n_projects)
        ],
    }

def make_call_stub(retval=None):
    calls = []

    def call_stub(*args, **kwargs):
        calls.append({'args': args, 'kwargs': kwargs})
        return retval

    call_stub.calls = calls
    return call_stub

Prompt:
Add generic monkeypatch call stub

from uuid import uuid4

from valohai_cli.utils import get_random_string

def get_project_data(n_projects):
    return {
        'results': [
            {'id': str(uuid4()), 'name': get_random_string()}
            for i in range(n_projects)
        ],
    }


#!/usr/bin/env python3
import os
import shutil
from PIL import Image

whitelist = (
    (1366, 768),
    (1600, 900),
    (1680, 1050),
    (1920, 1080),
    (1920, 1200),
)

def split(directory):
    filemap = {dimensions: set() for dimensions in whitelist}
    filemap['others'] = set()

    makepath = lambda filename: os.path.join(directory, filename)
    for filename in os.listdir(directory):
        abspath = makepath(filename)
        if not os.path.isfile(abspath):
            continue

        image = Image.open(abspath)
        if image.size in whitelist:
            filemap[image.size].add(abspath)

        else:
            filemap['others'].add(abspath)

    return filemap

def scatter(filemap, directory, fn):
    makepath = lambda resolution: os.path.join(directory, '%dx%d' % resolution)

    for resolution in whitelist:
        if resolution not in filemap or len(filemap[resolution]) == 0:
            continue

        abspath = makepath(resolution)
        os.makedirs(abspath, exist_ok=True)
        for filename in filemap[resolution]:
            try:
                fn(filename, abspath)
            except shutil.Error:
                pass

    for filename in filemap['others']:
        try:
            fn(filename, directory)
        except shutil.Error:
            pass

def main():
    import argparse

    parser = argparse.ArgumentParser(
        description='Automatic wallpaper sorter by image dimensions')
    parser.add_argument('origin', type=os.path.abspath)
    parser.add_argument('destiny', type=os.path.abspath)
    parser.add_argument('-m', '--mv', action='store_const', const=shutil.move, default=shutil.copy)

    args = parser.parse_args()
    scatter(split(args.origin), args.destiny, args.mv)

if __name__ == '__main__':
    main()

Add simple script to sort images

Add simple script to sort images

Response:
#!/usr/bin/env python3
import os
import shutil
from PIL import Image

whitelist = (
    (1366, 768),
    (1600, 900),
    (1680, 1050),
    (1920, 1080),
    (1920, 1200),
)

def split(directory):
    filemap = {dimensions: set() for dimensions in whitelist}
    filemap['others'] = set()

    makepath = lambda filename: os.path.join(directory, filename)
    for filename in os.listdir(directory):
        abspath = makepath(filename)
        if not os.path.isfile(abspath):
            continue

        image = Image.open(abspath)
        if image.size in whitelist:
            filemap[image.size].add(abspath)

        else:
            filemap['others'].add(abspath)

    return filemap

def scatter(filemap, directory, fn):
    makepath = lambda resolution: os.path.join(directory, '%dx%d' % resolution)

    for resolution in whitelist:
        if resolution not in filemap or len(filemap[resolution]) == 0:
            continue

        abspath = makepath(resolution)
        os.makedirs(abspath, exist_ok=True)
        for filename in filemap[resolution]:
            try:
                fn(filename, abspath)
            except shutil.Error:
                pass

    for filename in filemap['others']:
        try:
            fn(filename, directory)
        except shutil.Error:
            pass

def main():
    import argparse

    parser = argparse.ArgumentParser(
        description='Automatic wallpaper sorter by image dimensions')
    parser.add_argument('origin', type=os.path.abspath)
    parser.add_argument('destiny', type=os.path.abspath)
    parser.add_argument('-m', '--mv', action='store_const', const=shutil.move, default=shutil.copy)

    args = parser.parse_args()
    scatter(split(args.origin), args.destiny, args.mv)

if __name__ == '__main__':
    main()

Prompt: Add simple script to sort images


from django.test import TestCase
from django.test.utils import override_settings
from django.core.urlresolvers import reverse
from django.conf import settings
from mitxmako.shortcuts import marketing_link
from mock import patch

class ShortcutsTests(TestCase):
    """
    Test the mitxmako shortcuts file
    """

    @override_settings(MKTG_URLS={'ROOT': 'dummy-root', 'ABOUT': '/about-us'})
    @override_settings(MKTG_URL_LINK_MAP={'ABOUT': 'login'})
    def test_marketing_link(self):
        # test marketing site on
        with patch.dict('django.conf.settings.MITX_FEATURES', {'ENABLE_MKTG_SITE': True}):
            expected_link = 'dummy-root/about-us'
            link = marketing_link('ABOUT')
            self.assertEquals(link, expected_link)
        # test marketing site off
        with patch.dict('django.conf.settings.MITX_FEATURES', {'ENABLE_MKTG_SITE': False}):
            # we are using login because it is common across both cms and lms
            expected_link = reverse('login')
            link = marketing_link('ABOUT')
            self.assertEquals(link, expected_link)

Response:
from django.test import TestCase
from django.test.utils import override_settings
from django.core.urlresolvers import reverse
from django.conf import settings
from mitxmako.shortcuts import marketing_link
from mock import patch

class ShortcutsTests(TestCase):
    """
    Test the mitxmako shortcuts file
    """

    @override_settings(MKTG_URLS={'ROOT': 'dummy-root', 'ABOUT': '/about-us'})
    @override_settings(MKTG_URL_LINK_MAP={'ABOUT': 'login'})
    def test_marketing_link(self):
        # test marketing site on
        with patch.dict('django.conf.settings.MITX_FEATURES', {'ENABLE_MKTG_SITE': True}):
            expected_link = 'dummy-root/about-us'
            link = marketing_link('ABOUT')
            self.assertEquals(link, expected_link)
        # test marketing site off
        with patch.dict('django.conf.settings.MITX_FEATURES', {'ENABLE_MKTG_SITE': False}):
            # we are using login because it is common across both cms and lms
            expected_link = reverse('login')
            link = marketing_link('ABOUT')
            self.assertEquals(link, expected_link)

Prompt:
Fix test so that it works with both CMS and LMS settings

from django.test import TestCase
from django.test.utils import override_settings
from django.core.urlresolvers import reverse
from django.conf import settings
from mitxmako.shortcuts import marketing_link
from mock import patch

class ShortcutsTests(TestCase):
    """
    Test the mitxmako shortcuts file
    """

    @override_settings(MKTG_URLS={'ROOT': 'dummy-root', 'ABOUT': '/about-us'})
    @override_settings(MKTG_URL_LINK_MAP={'ABOUT': 'about_edx'})
    def test_marketing_link(self):
        # test marketing site on
        with patch.dict('django.conf.settings.MITX_FEATURES', {'ENABLE_MKTG_SITE': True}):
            expected_link = 'dummy-root/about-us'
            link = marketing_link('ABOUT')
            self.assertEquals(link, expected_link)
        # test marketing site off
        with patch.dict('django.conf.settings.MITX_FEATURES', {'ENABLE_MKTG_SITE': False}):
            expected_link = reverse('about_edx')
            link = marketing_link('ABOUT')
            self.assertEquals(link, expected_link)


#!/usr/bin/env python
# encoding: utf-8

from __future__ import absolute_import, unicode_literals
from .text_exporter import TextExporter
from .util import get_tags_count

class TagExporter(TextExporter):
    """This Exporter can lists the tags for entries and journals, exported as a plain text file."""
    names = ["tags"]
    extension = "tags"

    @classmethod
    def export_entry(cls, entry):
        """Returns a list of tags for a single entry."""
        return ", ".join(entry.tags)

    @classmethod
    def export_journal(cls, journal):
        """Returns a list of tags and their frequency for an entire journal."""
        tag_counts = get_tags_count(journal)
        result = ""
        if not tag_counts:
            return '[No tags found in journal.]'
        elif min(tag_counts)[0] == 0:
            tag_counts = filter(lambda x: x[0] > 1, tag_counts)
            result += '[Removed tags that appear only once.]\n'
        result += "\n".join("{0:20} : {1}".format(tag, n) for n, tag in sorted(tag_counts, reverse=True))
        return result

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

from __future__ import absolute_import, unicode_literals
from .text_exporter import TextExporter
from .util import get_tags_count

class TagExporter(TextExporter):
    """This Exporter can lists the tags for entries and journals, exported as a plain text file."""
    names = ["tags"]
    extension = "tags"

    @classmethod
    def export_entry(cls, entry):
        """Returns a list of tags for a single entry."""
        return ", ".join(entry.tags)

    @classmethod
    def export_journal(cls, journal):
        """Returns a list of tags and their frequency for an entire journal."""
        tag_counts = get_tags_count(journal)
        result = ""
        if not tag_counts:
            return '[No tags found in journal.]'
        elif min(tag_counts)[0] == 0:
            tag_counts = filter(lambda x: x[0] > 1, tag_counts)
            result += '[Removed tags that appear only once.]\n'
        result += "\n".join("{0:20} : {1}".format(tag, n) for n, tag in sorted(tag_counts, reverse=True))
        return result

Prompt:
Update `Tag` exporter code documentation.

#!/usr/bin/env python
# encoding: utf-8

from __future__ import absolute_import, unicode_literals
from .text_exporter import TextExporter
from .util import get_tags_count

class TagExporter(TextExporter):
    """This Exporter can convert entries and journals into json."""
    names = ["tags"]
    extension = "tags"

    @classmethod
    def export_entry(cls, entry):
        """Returns a markdown representation of a single entry."""
        return ", ".join(entry.tags)

    @classmethod
    def export_journal(cls, journal):
        """Returns a json representation of an entire journal."""
        tag_counts = get_tags_count(journal)
        result = ""
        if not tag_counts:
            return '[No tags found in journal.]'
        elif min(tag_counts)[0] == 0:
            tag_counts = filter(lambda x: x[0] > 1, tag_counts)
            result += '[Removed tags that appear only once.]\n'
        result += "\n".join("{0:20} : {1}".format(tag, n) for n, tag in sorted(tag_counts, reverse=True))
        return result


# -*- encoding: utf-8 -*-
#
# :authors: Arturo Filastò
# :licence: see LICENSE

from twisted.internet import defer
from twisted.python import usage
from ooni.templates import httpt

class UsageOptions(usage.Options):
    optParameters = [
                     ['url', 'u', None, 'Specify a single URL to test.'],
                     ['factor', 'f', 0.8, 'What factor should be used for triggering censorship (0.8 == 80%)']
                    ]

class HTTPBodyLength(httpt.HTTPTest):
    """
    Performs a two GET requests to the set of sites to be tested for
    censorship, one over a known good control channel (Tor), the other over the
    test network.
    We then look at the response body lengths and see if the control response
    differs from the experiment response by a certain factor.
    """
    name = "HTTP Body length test"
    author = "Arturo Filastò"
    version = "0.1"

    usageOptions = UsageOptions

    inputFile = ['file', 'f', None,
            'List of URLS to perform GET and POST requests to']

    # These values are used for determining censorship based on response body
    # lengths
    control_body_length = None
    experiment_body_length = None

    def setUp(self):
        """
        Check for inputs.
        """
        if self.input:
            self.url = self.input
        elif self.localOptions['url']:
            self.url = self.localOptions['url']
        else:
            raise Exception("No input specified")

        self.factor = self.localOptions['factor']

    def compare_body_lengths(self):
        body_length_a = self.control_body_length
        body_length_b = self.experiment_body_length

        rel = float(body_length_a)/float(body_length_b)
        if rel > 1:
            rel = 1/rel

        self.report['body_proportion'] = rel
        self.report['factor'] = self.factor
        if rel < self.factor:
            self.report['censorship'] = True
        else:
            self.report['censorship'] = False

    def test_get(self):
        def errback(failure):
            log.err("There was an error while testing %s" % self.url)
            log.exception(failure)

        def control_body(result):
            self.control_body_length = len(result)
            if self.experiment_body_length:
                self.compare_body_lengths()

        def experiment_body(result):
            self.experiment_body_length = len(result)
            if self.control_body_length:
                self.compare_body_lengths()

        dl = []
        experiment_request = self.doRequest(self.url, method="GET",
                body_processor=experiment_body)
        control_request = self.doRequest(self.url, method="GET",
                use_tor=True, body_processor=control_body)
        dl.append(experiment_request)
        dl.append(control_request)
        d = defer.DeferredList(dl)
        return d

Add test that detects censorship in HTTP pages based on HTTP body length

Response:
# -*- encoding: utf-8 -*-
#
# :authors: Arturo Filastò
# :licence: see LICENSE

from twisted.internet import defer
from twisted.python import usage
from ooni.templates import httpt

class UsageOptions(usage.Options):
    optParameters = [
                     ['url', 'u', None, 'Specify a single URL to test.'],
                     ['factor', 'f', 0.8, 'What factor should be used for triggering censorship (0.8 == 80%)']
                    ]

class HTTPBodyLength(httpt.HTTPTest):
    """
    Performs a two GET requests to the set of sites to be tested for
    censorship, one over a known good control channel (Tor), the other over the
    test network.
    We then look at the response body lengths and see if the control response
    differs from the experiment response by a certain factor.
    """
    name = "HTTP Body length test"
    author = "Arturo Filastò"
    version = "0.1"

    usageOptions = UsageOptions

    inputFile = ['file', 'f', None,
            'List of URLS to perform GET and POST requests to']

    # These values are used for determining censorship based on response body
    # lengths
    control_body_length = None
    experiment_body_length = None

    def setUp(self):
        """
        Check for inputs.
        """
        if self.input:
            self.url = self.input
        elif self.localOptions['url']:
            self.url = self.localOptions['url']
        else:
            raise Exception("No input specified")

        self.factor = self.localOptions['factor']

    def compare_body_lengths(self):
        body_length_a = self.control_body_length
        body_length_b = self.experiment_body_length

        rel = float(body_length_a)/float(body_length_b)
        if rel > 1:
            rel = 1/rel

        self.report['body_proportion'] = rel
        self.report['factor'] = self.factor
        if rel < self.factor:
            self.report['censorship'] = True
        else:
            self.report['censorship'] = False

    def test_get(self):
        def errback(failure):
            log.err("There was an error while testing %s" % self.url)
            log.exception(failure)

        def control_body(result):
            self.control_body_length = len(result)
            if self.experiment_body_length:
                self.compare_body_lengths()

        def experiment_body(result):
            self.experiment_body_length = len(result)
            if self.control_body_length:
                self.compare_body_lengths()

        dl = []
        experiment_request = self.doRequest(self.url, method="GET",
                body_processor=experiment_body)
        control_request = self.doRequest(self.url, method="GET",
                use_tor=True, body_processor=control_body)
        dl.append(experiment_request)
        dl.append(control_request)
        d = defer.DeferredList(dl)
        return d

Prompt: Add test that detects censorship in HTTP pages based on HTTP body length


# -*- encoding: utf-8 -*-

import os
import fcntl

class FileLock(object):

    def __init__(self, fd):

        # the fd is borrowed, so do not close it
        self.fd = fd

    def acquire(self, ex=False, nb=True):
        """
        Acquire a lock on the fd.

        :param ex (optional): default False, acquire a exclusive lock if True
        :param nb (optional): default True, non blocking if True
        :return: True if acquired

        """

        try:

            lock_flags = fcntl.LOCK_EX if ex else fcntl.LOCK_SH

            if nb:

                lock_flags |= fcntl.LOCK_NB

            fcntl.flock(self.fd, lock_flags)

            return True

        except IOError:

            return False

    def release(self):
        """
        Release lock on the fd.

        """

        fcntl.flock(self.fd, fcntl.LOCK_UN)

Response:
# -*- encoding: utf-8 -*-

import os
import fcntl

class FileLock(object):

    def __init__(self, fd):

        # the fd is borrowed, so do not close it
        self.fd = fd

    def acquire(self, ex=False, nb=True):
        """
        Acquire a lock on the fd.

        :param ex (optional): default False, acquire a exclusive lock if True
        :param nb (optional): default True, non blocking if True
        :return: True if acquired

        """

        try:

            lock_flags = fcntl.LOCK_EX if ex else fcntl.LOCK_SH

            if nb:

                lock_flags |= fcntl.LOCK_NB

            fcntl.flock(self.fd, lock_flags)

            return True

        except IOError:

            return False

    def release(self):
        """
        Release lock on the fd.

        """

        fcntl.flock(self.fd, fcntl.LOCK_UN)

Prompt:
Change parameter names in acquire.
Add some doc.

# -*- encoding: utf-8 -*-

import os
import fcntl

class FileLock(object):

    def __init__(self, fd):

        # the fd is borrowed, so do not close it
        self.fd = fd

    def acquire(self, write=False, block=False):

        try:

            lock_flags = fcntl.LOCK_EX if write else fcntl.LOCK_SH

            if not block:

                lock_flags |= fcntl.LOCK_NB

            fcntl.flock(self.fd, lock_flags)

            return True

        except IOError:

            return False

    def release(self):

        fcntl.flock(self.fd, fcntl.LOCK_UN)


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

class Grok(object):

    """stats.grok.se article statistics."""

    def __init__(self, title, site):
        self.site = site
        self.title = title

    def _make_url(self, year, month):
        """Make the URL to the JSON output of stats.grok.se service."""
        base_url = "http://stats.grok.se/json/"
        return base_url + "{0:s}/{1:d}{2:02d}/{3:s}".format(self.site, year, month, self.title)

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

class Grok(object):

    """stats.grok.se article statistics."""

    def __init__(self, title, site):
        self.site = site
        self.title = title

    def _make_url(self, year, month):
        """Make the URL to the JSON output of stats.grok.se service."""
        base_url = "http://stats.grok.se/json/"
        return base_url + "{0:s}/{1:d}{2:02d}/{3:s}".format(self.site, year, month, self.title)

Prompt:
Make Grok a new-style class inheriting from object

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

class Grok:

    """stats.grok.se article statistics."""

    def __init__(self, title, site):
        self.site = site
        self.title = title

    def _make_url(self, year, month):
        """Make the URL to the JSON output of stats.grok.se service."""
        base_url = "http://stats.grok.se/json/"
        return base_url + "{0:s}/{1:d}{2:02d}/{3:s}".format(self.site, year, month, self.title)


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

__version__ = '$Id$'

import family

# The Wikia Search family
# user-config.py: usernames['wikia']['wikia'] = 'User name'

class Family(family.Family):
    def __init__(self):
        family.Family.__init__(self)
        self.name = u'wikia'

        self.langs = {
            u'wikia': None,
        }

    def hostname(self, code):
        return u'www.wikia.com'

    def version(self, code):
        return "1.16.2"

    def scriptpath(self, code):
        return ''

    def apipath(self, code):
        return '/api.php'

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

__version__ = '$Id$'

import family

# The Wikia Search family
# user-config.py: usernames['wikia']['wikia'] = 'User name'

class Family(family.Family):
    def __init__(self):
        family.Family.__init__(self)
        self.name = u'wikia'

        self.langs = {
            u'wikia': None,
        }

    def hostname(self, code):
        return u'www.wikia.com'

    def version(self, code):
        return "1.16.2"

    def scriptpath(self, code):
        return ''

    def apipath(self, code):
        return '/api.php'

Prompt:
Update a version number from trunk r9016

https://mediawiki.org/wiki/Special:Code/pywikipedia/9040

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

__version__ = '$Id$'

import family

# The Wikia Search family
# user-config.py: usernames['wikia']['wikia'] = 'User name'

class Family(family.Family):
    def __init__(self):
        family.Family.__init__(self)
        self.name = u'wikia'

        self.langs = {
            u'wikia': None,
        }

    def hostname(self, code):
        return u'www.wikia.com'

    def version(self, code):
        return "1.15.1"

    def scriptpath(self, code):
        return ''

    def apipath(self, code):
        return '/api.php'


import inspect

class PreconditionError (TypeError):
    pass

def preconditions(*precs):

    precinfo = []
    for p in precs:
        spec = inspect.getargspec(p)
        if spec.varargs or spec.keywords:
            raise PreconditionError(
                'Precondition {!r} must not accept * nor ** args.'.format(p))

        i = -len(spec.defaults)
        appargs, closureargs = spec.args[:i], spec.args[i:]
        precinfo.append( (appargs, closureargs, p) )

    def decorate(f):
        def g(*a, **kw):
            return f(*a, **kw)
        return g
    return decorate

Response:
import inspect

class PreconditionError (TypeError):
    pass

def preconditions(*precs):

    precinfo = []
    for p in precs:
        spec = inspect.getargspec(p)
        if spec.varargs or spec.keywords:
            raise PreconditionError(
                'Precondition {!r} must not accept * nor ** args.'.format(p))

        i = -len(spec.defaults)
        appargs, closureargs = spec.args[:i], spec.args[i:]
        precinfo.append( (appargs, closureargs, p) )

    def decorate(f):
        def g(*a, **kw):
            return f(*a, **kw)
        return g
    return decorate

Prompt:
Implement two of the "early" InvalidPreconditionTests which can be checked prior to seeing the wrapping function.

class PreconditionError (TypeError):
    pass

def preconditions(*precs):
    def decorate(f):
        def g(*a, **kw):
            return f(*a, **kw)
        return g
    return decorate


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

import warnings

from django.conf import settings  # pylint: disable=W0611

from appconf import AppConf

class WatermarkSettings(AppConf):
    QUALITY = 85
    OBSCURE_ORIGINAL = True
    RANDOM_POSITION_ONCE = True
    WATERMARK_PERCENTAGE = getattr(settings, 'WATERMARK_PERCENTAGE', 30)

    class Meta:
        prefix = 'watermark'
        holder = 'watermarker.conf.settings'

    def configure_quality(self, value):
        if getattr(settings, 'WATERMARKING_QUALITY', None):
            warnings.warn("WATERMARKING_QUALITY is deprecated, use WATERMARK_QUALITY", DeprecationWarning)

        return value

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

import warnings

from django.conf import settings  # pylint: disable=W0611

from appconf import AppConf

class WatermarkSettings(AppConf):
    QUALITY = 85
    OBSCURE_ORIGINAL = True
    RANDOM_POSITION_ONCE = True
    WATERMARK_PERCENTAGE = getattr(settings, 'WATERMARK_PERCENTAGE', 30)

    class Meta:
        prefix = 'watermark'
        holder = 'watermarker.conf.settings'

    def configure_quality(self, value):
        if getattr(settings, 'WATERMARKING_QUALITY', None):
            warnings.warn("WATERMARKING_QUALITY is deprecated, use WATERMARK_QUALITY", DeprecationWarning)

        return value

Prompt:
Change AppConf class to use settings defined value or default.

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

import warnings

from django.conf import settings  # pylint: disable=W0611

from appconf import AppConf

class WatermarkSettings(AppConf):
    QUALITY = 85
    OBSCURE_ORIGINAL = True
    RANDOM_POSITION_ONCE = True
    WATERMARK_PERCENTAGE = 30

    class Meta:
        prefix = 'watermark'
        holder = 'watermarker.conf.settings'

    def configure_quality(self, value):
        if getattr(settings, 'WATERMARKING_QUALITY', None):
            warnings.warn("WATERMARKING_QUALITY is deprecated, use WATERMARK_QUALITY", DeprecationWarning)

        return value


from django.shortcuts import redirect

from thefederation.tasks import poll_node
from thefederation.utils import is_valid_hostname

def register_view(request, host):
    # TODO rate limit this view per caller ip?
    if not is_valid_hostname:
        return redirect("/")
    if poll_node(host):
        # Success!
        return redirect(f"/node/{host}")
    # TODO show an error or something
    return redirect("/")

Response:
from django.shortcuts import redirect

from thefederation.tasks import poll_node
from thefederation.utils import is_valid_hostname

def register_view(request, host):
    # TODO rate limit this view per caller ip?
    if not is_valid_hostname:
        return redirect("/")
    if poll_node(host):
        # Success!
        return redirect(f"/node/{host}")
    # TODO show an error or something
    return redirect("/")

Prompt:
Fix redirect to node page after registration

import time

from django.shortcuts import redirect

from thefederation.tasks import poll_node
from thefederation.utils import is_valid_hostname

def register_view(request, host):
    # TODO rate limit this view per caller ip?
    if not is_valid_hostname:
        return redirect("/")
    if poll_node(host):
        # Success!
        return redirect(f"/nodes/{host}")
    # TODO show an error or something
    return redirect("/")


#!/usr/local/bin/python
# Code Fights Longest Word Problem

import re

def longestWord(text):
    m = re.findall(r'\b[a-z]+?\b', text, flags=re.I)
    return max(m, key=len)

def main():
    tests = [
        ["Ready, steady, go!", "steady"],
        ["Ready[[[, steady, go!", "steady"],
        ["ABCd", "ABCd"]
    ]

    for t in tests:
        res = longestWord(t[0])
        ans = t[1]
        if ans == res:
            print("PASSED: longestWord({}) returned {}"
                  .format(t[0], res))
        else:
            print("FAILED: longestWord({}) returned {}, answer: {}"
                  .format(t[0], res, ans))

if __name__ == '__main__':
    main()

Solve Code Fights longest word problem

Solve Code Fights longest word problem

Response:
#!/usr/local/bin/python
# Code Fights Longest Word Problem

import re

def longestWord(text):
    m = re.findall(r'\b[a-z]+?\b', text, flags=re.I)
    return max(m, key=len)

def main():
    tests = [
        ["Ready, steady, go!", "steady"],
        ["Ready[[[, steady, go!", "steady"],
        ["ABCd", "ABCd"]
    ]

    for t in tests:
        res = longestWord(t[0])
        ans = t[1]
        if ans == res:
            print("PASSED: longestWord({}) returned {}"
                  .format(t[0], res))
        else:
            print("FAILED: longestWord({}) returned {}, answer: {}"
                  .format(t[0], res, ans))

if __name__ == '__main__':
    main()

Prompt: Solve Code Fights longest word problem


from flask import Blueprint

main = Blueprint('main', __name__)

from . import errors
from .views import (
    crown_hosting, g_cloud, login, marketplace, suppliers,
    digital_outcomes_and_specialists
)

Delete reference to removed view

Delete reference to removed view

Response:
from flask import Blueprint

main = Blueprint('main', __name__)

from . import errors
from .views import (
    crown_hosting, g_cloud, login, marketplace, suppliers,
    digital_outcomes_and_specialists
)

Prompt:
Delete reference to removed view

from flask import Blueprint

main = Blueprint('main', __name__)

from . import errors
from .views import (
    crown_hosting, digital_services_framework, g_cloud, login, marketplace, suppliers,
    digital_outcomes_and_specialists
)


#!/usr/bin/env python3

"""Generate fake NN files to test the lightweight classes"""

import argparse
import json
import h5py
import numpy as np

def _run():
    args = _get_args()
    _build_keras_arch("arch.json")
    _build_keras_inputs_file("inputs.json")
    _build_keras_weights("weights.h5", verbose=args.verbose)

def _get_args():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('-v', '--verbose', action='store_true')
    return parser.parse_args()

def _build_keras_arch(name):
    arch = {
        'layers': [
            {'activation': 'relu', 'name': 'Dense'}
        ]
    }
    with open(name, 'w') as out_file:
        out_file.write(json.dumps(arch, indent=2))

def _build_keras_inputs_file(name):
    def build_input(num):
        return {"name": "in{}".format(num), "offset": 0.0, "scale": 1.0}
    top = {
        "inputs": [build_input(x) for x in range(1,5)],
        "class_labels": ["out{}".format(x) for x in range(1,5)]
    }
    with open(name, 'w') as out_file:
        out_file.write(json.dumps(top, indent=2))

def _build_keras_weights(name, verbose):
    half_swap = np.zeros((4,4))
    half_swap[0,3] = 1.0
    half_swap[1,2] = 1.0
    if verbose:
        print(half_swap)

    bias = np.zeros(4)
    with h5py.File(name, 'w') as h5_file:
        layer0 = h5_file.create_group("layer_0")
        layer0.create_dataset("param_0", data=half_swap)
        layer0.create_dataset("param_1", data=bias)

if __name__ == "__main__":
    _run()

Add dummy Keras inputs builder

Add dummy Keras inputs builder

Response:
#!/usr/bin/env python3

"""Generate fake NN files to test the lightweight classes"""

import argparse
import json
import h5py
import numpy as np

def _run():
    args = _get_args()
    _build_keras_arch("arch.json")
    _build_keras_inputs_file("inputs.json")
    _build_keras_weights("weights.h5", verbose=args.verbose)

def _get_args():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('-v', '--verbose', action='store_true')
    return parser.parse_args()

def _build_keras_arch(name):
    arch = {
        'layers': [
            {'activation': 'relu', 'name': 'Dense'}
        ]
    }
    with open(name, 'w') as out_file:
        out_file.write(json.dumps(arch, indent=2))

def _build_keras_inputs_file(name):
    def build_input(num):
        return {"name": "in{}".format(num), "offset": 0.0, "scale": 1.0}
    top = {
        "inputs": [build_input(x) for x in range(1,5)],
        "class_labels": ["out{}".format(x) for x in range(1,5)]
    }
    with open(name, 'w') as out_file:
        out_file.write(json.dumps(top, indent=2))

def _build_keras_weights(name, verbose):
    half_swap = np.zeros((4,4))
    half_swap[0,3] = 1.0
    half_swap[1,2] = 1.0
    if verbose:
        print(half_swap)

    bias = np.zeros(4)
    with h5py.File(name, 'w') as h5_file:
        layer0 = h5_file.create_group("layer_0")
        layer0.create_dataset("param_0", data=half_swap)
        layer0.create_dataset("param_1", data=bias)

if __name__ == "__main__":
    _run()

Prompt: Add dummy Keras inputs builder


import requests
import collections

API_URL = 'https://api.github.com/users/{}/repos?per_page=100'

def main(user):
    return parse(request(user))

def request(user):
    return requests.get(url=API_URL.format(user))

def parse(response):
    repos = response.json()
    data = []
    if repos is None:
        return None
    for repo in repos:
        if 'name' in repo and not repo['fork']:
            data.append(
                collections.OrderedDict([('name', repo['name']),
                                         ('desc', repo['description']),
                                         ('lang', repo['language']),
                                         ('stars', repo['stargazers_count'])]))
    return data

if __name__ == '__main__':
    import pprint
    u = 'kshvmdn'
    pprint.pprint(main(u))

Response:
import requests
import collections

API_URL = 'https://api.github.com/users/{}/repos?per_page=100'

def main(user):
    return parse(request(user))

def request(user):
    return requests.get(url=API_URL.format(user))

def parse(response):
    repos = response.json()
    data = []
    if repos is None:
        return None
    for repo in repos:
        if 'name' in repo and not repo['fork']:
            data.append(
                collections.OrderedDict([('name', repo['name']),
                                         ('desc', repo['description']),
                                         ('lang', repo['language']),
                                         ('stars', repo['stargazers_count'])]))
    return data

if __name__ == '__main__':
    import pprint
    u = 'kshvmdn'
    pprint.pprint(main(u))

Prompt:
Update api url for recent 100 instead of default 30

import requests
import collections

API_URL = 'https://api.github.com/users/{}/repos'

def main(user):
    return parse(request(user))

def request(user):
    return requests.get(url=API_URL.format(user))

def parse(response):
    repos = response.json()
    data = []
    if repos is None:
        return None
    for repo in repos:
        if 'name' in repo and not repo['fork']:
            data.append(
                collections.OrderedDict([('name', repo['name']),
                                         ('desc', repo['description']),
                                         ('lang', repo['language']),
                                         ('stars', repo['stargazers_count'])]))
    return data

if __name__ == '__main__':
    import pprint
    u = 'kshvmdn'
    pprint.pprint(main(u))


#!/usr/bin/env python

import unittest
from playhouse.test_utils import test_database
from peewee import *

from parsemypsa.storage import objects

# Data for testing
test_db = SqliteDatabase(':memory:')
model_list = [objects.Alert, objects.VehiculeInformation, objects.Trip]

class TripTestCase(unittest.TestCase):
    def setUp(self):
        with test_database(test_db, model_list):
            self.trip1 = objects.Trip.create(id=1, timestamp=1462731168, duration=200, distance=1000, fuel_consumation=1, typ=0, merged=0)

    def test_mileage_calculation(self):
        with test_database(test_db, model_list):
            self.trip1.calculate_mileage()
            self.assertEqual(self.trip1.mileage, 1000)

    def test_formatted_date(self):
        with test_database(test_db, model_list):
            self.assertEqual(self.trip1.return_formatted_date(), "2016-05-08 20:12:48")

    def test_to_string(self):
        with test_database(test_db, model_list):
            self.assertEqual(str(self.trip1), "Trip 1 lasted 200")

Fix unittests broken after ORM adoption

Fix unittests broken after ORM adoption

Response:
#!/usr/bin/env python

import unittest
from playhouse.test_utils import test_database
from peewee import *

from parsemypsa.storage import objects

# Data for testing
test_db = SqliteDatabase(':memory:')
model_list = [objects.Alert, objects.VehiculeInformation, objects.Trip]

class TripTestCase(unittest.TestCase):
    def setUp(self):
        with test_database(test_db, model_list):
            self.trip1 = objects.Trip.create(id=1, timestamp=1462731168, duration=200, distance=1000, fuel_consumation=1, typ=0, merged=0)

    def test_mileage_calculation(self):
        with test_database(test_db, model_list):
            self.trip1.calculate_mileage()
            self.assertEqual(self.trip1.mileage, 1000)

    def test_formatted_date(self):
        with test_database(test_db, model_list):
            self.assertEqual(self.trip1.return_formatted_date(), "2016-05-08 20:12:48")

    def test_to_string(self):
        with test_database(test_db, model_list):
            self.assertEqual(str(self.trip1), "Trip 1 lasted 200")

Prompt:
Fix unittests broken after ORM adoption

#!/usr/bin/env python

import unittest

from parsemypsa.storage import objects

class TripTestCase(unittest.TestCase):
    def setUp(self):
        self.trip1 = objects.Trip.create(id=1, 1462731168, 200, 1000, 1, 0, 0)

    def test_mileage_calculation(self):
        self.trip1.calculate_mileage()
        self.assertEqual(self.trip1._mileage, 1000)

    def test_formatted_date(self):
        self.assertEqual(self.trip1.return_formatted_date(), "2016-05-08 20:12:48")

    def test_to_string(self):
        self.assertEqual(str(self.trip1), "Trip 1 lasted 200")


#!/usr/bin/env python

from __future__ import division
import requests
import json
import sys
from requests.exceptions import SSLError, InvalidSchema, ConnectionError

def get_link_status_code(link):
    headers = {'User-agent':'Mozilla/5.0'}
    try:
        r = requests.head(link, headers=headers, allow_redirects=True)
        return r.status_code
    except (Exception) as e:
        return '%s: %s' % (type(e).__name__, str(e))

def is_valid_link(status_code):
    if status_code == 200:
        return True
    else:
        return False

def process_links(links):
    bad_links = 0
    try:
        for link in links:
            status_code = get_link_status_code(link['href'])
            if not is_valid_link(status_code):
                print 'Invalid link (%s): %s [%s]' % (status_code, link['description'], link['href'])
                bad_links += 1
    except KeyboardInterrupt:
        pass

    linkrot = int(bad_links/len(links)*100)
    print '\n%s%% linkrot\n' % linkrot

def process_bookmarks_file(filename):
    with open(filename) as f:
        bookmarks = json.load(f)
        process_links(bookmarks)

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'Usage: pinboard_linkrot.py <bookmarks.json>'
        exit(1)
    process_bookmarks_file(sys.argv[1])

Response:
#!/usr/bin/env python

from __future__ import division
import requests
import json
import sys
from requests.exceptions import SSLError, InvalidSchema, ConnectionError

def get_link_status_code(link):
    headers = {'User-agent':'Mozilla/5.0'}
    try:
        r = requests.head(link, headers=headers, allow_redirects=True)
        return r.status_code
    except (Exception) as e:
        return '%s: %s' % (type(e).__name__, str(e))

def is_valid_link(status_code):
    if status_code == 200:
        return True
    else:
        return False

def process_links(links):
    bad_links = 0
    try:
        for link in links:
            status_code = get_link_status_code(link['href'])
            if not is_valid_link(status_code):
                print 'Invalid link (%s): %s [%s]' % (status_code, link['description'], link['href'])
                bad_links += 1
    except KeyboardInterrupt:
        pass

    linkrot = int(bad_links/len(links)*100)
    print '\n%s%% linkrot\n' % linkrot

def process_bookmarks_file(filename):
    with open(filename) as f:
        bookmarks = json.load(f)
        process_links(bookmarks)

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'Usage: pinboard_linkrot.py <bookmarks.json>'
        exit(1)
    process_bookmarks_file(sys.argv[1])

Prompt:
Return exception details when failing to load link

#!/usr/bin/env python

from __future__ import division
import requests
import json
import sys
from requests.exceptions import SSLError, InvalidSchema, ConnectionError

def get_link_status_code(link):
    headers = {'User-agent':'Mozilla/5.0'}
    try:
        r = requests.head(link, headers=headers, allow_redirects=True)
        return r.status_code
    except (SSLError, InvalidSchema, ConnectionError):
        return 409

def is_valid_link(status_code):
    if status_code == 200:
        return True
    else:
        return False

def process_links(links):
    bad_links = 0
    try:
        for link in links:
            status_code = get_link_status_code(link['href'])
            if not is_valid_link(status_code):
                print 'Invalid link (%s): %s [%s]' % (status_code, link['description'], link['href'])
                bad_links += 1
    except KeyboardInterrupt:
        pass

    linkrot = int(bad_links/len(links)*100)
    print '\n%s%% linkrot\n' % linkrot

def process_bookmarks_file(filename):
    with open(filename) as f:
        bookmarks = json.load(f)
        process_links(bookmarks)

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'Usage: pinboard_linkrot.py <bookmarks.json>'
        exit(1)
    process_bookmarks_file(sys.argv[1])