from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'fbxnano.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url('^status$', TemplateView.as_view(template_name='api/status.html'), name='status'),
)

from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView

from .views import StatusView

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'fbxnano.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url('^status$', StatusView.as_view(), name='status'),
)

Switch from generic TemplateView to new StatusView


from .factory import FactoryTask
from .report import ReportTask
from .sleep import SleepTask

# Generic schema to reference a task ID
TASKID_SCHEMA = {
    'type': 'string',
    'description': 'task_id'
}

from .factory import FactoryTask
from .report import ReportTask
from .sleep import SleepTask

# Generic schema to reference a task ID
TASKID_SCHEMA = {
    'type': 'string',
    'description': 'task_id',
    'maxLength': 128
}

Add maxlength to taskid schema

Add maxlength to taskid schema


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

# version string following pep-0396 and pep-0386
__version__ = '1.2.9.dev'  # pragma: nocover

default_app_config = 'filer.apps.FilerConfig'

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

# version string following pep-0396 and pep-0386
__version__ = '1.3.0.dev'  # pragma: nocover

default_app_config = 'filer.apps.FilerConfig'

Bump develop version to 1.3.0.dev

Bump develop version to 1.3.0.dev


#!/usr/bin/env python3

import sys
import json
import os
import jinja2

def render(tpl_path):
    path, filename = os.path.split(tpl_path)
    return jinja2.Environment(
        loader=jinja2.FileSystemLoader(path or './')
    ).get_template(filename).render()

n = len(sys.argv)
if ( n != 3 ):
    sys.exit("The template file name and output file name are expected as arguments")
# set template file name, output file name
driver_wrapper_template_filename = sys.argv[1]
driver_wrapper_output_filename = sys.argv[2]

# render the template
result = render(driver_wrapper_template_filename)

# write output to file
outFile = open(driver_wrapper_output_filename,"w")
outFile.write(result)
outFile.close()

#!/usr/bin/env python3

import sys
import json
import os
import jinja2

def render(tpl_path):
    path, filename = os.path.split(tpl_path)
    return jinja2.Environment(
        loader=jinja2.FileSystemLoader(path or './'),
        keep_trailing_newline=True,
    ).get_template(filename).render()

n = len(sys.argv)
if ( n != 3 ):
    sys.exit("The template file name and output file name are expected as arguments")
# set template file name, output file name
driver_wrapper_template_filename = sys.argv[1]
driver_wrapper_output_filename = sys.argv[2]

# render the template
result = render(driver_wrapper_template_filename)

# write output to file
outFile = open(driver_wrapper_output_filename,"w")
outFile.write(result)
outFile.close()

Fix trailing newline getting dropped

Fix trailing newline getting dropped

Signed-off-by: Gilles Peskine <f805f64266d288fc5467baa7be6cd0ff366f477b@arm.com>


class TestVersion:
    def test_version_is_consistent(self):
        from .. import VERSION

        with open('README.rst') as f:
            readme = f.read().split('\n')
            version_list = readme[3].split(':')[2].strip().split('.')
            version_list = [int(i) for i in version_list]
            readme_version = tuple(version_list)

        assert VERSION == readme_version

import re

def get_field(doc: str, name: str):
    match = re.search(':{}: (.*)$'.format(name), doc, re.IGNORECASE | re.MULTILINE)
    if match:
        return match.group(1).strip()

class TestVersion:
    def test_version_is_consistent(self):
        from .. import VERSION

        with open('README.rst') as f:
            readme = f.read()

            version = get_field(readme, 'version')
            version = version.split('.')
            version = [int(i) for i in version]
            version = tuple(version)

        assert VERSION == version

Clean up test for version number

Clean up test for version number

A new function is implemented to cleanly extract the version field from the
README.rst field list.


import json

from website.util.permissions import reduce_permissions

from admin.users.serializers import serialize_simple_node

def serialize_node(node):
    embargo = node.embargo
    if embargo is not None:
        embargo = node.embargo.end_date

    return {
        'id': node._id,
        'title': node.title,
        'public': node.is_public,
        'parent': node.parent_id,
        'root': node.root._id,
        'is_registration': node.is_registration,
        'date_created': node.date_created,
        'withdrawn': node.is_retracted,
        'embargo': embargo,
        'contributors': [serialize_simple_user_and_node_permissions(node, user) for user in node.contributors],
        'children': map(serialize_simple_node, node.nodes),
        'deleted': node.is_deleted,
        'pending_registration': node.is_pending_registration,
        'creator': node.creator._id,
        'spam_status': node.spam_status,
        'spam_pro_tip': node.spam_pro_tip,
        'spam_data': json.dumps(node.spam_data, indent=4),
        'is_public': node.is_public,
    }

def serialize_simple_user_and_node_permissions(node, user):
    return {
        'id': user._id,
        'name': user.fullname,
        'permission': reduce_permissions(node.get_permissions(user))
    }

import json

from website.util.permissions import reduce_permissions

from admin.users.serializers import serialize_simple_node

def serialize_node(node):
    embargo = node.embargo
    if embargo is not None:
        embargo = node.embargo.end_date

    return {
        'id': node._id,
        'title': node.title,
        'public': node.is_public,
        'parent': node.parent_id,
        'root': node.root._id,
        'is_registration': node.is_registration,
        'date_created': node.date_created,
        'withdrawn': node.is_retracted,
        'embargo': embargo,
        'contributors': [serialize_simple_user_and_node_permissions(node, user) for user in node.contributors],
        'children': map(serialize_simple_node, node.nodes),
        'deleted': node.is_deleted,
        'pending_registration': node.is_pending_registration,
        'registered_date': node.registered_date,
        'creator': node.creator._id,
        'spam_status': node.spam_status,
        'spam_pro_tip': node.spam_pro_tip,
        'spam_data': json.dumps(node.spam_data, indent=4),
        'is_public': node.is_public,
    }

def serialize_simple_user_and_node_permissions(node, user):
    return {
        'id': user._id,
        'name': user.fullname,
        'permission': reduce_permissions(node.get_permissions(user))
    }

Add date_registered to node serializer

Add date_registered to node serializer

