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

from setuptools import setup, find_packages

def get_deps():
  import re

  deps_raw = [ line.strip() for line in open("deps.txt")]
  deps = []
  for dep in deps_raw:
    if not dep or dep.startswith("#"):
      continue
    m = re.search("#egg=(.*)", dep)
    if m:
      dep = m.group(1)
    deps.append(dep)
  return deps

metadata = dict(
  name='Yaka Core',
  version='0.1dev',
  url='http://www.yaka.biz/',
  license='LGPL',
  author='Stefane Fermigier',
  author_email='sf@fermigier.com',
  description='Enterprise social networking meets CRM',
  long_description=__doc__,
  packages=['yaka'],
  platforms='any',
  classifiers=[
    'Development Status :: 3 - Alpha',
    'Environment :: Web Environment',
    'Intended Audience :: Developers',
    'License :: OSI Approved :: BSD License',
    'Operating System :: OS Independent',
    'Programming Language :: Python',
    ],
  # Setuptools specific
  install_requires=get_deps(),
  include_package_data=True,
  zip_safe=False,
)

setup(**metadata)

Fix build broken by previous commit.

Fix build broken by previous commit.

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

from setuptools import setup, find_packages

def get_deps():
  import re

  deps_raw = [ line.strip() for line in open("deps.txt")]
  deps = []
  for dep in deps_raw:
    if not dep or dep.startswith("#"):
      continue
    m = re.search("#egg=(.*)", dep)
    if m:
      dep = m.group(1)
    deps.append(dep)
  return deps

metadata = dict(
  name='Yaka Core',
  version='0.1dev',
  url='http://www.yaka.biz/',
  license='LGPL',
  author='Stefane Fermigier',
  author_email='sf@fermigier.com',
  description='Enterprise social networking meets CRM',
  long_description=__doc__,
  packages=['yaka'],
  platforms='any',
  classifiers=[
    'Development Status :: 3 - Alpha',
    'Environment :: Web Environment',
    'Intended Audience :: Developers',
    'License :: OSI Approved :: BSD License',
    'Operating System :: OS Independent',
    'Programming Language :: Python',
    ],
  # Setuptools specific
  install_requires=get_deps(),
  include_package_data=True,
  zip_safe=False,
)

setup(**metadata)

Prompt:
Fix build broken by previous commit.

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

from setuptools import setup, find_packages

deps = [ line.strip()
         for line in open("deps.txt")
         if line and not line.startswith("#") ]

metadata = dict(
  name='Yaka Core',
  version='0.1dev',
  url='http://www.yaka.biz/',
  license='LGPL',
  author='Stefane Fermigier',
  author_email='sf@fermigier.com',
  description='Enterprise social networking meets CRM',
  long_description=__doc__,
  packages=['yaka'],
  platforms='any',
  classifiers=[
    'Development Status :: 3 - Alpha',
    'Environment :: Web Environment',
    'Intended Audience :: Developers',
    'License :: OSI Approved :: BSD License',
    'Operating System :: OS Independent',
    'Programming Language :: Python',
    ],
  # Unsupported by distutils.
  #install_requires=deps,
  #include_package_data=True,
  #zip_safe=False,
)

setup(**metadata)


from __future__ import absolute_import, unicode_literals

from mopidy import config

def test_core_schema_has_cache_dir():
    assert 'cache_dir' in config._core_schema
    assert isinstance(config._core_schema['cache_dir'], config.Path)

def test_core_schema_has_config_dir():
    assert 'config_dir' in config._core_schema
    assert isinstance(config._core_schema['config_dir'], config.Path)

def test_core_schema_has_data_dir():
    assert 'data_dir' in config._core_schema
    assert isinstance(config._core_schema['data_dir'], config.Path)

def test_core_schema_has_max_tracklist_length():
    assert 'max_tracklist_length' in config._core_schema
    max_tracklist_length_schema = config._core_schema['max_tracklist_length']
    assert isinstance(max_tracklist_length_schema, config.Integer)
    assert max_tracklist_length_schema._minimum == 1
    assert max_tracklist_length_schema._maximum == 10000

tests: Add tests for defaults in core schema config

Tests check if default core schema has cache_dir, config_dir, data_dir and max_tracklist_length and if they have proper type

Response:
from __future__ import absolute_import, unicode_literals

from mopidy import config

def test_core_schema_has_cache_dir():
    assert 'cache_dir' in config._core_schema
    assert isinstance(config._core_schema['cache_dir'], config.Path)

def test_core_schema_has_config_dir():
    assert 'config_dir' in config._core_schema
    assert isinstance(config._core_schema['config_dir'], config.Path)

def test_core_schema_has_data_dir():
    assert 'data_dir' in config._core_schema
    assert isinstance(config._core_schema['data_dir'], config.Path)

def test_core_schema_has_max_tracklist_length():
    assert 'max_tracklist_length' in config._core_schema
    max_tracklist_length_schema = config._core_schema['max_tracklist_length']
    assert isinstance(max_tracklist_length_schema, config.Integer)
    assert max_tracklist_length_schema._minimum == 1
    assert max_tracklist_length_schema._maximum == 10000

Prompt:
tests: Add tests for defaults in core schema config

Tests check if default core schema has cache_dir, config_dir, data_dir and max_tracklist_length and if they have proper type


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from numpy.testing import assert_approx_equal, assert_allclose, assert_array_less
from nose.tools import raises, assert_true, assert_equal
from UliEngineering.Physics.NTC import *
from UliEngineering.Exceptions import *
import functools
import numpy as np

class TestNTC(object):
    def test_ntc_resistance(self):
        # Values arbitrarily from Murata NCP15WB473D03RC
        assert_approx_equal(ntc_resistance("47k", "4050K", "25°C"), 47000)
        assert_approx_equal(ntc_resistance("47k", "4050K", "0°C"), 162942.79)
        assert_approx_equal(ntc_resistance("47k", "4050K", "-18°C"), 463773.791)
        assert_approx_equal(ntc_resistance("47k", "4050K", "5°C"), 124819.66)
        assert_approx_equal(ntc_resistance("47k", "4050K", "60°C"), 11280.407)

Add more NTC unit tests

Add more NTC unit tests

Response:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from numpy.testing import assert_approx_equal, assert_allclose, assert_array_less
from nose.tools import raises, assert_true, assert_equal
from UliEngineering.Physics.NTC import *
from UliEngineering.Exceptions import *
import functools
import numpy as np

class TestNTC(object):
    def test_ntc_resistance(self):
        # Values arbitrarily from Murata NCP15WB473D03RC
        assert_approx_equal(ntc_resistance("47k", "4050K", "25°C"), 47000)
        assert_approx_equal(ntc_resistance("47k", "4050K", "0°C"), 162942.79)
        assert_approx_equal(ntc_resistance("47k", "4050K", "-18°C"), 463773.791)
        assert_approx_equal(ntc_resistance("47k", "4050K", "5°C"), 124819.66)
        assert_approx_equal(ntc_resistance("47k", "4050K", "60°C"), 11280.407)

Prompt:
Add more NTC unit tests

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from numpy.testing import assert_approx_equal, assert_allclose, assert_array_less
from nose.tools import raises, assert_true, assert_equal
from UliEngineering.Physics.NTC import *
from UliEngineering.Exceptions import *
import functools
import numpy as np

class TestNTC(object):
    def test_ntc_resistance(self):
        # Values arbitrarily from Murata NCP15WB473D03RC
        assert_approx_equal(ntc_resistance("47k", "4050K", "25°C"), 47000)
        assert_approx_equal(ntc_resistance("47k", "4050K", "0°C"), 162942.79)
        assert_approx_equal(ntc_resistance("47k", "4050K", "5°C"), 124819.66)
        assert_approx_equal(ntc_resistance("47k", "4050K", "60°C"), 11280.407)


import numpy as np
import pandas as pd
import sys
import os
from sklearn.externals import joblib
from sklearn.ensemble import RandomForestClassifier

scriptpath = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../'
sys.path.append(os.path.abspath(scriptpath))
import utils

parameter_str = '_'.join(['top', str(utils.k), 'cw', str(utils.click_weight), 'year', utils.train_year])
cforest = joblib.load(utils.model_path + 'rf_all_without_time_' + parameter_str +'.pkl')

test = joblib.load(utils.processed_data_path + 'test_all_' + parameter_str +'.pkl')
#X_test = test.ix[:,1:]
X_test = test.ix[:9,1:]

X_test.fillna(-1, inplace=True)

print "predict RandomForest Classifier..."

probs = cforest.predict_proba(X_test)
sorted_index = np.argsort(-np.array(probs))[:,:5]

result = pd.DataFrame(columns = {'hotel_cluster'})

result['hotel_cluster'] = np.array([np.array_str(sorted_index[i])[1:-1] for i in range(sorted_index.shape[0])])

result.hotel_cluster.to_csv(utils.model_path +
	'results/submission_rf_all_without_time_' + parameter_str + '.csv', header=True, index_label='id')

Handle missing value in predit rf

Handle missing value in predit rf

Response:
import numpy as np
import pandas as pd
import sys
import os
from sklearn.externals import joblib
from sklearn.ensemble import RandomForestClassifier

scriptpath = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../'
sys.path.append(os.path.abspath(scriptpath))
import utils

parameter_str = '_'.join(['top', str(utils.k), 'cw', str(utils.click_weight), 'year', utils.train_year])
cforest = joblib.load(utils.model_path + 'rf_all_without_time_' + parameter_str +'.pkl')

test = joblib.load(utils.processed_data_path + 'test_all_' + parameter_str +'.pkl')
#X_test = test.ix[:,1:]
X_test = test.ix[:9,1:]

X_test.fillna(-1, inplace=True)

print "predict RandomForest Classifier..."

probs = cforest.predict_proba(X_test)
sorted_index = np.argsort(-np.array(probs))[:,:5]

result = pd.DataFrame(columns = {'hotel_cluster'})

result['hotel_cluster'] = np.array([np.array_str(sorted_index[i])[1:-1] for i in range(sorted_index.shape[0])])

result.hotel_cluster.to_csv(utils.model_path +
	'results/submission_rf_all_without_time_' + parameter_str + '.csv', header=True, index_label='id')

Prompt:
Handle missing value in predit rf

import numpy as np
import pandas as pd
import sys
import os
from sklearn.externals import joblib
from sklearn.ensemble import RandomForestClassifier

scriptpath = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../'
sys.path.append(os.path.abspath(scriptpath))
import utils

parameter_str = '_'.join(['top', str(utils.k), 'cw', str(utils.click_weight), 'year', utils.train_year])
cforest = joblib.load(utils.model_path + 'rf_all_without_time_' + parameter_str +'.pkl')

test = joblib.load(utils.processed_data_path + 'test_all_' + parameter_str +'.pkl')
X_test = test.ix[:,1:]

print "predict RandomForest Classifier..."

probs = cforest.predict_proba(X_test)
sorted_index = np.argsort(-np.array(probs))[:,:5]

result = pd.DataFrame(columns = {'hotel_cluster'})

result['hotel_cluster'] = np.array([np.array_str(sorted_index[i])[1:-1] for i in range(sorted_index.shape[0])])

result.hotel_cluster.to_csv(utils.model_path +
	'results/submission_rf_all_without_time_' + parameter_str + '.csv', header=True, index_label='id')


