import torch
import torch.nn as nn
import torch.nn.functional as F

class DenseBinaryCrossEntropy(nn.Module):
    """ BCE using one-hot from dense targets w/ label smoothing
    NOTE for experiments comparing CE to BCE /w label smoothing, may remove
    """
    def __init__(self, smoothing=0.1):
        super(DenseBinaryCrossEntropy, self).__init__()
        assert 0. <= smoothing < 1.0
        self.smoothing = smoothing
        self.bce = nn.BCEWithLogitsLoss()

    def forward(self, x, target):
        num_classes = x.shape[-1]
        off_value = self.smoothing / num_classes
        on_value = 1. - self.smoothing + off_value
        target = target.long().view(-1, 1)
        target = torch.full(
            (target.size()[0], num_classes), off_value, device=x.device, dtype=x.dtype).scatter_(1, target, on_value)
        return self.bce(x, target)

Add a BCE loss impl that converts dense targets to sparse /w smoothing as an alternate to CE w/ smoothing. For training experiments.

Response:
import torch
import torch.nn as nn
import torch.nn.functional as F

class DenseBinaryCrossEntropy(nn.Module):
    """ BCE using one-hot from dense targets w/ label smoothing
    NOTE for experiments comparing CE to BCE /w label smoothing, may remove
    """
    def __init__(self, smoothing=0.1):
        super(DenseBinaryCrossEntropy, self).__init__()
        assert 0. <= smoothing < 1.0
        self.smoothing = smoothing
        self.bce = nn.BCEWithLogitsLoss()

    def forward(self, x, target):
        num_classes = x.shape[-1]
        off_value = self.smoothing / num_classes
        on_value = 1. - self.smoothing + off_value
        target = target.long().view(-1, 1)
        target = torch.full(
            (target.size()[0], num_classes), off_value, device=x.device, dtype=x.dtype).scatter_(1, target, on_value)
        return self.bce(x, target)

Prompt: Add a BCE loss impl that converts dense targets to sparse /w smoothing as an alternate to CE w/ smoothing. For training experiments.


# -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-09-11 14:35
from __future__ import unicode_literals
from __future__ import absolute_import

from django.db import migrations, models
from corehq.sql_db.operations import RawSQLMigration
from custom.icds_reports.utils.migrations import get_view_migrations

migrator = RawSQLMigration(('custom', 'icds_reports', 'migrations', 'sql_templates'))

class Migration(migrations.Migration):

    dependencies = [
        ('icds_reports', '0069_valid_visits'),
    ]

    operations = [
    ]
    operations.extend(get_view_migrations())

Remove adding field to View model

Remove adding field to View model

Response:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-09-11 14:35
from __future__ import unicode_literals
from __future__ import absolute_import

from django.db import migrations, models
from corehq.sql_db.operations import RawSQLMigration
from custom.icds_reports.utils.migrations import get_view_migrations

migrator = RawSQLMigration(('custom', 'icds_reports', 'migrations', 'sql_templates'))

class Migration(migrations.Migration):

    dependencies = [
        ('icds_reports', '0069_valid_visits'),
    ]

    operations = [
    ]
    operations.extend(get_view_migrations())

Prompt:
Remove adding field to View model

# -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-09-11 14:35
from __future__ import unicode_literals
from __future__ import absolute_import

from django.db import migrations, models
from corehq.sql_db.operations import RawSQLMigration
from custom.icds_reports.utils.migrations import get_view_migrations

migrator = RawSQLMigration(('custom', 'icds_reports', 'migrations', 'sql_templates'))

class Migration(migrations.Migration):

    dependencies = [
        ('icds_reports', '0069_valid_visits'),
    ]

    operations = [
        migrations.AddField(
            model_name='CcsRecordMonthlyView',
            name='open_in_month',
            field=models.SmallIntegerField(blank=True, null=True),
        ),
    ]
    operations.extend(get_view_migrations())


import pandas as pd
import numpy as np
import operator
from sys import argv
import os

def extract( file_name ):
    with open(file_name) as f:
        for i,line in enumerate(f,1):
            if "SCN" in line:
                return i
def main(lta_name):
    os.system('ltahdr -i'+ lta_name + '> lta_file.txt')
    dictionary = {}
    #lta_file = str(argv[1])
    skipped_rows = extract('lta_file.txt')-1

    header = pd.read_csv('lta_file.txt',skiprows=skipped_rows,delimiter=r"\s+")
    flux = list(set(header["OBJECT"]))
    #print flux

    header['Nrecs'] = header['Nrecs'].astype(float)

    for i in flux :
        temp = header.loc[header.OBJECT==i,'Nrecs'].values
        temp = np.mean(temp)
        dictionary[i]=temp
    #print dictionary

    source = max(dictionary.iteritems(),key=operator.itemgetter(1))[0]
    return source

Insert main body of code into function

Insert main body of code into function

Response:
import pandas as pd
import numpy as np
import operator
from sys import argv
import os

def extract( file_name ):
    with open(file_name) as f:
        for i,line in enumerate(f,1):
            if "SCN" in line:
                return i
def main(lta_name):
    os.system('ltahdr -i'+ lta_name + '> lta_file.txt')
    dictionary = {}
    #lta_file = str(argv[1])
    skipped_rows = extract('lta_file.txt')-1

    header = pd.read_csv('lta_file.txt',skiprows=skipped_rows,delimiter=r"\s+")
    flux = list(set(header["OBJECT"]))
    #print flux

    header['Nrecs'] = header['Nrecs'].astype(float)

    for i in flux :
        temp = header.loc[header.OBJECT==i,'Nrecs'].values
        temp = np.mean(temp)
        dictionary[i]=temp
    #print dictionary

    source = max(dictionary.iteritems(),key=operator.itemgetter(1))[0]
    return source

Prompt:
Insert main body of code into function

import pandas as pd
import numpy as np
import operator
from sys import argv
import os

def extract( file_name ):
        with open(file_name) as f:
                for i,line in enumerate(f,1):
                        if "SCN" in line:
                                return i

os.system('ltahdr -i'+ argv[1]+ '> lta_file.txt')
dictionary = {}
#lta_file = str(argv[1])
skipped_rows = extract('lta_file.txt')-1

header = pd.read_csv('lta_file.txt',skiprows=skipped_rows,delimiter=r"\s+")
flux = list(set(header["OBJECT"]))
print flux

header['Nrecs'] = header['Nrecs'].astype(float)

for i in flux :
        temp = header.loc[header.OBJECT==i,'Nrecs'].values
        temp = np.mean(temp)
        dictionary[i]=temp
print dictionary

source = max(dictionary.iteritems(),key=operator.itemgetter(1))[0]
print source


# -*- coding: utf-8 -*-
import numpy as np
from PIL import Image
from pylab import *
import types
from skimage import io, data

# 读取图片,灰度化，并转为数组
im0 = Image.open("test.jpeg").convert('L')
im = array(im0)

# print(type(im[1, 1]))

x = y = 0
m = im.shape[0]
n = im.shape[1]
h = range(m - 2)
k = range(n - 2)
matrix = np.array([[255 for i in range(n)] for i in range(m)])
limit = 255

gray()

# 将图像处理成“点图”
for x in h[::2]:
    for y in k[::2]:
        im[x + 1, y + 1] = (int(im[x, y]) + int(im[x, y + 2]) + int(
            im[x + 1, y + 1]) + int(im[x + 2, y]) + int(im[x + 2, y + 2])) / 5
        im[x, y] = 255

# 在“点图”中连线

imshow(im)
# imshow(matrix)
#io.imsave('dot.jpg', matrix)

# im0.save("test.jpg")

show()

Convert a image to many lines.

Convert a image to many lines.

Response:
# -*- coding: utf-8 -*-
import numpy as np
from PIL import Image
from pylab import *
import types
from skimage import io, data

# 读取图片,灰度化，并转为数组
im0 = Image.open("test.jpeg").convert('L')
im = array(im0)

# print(type(im[1, 1]))

x = y = 0
m = im.shape[0]
n = im.shape[1]
h = range(m - 2)
k = range(n - 2)
matrix = np.array([[255 for i in range(n)] for i in range(m)])
limit = 255

gray()

# 将图像处理成“点图”
for x in h[::2]:
    for y in k[::2]:
        im[x + 1, y + 1] = (int(im[x, y]) + int(im[x, y + 2]) + int(
            im[x + 1, y + 1]) + int(im[x + 2, y]) + int(im[x + 2, y + 2])) / 5
        im[x, y] = 255

# 在“点图”中连线

imshow(im)
# imshow(matrix)
#io.imsave('dot.jpg', matrix)

# im0.save("test.jpg")

show()

Prompt: Convert a image to many lines.


# cerbero - a multi-platform build system for Open Source software
# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import os

from cerbero.commands import Command, register_command
from cerbero.ide.common import PkgConfig
from cerbero.ide.vs.pkgconfig2vsprops import PkgConfig2VSProps, CommonVSProps
from cerbero.utils import _, N_, ArgparseArgument
from cerbero.utils import messages as m

DEFAULT_PREFIX_MACRO = 'CERBERO_SDK_ROOT'

class GenVSProps(Command):
    doc = N_('Generate Visual Studio property sheets to use the SDK from VS')
    name = 'genvsprops'

    def __init__(self):
        Command.__init__(self,
            [ArgparseArgument('-o', '--output_dir', default='.',
                help=_('output directory where .vsprops files will be saved')),
            ])

    def run(self, config, args):
        if not os.path.exists(args.output_dir):
            os.makedirs(args.output_dir)

        for pc in PkgConfig.list_all():
            m.action('Created %s.vsprops' % pc)
            p2v = PkgConfig2VSProps(pc, config.prefix, '$(%s)' %
                    DEFAULT_PREFIX_MACRO, False)
            p2v.create(args.output_dir)
            m.action('Created %s.vsprops' % pc)

        common = CommonVSProps(config.prefix, DEFAULT_PREFIX_MACRO)
        common.create(args.output_dir)
        m.message('Property sheets files were sucessfully created in %s' %
                  os.path.abspath(args.output_dir))

register_command(GenVSProps)

Add command to create VS property sheets for all pkgconfig pacakges

Response:
# cerbero - a multi-platform build system for Open Source software
# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import os

from cerbero.commands import Command, register_command
from cerbero.ide.common import PkgConfig
from cerbero.ide.vs.pkgconfig2vsprops import PkgConfig2VSProps, CommonVSProps
from cerbero.utils import _, N_, ArgparseArgument
from cerbero.utils import messages as m

DEFAULT_PREFIX_MACRO = 'CERBERO_SDK_ROOT'