[#OSF-7230]


# coding: utf-8
from __future__ import unicode_literals, division, absolute_import, print_function

import os

from flake8.engine import get_style_guide

cur_dir = os.path.dirname(__file__)
config_file = os.path.join(cur_dir, '..', 'tox.ini')

def run():
    """
    Runs flake8 lint

    :return:
        A bool - if flake8 did not find any errors
    """

    print('Running flake8')

    flake8_style = get_style_guide(config_file=config_file)

    paths = []
    for root, _, filenames in os.walk('asn1crypto'):
        for filename in filenames:
            if not filename.endswith('.py'):
                continue
            paths.append(os.path.join(root, filename))
    report = flake8_style.check_files(paths)
    success = report.total_errors == 0
    if success:
        print('OK')
    return success

# coding: utf-8
from __future__ import unicode_literals, division, absolute_import, print_function

import os

import flake8
if flake8.__version_info__ < (3,):
    from flake8.engine import get_style_guide
else:
    from flake8.api.legacy import get_style_guide

cur_dir = os.path.dirname(__file__)
config_file = os.path.join(cur_dir, '..', 'tox.ini')

def run():
    """
    Runs flake8 lint

    :return:
        A bool - if flake8 did not find any errors
    """

    print('Running flake8')

    flake8_style = get_style_guide(config_file=config_file)

    paths = []
    for root, _, filenames in os.walk('asn1crypto'):
        for filename in filenames:
            if not filename.endswith('.py'):
                continue
            paths.append(os.path.join(root, filename))
    report = flake8_style.check_files(paths)
    success = report.total_errors == 0
    if success:
        print('OK')
    return success

Add support for flake8 3.0

Add support for flake8 3.0


# Supported 16 color values:
#   'h0' (color number 0) through 'h15' (color number 15)
#    or
#   'default' (use the terminal's default foreground),
#   'black', 'dark red', 'dark green', 'brown', 'dark blue',
#   'dark magenta', 'dark cyan', 'light gray', 'dark gray',
#   'light red', 'light green', 'yellow', 'light blue',
#   'light magenta', 'light cyan', 'white'
#
# Supported 256 color values:
#   'h0' (color number 0) through 'h255' (color number 255)
#
# 256 color chart: http://en.wikipedia.org/wiki/File:Xterm_color_chart.png
#
# "setting_name": (foreground_color, background_color),

palette.update({
    "source": (add_setting("black", "underline"), "dark green"),
    "comment": ("h250", "default")
    })

# Supported 16 color values:
#   'h0' (color number 0) through 'h15' (color number 15)
#    or
#   'default' (use the terminal's default foreground),
#   'black', 'dark red', 'dark green', 'brown', 'dark blue',
#   'dark magenta', 'dark cyan', 'light gray', 'dark gray',
#   'light red', 'light green', 'yellow', 'light blue',
#   'light magenta', 'light cyan', 'white'
#
# Supported 256 color values:
#   'h0' (color number 0) through 'h255' (color number 255)
#
# 256 color chart: http://en.wikipedia.org/wiki/File:Xterm_color_chart.png
#
# "setting_name": (foreground_color, background_color),

# See this URL to see what keys there are:
# https://github.com/inducer/pudb/blob/master/pudb/theme.py

palette.update({
    "source": (add_setting("black", "underline"), "dark green"),
    "comment": ("h250", "default")
    })

Add link to defined colors to example theme


from django.conf.urls import url, include
from django.views.generic import TemplateView
from rest_framework import routers

from .views import FeatureRequestViewSet, ClientViewSet, ProductAreaViewSet

router = routers.DefaultRouter()

router.register(r'features', FeatureRequestViewSet)
router.register(r'client', ClientViewSet)
router.register(r'productarea', ProductAreaViewSet)

urlpatterns = [
    url(r'^', TemplateView.as_view(template_name='features/index.html')),
    url(r'^api/', include(router.urls)),
    url(r'api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]

from django.conf.urls import url, include
from django.views.generic import TemplateView
from rest_framework import routers

from .views import FeatureRequestViewSet, ClientViewSet, ProductAreaViewSet

router = routers.DefaultRouter()

router.register(r'features', FeatureRequestViewSet)
router.register(r'client', ClientViewSet)
router.register(r'productarea', ProductAreaViewSet)

urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name='features/index.html')),
    url(r'^api/', include(router.urls)),
    url(r'api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]

Index route should only match on '/'

BUGFIX: Index route should only match on '/'


# Copyright 2013 Getlogic BV, Sardar Yumatov
#
# 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.

__title__ = 'APNS client'
__version__ = "0.1.1"
__author__ = "Sardar Yumatov"
__contact__ = "ja.doma@gmail.com"
__license__ = "Apache 2.0"
__homepage__ = "https://bitbucket.org/sardarnl/apns-client/"
__copyright__ = 'Copyright 2013 Getlogic BV, Sardar Yumatov'

from apnsclient.apns import *

# Copyright 2013 Getlogic BV, Sardar Yumatov
#
# 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.

__title__ = 'APNS client'
__version__ = "0.1.5"
__author__ = "Sardar Yumatov"
__contact__ = "ja.doma@gmail.com"
__license__ = "Apache 2.0"
__homepage__ = "https://bitbucket.org/sardarnl/apns-client/"
__copyright__ = 'Copyright 2013 Getlogic BV, Sardar Yumatov'

from apnsclient.apns import *

Adjust the module __version__ to match the version advertised in PyPI.

--HG--
branch : intellectronica/adjust-the-module-__version__-to-match-t-1371450045566


class Fsharp31Package(GitHubTarballPackage):
	def __init__(self):
		GitHubTarballPackage.__init__(self,
			'fsharp', 'fsharp',
			'3.1.1.31',
			'1f79c0455fb8b5ec816985f922413894ce19359a',
			configure = '')
		self.sources.extend ([
			'fsharp-fix-net45-profile.patch')

	def prep(self):
		Package.prep (self)

		for p in range (1, len (self.sources)):
				self.sh ('patch -p1 < "%{sources[' + str (p) + ']}"')

	def build(self):
		self.sh ('autoreconf')
		self.sh ('./configure --prefix="%{prefix}"')
		self.sh ('make')

Fsharp31Package()

class Fsharp31Package(GitHubTarballPackage):
	def __init__(self):
		GitHubTarballPackage.__init__(self,
			'fsharp', 'fsharp',
			'3.1.1.31',
			'1f79c0455fb8b5ec816985f922413894ce19359a',
			configure = '')
		self.sources.extend ([
			'patches/fsharp-fix-net45-profile.patch'])

	def prep(self):
		Package.prep (self)

		for p in range (1, len (self.sources)):
				self.sh ('patch -p1 < "%{sources[' + str (p) + ']}"')

	def build(self):
		self.sh ('autoreconf')
		self.sh ('./configure --prefix="%{prefix}"')
		self.sh ('make')

Fsharp31Package()

Fix the typos, fix the build.

Fix the typos, fix the build.


"""Workflow execution engines."""
from resolwe.flow.engine import BaseEngine

class BaseExecutionEngine(BaseEngine):
    """A workflow execution engine."""

    def evaluate(self, data):
        """Return the code needed to compute a given Data object."""
        raise NotImplementedError

    def get_expression_engine(self, name):
        """Return an expression engine by its name."""
        return self.manager.get_expression_engine(name)

    def get_output_schema(self, process):
        """Return any additional output schema for the process."""
        return []

    def discover_process(self, path):
        """Perform process discovery in given path.

        This method will be called during process registration and
        should return a list of dictionaries with discovered process
        schemas.
        """
        return []

    def prepare_runtime(self, runtime_dir, data):
        """Prepare runtime directory.

        This method should return a dictionary of volume maps, where
        keys are files or directories relative the the runtime directory
        and values are paths under which these should be made available
        to the executing program. All volumes will be read-only.
        """
        return {}

Return empty dictionary instead of None

Return empty dictionary instead of None


# -*- coding: utf-8 -*-
import environ
env = environ.Env()
HOLVI_CNC = False

def get_holvi_singleton():
    global HOLVI_CNC
    if HOLVI_CNC:
        return HOLVI_CNC
    holvi_pool = env('HOLVI_POOL', default=None)
    holvi_key = env('HOLVI_APIKEY', default=None)
    if not holvi_pool or not holvi_key:
        return False
    import holviapi
    HOLVI_CNC = holviapi.Connection(holvi_pool, holvi_key)
    return HOLVI_CNC

# -*- coding: utf-8 -*-
import holviapi.utils

def get_nordea_payment_reference(member_id, number):
    base = member_id + 1000
    return holviapi.utils.int2fin_reference(int("%s%s" % (base, number)))

Remove copy-pasted code, add helper for making legacy reference number for payments


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

import email.quoprimime

def quoted_printable(val):
    try:
        if type(val) is str:
            return email.quoprimime.header_decode(val)
        else:
            return unicode(email.quoprimime.header_decode(str(val)), 'utf-8')

    except Exception, e:
        # We ignore errors here. Most of these originate from a spam
        # report adding a synopsis of a message with broken encodings.
        pass

    return val

from parse_mail import parse_mail

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

import email
import re

def header_decode(s):
    def unquote_match(match):
        s = match.group(0)
        return chr(int(s[1:3], 16))

    s = s.replace('_', ' ')
    return re.sub(r'=\w{2}', unquote_match, s)

def quoted_printable(val):
    try:
        if type(val) is str:
            save = header_decode(val)
            val = ' '.join([chunk.decode(encoding or 'ascii', 'replace') for chunk, encoding in
                email.Header.decode_header(val)])

            if len(val) > len(save):
                val = unicode(save, 'utf-8', 'replace')

        else:
            return unicode(email.quoprimime.header_decode(str(val)), 'utf-8', 'replace')

    except Exception, e:
        # We ignore errors here. Most of these originate from a spam
        # report adding a synopsis of a message with broken encodings.
        pass

    return val

from parse_mail import parse_mail

Update header_decode to handle bare and non-bare quoted-printable chars

Signed-off-by: Chris Lamb <711c73f64afdce07b7e38039a96d2224209e9a6c@chris-lamb.co.uk>


#  This Source Code Form is subject to the terms of the Mozilla Public
#  License, v. 2.0. If a copy of the MPL was not distributed with this
#  file, You can obtain one at http://mozilla.org/MPL/2.0/.

# Link states
UP, DOWN = (0, 1), (0, -1)
LEFT, RIGHT = (-1, 0), (1, 0)
SLACK = (0, 0)

Add link states to polymer_states

Add link states to polymer_states


"""
Line Chart with Sequence Generator
----------------------------------
This examples shows how to create multiple lines using the sequence generator.
"""
# category: line charts

import altair as alt

source = alt.sequence(start=0, stop=12.7, step=0.1, as_='x')

alt.Chart(source).mark_line().transform_calculate(
    sin='sin(datum.x)'
).transform_calculate(
    cos='cos(datum.x)'
).transform_fold(
    ['sin', 'cos']
).encode(
    x='x:Q',
    y='value:Q',
    color='key:N'
)

"""
Line Chart with Sequence Generator
----------------------------------
This examples shows how to create multiple lines using the sequence generator.
"""
# category: line charts

import altair as alt

source = alt.sequence(start=0, stop=12.7, step=0.1, as_='x')

alt.Chart(source).mark_line().transform_calculate(
    sin='sin(datum.x)',
    cos='cos(datum.x)'
).transform_fold(
    ['sin', 'cos']
).encode(
    x='x:Q',
    y='value:Q',
    color='key:N'
)

DOC: Modify generator example to use single calculation transform


from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from logging import getLogger
import os
import pwm

db = SQLAlchemy()
_logger = getLogger('pwm_server')

class PWMApp(Flask):

    def bootstrap(self):
        """ Initialize database tables for both pwm_server and pwm. """
        from .models import Certificate
        with self.app_context():
            db.metadata.create_all(db.engine, tables=[Certificate.__table__, pwm.Domain.__table__])

def create_app(config_file=None):
    app = PWMApp(__name__)
    app.config['WTF_CSRF_ENABLED'] = False

    if config_file:
        config_path = os.path.join(os.getcwd(), config_file)
        _logger.debug('Loading config from %s', config_path)
        app.config.from_pyfile(config_path)
    else:
        _logger.debug('Loading config from envvar, file %s', os.environ['PWM_SERVER_CONFIG_FILE'])
        app.config.from_envvar('PWM_SERVER_CONFIG_FILE')

    from . import views
    app.register_blueprint(views.mod)

    db.init_app(app)

    return app

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from logging import getLogger
import os
import pwm

db = SQLAlchemy()
_logger = getLogger('pwm_server')

class PWMApp(Flask):

    def bootstrap(self):
        """ Initialize database tables for both pwm_server and pwm. """
        from .models import Certificate
        with self.app_context():
            db.metadata.create_all(db.engine, tables=[Certificate.__table__, pwm.Domain.__table__])

def create_app(config_file=None):
    app = PWMApp(__name__)
    app.config['WTF_CSRF_ENABLED'] = False

    if config_file:
        config_path = os.path.join(os.getcwd(), config_file)
        _logger.debug('Loading config from %s', config_path)
    else:
        _logger.debug('Loading config from envvar, file %s', os.environ['PWM_SERVER_CONFIG_FILE'])
        config_path = os.path.join(os.getcwd(), os.environ['PWM_SERVER_CONFIG_FILE'])
    app.config.from_pyfile(config_path)

    from . import views
    app.register_blueprint(views.mod)

    db.init_app(app)

    return app

Resolve config from envvar relative to cwd


from sqlalchemy import MetaData, Table, inspect
from sqlalchemy.schema import CreateTable

from rs_sqla_test_utils.utils import clean, compile_query

def table_to_ddl(engine, table):
    return str(CreateTable(table)
               .compile(engine))

def test_view_reflection(redshift_engine):
    table_ddl = "CREATE TABLE my_table (col1 INTEGER, col2 INTEGER)"
    view_query = "SELECT my_table.col1, my_table.col2 FROM my_table"
    view_ddl = "CREATE VIEW my_view AS %s" % view_query
    conn = redshift_engine.connect()
    conn.execute(table_ddl)
    conn.execute(view_ddl)
    insp = inspect(redshift_engine)
    view_definition = insp.get_view_definition('my_view')
    assert(clean(compile_query(view_definition)) == clean(view_query))
    view = Table('my_view', MetaData(),
                 autoload=True, autoload_with=redshift_engine)
    assert(len(view.columns) == 2)

def test_late_binding_view_reflection(redshift_engine):
    table_ddl = "CREATE TABLE my_table (col1 INTEGER, col2 INTEGER)"
    view_query = "SELECT my_table.col1, my_table.col2 FROM public.my_table"
    view_ddl = ("CREATE VIEW my_late_view AS "
                "%s WITH NO SCHEMA BINDING" % view_query)
    conn = redshift_engine.connect()
    conn.execute(table_ddl)
    conn.execute(view_ddl)
    insp = inspect(redshift_engine)
    view_definition = insp.get_view_definition('my_late_view')

    # For some reason, Redshift returns the entire DDL for late binding views.
    assert(clean(compile_query(view_definition)) == clean(view_ddl))
    view = Table('my_late_view', MetaData(),
                 autoload=True, autoload_with=redshift_engine)
    assert(len(view.columns) == 2)

Add test for late-binding views

Add test for late-binding views


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

from importlib import import_module
import pkgutil
import sys
import inspect
from . import layers

def import_recursive(package, clsmembers):
    """
    Takes a package and imports all modules underneath it
    """

    pkg_dir = package.__path__
    module_location = package.__name__
    for (_module_loader, name, ispkg) in pkgutil.iter_modules(pkg_dir):
        module_name = "{}.{}".format(module_location, name)  # Module/package
        module = import_module(module_name)
        clsmembers += [cls[1] for cls in inspect.getmembers(module, inspect.isclass)]
        if ispkg:
            import_recursive(module, clsmembers)

clsmembers = []
import_recursive(sys.modules[__name__], clsmembers)

for cls in clsmembers:
    if issubclass(cls, layers.ModelLayer) and cls is not layers.ModelLayer:
        layers.register_layer(cls.__name__, cls)

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

from importlib import import_module
import pkgutil
import sys
from . import layers

def import_recursive(package):
    """
    Takes a package and imports all modules underneath it
    """

    pkg_dir = package.__path__
    module_location = package.__name__
    for (_module_loader, name, ispkg) in pkgutil.iter_modules(pkg_dir):
        module_name = "{}.{}".format(module_location, name)  # Module/package
        module = import_module(module_name)
        if ispkg:
            import_recursive(module)

def find_subclasses_recursively(base_cls, sub_cls):
    cur_sub_cls = base_cls.__subclasses__()
    sub_cls.update(cur_sub_cls)
    for cls in cur_sub_cls:
        find_subclasses_recursively(cls, sub_cls)

import_recursive(sys.modules[__name__])

model_layer_subcls = set()
find_subclasses_recursively(layers.ModelLayer, model_layer_subcls)

for cls in list(model_layer_subcls):
    layers.register_layer(cls.__name__, cls)

Allow to import subclasses of layers

Allow to import subclasses of layers

Summary:
We want it to be able to register children of layers who
are not direct children of ModelLayer.
This requires us to find subclasses of ModelLayer recursively.

Reviewed By: kittipatv, kennyhorror

Differential Revision: D5397120

fbshipit-source-id: cb1e03d72e3bedb960b1b865877a76e413218a71


from unittest import TestCase

from basics import BaseCharacter
from basics import BaseAttachment

class ModuleTest(TestCase):

    def test_character_attach_attachment(self):
        character = BaseCharacter().save()
        attachment = BaseAttachment().save()

        # Attachment should not be among the character's attachments
        self.assertNotIn(attachment.id, character.attachments())

        # Attach the attachment
        character.attach(attachment)

        # Attachment should be among the character's attachments
        self.assertIn(attachment.id, character.attachments())

    def test_container_containment(self):
        self.fail("Test unwritten")

from unittest import TestCase

from basics import BaseCharacter
from basics import BaseAttachment
from basics import BaseThing

class ModuleTest(TestCase):

    def test_character_attach_attachment(self):
        character = BaseCharacter().save()
        attachment = BaseAttachment().save()

        # Attachment should not be among the character's attachments
        self.assertNotIn(attachment.id, character.attachments())

        # Attach the attachment
        character.attach(attachment)

        # Attachment should be among the character's attachments
        self.assertIn(attachment.id, character.attachments())

    def test_container_containment(self):
        thing_a = BaseThing().save()
        thing_b = BaseThing().save()

        # thing_b should not be among thing_a's stuff
        self.assertNotIn(thing_b.id, thing_a.stuff())

        # thing_b aint contained
        self.assertIsNone(thing_b.container())

        # Move thing_b into thing_a
        thing_b.move_to(thing_a)

        # thing_b should be among thing_a's stuff
        self.assertIn(thing_b.id, thing_a.stuff())

        # thing_b is contained by thing_a
        self.assertEqual(thing_a, thing_b.container())

Write test for container containment.

Write test for container containment.


# coding: utf-8

import flynn.decoder
import flynn.encoder

def dump(obj, fp):
	return flynn.encoder.encode(fp, obj)

def dumps(obj):
	return flynn.encoder.encode_str(obj)

def dumph(obj):
	return "".join(hex(n)[2:].rjust(2, "0") for n in dumps(obj))

def load(s):
	return flynn.decoder.decode(s)

def loads(s):
	return flynn.decoder.decode(s)

def loadh(s):
	return flynn.decoder.decode(s)

# coding: utf-8

import base64

import flynn.decoder
import flynn.encoder

__all__ = [
	"decoder",
	"encoder",
	"dump",
	"dumps",
	"dumph",
	"load",
	"loads",
	"loadh"
]

def dump(obj, fp):
	return flynn.encoder.encode(fp, obj)

def dumps(obj):
	return flynn.encoder.encode_str(obj)

def dumph(obj):
	return base64.b16encode(dumps(obj)).decode("utf-8")

def load(s):
	return flynn.decoder.decode(s)

def loads(s):
	return flynn.decoder.decode(s)

def loadh(s):
	return flynn.decoder.decode(s)

Use base64 module to convert between bytes and base16 string


# MIT License
# Copyright (c) 2017 MassChallenge, Inc.

from impact.models import RefundCode
from impact.v1.helpers.model_helper import(
    INTEGER_ARRAY_FIELD,
    INTEGER_FIELD,
    ModelHelper,
    PK_FIELD,
    STRING_FIELD,
)

PROGRAMS_FIELD = {
    "json-schema": {
        "type": "array",
        "items": {"type": "string"},
    },
    "POST": {"required": False},
    "PATCH": {"required": False},
}

REFUND_CODE_FIELDS = {
    "id": PK_FIELD,
    "issued_to": INTEGER_FIELD,
    "created_at": STRING_FIELD,
    "unique_code": STRING_FIELD,
    "discount": INTEGER_FIELD,
    "maximum_uses": INTEGER_FIELD,
    "programs": INTEGER_ARRAY_FIELD,
}

class RefundCodeHelper(ModelHelper):
    model = RefundCode

    @classmethod
    def fields(self):
        return REFUND_CODE_FIELDS

    @property
    def issued_to(self):
        return self.field_pk("issued_to")

    @property
    def programs(self):
        if hasattr(self.subject, "programs"):
            programs = self.subject.programs
            if programs:
                return [program.pk for program in programs.all()]

# MIT License
# Copyright (c) 2017 MassChallenge, Inc.

from impact.models import RefundCode
from impact.v1.helpers.model_helper import(
    BOOLEAN_FIELD,
    INTEGER_ARRAY_FIELD,
    INTEGER_FIELD,
    ModelHelper,
    PK_FIELD,
    STRING_FIELD,
)

PROGRAMS_FIELD = {
    "json-schema": {
        "type": "array",
        "items": {"type": "string"},
    },
    "POST": {"required": False},
    "PATCH": {"required": False},
}

REFUND_CODE_FIELDS = {
    "id": PK_FIELD,
    "issued_to": INTEGER_FIELD,
    "created_at": STRING_FIELD,
    "unique_code": STRING_FIELD,
    "discount": INTEGER_FIELD,
    "maximum_uses": INTEGER_FIELD,
    "programs": INTEGER_ARRAY_FIELD,
    "notes": STRING_FIELD,
    "internal": BOOLEAN_FIELD,
}

class RefundCodeHelper(ModelHelper):
    model = RefundCode

    @classmethod
    def fields(self):
        return REFUND_CODE_FIELDS

    @property
    def issued_to(self):
        return self.field_pk("issued_to")

    @property
    def programs(self):
        if hasattr(self.subject, "programs"):
            programs = self.subject.programs
            if programs:
                return [program.pk for program in programs.all()]

Add Notes and Internal Fields

[AC-5291] Add Notes and Internal Fields


# -*- coding: utf-8 -*-
"""Generate a gallery to compare all available typhon styles.
"""
import numpy as np
import matplotlib.pyplot as plt
from typhon.plots import styles

def simple_plot(stylename):
    """Generate a simple plot using a given matplotlib style."""
    x = np.linspace(0, np.pi, 20)

    fig, ax = plt.subplots()
    for s in np.linspace(0, np.pi / 2, 12):
        ax.plot(x, np.sin(x+s),
                label=r'$\Delta\omega = {:.2f}$'.format(s),
                marker='.',
                )
    ax.set_ylabel('y-axis')
    ax.set_xlabel('x-axis')
    ax.set_title(stylename)
    ax.grid()
    ax.legend()

# Create plot using default styles.
simple_plot('matplotlib 2.0')

# Create a plot for each available typhon style.
for style_name in styles.available:
    with plt.style.context(styles(style_name)):
        simple_plot(style_name)

plt.show()

# -*- coding: utf-8 -*-
"""Generate a gallery to compare all available typhon styles.
"""
import numpy as np
import matplotlib.pyplot as plt
from typhon.plots import styles

def simple_plot(stylename):
    """Generate a simple plot using a given matplotlib style."""
    if stylename == 'typhon-dark':
        # TODO: Sphinx build is broken for non-white figure facecolor.
        return

    x = np.linspace(0, np.pi, 20)

    fig, ax = plt.subplots()
    for s in np.linspace(0, np.pi / 2, 12):
        ax.plot(x, np.sin(x+s),
                label=r'$\Delta\omega = {:.2f}$'.format(s),
                marker='.',
                )
    ax.set_ylabel('y-axis')
    ax.set_xlabel('x-axis')
    ax.set_title(stylename)
    ax.grid()
    ax.legend()

# Create plot using default styles.
simple_plot('matplotlib 2.0')

# Create a plot for each available typhon style.
for style_name in styles.available:
    with plt.style.context(styles(style_name)):
        simple_plot(style_name)

plt.show()

Exclude dark-colored theme from stylesheet gallery.


######
#  Check ExId (GUID) by Email through JDBC
#
#  Author:        Christoph Stoettner
#  Mail:          christoph.stoettner@stoeps.de
#  Documentation: http://scripting101.stoeps.de
#
#  Version:       2.0
#  Date:          2014-06-04
#
#  License:       Apache 2.0
#
#  Check ExId of a User in all Connections Applications

import ibmcnx.functions

cell = AdminConfig.getid( '"/Cell:' + AdminControl.getCell() + '/"' )
dbs = AdminConfig.list( 'DataSource', cell )

for db in dbs:
    t1 = ibmcnx.functions.getDSId( db )
    AdminConfig.list( t1 )

######
#  Check ExId (GUID) by Email through JDBC
#
#  Author:        Christoph Stoettner
#  Mail:          christoph.stoettner@stoeps.de
#  Documentation: http://scripting101.stoeps.de
#
#  Version:       2.0
#  Date:          2014-06-04
#
#  License:       Apache 2.0
#
#  Check ExId of a User in all Connections Applications

import ibmcnx.functions

cell = "'/Cell:" + AdminControl.getCell() + "/'"
print cell
cellid = AdminConfig.getid(  )
dbs = AdminConfig.list( 'DataSource', cellid )

for db in dbs:
    t1 = ibmcnx.functions.getDSId( db )
    AdminConfig.list( t1 )

4: Create script to save documentation to a file

Task-Url: http://github.com/stoeps13/ibmcnx2/issues/issue/4


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

from google.appengine.ext import ndb
import md5
import util

class BaseX(object):
  @classmethod
  def retrieve_one_by(cls, name, value):
    cls_db_list = cls.query(getattr(cls, name) == value).fetch(1)
    if cls_db_list:
      return cls_db_list[0]
    return None

class ConfigX(object):
  @classmethod
  def get_master_db(cls):
    return cls.get_or_insert('master')

class UserX(object):
  @property
  def avatar_url(self):
    return 'http://www.gravatar.com/avatar/%s?d=identicon&r=x' % (
        md5.new(self.email or self.name).hexdigest().lower()
      )

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

from google.appengine.ext import ndb
import md5
import util

class BaseX(object):
  @classmethod
  def retrieve_one_by(cls, name, value):
    cls_db_list = cls.query(getattr(cls, name) == value).fetch(1)
    if cls_db_list:
      return cls_db_list[0]
    return None

class ConfigX(object):
  @classmethod
  def get_master_db(cls):
    return cls.get_or_insert('master')

class UserX(object):
  @property
  def avatar_url(self):
    return 'http://www.gravatar.com/avatar/%s?d=identicon&r=x' % (
        md5.new((self.email or self.name).encode('utf-8')).hexdigest().lower()
      )

Fix generate md5 from unicode name and international email (http://en.wikipedia.org/wiki/International_email).


"""
Basic view mixins for predict views
"""

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required

from .models import PredictDataset

class PredictMixin(object):
    """The baseline predict view"""
    slug_field = 'md5'

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        """Only allow a logged in users to view"""
        return super(PredictMixin, self).dispatch(request, *args, **kwargs)

    def get_queryset(self):
        """Limit queryset to the user's own predictions only"""
        qs = PredictDataset.objects.all()
        if 'slug' not in self.kwargs:
            # Limit to my own predictions unless I have the md5
            qs = qs.filter(user_id=self.request.user.pk)
        return qs

"""
Basic view mixins for predict views
"""

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required

from .models import PredictDataset

class PredictMixin(object):
    """The baseline predict view"""
    slug_field = 'md5'

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        """Only allow a logged in users to view"""
        return super(PredictMixin, self).dispatch(request, *args, **kwargs)

    def get_queryset(self):
        """Limit queryset to the user's own predictions only"""
        qset = PredictDataset.objects.all()
        if 'slug' not in self.kwargs:
            # Limit to my own predictions unless I have the md5
            qset = qset.filter(user_id=self.request.user.pk)
        return qset.prefetch_related('strains', 'strains__piperun', 'strains__piperun__programs')

Improve prefetch speed in predict listing pages


from django.views.generic import DetailView, UpdateView
from django.contrib.auth.views import redirect_to_login
from django.core.urlresolvers import reverse
from django.shortcuts import redirect

from braces.views import LoginRequiredMixin

from .models import User

class ProfileDetailView(DetailView):
    '''
    Displays the user profile information
    '''
    model = User
    slug_field = 'username'
    slug_url_kwarg = 'username'

    def get(self, request, *args, **kwargs):
        user = request.user
        username = self.kwargs.get(self.slug_url_kwarg)

        if user.is_authenticated() and not username:
            return redirect('profile_detail', username=user.username)
        elif not user.is_authenticated() and not username:
            return redirect_to_login(reverse('profile_detail_me'))

        return super(ProfileDetailView, self).get(request, *args, **kwargs)

class ProfileUpdateView(LoginRequiredMixin, UpdateView):
    model = User
    slug_field = 'username'
    slug_url_kwarg = 'username'

from django.views.generic import DetailView, UpdateView
from django.contrib.auth.views import redirect_to_login
from django.core.urlresolvers import reverse
from django.shortcuts import redirect

from braces.views import LoginRequiredMixin

from .models import User

class ProfileDetailView(DetailView):
    '''
    Displays the user profile information
    '''
    queryset = User.objects.select_related('location', 'location__country')
    slug_field = 'username'
    slug_url_kwarg = 'username'

    def get(self, request, *args, **kwargs):
        user = request.user
        username = self.kwargs.get(self.slug_url_kwarg)

        if user.is_authenticated() and not username:
            return redirect('profile_detail', username=user.username)
        elif not user.is_authenticated() and not username:
            return redirect_to_login(reverse('profile_detail_me'))

        return super(ProfileDetailView, self).get(request, *args, **kwargs)

class ProfileUpdateView(LoginRequiredMixin, UpdateView):
    model = User
    slug_field = 'username'
    slug_url_kwarg = 'username'

Use select_related in user profile detail view


from traits.api import HasTraits

import json

class Instrument(HasTraits):
	"""
	Main super-class for all instruments.
	"""

	def get_settings(self):
		return self.__getstate__()

	def set_settings(self, settings):
		for key,value in settings.items():
			setattr(self, key, value)

from traits.api import HasTraits, Bool

import json

class Instrument(HasTraits):
	"""
	Main super-class for all instruments.
	"""
	enabled = Bool(True, desc='Whether the unit is used/enabled.')

	def get_settings(self):
		return self.__getstate__()

	def set_settings(self, settings):
		for key,value in settings.items():
			setattr(self, key, value)

Add enabled to top-level instrument class.


#-*- coding: utf-8 -*-
# version string following pep-0396 and pep-0386
__version__ = '0.9pbs.34'  # pragma: nocover

#-*- coding: utf-8 -*-
# version string following pep-0396 and pep-0386
__version__ = '0.9pbs.35'  # pragma: nocover

Bump version as instructed by bamboo.

Bump version as instructed by bamboo.


#!/usr/bin/env python
import sys

from funcy import split, map

from .analysis import global_usage, local_usage, FileSet

def main():
    command = sys.argv[1]
    opts, args = split(r'^--', sys.argv[2:])
    opts = dict(map(r'^--(\w+)(?:=(.+))?', opts))

    # Run ipdb on exception
    if 'ipdb' in opts:
        import ipdb, traceback

        def info(type, value, tb):
            traceback.print_exception(type, value, tb)
            print
            ipdb.pm()

        sys.excepthook = info

    # Register plugins
    from .ext import django
    django.register(args, opts)

    # Do the job
    files = FileSet(args, base=opts.get('base'), ignore=opts.get('ignore'))
    if command == 'global':
        global_usage(files)
    elif command == 'local':
        local_usage(files)
    else:
        print 'Unknown command', command

if __name__ == '__main__':
    main()

#!/usr/bin/env python
import sys

from funcy import split, map

from .analysis import global_usage, local_usage, FileSet

def main():
    command = sys.argv[1]
    opts, args = split(r'^--', sys.argv[2:])
    opts = dict(map(r'^--(\w+)(?:=(.+))?', opts))

    # Run ipdb on exception
    if 'ipdb' in opts:
        import ipdb, traceback

        def info(type, value, tb):
            traceback.print_exception(type, value, tb)
            print
            # Insert look-around helpers into the frame
            import inspect, ast
            from .asttools import to_source
            frame = inspect.getinnerframes(tb)[-1][0]
            frame.f_globals.setdefault('ast', ast)
            frame.f_globals.setdefault('to_source', to_source)
            # Run debugger
            ipdb.pm()

        sys.excepthook = info

    # Register plugins
    from .ext import django
    django.register(args, opts)

    # Do the job
    files = FileSet(args, base=opts.get('base'), ignore=opts.get('ignore'))
    if command == 'global':
        global_usage(files)
    elif command == 'local':
        local_usage(files)
    else:
        print 'Unknown command', command

if __name__ == '__main__':
    main()

Insert look-around helpers into ipdb context

These are `ast` and `to_source`.


# -*- coding: utf-8 -*-
import os
import shutil

from logya.core import Logya
from logya.fs import copytree
from logya.writer import DocWriter

class Generate(Logya):
    """Generate a Web site to deploy from current directory as source."""

    def __init__(self, **kwargs):

        super(self.__class__, self).__init__(**kwargs)
        self.init_env()

        # Init writer before executing scripts, so they can use it.
        self.writer = DocWriter(self.dir_deploy, self.template)

        if not kwargs['keep']:
            self.info('Remove existing deploy directory')
            shutil.rmtree(self.dir_deploy, True)

        self.info('Generating site in directory: {}'.format(self.dir_deploy))

        if os.path.exists(self.dir_static):
            self.info('Copy static files')
            copytree(self.dir_static, self.dir_deploy)

        self.info('Build document index')
        self.build_index()

        self.info('Write documents')
        for doc in self.docs.values():
            self.writer.write(doc, self.get_doc_template(doc))
        self.info(
            'Written {:d} documents to deploy directory'
            .format(len(self.docs)))

        self.info('Write index files')
        self.write_index_files()
        self.info(
            'Written {:d} index files to deploy directory'
            .format(len(self.index)))

# -*- coding: utf-8 -*-
import os
import shutil

from logya.core import Logya
from logya.fs import copytree
from logya.writer import DocWriter

class Generate(Logya):
    """Generate a Web site to deploy from current directory as source."""

    def __init__(self, **kwargs):
        super(self.__class__, self).__init__(**kwargs)
        self.init_env()
        self.writer = DocWriter(self.dir_deploy, self.template)

        if not kwargs['keep']:
            self.info('Remove existing deploy directory')
            shutil.rmtree(self.dir_deploy, True)

        self.info('Generate site in directory: {}'.format(self.dir_deploy))
        if os.path.exists(self.dir_static):
            self.info('Copy static files')
            copytree(self.dir_static, self.dir_deploy)

        self.build()
        self.write()

    def build(self):
        self.info('Build document index')
        self.build_index()

    def write(self):
        self.info('Write documents')
        for doc in self.docs.values():
            self.writer.write(doc, self.get_doc_template(doc))
        self.info(
            'Written {:d} documents to deploy directory'
            .format(len(self.docs)))

        self.info('Write index files')
        self.write_index_files()
        self.info(
            'Written {:d} index files to deploy directory'
            .format(len(self.index)))

Add build and write function to make it easy to subclass Generate and overwrite build step


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

from flask import Flask, render_template, g
from flask.ext.appconfig import AppConfig

def create_app(configfile=None):
    app = Flask('massa')
    AppConfig(app, configfile)

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

    from .container import build
    sl = build(app.config)

    from .api import bp
    app.register_blueprint(bp, url_prefix='/api')

    @app.before_request
    def globals():
        g.sl = sl

    return app

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

from flask import Flask, render_template, g
from flask.ext.appconfig import AppConfig
from .container import build
from .api import bp as api

def create_app(configfile=None):
    app = Flask('massa')
    AppConfig(app, configfile)

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

    sl = build(app.config)

    app.register_blueprint(api, url_prefix='/api')

    @app.before_request
    def globals():
        g.sl = sl

    return app

Move import statements to the top.

Move import statements to the top.


# -*- coding: utf-8 -*-
from distutils import version

__version__ = '0.2.0'
version_info = version.StrictVersion(__version__).version

from .parsers import PlistParser  # NOQA
from .renderers import PlistRenderer  # NOQA

Make parser and renderer available at package root


from django.core.handlers.wsgi import STATUS_CODE_TEXT
from django.template.response import SimpleTemplateResponse

class Response(SimpleTemplateResponse):
    """
    An HttpResponse that allows it's data to be rendered into
    arbitrary media types.
    """

    def __init__(self, data=None, status=None, headers=None,
                 renderer=None, accepted_media_type=None):
        """
        Alters the init arguments slightly.
        For example, drop 'template_name', and instead use 'data'.

        Setting 'renderer' and 'media_type' will typically be defered,
        For example being set automatically by the `APIView`.
        """
        super(Response, self).__init__(None, status=status)
        self.data = data
        self.headers = headers and headers[:] or []
        self.renderer = renderer
        self.accepted_media_type = accepted_media_type

    @property
    def rendered_content(self):
        self['Content-Type'] = self.renderer.media_type
        if self.data is None:
            return self.renderer.render()
        render_media_type = self.accepted_media_type or self.renderer.media_type
        return self.renderer.render(self.data, render_media_type)

    @property
    def status_text(self):
        """
        Returns reason text corresponding to our HTTP response status code.
        Provided for convenience.
        """
        return STATUS_CODE_TEXT.get(self.status_code, '')

from django.core.handlers.wsgi import STATUS_CODE_TEXT
from django.template.response import SimpleTemplateResponse

class Response(SimpleTemplateResponse):
    """
    An HttpResponse that allows it's data to be rendered into
    arbitrary media types.
    """

    def __init__(self, data=None, status=None, headers=None,
                 renderer=None, accepted_media_type=None):
        """
        Alters the init arguments slightly.
        For example, drop 'template_name', and instead use 'data'.

        Setting 'renderer' and 'media_type' will typically be defered,
        For example being set automatically by the `APIView`.
        """
        super(Response, self).__init__(None, status=status)
        self.data = data
        self.headers = headers and headers[:] or []
        self.renderer = renderer

        # Accepted media type is the portion of the request Accept header
        # that the renderer satisfied.  It could be '*/*', or somthing like
        # 'application/json; indent=4'
        #
        # This is NOT the value that will be returned in the 'Content-Type'
        # header, but we do need to know the value in case there are
        # any specific parameters which affect the rendering process.
        self.accepted_media_type = accepted_media_type

    @property
    def rendered_content(self):
        self['Content-Type'] = self.renderer.media_type
        if self.data is None:
            return self.renderer.render()
        render_media_type = self.accepted_media_type or self.renderer.media_type
        return self.renderer.render(self.data, render_media_type)

    @property
    def status_text(self):
        """
        Returns reason text corresponding to our HTTP response status code.
        Provided for convenience.
        """
        return STATUS_CODE_TEXT.get(self.status_code, '')

Tweak media_type -> accepted_media_type. Need to document, but marginally less confusing


from groundstation import logger
log = logger.getLogger(__name__)

def handle_describeobjects(self):
    if not self.payload:
        log.info("station %s sent empty DESCRIVEOBJECTS payload - new database?" % (str(self.origin)))
        return
    for obj in self.payload.split(chr(0)):
        if obj not in self.station or True:
            request = self._Request("FETCHOBJECT", payload=obj)
            self.stream.enqueue(request)
        else:
            log.debug("Not fetching already present object %s" % (str(obj)))

from groundstation import logger
log = logger.getLogger(__name__)

def handle_describeobjects(self):
    if not self.payload:
        log.info("station %s sent empty DESCRIVEOBJECTS payload - new database?" % (str(self.origin)))
        return
    for obj in self.payload.split(chr(0)):
        if obj not in self.station:
            request = self._Request("FETCHOBJECT", payload=obj)
            self.stream.enqueue(request)
        else:
            log.debug("Not fetching already present object %s" % (str(obj)))

Remove hook that snuck in

Remove hook that snuck in


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

urlpatterns = (
    url(r'^authorize/$', views.AuthorizationView.as_view(
        template_name='accounts/authorize_client.html',
    ), name="authorize"),
    url(r'^token/$', views.TokenView.as_view(), name="token"),
    url(r'^revoke_token/$', views.RevokeTokenView.as_view(), name="revoke-token"),
)

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

urlpatterns = (
    url(r'^authorize/?$', views.AuthorizationView.as_view(
        template_name='accounts/authorize_client.html',
    ), name="authorize"),
    url(r'^token/?$', views.TokenView.as_view(), name="token"),
    url(r'^revoke_token/?$', views.RevokeTokenView.as_view(), name="revoke-token"),
)

Make trailing slash optional in API oauth URL patterns

https://github.com/AudioCommons/ac-mediator/issues/19


# Project Euler - Problem 1
sum = 0
for i in xrange(1, 1001):
    if i % 3 == 0 or i % 5 == 0:
        sum = sum + i

print "The sum is: {}".format(sum)

# Project Euler - Problem 1
# If we list all the natural numbers below 10 that are multiples of 3 or 5,
# we get 3, 5, 6 and 9. The sum of these multiples is 23.
# Find the sum of all the multiples of 3 or 5 below 1000.

def main(limit):
    sum = 0
    for i in xrange(1, limit):
        if i % 3 == 0 or i % 5 == 0:
            sum = sum + i

    print "The sum of all multiples of 3 and 5 below {} is: {}".format(limit, sum)

if __name__ == "__main__":
    main(10)
    main(1001)

Clean up problem 1 solution a bit.

Clean up problem 1 solution a bit.


from __future__ import print_function

from metatlas.mzml_loader import mzml_to_hdf, get_test_data
from metatlas.h5_query import get_XICof, get_data

def rmse(target, predictions):
    target = target / target.max()
    predictions = predictions / predictions.max()

    return np.sqrt(((predictions - targets) ** 2).mean())

def test_xicof():
    return
    fid = tables.open_file('140808_1_RCH2_neg.h5')
    x, y = get_XICof(fid, 1, 1000, 1, 0)

    xicof_scidb = np.load('xicof_scidb.npy')

    assert rmse(y, xicof_scidb[:, 1]) < 0.01

    data = get_data(fid, 1, 0, mz_min=1, mz_max=1000)
    assert x.sum() == data['i'].sum()
    assert y[0] == data['rt'][0]
    assert y[-1] == data['rt'][-1]

from __future__ import print_function

from metatlas.mzml_loader import mzml_to_hdf, get_test_data
from metatlas.h5_query import get_XIC, get_data

def rmse(target, predictions):
    target = target / target.max()
    predictions = predictions / predictions.max()

    return np.sqrt(((predictions - targets) ** 2).mean())

def test_xicof():
    return
    fid = tables.open_file('140808_1_RCH2_neg.h5')
    x, y = get_XICof(fid, 1, 1000, 1, 0)

    xicof_scidb = np.load('xicof_scidb.npy')

    assert rmse(y, xicof_scidb[:, 1]) < 0.01

    data = get_data(fid, 1, 0, mz_min=1, mz_max=1000)
    assert x.sum() == data['i'].sum()
    assert y[0] == data['rt'][0]
    assert y[-1] == data['rt'][-1]

Fix another import in test

Fix another import in test


"""R markdown notebook format for Jupyter

Use this module to read or write Jupyter notebooks as Rmd documents (methods 'read', 'reads', 'write', 'writes')

Use the 'pre_save_hook' method (see its documentation) to automatically dump your Jupyter notebooks as a Rmd file, in addition
to the ipynb file.

Use the 'nbrmd' conversion script to convert Jupyter notebooks from/to R markdown notebooks.
"""

from .nbrmd import read, reads, readf, write, writes, writef
from .hooks import update_rmd, update_ipynb, update_rmd_and_ipynb, update_selected_formats
from .cm import RmdFileContentsManager

"""R markdown notebook format for Jupyter

Use this module to read or write Jupyter notebooks as Rmd documents (methods 'read', 'reads', 'write', 'writes')

Use the 'pre_save_hook' method (see its documentation) to automatically dump your Jupyter notebooks as a Rmd file, in addition
to the ipynb file.

Use the 'nbrmd' conversion script to convert Jupyter notebooks from/to R markdown notebooks.
"""

from .nbrmd import read, reads, readf, write, writes, writef
from .hooks import update_rmd, update_ipynb, update_rmd_and_ipynb, update_selected_formats
try:
  from .cm import RmdFileContentsManager
except ImportError as e:
  RmdFileContentsManager = e.message

Allow import in case of missing notebook package


from logtacts.settings import *
import dj_database_url

DEBUG = False
TEMPLATE_DEBUG = DEBUG

DATABASES['default'] = dj_database_url.parse(get_env_variable('LOGTACTS_DB_URL'))

SECRET_KEY = get_env_variable("LOGTACTS_SECRET_KEY")

ALLOWED_HOSTS = [
    'localhost',
    '127.0.0.1',
    '.pebble.ink',
    '.logtacts.com',
    '.contactotter.com',
]

SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 3600
SECURE_FRAME_DENY = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True

STATIC_URL = '//logtacts.s3.amazonaws.com/assets/'

INSTALLED_APPS += (
    'gunicorn',
    'opbeat.contrib.django',
)

MIDDLEWARE_CLASSES = (
    'opbeat.contrib.django.middleware.OpbeatAPMMiddleware',
) + MIDDLEWARE_CLASSES

OPBEAT = {
    'ORGANIZATION_ID': get_env_variable("OPBEAT_ORG_ID"),
    'APP_ID': get_env_variable("OPBEAT_APP_ID"),
    'SECRET_TOKEN': get_env_variable("OPBEAT_SECRET_KEY"),
}

from logtacts.settings import *
import dj_database_url

DEBUG = False
TEMPLATE_DEBUG = DEBUG

DATABASES['default'] = dj_database_url.parse(get_env_variable('LOGTACTS_DB_URL'))

SECRET_KEY = get_env_variable("LOGTACTS_SECRET_KEY")

ALLOWED_HOSTS = [
    'localhost',
    '127.0.0.1',
    '.pebble.ink',
    '.logtacts.com',
    '.contactotter.com',
    '.herokuapp.com',
]

SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 3600
SECURE_FRAME_DENY = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True

STATIC_URL = '//logtacts.s3.amazonaws.com/assets/'

INSTALLED_APPS += (
    'gunicorn',
    'opbeat.contrib.django',
)

MIDDLEWARE_CLASSES = (
    'opbeat.contrib.django.middleware.OpbeatAPMMiddleware',
) + MIDDLEWARE_CLASSES

OPBEAT = {
    'ORGANIZATION_ID': get_env_variable("OPBEAT_ORG_ID"),
    'APP_ID': get_env_variable("OPBEAT_APP_ID"),
    'SECRET_TOKEN': get_env_variable("OPBEAT_SECRET_KEY"),
}

Make sure heroku is in accepted hosts

Make sure heroku is in accepted hosts


# Copyright (c) 2014, German Neuroinformatics Node (G-Node)
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the Project.

# general config
extensions = ['sphinx.ext.autodoc']
source_suffix = '.rst'
master_doc = 'index'
project = 'NIX Python bindings'
copyright = '2014, German Neuroinformatics Node, Adrian Stoewer, Christian Kellner'
exclude_patterns = []
pygments_style = 'sphinx'

# html options
html_theme = 'default'
htmlhelp_basename = 'nix'

# Copyright (c) 2014, German Neuroinformatics Node (G-Node)
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the Project.

# general config
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx']
source_suffix = '.rst'
master_doc = 'index'
project = 'NIX Python bindings'
copyright = '2014, German Neuroinformatics Node, Adrian Stoewer, Christian Kellner'
exclude_patterns = []
pygments_style = 'sphinx'

# html options
html_theme = 'default'
htmlhelp_basename = 'nix'

# intersphinx configuration
intersphinx_mapping = {
    'http://docs.python.org/2.7' : None,
    'http://docs.scipy.org/doc/numpy': None
}

[doc] Enable intersphinx and add mapping for py2.7 + numpy


import sys
import logging

# NOTE: Be sure to keep the logging level commented out before commiting.  See:
#   https://github.com/numba/numba/issues/31
# A good work around is to make your tests handle a debug flag, per
# numba.tests.test_support.main().
logging.basicConfig(#level=logging.DEBUG,
                    format="\n\033[1m%(levelname)s -- %(module)s:%(lineno)d:%(funcName)s\033[0m\n%(message)s")

try:
    from . import minivect
except ImportError:
    print(logging.error("Did you forget to update submodule minivect?"))
    print(logging.error("Run 'git submodule init' followed by 'git submodule update'"))
    raise

from . import _numba_types
from ._numba_types import *
from . import decorators
from .decorators import *

def test():
    raise Exception("run nosetests from the numba directory")

__all__ = _numba_types.__all__ + decorators.__all__

import sys
import logging

# NOTE: Be sure to keep the logging level commented out before commiting.  See:
#   https://github.com/numba/numba/issues/31
# A good work around is to make your tests handle a debug flag, per
# numba.tests.test_support.main().

class _RedirectingHandler(logging.Handler):
    '''
    A log hanlder that applies its formatter and redirect the emission
    to a parent handler.
    '''
    def set_handler(self, handler):
        self.handler = handler

    def emit(self, record):
        # apply our own formatting
        record.msg = self.format(record)
        record.args = [] # clear the args
        # use parent handler to emit record
        self.handler.emit(record)

def _config_logger():
    root = logging.getLogger(__name__)
    format = "\n\033[1m%(levelname)s -- "\
             "%(module)s:%(lineno)d:%(funcName)s\033[0m\n%(message)s"
    try:
        parent_hldr = root.parent.handlers[0]
    except IndexError: # parent handler is not initialized?
        # build our own handler --- uses sys.stderr by default.
        parent_hldr = logging.StreamHandler()
    hldr = _RedirectingHandler()
    hldr.set_handler(parent_hldr)
    fmt = logging.Formatter(format)
    hldr.setFormatter(fmt)
    root.addHandler(hldr)
    root.propagate = False # do not propagate to the root logger

_config_logger()

try:
    from . import minivect
except ImportError:
    print(logging.error("Did you forget to update submodule minivect?"))
    print(logging.error("Run 'git submodule init' followed by 'git submodule update'"))
    raise

from . import _numba_types
from ._numba_types import *
from . import decorators
from .decorators import *

def test():
    raise Exception("run nosetests from the numba directory")

__all__ = _numba_types.__all__ + decorators.__all__

Update logging facility. Don't overide root logger with basicConfig.


# request handler for editing page (/edit) goes here
# assume admin login has already been handled

import cgi
from google.appengine.api import users
import webapp2

class DateHandler(webapp2.RequestHandler):

  def get(self):

    template_values = {

    }

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

class EditHandler(webapp2.RequestHandler):

  def get(self):

    template_values = {

    }

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

app = webapp2.WSGIApplication([
    ('/date', DateHandler)
    ('/edit', EditHandler)
], debug=True)

# request handler for editing page (/edit) goes here
# assume admin login has already been handled

import cgi
from google.appengine.api import users
import webapp2

class DateHandler(webapp2.RequestHandler):

  def get(self):
    date = self.request.get(date)
    template_values = {

    }

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

class EditHandler(webapp2.RequestHandler):

  def get(self):

    template_values = {

    }

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

app = webapp2.WSGIApplication([
    ('/date', DateHandler)
    ('/edit', EditHandler)
], debug=True)

Put in date = self.request.get(date) in order to call the date from the HTML.


from .format import DrmFormat
from .framebuffer import DrmFramebuffer
from .buffer import DrmDumbBuffer

class DrmImageFramebuffer(DrmFramebuffer):
    def __init__(self, drm=None, format_=None, width=None, height=None, bo=None):
        if drm and format_ and width and height and bo is None:
            bo = DrmDumbBuffer(drm, format_, width, height)
        elif (drm or format_ or width or height) and bo is None:
            raise TypeError()
        super(DrmImageFramebuffer, self).__init__(bo)
        self._setup()

    def _setup(self):
        from PIL import Image
        self.bo.mmap()
        if self.format.name == 'XR24':
            # didn't find an XRGB format
            self.image = Image.new("RGBX", (self.width, self.height))
        else:
            raise ValueError("DrmImageFramebuffer does not support format '%s'" % self.format.name)

    def flush(self, x1=None, y1=None, x2=None, y2=None):
        # FIXME: Revisit and see if this can be sped up and support big endian
        # Convert RGBX -> Little Endian XRGB
        b = bytearray(self.image.tobytes())
        b[0::4], b[2::4] = b[2::4], b[0::4]
        self.bo.map[:] = str(b)
        super(DrmImageFramebuffer, self).flush(x1, y1, x2, y2)

from .format import DrmFormat
from .framebuffer import DrmFramebuffer
from .buffer import DrmDumbBuffer

class DrmImageFramebuffer(DrmFramebuffer):
    def __init__(self, drm=None, format_=None, width=None, height=None, bo=None):
        if drm and format_ and width and height and bo is None:
            bo = DrmDumbBuffer(drm, format_, width, height)
        elif (drm or format_ or width or height) and bo is None:
            raise TypeError()
        super(DrmImageFramebuffer, self).__init__(bo)
        self._setup()

    def _setup(self):
        from PIL import Image
        self.bo.mmap()
        if self.format.name == 'XR24':
            # didn't find an XRGB format
            self.image = Image.new("RGBX", (self.width, self.height))
        else:
            raise ValueError("DrmImageFramebuffer does not support format '%s'" % self.format.name)

    def flush(self, x1=None, y1=None, x2=None, y2=None):
        # FIXME: Revisit and see if this can be sped up and support big endian
        # Convert RGBX -> Little Endian XRGB
        b = bytearray(self.image.tobytes())
        b[0::4], b[2::4] = b[2::4], b[0::4]
        self.bo.map[:] = bytes(b)
        super(DrmImageFramebuffer, self).flush(x1, y1, x2, y2)

Write bytes object to mmap instead of string

This fixes compatibility with python3


#!/usr/bin/python
import time
import serial

# configure the serial connections (the parameters differs on the
# device you are connecting to)

class Bumper(object):

    def __init__(self):
        try:
            self.ser = serial.Serial(
                port="/dev/ttyS0",
                baudrate=9600,
                parity=serial.PARITY_ODD,
                stopbits=serial.STOPBITS_TWO,
                bytesize=serial.SEVENBITS
            )
        except Exception:
            print("Bad initialisation! Check the configuration of "
                  "the serial port!")
            exit()
        self.ser.open()
        self.ser.isOpen()

    def loop(self):
        input=1
        while 1 :
            # get keyboard input
            input = raw_input(">> ")
            # Python 3 users
            # input = input(">> ")
            if input == "exit":
                self.ser.close()
                exit()
            else:
                # send the character to the device
                # (note that I happend a \r\n carriage return and line feed to
                # the characters - this is requested by my device)
                self.ser.write(input + "\r\n")
                out = ""
                # let's wait one second before reading output (let's give
                # device time to answer)
                time.sleep(1)
                while self.ser.inWaiting() > 0:
                    out += self.ser.read(1)

                if out != "":
                    print ">> " + out

def main():
    b = Bumper()
    b.loop()

if __name__ == "__main__":
    main()

#!/usr/bin/python
import time
import serial

# configure the serial connections (the parameters differs on the
# device you are connecting to)

class Bumper(object):

    def __init__(self):
        try:
            self.ser = serial.Serial(
                port="/dev/ttyS0",
                baudrate=38400,
                parity=serial.PARITY_ODD,
                stopbits=serial.STOPBITS_ONE,
                bytesize=serial.EIGHTBITS
            )
        except Exception:
            print("Bad initialisation! Check the configuration of "
                  "the serial port!")
            exit()
        self.ser.open()
        self.ser.isOpen()

    def loop(self):
        input=1
        while 1 :
            # get keyboard input
            input = raw_input(">> ")
            # Python 3 users
            # input = input(">> ")
            if input == "exit":
                self.ser.close()
                exit()
            else:
                # send the character to the device
                # (note that I happend a \r\n carriage return and line feed to
                # the characters - this is requested by my device)
                self.ser.write(input + "\r\n")
                out = ""
                # let's wait one second before reading output (let's give
                # device time to answer)
                time.sleep(1)
                while self.ser.inWaiting() > 0:
                    out += self.ser.read(1)

                if out != "":
                    print ">> " + out

def main():
    b = Bumper()
    b.loop()

if __name__ == "__main__":
    main()

Set parameters of serial class to match with kilt


from django import forms
from cyder.cydhcp.interface.dynamic_intr.models import (DynamicInterface,
                                                        DynamicIntrKeyValue)
from cyder.base.mixins import UsabilityFormMixin
from cyder.cydhcp.forms import RangeWizard

class DynamicInterfaceForm(RangeWizard, UsabilityFormMixin):

    def __init__(self, *args, **kwargs):
        super(DynamicInterfaceForm, self).__init__(*args, **kwargs)
        self.fields.keyOrder = ['system', 'domain', 'mac', 'vrf',
                                'site', 'range', 'workgroup', 'dhcp_enabled',
                                'dns_enabled', 'ctnr']

    class Meta:
        model = DynamicInterface
        exclude = ('last_seen')

class DynamicIntrKeyValueForm(forms.ModelForm):
    dynamic_interface = forms.ModelChoiceField(
        queryset=DynamicInterface.objects.all(),
        widget=forms.HiddenInput())

    class Meta:
        model = DynamicIntrKeyValue
        exclude = ('is_option', 'is_statement', 'is_quoted',)

from django import forms
from cyder.cydhcp.interface.dynamic_intr.models import (DynamicInterface,
                                                        DynamicIntrKeyValue)
from cyder.base.mixins import UsabilityFormMixin
from cyder.cydhcp.forms import RangeWizard

class DynamicInterfaceForm(RangeWizard, UsabilityFormMixin):

    def __init__(self, *args, **kwargs):
        super(DynamicInterfaceForm, self).__init__(*args, **kwargs)
        self.fields.keyOrder = ['system', 'domain', 'mac', 'vrf',
                                'site', 'range', 'workgroup', 'dhcp_enabled',
                                'dns_enabled', 'ctnr']
        self.fields['range'].required = True

    class Meta:
        model = DynamicInterface
        exclude = ('last_seen')

class DynamicIntrKeyValueForm(forms.ModelForm):
    dynamic_interface = forms.ModelChoiceField(
        queryset=DynamicInterface.objects.all(),
        widget=forms.HiddenInput())

    class Meta:
        model = DynamicIntrKeyValue
        exclude = ('is_option', 'is_statement', 'is_quoted',)

Reset range to be required in dynamic intr form


"""
  Copyright 2015 herd contributors

  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.
"""
# Standard library imports
import logging, logging.handlers
import sys

def get_logger(name):
    """
    Get logger to output to file and stdout

    :param name: name of module calling method
    :return: the logger
    """

    log_format = logging.Formatter("%(asctime)s - %(module)s - Line %(lineno)d - %(levelname)s \n%(message)s",
                                   "%Y-%m-%d %H:%M:%S")

    log_handler = logging.handlers.RotatingFileHandler('debug.log', mode='a', maxBytes=1024 * 1024)
    log_handler.setFormatter(log_format)

    stream_handler = logging.StreamHandler(sys.stdout)
    stream_handler.setFormatter(log_format)

    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)
    logger.addHandler(log_handler)
    logger.addHandler(stream_handler)
    return logger

"""
  Copyright 2015 herd contributors

  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.
"""
# Standard library imports
import logging, logging.handlers
import sys

def get_logger(name):
    """
    Get logger to output to file and stdout

    :param name: name of module calling method
    :return: the logger
    """

    log_format = logging.Formatter("%(asctime)s - %(module)s - Line %(lineno)d - %(levelname)s \n%(message)s",
                                   "%Y-%m-%d %H:%M:%S")

    log_handler = logging.handlers.RotatingFileHandler('debug.log', mode='a', maxBytes=50 * 1024, backupCount=1)
    log_handler.setFormatter(log_format)

    stream_handler = logging.StreamHandler(sys.stdout)
    stream_handler.setFormatter(log_format)

    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)
    logger.addHandler(log_handler)
    logger.addHandler(stream_handler)
    return logger

DM-12174: Manage Content Loader as Herd/DM tool
- Lower log size cap


import unittest
import util
from git_wrapper import GitWrapper

class GitWrapperIntegrationTest(util.RepoTestCase):
    def test_paths(self):
        self.open_tar_repo('project01')
        assert('test_file.txt' in self.repo.paths)
        assert('hello_world.rb' in self.repo.paths)

    def test_stage(self):
        self.open_tar_repo('project02')
        assert('not_committed_file.txt' in self.repo.stage)
        assert('second_not_committed_file.txt' in self.repo.stage)

    def test_paths_external_git_folder(self):
        self.open_tar_repo('project03', '../project03.git')
        assert('test_file.txt' in self.repo.paths)
        assert('hello_world.rb' in self.repo.paths)

    def test_stage_external_git_folder(self):
        self.open_tar_repo('project04', '../project04.git')
        assert('not_committed_file.txt' in self.repo.stage)
        assert('second_not_committed_file.txt' in self.repo.stage)

import unittest
import util
from git_wrapper import GitWrapper

class GitWrapperIntegrationTest(util.RepoTestCase):
    def test_paths(self):
        self.open_tar_repo('project01')
        assert('test_file.txt' in self.repo.paths)
        assert('hello_world.rb' in self.repo.paths)

    def test_stage(self):
        self.open_tar_repo('project02')
        assert('not_committed_file.txt' in self.repo.stage)
        assert('second_not_committed_file.txt' in self.repo.stage)

class GitWrapperIntegrationTestExternalGitFolder(util.RepoTestCase):
    def test_paths_external(self):
        self.open_tar_repo('project03', '../project03.git')
        assert('test_file.txt' in self.repo.paths)
        assert('hello_world.rb' in self.repo.paths)

    def test_stage_external(self):
        self.open_tar_repo('project04', '../project04.git')
        assert('not_committed_file.txt' in self.repo.stage)
        assert('second_not_committed_file.txt' in self.repo.stage)

Move external git folder integration tests to a separate class


def main():
    print('Hello, World.')

if __name__ == '__main__':
    main()

from bs4 import BeautifulSoup
import requests

def get_html():
    r = requests.get('http://vancouver.craigslist.ca/search/cto?query=Expedition')
    print(r.status_code)
    print(r.text)
    return r.text

def main():
    html = get_html()

    soup = BeautifulSoup(html, 'lxml')
    print(soup.prettify())
    mydivs = soup.findAll('a', {'class': 'hdrlnk'})
    for t in mydivs:
        print(t)

    print('Hello, World.')

if __name__ == '__main__':
    main()

Add soup extraction of links in results page


'''
'''
import functools
import operator
from .. import monads

def view(func, **constraints):
    '''Functions that decorates a view. This function can also checks the
    argument values
    '''
    func.is_view = True
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        try:
            if not functools.reduce(operator.__and__,
                                    [constraints[arg](kwargs[arg])
                                     for arg in constraints]):
                return monads.NoneMonad(ValueError(
                                                'Wrong view argument value'))
            return monads.ValueMonad(func(*args, **kwargs))
        except Exception as e:
            return monads.NoneMonad(e)
    return wrapper

'''
'''
import functools
import operator
from .. import monads

def view(func, **constraints):
    '''Functions that decorates a view. This function can also checks the
    argument values
    '''
    func.is_view = True
    @functools.wraps(func)
    @monads.handle_exception
    def wrapper(*args, **kwargs):
        if not functools.reduce(operator.__and__,
                                [constraints[arg](kwargs[arg])
                                 for arg in constraints]):
            return monads.NoneMonad(ValueError('Wrong view argument value'))
        return monads.ValueMonad(func(*args, **kwargs))
    return wrapper

Use exception handling with decorator

Use exception handling with decorator


from selenium import webdriver

from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.core.cache import cache

class BrowserTest(StaticLiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.PhantomJS()
        self.browser.set_window_size(1024, 768)

    def tearDown(self):
        self.browser.quit()
        cache.clear()

from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.chrome.options import Options
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.core.cache import cache

class BrowserTest(StaticLiveServerTestCase):
    def setUp(self):
        chrome_options = Options()
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--window-size=1920x1080")
        self.browser = WebDriver(chrome_options=chrome_options)
        self.browser.set_window_size(1024, 768)

    def tearDown(self):
        self.browser.quit()
        cache.clear()

Use headless chrome for functional test

Use headless chrome for functional test


import pydle
from .fixtures import with_client
from .mocks import Mock

@with_client()
def test_user_creation(server, client):
    client._create_user('WiZ')
    assert 'WiZ' in client.users
    assert client.users['WiZ']['nickname'] == 'WiZ'

@with_client()
def test_user_renaming(server, client):
    client._create_user('WiZ')
    client._rename_user('WiZ', 'jilles')

    assert 'WiZ' not in client.users
    assert 'jilles' in client.users
    assert client.users['jilles']['nickname'] == 'jilles'

@with_client()
def test_user_deletion(server, client):
    client._create_user('WiZ')
    client._destroy_user('WiZ')

    assert 'WiZ' not in client.users

import pydle
from .fixtures import with_client

@with_client()
def test_client_same_nick(server, client):
    assert client.is_same_nick('WiZ', 'WiZ')
    assert not client.is_same_nick('WiZ', 'jilles')
    assert not client.is_same_nick('WiZ', 'wiz')

@with_client()
def test_user_creation(server, client):
    client._create_user('WiZ')
    assert 'WiZ' in client.users
    assert client.users['WiZ']['nickname'] == 'WiZ'

@with_client()
def test_user_invalid_creation(server, client):
    client._create_user('irc.fbi.gov')
    assert 'irc.fbi.gov' not in client.users

@with_client()
def test_user_renaming(server, client):
    client._create_user('WiZ')
    client._rename_user('WiZ', 'jilles')

    assert 'WiZ' not in client.users
    assert 'jilles' in client.users
    assert client.users['jilles']['nickname'] == 'jilles'

@with_client()
def test_user_renaming_creation(server, client):
    client._rename_user('null', 'WiZ')

    assert 'WiZ' in client.users
    assert 'null' not in client.users

@with_client()
def test_user_deletion(server, client):
    client._create_user('WiZ')
    client._destroy_user('WiZ')

    assert 'WiZ' not in client.users

@with_client()
def test_user_synchronization(server, client):
    client._create_user('WiZ')
    client._sync_user('WiZ', { 'hostname': 'og.irc.developer' })

    assert client.users['WiZ']['hostname'] == 'og.irc.developer'

@with_client()
def test_user_synchronization_creation(server, client):
    client._sync_user('WiZ', {})
    assert 'WiZ' in client.users

@with_client()
def test_user_invalid_synchronization(server, client):
    client._sync_user('irc.fbi.gov', {})
    assert 'irc.fbi.gov' not in client.users

tests: Extend client:users tests to renaming and synchronization.


#!/usr/bin/env python
# coding=utf8

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

from .. import KeyValueStore

class MemcacheStore(KeyValueStore):
    def __contains__(self, key):
        try:
            return key in self.mc
        except TypeError:
            raise IOError('memcache implementation does not support '\
                          '__contains__')

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

    def _delete(self, key):
        self.mc.delete(key)

    def _get(self, key):
        rv = self.mc.get(key)
        if None == rv:
            raise KeyError(key)
        return rv

    def _get_file(self, key, file):
        file.write(self._get(key))

    def _open(self, key):
        return StringIO(self._get(key))

    def _put(self, key, data):
        self.mc.set(key, data)
        return key

    def _put_file(self, key, file):
        self.mc.set(key, file.read())
        return key

    def keys(self):
        raise IOError('Memcache does not support listing keys.')

    def iter_keys(self):
        raise IOError('Memcache does not support key iteration.')

#!/usr/bin/env python
# coding=utf8

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

from .. import KeyValueStore

class MemcacheStore(KeyValueStore):
    def __contains__(self, key):
        try:
            return key in self.mc
        except TypeError:
            raise IOError('memcache implementation does not support '\
                          '__contains__')

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

    def _delete(self, key):
        if not self.mc.delete(key):
            raise IOError('Error deleting key')

    def _get(self, key):
        rv = self.mc.get(key)
        if None == rv:
            raise KeyError(key)
        return rv

    def _get_file(self, key, file):
        file.write(self._get(key))

    def _open(self, key):
        return StringIO(self._get(key))

    def _put(self, key, data):
        if not self.mc.set(key, data):
            if len(data) >= 1024 * 1023:
                raise IOError('Failed to store data, probably too large. '\
                              'memcached limit is 1M')
            raise IOError('Failed to store data')
        return key

    def _put_file(self, key, file):
        return self._put(key, file.read())

    def keys(self):
        raise IOError('Memcache does not support listing keys.')

    def iter_keys(self):
        raise IOError('Memcache does not support key iteration.')

Check if putting/getting was actually successful.


import time
from prometheus_client import _INF

# TODO(korfuri): if python>3.3, use perf_counter() or monotonic().

def Time():
    """Returns some representation of the current time.

    This wrapper is meant to take advantage of a higher time
    resolution when available. Thus, its return value should be
    treated as an opaque object. It can be compared to the current
    time with TimeSince().
    """
    return time.time()

def TimeSince(t):
    """Compares a value returned by Time() to the current time.

    Returns:
      the time since t, in fractional seconds.
    """
    return time.time() - t

def PowersOf(logbase, count, lower=0, include_zero=True):
    """Returns a list of count powers of logbase (from logbase**lower)."""
    if not include_zero:
        return [logbase ** i for i in range(lower, count)] + [_INF]
    else:
        return [0] + [logbase ** i for i in range(lower, count)] + [_INF]

import time
from prometheus_client import _INF

# TODO(korfuri): if python>3.3, use perf_counter() or monotonic().

def Time():
    """Returns some representation of the current time.

    This wrapper is meant to take advantage of a higher time
    resolution when available. Thus, its return value should be
    treated as an opaque object. It can be compared to the current
    time with TimeSince().
    """
    return time.time()

def TimeSince(t):
    """Compares a value returned by Time() to the current time.

    Returns:
      the time since t, in fractional seconds.
    """
    return time.time() - t

def PowersOf(logbase, count, lower=0, include_zero=True):
    """Returns a list of count powers of logbase (from logbase**lower)."""
    if not include_zero:
        return [logbase ** i for i in range(lower, count+lower)] + [_INF]
    else:
        return [0] + [logbase ** i for i in range(lower, count+lower)] + [_INF]

Fix PowersOf to return `count` items, not "numbers up to base**count".


"""Remote control Samsung televisions via TCP/IP connection"""

from .remote import Remote

__title__        = "samsungctl"
__version__      = "0.6.0-git"
__url__          = "https://github.com/Ape/samsungctl"
__author__       = "Lauri Niskanen"
__author_email__ = "ape@ape3000.com"
__license__      = "MIT"

"""Remote control Samsung televisions via TCP/IP connection"""

from .remote import Remote

__title__        = "samsungctl"
__version__      = "0.6.0+1"
__url__          = "https://github.com/Ape/samsungctl"
__author__       = "Lauri Niskanen"
__author_email__ = "ape@ape3000.com"
__license__      = "MIT"

Use PEP 440 compatible local version identifier


import multiprocessing
import unicornhat
import importlib
import sys
import os

class State:
    ''' Handles the Unicorn HAT state'''

    def __init__(self):
        self.process = None

    def start_program(self, name, params={}):
        self.stop_program()

        if "brightness" in params:
            unicornhat.brightness(float(params["brightness"]))

        if "rotation" in params:
            unicornhat.rotation(int(params["rotation"]))

        program = importlib.import_module("app.programs." + name)
        self.process = multiprocessing.Process(target=program.run, args=(params,))
        self.process.start()

    def stop_program(self):
        if self.process is not None:
            self.process.terminate()
        unicornhat.show()

state = State()

import multiprocessing
import unicornhat
import importlib
import sys
import os

class State:
    ''' Handles the Unicorn HAT state'''

    def __init__(self):
        self._process = None

    def start_program(self, name, params={}):
        self.stop_program()

        if "brightness" in params:
            unicornhat.brightness(float(params["brightness"]))

        if "rotation" in params:
            unicornhat.rotation(int(params["rotation"]))

        program = importlib.import_module("app.programs." + name)
        self._process = multiprocessing.Process(target=program.run, args=(params,))
        self._process.start()

    def stop_program(self):
        if self._process is not None:
            self._process.terminate()
        unicornhat.show()

state = State()

Mark process member variable for internal use


"""microauth URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    # django-oauth-toolkit
    url(r'^oauth2/', include('oauth2_provider.urls', namespace='oauth2_provider')),
    url(r'^', include('apps.authentication.urls', namespace='microauth_authentication')),
    url(r'^api/', include('apps.api.urls', namespace='microauth_api')),
]

urlpatterns += staticfiles_urlpatterns()

"""microauth URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    # django-oauth-toolkit
    url(r'^oauth2/', include('oauth2_provider.urls', namespace='oauth2_provider')),
    url(r'^', include('apps.authentication.urls', namespace='microauth_authentication')),
    url(r'^api/', include('apps.api.urls', namespace='microauth_api')),
    url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}),

]