"""Test file for the ordered dictionary module, odict.py."""

from neuroimaging.externals.scipy.testing import *

from neuroimaging.utils.odict import odict

class TestOdict(TestCase):
    def setUp(self):
        print 'setUp'
        self.thedict = odict((('one', 1.0), ('two', 2.0), ('three', 3.0)))

    def test_copy(self):
        """Test odict.copy method."""
        print self.thedict
        cpydict = self.thedict.copy()
        assert cpydict == self.thedict
        # test that it's a copy and not a reference
        assert cpydict is not self.thedict

if __name__ == "__main__":
    nose.runmodule()

Fix nose call so tests run in __main__.

Response:
"""Test file for the ordered dictionary module, odict.py."""

from neuroimaging.externals.scipy.testing import *

from neuroimaging.utils.odict import odict

class TestOdict(TestCase):
    def setUp(self):
        print 'setUp'
        self.thedict = odict((('one', 1.0), ('two', 2.0), ('three', 3.0)))

    def test_copy(self):
        """Test odict.copy method."""
        print self.thedict
        cpydict = self.thedict.copy()
        assert cpydict == self.thedict
        # test that it's a copy and not a reference
        assert cpydict is not self.thedict

if __name__ == "__main__":
    nose.runmodule()

Prompt:
BUG: Fix nose call so tests run in __main__.
"""Test file for the ordered dictionary module, odict.py."""

from neuroimaging.externals.scipy.testing import *

from neuroimaging.utils.odict import odict

class TestOdict(TestCase):
    def setUp(self):
        print 'setUp'
        self.thedict = odict((('one', 1.0), ('two', 2.0), ('three', 3.0)))

    def test_copy(self):
        """Test odict.copy method."""
        print self.thedict
        cpydict = self.thedict.copy()
        assert cpydict == self.thedict
        # test that it's a copy and not a reference
        assert cpydict is not self.thedict

if __name__ == "__main__":
    nose.run(argv=['', __file__])


# -*- coding: utf-8 -*-
# Copyright (C) 2009  Renato Lima - Akretion
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

{
    'name': 'Brazilian Localisation ZIP Codes',
    'license': 'AGPL-3',
    'author': 'Akretion, Odoo Community Association (OCA)',
    'version': '9.0.1.0.0',
    'depends': [
        'l10n_br_base',
    ],
    'data': [
        'views/l10n_br_zip_view.xml',
        'views/res_partner_view.xml',
        'views/res_company_view.xml',
        'views/res_bank_view.xml',
        'wizard/l10n_br_zip_search_view.xml',
        'security/ir.model.access.csv',
    ],
    'test': [
        'test/zip_demo.yml'
    ],
    'category': 'Localization',
    'installable': True,
}

Change the version of module.

[MIG] Change the version of module.

Response:
# -*- coding: utf-8 -*-
# Copyright (C) 2009  Renato Lima - Akretion
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

{
    'name': 'Brazilian Localisation ZIP Codes',
    'license': 'AGPL-3',
    'author': 'Akretion, Odoo Community Association (OCA)',
    'version': '9.0.1.0.0',
    'depends': [
        'l10n_br_base',
    ],
    'data': [
        'views/l10n_br_zip_view.xml',
        'views/res_partner_view.xml',
        'views/res_company_view.xml',
        'views/res_bank_view.xml',
        'wizard/l10n_br_zip_search_view.xml',
        'security/ir.model.access.csv',
    ],
    'test': [
        'test/zip_demo.yml'
    ],
    'category': 'Localization',
    'installable': True,
}

Prompt:
[MIG] Change the version of module.

# -*- coding: utf-8 -*-
# Copyright (C) 2009  Renato Lima - Akretion
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

{
    'name': 'Brazilian Localisation ZIP Codes',
    'license': 'AGPL-3',
    'author': 'Akretion, Odoo Community Association (OCA)',
    'version': '8.0.1.0.1',
    'depends': [
        'l10n_br_base',
    ],
    'data': [
        'views/l10n_br_zip_view.xml',
        'views/res_partner_view.xml',
        'views/res_company_view.xml',
        'views/res_bank_view.xml',
        'wizard/l10n_br_zip_search_view.xml',
        'security/ir.model.access.csv',
    ],
    'test': ['test/zip_demo.yml'],
    'category': 'Localization',
    'installable': False,
}


import unittest, argparse
from echolalia.formatter.yamler import Formatter

class YamlerTestCase(unittest.TestCase):

  def setUp(self):
    self.parser = argparse.ArgumentParser()
    self.data = [{'char': chr(i), 'order': i - 96} for i in xrange(97, 100)]
    self.formatter = Formatter()

  def test_add_args(self):
    new_parser = self.formatter.add_args(self.parser)
    self.assertEqual(new_parser, self.parser)

  def test_marshall_header(self):
    new_parser = self.formatter.add_args(self.parser)
    args = new_parser.parse_args([])
    result = self.formatter.marshall(args, self.data)
    expect = "- char: a\n  order: 1\n- char: b\n  order: 2\n- char: c\n  order: 3\n"
    self.assertEqual(result, expect)

Add tests for formatter yaml

Add tests for formatter yaml

Response:
import unittest, argparse
from echolalia.formatter.yamler import Formatter

class YamlerTestCase(unittest.TestCase):

  def setUp(self):
    self.parser = argparse.ArgumentParser()
    self.data = [{'char': chr(i), 'order': i - 96} for i in xrange(97, 100)]
    self.formatter = Formatter()

  def test_add_args(self):
    new_parser = self.formatter.add_args(self.parser)
    self.assertEqual(new_parser, self.parser)

  def test_marshall_header(self):
    new_parser = self.formatter.add_args(self.parser)
    args = new_parser.parse_args([])
    result = self.formatter.marshall(args, self.data)
    expect = "- char: a\n  order: 1\n- char: b\n  order: 2\n- char: c\n  order: 3\n"
    self.assertEqual(result, expect)

Prompt: Add tests for formatter yaml


# coding=utf8

# Local modules
from common import debug
from common.action import action_classes
from common.telegram import telegram_utils

from dailydevo import cac_utils

class CACDevoAction(action_classes.Action):
    def identifier(self):
        return "/cacdevo"

    def name(self):
        return "Center for Action and Contemplation Devotional"

    def resolve(self, userObj):
        passage = cac_utils.get_cacdevo(userObj.get_version())

        if passage is not None:
            telegram_utils.send_msg(passage, userObj.get_uid())

        return True

def get():
    return [
        CACDevoAction()
    ]

Fix for CACDevo hidden functionality

Fix for CACDevo hidden functionality

Response:
# coding=utf8

# Local modules
from common import debug
from common.action import action_classes
from common.telegram import telegram_utils

from dailydevo import cac_utils

class CACDevoAction(action_classes.Action):
    def identifier(self):
        return "/cacdevo"

    def name(self):
        return "Center for Action and Contemplation Devotional"

    def resolve(self, userObj):
        passage = cac_utils.get_cacdevo(userObj.get_version())

        if passage is not None:
            telegram_utils.send_msg(passage, userObj.get_uid())

        return True

def get():
    return [
        CACDevoAction()
    ]

Prompt:
Fix for CACDevo hidden functionality

# coding=utf8

# Local modules
from common import debug
from common.action import action_classes
from common.telegram import telegram_utils

from dailydevo import cac_utils

class CACDevoAction(action_classes.Action):
    def identifier(self):
        return "/cacdevo"

    def name(self):
        return "Center for Action and Contemplation Devotional"

    def resolve(self, userObj):
        passage = cac_utils.get_cacdevo(userObj.get_version())

        if passage is not None:
            telegram_utils.send_msg(passage, userObj.get_uid())

def get():
    return [
        CACDevoAction()
    ]


from ovp_core.emails import BaseMail

class UserMail(BaseMail):
  """
  This class is responsible for firing emails for Users
  """
  def __init__(self, user, async_mail=None):
    super(UserMail, self).__init__(user.email, async_mail)

  def sendWelcome(self, context={}):
    """
    Sent when user registers
    """
    return self.sendEmail('welcome', 'Welcome', context)

  def sendRecoveryToken(self, context):
    """
    Sent when volunteer requests recovery token
    """
    context.update({
      'user_email': user.email
      })
    return self.sendEmail('recoveryToken', 'Password recovery', context)

Response:
from ovp_core.emails import BaseMail

class UserMail(BaseMail):
  """
  This class is responsible for firing emails for Users
  """
  def __init__(self, user, async_mail=None):
    super(UserMail, self).__init__(user.email, async_mail)

  def sendWelcome(self, context={}):
    """
    Sent when user registers
    """
    return self.sendEmail('welcome', 'Welcome', context)

  def sendRecoveryToken(self, context):
    """
    Sent when volunteer requests recovery token
    """
    context.update({
      'user_email': user.email
      })
    return self.sendEmail('recoveryToken', 'Password recovery', context)

Prompt:
Revert "fix getting user email for recoveryToken"

This reverts commit a47b098e4d644391213958f9e05c179a7410208d.

from ovp_core.emails import BaseMail

class UserMail(BaseMail):
  """
  This class is responsible for firing emails for Users
  """
  def __init__(self, user, async_mail=None):
    super(UserMail, self).__init__(user.email, async_mail)

  def sendWelcome(self, context={}):
    """
    Sent when user registers
    """
    return self.sendEmail('welcome', 'Welcome', context)

  def sendRecoveryToken(self, context):
    """
    Sent when volunteer requests recovery token
    """
    context.update({
      'user_email': self.email_address
      })
    return self.sendEmail('recoveryToken', 'Password recovery', context)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""pikalang module.

A brainfuck derivative based off the vocabulary of Pikachu from Pokemon.

Copyright (c) 2019 Blake Grotewold
"""

from __future__ import print_function

import sys
import os

from pikalang.interpreter import PikalangProgram

def load_source(file):
    if os.path.isfile(file):
        if os.path.splitext(file)[1] == ".pokeball":
            with open(file, "r") as pikalang_file:
                pikalang_data = pikalang_file.read()

            return pikalang_data

        else:
            print("pikalang: file is not a pokeball", file=sys.stderr)
            return False

    else:
        print("pikalang: file does not exist", file=sys.stderr)
        return False

def evaluate(source):
    """Run Pikalang system."""

    program = PikalangProgram(source)
    program.run()

Add proper printing for py2

Add proper printing for py2

Response:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""pikalang module.

A brainfuck derivative based off the vocabulary of Pikachu from Pokemon.

Copyright (c) 2019 Blake Grotewold
"""

from __future__ import print_function

import sys
import os

from pikalang.interpreter import PikalangProgram

def load_source(file):
    if os.path.isfile(file):
        if os.path.splitext(file)[1] == ".pokeball":
            with open(file, "r") as pikalang_file:
                pikalang_data = pikalang_file.read()

            return pikalang_data

        else:
            print("pikalang: file is not a pokeball", file=sys.stderr)
            return False

    else:
        print("pikalang: file does not exist", file=sys.stderr)
        return False

def evaluate(source):
    """Run Pikalang system."""

    program = PikalangProgram(source)
    program.run()

Prompt:
Add proper printing for py2

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""pikalang module.

A brainfuck derivative based off the vocabulary of Pikachu from Pokemon.

