"""List drivers and send them commands."""
import logging

import flask

from appengine import device, rest

class Query(object):
  def iter(self):
    for name, cls in device.DEVICE_TYPES.iteritems():
      yield Driver(name, cls)

class Driver(object):
  """This is a fake for compatibility with the rest module"""

  def __init__(self, name, cls):
    self._name = name
    self._cls = cls

  def to_dict(self):
    return {'name': self._name}

  # This is a trampoline through to the driver
  # mainly for commands
  def __getattr__(self, name):
    func = getattr(self._cls, name)
    if func is None or not getattr(func, 'is_static', False):
      logging.error('Command %s does not exist or is not a static command',
                    name)
      flask.abort(400)
    return func

  @staticmethod
  def query():
    return Query()

  @staticmethod
  def get_by_id(_id):
    return Driver(_id, device.DEVICE_TYPES[_id])

# pylint: disable=invalid-name
blueprint = flask.Blueprint('driver', __name__)
rest.register_class(blueprint, Driver, None)

"""List drivers and send them commands."""
import logging

import flask

from appengine import device, rest

class Query(object):
  def iter(self):
    for name, cls in device.DEVICE_TYPES.iteritems():
      yield Driver(name, cls)

class Driver(object):
  """This is a fake for compatibility with the rest module"""

  def __init__(self, name, cls):
    self._name = name
    self._cls = cls

  def to_dict(self):
    return {'name': self._name}

  # This is a trampoline through to the driver
  # mainly for commands
  def __getattr__(self, name):
    func = getattr(self._cls, name)
    if func is None or not getattr(func, 'is_static', False):
      logging.error('Command %s does not exist or is not a static command',
                    name)
      flask.abort(400)
    return func

  @staticmethod
  def put():
    pass

  @staticmethod
  def query():
    return Query()

  @staticmethod
  def get_by_id(_id):
    return Driver(_id, device.DEVICE_TYPES[_id])

# pylint: disable=invalid-name
blueprint = flask.Blueprint('driver', __name__)
rest.register_class(blueprint, Driver, None)

Fix 'put is not a command' error on static commands


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

import rospy
from humanoid_league_msgs.msg import BallInImage, BallRelative, BallsInImage
from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint

def run():
    pub_ball = rospy.Publisher("ball_in_image", BallsInImage, queue_size=1)
    pub_hmg = rospy.Publisher("head_motor_goals", JointTrajectory, queue_size=1)

    hmg = JointTrajectory()
    goal = JointTrajectoryPoint()
    goal.positions = [0, 0]
    goal.velocities = [0, 0]
    hmg.points = [goal]

    counter = 320
    direction = 1

    rospy.loginfo("Create Test")
    rospy.init_node("bitbots_testHeadBehaviour")
    pub_hmg.publish(hmg)

    rate = rospy.Rate(4)
    rospy.logdebug("Laeuft...")
    while not rospy.is_shutdown():
        # Ball in Image
        ball = BallInImage()
        ball.center.x = counter
        if(counter > 340 or counter < 300):
            direction *= -1
            counter += direction
        else:
            counter += direction
        ball.center.y = 200
        ball.diameter = 10
        ball.confidence = 1
        balls = BallsInImage()
        balls.candidates.append(ball)

        pub_ball.publish(balls)
        rospy.loginfo("Published ball: %s" % counter)
        rate.sleep()

if __name__ == "__main__":
    run()

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

import rospy
from humanoid_league_msgs.msg import BallInImage, BallRelative, BallInImageArray
from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint

def run():
    pub_ball = rospy.Publisher("ball_in_image", BallsInImage, queue_size=1)
    pub_hmg = rospy.Publisher("head_motor_goals", JointTrajectory, queue_size=1)

    hmg = JointTrajectory()
    goal = JointTrajectoryPoint()
    goal.positions = [0, 0]
    goal.velocities = [0, 0]
    hmg.points = [goal]

    counter = 320
    direction = 1

    rospy.loginfo("Create Test")
    rospy.init_node("bitbots_testHeadBehaviour")
    pub_hmg.publish(hmg)

    rate = rospy.Rate(4)
    rospy.logdebug("Laeuft...")
    while not rospy.is_shutdown():
        # Ball in Image
        ball = BallInImage()
        ball.center.x = counter
        if(counter > 340 or counter < 300):
            direction *= -1
            counter += direction
        else:
            counter += direction
        ball.center.y = 200
        ball.diameter = 10
        ball.confidence = 1
        balls = BallInImageArray()
        balls.candidates.append(ball)

        pub_ball.publish(balls)
        rospy.loginfo("Published ball: %s" % counter)
        rate.sleep()

if __name__ == "__main__":
    run()

Test Head Behavior: Apply new HLM

Test Head Behavior: Apply new HLM


#!/usr/bin/env python
import json

print json.dumps({
    "_meta": {
        "hostvars": {
            'afoo': {}
        },
    },
    "foo": {
        "hosts": ['afoo']
    }
})

#!/usr/bin/env python
import json

print(json.dumps({
    "_meta": {
        "hostvars": {
            'afoo': {}
        },
    },
    "foo": {
        "hosts": ['afoo']
    }
}))

Fix print statement to be py3 compatible


from twisted.internet import reactor, defer
from twisted.internet.protocol import ClientCreator
from twisted.protocols import amp
from ampserver import Sum, Divide

def doMath():
    d1 = ClientCreator(reactor, amp.AMP).connectTCP(
        '127.0.0.1', 1234).addCallback(
            lambda p: p.callRemote(Sum, a=13, b=81)).addCallback(
                lambda result: result['total'])
    def trapZero(result):
        result.trap(ZeroDivisionError)
        print "Divided by zero: returning INF"
        return 1e1000
    d2 = ClientCreator(reactor, amp.AMP).connectTCP(
        '127.0.0.1', 1234).addCallback(
            lambda p: p.callRemote(Divide, numerator=1234,
                                   denominator=0)).addErrback(trapZero)
    def done(result):
        print 'Done with math:', result
    defer.DeferredList([d1, d2]).addCallback(done)

if __name__ == '__main__':
    doMath()
    reactor.run()

from twisted.internet import reactor, defer
from twisted.internet.protocol import ClientCreator
from twisted.protocols import amp
from diceserver import Sum, Divide

def doMath():
    d1 = ClientCreator(reactor, amp.AMP).connectTCP(
        '127.0.0.1', 1234).addCallback(
            lambda p: p.callRemote(Sum, a=13, b=81)).addCallback(
                lambda result: result['total'])
    def trapZero(result):
        result.trap(ZeroDivisionError)
        print "Divided by zero: returning INF"
        return 1e1000
    d2 = ClientCreator(reactor, amp.AMP).connectTCP(
        '127.0.0.1', 1234).addCallback(
            lambda p: p.callRemote(Divide, numerator=1234,
                                   denominator=0)).addErrback(trapZero)
    def done(result):
        print 'Done with math:', result
    defer.DeferredList([d1, d2]).addCallback(done)

if __name__ == '__main__':
    doMath()
    reactor.run()

Fix import path to match rename

Fix import path to match rename


from django.forms import ModelForm, widgets

from .models import Fellow, Event, Expense, Blog

class FellowForm(ModelForm):
    class Meta:
        model = Fellow
        exclude = [
                "home_lon",
                "home_lat",
                "inauguration_year",
                "funding_notes",
                "mentor",
                ]

class EventForm(ModelForm):
    class Meta:
        model = Event
        exclude = [
                "status",
                "budget_approve",
                "report_url",
                ]

        # We don't want to expose fellows' data
        # so we will request the email
        # and match on the database.
        labels = {
                'fellow': 'Fellow',
                'url': "Event's homepage url",
                'name': "Event's name",
                }

class ExpenseForm(ModelForm):
    class Meta:
        model = Expense
        exclude = [
                'id',
                'status',
                ]

class BlogForm(ModelForm):
    class Meta:
        model = Blog
        exclude = [
                'status',
                ]

from django.forms import ModelForm, widgets

from .models import Fellow, Event, Expense, Blog

class FellowForm(ModelForm):
    class Meta:
        model = Fellow
        exclude = [
                "user",
                "home_lon",
                "home_lat",
                "inauguration_year",
                "funding_notes",
                "mentor",
                ]

class EventForm(ModelForm):
    class Meta:
        model = Event
        exclude = [
                "status",
                "budget_approve",
                "report_url",
                ]

        # We don't want to expose fellows' data
        # so we will request the email
        # and match on the database.
        labels = {
                'fellow': 'Fellow',
                'url': "Event's homepage url",
                'name': "Event's name",
                }

class ExpenseForm(ModelForm):
    class Meta:
        model = Expense
        exclude = [
                'id',
                'status',
                ]

class BlogForm(ModelForm):
    class Meta:
        model = Blog
        exclude = [
                'status',
                ]

Exclude user field from form

Exclude user field from form


'''
Arguments of the command line program.
'''

from . import __version__
from .cli import Imextract
from .args import ImextractInitArgs

parser, subparsers = Imextract.get_parser_and_subparsers(
    required_subparsers=['init', 'run', 'submit', 'cleanup'])

parser.description = '''
    Extract images from heterogeneous microscopic image file formats
    and store each 2D plane in a separate PNG file.
'''
parser.version = __version__

init_parser = subparsers.choices['init']
init_extra_group = init_parser.add_argument_group(
    'additional program-specific arguments')
ImextractInitArgs().add_to_argparser(init_extra_group)

for name in subparsers.choices:
    subparsers.choices[name].set_defaults(handler=Imextract.call)

'''
Arguments of the command line program.
'''

from . import __version__
from .cli import Imextract
from .args import ImextractInitArgs

parser, subparsers = Imextract.get_parser_and_subparsers(
    required_subparsers=['init', 'run', 'submit', 'cleanup', 'log'])

parser.description = '''
    Extract images from heterogeneous microscopic image file formats
    and store each 2D plane in a separate PNG file.
'''
parser.version = __version__

init_parser = subparsers.choices['init']
init_extra_group = init_parser.add_argument_group(
    'additional program-specific arguments')
ImextractInitArgs().add_to_argparser(init_extra_group)

for name in subparsers.choices:
    subparsers.choices[name].set_defaults(handler=Imextract.call)

Fix bug in imextract argument parser module


"""
Intel definitions.

Commonly used definitions.
"""
from __future__ import absolute_import
from cdsl.isa import TargetISA, CPUMode
import base.instructions
from . import instructions as x86

ISA = TargetISA('intel', [base.instructions.GROUP, x86.GROUP])

# CPU modes for 32-bit and 64-bit operation.
I32 = CPUMode('I32', ISA)
I64 = CPUMode('I64', ISA)

"""
Intel definitions.

Commonly used definitions.
"""
from __future__ import absolute_import
from cdsl.isa import TargetISA, CPUMode
import base.instructions
from . import instructions as x86

ISA = TargetISA('intel', [base.instructions.GROUP, x86.GROUP])

# CPU modes for 32-bit and 64-bit operation.
I64 = CPUMode('I64', ISA)
I32 = CPUMode('I32', ISA)

Define I64 before I32 for better encoding table compression.

The encoding list compression algorithm is not the sharpest knife in the
drawer. It can reuse subsets of I64 encoding lists for I32 instructions,
but only when the I64 lists are defined first.

