#!/usr/bin/env python

import sys, os
import serial
from PIL import Image

if not len(sys.argv) == 2:
    print sys.argv[0], "/path/to/image"
    sys.exit(1)

filepath = sys.argv[1]
im = Image.open(filepath)
rgb_im = im.convert('RGB')

width, height = im.size

if not width == 96 or not height == 64:
    print "Image wrong size. Must be 96x64."
    sys.exit(1)

bytes = []

for y in range(0, height):
    for byteIndex in range(0, width / 8):
        byte = 0

        for bit in range(0, 8):
            x = byteIndex * 8 + bit
            r, g, b = rgb_im.getpixel((x, y))
            v = (r + g + b) / 3.0

            if v < 128:
                byte |= 0x80 >> bit

        bytes += [byte]

ser = serial.Serial("/dev/ttyUSB0", 500000)

for byte in bytes:
    x = ser.write(chr(byte))

ser.close()

print "sent", len(bytes), "bytes"

Send images to calculator over serial.

Send images to calculator over serial.

Response:
#!/usr/bin/env python

import sys, os
import serial
from PIL import Image

if not len(sys.argv) == 2:
    print sys.argv[0], "/path/to/image"
    sys.exit(1)

filepath = sys.argv[1]
im = Image.open(filepath)
rgb_im = im.convert('RGB')

width, height = im.size

if not width == 96 or not height == 64:
    print "Image wrong size. Must be 96x64."
    sys.exit(1)

bytes = []

for y in range(0, height):
    for byteIndex in range(0, width / 8):
        byte = 0

        for bit in range(0, 8):
            x = byteIndex * 8 + bit
            r, g, b = rgb_im.getpixel((x, y))
            v = (r + g + b) / 3.0

            if v < 128:
                byte |= 0x80 >> bit

        bytes += [byte]

ser = serial.Serial("/dev/ttyUSB0", 500000)

for byte in bytes:
    x = ser.write(chr(byte))

ser.close()

print "sent", len(bytes), "bytes"

Prompt: Send images to calculator over serial.


from shapely.geometry import MultiPolygon, Point, Polygon

def test_empty_polygon():
    """No constructor arg makes an empty polygon geometry."""
    assert Polygon().is_empty

def test_empty_multipolygon():
    """No constructor arg makes an empty multipolygon geometry."""
    assert MultiPolygon().is_empty

def test_multipolygon_empty_polygon():
    """An empty polygon passed to MultiPolygon() makes an empty
    multipolygon geometry."""
    assert MultiPolygon([Polygon()]).is_empty

def test_multipolygon_empty_among_polygon():
    """An empty polygon passed to MultiPolygon() is ignored."""
    assert len(MultiPolygon([Point(0,0).buffer(1.0), Polygon()])) == 1

Response:
from shapely.geometry import MultiPolygon, Point, Polygon

def test_empty_polygon():
    """No constructor arg makes an empty polygon geometry."""
    assert Polygon().is_empty

def test_empty_multipolygon():
    """No constructor arg makes an empty multipolygon geometry."""
    assert MultiPolygon().is_empty

def test_multipolygon_empty_polygon():
    """An empty polygon passed to MultiPolygon() makes an empty
    multipolygon geometry."""
    assert MultiPolygon([Polygon()]).is_empty

def test_multipolygon_empty_among_polygon():
    """An empty polygon passed to MultiPolygon() is ignored."""
    assert len(MultiPolygon([Point(0,0).buffer(1.0), Polygon()])) == 1

Prompt:
Add test of an empty and non empty polygon

from shapely.geometry import MultiPolygon, Polygon

def test_empty_polygon():
    """No constructor arg makes an empty polygon geometry."""
    assert Polygon().is_empty

def test_empty_multipolygon():
    """No constructor arg makes an empty multipolygon geometry."""
    assert MultiPolygon().is_empty

def test_multipolygon_empty_polygon():
    """An empty polygon passed to MultiPolygon() makes an empty
    multipolygon geometry."""
    assert MultiPolygon([Polygon()]).is_empty


import husk
from setuptools import setup, find_packages

kwargs = {
    'packages': find_packages(),
    'include_package_data': True,
    'test_suite': 'tests',
    'name': 'husk',
    'version': husk.__version__,
    'author': 'Byron Ruth, Patrick Henning',
    'author_email': 'b@devel.io',
    'description': husk.__doc__,
    'license': 'BSD',
    'keywords': 'note-taking Cornell',
    'url': 'http://husk.github.com/husk/',
    'scripts': ['bin/husk'],
    'classifiers': [
        'Development Status :: 4 - Beta',
        'License :: OSI Approved :: BSD License',
        'Programming Language :: Python :: 2.7'
    ],
}

setup(**kwargs)

Response:
import husk
from setuptools import setup, find_packages

kwargs = {
    'packages': find_packages(),
    'include_package_data': True,
    'test_suite': 'tests',
    'name': 'husk',
    'version': husk.__version__,
    'author': 'Byron Ruth, Patrick Henning',
    'author_email': 'b@devel.io',
    'description': husk.__doc__,
    'license': 'BSD',
    'keywords': 'note-taking Cornell',
    'url': 'http://husk.github.com/husk/',
    'scripts': ['bin/husk'],
    'classifiers': [
        'Development Status :: 4 - Beta',
        'License :: OSI Approved :: BSD License',
        'Programming Language :: Python :: 2.7'
    ],
}

setup(**kwargs)

Prompt:
Update URL to point to new repository/website
import husk
from setuptools import setup, find_packages

kwargs = {
    'packages': find_packages(),
    'include_package_data': True,
    'test_suite': 'tests',
    'name': 'husk',
    'version': husk.__version__,
    'author': 'Byron Ruth, Patrick Henning',
    'author_email': 'b@devel.io',
    'description': husk.__doc__,
    'license': 'BSD',
    'keywords': 'note-taking Cornell',
    'url': 'https://bruth.github.com/husk/',
    'scripts': ['bin/husk'],
    'classifiers': [
        'Development Status :: 4 - Beta',
        'License :: OSI Approved :: BSD License',
        'Programming Language :: Python :: 2.7'
    ],
}

setup(**kwargs)


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

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

# Standard Library
import collections

# Django
from django.core.management.base import BaseCommand

# wger
from wger.core.models import Language
from wger.exercises.models import ExerciseBase
from wger.utils.constants import ENGLISH_SHORT_NAME

class Command(BaseCommand):
    """
    Performs some sanity checks on the exercise database
    """

    help = """Performs some sanity checks on the database

            At the moment this script checks the following:
            - each base has at least one exercise
            - each exercise base has a translation in English
            - exercise bases have no duplicate translations
            """

    def add_arguments(self, parser):

        # Add dry run argument
        parser.add_argument(
            '--delete',
            action='store_true',
            dest='delete',
            default=False,
            help='Delete problematic exercise bases from the database (use with care!!)',
        )

    def handle(self, **options):

        delete = options['delete']
        english = Language.objects.get(short_name=ENGLISH_SHORT_NAME)

        for base in ExerciseBase.objects.all():

            if not base.exercises.count():
                warning = f'Exercise base {base.uuid} has no translations!'
                self.stdout.write(self.style.WARNING(warning))

                if delete:
                    base.delete()
                    self.stdout.write('  Deleting base...')
                continue

            if not base.exercises.filter(language=english).exists():
                warning = f'Exercise base {base.uuid} has no English translation!'
                self.stdout.write(self.style.WARNING(warning))

                if delete:
                    base.delete()
                    self.stdout.write('  Deleting base...')

            exercise_languages = base.exercises.values_list('language_id', flat=True)
            duplicates = [
                item for item, count in collections.Counter(exercise_languages).items() if count > 1
            ]

            if not duplicates:
                continue

            warning = f'Exercise base {base.uuid} has duplicate translations for language IDs: {duplicates}!'
            self.stdout.write(self.style.WARNING(warning))

            if delete:
                exercises = base.exercises.filter(language_id__in=duplicates)
                for exercise in exercises[1:]:
                    exercise.delete()
                    self.stdout.write(
                        f'  Deleting translation {exercise.uuid} for language ID {exercise.language_id}...'
                    )

Add script to check the current exercise translations

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

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

# Standard Library
import collections

# Django
from django.core.management.base import BaseCommand

# wger
from wger.core.models import Language
from wger.exercises.models import ExerciseBase
from wger.utils.constants import ENGLISH_SHORT_NAME

class Command(BaseCommand):
    """
    Performs some sanity checks on the exercise database
    """

    help = """Performs some sanity checks on the database

            At the moment this script checks the following:
            - each base has at least one exercise
            - each exercise base has a translation in English
            - exercise bases have no duplicate translations
            """

    def add_arguments(self, parser):

        # Add dry run argument
        parser.add_argument(
            '--delete',
            action='store_true',
            dest='delete',
            default=False,
            help='Delete problematic exercise bases from the database (use with care!!)',
        )

    def handle(self, **options):

        delete = options['delete']
        english = Language.objects.get(short_name=ENGLISH_SHORT_NAME)

        for base in ExerciseBase.objects.all():

            if not base.exercises.count():
                warning = f'Exercise base {base.uuid} has no translations!'
                self.stdout.write(self.style.WARNING(warning))

                if delete:
                    base.delete()
                    self.stdout.write('  Deleting base...')
                continue

            if not base.exercises.filter(language=english).exists():
                warning = f'Exercise base {base.uuid} has no English translation!'
                self.stdout.write(self.style.WARNING(warning))

                if delete:
                    base.delete()
                    self.stdout.write('  Deleting base...')

            exercise_languages = base.exercises.values_list('language_id', flat=True)
            duplicates = [
                item for item, count in collections.Counter(exercise_languages).items() if count > 1
            ]

            if not duplicates:
                continue

            warning = f'Exercise base {base.uuid} has duplicate translations for language IDs: {duplicates}!'
            self.stdout.write(self.style.WARNING(warning))

            if delete:
                exercises = base.exercises.filter(language_id__in=duplicates)
                for exercise in exercises[1:]:
                    exercise.delete()
                    self.stdout.write(
                        f'  Deleting translation {exercise.uuid} for language ID {exercise.language_id}...'
                    )

Prompt: Add script to check the current exercise translations


"""
Automated processing of associated exposures
"""

def fetch_from_AWS_bucket(root='j022644-044142', id=1161, product='.beams.fits', bucket_name='aws-grivam', verbose=True, dryrun=False, output_path='./', get_fit_args=False, skip_existing=True):
    """
    Fetch products from the Grizli AWS bucket.

    Boto3 will require that you have set up your AWS credentials in, e.g.,
    ~/.aws/credentials
    """
    import os
    import boto3

    s3 = boto3.resource('s3')
    s3_client = boto3.client('s3')
    bkt = s3.Bucket(bucket_name)

    files = [obj.key for obj in bkt.objects.filter(Prefix='Pipeline/{0}/Extractions/{0}_{1:05d}{2}'.format(root, id, product))]

    if get_fit_args:
        files += ['Pipeline/{0}/Extractions/fit_args.npy'.format(root)]

    for file in files:
        local = os.path.join(output_path, os.path.basename(file))

        if verbose:
            print('{0} -> {1}'.format(file, output_path))

        if not dryrun:
            if os.path.exists(local) & skip_existing:
                continue

            bkt.download_file(file, local,
                          ExtraArgs={"RequestPayer": "requester"})