Copyright (c) 2019 Blake Grotewold
"""

import sys
import os

from pikalang.interpreter import PikalangProgram

def load_source(file):
    if os.path.isfile(file):
        if os.path.splitext(file)[1] == ".pokeball":
            with open(file, "r") as pikalang_file:
                pikalang_data = pikalang_file.read()

            return pikalang_data

        else:
            print("pikalang: file is not a pokeball", file=sys.stderr)
            return False

    else:
        print("pikalang: file does not exist", file=sys.stderr)
        return False

def evaluate(source):
    """Run Pikalang system."""

    program = PikalangProgram(source)
    program.run()


from typing import List
import json

class ETK(object):

    def __init__(self):
        pass

    def get_glossary(self, file_path) -> List[str]:
        """
        A glossary is a text file, one entry per line.

        Args:
            file_path (str): path to a text file containing a glossary.

        Returns: List of the strings in the glossary.
        """
        #to-do: this should be a list, not a dict
        res = dict()
        with open(file_path) as fp:
            line = fp.readline().rstrip('\n')
            while line:
                res[line] = line
                line = fp.readline().rstrip('\n')
        return res

    def invoke_extractor(self, extractor=None, doc=None, json_path=None, input_key=None, output_key=None):
        # cache parsed json_path, not a string, globally

        containers = doc.cdr_document['__content_strict']
        # containers = doc.select_containers(json_path)
        if isinstance(containers, list):
            for c in containers:
                segment = c.get(input_key)
                tokens = doc.get_tokens(segment)
        else:
            segment = containers.get(input_key)
            tokens = doc.get_tokens(segment)

        fake_extraction = [i.text for i in tokens]
        doc.store_extraction(extractor, fake_extraction, containers, output_key)
        print(json.dumps(doc.cdr_document, indent=2))
            # if extractor.requires_tokens():
            #     tokens = doc.get_tokens(segment, tokenizer=extractor.preferred_tokenizer())
            #     if tokens:
            #         extraction = extractor.extract(tokens, doc)
            #         doc.store_extraction(extractor, extraction, c, output_key)

Add comments and typing annotations

Add comments and typing annotations

Response:
from typing import List
import json

class ETK(object):

    def __init__(self):
        pass

    def get_glossary(self, file_path) -> List[str]:
        """
        A glossary is a text file, one entry per line.

        Args:
            file_path (str): path to a text file containing a glossary.

        Returns: List of the strings in the glossary.
        """
        #to-do: this should be a list, not a dict
        res = dict()
        with open(file_path) as fp:
            line = fp.readline().rstrip('\n')
            while line:
                res[line] = line
                line = fp.readline().rstrip('\n')
        return res

    def invoke_extractor(self, extractor=None, doc=None, json_path=None, input_key=None, output_key=None):
        # cache parsed json_path, not a string, globally

        containers = doc.cdr_document['__content_strict']
        # containers = doc.select_containers(json_path)
        if isinstance(containers, list):
            for c in containers:
                segment = c.get(input_key)
                tokens = doc.get_tokens(segment)
        else:
            segment = containers.get(input_key)
            tokens = doc.get_tokens(segment)

        fake_extraction = [i.text for i in tokens]
        doc.store_extraction(extractor, fake_extraction, containers, output_key)
        print(json.dumps(doc.cdr_document, indent=2))
            # if extractor.requires_tokens():
            #     tokens = doc.get_tokens(segment, tokenizer=extractor.preferred_tokenizer())
            #     if tokens:
            #         extraction = extractor.extract(tokens, doc)
            #         doc.store_extraction(extractor, extraction, c, output_key)

Prompt:
Add comments and typing annotations

import json

class ETK(object):

    def __init__(self):
        pass

    def get_glossary(self, file_path):
        res = dict()
        with open(file_path) as fp:
            line = fp.readline().rstrip('\n')
            while line:
                res[line] = line
                line = fp.readline().rstrip('\n')
        return res

    def invoke_extractor(self, extractor=None, doc=None, json_path=None, input_key=None, output_key=None):
        # cache parsed json_path, not a string, globally

        containers = doc.cdr_document['__content_strict']
        # containers = doc.select_containers(json_path)
        if isinstance(containers, list):
            for c in containers:
                segment = c.get(input_key)
                tokens = doc.get_tokens(segment)
        else:
            segment = containers.get(input_key)
            tokens = doc.get_tokens(segment)

        fake_extraction = [i.text for i in tokens]
        doc.store_extraction(extractor, fake_extraction, containers, output_key)
        print(json.dumps(doc.cdr_document, indent=2))
            # if extractor.requires_tokens():
            #     tokens = doc.get_tokens(segment, tokenizer=extractor.preferred_tokenizer())
            #     if tokens:
            #         extraction = extractor.extract(tokens, doc)
            #         doc.store_extraction(extractor, extraction, c, output_key)


import logging

log = logging.getLogger(__name__)

Remove flask usage in init

Remove flask usage in init

Response:
import logging

log = logging.getLogger(__name__)

Prompt:
Remove flask usage in init

import logging

from flask import Flask

log = logging.getLogger(__name__)

def create_app(config_dict):
    app = Flask(__name__, static_folder=None)
    app.config.update(**config_dict)

    from uiharu.api.views import api as api_blueprint
    from uiharu.weather.views import weather as weather_blueprint

    app.register_blueprint(api_blueprint, url_prefix='/api/v1')
    app.register_blueprint(weather_blueprint)

    log.info(app.url_map)

    return app


# Import blockmodels file
import BlockModels
import webapp2, jinja2, os
from datetime import *

jinja_environment = jinja2.Environment(autoescape=True,
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))

class CST(tzinfo):
    def utcoffset(self, dt):
        return timedelta(hours=-6)

    def tzname(self, dt):
        return "US/Central"

    def dst(self, dt):
        return timedelta(0)

cst = CST()

class Schedule_Handler(webapp2.RequestHandler):
    def get(self):
        schedule = BlockModels.schedule()
        tlocal = datetime.now(cst)
        formNow = datetime.strftime(tlocal, "%A, %b %d %I:%M:%S %p")
        template_values = {
            'schedule': schedule,
            'localtime': formNow,
        }

        template = jinja_environment.get_template('schedule.html')
        self.response.out.write(template.render(template_values))

app = webapp2.WSGIApplication([
    ('/schedule', Schedule_Handler)
], debug=True)

Response:
# Import blockmodels file
import BlockModels
import webapp2, jinja2, os
from datetime import *

jinja_environment = jinja2.Environment(autoescape=True,
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))

class CST(tzinfo):
    def utcoffset(self, dt):
        return timedelta(hours=-6)

    def tzname(self, dt):
        return "US/Central"

    def dst(self, dt):
        return timedelta(0)

cst = CST()

class Schedule_Handler(webapp2.RequestHandler):
    def get(self):
        schedule = BlockModels.schedule()
        tlocal = datetime.now(cst)
        formNow = datetime.strftime(tlocal, "%A, %b %d %I:%M:%S %p")
        template_values = {
            'schedule': schedule,
            'localtime': formNow,
        }

        template = jinja_environment.get_template('schedule.html')
        self.response.out.write(template.render(template_values))

app = webapp2.WSGIApplication([
    ('/schedule', Schedule_Handler)
], debug=True)

Prompt:
Change the MainHandler to Schedule_Handler and changed the “/“ to “/schedule”

# Import blockmodels file
import BlockModels
import webapp2, jinja2, os
from datetime import *

jinja_environment = jinja2.Environment(autoescape=True,
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))

class CST(tzinfo):
    def utcoffset(self, dt):
        return timedelta(hours=-6)

    def tzname(self, dt):
        return "US/Central"

    def dst(self, dt):
        return timedelta(0)

cst = CST()

class MainHandler(webapp2.RequestHandler):
    def get(self):
        schedule = BlockModels.schedule()
        tlocal = datetime.now(cst)
        formNow = datetime.strftime(tlocal, "%A, %b %d %I:%M:%S %p")
        template_values = {
            'schedule': schedule,
            'localtime': formNow,
        }

        template = jinja_environment.get_template('Prototype1.html')
        self.response.out.write(template.render(template_values))

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)


import numpy as np
from nose.tools import assert_true, assert_false, assert_equal, assert_raises
from numpy.testing import assert_array_equal, assert_array_almost_equal

import nibabel as nib
from dipy.data import get_data, get_sphere
from dipy.reconst.recspeed import peak_finding
from dipy.reconst.gqi import GeneralizedQSampling
from dipy.reconst.dsi import DiffusionSpectrumImaging

def test_dandelion():

    fimg,fbvals,fbvecs=get_data('small_101D')
    bvals=np.loadtxt(fbvals)
    gradients=np.loadtxt(fbvecs).T
    data=nib.load(fimg).get_data()

    """
    print(bvals.shape, gradients.shape, data.shape)
    sd=SphericalDandelion(data,bvals,gradients)

    sdf=sd.spherical_diffusivity(data[5,5,5])

    XA=sd.xa()
    np.set_printoptions(2)
    print XA.min(),XA.max(),XA.mean()
    print sdf*10**4
    """

TEST added test for dsi

TEST added test for dsi

Response:
import numpy as np
from nose.tools import assert_true, assert_false, assert_equal, assert_raises
from numpy.testing import assert_array_equal, assert_array_almost_equal

import nibabel as nib
from dipy.data import get_data, get_sphere
from dipy.reconst.recspeed import peak_finding
from dipy.reconst.gqi import GeneralizedQSampling
from dipy.reconst.dsi import DiffusionSpectrumImaging

def test_dandelion():

    fimg,fbvals,fbvecs=get_data('small_101D')
    bvals=np.loadtxt(fbvals)
    gradients=np.loadtxt(fbvecs).T
    data=nib.load(fimg).get_data()

    """
    print(bvals.shape, gradients.shape, data.shape)
    sd=SphericalDandelion(data,bvals,gradients)

    sdf=sd.spherical_diffusivity(data[5,5,5])

    XA=sd.xa()
    np.set_printoptions(2)
    print XA.min(),XA.max(),XA.mean()
    print sdf*10**4
    """

Prompt: TEST added test for dsi


import unittest

from app.calculator import Calculator

class TestCalculator(unittest.TestCase):

    def setUp(self):
        self.calc = Calculator()

    def test_calculator_addition_method_returns_correct_result(self):
        calc = Calculator()
        result = calc.addition(2,2)
        self.assertEqual(4, result)

    def test_calculator_subtraction_method_returns_correct_result(self):
        calc = Calculator()
        result = calc.substraction(4,2)
        self.assertEqual(2, result)

    def test_calculator_division_method_returns_correct_result(self):
        calc = Calculator()
        result = calc.division(5,2)
        self.assertEqual(2.5, result)

Add new test for division

Add new test for division

Response:
import unittest

from app.calculator import Calculator

class TestCalculator(unittest.TestCase):

    def setUp(self):
        self.calc = Calculator()

    def test_calculator_addition_method_returns_correct_result(self):
        calc = Calculator()
        result = calc.addition(2,2)
        self.assertEqual(4, result)

    def test_calculator_subtraction_method_returns_correct_result(self):
        calc = Calculator()
        result = calc.substraction(4,2)
        self.assertEqual(2, result)

    def test_calculator_division_method_returns_correct_result(self):
        calc = Calculator()
        result = calc.division(5,2)
        self.assertEqual(2.5, result)

Prompt:
Add new test for division

import unittest

from app.calculator import Calculator

class TestCalculator(unittest.TestCase):

    def setUp(self):
        self.calc = Calculator()

    def test_calculator_addition_method_returns_correct_result(self):
        calc = Calculator()
        result = calc.addition(2,2)
        self.assertEqual(4, result)

    def test_calculator_subtraction_method_returns_correct_result(self):
        calc = Calculator()
        result = calc.substraction(4,2)
        self.assertEqual(2, result)


#!/usr/bin/python
"""
T Macrina
160314

