# -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import, unicode_literals
import itertools
from .util import trim

def render(item, **fields):
    """ Render the given item
    """
    if item is None:
        return ''
    elif isinstance(item, Renderer):
        return item.render(**fields)
    elif isinstance(item, list):
        return ''.join(render(e) for e in item)
    else:
        return str(item)

class Renderer(object):
    template = ''
    _counter = itertools.count()

    def __init__(self, template=None):
        if template is not None:
            self.template = template

    def counter(self):
        return next(self._counter)

    def render_fields(self, fields):
        pass

    def render(self, template=None, **fields):
        fields.update({k:v for k, v in vars(self).items() if not k.startswith('_')})
        self.render_fields(fields)
        if template is None:
            template = self.template
        fields = {k:render(v) for k, v in fields.items()}
        try:
            return trim(template).format(**fields)
        except KeyError as e:
            raise KeyError(str(e), type(self))

# -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import, unicode_literals
import itertools
from .util import trim

def render(item, **fields):
    """ Render the given item
    """
    if item is None:
        return ''
    elif isinstance(item, Renderer):
        return item.render(**fields)
    elif isinstance(item, list):
        return ''.join(render(e) for e in item)
    else:
        return str(item)

class Renderer(object):
    template = ''
    _counter = itertools.count()

    def __init__(self, template=None):
        if template is not None:
            self.template = template

    def counter(self):
        return next(self._counter)

    def render_fields(self, fields):
        pass

    def render(self, template=None, **kwargs):
        fields = ({k:v for k, v in vars(self).items() if not k.startswith('_')})

        override = self.render_fields(fields)
        if template is None:
            if override is not None:
                template = override
            else:
                template = self.template

        fields.update(kwargs)
        fields = {k:render(v) for k, v in fields.items()}
        try:
            return trim(template).format(**fields)
        except KeyError as e:
            raise KeyError(str(e), type(self))

Allow override of template through return value of render_fields.


from email.mime.image import MIMEImage

from django.contrib.staticfiles import finders
from .base import EmailBase

class PlatformEmailMixin:
    """
    Attaches the static file images/logo.png so it can be used in an html
    email.
    """
    def get_attachments(self):
        attachments = super().get_attachments()
        filename = finders.find('images/email_logo.png')
        if filename:
            f = open(filename, 'rb')
            logo = MIMEImage(f.read())
            logo.add_header('Content-ID', '<{}>'.format('logo'))
            return attachments + [logo]
        return attachments

class SyncEmailMixin(EmailBase):
    """Send Emails synchronously."""

    @classmethod
    def send(cls, object, *args, **kwargs):
        """Call dispatch immediately"""
        return cls().dispatch(object, *args, **kwargs)

from email.mime.image import MIMEImage

from django.contrib.staticfiles import finders
from .base import EmailBase

class PlatformEmailMixin:
    """
    Attaches the static file images/logo.png so it can be used in an html
    email.
    """
    def get_attachments(self):
        attachments = super().get_attachments()
        filename = (
            finders.find('images/email_logo.png')
            or finders.find('images/email_logo.svg')
        )
        if filename:
            f = open(filename, 'rb')
            logo = MIMEImage(f.read())
            logo.add_header('Content-ID', '<{}>'.format('logo'))
            return attachments + [logo]
        return attachments

class SyncEmailMixin(EmailBase):
    """Send Emails synchronously."""

    @classmethod
    def send(cls, object, *args, **kwargs):
        """Call dispatch immediately"""
        return cls().dispatch(object, *args, **kwargs)

Allow svg as email logo attachment

Allow svg as email logo attachment


import yaml
import copy

vacuum_switching_lengths = [0, 100, 500, 1000, 5000, 10000]
solvent_switching_lengths = [500, 1000, 5000, 10000, 20000, 50000]

use_sterics = [True, False]
geometry_divisions = [90, 180, 360, 720]

# Load in template yaml:
with open("rj_hydration.yaml", "r") as templatefile:
    template_yaml = yaml.load(templatefile)

# Set up vacuum simulations:
for phase in ['vacuum', 'explicit']:
    if phase == "vacuum":
        switching_lengths = vacuum_switching_lengths
    else:
        switching_lengths = solvent_switching_lengths

    for switching_length in switching_lengths:
        for sterics in use_sterics:
            for geometry_division in geometry_divisions:
                yaml_dict = copy.deepcopy(template_yaml)
                specification_file_prefix = "{}_{}ncmc_{}sterics_{}geometry".format(phase, switching_length, sterics, geometry_division)
                specification_filename = specification_file_prefix + ".yaml"
                yaml_dict['geometry_divisions'][phase] = geometry_division
                yaml_dict['use_sterics']['vacuum'] = sterics
                yaml_dict['ncmc_switching_times'][phase] = switching_length
                yaml_dict['phase'] = phase
                yaml_dict['output_filename'] = specification_file_prefix + ".nc"

                with open(specification_filename, 'w') as yam_outfile:
                    yaml.dump(yaml_dict, yam_outfile)

Add script to generate the yaml files for experiments


# -*- coding: utf-8 -*-
from django.db         import IntegrityError
from scrapy.exceptions import DropItem
from titlecase         import titlecase
from dateutil          import parser, relativedelta

class EventPipeline(object):
    def process_item(self, item, spider):
        item['titleRaw'] = item['title']
        item['title']    = titlecase(item['title'])

        if (item['end'] < item['start']):
            item['end'] = item['end'] + relativedelta.relativedelta(years =+ 1)

        try:
            item.save()
        except IntegrityError:
            raise DropItem('Duplicate: ' + item['venue'] + ' - ' + item['title'])
        return item

# -*- coding: utf-8 -*-
from django.db         import IntegrityError
from scrapy.exceptions import DropItem
from titlecase         import titlecase
from dateutil          import parser, relativedelta

class EventPipeline(object):
    def process_item(self, item, spider):
        item['titleRaw'] = item['title']
        item['title']    = titlecase(item['title'])

        if 'end' in item and 'start' in item:
            if (item['end'] < item['start']):
                item['end'] = item['end'] + relativedelta.relativedelta(years =+ 1)

        try:
            item.save()
        except IntegrityError:
            raise DropItem('Duplicate: ' + item['venue'] + ' - ' + item['title'])
        return item

Verify that event end and start keys exist before accessing.


from nose.tools import assert_equal, assert_true
from ...helpers import BaseApplicationTest

class TestApplication(BaseApplicationTest):
    def setup(self):
        super(TestApplication, self).setup()

    def test_analytics_code_should_be_in_javascript(self):
        res = self.client.get('/static/javascripts/application.js')
        assert_equal(200, res.status_code)
        assert_true(
            'GOVUK.analytics.trackPageview'
            in res.get_data(as_text=True))

from nose.tools import assert_equal, assert_true
from ...helpers import BaseApplicationTest

class TestApplication(BaseApplicationTest):
    def setup(self):
        super(TestApplication, self).setup()

    def test_analytics_code_should_be_in_javascript(self):
        res = self.client.get('/static/javascripts/application.js')
        assert_equal(200, res.status_code)
        assert_true(
            'trackPageview'
            in res.get_data(as_text=True))

Test for presence of analytics in minified JS

Minifying the JS shortens variable names wherever their scope makes it possible.
This means that `GOVUK.analytics.trackPageView` gets shortened to something like
`e.t.trackPageView`, and not in a predicatable way. Because the `trackPageView`
method could be used by indeterminable other scripts the compiler can't minify
it, so testing for just the presence of its name is safe.


from rest_framework import permissions

# TODO Add write permission for 1%CREW / Assitants.
class IsAuthorOrReadOnly(permissions.BasePermission):
    """
    Custom permission to only allow author of an object to edit it.
    """

    def has_permission(self, request, view, obj=None):
        # Skip the check unless this is an object-level test.
        if obj is None:
            return True

        # Read permissions are allowed to any request.
        if request.method in permissions.SAFE_METHODS:
            return True

        # Write permissions are only allowed to the author of the reaction.
        return obj.author == request.user

from rest_framework import permissions

# TODO Add write permission for 1%CREW / Assistants.
class IsAuthorOrReadOnly(permissions.BasePermission):
    """
    Custom permission to only allow author of an object to edit it.
    """

    def has_permission(self, request, view, obj=None):
        # Skip the check unless this is an object-level test.
        if obj is None:
            return True

        # Read permissions are allowed to any request.
        if request.method in permissions.SAFE_METHODS:
            return True

        # Write permissions are only allowed to the author of the reaction.
        return obj.author == request.user

Fix spelling mistake in comment.

Fix spelling mistake in comment.


from django.conf.urls.defaults import patterns, include, url

from boundaryservice.views import *

urlpatterns = patterns('',
    url(r'^boundary-set/$', BoundarySetListView.as_view(), name='boundaryservice_set_list'),
    url(r'^boundary-set/(?P<slug>[\w_-]+)/$', BoundarySetDetailView.as_view(), name='boundaryservice_set_detail'),
    url(r'^boundary/$', BoundaryListView.as_view(), name='boundaryservice_boundary_list'),
    url(r'^boundary/(?P<geo_field>shape|simple_shape|centroid)$', BoundaryListView.as_view()),
    url(r'^boundary/(?P<set_slug>[\w_-]+)/$', BoundaryListView.as_view(), name='boundaryservice_boundary_list'),
    url(r'^boundary/(?P<set_slug>[\w_-]+)/(?P<geo_field>shape|simple_shape|centroid)$', BoundaryListView.as_view()),
    url(r'^boundary/(?P<set_slug>[\w_-]+)/(?P<slug>[\w_-]+)/$', BoundaryDetailView.as_view(), name='boundaryservice_boundary_detail'),
    url(r'^boundary/(?P<set_slug>[\w_-]+)/(?P<slug>[\w_-]+)/(?P<geo_field>shape|simple_shape|centroid)$', BoundaryGeoDetailView.as_view()),
)

from django.conf.urls.defaults import patterns, include, url

from boundaryservice.views import *

urlpatterns = patterns('',
    url(r'^boundary-sets/$', BoundarySetListView.as_view(), name='boundaryservice_set_list'),
    url(r'^boundary-sets/(?P<slug>[\w_-]+)/$', BoundarySetDetailView.as_view(), name='boundaryservice_set_detail'),
    url(r'^boundaries/$', BoundaryListView.as_view(), name='boundaryservice_boundary_list'),
    url(r'^boundaries/(?P<geo_field>shape|simple_shape|centroid)$', BoundaryListView.as_view()),
    url(r'^boundaries/(?P<set_slug>[\w_-]+)/$', BoundaryListView.as_view(), name='boundaryservice_boundary_list'),
    url(r'^boundaries/(?P<set_slug>[\w_-]+)/(?P<geo_field>shape|simple_shape|centroid)$', BoundaryListView.as_view()),
    url(r'^boundaries/(?P<set_slug>[\w_-]+)/(?P<slug>[\w_-]+)/$', BoundaryDetailView.as_view(), name='boundaryservice_boundary_detail'),
    url(r'^boundaries/(?P<set_slug>[\w_-]+)/(?P<slug>[\w_-]+)/(?P<geo_field>shape|simple_shape|centroid)$', BoundaryGeoDetailView.as_view()),
)

Use plural names for resource types in URLs


import os
from kokki import *

Package("erlang")