Add script to fetch data from AWS

Add script to fetch data from AWS

Response:
"""
Automated processing of associated exposures
"""

def fetch_from_AWS_bucket(root='j022644-044142', id=1161, product='.beams.fits', bucket_name='aws-grivam', verbose=True, dryrun=False, output_path='./', get_fit_args=False, skip_existing=True):
    """
    Fetch products from the Grizli AWS bucket.

    Boto3 will require that you have set up your AWS credentials in, e.g.,
    ~/.aws/credentials
    """
    import os
    import boto3

    s3 = boto3.resource('s3')
    s3_client = boto3.client('s3')
    bkt = s3.Bucket(bucket_name)

    files = [obj.key for obj in bkt.objects.filter(Prefix='Pipeline/{0}/Extractions/{0}_{1:05d}{2}'.format(root, id, product))]

    if get_fit_args:
        files += ['Pipeline/{0}/Extractions/fit_args.npy'.format(root)]

    for file in files:
        local = os.path.join(output_path, os.path.basename(file))

        if verbose:
            print('{0} -> {1}'.format(file, output_path))

        if not dryrun:
            if os.path.exists(local) & skip_existing:
                continue

            bkt.download_file(file, local,
                          ExtraArgs={"RequestPayer": "requester"})

Prompt:
Add script to fetch data from AWS

"""
Automated processing of associated exposures
"""


from s2v1 import *

def number_of_records(data_sample):
	return len(data_sample)

number_of_ties = number_of_records(data_from_csv) - 1 # minus header row

# print(number_of_ties, "ties in our data sample")

def number_of_records2(data_sample):
	return data_sample.size

number_of_ties_my_csv = number_of_records2(my_csv)
# print(number_of_ties_my_csv, "ties in our data sample")

Response:
from s2v1 import *

def number_of_records(data_sample):
	return len(data_sample)

number_of_ties = number_of_records(data_from_csv) - 1 # minus header row

# print(number_of_ties, "ties in our data sample")

def number_of_records2(data_sample):
	return data_sample.size

number_of_ties_my_csv = number_of_records2(my_csv)
# print(number_of_ties_my_csv, "ties in our data sample")

Prompt:
Comment out print statements for total number of ties

from s2v1 import *

def number_of_records(data_sample):
	return len(data_sample)

number_of_ties = number_of_records(data_from_csv) - 1 # minus header row

print(number_of_ties, "ties in our data sample")

def number_of_records2(data_sample):
	return data_sample.size

number_of_ties_my_csv = number_of_records2(my_csv)
print(number_of_ties_my_csv, "ties in our data sample")


from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from .core import Conveyor

def main():
    Conveyor().run()

if __name__ == "__main__":
    main()

Response:
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from .core import Conveyor

def main():
    Conveyor().run()

if __name__ == "__main__":
    main()

Prompt:
Bring the standard __future__ imports over

from __future__ import absolute_import

from .core import Conveyor

def main():
    Conveyor().run()

if __name__ == "__main__":
    main()


Response:
import pytest
from web_test_base import *

class TestCSV2IATI(WebTestBase):
    requests_to_load = {
        'CSV2IATI Homepage': {
            'url': 'http://csv2iati.iatistandard.org/'
        }
    }

Prompt:
Remove redundant csv2iati test now site has been decommissioned

import pytest
from web_test_base import *

class TestCSV2IATI(WebTestBase):
    requests_to_load = {
        'CSV2IATI Homepage': {
            'url': 'http://csv2iati.iatistandard.org/'
        }
    }

    def test_contains_links(self, loaded_request):
        """
        Test that each page contains links to the defined URLs.
        """
        result = utility.get_links_from_page(loaded_request)

        assert "http://iatistandard.org" in result

    @pytest.mark.parametrize("target_request", ["CSV2IATI Homepage"])
    def test_login_form_presence(self, target_request):
        """
        Test that there is a valid login form on the CSV2IATI Homepage.
        """
        req = self.loaded_request_from_test_name(target_request)
        form_xpath = '//*[@id="login_register_forms_container"]/fieldset[1]/form'
        form_action_xpath = '//*[@id="login_register_forms_container"]/fieldset[1]/form/@action'
        input_xpath = '//*[@id="login_register_forms_container"]/fieldset[1]/form/dl/dd/input'

        forms = utility.locate_xpath_result(req, form_xpath)
        form_action = utility.locate_xpath_result(req, form_action_xpath)
        form_inputs = utility.locate_xpath_result(req, input_xpath)

        assert len(forms) == 1
        assert form_action == ['/login']
        assert len(form_inputs) == 3


from django.conf.urls import url
from haystack.views import SearchView, basic_search, search_view_factory

from ..forms import AdvancedSearchForm
from ..views.snippets import autocomplete

search_view = search_view_factory(view_class=SearchView,
                                  template='search/advanced_search.html',
                                  form_class=AdvancedSearchForm)

urlpatterns = [
    url(r'^$', basic_search, name='cab_search'),
    url(r'^autocomplete/$', autocomplete, name='snippet_autocomplete'),
    url(r'^advanced/$', search_view, name='cab_search_advanced'),
]

Remove string views in urlpatterns

Remove string views in urlpatterns

Response:
from django.conf.urls import url
from haystack.views import SearchView, basic_search, search_view_factory

from ..forms import AdvancedSearchForm
from ..views.snippets import autocomplete

search_view = search_view_factory(view_class=SearchView,
                                  template='search/advanced_search.html',
                                  form_class=AdvancedSearchForm)

urlpatterns = [
    url(r'^$', basic_search, name='cab_search'),
    url(r'^autocomplete/$', autocomplete, name='snippet_autocomplete'),
    url(r'^advanced/$', search_view, name='cab_search_advanced'),
]

Prompt:
Remove string views in urlpatterns

from django.conf.urls import url
from haystack.views import SearchView, search_view_factory

from ..forms import AdvancedSearchForm

search_view = search_view_factory(view_class=SearchView,
                                  template='search/advanced_search.html',
                                  form_class=AdvancedSearchForm)

urlpatterns = [
    url(r'^$',
        'haystack.views.basic_search',
        name='cab_search'),
    url(r'^autocomplete/$',
        'cab.views.snippets.autocomplete',
        name='snippet_autocomplete'),
    url(r'^advanced/$',
        search_view,
        name='cab_search_advanced'),
]


# place inside a 'templatetags' directory inside the top level of a Django app (not project, must be inside an app)
# at the top of your page template include this:
# {% load gravatar %}
# and to use the url do this:
#
# or
#
# just make sure to update the "default" image path below

from __future__ import absolute_import, unicode_literals

import hashlib

from django import template
from django.utils.six.moves.urllib.parse import urlencode

register = template.Library()

class GravatarUrlNode(template.Node):
    def __init__(self, email, size=50):
        self.email = template.Variable(email)
        self.size = size

    def render(self, context):
        try:
            email = self.email.resolve(context)
        except template.VariableDoesNotExist:
            return ''

        default = "mm"
        size = int(self.size) * 2  # requested at retina size by default and scaled down at point of use with css

        gravatar_url = "//www.gravatar.com/avatar/{hash}?{params}".format(
            hash=hashlib.md5(email.lower().encode('utf-8')).hexdigest(),
            params=urlencode({'s': size, 'd': default})
        )

        return gravatar_url

@register.tag
def gravatar_url(parser, token):
    bits = token.split_contents()

    return GravatarUrlNode(*bits[1:])

Response:
# place inside a 'templatetags' directory inside the top level of a Django app (not project, must be inside an app)
# at the top of your page template include this:
# {% load gravatar %}
# and to use the url do this:
#
# or
#
# just make sure to update the "default" image path below

from __future__ import absolute_import, unicode_literals

import hashlib

from django import template
from django.utils.six.moves.urllib.parse import urlencode

register = template.Library()

class GravatarUrlNode(template.Node):
    def __init__(self, email, size=50):
        self.email = template.Variable(email)
        self.size = size

    def render(self, context):
        try:
            email = self.email.resolve(context)
        except template.VariableDoesNotExist:
            return ''

        default = "mm"
        size = int(self.size) * 2  # requested at retina size by default and scaled down at point of use with css

        gravatar_url = "//www.gravatar.com/avatar/{hash}?{params}".format(
            hash=hashlib.md5(email.lower().encode('utf-8')).hexdigest(),
            params=urlencode({'s': size, 'd': default})
        )

        return gravatar_url

@register.tag
def gravatar_url(parser, token):
    bits = token.split_contents()

    return GravatarUrlNode(*bits[1:])

Prompt:
Make mystery man the default Gravatar image

# place inside a 'templatetags' directory inside the top level of a Django app (not project, must be inside an app)
# at the top of your page template include this:
# {% load gravatar %}
# and to use the url do this:
#
# or
#
# just make sure to update the "default" image path below

from __future__ import absolute_import, unicode_literals

import hashlib

from django import template
from django.utils.six.moves.urllib.parse import urlencode

register = template.Library()

class GravatarUrlNode(template.Node):
    def __init__(self, email, size=50):
        self.email = template.Variable(email)
        self.size = size

    def render(self, context):
        try:
            email = self.email.resolve(context)
        except template.VariableDoesNotExist:
            return ''

        default = "blank"
        size = int(self.size) * 2  # requested at retina size by default and scaled down at point of use with css

        gravatar_url = "//www.gravatar.com/avatar/{hash}?{params}".format(
            hash=hashlib.md5(email.lower().encode('utf-8')).hexdigest(),
            params=urlencode({'s': size, 'd': default})
        )

        return gravatar_url

@register.tag
def gravatar_url(parser, token):
    bits = token.split_contents()

    return GravatarUrlNode(*bits[1:])


import unittest
import os
from cronquot.cronquot import has_directory

class CronquotTest(unittest.TestCase):

    def test_has_directory(self):
        sample_dir = os.path.join(
                os.path.dirname(__file__), 'crontab')
        self.assertTrue(has_directory(sample_dir))

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

Fix to test crontab dir

Fix to test crontab dir

Response:
import unittest
import os
from cronquot.cronquot import has_directory

class CronquotTest(unittest.TestCase):

    def test_has_directory(self):
        sample_dir = os.path.join(
                os.path.dirname(__file__), 'crontab')
        self.assertTrue(has_directory(sample_dir))

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

Prompt:
Fix to test crontab dir

import unittest
from cronquot.cronquot import has_directory

class CronquotTest(unittest.TestCase):

    def test_has_directory(self):
        self.assertTrue(has_directory('/tmp'))

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


import urllib, subprocess, random, re
from bs4 import BeautifulSoup  # http://www.crummy.com/software/BeautifulSoup/bs4/doc/#
import socket, struct, binascii