Make TIF images of all H5 files in directory
**All H5 files must have "img" group

Args:
	sys.argv[1]: full path to the H5 image directory

Returns:
	TIF files (extension changed to .tif) saved in the same directory
"""

from PIL import Image
import numpy as np
import h5py
import os
import sys

def h5_to_array(fn):
	"""Open H5 file with "img" group & convert to numpy ndarray of dtype

	Args:
		fn: filename (full path) of the image

	Returns:
		An ndarray of dtype
	"""
	f = h5py.File(fn, "r")
	return np.array(f["/main"]).T

def write_array_to_sections(fn, arr):
	"""Split 3d ndarray along z dim into 2d sections & save as tifs
	"""
	for i in range(arr.shape[2]):
		section = arr[:,:,i]
		new_fn = os.path.splitext(fn)[0] + "_%03d.tif" % (i+1)
		write_to_tif(new_fn, section)

def write_to_tif(fn, arr):
	"""Write ndarray to tif file
	"""
	img = Image.fromarray(arr)
	img.save(fn)

def main():
	"""Make TIF images of all H5 matrices in directory
	"""
	dir = os.getcwd()
	file = sys.argv[1]
	files = os.listdir(dir)
	if file.endswith(".h5") or file.endswith(".hdf5"):
		fn = os.path.join(dir, file)
		arr = h5_to_array(fn)
		write_array_to_sections(fn, arr)

if __name__ == '__main__':
	main()

Include Python script to convert 3D stack into dir of tif images

Response:
#!/usr/bin/python
"""
T Macrina
160314

Make TIF images of all H5 files in directory
**All H5 files must have "img" group

Args:
	sys.argv[1]: full path to the H5 image directory

Returns:
	TIF files (extension changed to .tif) saved in the same directory