Package("mercurial",
    provider = "kokki.providers.package.easy_install.EasyInstallProvider")

Script("install-busket",
    not_if = lambda:os.path.exists(env.config.busket.path),
    cwd = "/usr/local/src",
    code = (
        "git clone git://github.com/samuel/busket.git busket\n"
        "cd busket\n"
        "make release\n"
        "mv rel/busket {install_path}\n"
    ).format(install_path=env.config.busket.path)
)

import os
from kokki import *

Package("erlang")
# ubuntu's erlang is a bit messed up.. remove the man link
File("/usr/lib/erlang/man",
    action = "delete")

Package("mercurial",
    provider = "kokki.providers.package.easy_install.EasyInstallProvider")

Script("install-busket",
    not_if = lambda:os.path.exists(env.config.busket.path),
    cwd = "/usr/local/src",
    code = (
        "git clone git://github.com/samuel/busket.git busket\n"
        "cd busket\n"
        "make release\n"
        "mv rel/busket {install_path}\n"
    ).format(install_path=env.config.busket.path)
)

Remove man link for erlang in ubuntu

Remove man link for erlang in ubuntu


import os

import pkg_resources

from mopidy import config, ext

__version__ = pkg_resources.get_distribution("Mopidy-NAD").version

class Extension(ext.Extension):
    dist_name = "Mopidy-NAD"
    ext_name = "nad"
    version = __version__

    def get_default_config(self):
        conf_file = os.path.join(os.path.dirname(__file__), "ext.conf")
        return config.read(conf_file)

    def get_config_schema(self):
        schema = super().get_config_schema()
        schema["port"] = config.String()
        schema["source"] = config.String(optional=True)
        schema["speakers-a"] = config.Boolean(optional=True)
        schema["speakers-b"] = config.Boolean(optional=True)
        return schema

    def setup(self, registry):
        from mopidy_nad.mixer import NadMixer

        registry.add("mixer", NadMixer)

import pathlib

import pkg_resources

from mopidy import config, ext

__version__ = pkg_resources.get_distribution("Mopidy-NAD").version

class Extension(ext.Extension):
    dist_name = "Mopidy-NAD"
    ext_name = "nad"
    version = __version__

    def get_default_config(self):
        return config.read(pathlib.Path(__file__).parent / "ext.conf")

    def get_config_schema(self):
        schema = super().get_config_schema()
        schema["port"] = config.String()
        schema["source"] = config.String(optional=True)
        schema["speakers-a"] = config.Boolean(optional=True)
        schema["speakers-b"] = config.Boolean(optional=True)
        return schema

    def setup(self, registry):
        from mopidy_nad.mixer import NadMixer

        registry.add("mixer", NadMixer)

Use pathlib to read ext.conf

Use pathlib to read ext.conf


from abc import ABCMeta, abstractmethod

class Consensus(metaclass=ABCMeta):
    """
    An interface for defining a consensus protocol.
    The 'propose' and 'decide' methods need to be defined
    """

    @abstractmethod
    def propose(self, message):
        #raise NotImplementedError("Method 'propose' needs to be implemented")
        pass

    @abstractmethod
    def decide(self):
        #raise NotImplementedError("Method 'decide' needs to be implemented")
        pass

Add abstract Consensus protocol class

Add abstract Consensus protocol class


# -*- coding: utf-8 -*-
"""
    test_pops
    ~~~~~~~~~

    tests for population code
"""
from abm import pops
from abm.entities import Task
import pytest

from scipy.stats.distributions import uniform
import numpy as np

@pytest.fixture
def basicenv():
    return pops.Environment()

@pytest.mark.unit
def test_distribute_awards(basicenv):
    class MockEntity(object):
        total_award = 0

        def award(self, amount):
            self.total_award += amount

    basicenv.population = []
    for i in range(6):
        basicenv.population.append(MockEntity())
    basicenv.path = [1, 2, 3, 2, 4]
    task = Task(4)
    basicenv._distribute_awards(task)

    observed = [
        x.total_award for x in basicenv.population
    ]
    expected = ([0] + [.25] * 3 + [0, 0])

    assert observed == expected
    assert np.isclose(sum(observed), .75)

    basicenv.path = [0, 4]
    basicenv._distribute_awards(task)
    assert basicenv.population[0].total_award == 1.

    basicenv.path = [5] * (basicenv.path_cutoff + 1)
    basicenv._distribute_awards(task)
    assert basicenv.population[5].total_award == -.05

Add newline at the end of the file for my sanity.


from comics.aggregator.crawler import CrawlerBase, CrawlerImage
from comics.core.comic_data import ComicDataBase

class ComicData(ComicDataBase):
    name = "Colleges Roomies from Hell"
    language = "en"
    url = "http://www.crfh.net/"
    start_date = "1999-01-01"
    rights = "Maritza Campos"

class Crawler(CrawlerBase):
    history_capable_date = "1999-01-01"
    time_zone = "America/Merida"

    def crawl(self, pub_date):
        page_url = "http://www.crfh.net/d2/%s.html" % (
            pub_date.strftime("%Y%m%d"),
        )
        page = self.parse_page(page_url)
        url = page.src('img[src*="crfh%s"]' % pub_date.strftime("%Y%m%d"))
        url = url.replace("\n", "")
        return CrawlerImage(url)

from comics.aggregator.crawler import CrawlerBase, CrawlerImage
from comics.core.comic_data import ComicDataBase

class ComicData(ComicDataBase):
    name = "Colleges Roomies from Hell"
    language = "en"
    url = "http://www.crfh.net/"
    start_date = "1999-01-01"
    rights = "Maritza Campos"

class Crawler(CrawlerBase):
    history_capable_date = "1999-01-01"
    time_zone = "America/Merida"

    def crawl(self, pub_date):
        page_url = "http://www.crfh.net/d/%s.html" % (
            pub_date.strftime("%Y%m%d"),
        )
        page = self.parse_page(page_url)
        url = page.src('img[src*="crfh%s"]' % pub_date.strftime("%Y%m%d"))
        url = url.replace("\n", "")
        return CrawlerImage(url)

Update "Colleges Roomies from Hell" after site change


# Copyright (c) 2009-2010 gocept gmbh & co. kg
# See also LICENSE.txt

import zeit.content.cp.browser.blocks.teaser
import zeit.content.cp.interfaces
import zeit.edit.browser.block
import zeit.edit.browser.view
import zope.formlib.form

class ViewletManager(zeit.edit.browser.block.BlockViewletManager):

    @property
    def css_class(self):
        classes = super(ViewletManager, self).css_class
        return ' '.join(['editable-area', classes])

class EditProperties(zeit.content.cp.browser.blocks.teaser.EditLayout):

    interface = zeit.content.cp.interfaces.IArea
    layout_prefix = 'teaserbar'  # XXX should be area
    layouts = ()

    form_fields = zope.formlib.form.Fields()  # XXX implement me

class EditCommon(zeit.edit.browser.view.EditBox):

    form_fields = zope.formlib.form.Fields(
        zeit.content.cp.interfaces.IArea).select(
        'supertitle', 'teaserText', 'background_color')
    form_fields['background_color'].custom_widget = (
        zeit.cms.browser.widget.ColorpickerWidget)

# Copyright (c) 2009-2010 gocept gmbh & co. kg
# See also LICENSE.txt

import zeit.content.cp.browser.blocks.teaser
import zeit.content.cp.interfaces
import zeit.edit.browser.block
import zeit.edit.browser.view
import zope.formlib.form

class ViewletManager(zeit.edit.browser.block.BlockViewletManager):

    @property
    def css_class(self):
        classes = super(ViewletManager, self).css_class
        return ' '.join(['editable-area', classes])

class EditProperties(zeit.content.cp.browser.blocks.teaser.EditLayout):

    interface = zeit.content.cp.interfaces.IArea
    layout_prefix = 'teaserbar'  # XXX should be area
    layouts = ()

class EditCommon(zeit.edit.browser.view.EditBox):

    form_fields = zope.formlib.form.Fields(
        zeit.content.cp.interfaces.IArea).select(
        'supertitle', 'teaserText', 'background_color')
    form_fields['background_color'].custom_widget = (
        zeit.cms.browser.widget.ColorpickerWidget)

Remove field that has now the same default implementation on it's super class.


#!/usr/bin/env python3

class Triangle:
    """A class structure for storing and minipulating a triangle.

    The trianlge is represented as a 3-tuple of points. Each point is
    represented as a 2-tuple of floats, the first element being the
    x-coordinate and the second element being the y-coordinate.

    Several useful operations can be applied to a triangle such as, rotate,
    translate, split across altitude, and rectanglify.

    The Triangle (and underlying tuple) should be treated as an immutable
    data structure. All methods return a new triangle and do not modify the
    existing one."""

    def __init__(self, tpl):
        """tpl is a 3-tuple of coordinates"""
        self.points = tpl

class Shape:
    """A class structure for representing and minipulating arbitary shapes.

    A shape is defines as a list of triangles (see Triangle). Several
    operations can be applied to a shape such as rotation, translation and
    splitting the shape into two.

    This object should be treated as an immutable data structure. All methods
    return new shapes and do not modify the existing one."""

    def __init__(self, triangle_list):
        """triangle_list is a list of triangles"""
        self.triangles = triangle_list

#!/usr/bin/env python3

import math

class Triangle:
    """A class structure for storing and minipulating a triangle.

    The trianlge is represented as a 3-tuple of points. Each point is
    represented as a 2-tuple of floats, the first element being the
    x-coordinate and the second element being the y-coordinate.

    Several useful operations can be applied to a triangle such as, rotate,
    translate, split across altitude, and rectanglify.

    The Triangle (and underlying tuple) should be treated as an immutable
    data structure. All methods return a new triangle and do not modify the
    existing one."""

    def __init__(self, tpl):
        """tpl is a 3-tuple of coordinates"""
        self.points = tpl

    def rotate(self, pivot, rangle):
        """Return a new triangle rotate clockwise (by angle) around pivot.

        pivot -- A coordinate pair
        rangle -- The angle to rotate by in radians"""
        new_points = list()
        px, py = pivot
        for x, y in self.points:
            dx, dy = x - px, y - py
            current_angle = math.atan2(dy, dx)
            total_angle = current_angle + rangle
            r = math.hypot(dx, dy)
            nx = r*math.cos(total_angle) + px
            ny = r*math.sin(total_angle) + py
            new_points.append((nx, ny))
        return Triangle(tuple(new_points))

class Shape:
    """A class structure for representing and minipulating arbitary shapes.

    A shape is defines as a list of triangles (see Triangle). Several
    operations can be applied to a shape such as rotation, translation and
    splitting the shape into two.

    This object should be treated as an immutable data structure. All methods
    return new shapes and do not modify the existing one."""

    def __init__(self, triangle_list):
        """triangle_list is a list of triangles"""
        self.triangles = triangle_list

Add method to rotate triangle

Add method to rotate triangle


# coding: utf-8
import random
import urlparse
from string import ascii_letters, digits
from urllib import urlencode

# From http://tools.ietf.org/html/rfc6750#section-2.1
BEARER_TOKEN_CHARSET = ascii_letters + digits + '-._~+/'

def random_hash(length):
  return ''.join(random.sample(BEARER_TOKEN_CHARSET, length))

def random_hash_generator(length):
  return lambda: random_hash(length)