With this change and the previous change to the encoding list format, we
get the following table sizes for the Intel ISA:

ENCLISTS: 1478 B ->  662 B
LEVEL2:             1072 B (unchanged)
LEVEL1:     32 B ->   48 B
Total:    2582 B -> 1782 B (-31%)


from spec import Spec, skip

class Tessera(Spec):
    def is_importable(self):
        import tessera
        assert tessera.app
        assert tessera.db

from contextlib import contextmanager
import os
from shutil import rmtree
from tempfile import mkdtemp

from spec import Spec, skip

@contextmanager
def _tmp():
    try:
        tempdir = mkdtemp()
        yield tempdir
    finally:
        rmtree(tempdir)

@contextmanager
def _db():
    with _tmp() as tempdir:
        from tessera import app, db
        # Temp db location
        path = os.path.join(tempdir, 'tessera.db')
        dbfile = 'sqlite:///{0}'.format(path)
        # Inform app of that location & setup
        app.config.from_object(_config(SQLALCHEMY_DATABASE_URI=dbfile))
        db.create_all()
        # Let test have its way with that temp db
        yield db

class Config(object):
    pass

def _config(**options):
    config = Config()
    for key, value in options.iteritems():
        setattr(config, key, value)

class Tessera(Spec):
    def is_importable(self):
        import tessera
        assert tessera.app
        assert tessera.db

    def creates_a_nonempty_database_schema(self):
        with _db() as db:
            meta = db.MetaData()
            meta.reflect(db.engine)
            assert len(meta.tables) > 0

Add temp DB test harness + basic test

Add temp DB test harness + basic test


import discord
from discord.ext import commands
from Cogs import GetImage

def setup(bot):
    bot.add_cog(Emoji(bot))

class Emoji(commands.Cog):

    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def emoji(self, ctx, emoji = None):
        '''Outputs your CUSTOM emoji... but bigger! (Does not work with standard discord emojis)'''
        emojiparts = emoji.replace("<","").replace(">","").split(":") if emoji else []
        if not len(emojiparts) == 3:
            await ctx.send("Usage: `{}emoji [emoji]` - must be a CUSTOM emoji, and not just the name.".format(ctx.prefix))
            return
        emoji_obj = discord.PartialEmoji(animated=len(emojiparts[0]) > 0, name=emojiparts[1], id=emojiparts[2])
        if not emoji_obj.url:
            await ctx.send("Could not find url for emoji :(")
            return
        f = await GetImage.download(emoji_obj.url)
        if not f:
            await ctx.send("I couldn't get that emoji :(")
            return
        await ctx.send(file=discord.File(f))

import discord
from discord.ext import commands
from Cogs import GetImage

def setup(bot):
    bot.add_cog(Emoji(bot))

class Emoji(commands.Cog):

    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def emoji(self, ctx, emoji = None):
        '''Outputs the passed emoji... but bigger!'''
        if emoji is None:
            await ctx.send("Usage: `{}emoji [emoji]`".format(ctx.prefix))
            return
        if len(emoji) < 3:
            # Try to get just the unicode
            h = "-".join([hex(ord(x)).lower()[2:] for x in emoji])
            url = "https://raw.githubusercontent.com/twitter/twemoji/gh-pages/2/72x72/{}.png".format(h)
            f = await GetImage.download(url)
            if not f:
                await ctx.send("I couldn't get that emoji :(")
            else:
                await ctx.send(file=discord.File(f))
            return
        emojiparts = emoji.replace("<","").replace(">","").split(":") if emoji else []
        if not len(emojiparts) == 3:
            await ctx.send("Usage: `{}emoji [emoji]`".format(ctx.prefix))
            return
        emoji_obj = discord.PartialEmoji(animated=len(emojiparts[0]) > 0, name=emojiparts[1], id=emojiparts[2])
        if not emoji_obj.url:
            await ctx.send("Could not find a url for that emoji :(")
            return
        f = await GetImage.download(emoji_obj.url)
        if not f:
            await ctx.send("I couldn't get that emoji :(")
            return
        await ctx.send(file=discord.File(f))

Add support for built-in emojis

Add support for built-in emojis


from __future__ import unicode_literals

from . import SessionCart, CART_SESSION_KEY

class CartMiddleware(object):
    '''
    Saves the cart instance into the django session.
    '''

    def process_request(self, request):
        try:
            cart_data = request.session[CART_SESSION_KEY]
            cart = SessionCart.from_storage(cart_data)
        except KeyError:
            cart = SessionCart()
        setattr(request, 'cart', cart)

    def process_response(self, request, response):
        if hasattr(request, 'cart'):
            request.session[CART_SESSION_KEY] = request.cart.for_storage()
        return response

from __future__ import unicode_literals

from . import SessionCart, CART_SESSION_KEY

class CartMiddleware(object):
    '''
    Saves the cart instance into the django session.
    '''

    def process_request(self, request):
        try:
            cart_data = request.session[CART_SESSION_KEY]
            cart = SessionCart.from_storage(cart_data)
        except KeyError:
            cart = SessionCart()
        setattr(request, 'cart', cart)

    def process_response(self, request, response):
        if hasattr(request, 'cart') and request.cart.modified:
            request.session[CART_SESSION_KEY] = request.cart.for_storage()
        return response

Store cart in session only when it was modified


#### Migrate person to a new account within the same Org

# Requires admin role
# Useful when migrating to Enterprise Logins.
# Reassigns all items/groups to new owner and
# adds userTo to all groups which userFrom is a member.'''

from agoTools.admin import Admin
myAgol = Admin('<username>')  # Replace <username> your ADMIN account

# for migrating a single account...
myAgol.migrateAccount(myAgol, '<userFrom>', '<userTo>')   # Replace with usernames between which you are moving items

# for migrating a batch of accounts...
myAgol.migrateAccounts(myAgol, <path to user mapping CSV>)   # Replace with path to CSV file with col1=userFrom, col2=userTo

#### Migrate a member to a new account within the same Organization

# Requires admin role
# Useful when migrating to Enterprise Logins
# Reassigns all items/groups to new owner
# Adds userTo to all groups which userFrom is a member

from agoTools.admin import Admin
myAgol = Admin('<username>')  # Replace <username> your ADMIN account

# un-comment one of the lines below, depending on which workflow you wish to use

### for migrating a single account...
# myAgol.migrateAccount(myAgol, '<userFrom>', '<userTo>')   # Replace with usernames between which you are moving items

### for migrating multiple accounts...
# myAgol.migrateAccounts(myAgol, <path to user mapping CSV>)   # Replace with path to CSV file with col1=userFrom, col2=userTo

Enhance comments in Migrate Account sample


from django.core.management.base import NoArgsCommand
from django.conf import settings
from popit import PopIt
from speeches.models import Speaker

class Command(NoArgsCommand):
    help = 'Populates the database with people from Popit'

    def handle_noargs(self, **options):
        api = PopIt(instance = settings.POPIT_INSTANCE,
                    hostname = settings.POPIT_HOSTNAME,
                    api_version = settings.POPIT_API_VERSION)
        results = api.person.get()
        for person in results['results']:
            speaker, created = Speaker.objects.get_or_create(popit_id=person['_id'])
            # we ignore created for now, just always set the name
            speaker.name = person['name']
            speaker.save();

import logging
from django.core.management.base import NoArgsCommand
from django.conf import settings
from popit import PopIt
from speeches.models import Speaker

logger = logging.getLogger(__name__)

class Command(NoArgsCommand):

    help = 'Populates the database with people from Popit'

    def handle_noargs(self, **options):
        api = PopIt(instance = settings.POPIT_INSTANCE,
                    hostname = settings.POPIT_HOSTNAME,
                    api_version = settings.POPIT_API_VERSION)
        results = api.person.get()
        for person in results['results']:

            logger.warn('Processing: {0}'.format(person['meta']['api_url']))

            speaker, created = Speaker.objects.get_or_create(popit_url=person['meta']['api_url'])

            logger.warn('Person was created? {0}'.format(created))
            logger.warn('Persons id in the spoke db is: {0}'.format(speaker.id))

            # we ignore created for now, just always set the name
            speaker.name = person['name']
            speaker.save();

Update speaker population command to set popit_url instead of popit_id


#!/usr/bin/env python3

import os
import tempfile
from distutils.sysconfig import get_python_lib

from coalib.tests.TestHelper import TestHelper

if __name__ == '__main__':
    parser = TestHelper.create_argparser(description="Runs coalas tests.")
    parser.add_argument("-b",
                        "--ignore-bear-tests",
                        help="ignore bear tests",
                        action="store_true")
    parser.add_argument("-m",
                        "--ignore-main-tests",
                        help="ignore main program tests",
                        action="store_true")

    testhelper = TestHelper(parser)

    if not testhelper.args.ignore_main_tests:
        testhelper.add_test_files(os.path.abspath(os.path.join("coalib",
                                                               "tests")))
    if not testhelper.args.ignore_bear_tests:
        testhelper.add_test_files(os.path.abspath(os.path.join("bears",
                                                               "tests")))

    ignore_list = [
        os.path.join(tempfile.gettempdir(), "**"),
        os.path.join(get_python_lib(), "**"),
        os.path.join("coalib", "tests", "**"),
        os.path.join("bears", "tests", "**")
    ]

    exit(testhelper.execute_python3_files(ignore_list))

#!/usr/bin/env python3

import os
import tempfile
from distutils.sysconfig import get_python_lib

from coalib.tests.TestHelper import TestHelper

if __name__ == '__main__':
    parser = TestHelper.create_argparser(description="Runs coalas tests.")
    parser.add_argument("-b",
                        "--ignore-bear-tests",
                        help="ignore bear tests",
                        action="store_true")
    parser.add_argument("-m",
                        "--ignore-main-tests",
                        help="ignore main program tests",
                        action="store_true")

    testhelper = TestHelper(parser)

    if not testhelper.args.ignore_main_tests:
        testhelper.add_test_files(os.path.abspath(os.path.join("coalib",
                                                               "tests")))
    if not testhelper.args.ignore_bear_tests:
        testhelper.add_test_files(os.path.abspath(os.path.join("bears",
                                                               "tests")))

    ignore_list = [
        os.path.join(tempfile.gettempdir(), "**"),
        os.path.join(os.path.dirname(get_python_lib()), "**"),
        os.path.join("coalib", "tests", "**"),
        os.path.join("bears", "tests", "**")
    ]

    exit(testhelper.execute_python3_files(ignore_list))

Update run_test.py to fix coverage

tests: Update run_test.py to fix coverage


import astropy.io.fits as fits
import numpy as np
import sys

from .util import stack_fits_data

METHOD_MAP = {"median": lambda x: np.median(x, axis=0, overwrite_input=True),
              "average": lambda x: np.average(x, axis=0),
              "sum": lambda x: np.sum(x, axis=0)}

def create_parser(subparsers):
    parser_combine = subparsers.add_parser("combine", help="combine help")
    parser_combine.add_argument("-m", "--method", choices=["median", "average", "sum"], required=True)
    parser_combine.add_argument("-o", "--output", metavar="OUTFILE", default=sys.stdout.buffer)
    parser_combine.add_argument("file", nargs="+")
    parser_combine.set_defaults(func=main)