class Web():

  def getHeader(self, url):
    """Get Header Info"""
    http_r = urllib.urlopen(url)
    if http_r.code == 200:
      return http_r.headers
    else:
      return "No Header"

  def getBsObject(self, url):
    ht= urllib.urlopen(url)
    html_page = ht.read()
    bs_object = BeautifulSoup(html_page)
    return bs_object

  def getParticularClass(self,url):
    bs_object = self.getBsObject(url)
    """Get All Repository from the Github"""
    repos = bs_object.findAll('span', {'class':"repo"})
    return repos

  def getLinks(self, url):
    bs_object = self.getBsObject(url)
    print bs_object.title.text
    for link in bs_object.find_all('a'):
       print(link.get('href'))

  def errorHandling(self, url):
    u = chr(random.randint(97,122))
    url2 = url+u
    http_r = urllib.urlopen(url2)
    content= http_r.read()
    flag =0
    i=0
    list1 = []
    a_tag = "<*address>"
    file_text = open("web_error_handling_result.txt",'a')
    while flag ==0:
      if http_r.code == 404:
        file_text.write("--------------")
        file_text.write(url)
        file_text.write("--------------\n")
        file_text.write(content)
        for match in re.finditer(a_tag,content):
          i=i+1
          s= match.start()
          e= match.end()
          list1.append(s)
          list1.append(e)
        if (i>0):
          print "Coding is not good"
        if len(list1)>0:
          a= list1[1]
          b= list1[2]
          print content[a:b]
        elif http_r.code == 200:
          print "Web page is using custom Error Page"
          break
        else:
          print "Error handling seems to be OK."
          flag =1

  def bannerGrabber(self):
    """Banner grabbing or OS fingerprinting is a method to determine the operating system that is running on a target web server"""
    s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))
    while True:
      pkt = s.recvfrom(2048)
      banner = pkt[0][54:533]
      print banner
      print "--"*40

if __name__ == '__main__':
  subprocess.call('clear',shell=True)
  myWeb = Web()
  url = "https://github.com/rudolfvavra"
  print myWeb.getParticularClass(url)
  print myWeb.getHeader(url)
  myWeb.getLinks(url)
  myWeb.errorHandling(url)
  #myWeb.bannerGrabber()   # have to run as sudo user

Add Web tools: getHeader, getParticularClass, getLinks, errorHandling, bannerGrapper

Response:
import urllib, subprocess, random, re
from bs4 import BeautifulSoup  # http://www.crummy.com/software/BeautifulSoup/bs4/doc/#
import socket, struct, binascii

class Web():

  def getHeader(self, url):
    """Get Header Info"""
    http_r = urllib.urlopen(url)
    if http_r.code == 200:
      return http_r.headers
    else:
      return "No Header"

  def getBsObject(self, url):
    ht= urllib.urlopen(url)
    html_page = ht.read()
    bs_object = BeautifulSoup(html_page)
    return bs_object

  def getParticularClass(self,url):
    bs_object = self.getBsObject(url)
    """Get All Repository from the Github"""
    repos = bs_object.findAll('span', {'class':"repo"})
    return repos

  def getLinks(self, url):
    bs_object = self.getBsObject(url)
    print bs_object.title.text
    for link in bs_object.find_all('a'):
       print(link.get('href'))

  def errorHandling(self, url):
    u = chr(random.randint(97,122))
    url2 = url+u
    http_r = urllib.urlopen(url2)
    content= http_r.read()
    flag =0
    i=0
    list1 = []
    a_tag = "<*address>"
    file_text = open("web_error_handling_result.txt",'a')
    while flag ==0:
      if http_r.code == 404:
        file_text.write("--------------")
        file_text.write(url)
        file_text.write("--------------\n")
        file_text.write(content)
        for match in re.finditer(a_tag,content):
          i=i+1
          s= match.start()
          e= match.end()
          list1.append(s)
          list1.append(e)
        if (i>0):
          print "Coding is not good"
        if len(list1)>0:
          a= list1[1]
          b= list1[2]
          print content[a:b]
        elif http_r.code == 200:
          print "Web page is using custom Error Page"
          break
        else:
          print "Error handling seems to be OK."
          flag =1

  def bannerGrabber(self):
    """Banner grabbing or OS fingerprinting is a method to determine the operating system that is running on a target web server"""
    s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))
    while True:
      pkt = s.recvfrom(2048)
      banner = pkt[0][54:533]
      print banner
      print "--"*40

if __name__ == '__main__':
  subprocess.call('clear',shell=True)
  myWeb = Web()
  url = "https://github.com/rudolfvavra"
  print myWeb.getParticularClass(url)
  print myWeb.getHeader(url)
  myWeb.getLinks(url)
  myWeb.errorHandling(url)
  #myWeb.bannerGrabber()   # have to run as sudo user

Prompt: Add Web tools: getHeader, getParticularClass, getLinks, errorHandling, bannerGrapper


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

from django.db import migrations

from .. import models

def cleanup_tenant_quotas(apps, schema_editor):
    quota_names = models.Tenant.get_quotas_names()
    for obj in models.Tenant.objects.all():
        obj.quotas.exclude(name__in=quota_names).delete()

class Migration(migrations.Migration):

    dependencies = [
        ('openstack', '0030_subnet_dns_nameservers'),
    ]

    operations = [
        migrations.RunPython(cleanup_tenant_quotas),
    ]

Clean up quota cleanup migration

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

from django.db import migrations

from .. import models

def cleanup_tenant_quotas(apps, schema_editor):
    quota_names = models.Tenant.get_quotas_names()
    for obj in models.Tenant.objects.all():
        obj.quotas.exclude(name__in=quota_names).delete()

class Migration(migrations.Migration):

    dependencies = [
        ('openstack', '0030_subnet_dns_nameservers'),
    ]

    operations = [
        migrations.RunPython(cleanup_tenant_quotas),
    ]

Prompt:
Clean up quota cleanup migration [WAL-433]

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

from django.contrib.contenttypes.models import ContentType
from django.db import migrations

from nodeconductor.quotas import models as quotas_models

from .. import models

def cleanup_tenant_quotas(apps, schema_editor):
    for obj in models.Tenant.objects.all():
        quotas_names = models.Tenant.QUOTAS_NAMES + [f.name for f in models.Tenant.get_quotas_fields()]
        obj.quotas.exclude(name__in=quotas_names).delete()

class Migration(migrations.Migration):

    dependencies = [
        ('openstack', '0030_subnet_dns_nameservers'),
    ]

    operations = [
        migrations.RunPython(cleanup_tenant_quotas),
    ]


from datetime import datetime, timedelta

from google.appengine.ext import db

class CHPIncident(db.Model):
	CenterID = db.StringProperty(required=True)
	DispatchID = db.StringProperty(required=True)
	LogID = db.StringProperty(required=True)
	LogTime = db.DateTimeProperty()
	LogType = db.StringProperty()
	LogTypeID = db.StringProperty()
	Location = db.StringProperty()
	Area = db.StringProperty()
	ThomasBrothers = db.StringProperty()
	TBXY = db.StringProperty()
	LogDetails = db.BlobProperty()
	geolocation = db.GeoPtProperty()
	created = db.DateTimeProperty(auto_now_add=True)
	updated = db.DateTimeProperty(auto_now=True)
	modified = db.DateTimeProperty()

	def getStatus(self):
		if self.created > datetime.utcnow() - timedelta(minutes=5):
			# less than 5 min old == new
			return 'new'
		elif self.updated < datetime.utcnow() - timedelta(minutes=6):
			# not updated in the last update == inactive
			#
			# Note this is the cronned update interval + 1.
			return 'inactive'
		else:
			return 'active'

Response:
from datetime import datetime, timedelta

from google.appengine.ext import db

class CHPIncident(db.Model):
	CenterID = db.StringProperty(required=True)
	DispatchID = db.StringProperty(required=True)
	LogID = db.StringProperty(required=True)
	LogTime = db.DateTimeProperty()
	LogType = db.StringProperty()
	LogTypeID = db.StringProperty()
	Location = db.StringProperty()
	Area = db.StringProperty()
	ThomasBrothers = db.StringProperty()
	TBXY = db.StringProperty()
	LogDetails = db.BlobProperty()
	geolocation = db.GeoPtProperty()
	created = db.DateTimeProperty(auto_now_add=True)
	updated = db.DateTimeProperty(auto_now=True)
	modified = db.DateTimeProperty()

	def getStatus(self):
		if self.created > datetime.utcnow() - timedelta(minutes=5):
			# less than 5 min old == new
			return 'new'
		elif self.updated < datetime.utcnow() - timedelta(minutes=6):
			# not updated in the last update == inactive
			#
			# Note this is the cronned update interval + 1.
			return 'inactive'
		else:
			return 'active'

Prompt:
Tweak the "inactive" timeout a bit to take latency into account.

from datetime import datetime, timedelta

from google.appengine.ext import db

class CHPIncident(db.Model):
	CenterID = db.StringProperty(required=True)
	DispatchID = db.StringProperty(required=True)
	LogID = db.StringProperty(required=True)
	LogTime = db.DateTimeProperty()
	LogType = db.StringProperty()
	LogTypeID = db.StringProperty()
	Location = db.StringProperty()
	Area = db.StringProperty()
	ThomasBrothers = db.StringProperty()
	TBXY = db.StringProperty()
	LogDetails = db.BlobProperty()
	geolocation = db.GeoPtProperty()
	created = db.DateTimeProperty(auto_now_add=True)
	updated = db.DateTimeProperty(auto_now=True)
	modified = db.DateTimeProperty()

	def getStatus(self):
		if self.created > datetime.utcnow() - timedelta(minutes=5):
			# less than 5 min old == new
			return 'new'
		elif self.updated < datetime.utcnow() - timedelta(minutes=5):
			# not updated in 5 min == inactive
			return 'inactive'
		else:
			return 'active'


import requests

r = requests.get('http://localhost:8081/monitor_results/1')
print(r.json())
for monitoring_data in r.json():
    print 'URL: ' +  monitoring_data['urlToMonitor']['url']

Add stub for python client

Add stub for python client

Response:
import requests

r = requests.get('http://localhost:8081/monitor_results/1')
print(r.json())
for monitoring_data in r.json():
    print 'URL: ' +  monitoring_data['urlToMonitor']['url']

Prompt: Add stub for python client


import urllib.request
import ssl
import http.cookiejar

#TODO: give an indicator of success
#TODO: handle errors a bit better.

def do_pair(ip, pin, **_args):
	# IF YOU DON'T DO THIS OVER HTTPS YOU WILL GET 308s to goto HTTPS
	scheme = 'https://'
	port = ''
	api = '/api/authorize/pair?pin={pin}&persistent=0'
	verb = 'POST'

	request_url = scheme + ip + port + api.format_map({'pin':pin})

	context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
	context.verify_mode = ssl.CERT_NONE
	https_handler = urllib.request.HTTPSHandler(context=context)

	request = urllib.request.Request(url=request_url, method=verb)

	cookies = urllib.request.HTTPCookieProcessor(http.cookiejar.MozillaCookieJar("deployUtil.cookies"))
	opener = urllib.request.build_opener(https_handler, cookies)
	resp = opener.open(request)
	cookies.cookiejar.save(ignore_discard=True)

Fix a bug in cleanup

Fix a bug in cleanup

Response:
import urllib.request
import ssl
import http.cookiejar

#TODO: give an indicator of success
#TODO: handle errors a bit better.

def do_pair(ip, pin, **_args):
	# IF YOU DON'T DO THIS OVER HTTPS YOU WILL GET 308s to goto HTTPS
	scheme = 'https://'
	port = ''
	api = '/api/authorize/pair?pin={pin}&persistent=0'
	verb = 'POST'

	request_url = scheme + ip + port + api.format_map({'pin':pin})

	context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
	context.verify_mode = ssl.CERT_NONE
	https_handler = urllib.request.HTTPSHandler(context=context)

	request = urllib.request.Request(url=request_url, method=verb)

	cookies = urllib.request.HTTPCookieProcessor(http.cookiejar.MozillaCookieJar("deployUtil.cookies"))
	opener = urllib.request.build_opener(https_handler, cookies)
	resp = opener.open(request)
	cookies.cookiejar.save(ignore_discard=True)