def update_parameters(url, parameters):
  """ Updates a URL's existing GET parameters.

  @url: a URL string.
  @parameters: a dictionary of parameters, {string:string}.
  """
  parsed_url = urlparse(url)

  query_parameters = urlparse.parse_qsl(parsed_url.query)
  parsed_url.query = urlencode(query_parameters + parameters.items())

  return urlparse.urlunparse(parsed_url)

# coding: utf-8
import random
import urlparse
from string import ascii_letters, digits
from urllib import urlencode

# From http://tools.ietf.org/html/rfc6750#section-2.1
BEARER_TOKEN_CHARSET = ascii_letters + digits + '-._~+/'

def random_hash(length):
  return ''.join(random.sample(BEARER_TOKEN_CHARSET, length))

def random_hash_generator(length):
  return lambda: random_hash(length)

def update_parameters(url, parameters):
  """ Updates a URL's existing GET parameters.

  @url: a URL string.
  @parameters: a dictionary of parameters, {string:string}.
  """
  parsed_url = urlparse.urlparse(url)
  existing_query_parameters = urlparse.parse_qsl(parsed_url.query)
  # Read http://docs.python.org/2/library/urlparse.html#urlparse.urlparse
  # if this is confusing.
  return urlparse.urlunparse((
      parsed_url.scheme,
      parsed_url.netloc,
      parsed_url.path,
      parsed_url.params,
      urlencode(existing_query_parameters + parameters.items()),
      parsed_url.fragment
    ))

Fix query string update helper.

Fix query string update helper.


Config(
    net=Config(attention_type='hybrid2',
               shift_predictor_dims=[100],
               max_left=10,
               max_right=100),
    initialization=[
        ("/recognizer", "rec_weights_init", "IsotropicGaussian(0.1)"),
        ("/recognizer/generator/att_trans/hybrid_att/loc_att",
         "weights_init", "IsotropicGaussian(0.01)"),
        ("/recognizer/generator/att_trans/hybrid_att/loc_att",
         "biases_init", "IsotropicGaussian(5.0)")],
    data=Config(normalization="norm.pkl"))

Config(
    net=Config(dec_transition='GatedRecurrent',
               enc_transition='GatedRecurrent',
               attention_type='hybrid2',
               shift_predictor_dims=[100],
               max_left=10,
               max_right=100),
    initialization=[
        ("/recognizer", "rec_weights_init", "IsotropicGaussian(0.1)"),
        ("/recognizer/generator/att_trans/hybrid_att/loc_att",
         "weights_init", "IsotropicGaussian(0.001)"),
        ("/recognizer/generator/att_trans/hybrid_att/loc_att",
         "biases_init", "IsotropicGaussian(5.0)")],
    data=Config(normalization="norm.pkl"))

Fix hybrid2, but it is still no use

Fix hybrid2, but it is still no use


#-*- coding: utf-8 -*-
import pandas as pd
import pandas_datareader.data as web
import datetime
import config
import os
import re
import pickle

def get_file_path(code):
  return os.path.join(config.DATA_PATH, 'data', code + '.pkl')

def download(code, year1, month1, day1, year2, month2, day2):
  start = datetime.datetime(year1, month1, day1)
  end = datetime.datetime(year2, month2, day2)
  df = web.DataReader('%s.KS' % code, 'yahoo', start, end)
  save(code, df)

  return df

def load(code):
  try:
    return pd.read_pickle(code)
  except:
    pass

  return None

def save(code, df):
  df.to_pickle(code)

def dump(code, df):
  with open(get_file_path(code), 'wb') as handle:
    pickle.dump(df, handle)

#-*- coding: utf-8 -*-
import pandas as pd
import pandas_datareader.data as web
import datetime
import config
import os
import re
import pickle

def get_file_path(code):
  if not os.path.exists(config.DATA_PATH):
    try:
      os.makedirs(config.DATA_PATH)
    except:
      pass

  return os.path.join(config.DATA_PATH, 'data', code + '.pkl')

def download(code, year1, month1, day1, year2, month2, day2):
  start = datetime.datetime(year1, month1, day1)
  end = datetime.datetime(year2, month2, day2)
  df = web.DataReader('%s.KS' % code, 'yahoo', start, end)
  save(code, df)

  return df

def load(code):
  try:
    return pd.read_pickle(code)
  except:
    pass

  return None

def save(code, df):
  df.to_pickle(code)

def dump(code, df):
  with open(get_file_path(code), 'wb') as handle:
    pickle.dump(df, handle)

Fix the FileNotFoundError when data director is not exist


# -*- coding: utf-8 -*-
from flask import Flask

DB_SCHEMA = """
DROP TABLE IF EXISTS entries;
CREATE TABLE entries (
    id serial PRIMARY KEY,
    title VARCHAR(127) NOT NULL,
    text VARCHAR(10000) NOT NULL,
    created TIMESTAMP NOT NULL,
    )
"""

app = FLask(__name__)

@app.route('/')
def hello():
    return u'Hello World!'

if __name__ == '__main__':
    app.run(debug=True)

# -*- coding: utf-8 -*-
from flask import Flask
import os
import psycopg2
from contextlib import closing

DB_SCHEMA = """
DROP TABLE IF EXISTS entries;
CREATE TABLE entries (
    id serial PRIMARY KEY,
    title VARCHAR(127) NOT NULL,
    text VARCHAR(10000) NOT NULL,
    created TIMESTAMP NOT NULL
    )
"""

app = Flask(__name__)

app.config['DATABASE'] = os.environ.get(
    'DATABASE_URL', 'dbname=web_blog user=store'
)

def connect_db():
    """Return a connection to the database"""
    return psycopg2.connect(app.config['DATABASE'])

def init_db():
    """Initialize the database
    WARNING: executing this function will drop existing tables.
    """
    with closing(connect_db()) as db:
        db.cursor().execute(DB_SCHEMA)
        db.commit()

@app.route('/')
def hello():
    return u'Hello World!'

if __name__ == '__main__':
    app.run(debug=True)

Add functionality to connect to DB server and initialize our blog's database


import logging

__version__ = "0.19.1rc2"
version = tuple(__version__.split('.'))

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

import logging

__version__ = "0.19.1rc3"
version = tuple(__version__.split('.'))

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

Bump version 0.19.1rc2 --> 0.19.1rc3

Bump version 0.19.1rc2 --> 0.19.1rc3

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


# -*- coding: utf-8 -*-
#
# Copyright © 2017 Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see LICENSE.txt for details)

# noqa: D104

Add copyright notice, skip docstring checks


from dateutil.parser import parse

class Domain(object):
    def __init__(self, data):
        self.name = data['name']
        self.auto_renew = (data['f_auto_renew'] == 'Y')
        self.expiry_date = parse(data['expiredate']).date()

    @property
    def tld(self):
        return self.name.rsplit('.', 1)[1]

from dateutil.parser import parse

class Domain(object):
    def __init__(self, data):
        self.name = data['name']
        self.auto_renew = (data['f_auto_renew'] == 'Y')
        self.expiry_date = parse(data['expiredate']).date()

    @property
    def tld(self):
        return self.name.split('.')[-1]

Use more concise way to get tld

Use more concise way to get tld


# -*- coding: utf-8 -*-
#
# (c) 2014 Bjoern Ricks <bjoern.ricks@gmail.com>
#
# See LICENSE comming with the source of 'trex' for details.
#

from rest_framework.serializers import (
    HyperlinkedModelSerializer, HyperlinkedIdentityField,
)

from trex.models.project import Project, Entry

class ProjectSerializer(HyperlinkedModelSerializer):

    class Meta:
        model = Project
        fields = ("url", "id", "name", "description", "active", "created")

class ProjectDetailSerializer(HyperlinkedModelSerializer):

    entries = HyperlinkedIdentityField(view_name="project-entries-list")

    class Meta:
        model = Project
        fields = ("id", "name", "description", "active", "created", "entries")

class EntryProjectSerializer(HyperlinkedModelSerializer):

    class Meta:
        model = Project
        fields = ("url", "id", "name")

class EntryDetailSerializer(HyperlinkedModelSerializer):

    project = EntryProjectSerializer()

    class Meta:
        model = Entry
        fields = ("url", "id", "date", "duration", "description", "state",
                  "user_abbr", "user", "created", "project")

# -*- coding: utf-8 -*-
#
# (c) 2014 Bjoern Ricks <bjoern.ricks@gmail.com>
#
# See LICENSE comming with the source of 'trex' for details.
#

from rest_framework.serializers import (
    HyperlinkedModelSerializer, HyperlinkedIdentityField,
)

from trex.models.project import Project, Entry, Tag

class ProjectSerializer(HyperlinkedModelSerializer):

    class Meta:
        model = Project
        fields = ("url", "id", "name", "description", "active", "created")

class ProjectDetailSerializer(HyperlinkedModelSerializer):

    entries = HyperlinkedIdentityField(view_name="project-entries-list")

    class Meta:
        model = Project
        fields = ("id", "name", "description", "active", "created", "entries")

class EntryTagsSerializer(HyperlinkedModelSerializer):

    class Meta:
        model = Tag
        fields = ("url", "id", "name")

class EntryProjectSerializer(HyperlinkedModelSerializer):

    class Meta:
        model = Project
        fields = ("url", "id", "name")

class EntryDetailSerializer(HyperlinkedModelSerializer):

    tags = EntryTagsSerializer(many=True)
    project = EntryProjectSerializer()

    class Meta:
        model = Entry
        fields = ("url", "id", "date", "duration", "description", "state",
                  "user_abbr", "user", "created", "project", "tags")

Add EntryTagsSerializer for returning tags of an Entry


from binary_descriptor import BinaryDescriptor
import amitgroup as ag
import amitgroup.features

# TODO: This is temporarily moved
#@BinaryDescriptor.register('edges')
class EdgeDescriptor(BinaryDescriptor):
    """
    Binary descriptor based on edges.

    The parameters are similar to :func:`amitgroup.features.bedges`.

    Parameters
    ----------
    polarity_sensitive : bool
        If True, the polarity of the edges will matter. If False, then the direction of edges will not matter.
    k : int
        See :func:`amitgroup.features.bedges`.
    radius : int
        Radius of edge spreading. See :func:`amitgroup.features.bedges`.
    min_contrast : float
        See :func:`amitgroup.features.bedges`.
    """
    def __init__(self, polarity_sensitive=True, k=5, radius=1, min_contrast=0.1):
        self.settings = {}
        # Change this
        self.settings['contrast_insensitive'] = polarity_sensitive
        self.settings['k'] = k
        self.settings['radius'] = radius
        self.settings['min_contrast'] = min_contrast
        self.settings.update(settings)

    def extract_features(self, img):
        #return ag.features.bedges_from_image(img, **self.settings)
        return ag.features.bedges(img, **self.settings)

    def save_to_dict(self):
        return self.settings

    @classmethod
    def load_from_dict(cls, d):
        return cls(d)

EdgeDescriptor = BinaryDescriptor.register('edges')(EdgeDescriptor)

from binary_descriptor import BinaryDescriptor
import amitgroup as ag
import amitgroup.features