urlpatterns += staticfiles_urlpatterns()

Add a missing route leading to the login page.


import pandas
from .processor import CTDChemicalDiseaseProcessor, \
    CTDGeneDiseaseProcessor, CTDChemicalGeneProcessor

base_url = 'http://ctdbase.org/reports/'

urls = {
    'chemical_gene': base_url + 'CTD_chem_gene_ixns.tsv.gz',
    'chemical_disease': base_url + 'CTD_chemicals_diseases.tsv.gz',
    'gene_disease': base_url + 'CTD_genes_diseases.tsv.gz',
}

processors = {
    'chemical_gene': CTDChemicalGeneProcessor,
    'chemical_disease': CTDChemicalDiseaseProcessor,
    'gene_disease': CTDGeneDiseaseProcessor,
}

def process_from_web(subset):
    if subset not in urls:
        raise ValueError('%s is not a valid CTD subset.')
    df = pandas.read_csv(urls[subset], sep='\t', comment='#',
                         header=None)
    return process_dataframe(df)

def process_tsv(fname, subset):
    df = pandas.read_csv(fname, sep='\t', comment='#', header=None)
    return process_dataframe(df, subset)

def process_dataframe(df, subset):
    if subset not in processors:
        raise ValueError('%s is not a valid CTD subset.')
    cp = processors[subset](df)
    cp.extract_statements()
    return cp