"""

from PIL import Image
import numpy as np
import h5py
import os
import sys

def h5_to_array(fn):
	"""Open H5 file with "img" group & convert to numpy ndarray of dtype

	Args:
		fn: filename (full path) of the image

	Returns:
		An ndarray of dtype
	"""
	f = h5py.File(fn, "r")
	return np.array(f["/main"]).T

def write_array_to_sections(fn, arr):
	"""Split 3d ndarray along z dim into 2d sections & save as tifs
	"""
	for i in range(arr.shape[2]):
		section = arr[:,:,i]
		new_fn = os.path.splitext(fn)[0] + "_%03d.tif" % (i+1)
		write_to_tif(new_fn, section)

def write_to_tif(fn, arr):
	"""Write ndarray to tif file
	"""
	img = Image.fromarray(arr)
	img.save(fn)

def main():
	"""Make TIF images of all H5 matrices in directory
	"""
	dir = os.getcwd()
	file = sys.argv[1]
	files = os.listdir(dir)
	if file.endswith(".h5") or file.endswith(".hdf5"):
		fn = os.path.join(dir, file)
		arr = h5_to_array(fn)
		write_array_to_sections(fn, arr)

if __name__ == '__main__':
	main()

Prompt: Include Python script to convert 3D stack into dir of tif images


from datetime import datetime
from optparse import OptionParser
from urllib2 import urlopen

from BeautifulSoup import BeautifulSoup

if __name__ == '__main__':
    usage = "%prog <USERNAME> <WEB SERVER>"
    parser = OptionParser(usage=usage)
    opts,args = parser.parse_args()
    if len(args) != 2:
        parser.error("All arguments are required.")

    username,server = args
    if server[:7] != "http://":
        server = "http://%s" % (server,)
    soup = BeautifulSoup(urlopen('%s/user/%s/recent-tracks'
                                    % (server, username)))
    gobbles_list = soup.find('ul', {'class': 'gobbles'})
    timestamp = gobbles_list.find('li')['about'].split('#')[1].split('.')[0]
    print datetime.fromtimestamp(float(timestamp))

Add script to print time of most recent gobble.

Response:
from datetime import datetime
from optparse import OptionParser
from urllib2 import urlopen

from BeautifulSoup import BeautifulSoup

if __name__ == '__main__':
    usage = "%prog <USERNAME> <WEB SERVER>"
    parser = OptionParser(usage=usage)
    opts,args = parser.parse_args()
    if len(args) != 2:
        parser.error("All arguments are required.")

    username,server = args
    if server[:7] != "http://":
        server = "http://%s" % (server,)
    soup = BeautifulSoup(urlopen('%s/user/%s/recent-tracks'
                                    % (server, username)))
    gobbles_list = soup.find('ul', {'class': 'gobbles'})
    timestamp = gobbles_list.find('li')['about'].split('#')[1].split('.')[0]
    print datetime.fromtimestamp(float(timestamp))

Prompt: Add script to print time of most recent gobble.


from importlib import machinery

from django.template import loaders
from os import listdir
from os.path import dirname, splitext

MODULE_EXTENSIONS = tuple(machinery.all_suffixes())

def get_django_template_loaders():
    return [(loader.__name__.rsplit('.', 1)[1], loader)
            for loader in get_submodules(loaders) if hasattr(loader, 'Loader')]

def get_submodules(package):
    submodules = ("%s.%s" % (package.__name__, module) for module in package_contents(package))
    return [__import__(module, {}, {}, [module.rsplit(".", 1)[-1]]) for module in submodules]

def package_contents(package):
    package_path = dirname(loaders.__file__)
    contents = set([splitext(module)[0] for module in listdir(package_path) if module.endswith(MODULE_EXTENSIONS)])
    return contents

Response:
from importlib import machinery

from django.template import loaders
from os import listdir
from os.path import dirname, splitext

MODULE_EXTENSIONS = tuple(machinery.all_suffixes())

def get_django_template_loaders():
    return [(loader.__name__.rsplit('.', 1)[1], loader)
            for loader in get_submodules(loaders) if hasattr(loader, 'Loader')]

def get_submodules(package):
    submodules = ("%s.%s" % (package.__name__, module) for module in package_contents(package))
    return [__import__(module, {}, {}, [module.rsplit(".", 1)[-1]]) for module in submodules]

def package_contents(package):
    package_path = dirname(loaders.__file__)
    contents = set([splitext(module)[0] for module in listdir(package_path) if module.endswith(MODULE_EXTENSIONS)])
    return contents

Prompt:
Replace deprecated imp usage with importlib

import imp

from django.template import loaders
from os import listdir
from os.path import dirname, splitext

MODULE_EXTENSIONS = tuple([suffix[0] for suffix in imp.get_suffixes()])

def get_django_template_loaders():
    return [(loader.__name__.rsplit('.', 1)[1], loader)
            for loader in get_submodules(loaders) if hasattr(loader, 'Loader')]

def get_submodules(package):
    submodules = ("%s.%s" % (package.__name__, module) for module in package_contents(package))
    return [__import__(module, {}, {}, [module.rsplit(".", 1)[-1]]) for module in submodules]

def package_contents(package):
    package_path = dirname(loaders.__file__)
    contents = set([splitext(module)[0] for module in listdir(package_path) if module.endswith(MODULE_EXTENSIONS)])
    return contents


import django_rq
import logging
import os
import time

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'varify.conf.settings')

from django.core import management

log = logging.getLogger(__name__)

# We don't want this job to run forever so limit to this many attempts and
# if the queue is still not empty just give up.
MAX_ATTEMPTS = 100
# Time in seconds between polling the queue for the current count
POLL_DELAY = 300

queue = django_rq.get_queue('default')

attempts = 0

while attempts < MAX_ATTEMPTS:
    attempts += 1
    if queue.is_empty:
        management.call_command('samples', 'gene-ranks')
        log.debug('gene ranks updated')
        break
    log.debug('queue not empty, waiting {0} seconds'.format(POLL_DELAY))
    time.sleep(POLL_DELAY)
else:
    # Max attempts, log and exit
    log.error('Maximum attempts ({0}) made when trying to update gene ranks.'
              .format(MAX_ATTEMPTS))

Add script to call gene-ranks command after samples load

This will need to be added as a cron job like the recalculate_allele_frequencies script is used now.

Response:
import django_rq
import logging
import os
import time

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'varify.conf.settings')

from django.core import management

log = logging.getLogger(__name__)

# We don't want this job to run forever so limit to this many attempts and
# if the queue is still not empty just give up.
MAX_ATTEMPTS = 100
# Time in seconds between polling the queue for the current count
POLL_DELAY = 300

queue = django_rq.get_queue('default')

attempts = 0

while attempts < MAX_ATTEMPTS:
    attempts += 1
    if queue.is_empty:
        management.call_command('samples', 'gene-ranks')
        log.debug('gene ranks updated')
        break
    log.debug('queue not empty, waiting {0} seconds'.format(POLL_DELAY))
    time.sleep(POLL_DELAY)
else:
    # Max attempts, log and exit
    log.error('Maximum attempts ({0}) made when trying to update gene ranks.'
              .format(MAX_ATTEMPTS))

Prompt:
Add script to call gene-ranks command after samples load

This will need to be added as a cron job like the recalculate_allele_frequencies script is used now.


c = get_config()

# Tell IPython to use PostgresContentsManager.
c.NotebookApp.contents_manager_class = 'pgcontents.pgmanager.PostgresContentsManager'

# Set the url for the database used to store files.  See
# http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html#postgresql
# for more info on db url formatting.
# c.PostgresContentsManager.db_url = 'postgresql://ssanderson@/pgcontents'

# Set a user ID. Defaults to the result of getpass.getuser()
# c.PostgresContentsManager.user_id = 'my_awesome_username'

# Set a maximum file size, if desired.
# c.PostgresContentsManager.max_file_size_bytes = 1000000 # 1MB File cap

Add an example configuration file.

DOC: Add an example configuration file.

Response:
c = get_config()

# Tell IPython to use PostgresContentsManager.
c.NotebookApp.contents_manager_class = 'pgcontents.pgmanager.PostgresContentsManager'

# Set the url for the database used to store files.  See
# http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html#postgresql
# for more info on db url formatting.
# c.PostgresContentsManager.db_url = 'postgresql://ssanderson@/pgcontents'

# Set a user ID. Defaults to the result of getpass.getuser()
# c.PostgresContentsManager.user_id = 'my_awesome_username'

# Set a maximum file size, if desired.
# c.PostgresContentsManager.max_file_size_bytes = 1000000 # 1MB File cap

Prompt: DOC: Add an example configuration file.


from admin_honeypot import views
from django.urls import path, re_path

app_name = 'admin_honeypot'

urlpatterns = [
    path('login/', views.AdminHoneypot.as_view(), name='login'),
    re_path(r'^.*$', views.AdminHoneypot.as_view(), name='index'),
]

Update url() to path() in the urlconf.

Update url() to path() in the urlconf.

Response:
from admin_honeypot import views
from django.urls import path, re_path

app_name = 'admin_honeypot'

urlpatterns = [
    path('login/', views.AdminHoneypot.as_view(), name='login'),
    re_path(r'^.*$', views.AdminHoneypot.as_view(), name='index'),
]

Prompt:
Update url() to path() in the urlconf.

from admin_honeypot import views
from django.conf.urls import url

app_name = 'admin_honeypot'

urlpatterns = [
    url(r'^login/$', views.AdminHoneypot.as_view(), name='login'),
    url(r'^.*$', views.AdminHoneypot.as_view(), name='index'),
]


import mock
from prf.tests.prf_testcase import PrfTestCase
from prf.mongodb import get_document_cls, connect_dataset_aliases

class TestMongoDB(PrfTestCase):
    def setUp(self):
        super(TestMongoDB, self).setUp()
        self.drop_databases()
        self.unload_documents()

    def test_get_document_cls(self):
        cls = self.create_collection('default', 'col1')
        cls2 = self.create_collection('prf-test2', 'col2')
        cls3 = self.create_collection('default', 'col3')
        cls4 = self.create_collection('prf-test2', 'col3')

        dcls = get_document_cls('col1')
        dcls2 = get_document_cls('col2')
        dcls3 = get_document_cls('col3')
        assert cls == dcls
        assert cls2 == dcls2
        assert dcls2._meta['db_alias'] == 'prf-test2'
        # This is broken behavior with collision on collection names across dbs,
        # get_document_cls will return the most recently defined class with that name.
        assert dcls3 == cls4

    @mock.patch('prf.mongodb.mongo_connect')
    def test_connect_dataset_aliases_missing_config(self, connect):
        del self.conf.registry.settings['dataset.namespaces']
        connect_dataset_aliases(self.conf, self.conf.prf_settings())
        connect.assert_not_called()

Response:
import mock
from prf.tests.prf_testcase import PrfTestCase
from prf.mongodb import get_document_cls, connect_dataset_aliases

class TestMongoDB(PrfTestCase):
    def setUp(self):
        super(TestMongoDB, self).setUp()
        self.drop_databases()
        self.unload_documents()

    def test_get_document_cls(self):
        cls = self.create_collection('default', 'col1')
        cls2 = self.create_collection('prf-test2', 'col2')
        cls3 = self.create_collection('default', 'col3')
        cls4 = self.create_collection('prf-test2', 'col3')

        dcls = get_document_cls('col1')
        dcls2 = get_document_cls('col2')
        dcls3 = get_document_cls('col3')
        assert cls == dcls
        assert cls2 == dcls2
        assert dcls2._meta['db_alias'] == 'prf-test2'
        # This is broken behavior with collision on collection names across dbs,
        # get_document_cls will return the most recently defined class with that name.
        assert dcls3 == cls4

    @mock.patch('prf.mongodb.mongo_connect')
    def test_connect_dataset_aliases_missing_config(self, connect):
        del self.conf.registry.settings['dataset.namespaces']
        connect_dataset_aliases(self.conf, self.conf.prf_settings())
        connect.assert_not_called()

Prompt:
Make sure no crashes happen when no namespaces are set

from prf.tests.prf_testcase import PrfTestCase
from prf.mongodb import get_document_cls

class TestMongoDB(PrfTestCase):
    def setUp(self):
        super(TestMongoDB, self).setUp()
        self.drop_databases()
        self.unload_documents()

    def test_get_document_cls(self):
        cls = self.create_collection('default', 'col1')
        cls2 = self.create_collection('prf-test2', 'col2')
        cls3 = self.create_collection('default', 'col3')
        cls4 = self.create_collection('prf-test2', 'col3')

        dcls = get_document_cls('col1')
        dcls2 = get_document_cls('col2')
        dcls3 = get_document_cls('col3')
        assert cls == dcls
        assert cls2 == dcls2
        assert dcls2._meta['db_alias'] == 'prf-test2'
        # This is broken behavior with collision on collection names across dbs,
        # get_document_cls will return the most recently defined class with that name.
        assert dcls3 == cls4


from selenium import webdriver
import pytest

@pytest.fixture(scope='function')
def browser(request):
    browser_ = webdriver.Firefox()

    def fin():
        browser_.quit()
    request.addfinalizer(fin)

    return browser_

def test_can_show_a_relevant_code_snippet(browser):
    # Jan visits the site
    browser.get('http://localhost:8000')

    # He notices the title references the site name
    site_name = 'Scout'
    assert site_name.lower() in browser.title.lower()

    assert False, 'incomplete test'

Change FT to expect site name in title

Change FT to expect site name in title

Response:
from selenium import webdriver
import pytest

@pytest.fixture(scope='function')
def browser(request):
    browser_ = webdriver.Firefox()

    def fin():
        browser_.quit()
    request.addfinalizer(fin)

    return browser_

def test_can_show_a_relevant_code_snippet(browser):
    # Jan visits the site
    browser.get('http://localhost:8000')

    # He notices the title references the site name
    site_name = 'Scout'
    assert site_name.lower() in browser.title.lower()

    assert False, 'incomplete test'

Prompt:
Change FT to expect site name in title

from selenium import webdriver
import pytest

@pytest.fixture(scope='function')
def browser(request):
    browser_ = webdriver.Firefox()

    def fin():
        browser_.quit()
    request.addfinalizer(fin)

    return browser_

def test_can_show_a_relevant_code_snippet(browser):
    browser.get('http://localhost:8000')

    assert 'Django' in browser.title


import os
import json
import random
import flask
import requests
import hashlib

DNZ_URL = 'http://api.digitalnz.org/v3/records/'
DNZ_KEY = os.environ.get('DNZ_KEY')
records = {}

# TODO This should be switched to records associated with days.
# Create a hash table of all records.
for record in json.loads(open('data/records-2015.json').read())['records']:
    record_hash = hashlib.md5(str(record['id']).encode('utf-8')).hexdigest()
    records[record_hash] = record

app = flask.Flask(__name__)

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

@app.route('/hello')
def hello():
    return 'hello'

@app.route('/random')
def random_record():
    record_hash = random.choice(list(records.keys()))
    return str(get_metadata(records[record_hash]['id']))

def get_metadata(id):
    url = DNZ_URL + '{id}.json?api_key={key}'.format(id=id, key=DNZ_KEY)
    return requests.get(url).json()

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

Add method to query DNZ Metadata API

Add method to query DNZ Metadata API

Response:
import os
import json
import random
import flask
import requests
import hashlib

DNZ_URL = 'http://api.digitalnz.org/v3/records/'
DNZ_KEY = os.environ.get('DNZ_KEY')
records = {}

# TODO This should be switched to records associated with days.
# Create a hash table of all records.
for record in json.loads(open('data/records-2015.json').read())['records']:
    record_hash = hashlib.md5(str(record['id']).encode('utf-8')).hexdigest()
    records[record_hash] = record

app = flask.Flask(__name__)

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

@app.route('/hello')
def hello():
    return 'hello'

@app.route('/random')
def random_record():
    record_hash = random.choice(list(records.keys()))
    return str(get_metadata(records[record_hash]['id']))

def get_metadata(id):
    url = DNZ_URL + '{id}.json?api_key={key}'.format(id=id, key=DNZ_KEY)
    return requests.get(url).json()

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

Prompt:
Add method to query DNZ Metadata API

import os
import json
import random
import flask

from hashlib import md5

records = {}

# Create a hash table of all records.
for record in json.loads(open('data/records-2015.json').read())['records']:
    records[md5(str(record['id']).encode('utf-8')).hexdigest()] = record

app = flask.Flask(__name__)

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

@app.route('/hello')
def hello():
    return 'hello'

@app.route('/random')
def random_record():
    record_hash = random.choice(list(records.keys()))
    return str(records[record_hash])

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


import werkzeug.debug
import werkzeug.serving

from jacquard.commands import BaseCommand
from jacquard.service import get_wsgi_app

class RunServer(BaseCommand):
    help = "run a (local, debug) server"

    def add_arguments(self, parser):
        parser.add_argument(
            '-p',
            '--port',
            type=int,
            default=8888,
            help="port to bind to",
        )
        parser.add_argument(
            '-b',
            '--bind',
            type=str,
            default='::1',
            help="address to bind to",
        )

    def handle(self, config, options):
        app = get_wsgi_app(config)

        werkzeug.serving.run_simple(
            options.bind,
            options.port,
            app,
            use_reloader=True,
            use_debugger=True,
            use_evalex=True,
            threaded=False,
            processes=1,
        )

Make this derive from the correct type

Make this derive from the correct type

Response:
import werkzeug.debug
import werkzeug.serving

from jacquard.commands import BaseCommand
from jacquard.service import get_wsgi_app

class RunServer(BaseCommand):
    help = "run a (local, debug) server"

    def add_arguments(self, parser):
        parser.add_argument(
            '-p',
            '--port',
            type=int,
            default=8888,
            help="port to bind to",
        )
        parser.add_argument(
            '-b',
            '--bind',
            type=str,
            default='::1',
            help="address to bind to",
        )

    def handle(self, config, options):
        app = get_wsgi_app(config)

        werkzeug.serving.run_simple(
            options.bind,
            options.port,
            app,
            use_reloader=True,
            use_debugger=True,
            use_evalex=True,
            threaded=False,
            processes=1,
        )

Prompt:
Make this derive from the correct type

import werkzeug.debug
import werkzeug.serving

from jacquard.commands import BaseCommand
from jacquard.service import get_wsgi_app

class RunServer(object):
    help = "run a (local, debug) server"

    def add_arguments(self, parser):
        parser.add_argument(
            '-p',
            '--port',
            type=int,
            default=8888,
            help="port to bind to",
        )
        parser.add_argument(
            '-b',
            '--bind',
            type=str,
            default='::1',
            help="address to bind to",
        )

    def handle(self, config, options):
        app = get_wsgi_app(config)

        werkzeug.serving.run_simple(
            options.bind,
            options.port,
            app,
            use_reloader=True,
            use_debugger=True,
            use_evalex=True,
            threaded=False,
            processes=1,
        )


from django.test import TestCase
from django.conf import settings

from whats_fresh.models import *
from django.contrib.gis.db import models

import os
import time
import sys
import datetime

class ImageTestCase(TestCase):
    def setUp(self):
        # Set MEDIA ROOT to sample data for this test
        TEST_ROOT = os.path.abspath(os.path.dirname(__file__))

        self._old_MEDIA_ROOT = settings.MEDIA_ROOT
        settings.MEDIA_ROOT = os.path.join(TEST_ROOT, 'testdata/media/')

        self.image = Image(image='cat.jpg', caption='Meow!')

        # Set creation time variable to test if image.creation records properly
        self.creation_time = datetime.datetime.now()
        self.image.save()

    def tearDown(self):
        # reset MEDIA_ROOT
        settings.MEDIA_ROOT = self._old_MEDIA_ROOT

    def test_image_uploaded(self):
        """
        Tests that the image was successfully uploaded
        """
        self.assertEquals(self.image.image.url, '/media/cat.jpg')
        self.assertEquals(self.image.caption, 'Meow!')

        self.assertTrue(
            self.image.created.replace(tzinfo=None) - self.creation_time <
            datetime.timedelta(milliseconds = 10))

    def test_replace_image(self):
        """
        Tests that the image was properly replaced with a new image.
        Also sets modified time variable for later testing to make
        sure the time variables were properly set and saved.
        """
        # Sleep 25 milliseconds so that the modified time won't be within
        # The defined range of 10 milliseconds
        time.sleep(0.025)

        self.image.image = 'dog.jpg'
        self.mod_time = datetime.datetime.now()
        self.image.save()

        self.assertEquals(self.image.image.url, '/media/dog.jpg')

        self.assertTrue(
            self.image.modified.replace(tzinfo=None) - self.mod_time <
            datetime.timedelta(milliseconds = 10))

class PreparationsTestCase(TestCase):
    def test_fields_exist(self):
        model = models.get_model('whats_fresh', 'Preparation')
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('name')))
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('description')))
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('additional_info')))

    def test_no_additional_fields(self):
        fields = Preparation._meta.get_all_field_names()
        expected_fields = ['name', 'description', 'additional_info', u'id']

        self.assertTrue(sorted(fields) == sorted(expected_fields))

Make preparation model tests actually work

Response:
from django.test import TestCase
from django.conf import settings

from whats_fresh.models import *
from django.contrib.gis.db import models

import os
import time
import sys
import datetime

class ImageTestCase(TestCase):
    def setUp(self):
        # Set MEDIA ROOT to sample data for this test
        TEST_ROOT = os.path.abspath(os.path.dirname(__file__))

        self._old_MEDIA_ROOT = settings.MEDIA_ROOT
        settings.MEDIA_ROOT = os.path.join(TEST_ROOT, 'testdata/media/')

        self.image = Image(image='cat.jpg', caption='Meow!')

        # Set creation time variable to test if image.creation records properly
        self.creation_time = datetime.datetime.now()
        self.image.save()

    def tearDown(self):
        # reset MEDIA_ROOT
        settings.MEDIA_ROOT = self._old_MEDIA_ROOT

    def test_image_uploaded(self):
        """
        Tests that the image was successfully uploaded
        """
        self.assertEquals(self.image.image.url, '/media/cat.jpg')
        self.assertEquals(self.image.caption, 'Meow!')

        self.assertTrue(
            self.image.created.replace(tzinfo=None) - self.creation_time <
            datetime.timedelta(milliseconds = 10))

    def test_replace_image(self):
        """
        Tests that the image was properly replaced with a new image.
        Also sets modified time variable for later testing to make
        sure the time variables were properly set and saved.
        """
        # Sleep 25 milliseconds so that the modified time won't be within
        # The defined range of 10 milliseconds
        time.sleep(0.025)

        self.image.image = 'dog.jpg'
        self.mod_time = datetime.datetime.now()
        self.image.save()

        self.assertEquals(self.image.image.url, '/media/dog.jpg')

        self.assertTrue(
            self.image.modified.replace(tzinfo=None) - self.mod_time <
            datetime.timedelta(milliseconds = 10))

class PreparationsTestCase(TestCase):
    def test_fields_exist(self):
        model = models.get_model('whats_fresh', 'Preparation')
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('name')))
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('description')))
        self.assertEqual(
            models.TextField,
            type(model._meta.get_field_by_name('additional_info')))

    def test_no_additional_fields(self):
        fields = Preparation._meta.get_all_field_names()
        expected_fields = ['name', 'description', 'additional_info', u'id']

        self.assertTrue(sorted(fields) == sorted(expected_fields))

Prompt: Make preparation model tests actually work


#!/usr/bin/env python
# Copyright Datacratic 2016
# Author: Jean Raby <jean@datacratic.com>

# This script is called by runsv when the mldb service exits.
# See http://smarden.org/runit/runsv.8.html for more details.
# Two arguments are given to ./finish:
# The first one is ./run's exit code, or -1 if ./run didn't exit normally.
# The second one is the least significant byte of the exit status as
# determined by waitpid(2); for instance it is 0 if ./run exited normally,
# and the signal number if ./run was terminated by a signal.
# If runsv cannot start ./run for some reason, the exit code is 111 and the status is 0.

import os
import sys

sigmap = { 4: "SIGILL: illegal instruction - internal error",
           6: "SIGABRT: abort(3) called - internal error",
           9: "SIGKILL: killed from outside, did we run out of memory (OOM killed?)",
           11: "SIGSEGV: segfault - internal error",
           15: "SIGTERM: regular shutdown",
         }

msg = ""
sig = None
if len(sys.argv) == 3:
    exit_code = sys.argv[1]
    status_code = sys.argv[2]
    if os.WIFSIGNALED(int(status_code)):
        sig = os.WTERMSIG(int(status_code))

print  # we like space
print
if sig == None:
    print "MLDB exited."
else:
    msg = "MLDB exited due to signal %d." % (sig)
    if sig in sigmap:
        msg += " " + sigmap[sig]
    print msg
print
print

Response:
#!/usr/bin/env python
# Copyright Datacratic 2016
# Author: Jean Raby <jean@datacratic.com>

# This script is called by runsv when the mldb service exits.
# See http://smarden.org/runit/runsv.8.html for more details.
# Two arguments are given to ./finish:
# The first one is ./run's exit code, or -1 if ./run didn't exit normally.
# The second one is the least significant byte of the exit status as
# determined by waitpid(2); for instance it is 0 if ./run exited normally,
# and the signal number if ./run was terminated by a signal.
# If runsv cannot start ./run for some reason, the exit code is 111 and the status is 0.

import os
import sys

sigmap = { 4: "SIGILL: illegal instruction - internal error",
           6: "SIGABRT: abort(3) called - internal error",
           9: "SIGKILL: killed from outside, did we run out of memory (OOM killed?)",
           11: "SIGSEGV: segfault - internal error",
           15: "SIGTERM: regular shutdown",
         }

msg = ""
sig = None
if len(sys.argv) == 3:
    exit_code = sys.argv[1]
    status_code = sys.argv[2]
    if os.WIFSIGNALED(int(status_code)):
        sig = os.WTERMSIG(int(status_code))

print  # we like space
print
if sig == None:
    print "MLDB exited."
else:
    msg = "MLDB exited due to signal %d." % (sig)
    if sig in sigmap:
        msg += " " + sigmap[sig]
    print msg
print
print

Prompt:
Add signal map and human readable description

#!/usr/bin/env python
# Copyright Datacratic 2016
# Author: Jean Raby <jean@datacratic.com>

# This script is called by runsv when the mldb service exits.
# See http://smarden.org/runit/runsv.8.html for more details.
# Two arguments are given to ./finish:
# The first one is ./run's exit code, or -1 if ./run didn't exit normally.
# The second one is the least significant byte of the exit status as
# determined by waitpid(2); for instance it is 0 if ./run exited normally,
# and the signal number if ./run was terminated by a signal.
# If runsv cannot start ./run for some reason, the exit code is 111 and the status is 0.

import os
import sys

msg = ""
if len(sys.argv) == 3:
    exit_code = sys.argv[1]
    status_code = sys.argv[2]
    if os.WIFSIGNALED(int(status_code)):
        sig = os.WTERMSIG(int(status_code))
        msg = " Killed by signal %d." % sig

print "MLDB exited.%s" % (msg)


# Copyright 2012 Kevin Goodsell
#
# This software is licensed under the Eclipse Public License (EPL) V1.0.

'''
Interface to the STAF API.
'''

# Using __all__ makes pydoc work properly. Otherwise it looks at the modules the
# items actually come from and assumes they don't belong in the docs for
# __init__.
__all__ = [
    'Handle', 'wrap_data', 'add_privacy_delimiters',
    'remove_privacy_delimiters', 'mask_private_data',
    'escape_privacy_delimiters', 'errors', 'strerror', 'STAFError',
    'STAFResultError', 'unmarshal', 'unmarshal_force', 'STAFUnmarshalError',
    'MapClassDefinition', 'MapClass', 'unmarshal_recursive',
    'unmarshal_non_recursive', 'unmarshal_none',
]

from ._staf import (
    Handle,
    wrap_data,
    add_privacy_delimiters,
    remove_privacy_delimiters,
    mask_private_data,
    escape_privacy_delimiters,
)

from ._errors import (
    errors,
    strerror,
    STAFError,
    STAFResultError,
)

from ._marshal import (
    unmarshal,
    unmarshal_force,
    STAFUnmarshalError,
    MapClassDefinition,
    MapClass,
    unmarshal_recursive,
    unmarshal_non_recursive,
    unmarshal_none,
)

Add missing function in __all__.

Add missing function in __all__.

Response:
# Copyright 2012 Kevin Goodsell
#
# This software is licensed under the Eclipse Public License (EPL) V1.0.

'''
Interface to the STAF API.
'''

# Using __all__ makes pydoc work properly. Otherwise it looks at the modules the
# items actually come from and assumes they don't belong in the docs for
# __init__.
__all__ = [
    'Handle', 'wrap_data', 'add_privacy_delimiters',
    'remove_privacy_delimiters', 'mask_private_data',
    'escape_privacy_delimiters', 'errors', 'strerror', 'STAFError',
    'STAFResultError', 'unmarshal', 'unmarshal_force', 'STAFUnmarshalError',
    'MapClassDefinition', 'MapClass', 'unmarshal_recursive',
    'unmarshal_non_recursive', 'unmarshal_none',
]

from ._staf import (
    Handle,
    wrap_data,
    add_privacy_delimiters,
    remove_privacy_delimiters,
    mask_private_data,
    escape_privacy_delimiters,
)

from ._errors import (
    errors,
    strerror,
    STAFError,
    STAFResultError,
)

from ._marshal import (
    unmarshal,
    unmarshal_force,
    STAFUnmarshalError,
    MapClassDefinition,
    MapClass,
    unmarshal_recursive,
    unmarshal_non_recursive,
    unmarshal_none,
)

Prompt:
Add missing function in __all__.

# Copyright 2012 Kevin Goodsell
#
# This software is licensed under the Eclipse Public License (EPL) V1.0.

'''
Interface to the STAF API.
'''

# Using __all__ makes pydoc work properly. Otherwise it looks at the modules the
# items actually come from and assumes they don't belong in the docs for
# __init__.
__all__ = [
    'Handle', 'wrap_data', 'add_privacy_delimiters',
    'remove_privacy_delimiters', 'mask_private_data',
    'escape_privacy_delimiters', 'errors', 'strerror', 'STAFError',
    'STAFResultError', 'unmarshal', 'STAFUnmarshalError', 'MapClassDefinition',
    'MapClass', 'unmarshal_recursive', 'unmarshal_non_recursive',
    'unmarshal_none',
]

from ._staf import (
    Handle,
    wrap_data,
    add_privacy_delimiters,
    remove_privacy_delimiters,
    mask_private_data,
    escape_privacy_delimiters,
)

from ._errors import (
    errors,
    strerror,
    STAFError,
    STAFResultError,
)

from ._marshal import (
    unmarshal,
    unmarshal_force,
    STAFUnmarshalError,
    MapClassDefinition,
    MapClass,
    unmarshal_recursive,
    unmarshal_non_recursive,
    unmarshal_none,
)


#!/usr/bin/env python

# Copyright (c) 2017 Orange and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

# pylint: disable=missing-docstring

import logging
import unittest

from functest.opnfv_tests.features import domino
from functest.utils import constants

class DominoTesting(unittest.TestCase):

    logging.disable(logging.CRITICAL)

    def setUp(self):
        self.domino = domino.Domino()

    def test_init(self):
        self.assertEqual(self.domino.project_name, "domino")
        self.assertEqual(self.domino.case_name, "domino-multinode")
        self.assertEqual(
            self.domino.repo,
            constants.CONST.__getattribute__("dir_repo_domino"))
        self.assertEqual(
            self.domino.cmd,
            'cd {} && ./tests/run_multinode.sh'.format(self.domino.repo))

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

Add unit tests for domino

Add unit tests for domino

Change-Id: Ie6671080a3d38a17da0ee608a362605a6d9df9db
Signed-off-by: Cédric Ollivier <d48310251a4a484d041bc5d09a9ac4d86d20f793@orange.com>

Response:
#!/usr/bin/env python

# Copyright (c) 2017 Orange and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

# pylint: disable=missing-docstring

import logging
import unittest

from functest.opnfv_tests.features import domino
from functest.utils import constants

class DominoTesting(unittest.TestCase):

    logging.disable(logging.CRITICAL)

    def setUp(self):
        self.domino = domino.Domino()

    def test_init(self):
        self.assertEqual(self.domino.project_name, "domino")
        self.assertEqual(self.domino.case_name, "domino-multinode")
        self.assertEqual(
            self.domino.repo,
            constants.CONST.__getattribute__("dir_repo_domino"))
        self.assertEqual(
            self.domino.cmd,
            'cd {} && ./tests/run_multinode.sh'.format(self.domino.repo))

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

Prompt:
Add unit tests for domino

Change-Id: Ie6671080a3d38a17da0ee608a362605a6d9df9db
Signed-off-by: Cédric Ollivier <d48310251a4a484d041bc5d09a9ac4d86d20f793@orange.com>


from setuptools import setup, find_packages
import platform

distro, version, _ = platform.dist()

# Default to cent7
data_files = [('/usr/lib/systemd/system', ['pkg/hubble.service']),]

if distro == 'redhat' or distro == 'centos':
    if version.startswith('6'):
        data_files = [('/etc/init.d', ['pkg/hubble']),]
    elif version.startswith('7'):
        data_files = [('/usr/lib/systemd/system', ['pkg/hubble.service']),]

setup(
    name="hubblestack",
    version="2.0",
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'hubble = hubble.daemon:run',
        ],
    },
    install_requires=[
        'salt >= 2016.3.4',
    ],
    data_files=data_files,
    options={
#        'build_scripts': {
#            'executable': '/usr/bin/env python',
#        },
        'bdist_rpm': {
            'requires': 'salt',
        },
    },

)

Add platform detection for centos 6/7

Add platform detection for centos 6/7

Response:
from setuptools import setup, find_packages
import platform

distro, version, _ = platform.dist()

# Default to cent7
data_files = [('/usr/lib/systemd/system', ['pkg/hubble.service']),]

if distro == 'redhat' or distro == 'centos':
    if version.startswith('6'):
        data_files = [('/etc/init.d', ['pkg/hubble']),]
    elif version.startswith('7'):
        data_files = [('/usr/lib/systemd/system', ['pkg/hubble.service']),]

setup(
    name="hubblestack",
    version="2.0",
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'hubble = hubble.daemon:run',
        ],
    },
    install_requires=[
        'salt >= 2016.3.4',
    ],
    data_files=data_files,
    options={
#        'build_scripts': {
#            'executable': '/usr/bin/env python',
#        },
        'bdist_rpm': {
            'requires': 'salt',
        },
    },

)

Prompt:
Add platform detection for centos 6/7

from setuptools import setup, find_packages
setup(
    name="hubblestack",
    version="2.0",
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'hubble = hubble.daemon:run',
        ],
    },
    install_requires=[
        'salt >= 2016.3.4',
    ],
    data_files=[('/etc/init.d', ['pkg/hubble']),],
    options={
#        'build_scripts': {
#            'executable': '/usr/bin/env python',
#        },
        'bdist_rpm': {
            'requires': 'salt',
        },
    },

)


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

"""
byceps.blueprints.news.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from flask import abort, current_app, g