# TODO: This is temporarily moved
#@BinaryDescriptor.register('edges')
class EdgeDescriptor(BinaryDescriptor):
    """
    Binary descriptor based on edges.

    The parameters are similar to :func:`amitgroup.features.bedges`.

    Parameters
    ----------
    polarity_sensitive : bool
        If True, the polarity of the edges will matter. If False, then the direction of edges will not matter.
    k : int
        See :func:`amitgroup.features.bedges`.
    radius : int
        Radius of edge spreading. See :func:`amitgroup.features.bedges`.
    min_contrast : float
        See :func:`amitgroup.features.bedges`.
    """
    def __init__(self, polarity_sensitive=True, k=5, radius=1, min_contrast=0.1):
        self.settings = {}
        # Change this
        self.settings['contrast_insensitive'] = not polarity_sensitive
        self.settings['k'] = k
        self.settings['radius'] = radius
        self.settings['min_contrast'] = min_contrast
        self.settings.update(settings)

    def extract_features(self, img):
        #return ag.features.bedges_from_image(img, **self.settings)
        return ag.features.bedges(img, **self.settings)

    def save_to_dict(self):
        return self.settings

    @classmethod
    def load_from_dict(cls, d):
        return cls(d)

EdgeDescriptor = BinaryDescriptor.register('edges')(EdgeDescriptor)

Fix bug in EdgeDescriptor setting.

Fix bug in EdgeDescriptor setting.


from django.conf.urls import patterns, url

from maker.views import (RegsitrationView,
                            AuthenticationView,
                            MakerSelfView,
                            MakerProfileView,
                            ResetPasswordRequestView,
                            ResetPasswordProcessView,
                            ChangePasswordView,
                            EmailChangeRequestView,
                            EmailChangeProcessView)

urlpatterns = patterns(
    '',
    url(
        r'^/?$',
        MakerSelfView.as_view(),
        name='maker-self'
    ),
    url(
        r'^register/?$',
        RegsitrationView.as_view(),
        name='registration'
    ),
    url(
        r'^authenticate/?$',
        AuthenticationView.as_view(),
        name='authentication'
    ),
    url(
        r'^password/?$',
        ChangePasswordView.as_view(),
        name='change-password'
    ),
    url(
        r'^password/reset/?$',
        ResetPasswordRequestView.as_view(),
        name='password-reset'
    ),
    url(
        r'^password/reset/update?$',
        ResetPasswordProcessView.as_view(),
        name='password-reset-process'
    ),
    url(
        r'^email/?$',
        EmailChangeRequestView.as_view(),
        name='email-change-request'
    ),
    url(
        r'^email/update?$',
        EmailChangeProcessView.as_view(),
        name='email-change-process'
    ),
    url(
        r'^(?P<pk>[0-9]+)/?$',
        MakerProfileView.as_view(),
        name='maker-profile-view'
    )
)

from django.conf.urls import patterns, url

from maker.views import (RegsitrationView,
                            AuthenticationView,
                            MakerSelfView,
                            MakerProfileView,
                            ResetPasswordRequestView,
                            ResetPasswordProcessView,
                            ChangePasswordView,
                            EmailChangeRequestView,
                            EmailChangeProcessView)

urlpatterns = patterns(
    '',
    url(
        r'^self/?$',
        MakerSelfView.as_view(),
        name='maker-self'
    ),
    url(
        r'^register/?$',
        RegsitrationView.as_view(),
        name='registration'
    ),
    url(
        r'^authenticate/?$',
        AuthenticationView.as_view(),
        name='authentication'
    ),
    url(
        r'^password/?$',
        ChangePasswordView.as_view(),
        name='change-password'
    ),
    url(
        r'^password/reset/?$',
        ResetPasswordRequestView.as_view(),
        name='password-reset'
    ),
    url(
        r'^password/reset/update?$',
        ResetPasswordProcessView.as_view(),
        name='password-reset-process'
    ),
    url(
        r'^email/?$',
        EmailChangeRequestView.as_view(),
        name='email-change-request'
    ),
    url(
        r'^email/update?$',
        EmailChangeProcessView.as_view(),
        name='email-change-process'
    ),
    url(
        r'^(?P<pk>[0-9]+)/?$',
        MakerProfileView.as_view(),
        name='maker-profile-view'
    )
)

Change maker self URL name

Change maker self URL name


import pytest

from cosmo_tester.framework.test_hosts import Hosts, get_image
from cosmo_tester.test_suites.snapshots import get_multi_tenant_versions_list

@pytest.fixture(scope='function', params=get_multi_tenant_versions_list())
def hosts(request, ssh_key, module_tmpdir, test_config, logger):
    hosts = Hosts(
        ssh_key, module_tmpdir,
        test_config, logger, request,
        number_of_instances=4,
    )

    hosts.instances[0] = get_image(request.param, test_config)
    hosts.instances[1] = get_image('master', test_config)
    hosts.instances[2] = get_image('centos', test_config)
    hosts.instances[3] = get_image('centos', test_config)

    win_vm = hosts.instances[2]
    win_vm.prepare_for_windows('windows_2012')

    lin_vm = hosts.instances[3]
    lin_vm.image_name = test_config.platform['centos_7_image']
    lin_vm.username = test_config['test_os_usernames']['centos_7']

    hosts.create()

    try:
        yield hosts
    finally:
        hosts.destroy()

import pytest

from cosmo_tester.framework.test_hosts import Hosts, get_image
from cosmo_tester.test_suites.snapshots import get_multi_tenant_versions_list

@pytest.fixture(scope='function', params=get_multi_tenant_versions_list())
def hosts(request, ssh_key, module_tmpdir, test_config, logger):
    hosts = Hosts(
        ssh_key, module_tmpdir,
        test_config, logger, request,
        number_of_instances=4,
    )

    hosts.instances[0] = get_image(request.param, test_config)
    hosts.instances[1] = get_image('master', test_config)
    hosts.instances[2] = get_image('centos', test_config)
    hosts.instances[3] = get_image('centos', test_config)

    win_vm = hosts.instances[2]
    win_vm.prepare_for_windows('windows_2012')

    lin_vm = hosts.instances[3]
    lin_vm.image_name = test_config.platform['centos_7_image']
    lin_vm.username = test_config['test_os_usernames']['centos_7']

    hosts.create()

    try:
        if request.param in ['5.0.5', '5.1.0']:
            old_mgr = hosts.instances[0]
            old_mgr.wait_for_manager()
            old_mgr.run_command('mv /etc/cloudify/ssl/rabbitmq{_,-}cert.pem',
                                use_sudo=True)
            old_mgr.run_command('mv /etc/cloudify/ssl/rabbitmq{_,-}key.pem',
                                use_sudo=True)
            old_mgr.run_command(
                'chown rabbitmq. /etc/cloudify/ssl/rabbitmq-*', use_sudo=True)
            old_mgr.run_command('systemctl restart cloudify-rabbitmq',
                                use_sudo=True)
        yield hosts
    finally:
        hosts.destroy()

Use correct rabbit certs on old IP setter


# pylint: disable=missing-docstring, invalid-name, import-error
import pandas as pd

from mltils.encoders import InfrequentValueEncoder

def test_infrequent_value_encoder_1():
    ive = InfrequentValueEncoder()
    assert ive is not None

def test_infrequent_value_encoder_2():
    df = pd.DataFrame({'A': ['a', 'a', 'b', 'b', 'c']})
    ive = InfrequentValueEncoder(thrshld=1, str_rpl='ifq')
    encoded = ive.fit_transform(df)
    expected = pd.DataFrame({'A': ['a', 'a', 'b', 'b', 'ifq']})
    assert expected.equals(encoded)

Add unit tests for InfrequentValueEncoder


from bluebottle.quotes.models import Quote
from bluebottle.slides.models import Slide
from bluebottle.statistics.models import Statistic
from bluebottle.projects.models import Project

class HomePage(object):
    """
    Instead of serving all the objects separately we combine
    Slide, Quote and Stats into a dummy object
    """
    def get(self, language):
        self.id = language
        self.quotes = Quote.objects.published().filter(language=language)
        self.slides = Slide.objects.published().filter(language=language)
        self.statistics = Statistic.objects.filter(active=True, language=language).all()

        projects = Project.objects.filter(is_campaign=True, status__viewable=True)
        if language == 'en':
            projects = projects.filter(language__code=language)

        projects = projects.order_by('?')

        if len(projects) > 4:
            self.projects = projects[0:4]
        elif len(projects) > 0:
            self.projects = projects[0:len(projects)]
        else:
            self.projects = None

        return self

from bluebottle.quotes.models import Quote
from bluebottle.slides.models import Slide
from bluebottle.statistics.models import Statistic
from bluebottle.projects.models import Project

class HomePage(object):
    """
    Instead of serving all the objects separately we combine
    Slide, Quote and Stats into a dummy object
    """
    def get(self, language):
        self.id = language
        self.quotes = Quote.objects.published().filter(language=language)
        self.slides = Slide.objects.published().filter(language=language)
        self.statistics = Statistic.objects.filter(active=True, language=language).all()

        projects = Project.objects.filter(is_campaign=True, status__viewable=True)
        if language == 'en':
            projects = projects.filter(language__code=language)

        projects = projects.order_by('?')

        if len(projects) > 4:
            self.projects = projects[0:4]
        elif len(projects) > 0:
            self.projects = projects[0:len(projects)]
        else:
            self.projects = Project.objects.none()

        return self

Send an empty list instead of None if no projects

selected for homepage.


from disco.test import TestCase, TestJob

class InitJob(TestJob):
    sort = False

    @staticmethod
    def map_reader(stream, size, url, params):
        params.x = 10
        return (stream, size, url)

    @staticmethod
    def map_init(iter, params):
        assert hasattr(params, 'x')
        iter.next()
        params['x'] += 100

    @staticmethod
    def map(e, params):
        yield e, int(e) + params['x']

    @staticmethod
    def reduce_init(iter, params):
        params['y'] = 1000

    @staticmethod
    def reduce(iter, params):
        for k, v in iter:
            yield k, int(v) + params['y']

class InitTestCase(TestCase):
    def serve(self, path):
        return 'skipthis\n' + ('%s\n' % path) * 10

    def runTest(self):
        self.job = InitJob().run(input=self.test_server.urls(range(10)))
        results = list(self.results(self.job))
        for k, v in results:
            self.assertEquals(int(k) + 1110, int(v))
        self.assertEquals(len(results), 100)

from disco.test import TestCase, TestJob

class InitJob(TestJob):
    params = {'x': 10}
    sort = False

    @staticmethod
    def map_init(iter, params):
        iter.next()
        params['x'] += 100

    @staticmethod
    def map(e, params):
        yield e, int(e) + params['x']

    @staticmethod
    def reduce_init(iter, params):
        params['y'] = 1000

    @staticmethod
    def reduce(iter, params):
        for k, v in iter:
            yield k, int(v) + params['y']

class InitTestCase(TestCase):
    def serve(self, path):
        return 'skipthis\n' + ('%s\n' % path) * 10

    def runTest(self):
        self.job = InitJob().run(input=self.test_server.urls(range(10)))
        results = list(self.results(self.job))
        for k, v in results:
            self.assertEquals(int(k) + 1110, int(v))
        self.assertEquals(len(results), 100)

Revert "added a test for the map_reader before map_init -case which fails currently"
(deprecate init functions instead)

This reverts commit 88551bf444b7b358fea8e7eb4475df2c5d87ceeb.


from datetime import timedelta
import httplib
from tempfile import TemporaryFile
from zipfile import ZipFile
from celery.schedules import schedule