class GenVSProps(Command):
    doc = N_('Generate Visual Studio property sheets to use the SDK from VS')
    name = 'genvsprops'

    def __init__(self):
        Command.__init__(self,
            [ArgparseArgument('-o', '--output_dir', default='.',
                help=_('output directory where .vsprops files will be saved')),
            ])

    def run(self, config, args):
        if not os.path.exists(args.output_dir):
            os.makedirs(args.output_dir)

        for pc in PkgConfig.list_all():
            m.action('Created %s.vsprops' % pc)
            p2v = PkgConfig2VSProps(pc, config.prefix, '$(%s)' %
                    DEFAULT_PREFIX_MACRO, False)
            p2v.create(args.output_dir)
            m.action('Created %s.vsprops' % pc)

        common = CommonVSProps(config.prefix, DEFAULT_PREFIX_MACRO)
        common.create(args.output_dir)
        m.message('Property sheets files were sucessfully created in %s' %
                  os.path.abspath(args.output_dir))

register_command(GenVSProps)

Prompt: Add command to create VS property sheets for all pkgconfig pacakges


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

from chess.chess import Chess
import unittest

class TestBuildChess(unittest.TestCase):
    """
    `TestBuildChess()` class is unit-testing the class
    Chess().
    """

    # ///////////////////////////////////////////////////
    def setUp(self):
        params = [4, 4]
        pieces = {'King': 2, 'Queen': 1, 'Bishop': 1, 'Rook': 1, 'Knight': 1}
        params.append(pieces)
        self.chess = Chess(params)

    # ///////////////////////////////////////////////////
    def test_build_chess(self):
        """Tests validity of build chessboard"""
        self.assertEqual(self.chess.pieces_types == ['K', 'K', 'Q'], True)
        self.assertEqual(self.chess.number_pieces == 6, True)

    def test_solution_only_kings(self):
        params = [5, 5]
        pieces = {'King': 2, 'Queen': 0, 'Bishop': 0, 'Rook': 0, 'Knight': 0}
        params.append(pieces)
        self.chess = Chess(params)
        self.chess.run_game()
        self.assertEqual(self.chess.solutions == 228, True)

    def test_solution_only_queens(self):
        params = [5, 5]
        pieces = {'King': 0, 'Queen': 2, 'Bishop': 0, 'Rook': 0, 'Knight': 0}
        params.append(pieces)
        self.chess = Chess(params)
        self.chess.run_game()
        self.assertEqual(self.chess.solutions == 140, True)

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

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

from chess.chess import Chess
import unittest

class TestBuildChess(unittest.TestCase):
    """
    `TestBuildChess()` class is unit-testing the class
    Chess().
    """

    # ///////////////////////////////////////////////////
    def setUp(self):
        params = [4, 4]
        pieces = {'King': 2, 'Queen': 1, 'Bishop': 1, 'Rook': 1, 'Knight': 1}
        params.append(pieces)
        self.chess = Chess(params)

    # ///////////////////////////////////////////////////
    def test_build_chess(self):
        """Tests validity of build chessboard"""
        self.assertEqual(self.chess.pieces_types == ['K', 'K', 'Q'], True)
        self.assertEqual(self.chess.number_pieces == 6, True)

    def test_solution_only_kings(self):
        params = [5, 5]
        pieces = {'King': 2, 'Queen': 0, 'Bishop': 0, 'Rook': 0, 'Knight': 0}
        params.append(pieces)
        self.chess = Chess(params)
        self.chess.run_game()
        self.assertEqual(self.chess.solutions == 228, True)

    def test_solution_only_queens(self):
        params = [5, 5]
        pieces = {'King': 0, 'Queen': 2, 'Bishop': 0, 'Rook': 0, 'Knight': 0}
        params.append(pieces)
        self.chess = Chess(params)
        self.chess.run_game()
        self.assertEqual(self.chess.solutions == 140, True)

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

Prompt:
Add a TDD function ( queens on chess board)
# -*- coding: utf-8 -*-

from app.chess.chess import Chess
import unittest

class TestBuildChess(unittest.TestCase):
    """
    `TestBuildChess()` class is unit-testing the class
    Chess().
    """

    # ///////////////////////////////////////////////////
    def setUp(self):
        params = [4, 4]
        pieces = {'King': 2, 'Queen': 1, 'Bishop': 0, 'Rook': 0, 'Knight': 0}
        params.append(pieces)
        self.chess = Chess(params)

    # ///////////////////////////////////////////////////
    def test_solve(self):
        """Tests validity of solution"""
        self.assertEqual(self.chess.pieces_types == ['K', 'K', 'Q'], True)
        self.assertEqual(self.chess.number_pieces == 3, True)
#       self.assertEqual(self.chess.solutions == 1, True)

    def test_solution_only_kings(self):
        params = [5, 5]
        pieces = {'King': 2, 'Queen': 0, 'Bishop': 0, 'Rook': 0, 'Knight': 0}
        params.append(pieces)
        self.chess = Chess(params)
        self.chess.run_game()
        self.assertEqual(self.chess.solutions == 228, True)

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


Add missing newline between imports

Add missing newline between imports

Response:
from django import forms
from django.utils.translation import ugettext_lazy as _

from ...setting.models import Setting

class SettingForm(forms.ModelForm):
    class Meta:
        model = Setting
        exclude = []

    def clean_name(self):
        name = self.cleaned_data['name']
        if len(name.split()) > 1:
            raise forms.ValidationError(_("Name cannot contains whitespaces"))
        return name

Prompt:
Add missing newline between imports

from django import forms
from django.utils.translation import ugettext_lazy as _
from ...setting.models import Setting

class SettingForm(forms.ModelForm):
    class Meta:
        model = Setting
        exclude = []

    def clean_name(self):
        name = self.cleaned_data['name']
        if len(name.split()) > 1:
            raise forms.ValidationError(_("Name cannot contains whitespaces"))
        return name


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

import codecs
import pytest

from poyo import parse_string

@pytest.fixture
def string_data():
    with codecs.open('tests/foobar.yml', encoding='utf-8') as ymlfile:
        return ymlfile.read()

def test_parse_string(string_data):
    expected = {
        u'default_context': {
            u'greeting': u'こんにちは',
            u'email': u'raphael@hackebrot.de',
            u'docs': True,
            u'gui': False,
            123: 456.789,
            u'someint': 1000000,
            u'foo': u'hallo #welt',
            u'trueish': u'Falseeeeeee',
            u'doc_tools': [u'mkdocs', u'sphinx', None],
        },
        u'zZz': True,
        u'NullValue': None,
        u'Hello World': {
            None: u'This is madness',
            u'gh': u'https://github.com/{0}.git',
        },
        u'Yay #python': u'Cool!'
    }

    assert parse_string(string_data) == expected

Add list to expectation in test

Add list to expectation in test

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

import codecs
import pytest

from poyo import parse_string

@pytest.fixture
def string_data():
    with codecs.open('tests/foobar.yml', encoding='utf-8') as ymlfile:
        return ymlfile.read()

def test_parse_string(string_data):
    expected = {
        u'default_context': {
            u'greeting': u'こんにちは',
            u'email': u'raphael@hackebrot.de',
            u'docs': True,
            u'gui': False,
            123: 456.789,
            u'someint': 1000000,
            u'foo': u'hallo #welt',
            u'trueish': u'Falseeeeeee',
            u'doc_tools': [u'mkdocs', u'sphinx', None],
        },
        u'zZz': True,
        u'NullValue': None,
        u'Hello World': {
            None: u'This is madness',
            u'gh': u'https://github.com/{0}.git',
        },
        u'Yay #python': u'Cool!'
    }

    assert parse_string(string_data) == expected

Prompt:
Add list to expectation in test

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

import codecs
import pytest

from poyo import parse_string

@pytest.fixture
def string_data():
    with codecs.open('tests/foobar.yml', encoding='utf-8') as ymlfile:
        return ymlfile.read()

def test_parse_string(string_data):
    expected = {
        u'default_context': {
            u'greeting': u'こんにちは',
            u'email': u'raphael@hackebrot.de',
            u'docs': True,
            u'gui': False,
            123: 456.789,
            u'someint': 1000000,
            u'foo': u'hallo #welt',
            u'trueish': u'Falseeeeeee',
        },
        u'zZz': True,
        u'NullValue': None,
        u'Hello World': {
            None: u'This is madness',
            u'gh': u'https://github.com/{0}.git',
        },
        u'Yay #python': u'Cool!'
    }

    assert parse_string(string_data) == expected


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

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin

from .routers import router

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^api/v1/', include(router.urls)),
    url(r'^api/v1/api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^admin/', include(admin.site.urls)),
)

def mediafiles_urlpatterns():
    """
    Method for serve media files with runserver.
    """

    _media_url = settings.MEDIA_URL
    if _media_url.startswith('/'):
        _media_url = _media_url[1:]

    from django.views.static import serve
    return patterns('',
        (r'^%s(?P<path>.*)$' % 'media', serve,
            {'document_root': settings.MEDIA_ROOT})
    )

urlpatterns += staticfiles_urlpatterns(prefix="/static/")
urlpatterns += mediafiles_urlpatterns()

Set prefix to static url patterm call

Set prefix to static url patterm call

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

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin

from .routers import router

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^api/v1/', include(router.urls)),
    url(r'^api/v1/api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^admin/', include(admin.site.urls)),
)

def mediafiles_urlpatterns():
    """
    Method for serve media files with runserver.
    """

    _media_url = settings.MEDIA_URL
    if _media_url.startswith('/'):
        _media_url = _media_url[1:]

    from django.views.static import serve
    return patterns('',
        (r'^%s(?P<path>.*)$' % 'media', serve,
            {'document_root': settings.MEDIA_ROOT})
    )

urlpatterns += staticfiles_urlpatterns(prefix="/static/")
urlpatterns += mediafiles_urlpatterns()

Prompt:
Set prefix to static url patterm call

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

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin

from .routers import router

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^api/v1/', include(router.urls)),
    url(r'^api/v1/api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^admin/', include(admin.site.urls)),
)

def mediafiles_urlpatterns():
    """
    Method for serve media files with runserver.
    """

    _media_url = settings.MEDIA_URL
    if _media_url.startswith('/'):
        _media_url = _media_url[1:]

    from django.views.static import serve
    return patterns('',
        (r'^%s(?P<path>.*)$' % 'media', serve,
            {'document_root': settings.MEDIA_ROOT})
    )

urlpatterns += staticfiles_urlpatterns()
urlpatterns += mediafiles_urlpatterns()


# coding=utf-8
# -------------------------------------------------------------------------------
# Author: Alexandre Manhaes Savio <alexsavio@gmail.com>
# Grupo de Inteligencia Computational <www.ehu.es/ccwintco>
# Universidad del Pais Vasco UPV/EHU
#
# 2015, Alexandre Manhaes Savio
# Use this at your own risk!
# -------------------------------------------------------------------------------

import tempfile
import os.path      as op
import logging

from   .config import get_temp_dir

log = logging.getLogger(__name__)

def get_extension(filepath, check_if_exists=False):
    """Return the extension of fpath.

    Parameters
    ----------
    fpath: string
    File name or path

    check_if_exists: bool

    Returns
    -------
    str
    The extension of the file name or path
    """
    if check_if_exists:
        if not op.exists(filepath):
            err = 'File not found: ' + filepath
            log.error(err)
            raise IOError(err)

    try:
        rest, ext = op.splitext(filepath)
    except:
        raise
    else:
        return ext