def main(args):
    image_stack = stack_fits_data(args.file)
    result = METHOD_MAP[args.method](image_stack)
    hdu = fits.PrimaryHDU(result)
    hdu.writeto(args.output)

import astropy.io.fits as fits
import numpy as np
import sys

from .util import stack_fits_data

METHOD_MAP = {"median": lambda x: np.median(x, axis=0, overwrite_input=True),
              "average": lambda x: np.mean(x, axis=0),
              "sum": lambda x: np.sum(x, axis=0)}

def create_parser(subparsers):
    parser_combine = subparsers.add_parser("combine", help="combine help")
    parser_combine.add_argument("-m", "--method", choices=["median", "average", "sum"], required=True)
    parser_combine.add_argument("-o", "--output", metavar="OUTFILE", default=sys.stdout.buffer)
    parser_combine.add_argument("file", nargs="+")
    parser_combine.set_defaults(func=main)

def main(args):
    image_stack = stack_fits_data(args.file)
    result = METHOD_MAP[args.method](image_stack)
    hdu = fits.PrimaryHDU(result)
    hdu.writeto(args.output)

Use np.mean instead for unweighted mean

Use np.mean instead for unweighted mean

Signed-off-by: Lizhou Sha <d6acb26e253550574bc1141efa0eb5e6de15daeb@mit.edu>


from __future__ import absolute_import, print_function
import unittest
from qtpy.QtWidgets import QApplication
from addie.rietveld.braggtree import BraggTree, BankRegexException

class BraggTreeTests(unittest.TestCase):
    def setUp(self):
        self.main_window = QApplication([])

    def tearDown(self):
        self.main_window.quit()

    def test_get_bank_id(self):
        """Test we can extract a bank id from bank workspace name"""
        braggtree = BraggTree(None)
        target = 12345
        bank_wksp_name = "Bank {} - 90.0".format(target)
        bank_id = braggtree._get_bank_id(bank_wksp_name)
        self.assertEqual(int(bank_id), target)

    def test_get_bank_id_exception(self):
        """Test for raised exception from a bad workspace name"""
        braggtree = BraggTree(None)
        bad_ws = "Bank jkl 1 -- 90.0"
        self.assertRaises(BankRegexException, braggtree._get_bank_id, bad_ws)

    def test_do_plot_ws_exception(self):
        """Test for raised exception from MainWindow==None"""
        braggtree = BraggTree(None)
        self.assertRaises(NotImplementedError, braggtree.do_plot_ws)

if __name__ == '__main__':
    unittest.main()  # pragma: no cover

from __future__ import absolute_import, print_function
import pytest
from addie.main import MainWindow
from addie.rietveld.braggtree import BraggTree, BankRegexException

@pytest.fixture
def braggtree():
    return BraggTree(None)

def test_get_bank_id(qtbot, braggtree):
    """Test we can extract a bank id from bank workspace name"""
    target = 12345
    bank_wksp_name = "Bank {} - 90.0".format(target)
    bank_id = braggtree._get_bank_id(bank_wksp_name)
    assert int(bank_id) == target

def test_get_bank_id_exception(qtbot, braggtree):
    """Test for raised exception from a bad workspace name"""
    bad_ws = "Bank jkl 1 -- 90.0"
    with pytest.raises(BankRegexException) as e:
        braggtree._get_bank_id(bad_ws)

def test_do_plot_ws_exception(qtbot, braggtree):
    """Test for raised exception from MainWindow==None"""
    with pytest.raises(NotImplementedError) as e:
        braggtree.do_plot_ws()

Refactor BraggTree test to use pytest-qt


import logging

__version__ = "0.20.0rc10"
version = tuple(__version__.split('.'))

logging.getLogger(__name__).addHandler(logging.NullHandler())

import logging

__version__ = "0.20.0rc11"
version = tuple(__version__.split('.'))

logging.getLogger(__name__).addHandler(logging.NullHandler())

Bump version 0.20.0rc10 --> 0.20.0rc11

Bump version 0.20.0rc10 --> 0.20.0rc11

Signed-off-by: Jack Robison <40884020c67726395ea162083a125620dc32cdab@lbry.io>


import logging

__version__ = "0.14.2rc6"
version = tuple(__version__.split('.'))

logging.getLogger(__name__).addHandler(logging.NullHandler())

import logging

__version__ = "0.14.2rc7"
version = tuple(__version__.split('.'))

logging.getLogger(__name__).addHandler(logging.NullHandler())

Bump version 0.14.2rc6 --> 0.14.2rc7

Bump version 0.14.2rc6 --> 0.14.2rc7

Signed-off-by: Jack Robison <40884020c67726395ea162083a125620dc32cdab@lbry.io>


# -*- coding: utf-8 -*-
from django.db import models

# Create your models here.
class Necropsy (models.Model):
	clinical_information = models.TextField(null=True, blank=True)
	macroscopic = models.TextField(null=True, blank=True)
	microscopic = models.TextField(null=True, blank=True)
	conclusion = models.TextField(null=True, blank=True)
	notes = models.TextField(null=True, blank=True)
	footer = models.TextField(null=True, blank=True)

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

from django.db import models
from modeling.exam import Exam
from modeling.report import ReportStatus

class NecropsyStatus(models.Model):
    description = models.CharField(max_length=50)

class Necropsy(models.Model):
    clinical_information = models.TextField(null=True, blank=True)
    main_disease = models.TextField(null=True, blank=True)
    consequential_final_disease = models.TextField(null=True, blank=True)
    contributors_disease = models.TextField(null=True, blank=True)
    consequential_disease = models.TextField(null=True, blank=True)
    other_disases = models.TextField(null=True, blank=True)
    note = models.TextField(null=True, blank=True)
    footer = models.TextField(null=True, blank=True)
    status = models.ForeignKey(NecropsyStatus)
    exam = models.ForeignKey(Exam)

class NecropsyReport(models.Model):
    clinical_information = models.TextField(null=True, blank=True)
    main_disease = models.TextField(null=True, blank=True)
    consequential_final_disease = models.TextField(null=True, blank=True)
    contributors_disease = models.TextField(null=True, blank=True)
    consequential_disease = models.TextField(null=True, blank=True)
    other_disases = models.TextField(null=True, blank=True)
    note = models.TextField(null=True, blank=True)
    footer = models.TextField(null=True, blank=True)
    status = models.ForeignKey(ReportStatus)
    necropsy = models.ForeignKey(Necropsy)

Add NecropsyReport in Model Necropsy

Add NecropsyReport in Model Necropsy


import os

# must call "sudo apt-get install sendmail" first...

# if sts != 0:
#     print("Sendmail exit status "+str(sts))

def send_error(recipient, error_details):
	SENDMAIL = "/usr/sbin/sendmail" # sendmail location

	p = os.popen("%s -t" % SENDMAIL, "w")
	p.write("To: "+recipient+"\n")
	p.write("Subject: FoldAtlas error\n")
	p.write("\n") # blank line separating headers from body
	p.write("Some text\n")
	p.write("some more text\n")
	sts = p.close()

import traceback
import os
import urllib.request  # the lib that handles the url stuff

test_url = "http://www.foldatlas.com/transcript/AT2G45180.1"
recipient = "matthew.gs.norris@gmail.com"
search_str = "AT2G45180.1"

def run_test():
	try:
		data = urllib.request.urlopen(test_url) # it's a file like object and works just like a file
		text = str(data.read())

		if search_str in text:
			print("It worked!")
		else:
			send_error(text)
	except:
		send_error(traceback.format_exc())

def send_error(error_details):
	print("FAILED")
	SENDMAIL = "/usr/sbin/sendmail" # sendmail location

	p = os.popen("%s -t" % SENDMAIL, "w")
	p.write("To: "+recipient+"\n")
	p.write("Subject: FoldAtlas error\n")
	p.write("\n") # blank line separating headers from body
	p.write(error_details)
	sts = p.close()

run_test()

Monitor now checks and emails

Monitor now checks and emails


#!/usr/bin/env python

# tests require pytest-cov and pytest-xdist
import os
import signal
import sys
from bluesky.testing.noseclasses import KnownFailure
import pytest

try:
    from pcaspy import Driver, SimpleServer
    from multiprocessing import Process

    def to_subproc():

        prefix = 'BSTEST:'
        pvdb = {
            'VAL': {
                'prec': 3,
            },
        }

        class myDriver(Driver):
            def __init__(self):
                super(myDriver, self).__init__()

        if __name__ == '__main__':
            server = SimpleServer()
            server.createPV(prefix, pvdb)
            driver = myDriver()

            # process CA transactions
            while True:
                try:
                    server.process(0.1)
                except KeyboardInterrupt:
                    break

    p = Process(target=to_subproc)
except ImportError:
    p = None

def run():
    if p is not None:
        p.start()
    try:
        args = ['--cov bluesky']
        args.extend(sys.argv)
        pytest.main(args)
    finally:
        if p is not None:
            os.kill(p.pid, signal.SIGINT)
            p.join()

if __name__ == '__main__':
    run()

#!/usr/bin/env python

# tests require pytest-cov and pytest-xdist
import os
import signal
import sys
import pytest

try:
    from pcaspy import Driver, SimpleServer
    from multiprocessing import Process

    def to_subproc():

        prefix = 'BSTEST:'
        pvdb = {
            'VAL': {
                'prec': 3,
            },
        }

        class myDriver(Driver):
            def __init__(self):
                super(myDriver, self).__init__()

        if __name__ == '__main__':
            server = SimpleServer()
            server.createPV(prefix, pvdb)
            driver = myDriver()

            # process CA transactions
            while True:
                try:
                    server.process(0.1)
                except KeyboardInterrupt:
                    break

    p = Process(target=to_subproc)
except ImportError:
    p = None

def run():
    if p is not None:
        p.start()
    try:
        # adding rxs to show extra info on skips and xfails
        args = ['--cov bluesky -srx -vv']
        args.extend(sys.argv)
        pytest.main(args)
    finally:
        if p is not None:
            os.kill(p.pid, signal.SIGINT)
            p.join()

if __name__ == '__main__':
    run()

TST: Remove deleted subpackage. Add better args to pytest


# Copyright (c) 2016 The Regents of the University of Michigan
# All rights reserved.
# This software is licensed under the BSD 3-Clause License.

class Error(Exception):
    pass

class ConfigError(Error, RuntimeError):
    pass

class AuthenticationError(Error, RuntimeError):

    def __str__(self):
        if len(self.args) > 0:
            return "Failed to authenticate with host '{}'.".format(
                self.args[0])
        else:
            return "Failed to authenticate with host."

class ExportError(Error, RuntimeError):
    pass

class FileNotFoundError(Error, FileNotFoundError):
    pass

class FetchError(FileNotFoundError):
    pass

# Copyright (c) 2016 The Regents of the University of Michigan
# All rights reserved.
# This software is licensed under the BSD 3-Clause License.
from . import six

class Error(Exception):
    pass

class ConfigError(Error, RuntimeError):
    pass

class AuthenticationError(Error, RuntimeError):

    def __str__(self):
        if len(self.args) > 0:
            return "Failed to authenticate with host '{}'.".format(
                self.args[0])
        else:
            return "Failed to authenticate with host."