from molly.apps.places.parsers.naptan import NaptanParser

class NaptanImporter(object):

    IMPORTER_NAME = 'naptan'
    IMPORT_SCHEDULE = schedule(run_every=timedelta(weeks=1))

    HTTP_HOST = "www.dft.gov.uk"
    REMOTE_PATH = "/NaPTAN/snapshot/NaPTANxml.zip"

    def __init__(self, config):
        self._http_connection = httplib.HTTPConnection(self.HTTP_HOST)
        self._url = "http://%s%s" % (self.HTTP_HOST, self.REMOTE_PATH)

    def _get_file_from_url(self):
        temporary = TemporaryFile()
        self._http_connection.request('GET', self._url)
        temporary.write(self._http_connection.getresponse().read())
        return ZipFile(temporary).open('NaPTAN.xml')

    def load(self):
        parser = NaptanParser()
        for stop in parser.import_from_file(self._get_file_from_url(), self._url):
            self.stop_service.insert_and_merge(stop)

Provider = NaptanImporter

from datetime import timedelta
import httplib
from tempfile import TemporaryFile
from zipfile import ZipFile
from celery.schedules import schedule

from molly.apps.places.parsers.naptan import NaptanParser

class NaptanImporter(object):

    IMPORTER_NAME = 'naptan'
    IMPORT_SCHEDULE = schedule(run_every=timedelta(weeks=1))

    HTTP_HOST = "www.dft.gov.uk"
    REMOTE_PATH = "/NaPTAN/snapshot/NaPTANxml.zip"

    def __init__(self, config):
        self._http_connection = httplib.HTTPConnection(self.HTTP_HOST)
        self._url = "http://%s%s" % (self.HTTP_HOST, self.REMOTE_PATH)

    def _get_file_from_url(self):
        temporary = TemporaryFile()
        self._http_connection.request('GET', self._url)
        temporary.write(self._http_connection.getresponse().read())
        return ZipFile(temporary).open('NaPTAN.xml')

    def load(self):
        parser = NaptanParser()
        for stop in parser.import_from_file(self._get_file_from_url(), self._url):
            self.poi_service.add_or_update(stop)

Provider = NaptanImporter

Update the importer to use the places service


# pylint: disable=unused-variable

from unittest.mock import patch, Mock

from memegen.app import create_app
from memegen.settings import get_config
from memegen.routes._common import display

def describe_display():

    app = create_app(get_config('test'))
    app.config['GOOGLE_ANALYTICS_TID'] = 'my_tid'

    request_html = Mock()
    request_html.headers.get = Mock(return_value="text/html")
    request_html.path = "it's a path"

    @patch('memegen.routes._common.request', request_html)
    def it_returns_html_for_browsers():

        with app.test_request_context():
            html = display("my_title", "my_path", raw=True)

        print(html)
        assert "<title>my_title</title>" in html
        assert 'url("it\'s a path")' in html
        assert "ga('create', 'my_tid', 'auto');" in html

# pylint: disable=unused-variable,expression-not-assigned

from unittest.mock import patch, call, Mock

import pytest
from expecter import expect

from memegen.app import create_app
from memegen.settings import get_config
from memegen.routes._common import display

def describe_display():

    @pytest.fixture
    def app():
        app = create_app(get_config('test'))
        app.config['GOOGLE_ANALYTICS_TID'] = 'my_tid'
        return app

    request_html = Mock(path="it's a path")
    request_html.headers.get = Mock(return_value="text/html")

    request_image = Mock(path="it's a path")
    request_image.headers.get = Mock(return_value="(not a browser)")

    @patch('memegen.routes._common.request', request_html)
    def it_returns_html_for_browsers(app):

        with app.test_request_context():
            html = display("my_title", "my_path", raw=True)

        print(html)
        assert "<title>my_title</title>" in html
        assert 'url("it\'s a path")' in html
        assert "ga('create', 'my_tid', 'auto');" in html

    @patch('memegen.routes._common._track')
    @patch('memegen.routes._common.send_file')
    @patch('memegen.routes._common.request', request_image)
    def it_returns_an_image_otherwise(mock_send_file, mock_track):

        display("my_title", "my_path")

        expect(mock_track.mock_calls) == [
            call("my_title"),
        ]
        expect(mock_send_file.mock_calls) == [
            call("my_path", mimetype='image/jpeg'),
        ]

Test that a request defaults to sending an image


#!/usr/bin/python

import sys
import os

name = sys.argv[1]

status = sys.stdin.readline()
status = status.rstrip(os.linesep)

print("<%s>" % name)
print("\t<status=\"%s\" />" % status)

if status != "SKIP":
  print("\t<outcome>")
  for line in sys.stdin:
      # Escaping, ... !
      print(line.rstrip(os.linesep))
  print("\t</outcome>")

print("</%s>" % name)

#!/usr/bin/python

import sys
import os

name = sys.argv[1]

status = sys.stdin.readline()
status = status.rstrip(os.linesep)

print("<%s status=\"%s\">" % (name, status))

print("\t<outcome>")
for line in sys.stdin:
    # Escaping, ... !
    print(line.rstrip(os.linesep))
print("\t</outcome>")

print("</%s>" % name)

Fix the XML format produced

Fix the XML format produced


# coding=utf-8
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.

import sys
from time import time

from ntplib import NTPClient

from qrl.core import logger

ntp_server = 'pool.ntp.org'
version = 3
times = 5
drift = None

def get_ntp_response():
    try:
        ntp_client = NTPClient()
        response = ntp_client.request(ntp_server, version=version)
    except Exception as e:
        logger.exception(e)
        sys.exit(0)
    return response

def getNTP():
    ntp_timestamp = 0
    response = get_ntp_response()
    if response:
        ntp_timestamp = int(response.tx_time)
    return ntp_timestamp

def setDrift():
    global drift
    response = get_ntp_response()
    if not response:
        return response
    drift = response.offset

def getTime():
    global drift
    curr_time = drift + int(time())
    return curr_time

# coding=utf-8
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.

import sys
from time import time

from ntplib import NTPClient

from qrl.core import logger

ntp_servers = ['pool.ntp.org', 'ntp.ubuntu.com']
NTP_VERSION = 3
NTP_RETRIES = 6
drift = None

def get_ntp_response():
    for retry in range(NTP_RETRIES):
        ntp_server = ntp_servers[retry % len(ntp_servers)]
        try:
            ntp_client = NTPClient()
            response = ntp_client.request(ntp_server, version=NTP_VERSION)
        except Exception as e:
            logger.warning(e)
            continue
        return response

    # FIXME: Provide some proper clean before exiting
    logger.fatal("Could not contact NTP servers after %d retries", NTP_RETRIES)
    sys.exit(-1)

def getNTP():
    ntp_timestamp = 0
    response = get_ntp_response()
    if response:
        ntp_timestamp = int(response.tx_time)
    return ntp_timestamp

def setDrift():
    global drift
    response = get_ntp_response()
    if not response:
        return response
    drift = response.offset

def getTime():
    global drift
    curr_time = drift + int(time())
    return curr_time

Support multiple servers and retries

Support multiple servers and retries


from personalisation.models import AbstractBaseRule, Segment

class SegmentMiddleware(object):
    """Middleware for testing and putting a user in a segment"""

    def __init__(self, get_response=None):
        self.get_response = get_response

    def __call__(self, request):
        segments = Segment.objects.all().filter(status="enabled")

        chosen_segments = []

        for segment in segments:
            rules = AbstractBaseRule.objects.filter(segment=segment).select_subclasses()
            result = self.test_rules(rules, request)

            if result:
                self.add_segment_to_user(segment, request)

        response = self.get_response(request)

        print(request.session['segments'])

        return response

    def test_rules(self, rules, request):
        for rule in rules:
            result = rule.test_user(request)

            if result is False:
                return False

        return True

    def add_segment_to_user(self, segment, request):
        if 'segments' not in request.session:
            request.session['segments'] = []

        if segment not in request.session['segments']:
            request.session['segments'].append(segment.encoded_name())

from personalisation.models import AbstractBaseRule, Segment

class SegmentMiddleware(object):
    """Middleware for testing and putting a user in a segment"""

    def __init__(self, get_response=None):
        self.get_response = get_response

    def __call__(self, request):
        segments = Segment.objects.all().filter(status="enabled")

        chosen_segments = []

        for segment in segments:
            rules = AbstractBaseRule.objects.filter(segment=segment).select_subclasses()
            result = self.test_rules(rules, request)

            if result:
                self.add_segment_to_user(segment, request)

        response = self.get_response(request)

        if not request.session.get('segments'):
            request.session['segments'] = []

        print(request.session['segments'])

        return response

    def test_rules(self, rules, request):
        for rule in rules:
            result = rule.test_user(request)

            if result is False:
                return False

        return True

    def add_segment_to_user(self, segment, request):
        if 'segments' not in request.session:
            request.session['segments'] = []

        if segment not in request.session['segments']:
            request.session['segments'].append(segment.encoded_name())

Create empty 'segments' object in session if none exists


from django import forms

from discussion.models import Comment, Post, Discussion
from notification.models import NoticeSetting

class CommentForm(forms.ModelForm):
    class Meta:
        exclude = ('user', 'post')
        model = Comment
        widgets = {
            'body': forms.Textarea(attrs={'placeholder': 'Reply to this conversation'}),
        }

class PostForm(forms.ModelForm):
    class Meta:
        exclude = ('user', 'discussion')
        model = Post
        widgets = {
            'body': forms.Textarea(attrs={'placeholder': 'Start a conversation'}),
        }

class SearchForm(forms.Form):
    search = forms.CharField()
    discussion = forms.ModelChoiceField(
            required=False,
            queryset=Discussion.objects,
            empty_label='All discussions')

class SubscribeForm(forms.Form):
    send = forms.ModelMultipleChoiceField(
            NoticeSetting.objects,
            required=False,
            label=u'Notify me with updates from this discussion by',
            widget=forms.CheckboxSelectMultiple)

    def __init__(self, *args, **kwargs):
        qs = kwargs.pop('qs')
        super(SubscribeForm, self).__init__(*args, **kwargs)
        self.fields['send'].queryset = qs

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

from discussion.models import Comment, Post, Discussion
from notification.models import NoticeSetting

class CommentForm(forms.ModelForm):
    class Meta:
        exclude = ('user', 'post')
        model = Comment
        widgets = {
            'body': forms.Textarea(attrs={'placeholder': _('Reply to this conversation')}),
        }

class PostForm(forms.ModelForm):
    class Meta:
        exclude = ('user', 'discussion')
        model = Post
        widgets = {
            'body': forms.Textarea(attrs={'placeholder': _('Start a conversation')}),
        }

class SearchForm(forms.Form):
    search = forms.CharField()
    discussion = forms.ModelChoiceField(
            required=False,
            queryset=Discussion.objects,
            empty_label=_('All discussions'))

class SubscribeForm(forms.Form):
    send = forms.ModelMultipleChoiceField(
            NoticeSetting.objects,
            required=False,
            label=_('Notify me with updates from this discussion by'),
            widget=forms.CheckboxSelectMultiple)

    def __init__(self, *args, **kwargs):
        qs = kwargs.pop('qs')
        super(SubscribeForm, self).__init__(*args, **kwargs)
        self.fields['send'].queryset = qs

Make the odd string translatable.

Make the odd string translatable.


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

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

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