def add_extension_if_needed(filepath, ext, check_if_exists=False):
    """Add the extension ext to fpath if it doesn't have it.

    Parameters
    ----------
    filepath: str
    File name or path

    ext: str
    File extension

    check_if_exists: bool

    Returns
    -------
    File name or path with extension added, if needed.
    """
    if not filepath.endswith(ext):
        filepath += ext

    if check_if_exists:
        if not op.exists(filepath):
            err = 'File not found: ' + filepath
            log.error(err)
            raise IOError(err)

    return filepath

def remove_ext(filepath):
    """Removes the extension of the file.

    Parameters
    ----------
    filepath: str
        File path or name

    Returns
    -------
    str
        File path or name without extension
    """
    return filepath[:filepath.rindex(get_extension(filepath))]

def get_tempfile(suffix='.txt'):
    """
    Parameters
    ----------

    Returns
    -------
    """
    return tempfile.NamedTemporaryFile(suffix=suffix, dir=get_temp_dir())

Add helpers to manage file names

Add helpers to manage file names

Response:
# coding=utf-8
# -------------------------------------------------------------------------------
# Author: Alexandre Manhaes Savio <alexsavio@gmail.com>
# Grupo de Inteligencia Computational <www.ehu.es/ccwintco>
# Universidad del Pais Vasco UPV/EHU
#
# 2015, Alexandre Manhaes Savio
# Use this at your own risk!
# -------------------------------------------------------------------------------

import tempfile
import os.path      as op
import logging

from   .config import get_temp_dir

log = logging.getLogger(__name__)

def get_extension(filepath, check_if_exists=False):
    """Return the extension of fpath.

    Parameters
    ----------
    fpath: string
    File name or path

    check_if_exists: bool

    Returns
    -------
    str
    The extension of the file name or path
    """
    if check_if_exists:
        if not op.exists(filepath):
            err = 'File not found: ' + filepath
            log.error(err)
            raise IOError(err)

    try:
        rest, ext = op.splitext(filepath)
    except:
        raise
    else:
        return ext

def add_extension_if_needed(filepath, ext, check_if_exists=False):
    """Add the extension ext to fpath if it doesn't have it.

    Parameters
    ----------
    filepath: str
    File name or path

    ext: str
    File extension

    check_if_exists: bool

    Returns
    -------
    File name or path with extension added, if needed.
    """
    if not filepath.endswith(ext):
        filepath += ext

    if check_if_exists:
        if not op.exists(filepath):
            err = 'File not found: ' + filepath
            log.error(err)
            raise IOError(err)

    return filepath

def remove_ext(filepath):
    """Removes the extension of the file.

    Parameters
    ----------
    filepath: str
        File path or name

    Returns
    -------
    str
        File path or name without extension
    """
    return filepath[:filepath.rindex(get_extension(filepath))]

def get_tempfile(suffix='.txt'):
    """
    Parameters
    ----------

    Returns
    -------
    """
    return tempfile.NamedTemporaryFile(suffix=suffix, dir=get_temp_dir())

Prompt: Add helpers to manage file names


from parsl.config import Config
from parsl.data_provider.scheme import GlobusScheme
from parsl.executors.threads import ThreadPoolExecutor
from parsl.tests.utils import get_rundir

# If you are a developer running tests, make sure to update parsl/tests/configs/user_opts.py
# If you are a user copying-and-pasting this as an example, make sure to either
#       1) create a local `user_opts.py`, or
#       2) delete the user_opts import below and replace all appearances of `user_opts` with the literal value
#          (i.e., user_opts['swan']['username'] -> 'your_username')
from .user_opts import user_opts