class ExportError(Error, RuntimeError):
    pass

if six.PY2:
    class FileNotFoundError(Error, IOError):
        pass
else:
    class FileNotFoundError(Error, FileNotFoundError):
        pass

class FetchError(FileNotFoundError):
    pass

Fix py27 issue in error module.

Fix py27 issue in error module.

Inherit signac internal FileNotFoundError class from IOError
instead of FileNotFoundError in python 2.7.


"""
Unittests for opal.core.commandline
"""
from opal.core.test import OpalTestCase

from opal.core import commandline

"""
Unittests for opal.core.commandline
"""
from mock import patch, MagicMock

from opal.core.test import OpalTestCase

from opal.core import commandline

class StartprojectTestCase(OpalTestCase):

    def test_startproject(self):
        mock_args = MagicMock(name='Mock Args')
        mock_args.name = 'projectname'
        with patch.object(commandline.scaffold_utils, 'start_project') as sp:
            commandline.startproject(mock_args)
            sp.assert_called_with('projectname', commandline.USERLAND_HERE)

class StartpluginTestCase(OpalTestCase):

    def test_startplugin(self):
        mock_args = MagicMock(name='Mock Args')
        mock_args.name = 'pluginname'
        with patch.object(commandline.scaffold_utils, 'start_plugin') as sp:
            commandline.startplugin(mock_args)
            sp.assert_called_with('pluginname', commandline.USERLAND_HERE)

Add simple basic unittests for some of our commandline argparse target functions


"""Imports for IPython"""

# pylint: disable=W0611

# import this

import os
import re
import sys
import inspect
pyprint = print

mores = []

try:
    from rich.console import Console
    console = Console(color_system="standard")
    print = console.print
    mores += ["rich"]
except ImportError:
    pass

try:
    from importlib import reload
except ImportError:
    def reload(x):
        raise NotImplementedError("importlib.reload is not available")

try:
    import requests
    mores += ["requests"]
except ModuleNotFoundError:
    pass

try:
    import pysyte
    from pysyte.types import paths
    from pysyte.types.paths import path
    from pysyte import cli
except ImportError as e:
    print(e)
    sys.stderr.write("pip install pysyte # please")

try:
    from pathlib import Path
    mores += ["Path"]
except ImportError:
    pass

more = ", ".join([" "] + mores) if mores else ""
executable = sys.executable.replace(os.environ['HOME'], '~')
version = sys.version.split()[0]
stdout = lambda x: sys.stdout.write(f"{x}\n")

stdout(f"import os, re, sys, inspect, pysyte, paths, path, cli{more}")

stdout("")
stdout(f"{executable} {version}")

"""Imports for IPython"""

# pylint: disable=W0611

# import this

import os
import re
import sys
import inspect
pyprint = print

mores = []

try:
    from rich.console import Console
    console = Console(color_system="standard")
    print = console.print
    mores += ["rich"]
    from rich import pretty
    pretty.install()
except ImportError:
    pass

try:
    from importlib import reload
except ImportError:
    def reload(x):
        raise NotImplementedError("importlib.reload is not available")

try:
    import requests
    mores += ["requests"]
except ModuleNotFoundError:
    pass

try:
    import pysyte
    from pysyte.types import paths
    from pysyte.types.paths import path
    from pysyte import cli
except ImportError as e:
    print(e)
    sys.stderr.write("pip install pysyte # please")

try:
    from pathlib import Path
    mores += ["Path"]
except ImportError:
    pass

more = ", ".join([" "] + mores) if mores else ""
executable = sys.executable.replace(os.environ['HOME'], '~')
version = sys.version.split()[0]
stdout = lambda x: sys.stdout.write(f"{x}\n")

stdout(f"import os, re, sys, inspect, pysyte, paths, path, cli{more}")

stdout("")
stdout(f"{executable} {version}")

Use rich for printing in ipython

Use rich for printing in ipython


# Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.