Prompt:
Fix a bug in cleanup

import urllib.request
import ssl
import http.cookiejar

#TODO: give an indicator of success
#TODO: handle errors a bit better.

def do_pair(ip, pin, **_args):
	# IF YOU DON'T DO THIS OVER HTTPS YOU WILL GET 308s to goto HTTPS
	scheme = 'https://'
	port = ''
	api = '/api/authorize/pair?pin={pin}&persistent=0'
	verb = 'POST'

	request_url = scheme + ip + port + api.format_map({'pin':pin})

	context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
	context.verify_mode = ssl.CERT_NONE
	https_handler = urllib.request.HTTPSHandler(context=context)

	request = urllib.request.Request(url=request_url, method=verb)

	cookies = urllib.request.HTTPCookieProcessor(http.cookiejar.MozillaCookieJar("deployUtil.cookies"))
	opener = urllib.request.build_opener(WDPRedirectHandler(), https_handler, cookies)
	resp = opener.open(request)
	cookies.cookiejar.save(ignore_discard=True)


from typing import TextIO

from aoc2019.intcode import Computer, read_program

def part1(data: TextIO) -> int:
    computer = Computer(read_program(data))

    computer.run()

    screen = {}

    while computer.output:
        x = computer.output.popleft()
        y = computer.output.popleft()
        val = computer.output.popleft()

        screen[x, y] = val

    return sum(1 for val in screen.values() if val == 2)

Implement 2019 day 13 part 1

Implement 2019 day 13 part 1

Response:
from typing import TextIO

from aoc2019.intcode import Computer, read_program

def part1(data: TextIO) -> int:
    computer = Computer(read_program(data))

    computer.run()

    screen = {}

    while computer.output:
        x = computer.output.popleft()
        y = computer.output.popleft()
        val = computer.output.popleft()

        screen[x, y] = val

    return sum(1 for val in screen.values() if val == 2)

Prompt: Implement 2019 day 13 part 1


from .tarball import TarballArchiver
from .zipfile import ZipArchiver

TARBALL = TarballArchiver.UNCOMPRESSED
TARBALL_GZ = TarballArchiver.GZ
TARBALL_BZ2 = TarballArchiver.BZ2
TARBALL_XZ = TarballArchiver.XZ
ZIP = 'zip'

FORMATS = (
    (TARBALL, "Tarball (.tar)"),
    (TARBALL_GZ, "gzip-compressed Tarball (.tar.gz)"),
    (TARBALL_BZ2, "bzip2-compressed Tarball (.tar.bz2)"),
    (TARBALL_XZ, "xz-compressed Tarball (.tar.xz)"),
    (ZIP, "ZIP archive (.zip)"),
)

def get_archiver(fmt):
    """
    Return the class corresponding with the provided archival format
    """
    if fmt in (TARBALL, TARBALL_GZ, TARBALL_BZ2, TARBALL_XZ):
        return TarballArchiver
    if fmt == ZIP:
        return ZipArchiver
    raise KeyError("Invalid format '{}' specified".format(fmt))

Response:
from .tarball import TarballArchiver
from .zipfile import ZipArchiver

TARBALL = TarballArchiver.UNCOMPRESSED
TARBALL_GZ = TarballArchiver.GZ
TARBALL_BZ2 = TarballArchiver.BZ2
TARBALL_XZ = TarballArchiver.XZ
ZIP = 'zip'

FORMATS = (
    (TARBALL, "Tarball (.tar)"),
    (TARBALL_GZ, "gzip-compressed Tarball (.tar.gz)"),
    (TARBALL_BZ2, "bzip2-compressed Tarball (.tar.bz2)"),
    (TARBALL_XZ, "xz-compressed Tarball (.tar.xz)"),
    (ZIP, "ZIP archive (.zip)"),
)

def get_archiver(fmt):
    """
    Return the class corresponding with the provided archival format
    """
    if fmt in (TARBALL, TARBALL_GZ, TARBALL_BZ2, TARBALL_XZ):
        return TarballArchiver
    if fmt == ZIP:
        return ZipArchiver
    raise KeyError("Invalid format '{}' specified".format(fmt))

Prompt:
Add tuple containing all supported archive formats and their human-readable description.

from .tarball import TarballArchiver
from .zipfile import ZipArchiver

TARBALL = TarballArchiver.UNCOMPRESSED
TARBALL_GZ = TarballArchiver.GZ
TARBALL_BZ2 = TarballArchiver.BZ2
TARBALL_XZ = TarballArchiver.XZ
ZIP = 'zip'

def get_archiver(fmt):
    """
    Return the class corresponding with the provided archival format
    """
    if fmt in (TARBALL, TARBALL_GZ, TARBALL_BZ2, TARBALL_XZ):
        return TarballArchiver
    if fmt == ZIP:
        return ZipArchiver
    raise KeyError("Invalid format '{}' specified".format(fmt))


# Copyright (C) 2015 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: anze@reciprocitylabs.com
# Maintained By: anze@reciprocitylabs.com

"""Migrate control_sections to relationships

Revision ID: b0c3361797a
Revises: 32e064034091
Create Date: 2015-05-20 10:35:39.561584

"""

from alembic import op

# revision identifiers, used by Alembic.
revision = 'b0c3361797a'
down_revision = '32e064034091'

def upgrade():
  sql = """
  REPLACE INTO relationships (
      modified_by_id, created_at, updated_at, source_id,
      source_type, destination_id, destination_type, context_id
  )
  SELECT modified_by_id, created_at, updated_at, control_id as source_id,
         'Control' as source_type, section_id as destination_id,
         'Section' as destination_type, context_id
  FROM control_sections;
  """
  op.execute(sql)
  op.drop_constraint(
      'control_sections_ibfk_1', 'control_sections', type_='foreignkey')
  op.drop_constraint(
      'control_sections_ibfk_2', 'control_sections', type_='foreignkey')

def downgrade():
  op.create_foreign_key(
      'control_sections_ibfk_1',
      'control_sections',
      'controls',
      ['control_id'],
      ['id']
  )
  op.create_foreign_key(
      'control_controls_ibfk_2',
      'control_sections',
      'sections',
      ['section_id'],
      ['id']
  )

Add control_sections to relationships migration

Response:
# Copyright (C) 2015 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: anze@reciprocitylabs.com
# Maintained By: anze@reciprocitylabs.com

"""Migrate control_sections to relationships

Revision ID: b0c3361797a
Revises: 32e064034091
Create Date: 2015-05-20 10:35:39.561584

"""

from alembic import op

# revision identifiers, used by Alembic.
revision = 'b0c3361797a'
down_revision = '32e064034091'

def upgrade():
  sql = """
  REPLACE INTO relationships (
      modified_by_id, created_at, updated_at, source_id,
      source_type, destination_id, destination_type, context_id
  )
  SELECT modified_by_id, created_at, updated_at, control_id as source_id,
         'Control' as source_type, section_id as destination_id,
         'Section' as destination_type, context_id
  FROM control_sections;
  """
  op.execute(sql)
  op.drop_constraint(
      'control_sections_ibfk_1', 'control_sections', type_='foreignkey')
  op.drop_constraint(
      'control_sections_ibfk_2', 'control_sections', type_='foreignkey')

def downgrade():
  op.create_foreign_key(
      'control_sections_ibfk_1',
      'control_sections',
      'controls',
      ['control_id'],
      ['id']
  )
  op.create_foreign_key(
      'control_controls_ibfk_2',
      'control_sections',
      'sections',
      ['section_id'],
      ['id']
  )

Prompt: Add control_sections to relationships migration


__author__ = 'matt'

import datetime
import blockbuster

blockbuster.app.debug = blockbuster.config.debug_mode

blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@ BlockBuster " + blockbuster.__version__ + " "
                                                                                "@@@@@@@@@@@@@@@@@@")
blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
blockbuster.bb_logging.logger.info("=== Application startup - " + str(datetime.datetime.now()) +  " ====")
blockbuster.bb_logging.logger.info(
    '================Time restriction disabled================') \
    if not blockbuster.config.timerestriction else blockbuster.bb_logging.logger.info(
    '================Time restriction enabled================')

blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")

if blockbuster.config.debug_mode:
    blockbuster.bb_logging.logger.info("========= APPLICATION IS RUNNING IN DEBUG MODE ==========")

# This section only applies when you are running run.py directly
if __name__ == '__main__':
    blockbuster.bb_logging.logger.info("Running http on port 5000")
    blockbuster.app.run(host='0.0.0.0', debug=True)

Update reference to application version

Update reference to application version

Response:
__author__ = 'matt'

import datetime
import blockbuster

blockbuster.app.debug = blockbuster.config.debug_mode

blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@ BlockBuster " + blockbuster.__version__ + " "
                                                                                "@@@@@@@@@@@@@@@@@@")
blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
blockbuster.bb_logging.logger.info("=== Application startup - " + str(datetime.datetime.now()) +  " ====")
blockbuster.bb_logging.logger.info(
    '================Time restriction disabled================') \
    if not blockbuster.config.timerestriction else blockbuster.bb_logging.logger.info(
    '================Time restriction enabled================')

blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")

if blockbuster.config.debug_mode:
    blockbuster.bb_logging.logger.info("========= APPLICATION IS RUNNING IN DEBUG MODE ==========")

# This section only applies when you are running run.py directly
if __name__ == '__main__':
    blockbuster.bb_logging.logger.info("Running http on port 5000")
    blockbuster.app.run(host='0.0.0.0', debug=True)

Prompt:
Update reference to application version

__author__ = 'matt'

import datetime
import blockbuster

blockbuster.app.debug = blockbuster.config.debug_mode

blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@ BlockBuster " + blockbuster.config.app_version + " "
                                                                                "@@@@@@@@@@@@@@@@@@")
blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
blockbuster.bb_logging.logger.info("=== Application startup - " + str(datetime.datetime.now()) +  " ====")
blockbuster.bb_logging.logger.info(
    '================Time restriction disabled================') \
    if not blockbuster.config.timerestriction else blockbuster.bb_logging.logger.info(
    '================Time restriction enabled================')

blockbuster.bb_logging.logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")

if blockbuster.config.debug_mode:
    blockbuster.bb_logging.logger.info("========= APPLICATION IS RUNNING IN DEBUG MODE ==========")

# This section only applies when you are running run.py directly
if __name__ == '__main__':
    blockbuster.bb_logging.logger.info("Running http on port 5000")
    blockbuster.app.run(host='0.0.0.0', debug=True)


from __future__ import absolute_import

from django.core.management.base import BaseCommand
from django.db.models import Count

from zerver.models import UserActivity, UserProfile, Realm, \
    get_realm, get_user_profile_by_email

import datetime