config = Config(
    executors=[
        ThreadPoolExecutor(
            label='local_threads_globus',
            storage_access=[GlobusScheme(
                endpoint_uuid=user_opts['globus']['endpoint'],
                endpoint_path=user_opts['globus']['path']
            )[,
            working_dir=user_opts['globus']['path']
        )
    ],
    run_dir=get_rundir()
)

Fix storage_access in the test config

Fix storage_access in the test config

Response:
from parsl.config import Config
from parsl.data_provider.scheme import GlobusScheme
from parsl.executors.threads import ThreadPoolExecutor
from parsl.tests.utils import get_rundir

# If you are a developer running tests, make sure to update parsl/tests/configs/user_opts.py
# If you are a user copying-and-pasting this as an example, make sure to either
#       1) create a local `user_opts.py`, or
#       2) delete the user_opts import below and replace all appearances of `user_opts` with the literal value
#          (i.e., user_opts['swan']['username'] -> 'your_username')
from .user_opts import user_opts

config = Config(
    executors=[
        ThreadPoolExecutor(
            label='local_threads_globus',
            storage_access=[GlobusScheme(
                endpoint_uuid=user_opts['globus']['endpoint'],
                endpoint_path=user_opts['globus']['path']
            )[,
            working_dir=user_opts['globus']['path']
        )
    ],
    run_dir=get_rundir()
)

Prompt:
Fix storage_access in the test config

from parsl.config import Config
from parsl.data_provider.scheme import GlobusScheme
from parsl.executors.threads import ThreadPoolExecutor
from parsl.tests.utils import get_rundir

# If you are a developer running tests, make sure to update parsl/tests/configs/user_opts.py
# If you are a user copying-and-pasting this as an example, make sure to either
#       1) create a local `user_opts.py`, or
#       2) delete the user_opts import below and replace all appearances of `user_opts` with the literal value
#          (i.e., user_opts['swan']['username'] -> 'your_username')
from .user_opts import user_opts

config = Config(
    executors=[
        ThreadPoolExecutor(
            label='local_threads_globus',
            storage_access=GlobusScheme(
                endpoint_uuid=user_opts['globus']['endpoint'],
                endpoint_path=user_opts['globus']['path']
            ),
            working_dir=user_opts['globus']['path']
        )
    ],
    run_dir=get_rundir()
)


"""
Test that debug symbols have the correct order as specified by the order file.
"""

import os, time
import re
import unittest
import lldb
import lldbtest

class TestClassTypes(lldbtest.TestBase):

    mydir = "order"

    def test_order(self):
        """Test debug symbols follow the correct order by the order file."""
        res = self.res
        exe = os.path.join(os.getcwd(), "a.out")
        self.ci.HandleCommand("file " + exe, res)
        self.assertTrue(res.Succeeded())

        # Test that the debug symbols have Function f3 before Function f1.
        self.ci.HandleCommand("image dump symtab a.out", res)
        self.assertTrue(res.Succeeded())
        output = res.GetOutput()
        mo_f3 = re.search("Function +.+f3", output)
        mo_f1 = re.search("Function +.+f1", output)

        # Match objects for f3 and f1 must exist and f3 must come before f1.
        self.assertTrue(mo_f3 and mo_f1 and mo_f3.start() < mo_f1.start())

        self.ci.HandleCommand("run", res)
        self.assertTrue(res.Succeeded())

if __name__ == '__main__':
    lldb.SBDebugger.Initialize()
    unittest.main()
    lldb.SBDebugger.Terminate()

Test that debug symbols have the correct order as specified by the order file.

git-svn-id: b33bab8abb5b18c12ee100cd7761ab452d00b2b0@107844 91177308-0d34-0410-b5e6-96231b3b80d8

Response:
"""
Test that debug symbols have the correct order as specified by the order file.
"""

import os, time
import re
import unittest
import lldb
import lldbtest

class TestClassTypes(lldbtest.TestBase):

    mydir = "order"

    def test_order(self):
        """Test debug symbols follow the correct order by the order file."""
        res = self.res
        exe = os.path.join(os.getcwd(), "a.out")
        self.ci.HandleCommand("file " + exe, res)
        self.assertTrue(res.Succeeded())

        # Test that the debug symbols have Function f3 before Function f1.
        self.ci.HandleCommand("image dump symtab a.out", res)
        self.assertTrue(res.Succeeded())
        output = res.GetOutput()
        mo_f3 = re.search("Function +.+f3", output)
        mo_f1 = re.search("Function +.+f1", output)

        # Match objects for f3 and f1 must exist and f3 must come before f1.
        self.assertTrue(mo_f3 and mo_f1 and mo_f3.start() < mo_f1.start())

        self.ci.HandleCommand("run", res)
        self.assertTrue(res.Succeeded())

if __name__ == '__main__':
    lldb.SBDebugger.Initialize()
    unittest.main()
    lldb.SBDebugger.Terminate()

Prompt:
Test that debug symbols have the correct order as specified by the order file.

git-svn-id: b33bab8abb5b18c12ee100cd7761ab452d00b2b0@107844 91177308-0d34-0410-b5e6-96231b3b80d8


######
#  Create a file (html or markdown) with the output of
#      - JVMHeap
#      - LogFiles
#      - Ports
#      - Variables
#
#  Author:        Christoph Stoettner
#  Mail:          christoph.stoettner@stoeps.de
#  Documentation: http://scripting101.stoeps.de
#
#  Version:       2.0
#  Date:          2014-06-08
#
#  License:       Apache 2.0
#

# TODO: Create a menu for file selection

import ibmcnx.filehandle
import sys

emp1 = ibmcnx.filehandle.Ibmcnxfile()

sys.stdout = emp1

print '# JVM Settings of all AppServers:'
execfile( 'ibmcnx/doc/JVMSettings.py' )

print '# Used Ports:'
execfile( 'ibmcnx/doc/Ports.py' )

print '# LogFile Settgins:'
execfile( 'ibmcnx/doc/LogFiles.py' )

print '# WebSphere Variables'
execfile( 'ibmcnx/doc/Variables.py' )

Response:
######
#  Create a file (html or markdown) with the output of
#      - JVMHeap
#      - LogFiles
#      - Ports
#      - Variables
#
#  Author:        Christoph Stoettner
#  Mail:          christoph.stoettner@stoeps.de
#  Documentation: http://scripting101.stoeps.de
#
#  Version:       2.0
#  Date:          2014-06-08
#
#  License:       Apache 2.0
#

# TODO: Create a menu for file selection

import ibmcnx.filehandle
import sys

emp1 = ibmcnx.filehandle.Ibmcnxfile()

sys.stdout = emp1

print '# JVM Settings of all AppServers:'
execfile( 'ibmcnx/doc/JVMSettings.py' )

print '# Used Ports:'
execfile( 'ibmcnx/doc/Ports.py' )

print '# LogFile Settgins:'
execfile( 'ibmcnx/doc/LogFiles.py' )

print '# WebSphere Variables'
execfile( 'ibmcnx/doc/Variables.py' )

Prompt:
4: Create script to save documentation to a file

Task-Url: http://github.com/stoeps13/ibmcnx2/issues/issue/4
######
#  Create a file (html or markdown) with the output of
#      - JVMHeap
#      - LogFiles
#      - Ports
#      - Variables
#
#  Author:        Christoph Stoettner
#  Mail:          christoph.stoettner@stoeps.de
#  Documentation: http://scripting101.stoeps.de
#
#  Version:       2.0
#  Date:          2014-06-08
#
#  License:       Apache 2.0
#

# TODO: Create a menu for file selection

import ibmcnx.filehandle
import sys

sys.stdout = open("/tmp/documentation.txt", "w")

print '# JVM Settings of all AppServers:'
execfile( 'ibmcnx/doc/JVMSettings.py' )

print '# Used Ports:'
execfile( 'ibmcnx/doc/Ports.py' )

print '# LogFile Settgins:'
execfile( 'ibmcnx/doc/LogFiles.py' )

print '# WebSphere Variables'
execfile( 'ibmcnx/doc/Variables.py' )


"""Decorators.

"""

from functools import wraps

from astropy import units as u

from poliastro.bodies import Body
from poliastro.twobody.rv import RVState

u.kms = u.km / u.s
u.km3s2 = u.km ** 3 / u.s ** 2

def state_from_vector(func):

    @wraps(func)
    def wrapper(t, u_, k, *args, **kwargs):
        r, v = u_[:3], u_[3:]
        ss = RVState(Body(k * u.km3s2), r * u.km, v * u.kms)

        return func(t, ss, *args, **kwargs)

    return wrapper

Response:
"""Decorators.

"""

from functools import wraps

from astropy import units as u

from poliastro.bodies import Body
from poliastro.twobody.rv import RVState

u.kms = u.km / u.s
u.km3s2 = u.km ** 3 / u.s ** 2

def state_from_vector(func):

    @wraps(func)
    def wrapper(t, u_, k, *args, **kwargs):
        r, v = u_[:3], u_[3:]
        ss = RVState(Body(k * u.km3s2), r * u.km, v * u.kms)

        return func(t, ss, *args, **kwargs)

    return wrapper

Prompt:
Remove extra arguments from decorated function

"""Decorators.

"""

from functools import wraps

from astropy import units as u

from poliastro.bodies import Body
from poliastro.twobody.rv import RVState

u.kms = u.km / u.s
u.km3s2 = u.km ** 3 / u.s ** 2

def state_from_vector(func):

    @wraps(func)
    def wrapper(t, u_, k, *args, **kwargs):
        r, v = u_[:3], u_[3:]
        ss = RVState(Body(k * u.km3s2), r * u.km, v * u.kms)

        return func(t, ss, r, v, *args, **kwargs)

    return wrapper


import pandas as pd
import numpy as np
from xgboost.sklearn import XGBClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.cross_validation import cross_val_score
from sklearn.cross_validation import KFold
from sklearn.feature_selection import SelectFromModel

from utils.metrics import ndcg_scorer

path = '../data/processed/'
train_users = pd.read_csv(path + 'ohe_count_processed_train_users.csv')
y_train = train_users['country_destination']
train_users.drop('country_destination', axis=1, inplace=True)
train_users.drop('id', axis=1, inplace=True)
train_users = train_users.fillna(-1)
x_train = train_users.values
label_encoder = LabelEncoder()
encoded_y_train = label_encoder.fit_transform(y_train)

clf = XGBClassifier(n_estimators=1, nthread=-1, seed=42)
kf = KFold(len(x_train), n_folds=5, random_state=42)

score = cross_val_score(clf, x_train, encoded_y_train,
                        cv=kf, scoring=ndcg_scorer)
print 'Score:', score.mean()

class CustomXGB(XGBClassifier):

    @property
    def feature_importances_(self):
        """Return the feature importances (the higher, the more important the
           feature).
        Returns
        -------
        feature_importances_ : array, shape = [n_features]
        """
        booster = self.booster()
        scores = booster.get_fscore()
        all_scores = pd.Series(np.zeros(x_train.shape[1]))
        scores = pd.Series(scores)
        scores.index = scores.index.map(lambda x: x[1:]).astype(int)
        final_scores = all_scores + scores
        importances = final_scores.fillna(0).values
        return importances

custom = CustomXGB(n_estimators=1, seed=42, nthread=-1)
model = SelectFromModel(custom)
X_new = model.fit_transform(x_train, encoded_y_train)

score = cross_val_score(clf, X_new, encoded_y_train,
                        cv=kf, scoring=ndcg_scorer)
print 'Score:', score.mean()

Add structure to feature selection script

Response:
import pandas as pd
import numpy as np
from xgboost.sklearn import XGBClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.cross_validation import cross_val_score
from sklearn.cross_validation import KFold
from sklearn.feature_selection import SelectFromModel

from utils.metrics import ndcg_scorer

path = '../data/processed/'
train_users = pd.read_csv(path + 'ohe_count_processed_train_users.csv')
y_train = train_users['country_destination']
train_users.drop('country_destination', axis=1, inplace=True)
train_users.drop('id', axis=1, inplace=True)
train_users = train_users.fillna(-1)
x_train = train_users.values
label_encoder = LabelEncoder()
encoded_y_train = label_encoder.fit_transform(y_train)

clf = XGBClassifier(n_estimators=1, nthread=-1, seed=42)
kf = KFold(len(x_train), n_folds=5, random_state=42)

score = cross_val_score(clf, x_train, encoded_y_train,
                        cv=kf, scoring=ndcg_scorer)
print 'Score:', score.mean()

class CustomXGB(XGBClassifier):

    @property
    def feature_importances_(self):
        """Return the feature importances (the higher, the more important the
           feature).
        Returns
        -------
        feature_importances_ : array, shape = [n_features]
        """
        booster = self.booster()
        scores = booster.get_fscore()
        all_scores = pd.Series(np.zeros(x_train.shape[1]))
        scores = pd.Series(scores)
        scores.index = scores.index.map(lambda x: x[1:]).astype(int)
        final_scores = all_scores + scores
        importances = final_scores.fillna(0).values
        return importances

custom = CustomXGB(n_estimators=1, seed=42, nthread=-1)
model = SelectFromModel(custom)
X_new = model.fit_transform(x_train, encoded_y_train)

score = cross_val_score(clf, X_new, encoded_y_train,
                        cv=kf, scoring=ndcg_scorer)
print 'Score:', score.mean()

Prompt: Add structure to feature selection script


# Script to plot original wav file against wav file resampled at 16 kHz

# Licensed under Apache v2 (see LICENSE)

from __future__ import division

import sys
import os
import glob
import numpy as np
import matplotlib.pyplot as plt

from numpy.random import randint
from scipy.signal import resample

# Problems doing this import
# May need to move this file to top level directory
from opensauce.helpers import wavread, round_half_away_from_zero

def main(wav_dir, fs_rs):
    """Compare original data vs resampled data for all wav files in wav_dir,
    where resampling frequency is given in Hz by fs_rs
    """
    # Find all .wav files in test/data directory
    wav_files = glob.glob(os.path.join(wav_dir, '*.wav'))

    for wav_file in wav_files:
        print('Processing wav file {}'.format(wav_file))
        # y is data points, fs is sampling frequency
        y, fs = wavread(wav_file)
        # ns is number of samples
        ns = len(y)
        period = 1.0 / fs
        # Time points corresponding to samples
        t = np.arange(0, ns*period, period)

        # Resample to 16 kHz
        period_rs = 1.0 / fs_rs
        # Number of points in resample
        ns_rs = round_half_away_from_zero(ns * fs_rs / fs)
        # Do resample
        y_rs, t_rs = resample(y, ns_rs, t)

        # Number of points to plot
        n = 1000
        # Start plotting from this data point
        s = randint(10000, 35015-n)
        plt.figure()
        plt.plot(t, y, 'b-', markersize=1)
        plt.plot(t_rs, y_rs, 'ro', markersize=3)
        plt.xlim(s*period, (s+n)*period)
        plt.title(os.path.basename(wav_file))
        plt.xlabel('Time (s)')
        plt.ylabel('Amplitude')
        plt.savefig(os.path.splitext(os.path.basename(wav_file))[0] + '.pdf')

if __name__ == '__main__':
    main(sys.argv[1], int(sys.argv[2]))

Add script to check resampling of wav files

Response:
# Script to plot original wav file against wav file resampled at 16 kHz

# Licensed under Apache v2 (see LICENSE)

from __future__ import division

import sys
import os
import glob
import numpy as np
import matplotlib.pyplot as plt

from numpy.random import randint
from scipy.signal import resample

# Problems doing this import
# May need to move this file to top level directory
from opensauce.helpers import wavread, round_half_away_from_zero

def main(wav_dir, fs_rs):
    """Compare original data vs resampled data for all wav files in wav_dir,
    where resampling frequency is given in Hz by fs_rs
    """
    # Find all .wav files in test/data directory
    wav_files = glob.glob(os.path.join(wav_dir, '*.wav'))

    for wav_file in wav_files:
        print('Processing wav file {}'.format(wav_file))
        # y is data points, fs is sampling frequency
        y, fs = wavread(wav_file)
        # ns is number of samples
        ns = len(y)
        period = 1.0 / fs
        # Time points corresponding to samples
        t = np.arange(0, ns*period, period)

        # Resample to 16 kHz
        period_rs = 1.0 / fs_rs
        # Number of points in resample
        ns_rs = round_half_away_from_zero(ns * fs_rs / fs)
        # Do resample
        y_rs, t_rs = resample(y, ns_rs, t)

        # Number of points to plot
        n = 1000
        # Start plotting from this data point
        s = randint(10000, 35015-n)
        plt.figure()
        plt.plot(t, y, 'b-', markersize=1)
        plt.plot(t_rs, y_rs, 'ro', markersize=3)
        plt.xlim(s*period, (s+n)*period)
        plt.title(os.path.basename(wav_file))
        plt.xlabel('Time (s)')
        plt.ylabel('Amplitude')
        plt.savefig(os.path.splitext(os.path.basename(wav_file))[0] + '.pdf')

if __name__ == '__main__':
    main(sys.argv[1], int(sys.argv[2]))

Prompt: Add script to check resampling of wav files


from txircd.channel import IRCChannel
from txircd.modbase import Module

class Autojoin(Module):
	def joinOnConnect(self, user):
		if "client_join_on_connect" in self.ircd.servconfig:
			for channel in self.ircd.servconfig["client_join_on_connect"]:
				user.join(self.ircd.channels[channel] if channel in self.ircd.channels else IRCChannel(self.ircd, channel))
		return True

class Spawner(object):
	def __init__(self, ircd):
		self.ircd = ircd
		self.conn_join = None

	def spawn(self):
		self.conn_join = Autojoin().hook(self.ircd)
		return {
			"actions": {
				"register": [self.conn_join.joinOnConnect]
			}
		}

	def cleanup(self):
		self.ircd.actions["register"].remove(self.conn_join.joinOnConnect)

Response:
from txircd.channel import IRCChannel
from txircd.modbase import Module

class Autojoin(Module):
	def joinOnConnect(self, user):
		if "client_join_on_connect" in self.ircd.servconfig:
			for channel in self.ircd.servconfig["client_join_on_connect"]:
				user.join(self.ircd.channels[channel] if channel in self.ircd.channels else IRCChannel(self.ircd, channel))
		return True

class Spawner(object):
	def __init__(self, ircd):
		self.ircd = ircd
		self.conn_join = None

	def spawn(self):
		self.conn_join = Autojoin().hook(self.ircd)
		return {
			"actions": {
				"register": [self.conn_join.joinOnConnect]
			}
		}

	def cleanup(self):
		self.ircd.actions["register"].remove(self.conn_join.joinOnConnect)

Prompt:
Fix once again nobody being allowed to connect

from txircd.channel import IRCChannel
from txircd.modbase import Module

class Autojoin(Module):
	def joinOnConnect(self, user):
		if "client_join_on_connect" in self.ircd.servconfig:
			for channel in self.ircd.servconfig["client_join_on_connect"]:
				user.join(self.ircd.channels[channel] if channel in self.ircd.channels else IRCChannel(self.ircd, channel))

class Spawner(object):
	def __init__(self, ircd):
		self.ircd = ircd
		self.conn_join = None

	def spawn(self):
		self.conn_join = Autojoin().hook(self.ircd)
		return {
			"actions": {
				"register": self.conn_join.joinOnConnect
			}
		}

	def cleanup(self):
		self.ircd.actions["register"].remove(self.conn_join.joinOnConnect)


import javascript.traverser as traverser
from javascript.spidermonkey import get_tree
from appvalidator.constants import SPIDERMONKEY_INSTALLATION
from ..contextgenerator import ContextGenerator

def test_js_file(err, filename, data, line=0, context=None):
    "Tests a JS file by parsing and analyzing its tokens"

    if (SPIDERMONKEY_INSTALLATION is None or
        err.get_resource("SPIDERMONKEY") is None):  # Default value is False
        return

    # Set the tier to 4 (Security Tests)
    if err is not None:
        before_tier = err.tier
        err.set_tier(3)

    tree = get_tree(data, err, filename,
                    err and err.get_resource("SPIDERMONKEY") or
                    SPIDERMONKEY_INSTALLATION)
    if not tree:
        err.metadata.ran_js_tests = "no;missing ast"
        if err is not None:
            err.set_tier(before_tier)
        return

    trav = traverser.Traverser(
        err, filename, line, context=context or ContextGenerator(data))
    trav.run(tree)

    err.metadata.ran_js_tests = "yes"

    # Reset the tier so we don't break the world
    if err is not None:
        err.set_tier(before_tier)

Response:
import javascript.traverser as traverser
from javascript.spidermonkey import get_tree
from appvalidator.constants import SPIDERMONKEY_INSTALLATION
from ..contextgenerator import ContextGenerator

def test_js_file(err, filename, data, line=0, context=None):
    "Tests a JS file by parsing and analyzing its tokens"

    if (SPIDERMONKEY_INSTALLATION is None or
        err.get_resource("SPIDERMONKEY") is None):  # Default value is False
        return

    # Set the tier to 4 (Security Tests)
    if err is not None:
        before_tier = err.tier
        err.set_tier(3)

    tree = get_tree(data, err, filename,
                    err and err.get_resource("SPIDERMONKEY") or
                    SPIDERMONKEY_INSTALLATION)
    if not tree:
        err.metadata.ran_js_tests = "no;missing ast"
        if err is not None:
            err.set_tier(before_tier)
        return

    trav = traverser.Traverser(
        err, filename, line, context=context or ContextGenerator(data))
    trav.run(tree)

    err.metadata.ran_js_tests = "yes"

    # Reset the tier so we don't break the world
    if err is not None:
        err.set_tier(before_tier)

Prompt:
Add information about JS test status to metadata
import javascript.traverser as traverser
from javascript.spidermonkey import get_tree
from appvalidator.constants import SPIDERMONKEY_INSTALLATION
from ..contextgenerator import ContextGenerator

def test_js_file(err, filename, data, line=0, context=None):
    "Tests a JS file by parsing and analyzing its tokens"

    if (SPIDERMONKEY_INSTALLATION is None or
        err.get_resource("SPIDERMONKEY") is None):  # Default value is False
        return

    # Set the tier to 4 (Security Tests)
    if err is not None:
        before_tier = err.tier
        err.set_tier(3)

    tree = get_tree(data, err, filename,
                    err and err.get_resource("SPIDERMONKEY") or
                    SPIDERMONKEY_INSTALLATION)
    if not tree:
        if err is not None:
            err.set_tier(before_tier)
        return

    trav = traverser.Traverser(
        err, filename, line, context=context or ContextGenerator(data))
    trav.run(tree)

    # Reset the tier so we don't break the world
    if err is not None:
        err.set_tier(before_tier)


from nose.plugins.skip import SkipTest
from urllib3.packages import six

try:
    from urllib3.contrib.pyopenssl import (inject_into_urllib3,
                                           extract_from_urllib3)
except ImportError as e:
    raise SkipTest('Could not import PyOpenSSL: %r' % e)

from ..with_dummyserver.test_https import TestHTTPS, TestHTTPS_TLSv1
from ..with_dummyserver.test_socketlevel import TestSNI, TestSocketClosing

def setup_module():
    inject_into_urllib3()

def teardown_module():
    extract_from_urllib3()

Enable PyOpenSSL testing on Python 3.

Enable PyOpenSSL testing on Python 3.

Response:
from nose.plugins.skip import SkipTest
from urllib3.packages import six

try:
    from urllib3.contrib.pyopenssl import (inject_into_urllib3,
                                           extract_from_urllib3)
except ImportError as e:
    raise SkipTest('Could not import PyOpenSSL: %r' % e)

from ..with_dummyserver.test_https import TestHTTPS, TestHTTPS_TLSv1
from ..with_dummyserver.test_socketlevel import TestSNI, TestSocketClosing

def setup_module():
    inject_into_urllib3()

def teardown_module():
    extract_from_urllib3()

Prompt:
Enable PyOpenSSL testing on Python 3.

from nose.plugins.skip import SkipTest
from urllib3.packages import six

if six.PY3:
    raise SkipTest('Testing of PyOpenSSL disabled on PY3')

try:
    from urllib3.contrib.pyopenssl import (inject_into_urllib3,
                                           extract_from_urllib3)
except ImportError as e:
    raise SkipTest('Could not import PyOpenSSL: %r' % e)

from ..with_dummyserver.test_https import TestHTTPS, TestHTTPS_TLSv1
from ..with_dummyserver.test_socketlevel import TestSNI, TestSocketClosing

def setup_module():
    inject_into_urllib3()

def teardown_module():
    extract_from_urllib3()


import os
from ij import IJ, ImagePlus
from ij.gui import GenericDialog

def run():
  srcDir = IJ.getDirectory("Input_directory")
  if not srcDir:
    return
  dstDir = IJ.getDirectory("Output_directory")
  if not dstDir:
    return
  gd = GenericDialog("Process Folder")
  gd.addStringField("File_extension", ".tif")
  gd.addStringField("File_name_contains", "")
  gd.addCheckbox("Keep directory structure when saving", True)
  gd.showDialog()
  if gd.wasCanceled():
    return
  ext = gd.getNextString()
  containString = gd.getNextString()
  keepDirectories = gd.getNextBoolean()
  for root, directories, filenames in os.walk(srcDir):
    for filename in filenames:
      # Check for file extension
      if not filename.endswith(ext):
        continue
      # Check for file name pattern
      if containString not in filename:
        continue
      process(srcDir, dstDir, root, filename, keepDirectories)

def process(srcDir, dstDir, currentDir, fileName, keepDirectories):
  print "Processing:"

  # Opening the image
  print "Open image file", fileName
  imp = IJ.openImage(os.path.join(currentDir, fileName))

  # Put your processing commands here!

  # Saving the image
  saveDir = currentDir.replace(srcDir, dstDir) if keepDirectories else dstDir
  if not os.path.exists(saveDir):
    os.makedirs(saveDir)
  print "Saving to", saveDir
  IJ.saveAs(imp, "Tiff", os.path.join(saveDir, fileName));
  imp.close()

run()

Add python template to process a folder of images

Response:
import os
from ij import IJ, ImagePlus
from ij.gui import GenericDialog

def run():
  srcDir = IJ.getDirectory("Input_directory")
  if not srcDir:
    return
  dstDir = IJ.getDirectory("Output_directory")
  if not dstDir:
    return
  gd = GenericDialog("Process Folder")
  gd.addStringField("File_extension", ".tif")
  gd.addStringField("File_name_contains", "")
  gd.addCheckbox("Keep directory structure when saving", True)
  gd.showDialog()
  if gd.wasCanceled():
    return
  ext = gd.getNextString()
  containString = gd.getNextString()
  keepDirectories = gd.getNextBoolean()
  for root, directories, filenames in os.walk(srcDir):
    for filename in filenames:
      # Check for file extension
      if not filename.endswith(ext):
        continue
      # Check for file name pattern
      if containString not in filename:
        continue
      process(srcDir, dstDir, root, filename, keepDirectories)

def process(srcDir, dstDir, currentDir, fileName, keepDirectories):
  print "Processing:"

  # Opening the image
  print "Open image file", fileName
  imp = IJ.openImage(os.path.join(currentDir, fileName))

  # Put your processing commands here!

  # Saving the image
  saveDir = currentDir.replace(srcDir, dstDir) if keepDirectories else dstDir
  if not os.path.exists(saveDir):
    os.makedirs(saveDir)
  print "Saving to", saveDir
  IJ.saveAs(imp, "Tiff", os.path.join(saveDir, fileName));
  imp.close()

run()

Prompt: Add python template to process a folder of images


class Base1:
    def __init__(self, *args):
        print("Base1.__init__", args)

class Ctuple1(Base1, tuple):
    pass

a = Ctuple1()
print(len(a))
a = Ctuple1([1, 2, 3])
print(len(a))

print("---")

class Ctuple2(tuple, Base1):
    pass

a = Ctuple2()
print(len(a))
a = Ctuple2([1, 2, 3])
print(len(a))

a = tuple([1,2,3])
b = Ctuple1([1,2,3])
c = Ctuple2([1,2,3])

print(a == b)
print(b == c)
print(c == a)

Response:
class Base1:
    def __init__(self, *args):
        print("Base1.__init__", args)

class Ctuple1(Base1, tuple):
    pass

a = Ctuple1()
print(len(a))
a = Ctuple1([1, 2, 3])
print(len(a))

print("---")

class Ctuple2(tuple, Base1):
    pass

a = Ctuple2()
print(len(a))
a = Ctuple2([1, 2, 3])
print(len(a))

a = tuple([1,2,3])
b = Ctuple1([1,2,3])
c = Ctuple2([1,2,3])

print(a == b)
print(b == c)
print(c == a)

Prompt:
tests/basics: Expand test cases for equality of subclasses.

class Base1:
    def __init__(self, *args):
        print("Base1.__init__", args)

class Ctuple1(Base1, tuple):
    pass

a = Ctuple1()
print(len(a))
a = Ctuple1([1, 2, 3])
print(len(a))

print("---")

class Ctuple2(tuple, Base1):
    pass

a = Ctuple2()
print(len(a))
a = Ctuple2([1, 2, 3])
print(len(a))


import subprocess
import logging
import os

def fix_env_in_case_of_invalid_locale_name(env):
    """See https://github.com/ros-drivers/hokuyo_node/issues/3"""
    env["LC_ALL"] = "C"

def run(command, cwd=None):
    env = os.environ.copy()
    fix_env_in_case_of_invalid_locale_name(env)
    try:
        return subprocess.check_output(command, cwd=cwd, stderr=subprocess.STDOUT, stdin=open("/dev/null"),
                                       close_fds=True, env=env)
    except subprocess.CalledProcessError as e:
        logging.error("Failed command '%s' output:\n%s" % (command, e.output))
        raise

Response:
import subprocess
import logging
import os

def fix_env_in_case_of_invalid_locale_name(env):
    """See https://github.com/ros-drivers/hokuyo_node/issues/3"""
    env["LC_ALL"] = "C"

def run(command, cwd=None):
    env = os.environ.copy()
    fix_env_in_case_of_invalid_locale_name(env)
    try:
        return subprocess.check_output(command, cwd=cwd, stderr=subprocess.STDOUT, stdin=open("/dev/null"),
                                       close_fds=True, env=env)
    except subprocess.CalledProcessError as e:
        logging.error("Failed command '%s' output:\n%s" % (command, e.output))
        raise

Prompt:
Fix env var for osmosis when locale name is invalid

See https://github.com/ros-drivers/hokuyo_node/issues/3

import subprocess
import logging

def run(command, cwd=None):
    try:
        return subprocess.check_output(
            command, cwd=cwd, stderr=subprocess.STDOUT,
            stdin=open("/dev/null"), close_fds=True)
    except subprocess.CalledProcessError as e:
        logging.error("Failed command '%s' output:\n%s" % (command, e.output))
        raise


from werkzeug.wrappers import Request

from lexington.util.di import depends_on

@depends_on(['environ'])
def get_request(environ):
    return Request(environ)

@depends_on(['request'])
def get_method(request):
    return request.method

@depends_on(['request'])
def get_path(request):
    return request.path

@depends_on(['request'])
def get_query_string(request):
    return request.query_string

@depends_on(['request'])
def get_query(request):
    return request.args

def register_all(dependencies):
    dependant_functions = {
        'request': get_request,
        'method': get_method,
        'path': get_path,
        'query_string': get_query_string,
        'query': get_query,
    }
    for name, dependant in dependant_functions.items():
        dependencies.register_dependant(name, dependant)

Reduce direct dependencies on environ

Reduce direct dependencies on environ

Response:
from werkzeug.wrappers import Request

from lexington.util.di import depends_on

@depends_on(['environ'])
def get_request(environ):
    return Request(environ)

@depends_on(['request'])
def get_method(request):
    return request.method

@depends_on(['request'])
def get_path(request):
    return request.path

@depends_on(['request'])
def get_query_string(request):
    return request.query_string

@depends_on(['request'])
def get_query(request):
    return request.args

def register_all(dependencies):
    dependant_functions = {
        'request': get_request,
        'method': get_method,
        'path': get_path,
        'query_string': get_query_string,
        'query': get_query,
    }
    for name, dependant in dependant_functions.items():
        dependencies.register_dependant(name, dependant)

Prompt:
Reduce direct dependencies on environ

from urllib import parse
from werkzeug.wrappers import Request

from lexington.util.di import depends_on

@depends_on(['environ'])
def get_request(environ):
    return Request(environ)

# TODO: clean up all of these...
@depends_on(['environ'])
def get_method(environ):
    return environ['REQUEST_METHOD']

@depends_on(['environ'])
def get_path(environ):
    return environ['PATH_INFO']

@depends_on(['environ'])
def get_query_string(environ):
    return environ['QUERY_STRING']

@depends_on(['query_string'])
def get_query(query_string):
    return parse.parse_qs(query_string)

def register_all(dependencies):
    dependant_functions = {
        'request': get_request,
        'method': get_method,
        'path': get_path,
        'query_string': get_query_string,
        'query': get_query,
    }
    for name, dependant in dependant_functions.items():
        dependencies.register_dependant(name, dependant)


#!/usr/bin/env python

'''
Run pylint on a Jinja 2 template.
'''

import jinja2, os, subprocess, sys, tempfile

# Clagged pieces from the runner.
START_BLOCK = '/*-'
END_BLOCK = '-*/'
START_VARIABLE = '/*?'
END_VARIABLE = '?*/'
START_COMMENT = '/*#'
END_COMMENT = '#*/'

def main(argv, out, err):
    if len(argv) < 2 or argv[1] in ['--help', '-?']:
        print >>err, 'usage: %s file pylint_args...' % argv[0]
        return -1

    if not os.path.exists(argv[1]):
        print >>err, '%s not found' % argv[1]
        return -1

    root, template = os.path.split(os.path.abspath(argv[1]))

    # Construct a Jinja environment that matches that of the runner.
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(root),
        extensions=["jinja2.ext.do", "jinja2.ext.loopcontrols"],
        block_start_string=START_BLOCK,
        block_end_string=END_BLOCK,
        variable_start_string=START_VARIABLE,
        variable_end_string=END_VARIABLE,
        comment_start_string=START_COMMENT,
        comment_end_string=END_COMMENT)

    # Compile the template requested to a temporary directory.
    tmp = tempfile.mkdtemp()
    print >>out, 'compiling to %s...' % tmp
    env.compile_templates(tmp, filter_func=lambda x: x == template, zip=None,
        ignore_errors=False)

    # Find it and run pylint on it.
    py = os.path.join(tmp, os.listdir(tmp)[0])
    print 'running pylint on %s...' % py
    return subprocess.call(['pylint', py] + argv[2:])

if __name__ == '__main__':
    sys.exit(main(sys.argv, sys.stdout, sys.stderr))

Add a tool for running pylint on Jinja templates.

Closes JIRA CAMKES-315

Response:
#!/usr/bin/env python

'''
Run pylint on a Jinja 2 template.
'''

import jinja2, os, subprocess, sys, tempfile

# Clagged pieces from the runner.
START_BLOCK = '/*-'
END_BLOCK = '-*/'
START_VARIABLE = '/*?'
END_VARIABLE = '?*/'
START_COMMENT = '/*#'
END_COMMENT = '#*/'

def main(argv, out, err):
    if len(argv) < 2 or argv[1] in ['--help', '-?']:
        print >>err, 'usage: %s file pylint_args...' % argv[0]
        return -1

    if not os.path.exists(argv[1]):
        print >>err, '%s not found' % argv[1]
        return -1

    root, template = os.path.split(os.path.abspath(argv[1]))

    # Construct a Jinja environment that matches that of the runner.
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(root),
        extensions=["jinja2.ext.do", "jinja2.ext.loopcontrols"],
        block_start_string=START_BLOCK,
        block_end_string=END_BLOCK,
        variable_start_string=START_VARIABLE,
        variable_end_string=END_VARIABLE,
        comment_start_string=START_COMMENT,
        comment_end_string=END_COMMENT)

    # Compile the template requested to a temporary directory.
    tmp = tempfile.mkdtemp()
    print >>out, 'compiling to %s...' % tmp
    env.compile_templates(tmp, filter_func=lambda x: x == template, zip=None,
        ignore_errors=False)

    # Find it and run pylint on it.
    py = os.path.join(tmp, os.listdir(tmp)[0])
    print 'running pylint on %s...' % py
    return subprocess.call(['pylint', py] + argv[2:])

if __name__ == '__main__':
    sys.exit(main(sys.argv, sys.stdout, sys.stderr))

Prompt:
Add a tool for running pylint on Jinja templates.

Closes JIRA CAMKES-315


import pytest

from ceph_deploy.cli import get_parser

class TestParserRGW(object):

    def setup(self):
        self.parser = get_parser()

    def test_rgw_help(self, capsys):
        with pytest.raises(SystemExit):
            self.parser.parse_args('rgw --help'.split())
        out, err = capsys.readouterr()
        assert 'usage: ceph-deploy rgw' in out
        assert 'positional arguments:' in out
        assert 'optional arguments:' in out

    @pytest.mark.skipif(reason="http://tracker.ceph.com/issues/12150")
    def test_rgw_create_host_required(self, capsys):
        with pytest.raises(SystemExit):
            self.parser.parse_args('rgw create'.split())
        out, err = capsys.readouterr()
        assert "error: too few arguments" in err

    def test_rgw_create_one_host(self):
        args = self.parser.parse_args('rgw create host1'.split())
        assert args.rgw[0][0] == 'host1'

    def test_rgw_create_multiple_hosts(self):
        hostnames = ['host1', 'host2', 'host3']
        args = self.parser.parse_args(['rgw', 'create'] + hostnames)
        # args.rgw is a list of tuples, and tuple[0] is the hostname
        hosts = [x[0] for x in args.rgw]
        assert frozenset(hosts) == frozenset(hostnames)

Add argparse tests for rgw module

[RM-11742] Add argparse tests for rgw module

Signed-off-by: Travis Rhoden <e5e44d6dbac12e32e01c3bb8b67940d8b42e225b@redhat.com>

Response:
import pytest

from ceph_deploy.cli import get_parser

class TestParserRGW(object):

    def setup(self):
        self.parser = get_parser()

    def test_rgw_help(self, capsys):
        with pytest.raises(SystemExit):
            self.parser.parse_args('rgw --help'.split())
        out, err = capsys.readouterr()
        assert 'usage: ceph-deploy rgw' in out
        assert 'positional arguments:' in out
        assert 'optional arguments:' in out

    @pytest.mark.skipif(reason="http://tracker.ceph.com/issues/12150")
    def test_rgw_create_host_required(self, capsys):
        with pytest.raises(SystemExit):
            self.parser.parse_args('rgw create'.split())
        out, err = capsys.readouterr()
        assert "error: too few arguments" in err

    def test_rgw_create_one_host(self):
        args = self.parser.parse_args('rgw create host1'.split())
        assert args.rgw[0][0] == 'host1'

    def test_rgw_create_multiple_hosts(self):
        hostnames = ['host1', 'host2', 'host3']
        args = self.parser.parse_args(['rgw', 'create'] + hostnames)
        # args.rgw is a list of tuples, and tuple[0] is the hostname
        hosts = [x[0] for x in args.rgw]
        assert frozenset(hosts) == frozenset(hostnames)

Prompt:
[RM-11742] Add argparse tests for rgw module

Signed-off-by: Travis Rhoden <e5e44d6dbac12e32e01c3bb8b67940d8b42e225b@redhat.com>


"""empty message

Revision ID: df33f3613823
Revises: 15741cc426db
Create Date: 2017-07-14 11:37:46.347709

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'df33f3613823'
down_revision = '15741cc426db'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('patients', sa.Column('age', sa.Integer(), nullable=True))
    op.add_column('patients', sa.Column('age_unit', sa.String(length=1), nullable=True))
    op.add_column('patients', sa.Column('birth_date', sa.Date(), nullable=True))
    op.add_column('patients', sa.Column('gender', sa.String(length=1), nullable=True))
    op.add_column('users', sa.Column('password_hash', sa.String(length=128), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('users', 'password_hash')
    op.drop_column('patients', 'gender')
    op.drop_column('patients', 'birth_date')
    op.drop_column('patients', 'age_unit')
    op.drop_column('patients', 'age')
    # ### end Alembic commands ###

Add migration for patient model

:rocket: Add migration for patient model

Response:
"""empty message

Revision ID: df33f3613823
Revises: 15741cc426db
Create Date: 2017-07-14 11:37:46.347709

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'df33f3613823'
down_revision = '15741cc426db'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('patients', sa.Column('age', sa.Integer(), nullable=True))
    op.add_column('patients', sa.Column('age_unit', sa.String(length=1), nullable=True))
    op.add_column('patients', sa.Column('birth_date', sa.Date(), nullable=True))
    op.add_column('patients', sa.Column('gender', sa.String(length=1), nullable=True))
    op.add_column('users', sa.Column('password_hash', sa.String(length=128), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('users', 'password_hash')
    op.drop_column('patients', 'gender')
    op.drop_column('patients', 'birth_date')
    op.drop_column('patients', 'age_unit')
    op.drop_column('patients', 'age')
    # ### end Alembic commands ###

Prompt: :rocket: Add migration for patient model


""" rhcephcompose CLI """

from argparse import ArgumentParser
import kobo.conf
from rhcephcompose.compose import Compose

class RHCephCompose(object):
    """ Main class for rhcephcompose CLI. """

    def __init__(self):

        parser = ArgumentParser(description='Generate a compose for RHCS.')
        parser.add_argument('config_file', metavar='config',
                            help='main configuration file for this release.')
        parser.add_argument('--insecure', action='store_const', const=True,
                            default=False, help='skip SSL verification')
        args = parser.parse_args()

        conf = kobo.conf.PyConfigParser()
        conf.load_from_file(args.config_file)
        if args.insecure:
            conf['chacra_ssl_verify'] = False
            import requests
            from requests.packages.urllib3.exceptions\
                import InsecureRequestWarning
            requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

        compose = Compose(conf)
        compose.run()

Response:
""" rhcephcompose CLI """

from argparse import ArgumentParser
import kobo.conf
from rhcephcompose.compose import Compose

class RHCephCompose(object):
    """ Main class for rhcephcompose CLI. """

    def __init__(self):

        parser = ArgumentParser(description='Generate a compose for RHCS.')
        parser.add_argument('config_file', metavar='config',
                            help='main configuration file for this release.')
        parser.add_argument('--insecure', action='store_const', const=True,
                            default=False, help='skip SSL verification')
        args = parser.parse_args()

        conf = kobo.conf.PyConfigParser()
        conf.load_from_file(args.config_file)
        if args.insecure:
            conf['chacra_ssl_verify'] = False
            import requests
            from requests.packages.urllib3.exceptions\
                import InsecureRequestWarning
            requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

        compose = Compose(conf)
        compose.run()

Prompt:
Add --insecure option to command line to disable SSL certificate verification when communicating with chacra

""" rhcephcompose CLI """

from argparse import ArgumentParser
import kobo.conf
from rhcephcompose.compose import Compose

class RHCephCompose(object):
    """ Main class for rhcephcompose CLI. """

    def __init__(self):

        parser = ArgumentParser(description='Generate a compose for RHCS.')
        parser.add_argument('config_file', metavar='config',
                            help='main configuration file for this release.')
        args = parser.parse_args()

        conf = kobo.conf.PyConfigParser()
        conf.load_from_file(args.config_file)

        compose = Compose(conf)
        compose.run()


"""Prints out submitted plugins that we don't already know about.

Also deletes any empty submissions.
"""

import json
import re

import rethinkdb as r

import db.github_repos
import db.util

r_conn = db.util.r_conn

_GITHUB_LINK_REGEX = re.compile(r'github.com/(.*?)/([^/?#]*)')

def delete_empty_submissions():
    """Delete submitted plugins that don't have enough info for us to act on.

    Since we have no form validation, many submissions are just people who
    click the "submit" button.
    """
    deleted = r.table('submitted_plugins').filter({
        'name': '',
        'author': '',
        'github-link': '',
        'vimorg-link': '',
    }).delete().run(r_conn())

    print 'Deleted empty submissions:'
    print deleted

def main():
    delete_empty_submissions()

    known_vimorg_plugins = []
    known_github_plugins = []
    new_plugins = []
    unparseable_plugins = []

    submissions = r.table('submitted_plugins').run(r_conn())
    for submission in submissions:
        if submission['vimorg-link']:
            known_vimorg_plugins.append(submission)
            continue

        github_link = submission['github-link']
        if github_link:
            matches = _GITHUB_LINK_REGEX.findall(github_link)
            if not matches:
                unparseable_plugins.append(submission)
                continue

            repo_owner, repo_name = matches[0]
            db_repo = db.github_repos.PluginGithubRepos.get_with_owner_repo(
                    repo_owner, repo_name)

            if db_repo:
                known_github_plugins.append(submission)
            else:
                new_plugins.append(submission)

    print
    print '%s submissions are known vim.org plugins' % len(
            known_vimorg_plugins)
    print '%s submissions are known github.com plugins' % len(
            known_github_plugins)

    print
    print '%s submissions have unparseable github.com links:' % len(
            unparseable_plugins)
    for submission in unparseable_plugins:
        print submission

    print
    print '%s submissions are new plugins:' % len(new_plugins)
    for submission in new_plugins:
        print json.dumps(submission, indent=2)

if __name__ == '__main__':
    main()

Add a script to easily view new submitted plugins

Summary: That is, submitted plugins that we don't already know about and are non-empty.

Test Plan:
- imported the `submitted_plugins` table from prod
- ran `PYTHONPATH=. python tools/clean_submitted_plugins.py`
- saw the output looked reasonable

Reviewers: xymostech, spicyj

Reviewed By: spicyj

Differential Revision: http://phabricator.benalpert.com/D213

Response:
"""Prints out submitted plugins that we don't already know about.

Also deletes any empty submissions.
"""

import json
import re

import rethinkdb as r

import db.github_repos
import db.util

r_conn = db.util.r_conn

_GITHUB_LINK_REGEX = re.compile(r'github.com/(.*?)/([^/?#]*)')

def delete_empty_submissions():
    """Delete submitted plugins that don't have enough info for us to act on.

    Since we have no form validation, many submissions are just people who
    click the "submit" button.
    """
    deleted = r.table('submitted_plugins').filter({
        'name': '',
        'author': '',
        'github-link': '',
        'vimorg-link': '',
    }).delete().run(r_conn())

    print 'Deleted empty submissions:'
    print deleted

def main():
    delete_empty_submissions()

    known_vimorg_plugins = []
    known_github_plugins = []
    new_plugins = []
    unparseable_plugins = []

    submissions = r.table('submitted_plugins').run(r_conn())
    for submission in submissions:
        if submission['vimorg-link']:
            known_vimorg_plugins.append(submission)
            continue

        github_link = submission['github-link']
        if github_link:
            matches = _GITHUB_LINK_REGEX.findall(github_link)
            if not matches:
                unparseable_plugins.append(submission)
                continue

            repo_owner, repo_name = matches[0]
            db_repo = db.github_repos.PluginGithubRepos.get_with_owner_repo(
                    repo_owner, repo_name)

            if db_repo:
                known_github_plugins.append(submission)
            else:
                new_plugins.append(submission)

    print
    print '%s submissions are known vim.org plugins' % len(
            known_vimorg_plugins)
    print '%s submissions are known github.com plugins' % len(
            known_github_plugins)

    print
    print '%s submissions have unparseable github.com links:' % len(
            unparseable_plugins)
    for submission in unparseable_plugins:
        print submission

    print
    print '%s submissions are new plugins:' % len(new_plugins)
    for submission in new_plugins:
        print json.dumps(submission, indent=2)

if __name__ == '__main__':
    main()

Prompt:
Add a script to easily view new submitted plugins

Summary: That is, submitted plugins that we don't already know about and are non-empty.

Test Plan:
- imported the `submitted_plugins` table from prod
- ran `PYTHONPATH=. python tools/clean_submitted_plugins.py`
- saw the output looked reasonable

Reviewers: xymostech, spicyj

Reviewed By: spicyj

Differential Revision: http://phabricator.benalpert.com/D213


import pytest

from django.test import TestCase
from django.core.urlresolvers import reverse

from molo.core.tests.base import MoloTestCaseMixin

@pytest.mark.django_db
class TestImportFromGit(TestCase, MoloTestCaseMixin):

    def setUp(self):
        self.mk_main()
        self.user = self.login()

    def test_wagtail_has_import_menu_item(self):
        response = self.client.get(reverse('import-from-git'))
        self.assertContains(
            response, 'Import content from a Universal Core site')

Fix import UI django view's tests

Fix import UI django view's tests

Response:
import pytest

from django.test import TestCase
from django.core.urlresolvers import reverse

from molo.core.tests.base import MoloTestCaseMixin

@pytest.mark.django_db
class TestImportFromGit(TestCase, MoloTestCaseMixin):

    def setUp(self):
        self.mk_main()
        self.user = self.login()

    def test_wagtail_has_import_menu_item(self):
        response = self.client.get(reverse('import-from-git'))
        self.assertContains(
            response, 'Import content from a Universal Core site')

Prompt:
Fix import UI django view's tests

import pytest

from django.test import TestCase
from django.core.urlresolvers import reverse

from molo.core.tests.base import MoloTestCaseMixin

@pytest.mark.django_db
class TestImportFromGit(TestCase, MoloTestCaseMixin):

    def setUp(self):
        self.mk_main()
        self.user = self.login()

    def test_wagtail_has_import_menu_item(self):
        response = self.client.get(reverse("import-from-git"))
        self.assertContains(response, 'Import content from Git')


# coding=utf-8
from setuptools import setup, find_packages

setup(
    name="git-up",
    version="1.3.0",
    packages=find_packages(exclude=["tests"]),
    scripts=['PyGitUp/gitup.py'],
    install_requires=['GitPython==1.0.0', 'colorama==0.3.3',
                      'termcolor==1.1.0', 'docopt==0.6.2',
                      'six==1.9.0'],

    # Tests
    test_suite="nose.collector",
    tests_require='nose',

    # Executable
    entry_points={
        'console_scripts': [
            'git-up = PyGitUp.gitup:run'
        ]
    },

    # Additional data
    package_data={
        'PyGitUp': ['check-bundler.rb'],
        '': ['README.rst', 'LICENCE']
    },

    zip_safe=False,

    # Metadata
    author="Markus Siemens",
    author_email="markus@m-siemens.de",
    description="A python implementation of 'git up'",
    license="MIT",
    keywords="git git-up",
    url="https://github.com/msiemens/PyGitUp",
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Environment :: Console",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Topic :: Software Development :: Version Control",
        "Topic :: Utilities"
    ],

    long_description=open('README.rst').read()
)

Use full module path for entry point

Use full module path for entry point

Response:
# coding=utf-8
from setuptools import setup, find_packages

setup(
    name="git-up",
    version="1.3.0",
    packages=find_packages(exclude=["tests"]),
    scripts=['PyGitUp/gitup.py'],
    install_requires=['GitPython==1.0.0', 'colorama==0.3.3',
                      'termcolor==1.1.0', 'docopt==0.6.2',
                      'six==1.9.0'],

    # Tests
    test_suite="nose.collector",
    tests_require='nose',

    # Executable
    entry_points={
        'console_scripts': [
            'git-up = PyGitUp.gitup:run'
        ]
    },

    # Additional data
    package_data={
        'PyGitUp': ['check-bundler.rb'],
        '': ['README.rst', 'LICENCE']
    },

    zip_safe=False,

    # Metadata
    author="Markus Siemens",
    author_email="markus@m-siemens.de",
    description="A python implementation of 'git up'",
    license="MIT",
    keywords="git git-up",
    url="https://github.com/msiemens/PyGitUp",
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Environment :: Console",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Topic :: Software Development :: Version Control",
        "Topic :: Utilities"
    ],

    long_description=open('README.rst').read()
)

Prompt:
Use full module path for entry point

# coding=utf-8
from setuptools import setup, find_packages

setup(
    name="git-up",
    version="1.3.0",
    packages=find_packages(exclude=["tests"]),
    scripts=['PyGitUp/gitup.py'],
    install_requires=['GitPython==1.0.0', 'colorama==0.3.3',
                      'termcolor==1.1.0', 'docopt==0.6.2',
                      'six==1.9.0'],

    # Tests
    test_suite="nose.collector",
    tests_require='nose',

    # Executable
    entry_points={
        'console_scripts': [
            'git-up = gitup:run'
        ]
    },

    # Additional data
    package_data={
        'PyGitUp': ['check-bundler.rb'],
        '': ['README.rst', 'LICENCE']
    },

    zip_safe=False,

    # Metadata
    author="Markus Siemens",
    author_email="markus@m-siemens.de",
    description="A python implementation of 'git up'",
    license="MIT",
    keywords="git git-up",
    url="https://github.com/msiemens/PyGitUp",
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Environment :: Console",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Topic :: Software Development :: Version Control",
        "Topic :: Utilities"
    ],

    long_description=open('README.rst').read()
)


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

from django.forms import Form

from .fields import UploadField

class TestUploadForm(Form):
    test_files = UploadField(
        extensions=['.vcf'],
    )

Add a test form object for testing

Add a test form object for testing

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

from django.forms import Form

from .fields import UploadField

class TestUploadForm(Form):
    test_files = UploadField(
        extensions=['.vcf'],
    )

Prompt: Add a test form object for testing


import unittest
import os

os.environ['OGN_CONFIG_MODULE'] = 'config/test.py'

from ogn_python import db   # noqa: E402

class TestBaseDB(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        db.drop_all()
        db.session.execute('CREATE EXTENSION IF NOT EXISTS postgis;')
        db.session.commit()
        db.create_all()

    def setUp(self):
        pass

    def tearDown(self):
        db.session.execute("""
            DELETE FROM aircraft_beacons;
            DELETE FROM receiver_beacons;
            DELETE FROM takeoff_landings;
            DELETE FROM logbook;
            DELETE FROM receiver_coverages;
            DELETE FROM device_stats;
            DELETE FROM receiver_stats;
            DELETE FROM receivers;
            DELETE FROM devices;
        """)

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

Drop db before each test

Drop db before each test

Response:
import unittest
import os

os.environ['OGN_CONFIG_MODULE'] = 'config/test.py'

from ogn_python import db   # noqa: E402

class TestBaseDB(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        db.drop_all()
        db.session.execute('CREATE EXTENSION IF NOT EXISTS postgis;')
        db.session.commit()
        db.create_all()

    def setUp(self):
        pass

    def tearDown(self):
        db.session.execute("""
            DELETE FROM aircraft_beacons;
            DELETE FROM receiver_beacons;
            DELETE FROM takeoff_landings;
            DELETE FROM logbook;
            DELETE FROM receiver_coverages;
            DELETE FROM device_stats;
            DELETE FROM receiver_stats;
            DELETE FROM receivers;
            DELETE FROM devices;
        """)

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

Prompt:
Drop db before each test

import unittest
import os

os.environ['OGN_CONFIG_MODULE'] = 'config/test.py'

from ogn_python import db   # noqa: E402

class TestBaseDB(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        db.session.execute('CREATE EXTENSION IF NOT EXISTS postgis;')
        db.session.commit()
        db.create_all()

    def setUp(self):
        pass

    def tearDown(self):
        db.session.execute("""
            DELETE FROM aircraft_beacons;
            DELETE FROM receiver_beacons;
            DELETE FROM takeoff_landings;
            DELETE FROM logbook;
            DELETE FROM receiver_coverages;
            DELETE FROM device_stats;
            DELETE FROM receiver_stats;
            DELETE FROM receivers;
            DELETE FROM devices;
        """)

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


import numpy as np
from sklearn.cross_validation import cross_val_score
from sklearn.externals import joblib as pickle

class scorer(object):
    def __init__(self, model_instance, descriptor_generator_instance):
        self.model = model_instance
        self.descriptor_generator = descriptor_generator_instance

    def fit(self, ligands, target):
        self.train_descs = self.descriptor_generator.build(ligands)
        self.train_target = target
        return self.model.fit(descs,target)

    def predict(self, ligands):
        descs = self.descriptor_generator.build(ligands)
        return self.model.predict(descs)

    def score(self, ligands, target):
        descs = self.descriptor_generator.build(ligands)
        return self.model.score(descs,target)

    def cross_validate(n = 10, test_set = None, test_target = None):
        if test_set and test_target:
            cv_set = np.vstack((self.train_descs, test_set))
            cv_target = np.vstack((self.train_target, test_target))
        else:
            cv_set = self.train_descs
            cv_target = self.train_target
        return cross_val_score(self.model, cv_set, cv_target, cv = n)

    def save(filename):
        f = open(filename,'wb')
        pickle.dump(self, filename)
        f.close()

    @classmethod
    def load(filename):
        return pickle.load(open(filename,'rb'))

Response:
import numpy as np
from sklearn.cross_validation import cross_val_score
from sklearn.externals import joblib as pickle

class scorer(object):
    def __init__(self, model_instance, descriptor_generator_instance):
        self.model = model_instance
        self.descriptor_generator = descriptor_generator_instance

    def fit(self, ligands, target):
        self.train_descs = self.descriptor_generator.build(ligands)
        self.train_target = target
        return self.model.fit(descs,target)

    def predict(self, ligands):
        descs = self.descriptor_generator.build(ligands)
        return self.model.predict(descs)

    def score(self, ligands, target):
        descs = self.descriptor_generator.build(ligands)
        return self.model.score(descs,target)

    def cross_validate(n = 10, test_set = None, test_target = None):
        if test_set and test_target:
            cv_set = np.vstack((self.train_descs, test_set))
            cv_target = np.vstack((self.train_target, test_target))
        else:
            cv_set = self.train_descs
            cv_target = self.train_target
        return cross_val_score(self.model, cv_set, cv_target, cv = n)

    def save(filename):
        f = open(filename,'wb')
        pickle.dump(self, filename)
        f.close()

    @classmethod
    def load(filename):
        return pickle.load(open(filename,'rb'))

Prompt:
Make scorer accept instances of model and desc. gen.

import numpy as np
from sklearn.cross_validation import cross_val_score
from sklearn.externals import joblib as pickle

class scorer(object):
    def __init__(self, model, descriptor_generator, model_opts = {}, desc_opts = {}):
        self.model = model()
        self.descriptor_generator = descriptor_generator(**desc_opts)

    def fit(self, ligands, target):
        self.train_descs = self.descriptor_generator.build(ligands)
        self.train_target = target
        return self.model.fit(descs,target)

    def predict(self, ligands):
        descs = self.descriptor_generator.build(ligands)
        return self.model.predict(descs)

    def score(self, ligands, target):
        descs = self.descriptor_generator.build(ligands)
        return self.model.score(descs,target)

    def cross_validate(n = 10, test_set = None, test_target = None):
        if test_set and test_target:
            cv_set = np.vstack((self.train_descs, test_set))
            cv_target = np.vstack((self.train_target, test_target))
        else:
            cv_set = self.train_descs
            cv_target = self.train_target
        return cross_val_score(self.model, cv_set, cv_target, cv = n)

    def save(filename):
        f = open(filename,'wb')
        pickle.dump(self, filename)
        f.close()

    @classmethod
    def load(filename):
        return pickle.load(open(filename,'rb'))


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

from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

    dependencies = [
        ('wagtailimages', '0013_make_rendition_upload_callable'),
        ('catalogue', '0010_auto_20160616_1048'),
    ]

    operations = [
        migrations.AddField(
            model_name='category',
            name='description',
            field=models.TextField(verbose_name='Description', blank=True),
        ),
        migrations.AddField(
            model_name='category',
            name='image',
            field=models.ForeignKey(related_name='+', on_delete=django.db.models.deletion.SET_NULL, blank=True, to='wagtailimages.Image', null=True),
        ),
        migrations.AddField(
            model_name='category',
            name='name',
            field=models.CharField(max_length=255, verbose_name='Name', db_index=True),
        ),
    ]

Add field instead of alter field

Add field instead of alter field

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

from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

    dependencies = [
        ('wagtailimages', '0013_make_rendition_upload_callable'),
        ('catalogue', '0010_auto_20160616_1048'),
    ]

    operations = [
        migrations.AddField(
            model_name='category',
            name='description',
            field=models.TextField(verbose_name='Description', blank=True),
        ),
        migrations.AddField(
            model_name='category',
            name='image',
            field=models.ForeignKey(related_name='+', on_delete=django.db.models.deletion.SET_NULL, blank=True, to='wagtailimages.Image', null=True),
        ),
        migrations.AddField(
            model_name='category',
            name='name',
            field=models.CharField(max_length=255, verbose_name='Name', db_index=True),
        ),
    ]

Prompt:
Add field instead of alter field

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

from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

    dependencies = [
        ('wagtailimages', '0013_make_rendition_upload_callable'),
        ('catalogue', '0010_auto_20160616_1048'),
    ]

    operations = [
        migrations.AddField(
            model_name='category',
            name='description',
            field=models.TextField(verbose_name='Description', blank=True),
        ),
        migrations.AddField(
            model_name='category',
            name='image',
            field=models.ForeignKey(related_name='+', on_delete=django.db.models.deletion.SET_NULL, blank=True, to='wagtailimages.Image', null=True),
        ),
        migrations.AlterField(
            model_name='category',
            name='name',
            field=models.CharField(max_length=255, verbose_name='Name', db_index=True),
        ),
    ]