from ...services.news import service as news_service
from ...util.framework import create_blueprint
from ...util.templating import templated

blueprint = create_blueprint('news', __name__)

@blueprint.route('/', defaults={'page': 1})
@blueprint.route('/pages/<int:page>')
@templated
def index(page):
    """Show a page of news items."""
    items_per_page = _get_items_per_page_value()

    items = news_service.get_items_paginated(g.party.brand.id, page,
                                             items_per_page)

    return {
        'items': items,
        'page': page,
    }

@blueprint.route('/<slug>')
@templated
def view(slug):
    """Show a single news item."""
    item = news_service.find_item_by_slug(g.party.brand.id, slug)

    if item is None:
        abort(404)

    return {
        'item': item,
    }

def _get_items_per_page_value(default=4):
    return int(current_app.config.get('NEWS_ITEMS_PER_PAGE', default))

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

"""
byceps.blueprints.news.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from flask import abort, current_app, g

from ...services.news import service as news_service
from ...util.framework import create_blueprint
from ...util.templating import templated

blueprint = create_blueprint('news', __name__)

@blueprint.route('/', defaults={'page': 1})
@blueprint.route('/pages/<int:page>')
@templated
def index(page):
    """Show a page of news items."""
    items_per_page = _get_items_per_page_value()

    items = news_service.get_items_paginated(g.party.brand.id, page,
                                             items_per_page)

    return {
        'items': items,
        'page': page,
    }

@blueprint.route('/<slug>')
@templated
def view(slug):
    """Show a single news item."""
    item = news_service.find_item_by_slug(g.party.brand.id, slug)

    if item is None:
        abort(404)

    return {
        'item': item,
    }

def _get_items_per_page_value(default=4):
    return int(current_app.config.get('NEWS_ITEMS_PER_PAGE', default))

Prompt:
Allow configuration of the number of news items per page

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

"""
byceps.blueprints.news.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from flask import abort, g