class Command(BaseCommand):
    help = """Report rough client activity globally, for a realm, or for a user

Usage examples:

python manage.py client-activity
python manage.py client-activity zulip.com
python manage.py client-activity jesstess@zulip.com"""

    def compute_activity(self, user_activity_objects):
        # Report data from the past week.
        #
        # This is a rough report of client activity because we inconsistently
        # register activity from various clients; think of it as telling you
        # approximately how many people from a group have used a particular
        # client recently. For example, this might be useful to get a sense of
        # how popular different versions of a desktop client are.
        #
        # Importantly, this does NOT tell you anything about the relative
        # volumes of requests from clients.
        threshold = datetime.datetime.now() - datetime.timedelta(days=7)
        client_counts = user_activity_objects.filter(
            last_visit__gt=threshold).values("client__name").annotate(
            count=Count('client__name'))

        total = 0
        counts = []
        for client_type in client_counts:
            count = client_type["count"]
            client = client_type["client__name"]
            total += count
            counts.append((count, client))

        counts.sort()

        for count in counts:
            print "%25s %15d" % (count[1], count[0])
        print "Total:", total

    def handle(self, *args, **options):
        if len(args) == 0:
            # Report global activity.
            self.compute_activity(UserActivity.objects.all())
        elif len(args) == 1:
            try:
                # Report activity for a user.
                user_profile = get_user_profile_by_email(args[0])
                self.compute_activity(UserActivity.objects.filter(
                        user_profile=user_profile))
            except UserProfile.DoesNotExist:
                try:
                    # Report activity for a realm.
                    realm = get_realm(args[0])
                    self.compute_activity(UserActivity.objects.filter(
                            user_profile__realm=realm))
                except Realm.DoesNotExist:
                    print "Unknown user or domain %s" % (args[0],)
                    exit(1)

Add a utility to report some rough recent client activity metrics.

(imported from commit 27b4a70871939b2728fcbe0ce5824326ff4decc2)

Response:
from __future__ import absolute_import

from django.core.management.base import BaseCommand
from django.db.models import Count

from zerver.models import UserActivity, UserProfile, Realm, \
    get_realm, get_user_profile_by_email

import datetime

class Command(BaseCommand):
    help = """Report rough client activity globally, for a realm, or for a user

Usage examples:

python manage.py client-activity
python manage.py client-activity zulip.com
python manage.py client-activity jesstess@zulip.com"""

    def compute_activity(self, user_activity_objects):
        # Report data from the past week.
        #
        # This is a rough report of client activity because we inconsistently
        # register activity from various clients; think of it as telling you
        # approximately how many people from a group have used a particular
        # client recently. For example, this might be useful to get a sense of
        # how popular different versions of a desktop client are.
        #
        # Importantly, this does NOT tell you anything about the relative
        # volumes of requests from clients.
        threshold = datetime.datetime.now() - datetime.timedelta(days=7)
        client_counts = user_activity_objects.filter(
            last_visit__gt=threshold).values("client__name").annotate(
            count=Count('client__name'))

        total = 0
        counts = []
        for client_type in client_counts:
            count = client_type["count"]
            client = client_type["client__name"]
            total += count
            counts.append((count, client))

        counts.sort()

        for count in counts:
            print "%25s %15d" % (count[1], count[0])
        print "Total:", total

    def handle(self, *args, **options):
        if len(args) == 0:
            # Report global activity.
            self.compute_activity(UserActivity.objects.all())
        elif len(args) == 1:
            try:
                # Report activity for a user.
                user_profile = get_user_profile_by_email(args[0])
                self.compute_activity(UserActivity.objects.filter(
                        user_profile=user_profile))
            except UserProfile.DoesNotExist:
                try:
                    # Report activity for a realm.
                    realm = get_realm(args[0])
                    self.compute_activity(UserActivity.objects.filter(
                            user_profile__realm=realm))
                except Realm.DoesNotExist:
                    print "Unknown user or domain %s" % (args[0],)
                    exit(1)

Prompt:
Add a utility to report some rough recent client activity metrics.

(imported from commit 27b4a70871939b2728fcbe0ce5824326ff4decc2)


# -*- coding: utf-8 -*-
# ###
# Copyright (c) 2015, Rice University
# This software is subject to the provisions of the GNU Affero General
# Public License version 3 (AGPLv3).
# See LICENCE.txt for details.
# ###

from .. import utils

__all__ = ('cli_loader',)

@utils.with_cursor
def cli_command(cursor, migrations_directory='', version=None, **kwargs):
    cursor.execute("""\
        SELECT 1 FROM information_schema.tables
        WHERE table_name = 'schema_migrations'""")
    table_exists = cursor.fetchone()
    if table_exists:
        print('Schema migrations already initialized.')
        return

    cursor.execute("""\
        CREATE TABLE schema_migrations (
            version TEXT NOT NULL,
            applied TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
        )""")
    versions = []
    if version is None:
        timestamp = utils.timestamp()
    else:
        timestamp = str(version)
    for version, name in utils.get_migrations(migrations_directory):
        if version <= timestamp:
            versions.append((version,))
    cursor.executemany("""\
        INSERT INTO schema_migrations VALUES (%s)
        """, versions)
    print('Schema migrations initialized.')

def cli_loader(parser):
    parser.add_argument('--version', type=int,
                        help='Set the schema version to VERSION, '
                             'default current timestamp')
    return cli_command

Response:
# -*- coding: utf-8 -*-
# ###
# Copyright (c) 2015, Rice University
# This software is subject to the provisions of the GNU Affero General
# Public License version 3 (AGPLv3).
# See LICENCE.txt for details.
# ###

from .. import utils

__all__ = ('cli_loader',)

@utils.with_cursor
def cli_command(cursor, migrations_directory='', version=None, **kwargs):
    cursor.execute("""\
        SELECT 1 FROM information_schema.tables
        WHERE table_name = 'schema_migrations'""")
    table_exists = cursor.fetchone()
    if table_exists:
        print('Schema migrations already initialized.')
        return

    cursor.execute("""\
        CREATE TABLE schema_migrations (
            version TEXT NOT NULL,
            applied TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
        )""")
    versions = []
    if version is None:
        timestamp = utils.timestamp()
    else:
        timestamp = str(version)
    for version, name in utils.get_migrations(migrations_directory):
        if version <= timestamp:
            versions.append((version,))
    cursor.executemany("""\
        INSERT INTO schema_migrations VALUES (%s)
        """, versions)
    print('Schema migrations initialized.')

def cli_loader(parser):
    parser.add_argument('--version', type=int,
                        help='Set the schema version to VERSION, '
                             'default current timestamp')
    return cli_command

Prompt:
Stop changing schema_migrations data if the table already exists

# -*- coding: utf-8 -*-
# ###
# Copyright (c) 2015, Rice University
# This software is subject to the provisions of the GNU Affero General
# Public License version 3 (AGPLv3).
# See LICENCE.txt for details.
# ###

from .. import utils

__all__ = ('cli_loader',)

@utils.with_cursor
def cli_command(cursor, migrations_directory='', version=None, **kwargs):
    cursor.execute("""\
        CREATE TABLE IF NOT EXISTS schema_migrations (
            version TEXT NOT NULL,
            applied TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
        )""")
    cursor.execute("""\
        DELETE FROM schema_migrations""")
    versions = []
    if version is None:
        timestamp = utils.timestamp()
    else:
        timestamp = str(version)
    for version, name in utils.get_migrations(migrations_directory):
        if version <= timestamp:
            versions.append((version,))
    cursor.executemany("""\
        INSERT INTO schema_migrations VALUES (%s)
        """, versions)
    print('Schema migrations initialized.')

def cli_loader(parser):
    parser.add_argument('--version', type=int,
                        help='Set the schema version to VERSION, '
                             'default current timestamp')
    return cli_command


import sys
from django.apps import AppConfig
from django.conf import settings
from common.helpers.db import db_is_initialized

class CommonConfig(AppConfig):
    name = 'common'

    def ready(self):
        self.display_missing_environment_variables()
        from common.helpers.tags import import_tags_from_csv
        if 'loaddata' in sys.argv:
            self.loaddata_clean()
        elif db_is_initialized():
            import_tags_from_csv()

    def display_missing_environment_variables(self):
        missing_required_variables = []
        for key, value in settings.ENVIRONMENT_VARIABLE_WARNINGS.items():
            if not (hasattr(settings, key)):
                if value['error']:
                    missing_required_variables.append(key)
                print(key + ' not set: ' + value['message'])
        if len(missing_required_variables) > 0:
            raise EnvironmentError('Required environment variables missing: ' + ','.join(missing_required_variables))

    def loaddata_clean(self):
        from django.contrib.contenttypes.models import ContentType
        ContentType.objects.all().delete()

Response:
import sys
from django.apps import AppConfig
from django.conf import settings
from common.helpers.db import db_is_initialized

class CommonConfig(AppConfig):
    name = 'common'

    def ready(self):
        self.display_missing_environment_variables()
        from common.helpers.tags import import_tags_from_csv
        if 'loaddata' in sys.argv:
            self.loaddata_clean()
        elif db_is_initialized():
            import_tags_from_csv()

    def display_missing_environment_variables(self):
        missing_required_variables = []
        for key, value in settings.ENVIRONMENT_VARIABLE_WARNINGS.items():
            if not (hasattr(settings, key)):
                if value['error']:
                    missing_required_variables.append(key)
                print(key + ' not set: ' + value['message'])
        if len(missing_required_variables) > 0:
            raise EnvironmentError('Required environment variables missing: ' + ','.join(missing_required_variables))

    def loaddata_clean(self):
        from django.contrib.contenttypes.models import ContentType
        ContentType.objects.all().delete()

Prompt:
Clean content types table and don't load tags when running loaddata

from django.apps import AppConfig
from django.conf import settings
from common.helpers.db import db_is_initialized

class CommonConfig(AppConfig):
    name = 'common'

    def ready(self):
        self.display_missing_environment_variables()
        from common.helpers.tags import import_tags_from_csv
        if db_is_initialized():
            import_tags_from_csv()

    def display_missing_environment_variables(self):
        missing_required_variables = []
        for key, value in settings.ENVIRONMENT_VARIABLE_WARNINGS.items():
            if not (hasattr(settings, key)):
                if value['error']:
                    missing_required_variables.append(key)
                print(key + ' not set: ' + value['message'])
        if len(missing_required_variables) > 0:
            raise EnvironmentError('Required environment variables missing: ' + ','.join(missing_required_variables))


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

from django.db import models, migrations

def delete_imageinfo(apps, schema_editor):
    ImageInfo = apps.get_model('core.ImageInfo')
    ImageInfo.objects.all().delete()

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0070_add_service_directory_api'),
    ]

    operations = [
        migrations.RunPython(delete_imageinfo),
    ]

Remove old type of image hashes

Remove old type of image hashes

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

from django.db import models, migrations

def delete_imageinfo(apps, schema_editor):
    ImageInfo = apps.get_model('core.ImageInfo')
    ImageInfo.objects.all().delete()

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0070_add_service_directory_api'),
    ]

    operations = [
        migrations.RunPython(delete_imageinfo),
    ]

Prompt: Remove old type of image hashes


from datetime import datetime

from rest_framework import renderers
from icalendar import Calendar, Event

class ICalRenderer(renderers.BaseRenderer):
    media_type = 'text/calendar'
    format = 'ics'

    def render(self, data, media_type=None, renderer_context=None):
        cal = Calendar()
        cal.add('prodid', 'talks.ox.ac.uk')
        cal.add('version', '2.0')

        for e in data:
            cal.add_component(self._event_to_ics(e))
        return cal.to_ical()

    @staticmethod
    def _event_to_ics(e):
        event = Event()
        if 'title' in e:
            event.add('summary', e['title'])
        if 'description' in e:
            event.add('description', e['description'])
        if 'start' in e:
            event.add('dtstart', dt_string_to_object(e['start']))
        if 'url' in e:
            event.add('url', e['url'])
            event.add('uid', e['url'])
        # TODO add location field
        return event