import pandas
from .processor import CTDChemicalDiseaseProcessor, \
    CTDGeneDiseaseProcessor, CTDChemicalGeneProcessor

base_url = 'http://ctdbase.org/reports/'

urls = {
    'chemical_gene': base_url + 'CTD_chem_gene_ixns.tsv.gz',
    'chemical_disease': base_url + 'CTD_chemicals_diseases.tsv.gz',
    'gene_disease': base_url + 'CTD_genes_diseases.tsv.gz',
}

processors = {
    'chemical_gene': CTDChemicalGeneProcessor,
    'chemical_disease': CTDChemicalDiseaseProcessor,
    'gene_disease': CTDGeneDiseaseProcessor,
}

def process_from_web(subset, url=None):
    if subset not in urls:
        raise ValueError('%s is not a valid CTD subset.' % subset)
    url = url if url else urls[subset]
    return _process_url_or_file(url, subset)

def process_tsv(fname, subset):
    return _process_url_or_file(fname, subset)

def _process_url_or_file(path, subset):
    df = pandas.read_csv(path, sep='\t', comment='#',
                         header=None, dtype=str, keep_default_na=False)
    return process_dataframe(df, subset)

def process_dataframe(df, subset):
    if subset not in processors:
        raise ValueError('%s is not a valid CTD subset.' % subset)
    cp = processors[subset](df)
    cp.extract_statements()
    return cp