Remove admin docs by default since they are never used.


# Copyright 2017 Google Inc. All Rights Reserved.
#
# 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.

"""Single source of truth for dsub's version.

This must remain small and dependency-free so that any dsub module may
import it without creating circular dependencies. Note that this module
is parsed as a text file by setup.py and changes to the format of this
file could break setup.py.

The version should follow formatting requirements specified in PEP-440.
 - https://www.python.org/dev/peps/pep-0440

A typical release sequence will be versioned as:
  0.1.3.dev0 -> 0.1.3 -> 0.1.4.dev0 -> ...
"""

DSUB_VERSION = '0.4.2.dev0'

# Copyright 2017 Google Inc. All Rights Reserved.
#
# 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.

"""Single source of truth for dsub's version.

This must remain small and dependency-free so that any dsub module may
import it without creating circular dependencies. Note that this module
is parsed as a text file by setup.py and changes to the format of this
file could break setup.py.

The version should follow formatting requirements specified in PEP-440.
 - https://www.python.org/dev/peps/pep-0440

A typical release sequence will be versioned as:
  0.1.3.dev0 -> 0.1.3 -> 0.1.4.dev0 -> ...
"""

DSUB_VERSION = '0.4.2'

Update dsub version to 0.4.2

Update dsub version to 0.4.2

PiperOrigin-RevId: 337172014


from django.conf.urls import patterns, url

# API URLs
from .views import LocationFromIpView

urlpatterns = patterns('',
    url(r'^location/(?P<ip_address>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))/$', LocationFromIpView.as_view(),
        name='geoip-explicit-ip-view'),
    url(r'^location/$', LocationFromIpView.as_view(), name='geoip-implicit-ip-view')  # Take IP addr from request
)

from django.conf.urls import url

# API URLs
from .views import LocationFromIpView

urlpatterns = [
    url(r'^location/(?P<ip_address>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))/$', LocationFromIpView.as_view(),
        name='geoip-explicit-ip-view'),
    url(r'^location/$', LocationFromIpView.as_view(), name='geoip-implicit-ip-view')  # Take IP addr from request
]

Upgrade to new urlpatterns format

Upgrade to new urlpatterns format


'''
Swap the spatial axes. Swap the spectral and stokes axes.
'''

import sys
from astropy.io import fits

hdu = fits.open(sys.argv[1], mode='update')

hdu[0].data = hdu[0].data.swapaxes(0, 1)

hdu[0].data = hdu[0].data[:, :, :, ::-1]
hdu[0].data = hdu[0].data[:, :, ::-1, :]

hdu.flush()

execfile("~/Dropbox/code_development/ewky_scripts/header_swap_axis.py")

hdu[0].header = header_swapaxes(hdu[0].header, 2, 3)

hdu.flush()

hdu.close()

'''
\Swap the spectral and stokes axes. Needed due to issue in regridding function
'''

import sys
from astropy.io import fits

hdu = fits.open(sys.argv[1], mode='update')

hdu[0].data = hdu[0].data.swapaxes(0, 1)

execfile("/home/eric/Dropbox/code_development/ewky_scripts/header_swap_axis.py")

hdu[0].header = header_swapaxes(hdu[0].header, 2, 3)

hdu.flush()

hdu.close()

Update what's needed to correct mask and model


import unittest

import config
from .. import ntokloapi

class BlacklistTest(unittest.TestCase):

    def setUp(self):
        self.blacklist = ntokloapi.Blacklist(config.TEST_KEY, config.TEST_SECRET)

    def test_blacklist_add_singleitem(self):

        response = self.blacklist.add(productid=['10201', ])
        assert response == "204"

    def test_blacklist_add_multipleitems(self):
        response = self.blacklist.add(productid=['10202', '10203'])
        assert response == "204"

    def test_blacklist_add_empty_elements(self):
        response = self.blacklist.add(productid=['10204', '10205', '', ''])
        assert response == "204"

    def test_blacklist_remove_singleitem(self):
        response = self.blacklist.remove(productid=['10201', ])
        assert response == "204"

    def test_blacklist_remove_multipleitems(self):
        response = self.blacklist.remove(productid=['10202', '10203'])
        assert response == "204"

    def test_blacklist_remove_empty_elements(self):
        response = self.blacklist.remove(productid=['10204', '10205', '', ''])
        assert response == "204"

    def test_blacklist_show_items(self):
        response = self.blacklist.list()
        assert not response

import unittest

import config
import ntokloapi

class BlacklistTest(unittest.TestCase):

    def setUp(self):
        self.blacklist = ntokloapi.Blacklist(config.TEST_KEY, config.TEST_SECRET)

    def test_blacklist_add_singleitem(self):

        response = self.blacklist.add(productid=['10201', ])
        assert response == 204

    def test_blacklist_add_multipleitems(self):
        response = self.blacklist.add(productid=['10202', '10203'])
        assert response == 204

    def test_blacklist_add_empty_elements(self):
        response = self.blacklist.add(productid=['10204', '10205', '', ''])
        assert response == 204

    def test_blacklist_remove_singleitem(self):
        response = self.blacklist.remove(productid=['10201', ])
        assert response == 204

    def test_blacklist_remove_multipleitems(self):
        response = self.blacklist.remove(productid=['10202', '10203'])
        assert response == 204

    def test_blacklist_remove_empty_elements(self):
        response = self.blacklist.remove(productid=['10204', '10205', '', ''])
        assert response == 204

    def test_blacklist_show_items(self):
        response = self.blacklist.list()
        assert not response

Fix unit tests for the blacklist

Fix unit tests for the blacklist


from django.db import models

class Lease(models.Model):
    class Meta:
        db_table = 'autoreg'

    mac = models.CharField(max_length=17, db_index=True)
    ip = models.IntegerField(primary_key=True)
    date = models.IntegerField()

from django.db import models
from ipaddr import IPv4Address

class Lease(models.Model):
    class Meta:
        db_table = 'autoreg'

    mac = models.CharField(max_length=17, db_index=True)
    ip = models.IntegerField(primary_key=True)
    date = models.IntegerField()

    def __str__(self):
        return unicode(self).encode('ascii', 'replace')

    def __unicode__(self):
        return unicode(IPv4Address(self.ip)) + u' = ' + unicode(self.mac)

    def __repr__(self):
        return u'<Lease: ' + unicode(self) + u'>'

Add __str__, __unicode__, and __repr__

Add __str__, __unicode__, and __repr__


# -*- coding: utf-8 -*-
import importlib
import re
import warnings

from django.conf import settings
from django.utils.translation import ugettext_lazy as _, ugettext

BROWSERS = (
    (re.compile('Chrome'), _('Chrome')),
    (re.compile('Safari'), _('Safari')),
    (re.compile('Firefox'), _('Firefox')),
    (re.compile('Opera'), _('Opera')),
    (re.compile('IE'), _('Internet Explorer')),
)

DEVICES = (
    (re.compile('Android'), _('Android')),
    (re.compile('Linux'), _('Linux')),
    (re.compile('iPhone'), _('iPhone')),
    (re.compile('iPad'), _('iPad')),
    (re.compile('(Mac OS X)'), _('OS X')),
    (re.compile('NT 5.1'), _('Windows XP')),
    (re.compile('NT 6.0'), _('Windows Vista')),
    (re.compile('NT 6.1'), _('Windows 7')),
    (re.compile('NT 6.2'), _('Windows 8')),
    (re.compile('NT 6.3'), _('Windows 8.1')),
    (re.compile('Windows'), _('Windows')),
)

def get_device(user_agent):
    """
    Transform a User Agent into a human readable text.
    """
    infos = []

    for regex, name in BROWSERS:
        if regex.search(user_agent):
            infos.append('%s' % name)
            break

    for regex, name in DEVICES:
        if regex.search(user_agent):
            infos.append('%s' % name)
            break

    return ', '.join(infos)

def get_session_store():
    mod = getattr(settings, 'SESSION_ENGINE', 'django.contrib.sessions.backends.db')
    engine = importlib.import_module(mod)
    store = engine.SessionStore()
    return store

# -*- coding: utf-8 -*-
try:
    from django.utils.importlib import import_module
except ImportError:
    from importlib import import_module

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

def get_session_store():
    mod = getattr(settings, 'SESSION_ENGINE', 'django.contrib.sessions.backends.db')
    engine = import_module(mod)
    store = engine.SessionStore()
    return store

def get_resolver(request, setting):
    module_path = getattr(app_settings, setting)

    try:
        module, attribute = module_path.rsplit('.', 1)
        resolver_module = import_module(module)
        resolver = getattr(resolver_module, attribute)

    except ImportError:
        raise ImproperlyConfigured(
            "Please specify a valid %s module. "
            "Could not find %s " % (setting, module))

    except AttributeError:
        raise ImproperlyConfigured(
            "Please specify a valid %s "
            "function. Could not find %s function in module %s" %
            (setting, attribute, module))

    return resolver(request)

Add get_resolver() util and remove get_device() (now use ua-parser).


# Copyright 2014 Google Inc. All rights reserved.
#
# 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.

VERSION = '0.9.4'

# Copyright 2014 Google Inc. All rights reserved.
#
# 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.

VERSION = '0.9.4pre'

Add a -vvv mode to log when tests are queued for running.

If one is running a bunch of tests in parallel and something
is not working right, it can be useful to see which tests are
currently executing at the same time. There isn't a great way
to do this in typ, because we don't know when tests are actually
picked up for execution by the child processes, but we can at
least log which tests has been queued for running.

This patch adds a -vvv option (verbose=3). This will make
the test log twice as long, since we will now log lines for
when every test starts and stops, but at least that can help
with debugging.


"""snowman URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/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 url, include
from django.contrib import admin
from django.views.generic import RedirectView

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('api.router')),
    url(r'^.*$', RedirectView.as_view(pattern_name='api-root', permanent=True), name='index')
]

"""snowman URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/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 url, include
from django.contrib import admin
from django.views.generic import RedirectView

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('api.router')),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^.*$', RedirectView.as_view(pattern_name='api-root', permanent=True), name='index')
]

Add simple login form in the API.

Add simple login form in the API.

This is usefull for developers to explore the Browlable api directly
on the browser.


import colorsys
from PIL import Image, ImageFont, ImageDraw

class TextRenderer:

    def __init__(self, font="./NotoSansCJK-Bold.otf",
                 font_color=(0, 120, 0), color_bg=False):
        self.image = None

        # params
        self.color_bg = color_bg
        self.font_color = font_color

        # new image and font
        self.font = ImageFont.truetype(font, 30)
        return None

    def getFrameCount(self):
        return 1

    def rainbow_bg(c):
        # hue, lightness, saturation to rgb
        vals = colorsys.hls_to_rgb(round(c / 360.0, 2), 0.05, 1)
        return (int(vals[0] * 255), int(vals[1] * 255), int(vals[2] * 255))

    def draw_text(self, text_to_send):
        x, y = self.font.getsize(text_to_send)

        self.im = Image.new("RGBA", (x, y+10), "black")
        # Add padding below, because PIL sucks!
        self.draw = ImageDraw.Draw(self.im)

        self.draw.text(
            (0, 0), text_to_send, font=self.font, fill=self.font_color
        )

    def render(self, msgText):
        self.draw_text(' '.join(msgText))

    def getImage(self):
        return self.image

    def get_queue_token(self, msgToken):
        queue_token = {}
        # TODO: add possible params
        self.render(msgToken["text"])
        queue_token["image"] = [self.im]
        queue_token["frame_count"] = self.getFrameCount()
        queue_token["action"] = "scroll"
        queue_token["valid"] = True

        return queue_token