def dt_string_to_object(string):
    """Transforms a string date into a datetime object
    :param string: string representing a date/time
    :return: python datetime object
    """
    return datetime.strptime(string, "%Y-%m-%dT%H:%M:%SZ")

Refactor method string to date

Refactor method string to date

Response:
from datetime import datetime

from rest_framework import renderers
from icalendar import Calendar, Event

class ICalRenderer(renderers.BaseRenderer):
    media_type = 'text/calendar'
    format = 'ics'

    def render(self, data, media_type=None, renderer_context=None):
        cal = Calendar()
        cal.add('prodid', 'talks.ox.ac.uk')
        cal.add('version', '2.0')

        for e in data:
            cal.add_component(self._event_to_ics(e))
        return cal.to_ical()

    @staticmethod
    def _event_to_ics(e):
        event = Event()
        if 'title' in e:
            event.add('summary', e['title'])
        if 'description' in e:
            event.add('description', e['description'])
        if 'start' in e:
            event.add('dtstart', dt_string_to_object(e['start']))
        if 'url' in e:
            event.add('url', e['url'])
            event.add('uid', e['url'])
        # TODO add location field
        return event

def dt_string_to_object(string):
    """Transforms a string date into a datetime object
    :param string: string representing a date/time
    :return: python datetime object
    """
    return datetime.strptime(string, "%Y-%m-%dT%H:%M:%SZ")

Prompt:
Refactor method string to date

from datetime import datetime

from rest_framework import renderers
from icalendar import Calendar, Event

class ICalRenderer(renderers.BaseRenderer):
    media_type = 'text/calendar'
    format = 'ics'

    def render(self, data, media_type=None, renderer_context=None):
        cal = Calendar()
        cal.add('prodid', 'talks.ox.ac.uk')
        cal.add('version', '2.0')

        for e in data:
            cal.add_component(self._event_to_ics(e))
        return cal.to_ical()

    @staticmethod
    def _event_to_ics(e):
        event = Event()
        if 'title' in e:
            event.add('summary', e['title'])
        if 'description' in e:
            event.add('description', e['description'])
        if 'start' in e:
            # 2015-01-29T18:00:00Z
            event.add('dtstart', datetime.strptime(e['start'], "%Y-%m-%dT%H:%M:%SZ"))
        if 'url' in e:
            event.add('url', e['url'])
            event.add('uid', e['url'])
        # TODO add location field
        return event


#!/usr/bin/env python3

# One common issue is that Saleae records traces into memory, which means that
# it can't handle very long captures. This example shows how to use scripting to
# do long recordings over time. There will be brief gaps every time Saleae saves
# the old recording and starts a new one.

import os
import time

import saleae

folder = time.strftime('%Y-%m-%d--%H-%M-%S')
os.mkdir(folder)

s = saleae.Saleae()

# Note: This is a short number of samples. You'll probably want more.
s.set_num_samples(1e6)

for i in range(5):
	path = os.path.abspath(os.path.join(folder, str(i)))
	s.capture_to_file(path)

Add example for repeated / continuous recordings

Response:
#!/usr/bin/env python3

# One common issue is that Saleae records traces into memory, which means that
# it can't handle very long captures. This example shows how to use scripting to
# do long recordings over time. There will be brief gaps every time Saleae saves
# the old recording and starts a new one.

import os
import time

import saleae

folder = time.strftime('%Y-%m-%d--%H-%M-%S')
os.mkdir(folder)

s = saleae.Saleae()

# Note: This is a short number of samples. You'll probably want more.
s.set_num_samples(1e6)

for i in range(5):
	path = os.path.abspath(os.path.join(folder, str(i)))
	s.capture_to_file(path)

Prompt: Add example for repeated / continuous recordings


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

from datetime import timedelta

from django.conf import settings
from django.utils import timezone

try:
    from django.core.management.base import BaseCommand
except ImportError:
    from django.core.management.base import NoArgsCommand as BaseCommand

from hitcount.models import Hit

class Command(BaseCommand):
    help = "Can be run as a cronjob or directly to clean out old Hits objects from the database."

    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)

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

    def handle_noargs(self, **options):
        grace = getattr(settings, 'HITCOUNT_KEEP_HIT_IN_DATABASE', {'days': 30})
        period = timezone.now() - timedelta(**grace)
        qs = Hit.objects.filter(created__lt=period)
        number_removed = qs.count()
        qs.delete()
        self.stdout.write('Successfully removed %s Hits' % number_removed)

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

from datetime import timedelta

from django.conf import settings
from django.utils import timezone

try:
    from django.core.management.base import BaseCommand
except ImportError:
    from django.core.management.base import NoArgsCommand as BaseCommand

from hitcount.models import Hit

class Command(BaseCommand):
    help = "Can be run as a cronjob or directly to clean out old Hits objects from the database."

    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)

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

    def handle_noargs(self, **options):
        grace = getattr(settings, 'HITCOUNT_KEEP_HIT_IN_DATABASE', {'days': 30})
        period = timezone.now() - timedelta(**grace)
        qs = Hit.objects.filter(created__lt=period)
        number_removed = qs.count()
        qs.delete()
        self.stdout.write('Successfully removed %s Hits' % number_removed)

Prompt:
Use count() on queryset instead of len()

Ensure a fast query even for millions of rows.

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

from datetime import timedelta

from django.conf import settings
from django.utils import timezone

try:
    from django.core.management.base import BaseCommand
except ImportError:
    from django.core.management.base import NoArgsCommand as BaseCommand

from hitcount.models import Hit

class Command(BaseCommand):
    help = "Can be run as a cronjob or directly to clean out old Hits objects from the database."

    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)

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

    def handle_noargs(self, **options):
        grace = getattr(settings, 'HITCOUNT_KEEP_HIT_IN_DATABASE', {'days': 30})
        period = timezone.now() - timedelta(**grace)
        qs = Hit.objects.filter(created__lt=period)
        number_removed = len(qs)
        qs.delete()
        self.stdout.write('Successfully removed %s Hits' % number_removed)


#!/usr/bin/env python
import rospy
from interactivespaces_msgs.msg import GenericMessage
DIRECTOR_MESSAGE = """
{
  "description": "bogus",
  "duration": 0,
  "name": "test whatever",
  "resource_uri": "bogus",
  "slug": "test message",
  "windows": [
    {
      "activity": "mirror",
      "activity_config": {
        "viewport": "center"
      },
      "assets": [
      ],
      "width": 450,
      "height": 800,
      "presentation_viewport": "left_one",
      "x_coord": 0,
      "y_coord": 0
    }
  ]
}
"""

if __name__ == '__main__':
    scene_msg = GenericMessage()
    scene_msg.type = 'json'
    scene_msg.message = DIRECTOR_MESSAGE

    rospy.init_node('director_messager')
    scene_pub = rospy.Publisher('/director/scene', GenericMessage, queue_size=100)

    rospy.sleep(2)
    scene_pub.publish(scene_msg)
    rospy.sleep(2)

Response:
#!/usr/bin/env python
import rospy
from interactivespaces_msgs.msg import GenericMessage
DIRECTOR_MESSAGE = """
{
  "description": "bogus",
  "duration": 0,
  "name": "test whatever",
  "resource_uri": "bogus",
  "slug": "test message",
  "windows": [
    {
      "activity": "mirror",
      "activity_config": {
        "viewport": "center"
      },
      "assets": [
      ],
      "width": 450,
      "height": 800,
      "presentation_viewport": "left_one",
      "x_coord": 0,
      "y_coord": 0
    }
  ]
}
"""

if __name__ == '__main__':
    scene_msg = GenericMessage()
    scene_msg.type = 'json'
    scene_msg.message = DIRECTOR_MESSAGE

    rospy.init_node('director_messager')
    scene_pub = rospy.Publisher('/director/scene', GenericMessage, queue_size=100)

    rospy.sleep(2)
    scene_pub.publish(scene_msg)
    rospy.sleep(2)

Prompt:
Update mirror test scene for single activity

#!/usr/bin/env python
import rospy
from interactivespaces_msgs.msg import GenericMessage
DIRECTOR_MESSAGE = """
{
  "description": "bogus",
  "duration": 0,
  "name": "test whatever",
  "resource_uri": "bogus",
  "slug": "test message",
  "windows": [
    {
      "activity": "mirror",
      "activity_config": {
        "viewport": "center"
      },
      "assets": [
      ],
      "width": 450,
      "height": 800,
      "presentation_viewport": "left_one",
      "x_coord": 0,
      "y_coord": 0
    },
    {
      "activity": "mirror",
      "activity_config": {
        "viewport": "center"
      },
      "assets": [
      ],
      "width": 450,
      "height": 800,
      "presentation_viewport": "right_one",
      "x_coord": 0,
      "y_coord": 0
    }
  ]
}
"""

if __name__ == '__main__':
    scene_msg = GenericMessage()
    scene_msg.type = 'json'
    scene_msg.message = DIRECTOR_MESSAGE

    rospy.init_node('director_messager')
    scene_pub = rospy.Publisher('/director/scene', GenericMessage, queue_size=100)

    rospy.sleep(2)
    scene_pub.publish(scene_msg)
    rospy.sleep(2)


#  File:       luhn.py
#  Purpose:    Write a program that can take a number and determine whether
#              or not it is valid per the Luhn formula.
#  Programmer: Amal Shehu
#  Course:     Exercism
#  Date:       Sunday 18 September 2016, 09:55 PM

def Luhn(card_number):
    def digits_of(n):
        return [int(d) for d in str(n)]
    digits = digits_of(card_number)
    odd = digits[-1::-2]
    even = digits[-2::-2]
    checksum = 0
    checksum += sum(odd_digits)
    for d in even_digits:
        checksum += sum(digits_of(d*2))
    return checksum % 10
def checksum(card_number):
    return luhn(card_number)

Return the remainder of checksum

Return the remainder of checksum

Response:
#  File:       luhn.py
#  Purpose:    Write a program that can take a number and determine whether
#              or not it is valid per the Luhn formula.
#  Programmer: Amal Shehu
#  Course:     Exercism
#  Date:       Sunday 18 September 2016, 09:55 PM

def Luhn(card_number):
    def digits_of(n):
        return [int(d) for d in str(n)]
    digits = digits_of(card_number)
    odd = digits[-1::-2]
    even = digits[-2::-2]
    checksum = 0
    checksum += sum(odd_digits)
    for d in even_digits:
        checksum += sum(digits_of(d*2))
    return checksum % 10
def checksum(card_number):
    return luhn(card_number)

Prompt:
Return the remainder of checksum

#  File:       luhn.py
#  Purpose:    Write a program that can take a number and determine whether
#              or not it is valid per the Luhn formula.
#  Programmer: Amal Shehu
#  Course:     Exercism
#  Date:       Sunday 18 September 2016, 09:55 PM

def Luhn(card_number):
    def digits_of(n):
        return [int(d) for d in str(n)]
    digits = digits_of(card_number)
    odd = digits[-1::-2]
    even = digits[-2::-2]
    checksum = 0
    checksum += sum(odd_digits)