def get_check_digit_from_checkable_int (number):
    return (9 * ((number // 10) + rotate_digit(number % 10))) % 10

def rotate_digit (digit):
    if digit > 4:
        return (digit * 2) - 9

    else:
        return digit * 2

def get_check_digit (number = None):
    if number:
        return get_check_digit_from_checkable_int(int(number))

    else:
        return None

# Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.

def rotate_digit (digit):
    if digit > 4:
        return (digit * 2) - 9

    else:
        return digit * 2

def get_check_digit_from_checkable_int (number):
    return (9 * ((number // 10) + rotate_digit(number % 10))) % 10

def get_check_digit (number = None):
    if number:
        return get_check_digit_from_checkable_int(int(number))

    else:
        return None

Reorder methods to make sense

Reorder methods to make sense


import os
import jinja2

basedir = os.path.abspath(os.path.dirname(__file__))

class Config:
    @staticmethod
    def init_app(app):
        repo_root = os.path.abspath(os.path.dirname(__file__))
        template_folders = [
            os.path.join(repo_root,
                         'bower_components/govuk_template/views/layouts'),
            os.path.join(repo_root, 'app/templates')
        ]
        jinja_loader = jinja2.FileSystemLoader(template_folders)
        app.jinja_loader = jinja_loader

class Test(Config):
    DEBUG = True

class Development(Config):
    DEBUG = True,
    BASE_TEMPLATE_DATA = {
        'asset_path': '/static/',
        'header_class': 'with-proposition'
    }

class Live(Config):
    DEBUG = False

config = {
    'live': Live,
    'development': Development,
    'test': Test,
    'default': Development
}

import os
import jinja2

basedir = os.path.abspath(os.path.dirname(__file__))

class Config(object):
    DEBUG = False
    STATIC_URL_PATH = '/supplier/static'
    ASSET_PATH = STATIC_URL_PATH + '/'
    BASE_TEMPLATE_DATA = {
        'asset_path': ASSET_PATH,
        'header_class': 'with-proposition'
    }

    @staticmethod
    def init_app(app):
        repo_root = os.path.abspath(os.path.dirname(__file__))
        template_folders = [
            os.path.join(repo_root,
                         'bower_components/govuk_template/views/layouts'),
            os.path.join(repo_root, 'app/templates')
        ]
        jinja_loader = jinja2.FileSystemLoader(template_folders)
        app.jinja_loader = jinja_loader

class Test(Config):
    DEBUG = True

class Development(Config):
    DEBUG = True,

class Live(Config):
    DEBUG = False

config = {
    'live': Live,
    'development': Development,
    'test': Test,
    'default': Development
}

Add supplier/ prefix to static file paths


import os

class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY')
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True

    @staticmethod
    def init_app(app):
        pass

class DevelopmentConfig(Config):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL')

class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL')

class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')

    @classmethod
    def init_app(cls, app):
        pass

class HerokuConfig(ProductionConfig):
    @classmethod
    def init_app(cls, app):
        ProductionConfig.init_app(app)

        # # log to stderr
        # import logging
        # from logging import StreamHandler
        # file_handler = StreamHandler()
        # file_handler.setLevel(logging.WARNING)
        # app.logger.addHandler(file_handler)

config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'heroku': HerokuConfig,

    'default': DevelopmentConfig
}

import os

class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY')
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True

    @staticmethod
    def init_app(app):
        pass

class DevelopmentConfig(Config):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL')

class TestingConfig(Config):
    TESTING = True

    if os.environ.get('TEST_DATABASE_URL'):
        SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL')
    else:
        basedir = os.path.abspath(os.path.dirname(__file__))
        SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'data-test.sqlite')

class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')

    @classmethod
    def init_app(cls, app):
        pass

class HerokuConfig(ProductionConfig):
    @classmethod
    def init_app(cls, app):
        ProductionConfig.init_app(app)

        # # log to stderr
        # import logging
        # from logging import StreamHandler
        # file_handler = StreamHandler()
        # file_handler.setLevel(logging.WARNING)
        # app.logger.addHandler(file_handler)

config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'heroku': HerokuConfig,

    'default': DevelopmentConfig
}

Use sqlite as DB for test if none set in environment


from __future__ import absolute_import

from rest_framework.response import Response

from sentry.api import client
from sentry.api.base import DocSection
from sentry.api.bases.group import GroupEndpoint

class GroupEventsLatestEndpoint(GroupEndpoint):
    doc_section = DocSection.EVENTS

    def get(self, request, group):
        """
        Retrieve the latest sample for an aggregate

        Return details on the latest sample for this aggregate.

            {method} {path}

        """
        event = group.get_latest_event()

        try:
            return client.get('/events/{}/'.format(event.id), request.user, request.auth)
        except client.ApiError as e:
            return Response(e.body, status=e.status_code)

from __future__ import absolute_import

from rest_framework.response import Response

from sentry.api import client
from sentry.api.base import DocSection
from sentry.api.bases.group import GroupEndpoint

class GroupEventsLatestEndpoint(GroupEndpoint):
    doc_section = DocSection.EVENTS

    def get(self, request, group):
        """
        Retrieve the latest sample for an aggregate

        Return details on the latest sample for this aggregate.

            {method} {path}

        """
        event = group.get_latest_event()
        if not event:
            return Response({'detail': 'No events found for group'}, status=404)

        try:
            return client.get('/events/{}/'.format(event.id), request.user, request.auth)
        except client.ApiError as e:
            return Response(e.body, status=e.status_code)

Handle no latest event (fixes GH-1727)

Handle no latest event (fixes GH-1727)


#!/usr/bin/python2.5
#
# Copyright 2009 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This module contains the ProjectSurvey model.
"""

__authors__ = [
  '"Daniel Diniz" <ajaksu@gmail.com>',
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
]

from soc.models.survey import Survey

class ProjectSurvey(Survey):
  """Survey for Students that have a StudentProject.
  """

  def __init__(self, *args, **kwargs):
    super(ProjectSurvey, self).__init__(*args, **kwargs)
    # TODO: prefix has to be set to gsoc_program once data has been transferred
    self.prefix = 'program'
    self.taking_access = 'student'

#!/usr/bin/python2.5
#
# Copyright 2009 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This module contains the ProjectSurvey model.
"""

__authors__ = [
  '"Daniel Diniz" <ajaksu@gmail.com>',
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
]

from soc.models.survey import Survey

class ProjectSurvey(Survey):
  """Survey for Students that have a StudentProject.
  """

  def __init__(self, *args, **kwargs):
    super(ProjectSurvey, self).__init__(*args, **kwargs)
    self.prefix = 'gsoc_program'
    self.taking_access = 'student'

Set the default prefix for ProjectSurveys to gsoc_program.


from collections import defaultdict
from feature_extraction.util import DefaultAttributeDict

class Measurement(object):
	"""
	A generic feature measurement.

	Attributes
	----------
	default_options
		Can be set by subclasses to set default option values
	"""

	default_options = {}

	def __init__(self, options=None):
		"""
		When initializing this measurement, options can be passed.
		These are exposed to internal algorithms as `self.options`.

		Parameters
		----------
		options : dict
			A dict of options for this measurement.
		"""
		self.options = DefaultAttributeDict()
		self.options.update(self.default_options or {})
		self.options.update(options or {})

from .pixelaverage import PixelAverage
from .texture_haralick import HaralickTexture

from collections import defaultdict
from feature_extraction.util import AttributeDict

class Measurement(object):
	"""
	A generic feature measurement.

	Attributes
	----------
	default_options
		Can be set by subclasses to set default option values
	"""

	default_options = {}

	def __init__(self, options=None):
		"""
		When initializing this measurement, options can be passed.
		These are exposed to internal algorithms as `self.options`.

		Parameters
		----------
		options : dict
			A dict of options for this measurement.
		"""
		self.options = AttributeDict()
		self.options.update(self.default_options or {})
		self.options.update(options or {})

from .pixelaverage import PixelAverage
from .texture_haralick import HaralickTexture

Switch back to AttributeDict for measurement options


import unittest2
import datetime

from google.appengine.ext import testbed

from datafeeds.datafeed_fms import DatafeedFms

class TestDatafeedFmsTeams(unittest2.TestCase):

    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_urlfetch_stub()
        self.testbed.init_memcache_stub()

        self.datafeed = DatafeedFms()

    def tearDown(self):
        self.testbed.deactivate()

    def test_getFmsTeamList(self):
        teams = self.datafeed.getFmsTeamList()
        self.find177(teams)

    def find177(self, teams):
        found_177 = False
        for team in teams:
            if team.team_number == 177:
                found_177 = True
                self.assertEqual(team.name, "UTC / Ensign Bickford Aerospace & Defense & South Windsor High School")
                #self.assertEqual(team.address, u"South Windsor, CT, USA")
                self.assertEqual(team.nickname, "Bobcat Robotics")

        self.assertTrue(found_177)
        self.assertTrue(len(teams) > 0)

import unittest2
import datetime

from google.appengine.ext import testbed

from datafeeds.datafeed_fms import DatafeedFms

class TestDatafeedFmsTeams(unittest2.TestCase):

    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_urlfetch_stub()
        self.testbed.init_memcache_stub()

        self.datafeed = DatafeedFms()

    def tearDown(self):
        self.testbed.deactivate()

    def test_getFmsTeamList(self):
        teams = self.datafeed.getFmsTeamList()
        self.find177(teams)

    def find177(self, teams):
        found_177 = False
        for team in teams:
            if team.team_number == 177:
                found_177 = True
                self.assertEqual(team.name, "ClearEdge Power / United Technologies / Gain Talent / EBA&D & South Windsor High School")
                #self.assertEqual(team.address, u"South Windsor, CT, USA")
                self.assertEqual(team.nickname, "Bobcat Robotics")

        self.assertTrue(found_177)
        self.assertTrue(len(teams) > 0)

Update test case for 2014

Update test case for 2014


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

# Copyright 2013 Jacek Mitręga

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import unicode_literals

import envoy
import logging

logger = logging.getLogger(__name__)

class KdeWindowManager(object):

    def __init__(self, workspace=6):
        self.prev_workspace = 1
        self.workspace = 6

    def show(self):
        envoy.run('qdbus org.kde.kwin /KWin org.kde.KWin.setCurrentDesktop 6',
                  timeout=2)
        # envoy.run('killall firefox', timeout=2)
        # envoy.connect('firefox http://standup-backend.herokuapp.com/?room=12')

    def hide(self):
        envoy.run(('qdbus org.kde.kwin /KWin '
                  'org.kde.KWin.setCurrentDesktop {}')
                      .format(self.prev_workspace),
                  timeout=2)
        # envoy.run('killall firefox', timeout=2)

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

# Copyright 2013 Jacek Mitręga

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import unicode_literals

import envoy
import logging

logger = logging.getLogger(__name__)

class KdeWindowManager(object):

    def __init__(self, workspace=6):
        self.prev_workspace = 1
        self.workspace = 6

    def show(self):
        logger.info('kde show')
        envoy.run('qdbus org.kde.kwin /KWin org.kde.KWin.setCurrentDesktop 6',
                  timeout=2)
        # envoy.run('killall firefox', timeout=2)
        # envoy.connect('firefox http://standup-backend.herokuapp.com/?room=12')

    def hide(self):
        logger.info('kde hide')
        envoy.run(('qdbus org.kde.kwin /KWin '
                  'org.kde.KWin.setCurrentDesktop {}')
                      .format(self.prev_workspace),
                  timeout=2)
        # envoy.run('killall firefox', timeout=2)

Add KDE show & hide logging

Add KDE show & hide logging


"""
categories: Types,Exception
description: Exception.__init__ raises TypeError if overridden and called by subclass
cause: Unknown
workaround: Unknown
"""
class A(Exception):
    def __init__(self):
        Exception.__init__(self)

a = A()

"""
categories: Types,Exception
description: Exception.__init__ method does not exist.
cause: Subclassing native classes is not fully supported in MicroPython.
workaround: Call using ``super()`` instead::

class A(Exception):
    def __init__(self):
        super().__init__()
"""
class A(Exception):
    def __init__(self):
        Exception.__init__(self)

a = A()

tests/cpydiff: Update subclassing Exception case and give work-around.


import base64
from Crypto.Cipher import AES
from Crypto import Random

def encrypt(raw, key):
    raw = pad(raw)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    return base64.b64encode(iv + cipher.encrypt(raw))

def decrypt(enc, key):
    enc = base64.b64decode(enc)
    iv = enc[:AES.block_size]
    cipher = AES.new(key, AES.MODE_CBC, iv)
    result = unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')
    return result

def pad(s):
    bs = 32
    return s + (bs - len(s) % bs) * chr(bs - len(s) % bs)

def unpad(s):
    return s[:-ord(s[len(s)-1:])]

import base64
from Crypto.Cipher import AES
from Crypto import Random

def encrypt(raw, key):
    raw = pad(raw)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    return base64.b64encode(iv + cipher.encrypt(raw)).decode('utf-8')

def decrypt(enc, key):
    enc = base64.b64decode(enc)
    iv = enc[:AES.block_size]
    cipher = AES.new(key, AES.MODE_CBC, iv)
    result = unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')
    return result

def pad(s):
    bs = 32
    return s + (bs - len(s) % bs) * chr(bs - len(s) % bs)

def unpad(s):
    return s[:-ord(s[len(s)-1:])]

Add decode(utf-8) to return on encrypt

Add decode(utf-8) to return on encrypt


from Bio import SeqIO

def sequence_length(fasta):
    """
    return a dict of the lengths of sequences in a fasta file
    """
    file_handle = open(fasta)
    in_handle = SeqIO.parse(file_handle, "fasta")
    records = {record.id: len(record) for record in in_handle}
    file_handle.close()
    return records

from Bio import SeqIO

def sequence_length(fasta):
    """
    return a dict of the lengths of sequences in a fasta file
    """
    sequences = SeqIO.parse(fasta, "fasta")
    records = {record.id: len(record) for record in sequences}
    return records

def sequence_names(fasta):
    """
    return a list of the sequence IDs in a FASTA file
    """
    sequences = SeqIO.parse(fasta, "fasta")
    records = [record.id for record in sequences]
    return records

Add function to get list of sequence names from a FASTA file.

Refactor to be simpler.


#!/usr/bin/env python

# Copyright (c) 2010 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
Verifies that .so files that are order only dependencies are specified by
their install location rather than by their alias.
"""

import os
import TestGyp

test = TestGyp.TestGyp(formats=['make'])

test.run_gyp('shared_dependency.gyp',
             chdir='src')
test.relocate('src', 'relocate/src')

test.build('shared_dependency.gyp', test.ALL, chdir='relocate/src')

with open('relocate/src/Makefile') as makefile:
  make_contents = makefile.read()

# If we remove the code to generate lib1, Make should still be able
# to build lib2 since lib1.so already exists.
make_contents = make_contents.replace('include lib1.target.mk', '')
with open('relocate/src/Makefile', 'w') as makefile:
  makefile.write(make_contents)

test.build('shared_dependency.gyp', test.ALL, chdir='relocate/src')

test.pass_test()

#!/usr/bin/env python

# Copyright (c) 2010 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
Verifies that .so files that are order only dependencies are specified by
their install location rather than by their alias.
"""

# Python 2.5 needs this for the with statement.
from __future__ import with_statement

import os
import TestGyp

test = TestGyp.TestGyp(formats=['make'])

test.run_gyp('shared_dependency.gyp',
             chdir='src')
test.relocate('src', 'relocate/src')

test.build('shared_dependency.gyp', test.ALL, chdir='relocate/src')

with open('relocate/src/Makefile') as makefile:
  make_contents = makefile.read()

# If we remove the code to generate lib1, Make should still be able
# to build lib2 since lib1.so already exists.
make_contents = make_contents.replace('include lib1.target.mk', '')
with open('relocate/src/Makefile', 'w') as makefile:
  makefile.write(make_contents)

test.build('shared_dependency.gyp', test.ALL, chdir='relocate/src')

test.pass_test()

Add with_statement import for python2.5.

See http://www.python.org/dev/peps/pep-0343/ which describes
the with statement.
Review URL: http://codereview.chromium.org/5690003


registered = {}
registered_lock = {}
parameters = {}

def register(f=None, lock=True, params={}):
    """Decorator to add the function to the cronjob library.

        @cronjobs.register
        def my_task():
            print('I can be run once/machine at a time.')

        @cronjobs.register(lock=False)
        def my_task():
            print('I am concurrent friendly!')

    """

    def decorator(f, lock=lock):
        registered[f.__name__] = f
        parameters[f.__name__] = params
        if lock:
            registered_lock[f.__name__] = f
        return f

    if callable(f):
        return decorator(f, lock)
    return decorator

registered = {}
registered_lock = {}
parameters = {}
from decorator import decorator

def register(f=None, lock=True, params={}):
    """Decorator to add the function to the cronjob library.

        @cronjobs.register
        def my_task():
            print('I can be run once/machine at a time.')

        @cronjobs.register(lock=False)
        def my_task():
            print('I am concurrent friendly!')

    """

    def cron_decorator(f, lock=lock):
        registered[f.__name__] = f
        parameters[f.__name__] = params
        if lock:
            registered_lock[f.__name__] = f
        return f

    if callable(f):
        return decorator(cron_decorator(f, lock),f)
    return cron_decorator

Change decorators in cron to preserve signature


from neomodel import StructuredNode, StringProperty, DoesNotExist, CypherException
import pickle

class Person(StructuredNode):
    name = StringProperty(unique_index=True)

def test_cypher_exception_can_be_displayed():
    print CypherException("SOME QUERY", (), "ERROR", None, None)

def test_object_does_not_exist():
    try:
        Person.nodes.get(name="johnny")
    except Person.DoesNotExist as e:
        pickle_instance = pickle.dumps(e)
        assert pickle_instance
        assert pickle.loads(pickle_instance)
        assert isinstance(pickle.loads(pickle_instance), DoesNotExist)

def test_raise_does_not_exist():
    try:
        raise DoesNotExist("My Test Message")
    except DoesNotExist as e:
        pickle_instance = pickle.dumps(e)
        assert pickle_instance
        assert pickle.loads(pickle_instance)
        assert isinstance(pickle.loads(pickle_instance), DoesNotExist)

from neomodel import StructuredNode, StringProperty, DoesNotExist, CypherException
import pickle

class Person(StructuredNode):
    name = StringProperty(unique_index=True)

def test_cypher_exception_can_be_displayed():
    print(CypherException("SOME QUERY", (), "ERROR", None, None))

def test_object_does_not_exist():
    try:
        Person.nodes.get(name="johnny")
    except Person.DoesNotExist as e:
        pickle_instance = pickle.dumps(e)
        assert pickle_instance
        assert pickle.loads(pickle_instance)
        assert isinstance(pickle.loads(pickle_instance), DoesNotExist)

def test_raise_does_not_exist():
    try:
        raise DoesNotExist("My Test Message")
    except DoesNotExist as e:
        pickle_instance = pickle.dumps(e)
        assert pickle_instance
        assert pickle.loads(pickle_instance)
        assert isinstance(pickle.loads(pickle_instance), DoesNotExist)

Fix syntax Error for compability with python 3.X


from django.shortcuts import render
from django.conf.urls.defaults import *
from django.contrib.auth.decorators import user_passes_test

from .forms import ProtocolForm, TopicFormSet

def can_add_protocols(user):
    return user.is_authenticated() and user.has_perm('protocols.add_protocol')

@user_passes_test(can_add_protocols)
def add(request):
    data = request.POST if request.POST else None
    protocol_form = ProtocolForm(data)
    topic_form = TopicFormSet(data)

    if protocol_form.is_valid() and topic_form.is_valid():
        protocol_form.save()
        topic_form.save()

    return render(request, 'protocols/add.html', locals())

from django.shortcuts import render
from django.conf.urls import *
from django.contrib.auth.decorators import user_passes_test

from .forms import ProtocolForm, TopicFormSet

def can_add_protocols(user):
    return user.is_authenticated() and user.has_perm('protocols.add_protocol')

@user_passes_test(can_add_protocols)
def add(request):
    data = request.POST if request.POST else None
    protocol_form = ProtocolForm(data)
    topic_form = TopicFormSet(data)

    if protocol_form.is_valid() and topic_form.is_valid():
        protocol_form.save()
        topic_form.save()

    return render(request, 'protocols/add.html', locals())

Change django.conf.urls.defaults (it is depricated)


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

import os
import subprocess

class ProtractorTestCaseMixin(object):
    protractor_conf = 'protractor.conf.js'
    suite = None
    specs = None

    @classmethod
    def setUpClass(cls):
        super(ProtractorTestCaseMixin, cls).setUpClass()
        with open(os.devnull, 'wb') as f:
            subprocess.call(['webdriver-manager', 'update'], stdout=f, stderr=f)
            cls.webdriver = subprocess.Popen(
                ['webdriver-manager', 'start'], stdout=f, stderr=f)

    @classmethod
    def tearDownClass(cls):
        cls.webdriver.kill()
        super(ProtractorTestCaseMixin, cls).tearDownClass()

    def test_run(self):
        protractor_command = 'protractor {}'.format(self.protractor_conf)
        if self.specs:
            protractor_command += ' --specs {}'.format(','.join(self.specs))
        if self.suite:
            protractor_command += ' --suite {}'.format(self.suite)
        protractor_command += ' --params.live_server_url={}'.format(self.live_server_url)
        return_code = subprocess.call(protractor_command.split())
        self.assertEqual(return_code, 0)

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

import os
import subprocess

class ProtractorTestCaseMixin(object):
    protractor_conf = 'protractor.conf.js'
    suite = None
    specs = None

    @classmethod
    def setUpClass(cls):
        super(ProtractorTestCaseMixin, cls).setUpClass()
        with open(os.devnull, 'wb') as f:
            subprocess.call(['webdriver-manager', 'update'], stdout=f, stderr=f)
            cls.webdriver = subprocess.Popen(
                ['webdriver-manager', 'start'], stdout=f, stderr=f)

    @classmethod
    def tearDownClass(cls):
        cls.webdriver.kill()
        super(ProtractorTestCaseMixin, cls).tearDownClass()

    def get_protractor_params(self):
        """A hook for adding params that protractor will receive."""
        return {
            'live_server_url': self.live_server_url
        }

    def test_run(self):
        protractor_command = 'protractor {}'.format(self.protractor_conf)
        if self.specs:
            protractor_command += ' --specs {}'.format(','.join(self.specs))
        if self.suite:
            protractor_command += ' --suite {}'.format(self.suite)
        for key, value in self.get_protractor_params().iteritems():
            protractor_command += ' --params.{key}={value}'.format(
                key=key, value=value
            )
        return_code = subprocess.call(protractor_command.split())
        self.assertEqual(return_code, 0)

Add hook for protactor params

Add hook for protactor params


import unittest
from nose.plugins.attrib import attr

from openquake.qa_tests_data.classical_risk import (
    case_1, case_2, case_3, case_4)
from openquake.calculators.tests import CalculatorTestCase

class ClassicalRiskTestCase(CalculatorTestCase):

    @attr('qa', 'risk', 'classical_risk')
    def test_case_1(self):
        raise unittest.SkipTest

    @attr('qa', 'risk', 'classical_risk')
    def test_case_2(self):
        raise unittest.SkipTest

    @attr('qa', 'risk', 'classical_risk')
    def test_case_3(self):
        out = self.run_calc(case_3.__file__, 'job_haz.ini,job_risk.ini',
                            exports='csv')
        [fname] = out['avg_losses-rlzs', 'csv']
        self.assertEqualFiles('expected/rlz-000-avg_loss.csv', fname)

    @attr('qa', 'risk', 'classical_risk')
    def test_case_4(self):
        out = self.run_calc(case_4.__file__, 'job_haz.ini,job_risk.ini',
                            exports='csv')
        fnames = out['avg_losses-rlzs', 'csv']
        self.assertEqualFiles('expected/rlz-000-avg_loss.csv', fnames[0])
        self.assertEqualFiles('expected/rlz-001-avg_loss.csv', fnames[1])

import unittest
from nose.plugins.attrib import attr

from openquake.qa_tests_data.classical_risk import (
    case_1, case_2, case_3, case_4)
from openquake.calculators.tests import CalculatorTestCase

class ClassicalRiskTestCase(CalculatorTestCase):

    @attr('qa', 'risk', 'classical_risk')
    def test_case_1(self):
        out = self.run_calc(case_1.__file__, 'job_risk.ini', exports='xml')

    @attr('qa', 'risk', 'classical_risk')
    def test_case_2(self):
        raise unittest.SkipTest

    @attr('qa', 'risk', 'classical_risk')
    def test_case_3(self):
        out = self.run_calc(case_3.__file__, 'job_haz.ini,job_risk.ini',
                            exports='csv')
        [fname] = out['avg_losses-rlzs', 'csv']
        self.assertEqualFiles('expected/rlz-000-avg_loss.csv', fname)

    @attr('qa', 'risk', 'classical_risk')
    def test_case_4(self):
        out = self.run_calc(case_4.__file__, 'job_haz.ini,job_risk.ini',
                            exports='csv')
        fnames = out['avg_losses-rlzs', 'csv']
        self.assertEqualFiles('expected/rlz-000-avg_loss.csv', fnames[0])
        self.assertEqualFiles('expected/rlz-001-avg_loss.csv', fnames[1])

Work on classical_risk test_case_1 and test_case_2


import unittest, boto, os
from bucketeer import commit

class BuckeeterTest(unittest.TestCase):

  # Constants - TODO move to config file
  global existing_bucket, test_dir, test_file
  existing_bucket = 'bucket.exists'
  test_dir = 'bucketeer_test_dir'
  test_file = 'bucketeer_test_file'

  def setUp(self):
    connection = boto.connect_s3()

    # Create a bucket to test on existing bucket
    bucket = connection.create_bucket(existing_bucket)

    # Create directory to house test files
    os.makedirs(test_dir)

    # Create test file
    open(test_dir + '/' + test_file, 'w').close()

    return

  def tearDown(self):
    connection = boto.connect_s3()

    # Remove all files uploaded to s3
    bucket = connection.get_bucket(existing_bucket)
    for s3_file in bucket.list():
      bucket.delete_key(s3_file.key)

    # Remove bucket created to test on existing bucket
    bucket = connection.delete_bucket(existing_bucket)

    # Remove test file
    os.remove(test_dir + '/' + test_file)

    # Remove directory created to house test files
    os.rmdir(test_dir)

    return

  def testMain(self):
    self.assertTrue(commit)

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

import unittest, boto, os
from bucketeer import commit

class BuckeeterTest(unittest.TestCase):

  # Constants - TODO move to config file
  global existing_bucket, test_dir, test_file
  existing_bucket = 'bucket.exists'
  test_dir = 'bucketeer_test_dir'
  test_file = 'bucketeer_test_file'

  def setUp(self):
    connection = boto.connect_s3()

    # Create a bucket to test on existing bucket
    bucket = connection.create_bucket(existing_bucket)

    # Create directory to house test files
    os.makedirs(test_dir)

    # Create test file
    open(test_dir + '/' + test_file, 'w').close()

    return

  def tearDown(self):
    connection = boto.connect_s3()

    # Remove all files uploaded to s3
    bucket = connection.get_bucket(existing_bucket)
    for s3_file in bucket.list():
      bucket.delete_key(s3_file.key)

    # Remove bucket created to test on existing bucket
    bucket = connection.delete_bucket(existing_bucket)

    # Remove test file
    os.remove(test_dir + '/' + test_file)

    # Remove directory created to house test files
    os.rmdir(test_dir)

    return

  def testMain(self):
    self.assertTrue(commit)

  def testNewFileUploadExistingBucket(self):
    result = commit.commit_to_s3(existing_bucket, test_dir)
    self.assertTrue(result)

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

Add test for new file upload to existing bucket


from django.conf import settings
from django.contrib.auth.models import User
from django.db import models

class UserMetadata(models.Model):
    user = models.ForeignKey(
        User, related_name="metadata", unique=True, on_delete=models.CASCADE
    )
    tos_version = models.IntegerField(default=0)
    tos_accepted = models.DateTimeField(default=None, null=True,)
    privacy_policy_version = models.IntegerField(default=0)
    privacy_policy_accepted = models.DateTimeField(default=None, null=True,)
    colorscheme = models.CharField(default="dark-yellow-green.theme", max_length=255,)

    @property
    def tos_up_to_date(self):
        return self.tos_version == settings.TOS_VERSION

    @property
    def privacy_policy_up_to_date(self):
        return self.privacy_policy_version == settings.PRIVACY_POLICY_VERSION

    @classmethod
    def get_for_user(cls, user):
        meta, created = UserMetadata.objects.get_or_create(user=user)
        return meta

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        from . import TaskStore

        if self.tos_up_to_date and self.privacy_policy_up_to_date:
            store = TaskStore.get_for_user(self.user)
            store.taskd_account.resume()

    def __str__(self):
        return self.user.username

    class Meta:
        app_label = "taskmanager"

from django.conf import settings
from django.contrib.auth.models import User
from django.db import models

class UserMetadata(models.Model):
    user = models.OneToOneField(
        User, related_name="metadata", on_delete=models.CASCADE
    )
    tos_version = models.IntegerField(default=0)
    tos_accepted = models.DateTimeField(default=None, null=True,)
    privacy_policy_version = models.IntegerField(default=0)
    privacy_policy_accepted = models.DateTimeField(default=None, null=True,)
    colorscheme = models.CharField(default="dark-yellow-green.theme", max_length=255,)

    @property
    def tos_up_to_date(self):
        return self.tos_version == settings.TOS_VERSION

    @property
    def privacy_policy_up_to_date(self):
        return self.privacy_policy_version == settings.PRIVACY_POLICY_VERSION

    @classmethod
    def get_for_user(cls, user):
        meta, created = UserMetadata.objects.get_or_create(user=user)
        return meta

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        from . import TaskStore

        if self.tos_up_to_date and self.privacy_policy_up_to_date:
            store = TaskStore.get_for_user(self.user)
            store.taskd_account.resume()

    def __str__(self):
        return self.user.username

    class Meta:
        app_label = "taskmanager"

Change mapping to avoid warning

Change mapping to avoid warning


#!/usr/bin/python3
#
# Define containerized environment for running Diosix on Qemu
#
# On Google Cloud Run: Creates HTTP server on port 8080
# or whatever was specified using the PORT system variable.
# Outputs via the HTTP port. This requires K_SERVICE to be set.
#
# On all other environments: Log to stdout
#
# syntax: entrypoint.py <command>
#
# Author: Chris Williams <diodesign@tuta.io>
#

import os
import sys

global command_result

from flask import Flask

if __name__ == "__main__":
    if (os.environ.get('K_SERVICE')) != '':
        print('Running HTTP service for Google Cloud')
        # app = Flask(__name__)
        # @app.route('/')
        # def ContainerService():
        #   return 'Container built. Use docker images and docker run in the Google Cloud shell to run this container.\n'
        # app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))
    else:
        print('Running locally')
        # stream = os.popen('. $HOME/.cargo/env && cd /build/diosix && {}'.format(' '.join(sys.argv[1:])))
        # output = stream.read()
        # output

#!/usr/bin/python3
#
# Define containerized environment for running Diosix on Qemu
#
# On Google Cloud Run: Creates HTTP server on port 8080
# or whatever was specified using the PORT system variable.
# Outputs via the HTTP port. This requires K_SERVICE to be set.
#
# On all other environments: Log to stdout
#
# syntax: entrypoint.py <command>
#
# Author: Chris Williams <diodesign@tuta.io>
#

import os
import sys

global command_result

from flask import Flask

if __name__ == "__main__":
    print('{} {} {}', os.environ.get('K_SERVICE'), os.environ.get('K_REVISION'), os.environ.get('K_CONFIGURATION'))

    if (os.environ.get('K_SERVICE')) != '':
        print('Running HTTP service for Google Cloud')
        # app = Flask(__name__)
        # @app.route('/')
        # def ContainerService():
        #   return 'Container built. Use docker images and docker run in the Google Cloud shell to run this container.\n'
        # app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))
    else:
        print('Running locally')
        # stream = os.popen('. $HOME/.cargo/env && cd /build/diosix && {}'.format(' '.join(sys.argv[1:])))
        # output = stream.read()
        # output

Debug Google Cloud Run support

Debug Google Cloud Run support


# Non-parameterized functions
from accuracy   import accuracy
from basic_math import exp, log
from concat     import concat
from copy       import copy
from dropout    import dropout
from identity   import identity
from leaky_relu import leaky_relu
from lstm       import lstm
from mean_squared_error import mean_squared_error
from pooling_2d import average_pooling_2d, max_pooling_2d
from relu       import relu
from sigmoid    import sigmoid
from softmax    import softmax
from softmax_cross_entropy import softmax_cross_entropy
from sum        import sum
from tanh       import tanh

# Parameterized layers
from batch_normalization import BatchNormalization
from convolution_2d import Convolution2D
from embed_id       import EmbedID
from inception      import Inception
from linear         import Linear
from parameter      import Parameter
from prelu          import PReLU

"""Collection of :class:`~chainer.Function` implementations."""

# Parameterized function classes
from batch_normalization import BatchNormalization
from convolution_2d      import Convolution2D
from embed_id            import EmbedID
from inception           import Inception
from linear              import Linear
from parameter           import Parameter
from prelu               import PReLU

# Array manipulation functions
from concat   import concat
from copy     import copy
from dropout  import dropout
from identity import identity

# Activation functions
from basic_math import exp, log
from leaky_relu import leaky_relu
from lstm       import lstm
from relu       import relu
from sigmoid    import sigmoid
from softmax    import softmax
from tanh       import tanh

# Pooling functions
from pooling_2d import average_pooling_2d, max_pooling_2d

# Loss, evaluation and aggregation
from accuracy              import accuracy
from mean_squared_error    import mean_squared_error
from softmax_cross_entropy import softmax_cross_entropy
from sum                   import sum

Sort function imports to fit with documentation order


import argparse
import os
import sys

# Force this to happen before loading django
try:
    os.environ["DJANGO_SETTINGS_MODULE"] = "testtinymce.settings"
    test_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.insert(0, test_dir)
except ImportError:
    pass
else:
    import django
    from django.conf import settings
    from django.test.utils import get_runner

def runtests(verbosity=1, failfast=False):
    django.setup()
    TestRunner = get_runner(settings)
    test_runner = TestRunner(interactive=True, verbosity=verbosity, failfast=failfast)
    failures = test_runner.run_tests(["tinymce"])
    sys.exit(bool(failures))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Run the django-tinymce test suite.")
    parser.add_argument(
        "-v",
        "--verbosity",
        default=1,
        type=int,
        choices=[0, 1, 2, 3],
        help="Verbosity level; 0=minimal output, 1=normal output, 2=all output",
    )
    parser.add_argument(
        "--failfast",
        action="store_true",
        help="Stop running the test suite after first failed test.",
    )
    options = parser.parse_args()
    runtests(verbosity=options.verbosity, failfast=options.failfast)

import argparse
import os
import sys

# Force this to happen before loading django
try:
    os.environ["DJANGO_SETTINGS_MODULE"] = "testtinymce.settings"
    test_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.insert(0, test_dir)
except ImportError:
    pass
else:
    import django
    from django.conf import settings
    from django.test.utils import get_runner

def runtests(modules=["tinymce"], verbosity=1, failfast=False):
    django.setup()
    TestRunner = get_runner(settings)
    test_runner = TestRunner(interactive=True, verbosity=verbosity, failfast=failfast)
    failures = test_runner.run_tests(modules)
    sys.exit(bool(failures))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Run the django-tinymce test suite.")
    parser.add_argument(
        "modules",
        nargs="*",
        metavar="module",
        help='Optional path(s) to test modules; e.g. "tinymce" or '
        '"tinymce.tests.test_widgets".',
    )
    parser.add_argument(
        "-v",
        "--verbosity",
        default=1,
        type=int,
        choices=[0, 1, 2, 3],
        help="Verbosity level; 0=minimal output, 1=normal output, 2=all output",
    )
    parser.add_argument(
        "--failfast",
        action="store_true",
        help="Stop running the test suite after first failed test.",
    )
    options = parser.parse_args()
    runtests(modules=options.modules, verbosity=options.verbosity, failfast=options.failfast)

Add ability to run subset of tests

Add ability to run subset of tests


#!/usr/bin/env python
import sys

from django.conf import settings

if not settings.configured:
    settings.configure(
        DATABASES={
            'default': {
                'ENGINE': 'django.db.backends.sqlite3',
                'NAME': ':memory:',
            }
        },
        INSTALLED_APPS=(
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'td_biblio',
        ),
        ROOT_URLCONF='td_biblio.urls',
        SITE_ID=1,
        SECRET_KEY='this-is-just-for-tests-so-not-that-secret',
    )

from django.test.utils import get_runner

def runtests():
    TestRunner = get_runner(settings)
    test_runner = TestRunner(verbosity=1, interactive=True, failfast=False)
    failures = test_runner.run_tests(['td_biblio', ])
    sys.exit(failures)

if __name__ == '__main__':
    runtests()

#!/usr/bin/env python
import sys

from django.conf import settings

if not settings.configured:
    settings.configure(
        DATABASES={
            'default': {
                'ENGINE': 'django.db.backends.sqlite3',
                'NAME': ':memory:',
            }
        },
        INSTALLED_APPS=(
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'td_biblio',
        ),
        ROOT_URLCONF='td_biblio.urls',
        SITE_ID=1,
        SECRET_KEY='this-is-just-for-tests-so-not-that-secret',
    )

from django.test.utils import get_runner

def runtests():
    TestRunner = get_runner(settings)
    test_runner = TestRunner(verbosity=1, interactive=True, failfast=False)
    failures = test_runner.run_tests([
        'td_biblio.tests.test_commands',
        'td_biblio.tests.test_factories',
        'td_biblio.tests.test_models',
        'td_biblio.tests.test_views',
    ])
    sys.exit(failures)

if __name__ == '__main__':
    runtests()

Add more flexibity to run tests independantly


#!/usr/bin/env python

import os
import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "farnsworth.settings")
this_dir = os.path.abspath(os.path.dirname(__file__))
if this_dir not in sys.path:
	sys.path.insert(0, this_dir)

from django.test.utils import get_runner
from django.conf import settings

def runtests():
	TestRunner = get_runner(settings)
	test_runner = TestRunner(verbosity=1, interactive=True, failfast=False)
	failures = test_runner.run_tests(["base", "threads", "events", "managers"])
	sys.exit(bool(failures))

if __name__ == "__main__":
	runtests()

#!/usr/bin/env python

import os
import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "farnsworth.settings")
this_dir = os.path.abspath(os.path.dirname(__file__))
if this_dir not in sys.path:
	sys.path.insert(0, this_dir)

from django.test.utils import get_runner
from django.conf import settings

def runtests():
	TestRunner = get_runner(settings)
	test_runner = TestRunner(verbosity=1, interactive=True, failfast=False)
	failures = test_runner.run_tests([
        "base",
        "threads",
        "events",
        "managers",
        "workshift",
        ])
	sys.exit(bool(failures))

if __name__ == "__main__":
	runtests()

Add workshift to the list of tests

Add workshift to the list of tests


#!/usr/bin/env python

# Look for "szone_error" (Tiger), "malloc_error_break" (Leopard), "MallocHelp" (?)
# which are signs of malloc being unhappy (double free, out-of-memory, etc).

def amiss(logPrefix):
    foundSomething = False

    currentFile = file(logPrefix + "-err", "r")

    pline = ""
    ppline = ""

    for line in currentFile:
        line = line.strip("\x07").rstrip("\n")

        if (-1 != line.find("szone_error")
         or -1 != line.find("malloc_error_break")
         or -1 != line.find("MallocHelp")):
            if (-1 != pline.find("can't allocate region")):
                print ""
                print ppline
                print pline
                print line
                foundSomething = True
                break # Don't flood the log with repeated malloc failures

        ppline = pline
        pline = line

    currentFile.close()

    return foundSomething

#!/usr/bin/env python

# Look for "szone_error" (Tiger), "malloc_error_break" (Leopard), "MallocHelp" (?)
# which are signs of malloc being unhappy (double free, out-of-memory, etc).

def amiss(logPrefix):
    foundSomething = False

    currentFile = file(logPrefix + "-err", "r")

    pline = ""
    ppline = ""

    for line in currentFile:
        line = line.strip("\x07").rstrip("\n")

        if (-1 != line.find("szone_error")
         or -1 != line.find("malloc_error_break")
         or -1 != line.find("MallocHelp")):
            if (-1 == pline.find("can't allocate region")):
                print ""
                print ppline
                print pline
                print line
                foundSomething = True
                break # Don't flood the log with repeated malloc failures

        ppline = pline
        pline = line

    currentFile.close()

    return foundSomething

Fix reversed condition for ignoring "can't allocate region" errors


# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2016-12-09 11:56
from __future__ import unicode_literals

from django.db import migrations
from django.core.management import call_command

def create_revisions(apps, schema_editor):
    call_command('createinitialrevisions')

class Migration(migrations.Migration):

    dependencies = [
        ('games', '0015_installer_draft'),
    ]

    operations = [
        migrations.RunPython(create_revisions)
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2016-12-09 11:56
from __future__ import unicode_literals

from django.db import migrations
from django.core.management import call_command

def create_revisions(apps, schema_editor):
    call_command('createinitialrevisions')

class Migration(migrations.Migration):

    dependencies = [
        ('games', '0015_installer_draft'),
        ('reversion', '0001_squashed_0004_auto_20160611_1202'),
    ]

    operations = [
        migrations.RunPython(create_revisions)
    ]

Add dependency to reversion data migration


#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

import arrayfire as af

def display_func(verbose):
    if (verbose):
        return af.display
    else:
        def eval_func(foo):
            res = foo
        return eval_func

def print_func(verbose):
    def print_func_impl(*args):
        if (verbose):
            print(args)
        else:
            res = [args]
    return print_func_impl

class _simple_test_dict(dict):

    def __init__(self):
        self.print_str = "Simple %16s: %s"
        super(_simple_test_dict, self).__init__()

    def run(self, name_list=None, verbose=False):
        test_list = name_list if name_list is not None else self.keys()
        for key in test_list:

            try:
                test = self[key]
            except:
                print(self.print_str % (key, "NOTFOUND"))
                continue

            try:
                test(verbose)
                print(self.print_str % (key, "PASSED"))
            except:
                print(self.print_str % (key, "FAILED"))

tests = _simple_test_dict()

#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

import traceback
import logging
import arrayfire as af

def display_func(verbose):
    if (verbose):
        return af.display
    else:
        def eval_func(foo):
            res = foo
        return eval_func

def print_func(verbose):
    def print_func_impl(*args):
        if (verbose):
            print(args)
        else:
            res = [args]
    return print_func_impl

class _simple_test_dict(dict):

    def __init__(self):
        self.print_str = "Simple %16s: %s"
        super(_simple_test_dict, self).__init__()

    def run(self, name_list=None, verbose=False):
        test_list = name_list if name_list is not None else self.keys()
        for key in test_list:
            try:
                test = self[key]
            except:
                print(self.print_str % (key, "NOTFOUND"))
                continue

            try:
                test(verbose)
                print(self.print_str % (key, "PASSED"))
            except Exception as e:
                print(self.print_str % (key, "FAILED"))
                if (verbose):
                    logging.error(traceback.format_exc())

tests = _simple_test_dict()

Add proper logging to tests when in verbose mode


#!/usr/bin/env python
# -*- coding: utf8 -*-

# My imports
import argparse
import gzip

def _parser():
    parser = argparse.ArgumentParser(description='Prepare the data downloaded '
                                     'from VALD.')
    parser.add_argument('input', help='input compressed file')
    parser.add_argument('-o', '--output',
                        help='Optional output',
                        default=False)
    return parser.parse_args()

def main(input, output=False):
    if not isinstance(input, str):
        raise TypeError('Input must be a str. A %s was parsed' % type(input))
    if not isinstance(output, str) and output:
        raise TypeError('Output must be a str. A %s was parsed' % type(output))

    # TODO: Check if the input exists

    fname = input.rpartition('.')[0]
    if not output:
        output = '%s.dat' % fname
    oref = '%s.ref' % fname

    fout = ''
    fref = ''
    with gzip.open(input, 'r') as lines:
        for i, line in enumerate(lines):
            if i < 2:
                fout += '# %s' % line.replace("'", '')
            else:
                fout += line.replace("'", '')
            if 'References' in line:
                break

    with open(output, 'w') as fo:
        fo.write(fout)

if __name__ == '__main__':
    args = _parser()
    input, output = args.input, args.output
    main(input, output)

#!/usr/bin/env python
# -*- coding: utf8 -*-

# My imports
import argparse
import gzip
import os

def _parser():
    parser = argparse.ArgumentParser(description='Prepare the data downloaded '
                                     'from VALD.')
    parser.add_argument('input', help='input compressed file', type=str)
    parser.add_argument('-o', '--output',
                        help='Optional output',
                        default=False, type=str)
    return parser.parse_args()

def main(input, output=False):

    if not os.path.isfile(input):
        raise IOError('File: %s does not exists' % input)

    fname = input.rpartition('.')[0]
    if not output:
        output = '%s.dat' % fname
    oref = '%s.ref' % fname

    fout = ''
    fref = ''
    with gzip.open(input, 'r') as lines:
        for i, line in enumerate(lines):
            if i < 2:
                fout += '# %s' % line.replace("'", '')
            else:
                fout += line.replace("'", '')
            if 'References' in line:
                break

    with open(output, 'w') as fo:
        fo.write(fout)

if __name__ == '__main__':
    args = _parser()
    input, output = args.input, args.output
    main(input, output)

Check if the file exists before doing anything else.


import os
import re

from django.conf import settings
from django.core.management import call_command, BaseCommand

class Command(BaseCommand):
    """Load the Django project fixtures and all version fixtures"""

    def handle(self, **options):
        fixtures_dir = os.path.join(settings.DIRNAME, 'cbv', 'fixtures')
        self.stdout.write('Loading project.json')
        call_command('loaddata', 'cbv/fixtures/project.json')
        version_fixtures = [re.match(r'((?:\d+\.){2,3}json)', filename) for filename in os.listdir(fixtures_dir)]
        for match in version_fixtures:
            try:
                fixture = match.group()
            except AttributeError:
                continue
            self.stdout.write('Loading {}'.format(fixture))
            call_command('loaddata', 'cbv/fixtures/{}'.format(fixture))

import glob
import os

from django.core.management import call_command, BaseCommand

class Command(BaseCommand):
    """Load the Django project fixtures and all version fixtures"""

    def handle(self, **options):
        self.stdout.write('Loading project.json')
        call_command('loaddata', 'cbv/fixtures/project.json')
        version_fixtures = glob.glob(os.path.join('cbv', 'fixtures', '*.*.*json'))
        for fixture in version_fixtures:
            self.stdout.write('Loading {}'.format(fixture))
            call_command('loaddata', fixture)

Use glob for finding version fixtures

Use glob for finding version fixtures

Thanks @ghickman!


"""
TornWAMP user-configurable structures.
"""
from tornwamp.processors import GoodbyeProcessor, HelloProcessor, pubsub, rpc
from tornwamp.messages import Code

processors = {
    Code.HELLO: HelloProcessor,
    Code.GOODBYE: GoodbyeProcessor,
    Code.SUBSCRIBE: pubsub.SubscribeProcessor,
    Code.CALL: rpc.CallProcessor
}
#    2: 'welcome',
#    3: 'abort',
#    4: 'challenge',
#    5: 'authenticate',
#    7: 'heartbeat',
#    8: 'error',
#    16: 'publish',
#    17: 'published',
#    32: 'subscribe',
#    33: 'subscribed',
#    34: 'unsubscribe',
#    35: 'unsubscribed',
#    36: 'event',
#    49: 'cancel',
#    50: 'result',
#    64: 'register',
#    65: 'registered',
#    66: 'unregister',
#    67: 'unregistered',
#    68: 'invocation',
#    69: 'interrupt',
#    70: 'yield'

"""
TornWAMP user-configurable structures.
"""
from tornwamp.processors import GoodbyeProcessor, HelloProcessor, pubsub, rpc
from tornwamp.messages import Code

processors = {
    Code.HELLO: HelloProcessor,
    Code.GOODBYE: GoodbyeProcessor,
    Code.SUBSCRIBE: pubsub.SubscribeProcessor,
    Code.CALL: rpc.CallProcessor,
    Code.PUBLISH: pubsub.PublishProcessor
}
#    2: 'welcome',
#    3: 'abort',
#    4: 'challenge',
#    5: 'authenticate',
#    7: 'heartbeat',
#    8: 'error',
#    16: 'publish',
#    17: 'published',
#    32: 'subscribe',
#    33: 'subscribed',
#    34: 'unsubscribe',
#    35: 'unsubscribed',
#    36: 'event',
#    49: 'cancel',
#    50: 'result',
#    64: 'register',
#    65: 'registered',
#    66: 'unregister',
#    67: 'unregistered',
#    68: 'invocation',
#    69: 'interrupt',
#    70: 'yield'

Add PublishProcessor to processors' list


from __future__ import print_function
import sys
import os
import time
import socket
import argparse
import subprocess

parser = argparse.ArgumentParser()
parser.add_argument('--launch-adapter')
parser.add_argument('--lldb')
parser.add_argument('--wait-port')

args = parser.parse_args()

if args.launch_adapter:
    lldb = args.lldb or 'lldb'
    cmd = [lldb, '-b',
        '-O', 'command script import %s' % args.launch_adapter,
        '-O', 'script import ptvsd; ptvsd.enable_attach(address=("0.0.0.0", 3000)); ptvsd.wait_for_attach(); adapter.run_tcp_session(4711)',
    ]
    print('Launching', cmd)
    subprocess.Popen(cmd, preexec_fn=lambda: os.setsid())

if args.wait_port:
    port = int(args.wait_port)
    print('Waiting for port %d' % port)

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    while True:
        result = sock.connect_ex(('127.0.0.1', port))
        if result == 0:
            break
        time.sleep(0.5)

    print('Port opened')
    sock.shutdown(socket.SHUT_WR)
    sock.close()

from __future__ import print_function
import sys
import os
import time
import socket
import argparse
import subprocess

parser = argparse.ArgumentParser()
parser.add_argument('--launch-adapter')
parser.add_argument('--lldb')
parser.add_argument('--wait-port')

args = parser.parse_args()

if args.launch_adapter:
    lldb = args.lldb or 'lldb'
    cmd = [lldb, '-b',
        '-O', 'command script import %s' % args.launch_adapter,
        '-O', 'script sys.argv=["lldb"]; import ptvsd; ptvsd.enable_attach(address=("0.0.0.0", 3000)); ptvsd.wait_for_attach()',
        '-O', 'script adapter.run_tcp_session(4711)',
    ]
    print('Launching', cmd)
    if sys.platform != 'win32':
        subprocess.Popen(cmd, preexec_fn=lambda: os.setsid())
    else:
        subprocess.Popen(cmd, creationflags=subprocess.CREATE_NEW_CONSOLE)

if args.wait_port:
    port = int(args.wait_port)
    print('Waiting for port %d' % port)

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    while True:
        result = sock.connect_ex(('127.0.0.1', port))
        if result == 0:
            break
        time.sleep(0.5)

    print('Port opened')
    sock.shutdown(socket.SHUT_WR)
    sock.close()

Fix python debugging on Windows.

Fix python debugging on Windows.