from PIL import Image, ImageFont, ImageDraw

class TextRenderer:

    def __init__(self, font="./NotoSansCJK-Bold.otf",
                 font_color=(0, 120, 0), color_bg=False):

        # params
        self.color_bg = color_bg
        self.font_color = font_color
        # new image and font
        self.font = ImageFont.truetype(font, 30)

    def draw_text(self, text_to_send):
        x, y = self.font.getsize(text_to_send)

        # Add padding below, because PIL sucks!
        image = Image.new("RGBA", (x, y+10), "black")

        ImageDraw.Draw(image).text(
            (0, 0), text_to_send, font=self.font, fill=self.font_color
        )

        return image

    def get_queue_token(self, msgToken):
        queue_token = {}
        # TODO: add possible params
        queue_token["image"] = [self.draw_text(' '.join(msgToken["text"]))]
        queue_token["frame_count"] = 1
        queue_token["action"] = "scroll"
        queue_token["valid"] = True

        return queue_token

Remove all unused code in text renderer.


from __future__ import absolute_import, division, print_function

from glue.external.echo import CallbackProperty, keep_in_sync
from glue.core.state_objects import State

__all__ = ['VispyLayerState']

class VispyLayerState(State):
    """
    A base state object for all Vispy layers
    """

    layer = CallbackProperty()
    visible = CallbackProperty(True)
    zorder = CallbackProperty(0)
    color = CallbackProperty()
    alpha = CallbackProperty()

    def __init__(self, **kwargs):

        super(VispyLayerState, self).__init__(**kwargs)

        self._sync_color = None
        self._sync_alpha = None

        self.add_callback('layer', self._layer_changed)
        self._layer_changed()

    def _layer_changed(self):

        if self._sync_color is not None:
            self._sync_color.stop_syncing()

        if self._sync_alpha is not None:
            self._sync_alpha.stop_syncing()

        if self.layer is not None:

            self.color = self.layer.style.color
            self.alpha = self.layer.style.alpha

            self._sync_color = keep_in_sync(self, 'color', self.layer.style, 'color')
            self._sync_alpha = keep_in_sync(self, 'alpha', self.layer.style, 'alpha')

from __future__ import absolute_import, division, print_function

from glue.external.echo import CallbackProperty, keep_in_sync
from glue.core.state_objects import State
from glue.core.message import LayerArtistUpdatedMessage

__all__ = ['VispyLayerState']

class VispyLayerState(State):
    """
    A base state object for all Vispy layers
    """

    layer = CallbackProperty()
    visible = CallbackProperty(True)
    zorder = CallbackProperty(0)
    color = CallbackProperty()
    alpha = CallbackProperty()

    def __init__(self, **kwargs):

        super(VispyLayerState, self).__init__(**kwargs)

        self._sync_color = None
        self._sync_alpha = None

        self.add_callback('layer', self._layer_changed)
        self._layer_changed()

        self.add_global_callback(self._notify_layer_update)

    def _notify_layer_update(self, **kwargs):
        message = LayerArtistUpdatedMessage(self)
        if self.layer is not None and self.layer.hub is not None:
            self.layer.hub.broadcast(message)

    def _layer_changed(self):

        if self._sync_color is not None:
            self._sync_color.stop_syncing()

        if self._sync_alpha is not None:
            self._sync_alpha.stop_syncing()

        if self.layer is not None:

            self.color = self.layer.style.color
            self.alpha = self.layer.style.alpha

            self._sync_color = keep_in_sync(self, 'color', self.layer.style, 'color')
            self._sync_alpha = keep_in_sync(self, 'alpha', self.layer.style, 'alpha')

Make sure layer artist icon updates when changing the color mode or colormaps


import pandas as pd
import gzip
import boto3
import re

def default_key_map(key):
    return re.sub('_input', '_output',  key)

class S3Dest():
    def __init__(self, key_map=default_key_map):
        self.key_map = key_map

    def skip_keys(self, bucket, source_prefix):
        s3 = boto3.resource('s3')
        objs = s3.Bucket(bucket).objects.filter(Prefix=self.key_map(source_prefix))
        return [obj.key for obj in objs]

    def put(self, data, obj):
        body = self.generate_output_file(data)
        dest_key = self.key_map(obj.key)

        s3 = boto3.resource('s3')
        s3.Object(obj.bucket_name, dest_key).put(Body=body)

    def generate_output_file(self, data):
        df = pd.DataFrame(data)
        tsv = df.to_csv(sep='\t', header=False, index=False)
        return gzip.compress(tsv.encode())

import pandas as pd
import gzip
import boto3
import re
import io

def default_key_map(key):
    return re.sub('_input', '_output',  key)

class S3Dest():
    def __init__(self, key_map=default_key_map):
        self.key_map = key_map

    def skip_keys(self, bucket, source_prefix):
        s3 = boto3.resource('s3')
        objs = s3.Bucket(bucket).objects.filter(Prefix=self.key_map(source_prefix))
        return [obj.key for obj in objs]

    def put(self, data, obj):
        body = self.generate_output_file(data)
        dest_key = self.key_map(obj.key)

        s3 = boto3.resource('s3')
        s3.Object(obj.bucket_name, dest_key).upload_fileobj(io.BytesIO(body))

    def generate_output_file(self, data):
        df = pd.DataFrame(data)
        tsv = df.to_csv(sep='\t', header=False, index=False)
        return gzip.compress(tsv.encode())

Use managed transfer for uploading

Use managed transfer for uploading


from django.core.management.base import BaseCommand, CommandError

from ...cron import parse_auth_log

class Command(BaseCommand):
    def handle(self, *args, **options):
        parse_auth_log()

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError

try:
    import simplejson as json
except ImportError as e:
    import json

from rest_framework.renderers import JSONRenderer
from unipath import Path

from ...api.serializers import HackLocationSerializer
from ...cron import parse_auth_log
from ...models import SshHackLocation

class Command(BaseCommand):
    def handle(self, *args, **options):
        parse_auth_log()
        # Write the attempt data to json for fast AJAX loading
        serializer = HackLocationSerializer(SshHackLocation.objects.all(), many=True)
        data = JSONRenderer().render(serializer.data, 'application/json', {})
        json_output_file = Path(settings.STATIC_ROOT).child('hack_location.json')
        with open(json_output_file, 'wb') as f:
            f.write(data)

Write hack locations to json after parsing log file


try:
    import sip
    from uitools.qt import QtCore
    import maya.OpenMayaUI as apiUI

# These modules will not exist while building the docs.
except ImportError:
    import os
    if os.environ.get('SPHINX') != 'True':
        raise

def get_maya_window():
    """Get the main Maya window as a QtGui.QMainWindow."""
    ptr = apiUI.MQtUtil.mainWindow()
    if ptr is not None:
        return sip.wrapinstance(long(ptr), QtCore.QObject)

def maya_to_qt(maya_object):
    """Convert a Maya UI path to a Qt object.

    :param str maya_object: The path of the Maya UI object to convert.
    :returns: QtCore.QObject or None

    """

    ptr = (
        apiUI.MQtUtil.findControl(maya_object) or
        apiUI.MQtUtil.findLayout(maya_object) or
        apiUI.MQtUtil.findMenuItem(maya_object)
    )
    if ptr is not None:
        return sip.wrapinstance(long(ptr), QtCore.QObject)

try:
    from uitools.sip import wrapinstance
    from uitools.qt import QtCore
    import maya.OpenMayaUI as apiUI

# These modules will not exist while building the docs.
except ImportError:
    import os
    if os.environ.get('SPHINX') != 'True':
        raise

def get_maya_window():
    """Get the main Maya window as a QtGui.QMainWindow."""
    ptr = apiUI.MQtUtil.mainWindow()
    if ptr is not None:
        return wrapinstance(long(ptr), QtCore.QObject)

def maya_to_qt(maya_object):
    """Convert a Maya UI path to a Qt object.

    :param str maya_object: The path of the Maya UI object to convert.
    :returns: QtCore.QObject or None

    """

    ptr = (
        apiUI.MQtUtil.findControl(maya_object) or
        apiUI.MQtUtil.findLayout(maya_object) or
        apiUI.MQtUtil.findMenuItem(maya_object)
    )
    if ptr is not None:
        return wrapinstance(long(ptr), QtCore.QObject)

Use uitools.sip instead of straight sip

Use uitools.sip instead of straight sip


#-- encoding: utf-8 --
from django.conf.urls.defaults import patterns, url

from haystack.views import search_view_factory

import views
import ajax

urlpatterns = patterns('',
    url(r'^$', views.homepage, name='i4p-index'),
    url(r'^homepage/ajax/slider/bestof/$', ajax.slider_bestof, name='i4p-homepage-ajax-slider-bestof'),
    url(r'^homepage/ajax/slider/latest/$', ajax.slider_latest, name='i4p-homepage-ajax-slider-latest'),
    url(r'^homepage/ajax/slider/commented/$', ajax.slider_most_commented, name='i4p-homepage-ajax-slider-commented'),

    url(r'^history/check_version/(?P<pk>[\d]+)$', views.VersionActivityCheckView.as_view(), name='history-check-version'),
    url(r'^search/', search_view_factory(view_class=views.SearchView), name='i4p-search'),
    url(r'^location/(?P<location_id>\d+)', views.LocationEditView.as_view(), name='i4p-location-edit'),
    url(r'^locations/$', views.LocationListView.as_view(), name='i4p-location-list'),
    url(r'^locations/missing/(?P<missing_field_name>\w+)$', views.LocationListView.as_view(), name='i4p-location-missing-list'),

)

#-- encoding: utf-8 --
from django.conf.urls.defaults import patterns, url

from haystack.views import search_view_factory

import views
import ajax

urlpatterns = patterns('',
    #url(r'^$', views.homepage, name='i4p-index'),
    url(r'^homepage/ajax/slider/bestof/$', ajax.slider_bestof, name='i4p-homepage-ajax-slider-bestof'),
    url(r'^homepage/ajax/slider/latest/$', ajax.slider_latest, name='i4p-homepage-ajax-slider-latest'),
    url(r'^homepage/ajax/slider/commented/$', ajax.slider_most_commented, name='i4p-homepage-ajax-slider-commented'),

    url(r'^history/check_version/(?P<pk>[\d]+)$', views.VersionActivityCheckView.as_view(), name='history-check-version'),
    url(r'^search/', search_view_factory(view_class=views.SearchView), name='i4p-search'),
    url(r'^location/(?P<location_id>\d+)', views.LocationEditView.as_view(), name='i4p-location-edit'),
    url(r'^locations/$', views.LocationListView.as_view(), name='i4p-location-list'),
    url(r'^locations/missing/(?P<missing_field_name>\w+)$', views.LocationListView.as_view(), name='i4p-location-missing-list'),

)

Remove explicit link to homepage view in i4p_base


"""Stop Bins."""
import util
import errors
import registry
import entities