from ...services.news import service as news_service
from ...util.framework import create_blueprint
from ...util.templating import templated

blueprint = create_blueprint('news', __name__)

ITEMS_PER_PAGE = 4

@blueprint.route('/', defaults={'page': 1})
@blueprint.route('/pages/<int:page>')
@templated
def index(page):
    """Show a page of news items."""
    items = news_service.get_items_paginated(g.party.brand.id, page,
                                             ITEMS_PER_PAGE)

    return {
        'items': items,
        'page': page,
    }

@blueprint.route('/<slug>')
@templated
def view(slug):
    """Show a single news item."""
    item = news_service.find_item_by_slug(g.party.brand.id, slug)

    if item is None:
        abort(404)

    return {
        'item': item,
    }


import pya
import re

WIDTH = 2048
HEIGHT = 2048

app = pya.Application.instance()
win = app.main_window()

# Load technology file
tech = pya.Technology()
tech.load(tech_file)
layoutOptions = tech.load_layout_options

# Load def file in the main window
cell_view = win.load_layout(input_layout, layoutOptions, 0)
layout_view = cell_view.view()

layout_view.save_image(input_layout+".png", WIDTH, HEIGHT)

app.exit(0)

Add a klayout script to screenshot layouts

Response:
import pya
import re

WIDTH = 2048
HEIGHT = 2048

app = pya.Application.instance()
win = app.main_window()

# Load technology file
tech = pya.Technology()
tech.load(tech_file)
layoutOptions = tech.load_layout_options

# Load def file in the main window
cell_view = win.load_layout(input_layout, layoutOptions, 0)
layout_view = cell_view.view()

layout_view.save_image(input_layout+".png", WIDTH, HEIGHT)

app.exit(0)

Prompt: Add a klayout script to screenshot layouts


#!/usr/bin/env python
import os
import sys
import unittest
from flask.ext.script import Manager, Server
from app import app

manager = Manager(app)

server = Server(host='0.0.0.0', use_debugger=True)
manager.add_command("runserver", server)

@manager.command
def test():
    """Runs all the tests"""
    parent_dir = os.path.dirname(os.path.abspath(__file__))
    test_dir = os.path.join(parent_dir, 'tests')
    tests = unittest.TestLoader().discover(test_dir)
    results = unittest.TextTestRunner(verbosity=2).run(tests)
    ret = not results.wasSuccessful()
    sys.exit(ret)

if __name__ == '__main__':
    manager.run()

Response:
#!/usr/bin/env python
import os
import sys
import unittest
from flask.ext.script import Manager, Server
from app import app

manager = Manager(app)

server = Server(host='0.0.0.0', use_debugger=True)
manager.add_command("runserver", server)

@manager.command
def test():
    """Runs all the tests"""
    parent_dir = os.path.dirname(os.path.abspath(__file__))
    test_dir = os.path.join(parent_dir, 'tests')
    tests = unittest.TestLoader().discover(test_dir)
    results = unittest.TextTestRunner(verbosity=2).run(tests)
    ret = not results.wasSuccessful()
    sys.exit(ret)

if __name__ == '__main__':
    manager.run()

Prompt:
Set the exit code when running tests correctly

#!/usr/bin/env python
import os
import unittest
from flask.ext.script import Manager, Server
from app import app

manager = Manager(app)

server = Server(host='0.0.0.0', use_debugger=True)
manager.add_command("runserver", server)

@manager.command
def test():
    """Runs all the tests"""
    parent_dir = os.path.dirname(os.path.abspath(__file__))
    test_dir = os.path.join(parent_dir, 'tests')
    tests = unittest.TestLoader().discover(test_dir)
    results = unittest.TextTestRunner(verbosity=2).run(tests)

if __name__ == '__main__':
    manager.run()


import time

from collections import deque
from pybloom import ScalableBloomFilter

class DecayScalableBloomFilter(ScalableBloomFilter):
    '''
    Stepwise decaying Bloom Filter
    '''
    def __init__(self, initial_capacity=1000, error_rate=0.01, window_period = 60):
        super(DecayScalableBloomFilter, self).__init__(initial_capacity, error_rate)
        self.window_period = 60
        self.timestamp = time.time()
        self._expired = False

    @property
    def expired(self):
        if time.time() - self.timestamp > self.window_period:
            self._expired = True
        return self._expired

class SlidingWindowScalableBloomFilter(object):
    '''
    Sliding Window Bloom Filter using a coarse expiration
    '''

    VALID_RES = {'Min': 60,
                 'Hour': 3600,
                 'Day': 86400}

    def __init__(self, initial_capacity=1000, window_period = "10_Min"):
        self.initial_capacity = initial_capacity
        self.error_rate = 0.01
        self._setup_window_period(window_period)

    def _setup_window_period(self, window_period):
        try:
            self.amount, self.res = window_period.split('_')
            self.amount = int(self.amount)
        except ValueError:
            raise Exception('Invalid window period')
        self.window_period = SlidingWindowScalableBloomFilter.VALID_RES[self.res]
        self.filters = deque(maxlen = self.amount)

    def total_error(self):
        '''
        Return the total error: temporal error + native bf error
        '''
        temporal_error = float(1.0 / self.amount)
        total_error = self.error_rate + temporal_error
        return total_error

    def __contains__(self, key):
        for f in reversed(self.filters):
            if key in f:
                return True
        return False

    def check_expiration(self):
        filter = self.filters[0]
        if filter.expired:
            filter = DecayScalableBloomFilter(initial_capacity=self.initial_capacity,
                                              error_rate=self.error_rate,
                                              window_period=self.window_period)
            self.filters.append(filter)

    def add(self, key):
        self.check_expiration()
        if key in self:
            return True
        if not self.filters:
            filter = DecayScalableBloomFilter(initial_capacity=self.initial_capacity,
                                              error_rate=self.error_rate,
                                              window_period=self.window_period)
            self.filters.append(filter)
        else:
            filter = self.filters[-1]
        filter.add(key)
        return False