def checksum(card_number):
    return luhn(card_number)


from rdopkg import guess
from collections import namedtuple
import pytest

VersionTestCase = namedtuple('VersionTestCase', ('expected', 'input_data'))

data_table_good = [
    VersionTestCase(('1.2.3', None), '1.2.3'),
    VersionTestCase(('1.2.3', 'vX.Y.Z'), 'v1.2.3'),
    VersionTestCase(('1.2.3', 'VX.Y.Z'), 'V1.2.3'),
    VersionTestCase(('banana', None), 'banana'),
]

data_table_bad = [
    VersionTestCase((None, None), None),
    VersionTestCase((None, None), []),
    VersionTestCase((None, None), ()),
    VersionTestCase((None, None), ''),
    VersionTestCase((None, None), {}),
]

data_table_ugly = [
    VersionTestCase((None, None), ('foo', 'bar', 'bah')),
    VersionTestCase((None, None), ['foo', 'bar', 'bah']),
    VersionTestCase((None, None), {'foo': 'bar'}),
]

def test_table_data_good_tag2version():
    for entry in data_table_good:
        assert entry.expected == guess.tag2version(entry.input_data)

def test_table_data_bad_tag2version():
    for entry in data_table_bad:
        # Input Validation should probably return to us (None, None)
        # assert entry.expected == guess.tag2version(entry.input_data)
        assert (entry.input_data, None) == guess.tag2version(entry.input_data)

def test_table_data_ugly_tag2version():
    for entry in data_table_ugly:
        # TODO: probably should be a more specific exception
        with pytest.raises(Exception):
            guess.tag2version(entry.input_data)

def test_version2tag_simple():
    assert '1.2.3' == guess.version2tag('1.2.3')

def test_version2tag_type1():
    assert 'v1.2.3' == guess.version2tag('1.2.3', 'vX.Y.Z')

def test_version2tag_type2():
    assert 'V1.2.3' == guess.version2tag('1.2.3', 'VX.Y.Z')

Add test coverage for rdopkg.guess version2tag and tag2version

adding coverage unittest, there are some not well handled input cases
but better to capture existing behavior and update tests and code to
handle things better

Change-Id: I16dfb60886a1ac5ddfab86100e08ac23f8cf6c65

Response:
from rdopkg import guess
from collections import namedtuple
import pytest

VersionTestCase = namedtuple('VersionTestCase', ('expected', 'input_data'))

data_table_good = [
    VersionTestCase(('1.2.3', None), '1.2.3'),
    VersionTestCase(('1.2.3', 'vX.Y.Z'), 'v1.2.3'),
    VersionTestCase(('1.2.3', 'VX.Y.Z'), 'V1.2.3'),
    VersionTestCase(('banana', None), 'banana'),
]

data_table_bad = [
    VersionTestCase((None, None), None),
    VersionTestCase((None, None), []),
    VersionTestCase((None, None), ()),
    VersionTestCase((None, None), ''),
    VersionTestCase((None, None), {}),
]

data_table_ugly = [
    VersionTestCase((None, None), ('foo', 'bar', 'bah')),
    VersionTestCase((None, None), ['foo', 'bar', 'bah']),
    VersionTestCase((None, None), {'foo': 'bar'}),
]

def test_table_data_good_tag2version():
    for entry in data_table_good:
        assert entry.expected == guess.tag2version(entry.input_data)

def test_table_data_bad_tag2version():
    for entry in data_table_bad:
        # Input Validation should probably return to us (None, None)
        # assert entry.expected == guess.tag2version(entry.input_data)
        assert (entry.input_data, None) == guess.tag2version(entry.input_data)

def test_table_data_ugly_tag2version():
    for entry in data_table_ugly:
        # TODO: probably should be a more specific exception
        with pytest.raises(Exception):
            guess.tag2version(entry.input_data)

def test_version2tag_simple():
    assert '1.2.3' == guess.version2tag('1.2.3')

def test_version2tag_type1():
    assert 'v1.2.3' == guess.version2tag('1.2.3', 'vX.Y.Z')

def test_version2tag_type2():
    assert 'V1.2.3' == guess.version2tag('1.2.3', 'VX.Y.Z')

Prompt:
Add test coverage for rdopkg.guess version2tag and tag2version

adding coverage unittest, there are some not well handled input cases
but better to capture existing behavior and update tests and code to
handle things better

Change-Id: I16dfb60886a1ac5ddfab86100e08ac23f8cf6c65


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
import argparse
import logging
import json
from s3stash.nxstashref_image import NuxeoStashImage