class StopBin(object):
  def __init__(self, prefix):
    self.prefix = prefix
    self._stops = {}

  def stops(self):
    return self._stops.values()

  def add_stop(self, stop):
    key = stop.onestop()
    # New stop
    if key not in self._stops:
      self._stops[key] = stop
    else:
      self._stops[key].merge(stop)

  @classmethod
  def from_json(cls, data):
    stopbin = cls(prefix=data['prefix'])
    for feature in data['features']:
      stop = entities.OnestopStop.from_json(feature)
      stopbin.add_stop(stop)
    return stopbin

  def json(self):
    return {
      'type': 'FeatureCollection',
      'properties': {},
      'prefix': self.prefix,
      'features': [
        i.json()
        for i in
        sorted(self.stops(), key=lambda x:x.onestop())
      ]
    }

"""Stop Bins."""
import util
import errors
import registry
import entities

class StopBin(object):
  def __init__(self, prefix):
    self.prefix = prefix
    self._stops = {}

  def stops(self):
    return self._stops.values()

  def add_stop(self, stop):
    key = stop.onestop()
    # New stop
    if key not in self._stops:
      self._stops[key] = stop
    else:
      self._stops[key].merge(stop)
    return self._stops[key]

  @classmethod
  def from_json(cls, data):
    stopbin = cls(prefix=data['prefix'])
    for feature in data['features']:
      stop = entities.OnestopStop.from_json(feature)
      stopbin.add_stop(stop)
    return stopbin

  def json(self):
    return {
      'type': 'FeatureCollection',
      'properties': {},
      'prefix': self.prefix,
      'features': [
        i.json()
        for i in
        sorted(self.stops(), key=lambda x:x.onestop())
      ]
    }

Return added stop in StopBin.add_stop()

Return added stop in StopBin.add_stop()


# -*- encoding: utf-8 -*-
##############################################################################
# For copyright and license notices, see __openerp__.py file in root directory
##############################################################################

from openerp import models, fields, api

class ResPartner(models.Model):
    _inherit = 'res.partner'

    registrations = fields.One2many(
        string="Event registrations",
        comodel_name='event.registration', inverse_name="partner_id")
    registration_count = fields.Integer(
        string='Event registrations number', compute='_count_registration',
        store=True)
    attended_registration_count = fields.Integer(
        string='Event attended registrations number',
        compute='_count_registration', store=True)

    @api.one
    @api.depends('registrations')
    def _count_registration(self):
        self.registration_count = len(self.registrations)
        self.attended_registration_count = len(self.registrations.filtered(
            lambda x: x.state == 'done'))

# -*- encoding: utf-8 -*-
##############################################################################
# For copyright and license notices, see __openerp__.py file in root directory
##############################################################################

from openerp import models, fields, api

class ResPartner(models.Model):
    _inherit = 'res.partner'

    registrations = fields.One2many(
        string="Event registrations",
        comodel_name='event.registration', inverse_name="partner_id")
    registration_count = fields.Integer(
        string='Event registrations number', compute='_count_registration',
        store=True)
    attended_registration_count = fields.Integer(
        string='Event attended registrations number',
        compute='_count_attended_registration', store=True)

    @api.one
    @api.depends('registrations')
    def _count_registration(self):
        self.registration_count = len(self.registrations)

    @api.one
    @api.depends('registrations.state')
    def _count_attended_registration(self):
        self.attended_registration_count = len(self.registrations.filtered(
            lambda x: x.state == 'done'))

Revert last commit and tiggers _count_attended_registration method when one registrations.state changes


import os

from celery import Celery

app = Celery('dummy_tasks', broker=os.environ['TEST_HADES_BROKER_URI'],
             backend=os.environ['TEST_HADES_RESULT_BACKEND_URI'])

@app.task
def get_port_auth_attempts(nasipaddress, nasportid):
    return ["Success!", "No success! :-(",
            "Gotten: {}/{}".format(nasipaddress, nasportid)]

import os
from datetime import datetime
from time import sleep

from celery import Celery

app = Celery('dummy_tasks', broker=os.environ['TEST_HADES_BROKER_URI'],
             backend=os.environ['TEST_HADES_RESULT_BACKEND_URI'])

@app.task
def get_port_auth_attempts(nasipaddress, nasportid, limit=100):
    if nasportid == 'magic_sleep':
        # sleep for 10 seconds, which is longer than the default
        sleep(10)
        return []

    if nasipaddress != '141.30.223.206' or nasportid != 'C6':
        return []
    return [
        # (packettype, replymessage, username, auth_date, egress_vlan)
        # TODO: What VLAN should there be on Auth-Reject?  In any
        # case, it will be unused.
        ("Auth-Reject", "", "00:de:ad:be:ef:00", datetime(2017, 4, 20, 18, 25), None),
        ("Auth-Access", "Wu5_untagged", "00:de:ad:be:ef:00", datetime(2017, 4, 20, 18, 20), 15),
        ("Auth-Access", "unknown", "00:de:ad:be:ef:01", datetime(2017, 4, 20, 18, 5), 1001),
        ("Auth-Access", "traffic", "00:de:ad:be:ef:00", datetime(2017, 4, 20, 18, 0), 1001),
    ][:limit]

Implement correct function signature in dummy API and sleep trigger


import os

from setuptools import setup, find_packages

here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.txt')).read()

version = '0.1'
requires = ['pyramid', 'pyramid_debugtoolbar']

if __name__ == '__main__':
    setup(name='pings',
          version=version,
          description='pings',
          long_description=README,
          classifiers=[
            "Programming Language :: Python",
            "Framework :: Pylons",
            "Topic :: Internet :: WWW/HTTP",
            "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
            ],
          author='Christian Hudon',
          author_email='chrish@pianocktail.org',
          url='https://github.com/lisa-lab/pings',
          keywords='web pyramid pylons',
          packages=find_packages(),
          include_package_data=True,
          zip_safe=False,
          install_requires=requires,
          tests_require=requires,
          test_suite="pings",
          scripts = ['leaderboards_server', 'storage_server'],
          entry_points = """\
          [paste.app_factory]
          main = pings.web_server:main
          """,
          paster_plugins=['pyramid'],
          )

import os

from setuptools import setup, find_packages

here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.txt')).read()

version = '0.1'
# Use requirements.txt for all requirements, at least for now.
requires = []

if __name__ == '__main__':
    setup(name='pings',
          version=version,
          description='pings',
          long_description=README,
          classifiers=[
            "Programming Language :: Python",
            "Framework :: Pylons",
            "Topic :: Internet :: WWW/HTTP",
            "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
            ],
          author='Christian Hudon',
          author_email='chrish@pianocktail.org',
          url='https://github.com/lisa-lab/pings',
          keywords='web pyramid pylons',
          packages=find_packages(),
          include_package_data=True,
          zip_safe=False,
          install_requires=requires,
          tests_require=requires,
          test_suite="pings",
          scripts = ['leaderboards_server', 'storage_server'],
          entry_points = """\
          [paste.app_factory]
          main = pings.web_server:main
          """,
          paster_plugins=['pyramid'],
          )


from ..constants import FORMAT_CHECKS
from ..post import HEADER_OPTIONAL_FIELD_TYPES, HEADER_REQUIRED_FIELD_TYPES
from ..postprocessor import KnowledgePostProcessor

class FormatChecks(KnowledgePostProcessor):
    _registry_keys = [FORMAT_CHECKS]

    def process(self, kp):
        headers = kp.headers
        for field, typ, input in HEADER_REQUIRED_FIELD_TYPES:
            assert field in headers, "Required field `{}` missing from headers.".format(
                field)
            assert isinstance(headers[field], typ), "Value for field `{}` is of type {}, and needs to be of type {}.".format(
                field, type(headers[field]), typ)
        for field, typ, input in HEADER_OPTIONAL_FIELD_TYPES:
            if field in headers:
                assert isinstance(headers[field], typ), "Value for field `{}` is of type {}, and needs to be of type {}.".format(
                    field, type(headers[field]), typ)

from ..constants import FORMAT_CHECKS
from ..post import HEADER_OPTIONAL_FIELD_TYPES, HEADER_REQUIRED_FIELD_TYPES
from ..postprocessor import KnowledgePostProcessor

class FormatChecks(KnowledgePostProcessor):
    _registry_keys = [FORMAT_CHECKS]

    def process(self, kp):
        headers = kp.headers
        for field, typ, input in HEADER_REQUIRED_FIELD_TYPES:
            assert field in headers, \
                "Required field `{field}` missing from headers."
            assert isinstance(headers[field], typ), \
                f"Value for field `{field}` is of type " + \
                f"{type(headers[field])}, and needs to be of type {typ}."
        for field, typ, input in HEADER_OPTIONAL_FIELD_TYPES:
            if field in headers:
                assert isinstance(headers[field], typ), \
                    f"Value for field `{field}` is of type " + \
                    f"{type(headers[field])}, and needs to be of type {typ}."

Fix lint issues related to long lines

Fix lint issues related to long lines


from wal_e import retries
from wal_e.worker.base import _Deleter

class Deleter(_Deleter):

    def __init__(self, wabs_conn, container):
        super(Deleter, self).__init__()
        self.wabs_conn = wabs_conn
        self.container = container

    @retries.retry()
    def _delete_batch(self, page):
        # Azure Blob Service has no concept of mass-delete, so we must nuke
        # each blob one-by-one...
        for blob in page:
            self.wabs_conn.delete_blob(self.container, blob.name)

from wal_e import retries
from wal_e import log_help
from wal_e.worker.base import _Deleter

try:
    # New class name in the Azure SDK sometime after v1.0.
    #
    # See
    # https://github.com/Azure/azure-sdk-for-python/blob/master/ChangeLog.txt
    from azure.common import AzureMissingResourceHttpError
except ImportError:
    # Backwards compatbility for older Azure drivers.
    from azure import WindowsAzureMissingResourceError \
        as AzureMissingResourceHttpError

logger = log_help.WalELogger(__name__)

class Deleter(_Deleter):

    def __init__(self, wabs_conn, container):
        super(Deleter, self).__init__()
        self.wabs_conn = wabs_conn
        self.container = container

    @retries.retry()
    def _delete_batch(self, page):
        # Azure Blob Service has no concept of mass-delete, so we must nuke
        # each blob one-by-one...
        for blob in page:
            try:
                self.wabs_conn.delete_blob(self.container, blob.name)
            except AzureMissingResourceHttpError:
                logger.warning(
                    msg='failed while deleting resource',
                    detail='Blob {0} does not exist in container {1}.'.format(
                        blob.name, self.container))

Fix infinite retry while deleting missing resource in WABS


import os

qt_api = os.environ.get('QT_API', 'pyqt')

if qt_api == 'pyqt':
    from PyQt4.QtCore import *

    from PyQt4.QtCore import pyqtSignal as Signal
    from PyQt4.Qt import QCoreApplication
    from PyQt4.Qt import Qt

else:
    from PySide.QtCore import *

import os

qt_api = os.environ.get('QT_API', 'pyqt')

if qt_api == 'pyqt':
    from PyQt4.QtCore import *

    from PyQt4.QtCore import pyqtSignal as Signal
    from PyQt4.Qt import QCoreApplication
    from PyQt4.Qt import Qt

    # Emulate PySide version metadata.
    __version__ = QT_VERSION_STR
    __version_info__ = tuple(map(int, QT_VERSION_STR.split('.')))

else:
    from PySide.QtCore import *

Add PySide-style version metadata when PyQt4 is present.