Add simple SlidingWindowScalableBloomFilter for combating overengineer habit

Response:
import time

from collections import deque
from pybloom import ScalableBloomFilter

class DecayScalableBloomFilter(ScalableBloomFilter):
    '''
    Stepwise decaying Bloom Filter
    '''
    def __init__(self, initial_capacity=1000, error_rate=0.01, window_period = 60):
        super(DecayScalableBloomFilter, self).__init__(initial_capacity, error_rate)
        self.window_period = 60
        self.timestamp = time.time()
        self._expired = False

    @property
    def expired(self):
        if time.time() - self.timestamp > self.window_period:
            self._expired = True
        return self._expired

class SlidingWindowScalableBloomFilter(object):
    '''
    Sliding Window Bloom Filter using a coarse expiration
    '''

    VALID_RES = {'Min': 60,
                 'Hour': 3600,
                 'Day': 86400}

    def __init__(self, initial_capacity=1000, window_period = "10_Min"):
        self.initial_capacity = initial_capacity
        self.error_rate = 0.01
        self._setup_window_period(window_period)

    def _setup_window_period(self, window_period):
        try:
            self.amount, self.res = window_period.split('_')
            self.amount = int(self.amount)
        except ValueError:
            raise Exception('Invalid window period')
        self.window_period = SlidingWindowScalableBloomFilter.VALID_RES[self.res]
        self.filters = deque(maxlen = self.amount)

    def total_error(self):
        '''
        Return the total error: temporal error + native bf error
        '''
        temporal_error = float(1.0 / self.amount)
        total_error = self.error_rate + temporal_error
        return total_error

    def __contains__(self, key):
        for f in reversed(self.filters):
            if key in f:
                return True
        return False

    def check_expiration(self):
        filter = self.filters[0]
        if filter.expired:
            filter = DecayScalableBloomFilter(initial_capacity=self.initial_capacity,
                                              error_rate=self.error_rate,
                                              window_period=self.window_period)
            self.filters.append(filter)

    def add(self, key):
        self.check_expiration()
        if key in self:
            return True
        if not self.filters:
            filter = DecayScalableBloomFilter(initial_capacity=self.initial_capacity,
                                              error_rate=self.error_rate,
                                              window_period=self.window_period)
            self.filters.append(filter)
        else:
            filter = self.filters[-1]
        filter.add(key)
        return False

Prompt: Add simple SlidingWindowScalableBloomFilter for combating overengineer habit


from random import randint

def get_random_number(start=1, end=10):
    """Generates and returns random number between :start: and :end:"""
    return randint(start, end)

Fix python random number generator.

Fix python random number generator.

Response:
from random import randint

def get_random_number(start=1, end=10):
    """Generates and returns random number between :start: and :end:"""
    return randint(start, end)

Prompt:
Fix python random number generator.

def get_random_number(start=1, end=10):
    """https://xkcd.com/221/"""
    return 4


from pathlib import Path

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def filename(path):
    """Removes parent path from a relative or absolute filename

    Returns: the filename
    """
    return Path(path).name

Fix filename template tag docstring

Fix filename template tag docstring

Response:
from pathlib import Path

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def filename(path):
    """Removes parent path from a relative or absolute filename

    Returns: the filename
    """
    return Path(path).name

Prompt:
Fix filename template tag docstring

from pathlib import Path

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def filename(path):
    """Removes traceback lines from a string (if any). It has no effect when
    no 'Traceback' pattern has been found.

    Returns: raws before the 'Traceback' pattern
    """
    return Path(path).name


# -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-11-07 08:44
from __future__ import unicode_literals

from django.db import migrations

def populate_entity_type(apps, schema_editor):
    """Populate entity type from attached descriptor schema."""
    Entity = apps.get_model('flow', 'Entity')

    for entity in Entity.objects.all():
        if entity.descriptor_schema is not None:
            entity.type = entity.descriptor_schema.slug
            entity.save()

class Migration(migrations.Migration):

    dependencies = [
        ('flow', '0024_process_entity_3'),
    ]

    operations = [
        migrations.RunPython(populate_entity_type)
    ]

Add migration to populate Entity type

Add migration to populate Entity type

Response:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-11-07 08:44
from __future__ import unicode_literals

from django.db import migrations

def populate_entity_type(apps, schema_editor):
    """Populate entity type from attached descriptor schema."""
    Entity = apps.get_model('flow', 'Entity')

    for entity in Entity.objects.all():
        if entity.descriptor_schema is not None:
            entity.type = entity.descriptor_schema.slug
            entity.save()

class Migration(migrations.Migration):

    dependencies = [
        ('flow', '0024_process_entity_3'),
    ]

    operations = [
        migrations.RunPython(populate_entity_type)
    ]

Prompt: Add migration to populate Entity type


# -------------------------------------------------------
import socket, traceback
import time
import json

import numpy as np
from scipy.spatial.transform import Rotation as R

host = ''
port = 2390

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))

filein = open('saveUDP.txt', 'w')
t0 = time.time()

# Place IMU x-axis into wind going direction when launching script
is_init_done = False
wind_yaw = 0
while time.time()-t0 < 200:
    try:
        message, address = s.recvfrom(4096)
        #print(message)
        msg = json.loads(message.decode("utf-8"))
        if is_init_done==False:
          wind_yaw = msg["Yaw"]
          is_init_done = True
        msg['Yaw'] = msg['Yaw']-wind_yaw
        print(msg)

        ypr = [msg['Yaw'], msg['Pitch'], msg['Roll']]
        seq = 'ZYX' # small letters from intrinsic rotations

        r = R.from_euler(seq, ypr, degrees=True)

        # Compute coordinates in NED (could be useful to compare position with GPS position for example)
        line_length = 10
        base_to_kite = [0, 0, line_length]
        base_to_kite_in_NED = r.apply(base_to_kite)

        # Express kite coordinates as great roll, great pitch and small yaw angles
        grpy=r.as_euler(seq="XYZ")
        print(grpy*180/np.pi)

        filein.write('%s\n' % (message))
    except (KeyboardInterrupt, SystemExit):
       raise
    except:
       traceback.print_exc()
filein.close()
# -------------------------------------------------------

Response:
# -------------------------------------------------------
import socket, traceback
import time
import json

import numpy as np
from scipy.spatial.transform import Rotation as R

host = ''
port = 2390

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))

filein = open('saveUDP.txt', 'w')
t0 = time.time()

# Place IMU x-axis into wind going direction when launching script
is_init_done = False
wind_yaw = 0
while time.time()-t0 < 200:
    try:
        message, address = s.recvfrom(4096)
        #print(message)
        msg = json.loads(message.decode("utf-8"))
        if is_init_done==False:
          wind_yaw = msg["Yaw"]
          is_init_done = True
        msg['Yaw'] = msg['Yaw']-wind_yaw
        print(msg)

        ypr = [msg['Yaw'], msg['Pitch'], msg['Roll']]
        seq = 'ZYX' # small letters from intrinsic rotations

        r = R.from_euler(seq, ypr, degrees=True)

        # Compute coordinates in NED (could be useful to compare position with GPS position for example)
        line_length = 10
        base_to_kite = [0, 0, line_length]
        base_to_kite_in_NED = r.apply(base_to_kite)

        # Express kite coordinates as great roll, great pitch and small yaw angles
        grpy=r.as_euler(seq="XYZ")
        print(grpy*180/np.pi)

        filein.write('%s\n' % (message))
    except (KeyboardInterrupt, SystemExit):
       raise
    except:
       traceback.print_exc()
filein.close()
# -------------------------------------------------------

Prompt:
Add computations of great roll, pitch and small yaw angle (kite angles)

# -------------------------------------------------------
import socket, traceback
import time
import json

host = ''
port = 2390

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))

filein = open('saveUDP.txt', 'w')
t0 = time.time()
while time.time()-t0 < 200:
    try:
        message, address = s.recvfrom(4096)
        print(message)
        json.loads(message.decode("utf-8"))
        filein.write('%s\n' % (message))
    except (KeyboardInterrupt, SystemExit):
       raise
    except:
       traceback.print_exc()
filein.close()
# -------------------------------------------------------


#!/usr/bin/env python

from os.path import join

def configuration(parent_package = '', top_path = None):
    from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs
    config = Configuration('spatial', parent_package, top_path)

    config.add_data_dir('tests')

    #config.add_extension('_vq',
    #    sources=[join('src', 'vq_module.c'), join('src', 'vq.c')],
    #    include_dirs = [get_numpy_include_dirs()])
    config.add_sconscript('SConstruct')

    return config

if __name__ == '__main__':
    from numpy.distutils.core import setup
    setup(maintainer = "SciPy Developers",
          author = "Eric Jones",
          maintainer_email = "scipy-dev@scipy.org",
          description = "Clustering Algorithms (Information Theory)",
          url = "http://www.scipy.org",
          license = "SciPy License (BSD Style)",
          **configuration(top_path='').todict()
          )

Response:
#!/usr/bin/env python

from os.path import join

def configuration(parent_package = '', top_path = None):
    from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs
    config = Configuration('spatial', parent_package, top_path)

    config.add_data_dir('tests')

    #config.add_extension('_vq',
    #    sources=[join('src', 'vq_module.c'), join('src', 'vq.c')],
    #    include_dirs = [get_numpy_include_dirs()])
    config.add_sconscript('SConstruct')

    return config

if __name__ == '__main__':
    from numpy.distutils.core import setup
    setup(maintainer = "SciPy Developers",
          author = "Eric Jones",
          maintainer_email = "scipy-dev@scipy.org",
          description = "Clustering Algorithms (Information Theory)",
          url = "http://www.scipy.org",
          license = "SciPy License (BSD Style)",
          **configuration(top_path='').todict()
          )

Prompt:
Update setup.py file for numscons build.

git-svn-id: 003f22d385e25de9cff933a5ea4efd77cb5e7b28@4958 d6536bca-fef9-0310-8506-e4c0a848fbcf

#!/usr/bin/env python

from os.path import join

def configuration(parent_package = '', top_path = None):
    from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs
    config = Configuration('cluster', parent_package, top_path)

    config.add_data_dir('tests')

    #config.add_extension('_vq',
    #    sources=[join('src', 'vq_module.c'), join('src', 'vq.c')],
    #    include_dirs = [get_numpy_include_dirs()])
    config.add_sconscript('SConstruct')

    return config

if __name__ == '__main__':
    from numpy.distutils.core import setup
    setup(maintainer = "SciPy Developers",
          author = "Eric Jones",
          maintainer_email = "scipy-dev@scipy.org",
          description = "Clustering Algorithms (Information Theory)",
          url = "http://www.scipy.org",
          license = "SciPy License (BSD Style)",
          **configuration(top_path='').todict()
          )