Refactor API to have single pandas load

Refactor API to have single pandas load


default_app_config = 'pucas.apps.PucasConfig'

__version_info__ = (0, 5, 1, None)

# Dot-connect all but the last. Last is dash-connected if not None.
__version__ = '.'.join([str(i) for i in __version_info__[:-1]])
if __version_info__[-1] is not None:
    __version__ += ('-%s' % (__version_info__[-1],))

default_app_config = 'pucas.apps.PucasConfig'

__version_info__ = (0, 6, 0, 'dev')

# Dot-connect all but the last. Last is dash-connected if not None.
__version__ = '.'.join([str(i) for i in __version_info__[:-1]])
if __version_info__[-1] is not None:
    __version__ += ('-%s' % (__version_info__[-1],))

Set develop version back to 0.6-dev

Set develop version back to 0.6-dev


# coding: utf8
from __future__ import unicode_literals

# Here we only want to include the absolute most common words. Otherwise,
# this list would get impossibly long for German – especially considering the
# old vs. new spelling rules, and all possible cases.

_exc = {
    "daß": "dass"
}

NORM_EXCEPTIONS = {}

for string, norm in _exc.items():
    NORM_EXCEPTIONS[string] = norm
    NORM_EXCEPTIONS[string.title()] = norm

# coding: utf8
from __future__ import unicode_literals