def main(argv=None):

    parser = argparse.ArgumentParser(description='Produce jp2 version of Nuxeo image file and stash in S3.')
    parser.add_argument('path', help="Nuxeo document path")
    parser.add_argument('--bucket', default='ucldc-private-files/jp2000', help="S3 bucket name")
    parser.add_argument('--region', default='us-west-2', help='AWS region')
    parser.add_argument('--pynuxrc', default='~/.pynuxrc', help="rc file for use by pynux")
    parser.add_argument('--replace', action="store_true", help="replace file on s3 if it already exists")
    if argv is None:
        argv = parser.parse_args()

    # logging
    # FIXME would like to name log with nuxeo UID
    filename = argv.path.split('/')[-1]
    logfile = "logs/{}.log".format(filename)
    print "LOG:\t{}".format(logfile)
    logging.basicConfig(filename=logfile, level=logging.INFO, format='%(asctime)s (%(name)s) [%(levelname)s]: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
    logger = logging.getLogger(__name__)

    # convert and stash jp2
    nxstash = NuxeoStashImage(argv.path, argv.bucket, argv.region, argv.pynuxrc, argv.replace)
    report = nxstash.nxstashref()

    # output report to json file
    reportfile = "reports/{}.json".format(filename)
    with open(reportfile, 'w') as f:
        json.dump(report, f, sort_keys=True, indent=4)

    # parse report to give basic stats
    print "REPORT:\t{}".format(reportfile)
    print "SUMMARY:"
    if 'already_s3_stashed' in report.keys():
        print "already stashed:\t{}".format(report['already_s3_stashed'])
    print "converted:\t{}".format(report['converted'])
    print "stashed:\t{}".format(report['stashed'])

    print "\nDone."

if __name__ == "__main__":
    sys.exit(main())

Add code for stashing a single nuxeo image on s3.

Response:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
import argparse
import logging
import json
from s3stash.nxstashref_image import NuxeoStashImage

def main(argv=None):

    parser = argparse.ArgumentParser(description='Produce jp2 version of Nuxeo image file and stash in S3.')
    parser.add_argument('path', help="Nuxeo document path")
    parser.add_argument('--bucket', default='ucldc-private-files/jp2000', help="S3 bucket name")
    parser.add_argument('--region', default='us-west-2', help='AWS region')
    parser.add_argument('--pynuxrc', default='~/.pynuxrc', help="rc file for use by pynux")
    parser.add_argument('--replace', action="store_true", help="replace file on s3 if it already exists")
    if argv is None:
        argv = parser.parse_args()

    # logging
    # FIXME would like to name log with nuxeo UID
    filename = argv.path.split('/')[-1]
    logfile = "logs/{}.log".format(filename)
    print "LOG:\t{}".format(logfile)
    logging.basicConfig(filename=logfile, level=logging.INFO, format='%(asctime)s (%(name)s) [%(levelname)s]: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
    logger = logging.getLogger(__name__)

    # convert and stash jp2
    nxstash = NuxeoStashImage(argv.path, argv.bucket, argv.region, argv.pynuxrc, argv.replace)
    report = nxstash.nxstashref()

    # output report to json file
    reportfile = "reports/{}.json".format(filename)
    with open(reportfile, 'w') as f:
        json.dump(report, f, sort_keys=True, indent=4)

    # parse report to give basic stats
    print "REPORT:\t{}".format(reportfile)
    print "SUMMARY:"
    if 'already_s3_stashed' in report.keys():
        print "already stashed:\t{}".format(report['already_s3_stashed'])
    print "converted:\t{}".format(report['converted'])
    print "stashed:\t{}".format(report['stashed'])

    print "\nDone."

if __name__ == "__main__":
    sys.exit(main())

Prompt: Add code for stashing a single nuxeo image on s3.


HOME_URL = 'https://raw.githubusercontent.com/ftobia/longshot/develop/longshot.py'

def upgrade():
    backup_self()
    download_and_overwrite()
    restart()

def backup_self():
    import shutil
    new_name = __file__ + '.bak'
    shutil.copy(__file__, new_name)

def download_and_overwrite():
    import urllib2
    response = urllib2.urlopen(HOME_URL)
    with open(__file__, 'w') as f:
        f.write(response.read())

def restart():
    import os
    os.execlp('python', __name__)

if __name__ == '__main__':
    backup_self()
    download_and_overwrite()

Use the right download URL.

Use the right download URL.

Response:
HOME_URL = 'https://raw.githubusercontent.com/ftobia/longshot/develop/longshot.py'

def upgrade():
    backup_self()
    download_and_overwrite()
    restart()

def backup_self():
    import shutil
    new_name = __file__ + '.bak'
    shutil.copy(__file__, new_name)

def download_and_overwrite():
    import urllib2
    response = urllib2.urlopen(HOME_URL)
    with open(__file__, 'w') as f:
        f.write(response.read())

def restart():
    import os
    os.execlp('python', __name__)

if __name__ == '__main__':
    backup_self()
    download_and_overwrite()

Prompt:
Use the right download URL.

HOME_URL = 'https://github.com/ftobia/longshot/blob/master/longshot.py'

def upgrade():
    backup_self()
    download_and_overwrite()
    restart()

def backup_self():
    import shutil
    new_name = __file__ + '.bak'
    shutil.copy(__file__, new_name)

def download_and_overwrite():
    import urllib2
    response = urllib2.urlopen(HOME_URL)
    with open(__file__, 'w') as f:
        f.write(response.read())

def restart():
    import os
    os.execlp('python', __name__)

if __name__ == '__main__':
    backup_self()
    download_and_overwrite()


from django.test import TestCase
from Instanssi.kompomaatti.models import Entry

VALID_YOUTUBE_URLS = [
    # must handle various protocols and hostnames in the video URL
    "http://www.youtube.com/v/asdf123456",
    "https://www.youtube.com/v/asdf123456/",
    "//www.youtube.com/v/asdf123456",
    "www.youtube.com/v/asdf123456",
    "youtube.com/v/asdf123456/",
    # must handle various other ways to define the video
    "www.youtube.com/watch?v=asdf123456",
    "http://youtu.be/asdf123456",
    "https://youtu.be/asdf123456/"
]

class KompomaattiTests(TestCase):
    def setUp(self):
        pass

    def test_youtube_urls(self):
        """Test YouTube video id extraction from URLs."""
        for url in VALID_YOUTUBE_URLS:
            self.assertEqual(Entry.youtube_url_to_id(url), "asdf123456",
                             msg="failing URL: %s" % url)

Response:
from django.test import TestCase
from Instanssi.kompomaatti.models import Entry

VALID_YOUTUBE_URLS = [
    # must handle various protocols and hostnames in the video URL
    "http://www.youtube.com/v/asdf123456",
    "https://www.youtube.com/v/asdf123456/",
    "//www.youtube.com/v/asdf123456",
    "www.youtube.com/v/asdf123456",
    "youtube.com/v/asdf123456/",
    # must handle various other ways to define the video
    "www.youtube.com/watch?v=asdf123456",
    "http://youtu.be/asdf123456",
    "https://youtu.be/asdf123456/"
]

class KompomaattiTests(TestCase):
    def setUp(self):
        pass

    def test_youtube_urls(self):
        """Test YouTube video id extraction from URLs."""
        for url in VALID_YOUTUBE_URLS:
            self.assertEqual(Entry.youtube_url_to_id(url), "asdf123456",
                             msg="failing URL: %s" % url)

Prompt:
kompomaatti: Add more test data; improve feedback on failing case

from django.test import TestCase
from Instanssi.kompomaatti.models import Entry

VALID_YOUTUBE_URLS = [
    # must handle various protocols in the video URL
    "http://www.youtube.com/v/asdf123456",
    "https://www.youtube.com/v/asdf123456/",
    "//www.youtube.com/v/asdf123456",
    "www.youtube.com/v/asdf123456",
    # must handle various other ways to define the video
    "www.youtube.com/watch?v=asdf123456",
    "http://youtu.be/asdf123456",
    "http://youtu.be/asdf123456/"
]

class KompomaattiTests(TestCase):
    def setUp(self):
        pass

    def test_youtube_urls(self):
        """Test that various YouTube URLs are parsed properly."""
        for url in VALID_YOUTUBE_URLS:
            print("Test URL: %s" % url)
            self.assertEqual(Entry.youtube_url_to_id(url), "asdf123456")


import sys
import logging
from os.path import isdir, isfile
from gensim import models
from corpus import Corpus
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

class LDACorpus(Corpus):

    def __init__(self, dict_loc, vec_loc, no_topics=100, update=1, chunksize=10000, passes=1):
        Corpus.__init__(self)
        Corpus.load(self, dict_loc, vec_loc)
        self.no_topics = no_topics
        self.update = update
        self.chunksize = chunksize
        self.passes = passes
        self.dict_loc = dict_loc
        self.vec_loc = vec_loc
        self.transformation = models.ldamodel.LdaModel(corpus=self.docs, id2word=self.dictionary, num_topics=self.no_topics, update_every=self.update, chunksize=self.chunksize, passes=self.passes)

    def print_topics(self):
        self.transformation.print_topics(20)

def main():
    if len(sys.argv) > 2 and isdir(sys.argv[1]) and isfile(sys.argv[2]) and isfile(sys.argv[3]):
        corpus = Corpus(sys.argv[1])
        corpus.save(sys.argv[2], sys.argv[3])
        corpus = LDACorpus(sys.argv[2], sys.argv[3], no_topics=25)
        corpus.print_topics()
    else:
        print "Corpus requires directory as an argument."

if __name__ == "__main__": main()

Add basic framework for LDA

Add basic framework for LDA

Response:
import sys
import logging
from os.path import isdir, isfile
from gensim import models
from corpus import Corpus
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

class LDACorpus(Corpus):

    def __init__(self, dict_loc, vec_loc, no_topics=100, update=1, chunksize=10000, passes=1):
        Corpus.__init__(self)
        Corpus.load(self, dict_loc, vec_loc)
        self.no_topics = no_topics
        self.update = update
        self.chunksize = chunksize
        self.passes = passes
        self.dict_loc = dict_loc
        self.vec_loc = vec_loc
        self.transformation = models.ldamodel.LdaModel(corpus=self.docs, id2word=self.dictionary, num_topics=self.no_topics, update_every=self.update, chunksize=self.chunksize, passes=self.passes)

    def print_topics(self):
        self.transformation.print_topics(20)

def main():
    if len(sys.argv) > 2 and isdir(sys.argv[1]) and isfile(sys.argv[2]) and isfile(sys.argv[3]):
        corpus = Corpus(sys.argv[1])
        corpus.save(sys.argv[2], sys.argv[3])
        corpus = LDACorpus(sys.argv[2], sys.argv[3], no_topics=25)
        corpus.print_topics()
    else:
        print "Corpus requires directory as an argument."

if __name__ == "__main__": main()

Prompt: Add basic framework for LDA


from typing import Dict, Any

import attr

@attr.s(slots=True)
class Rule:
    """
    A rule describes how and when to make backups.
    """

    name = attr.ib()
    namespace = attr.ib()

    deltas = attr.ib()
    gce_disk = attr.ib()
    gce_disk_zone = attr.ib()

    claim_name = attr.ib()

    @property
    def pretty_name(self):
        return self.claim_name or self.name

    def to_dict(self) -> Dict[str, Any]:
        """ Helper, returns attr.asdict(self) """
        return attr.asdict(self)

    def __str__ (self):
        return self.name

Response:
from typing import Dict, Any

import attr

@attr.s(slots=True)
class Rule:
    """
    A rule describes how and when to make backups.
    """

    name = attr.ib()
    namespace = attr.ib()

    deltas = attr.ib()
    gce_disk = attr.ib()
    gce_disk_zone = attr.ib()

    claim_name = attr.ib()

    @property
    def pretty_name(self):
        return self.claim_name or self.name

    def to_dict(self) -> Dict[str, Any]:
        """ Helper, returns attr.asdict(self) """
        return attr.asdict(self)

    def __str__ (self):
        return self.name

Prompt:
Fix accidentally commited attribute name change

from typing import Dict, Any

import attr

@attr.s(slots=True)
class Rule:
    """
    A rule describes how and when to make backups.
    """

    volume_name = attr.ib()
    namespace = attr.ib()

    deltas = attr.ib()
    gce_disk = attr.ib()
    gce_disk_zone = attr.ib()

    claim_name = attr.ib()

    @property
    def pretty_name(self):
        return self.claim_name or self.name

    def to_dict(self) -> Dict[str, Any]:
        """ Helper, returns attr.asdict(self) """
        return attr.asdict(self)

    def __str__ (self):
        return self.name


import sys
import re

def main(argv):
    # Load the positive and negative words
    words = {}
    with open("words/positive.txt") as file:
        for line in file:
            words[line.rstrip()] = 1

    with open("words/negative.txt") as file:
        for line in file:
            words[line.rstrip()] = -1

    # Perform the sentiment analysis
    for message in sys.stdin:
        score = 0
        found = 0
        for w in message.split():
            # Only keep alphanumeric characters and some punctuation.
            w = re.sub(r'[^\-\'+\w]', '', w).lower()
            if w in words:
                score += words[w]
                found += 1

        print(round(score / float(found) if found != 0 else 0, 2))

if __name__ == "__main__":
    main(sys.argv[1:])

Response:
import sys
import re

def main(argv):
    # Load the positive and negative words
    words = {}
    with open("words/positive.txt") as file:
        for line in file:
            words[line.rstrip()] = 1

    with open("words/negative.txt") as file:
        for line in file:
            words[line.rstrip()] = -1

    # Perform the sentiment analysis
    for message in sys.stdin:
        score = 0
        found = 0
        for w in message.split():
            # Only keep alphanumeric characters and some punctuation.
            w = re.sub(r'[^\-\'+\w]', '', w).lower()
            if w in words:
                score += words[w]
                found += 1

        print(round(score / float(found) if found != 0 else 0, 2))

if __name__ == "__main__":
    main(sys.argv[1:])

Prompt:
Read from standard input and perform on each line

The analyze script can now be run with, for example
- echo "Message" | python analyze.py
- cat | python analyze.py (enter messages and end with Ctrl-D)
- python analyze.py < filename
- MapReduce (at some point)

import sys
import re

def main(argv):
    # Message to perform sentiment analysis on
    message = argv[0] if len(argv) > 0 else ""

    if message == "":
        print("Usage: python analyze.py [message]")
        sys.exit(1)

    # Load the positive and negative words
    words = {}
    with open("words/positive.txt") as file:
        for line in file:
            words[line.rstrip()] = 1

    with open("words/negative.txt") as file:
        for line in file:
            words[line.rstrip()] = -1

    # Perform the sentiment analysis
    score = 0
    found = 0
    for w in message.split():
        # Only keep alphanumeric characters and some punctuation.
        w = re.sub(r'[^\-\'+\w]', '', w).lower()
        if w in words:
            score += words[w]
            found += 1

    print(round(score / float(found) if found != 0 else 0, 2))

if __name__ == "__main__":
    main(sys.argv[1:])


import os
import subprocess
import tempfile
import nbformat

class TestExamples:

    def _notebook_run(self, path):
        """
        Execute a notebook via nbconvert and collect output.
        Returns (parsed nb object, execution errors)
        """
        dirname, __ = os.path.split(path)
        os.chdir(dirname)
        with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
            args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
                    "--ExecutePreprocessor.timeout=200",
                    "--output", fout.name, path]
            subprocess.check_call(args)

            fout.seek(0)
            nb = nbformat.read(fout, nbformat.current_nbformat)

        errors = [output for cell in nb.cells if "outputs" in cell
                  for output in cell["outputs"]
                  if output.output_type == "error"]

        return nb, errors

    def test_load_era5_ipynb(self):
        parent_dirname = os.path.dirname(os.path.dirname(__file__))
        nb, errors = self._notebook_run(
            os.path.join(parent_dirname, 'example',
                         'load_era5_weather_data.ipynb'))
        assert errors == []

    def test_pvlib_ipynb(self):
        parent_dirname = os.path.dirname(os.path.dirname(__file__))
        nb, errors = self._notebook_run(
            os.path.join(parent_dirname, 'example',
                         'run_pvlib_model.ipynb'))
        assert errors == []

    def test_windpowerlib_turbine_ipynb(self):
        parent_dirname = os.path.dirname(os.path.dirname(__file__))
        nb, errors = self._notebook_run(
            os.path.join(parent_dirname, 'example',
                         'run_windpowerlib_turbine_model.ipynb'))
        assert errors == []

Add tests for jupyter notebooks

Add tests for jupyter notebooks

Response:
import os
import subprocess
import tempfile
import nbformat

class TestExamples:

    def _notebook_run(self, path):
        """
        Execute a notebook via nbconvert and collect output.
        Returns (parsed nb object, execution errors)
        """
        dirname, __ = os.path.split(path)
        os.chdir(dirname)
        with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
            args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
                    "--ExecutePreprocessor.timeout=200",
                    "--output", fout.name, path]
            subprocess.check_call(args)

            fout.seek(0)
            nb = nbformat.read(fout, nbformat.current_nbformat)

        errors = [output for cell in nb.cells if "outputs" in cell
                  for output in cell["outputs"]
                  if output.output_type == "error"]

        return nb, errors

    def test_load_era5_ipynb(self):
        parent_dirname = os.path.dirname(os.path.dirname(__file__))
        nb, errors = self._notebook_run(
            os.path.join(parent_dirname, 'example',
                         'load_era5_weather_data.ipynb'))
        assert errors == []

    def test_pvlib_ipynb(self):
        parent_dirname = os.path.dirname(os.path.dirname(__file__))
        nb, errors = self._notebook_run(
            os.path.join(parent_dirname, 'example',
                         'run_pvlib_model.ipynb'))
        assert errors == []

    def test_windpowerlib_turbine_ipynb(self):
        parent_dirname = os.path.dirname(os.path.dirname(__file__))
        nb, errors = self._notebook_run(
            os.path.join(parent_dirname, 'example',
                         'run_windpowerlib_turbine_model.ipynb'))
        assert errors == []

Prompt: Add tests for jupyter notebooks