# Here we only want to include the absolute most common words. Otherwise,
# this list would get impossibly long for German – especially considering the
# old vs. new spelling rules, and all possible cases.

_exc = {
    "daß": "dass"
}

NORM_EXCEPTIONS = {}

for string, norm in _exc.items():
    NORM_EXCEPTIONS[string.title()] = norm

Revert "Also include lowercase norm exceptions"

This reverts commit 70f4e8adf37cfcfab60be2b97d6deae949b30e9e.


import unittest
from collections import namedtuple
# TODO: Move this code to a module so we don't depend on PYTHONPATH and that sort
# of ugliness.
from jarvis import convert_file_to_json, get_tags

JarvisSettings = namedtuple('JarvisSettings', ['tags_directory'])

class TestJarvis(unittest.TestCase):

    def setUp(self):
        with open('fixtures/test_log.md', 'r') as f:
            self.test_log = f.read()

        self.js = JarvisSettings('fixtures/tags')

    def test_convert_log(self):
        try:
            j = convert_file_to_json(self.test_log)
            # Version 0.2.0
            expected_keys = sorted(['version', 'created', 'body', 'tags',
                'occurred', 'author'])
            actual_keys = sorted(j.keys())
            self.assertListEqual(expected_keys, actual_keys)
        except Exception as e:
            self.fail("Unexpected error while parsing test_log")

    def test_get_tags(self):
        expected_tags = ['TestA', 'TestB&C']
        actual_tags = get_tags(self.js)
        self.assertListEqual(expected_tags, actual_tags)

if __name__ == "__main__":
    """
    To run:
    export PYTHONPATH="$HOME/oz/workspace/Jarvis/bin"

    python -m unittest test_jarvis.py
    """
    unittest.main()

import unittest
from collections import namedtuple
# TODO: Move this code to a module so we don't depend on PYTHONPATH and that sort
# of ugliness.
from jarvis import convert_file_to_json, get_tags

JarvisSettings = namedtuple('JarvisSettings', ['tags_directory'])

class TestJarvis(unittest.TestCase):

    def setUp(self):
        with open('fixtures/test_log.md', 'r') as f:
            self.test_log = f.read()

        self.js = JarvisSettings('fixtures/tags')

    def test_convert_file_to_json(self):
        try:
            j = convert_file_to_json(self.test_log)
            # Version 0.2.0
            expected_keys = sorted(['version', 'created', 'body', 'tags',
                'occurred', 'author'])
            actual_keys = sorted(j.keys())
            self.assertListEqual(expected_keys, actual_keys)
        except Exception as e:
            self.fail("Unexpected error while parsing test_log")

    def test_get_tags(self):
        expected_tags = ['TestA', 'TestB&C']
        actual_tags = get_tags(self.js)
        self.assertListEqual(expected_tags, actual_tags)

if __name__ == "__main__":
    """
    To run:
    export PYTHONPATH="$HOME/oz/workspace/Jarvis/bin"

    python -m unittest test_jarvis.py
    """
    unittest.main()

Change test method name to better match

Change test method name to better match


import datetime

from lino_extjs6.projects.mysite.settings import *

class Site(Site):
    project_name = 'extjs_demo'
    is_demo_site = True
    # ignore_dates_after = datetime.date(2019, 05, 22)
    the_demo_date = datetime.date(2015, 03, 12)

SITE = Site(globals())

SECRET_KEY = "1234"
# ALLOWED_HOSTS = ['*']
DEBUG = True

import datetime

from lino_extjs6.projects.mysite.settings import *

class Site(Site):
    project_name = 'extjs_demo'
    is_demo_site = True
    # ignore_dates_after = datetime.date(2019, 05, 22)
    the_demo_date = datetime.date(2015, 3, 12)

SITE = Site(globals())

SECRET_KEY = "1234"
# ALLOWED_HOSTS = ['*']
DEBUG = True

Add support to Py3 for datetime

Add support to Py3 for datetime