<commit_before>import inspect
import config
import random
import re
import shelve

from helpers import async_get, markdown_escapes, command

class Dispatcher:

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

    @command('`/help` - return this')
    async def help(self):
        predicate = lambda f: inspect.ismethod(f) and hasattr(f, 'docstring')
        fns = inspect.getmembers(self, predicate=predicate)

        msg = "*Command list*\n\n"
        for _, f in fns:
            msg += f.docstring + '\n'

        await self.bot.sendMessage(self.chat_id, msg, parse_mode='Markdown')

    async def test(args):
        data = await async_get('http://localhost:8000/')
        print(data)

    @command('`/js <library>` - checks if library is cool or not')
    async def js(self, string):
        def messages(desc, pop):
            n = pop*100
            tmpl = '{}: {}.\nScore: {:.1f} out of 100, {}'
            if n > 90:
                m = 'cool as fuck'
            elif n > 80:
                m = 'pretty ok'
            elif n > 70:
                m = 'meh'
            elif n > 60:
                m = 'old'
            elif n > 50:
                m = 'deprecated'
            elif n > 40:
                m = 'lol seriously?'
            elif n > 30:
                m = 'lol seriously?'
            elif n > 20:
                m = 'please die'
            elif n > 20:
                m = '...'
            elif n > 10:
                m = 'burn your computer'
            else:
                m = 'Exception("<commits suicide>")'

            return tmpl.format(string, desc, n, m)

        if not string:
            return

        data = await async_get(
                'https://api.npms.io/v2/search?q={}'.format(string))

        if data['results']:
            pop = data['results'][0]['score']['detail']['popularity']
            desc = data['results'][0]['package']['description']
            await self.bot.sendMessage(self.chat_id, messages(desc, pop))
        else:
            await self.bot.sendMessage(self.chat_id, 'No')

    @command('`/sadness` - cry')
    async def sadness(self):
        sub = random.choice(['vaporwaveaesthetics',
                             'vaporwaveart',
                             'vaporwave'])

        url = 'https://imgur.com/r/{}/hot.json'.format(sub)

        data = await async_get(url)
        img = random.choice(data['data'])
        text = img['title']
        url = 'https://imgur.com/{}{}'.format(img['hash'], img['ext'])
        await self.bot.sendPhoto(self.chat_id, url, caption=text)

    @command('`/remember <keyword> <text>` - have memories')
    async def remember(self, string):
        """ Splits on spaces and takes first
            word as `keyword` rest as `body`.
            If body is not empty store that in memory,
            else return it.
        """
        if not string:
            return

        with shelve.open('db', 'c') as db:
            keyword, *body = string.split()
            if body:
                db[keyword] = ' '.join(body)
                txt = 'I\'ll remember that'
            else:
                result = db.get(keyword, None)
                if result:
                    txt = '{} = {}'.format(keyword, result)
                else:
                    txt = 'No idea about {}'.format(keyword)
        await self.bot.sendMessage(self.chat_id, txt)

    @command('`/puppy` - good doggo')
    async def puppy(self):
        sub = 'doggos' if random.random() > 0.1 else 'boats'
        url = 'https://imgur.com/r/{}/hot.json'.format(sub)

        data = await async_get(url)

        img = random.choice(data['data'])
        text = img['title']
        url = 'https://imgur.com/{}{}'.format(img['hash'], img['ext'])

        await self.bot.sendPhoto(self.chat_id, url, caption=text)

    @command('`/urban <word>` - cool dict')
    async def urban(self, string):
        url = ('https://mashape-community-urban-dictionary.p.mashape.com/'
               'define?term={}.').format(string)
        data = await async_get(url, headers={'X-Mashape-Key':
            'GNy1l9QrcUmshewiEylj8w3VdCpVp1tbthojsnpeTXm87VYeaY'})

        if data['result_type'] == 'exact':
            info = random.choice(data['list'])
            definition = '*Definition:* {}\n'.format(
                    info['definition'].translate(markdown_escapes))
            example = '*Example:* {}'.format(
                    info['example'].translate(markdown_escapes))

            await self.bot.sendMessage(self.chat_id,
                                       definition + '\n' + example,
                                       parse_mode='Markdown')
        else:
            await self.bot.sendMessage(self.chat_id, 'Nope')

    @command('`/vape <text>` - A E S T H E T I C S')
    async def vape(self, text):
        def trans(c):
            i = ord(c)
            if ord('!') <= i <= ord('}'):
                return chr(0xFF01 + i - ord('!'))
            else:
                return c
        if text:
            await self.bot.sendMessage(self.chat_id,
                                       ''.join(trans(c) for c in text))
        else:
            await self.bot.sendMessage(self.chat_id, 'ｎｅｖｅｒｍｉｎｄ')

    @command('`/money <amount> <coin> to <coin>` - exchange rate')
    async def money(self, args):
        try:
            amount, fromcoin, _, tocoin, *_ = args.split(' ')
            amount = float(amount)
            fromcoin, tocoin = fromcoin.upper(), tocoin.upper()
        except ValueError:
            await self.bot.sendMessage(self.chat_id,
                    'Wrong format.\nExample: /money 5.9 usd to ars')
            return

        url = ('http://query.yahooapis.com/v1/public/yql?q='
               'select * from yahoo.finance.xchange where pair = "{}{}"'
               '&format=json&&env=store://datatables.org/alltableswithkeys')\
           .format(fromcoin, tocoin)
        data = await async_get(url)

        try:
            rate = float(data['query']['results']['rate']['Rate'])
            text = '{amount} {fromcoin} = {result} {tocoin}'.format(
                        amount=amount,
                        fromcoin=fromcoin,
                        result=amount*rate,
                        tocoin=tocoin)
            await self.bot.sendMessage(self.chat_id, text)
        except (ValueError, KeyError):
            await self.bot.sendMessage(self.chat_id, 'Unknow coin (probably).')

    async def shrek(self):
        if random.random() > 0.2:
            pack = await self.bot.getStickerSet('ShrekLitoris')
            sticker = random.choice(pack['stickers'])
            await self.bot.sendSticker(self.chat_id, sticker['file_id'])
        else:
            with open('allstar.txt') as f:
                await self.bot.sendMessage(self.chat_id, f.read())
                await self.bot.sendMessage(self.chat_id,
                        '`You have been Shrek\'d`', parse_mode='Markdown')
<commit_msg>path things<commit_after>import re
import inspect
import config
import random
import shelve
from os import path

from helpers import async_get, markdown_escapes, command

class Dispatcher:

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

    @command('`/help` - return this')
    async def help(self):
        predicate = lambda f: inspect.ismethod(f) and hasattr(f, 'docstring')
        fns = inspect.getmembers(self, predicate=predicate)

        msg = "*Command list*\n\n"
        for _, f in fns:
            msg += f.docstring + '\n'

        await self.bot.sendMessage(self.chat_id, msg, parse_mode='Markdown')

    #@command('')
    async def test(self, args):
        #data = await async_get('http://localhost:8000/')
        print(path.dirname(__file__))

    @command('`/js <library>` - checks if library is cool or not')
    async def js(self, string):
        def messages(desc, pop):
            n = pop*100
            tmpl = '{}: {}.\nScore: {:.1f} out of 100, {}'
            if n > 90:
                m = 'cool as fuck'
            elif n > 80:
                m = 'pretty ok'
            elif n > 70:
                m = 'meh'
            elif n > 60:
                m = 'old'
            elif n > 50:
                m = 'deprecated'
            elif n > 40:
                m = 'lol seriously?'
            elif n > 30:
                m = 'lol seriously?'
            elif n > 20:
                m = 'please die'
            elif n > 20:
                m = '...'
            elif n > 10:
                m = 'burn your computer'
            else:
                m = 'Exception("<commits suicide>")'

            return tmpl.format(string, desc, n, m)

        if not string:
            return

        data = await async_get(
                'https://api.npms.io/v2/search?q={}'.format(string))

        if data['results']:
            pop = data['results'][0]['score']['detail']['popularity']
            desc = data['results'][0]['package']['description']
            await self.bot.sendMessage(self.chat_id, messages(desc, pop))
        else:
            await self.bot.sendMessage(self.chat_id, 'No')

    @command('`/sadness` - cry')
    async def sadness(self):
        sub = random.choice(['vaporwaveaesthetics',
                             'vaporwaveart',
                             'vaporwave'])

        url = 'https://imgur.com/r/{}/hot.json'.format(sub)

        data = await async_get(url)
        img = random.choice(data['data'])
        text = img['title']
        url = 'https://imgur.com/{}{}'.format(img['hash'], img['ext'])
        await self.bot.sendPhoto(self.chat_id, url, caption=text)

    @command('`/remember <keyword> <text>` - have memories')
    async def remember(self, string):
        """ Splits on spaces and takes first
            word as `keyword` rest as `body`.
            If body is not empty store that in memory,
            else return it.
        """
        if not string:
            return

        with shelve.open('db', 'c') as db:
            keyword, *body = string.split()
            if body:
                db[keyword] = ' '.join(body)
                txt = 'I\'ll remember that'
            else:
                result = db.get(keyword, None)
                if result:
                    txt = '{} = {}'.format(keyword, result)
                else:
                    txt = 'No idea about {}'.format(keyword)
        await self.bot.sendMessage(self.chat_id, txt)

    @command('`/puppy` - good doggo')
    async def puppy(self):
        sub = 'doggos' if random.random() > 0.1 else 'boats'
        url = 'https://imgur.com/r/{}/hot.json'.format(sub)

        data = await async_get(url)

        img = random.choice(data['data'])
        text = img['title']
        url = 'https://imgur.com/{}{}'.format(img['hash'], img['ext'])

        await self.bot.sendPhoto(self.chat_id, url, caption=text)

    @command('`/urban <word>` - cool dict')
    async def urban(self, string):
        url = ('https://mashape-community-urban-dictionary.p.mashape.com/'
               'define?term={}.').format(string)
        data = await async_get(url, headers={'X-Mashape-Key':
            'GNy1l9QrcUmshewiEylj8w3VdCpVp1tbthojsnpeTXm87VYeaY'})

        if data['result_type'] == 'exact':
            info = random.choice(data['list'])
            definition = '*Definition:* {}\n'.format(
                    info['definition'].translate(markdown_escapes))
            example = '*Example:* {}'.format(
                    info['example'].translate(markdown_escapes))

            await self.bot.sendMessage(self.chat_id,
                                       definition + '\n' + example,
                                       parse_mode='Markdown')
        else:
            await self.bot.sendMessage(self.chat_id, 'Nope')

    @command('`/vape <text>` - A E S T H E T I C S')
    async def vape(self, text):
        def trans(c):
            i = ord(c)
            if ord('!') <= i <= ord('}'):
                return chr(0xFF01 + i - ord('!'))
            else:
                return c
        if text:
            await self.bot.sendMessage(self.chat_id,
                                       ''.join(trans(c) for c in text))
        else:
            await self.bot.sendMessage(self.chat_id, 'ｎｅｖｅｒｍｉｎｄ')

    @command('`/money <amount> <coin> to <coin>` - exchange rate')
    async def money(self, args):
        try:
            amount, fromcoin, _, tocoin, *_ = args.split(' ')
            amount = float(amount)
            fromcoin, tocoin = fromcoin.upper(), tocoin.upper()
        except ValueError:
            await self.bot.sendMessage(self.chat_id,
                    'Wrong format.\nExample: /money 5.9 usd to ars')
            return

        url = ('http://query.yahooapis.com/v1/public/yql?q='
               'select * from yahoo.finance.xchange where pair = "{}{}"'
               '&format=json&&env=store://datatables.org/alltableswithkeys')\
           .format(fromcoin, tocoin)
        data = await async_get(url)

        try:
            rate = float(data['query']['results']['rate']['Rate'])
            text = '{amount} {fromcoin} = {result} {tocoin}'.format(
                        amount=amount,
                        fromcoin=fromcoin,
                        result=amount*rate,
                        tocoin=tocoin)
            await self.bot.sendMessage(self.chat_id, text)
        except (ValueError, KeyError):
            await self.bot.sendMessage(self.chat_id, 'Unknow coin (probably).')

    async def shrek(self):
        if random.random() > 0.15:
            pack = await self.bot.getStickerSet('ShrekLitoris')
            sticker = random.choice(pack['stickers'])
            await self.bot.sendSticker(self.chat_id, sticker['file_id'])
        else:
            fname = path.join(path.dirname(__file__), 'allstar.txt')
            with open(fname) as f:
                await self.bot.sendMessage(self.chat_id, f.read())
                await self.bot.sendMessage(self.chat_id,
                        '`You have been Shrek\'d`', parse_mode='Markdown')
<|endoftext|>


<commit_before>import numpy as np
from mushroom.distributions import *
from mushroom.utils.numerical_gradient import numerical_diff_dist

def gaussian():
    print('Testing gaussian distribution...')
    n_dims = 6

    random_matrix = np.random.rand(n_dims, n_dims)
    sigma = random_matrix.dot(random_matrix.T)

    mu = np.random.randn(n_dims)

    dist = GaussianDistribution(mu, sigma)

    for i in range(20):
        theta = dist.sample()
        exact_diff = dist.diff(theta)
        numerical_diff = numerical_diff_dist(dist, theta)

        assert np.allclose(exact_diff, numerical_diff)

def cholesky_gaussian():
    print('Testing gaussian cholesky distribution...')
    n_dims = 6

    random_matrix = np.random.rand(n_dims, n_dims)
    sigma = random_matrix.dot(random_matrix.T)

    mu = np.random.randn(n_dims)

    dist = GaussianCholeskyDistribution(mu, sigma)

    for i in range(20):
        theta = dist.sample()
        exact_diff = dist.diff(theta)
        numerical_diff = numerical_diff_dist(dist, theta)

        assert np.allclose(exact_diff, numerical_diff)

if __name__ == '__main__':
    print('Executing policy test...')

    gaussian()
    cholesky_gaussian()
<commit_msg>pep8 fix<commit_after>import numpy as np
from mushroom.distributions import *
from mushroom.utils.numerical_gradient import numerical_diff_dist

def gaussian():
    print('Testing gaussian distribution...')
    n_dims = 6

    random_matrix = np.random.rand(n_dims, n_dims)
    sigma = random_matrix.dot(random_matrix.T)

    mu = np.random.randn(n_dims)

    dist = GaussianDistribution(mu, sigma)

    for i in range(20):
        theta = dist.sample()
        exact_diff = dist.diff(theta)
        numerical_diff = numerical_diff_dist(dist, theta)

        assert np.allclose(exact_diff, numerical_diff)

def cholesky_gaussian():
    print('Testing gaussian cholesky distribution...')
    n_dims = 6

    random_matrix = np.random.rand(n_dims, n_dims)
    sigma = random_matrix.dot(random_matrix.T)

    mu = np.random.randn(n_dims)

    dist = GaussianCholeskyDistribution(mu, sigma)

    for i in range(20):
        theta = dist.sample()
        exact_diff = dist.diff(theta)
        numerical_diff = numerical_diff_dist(dist, theta)

        assert np.allclose(exact_diff, numerical_diff)

if __name__ == '__main__':
    print('Executing policy test...')

    gaussian()
    cholesky_gaussian()
<|endoftext|>


<commit_before>from django.contrib.auth.models import User
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
from django.utils import timezone
from django.utils.http import urlsafe_base64_encode
from django.utils.encoding import force_bytes
from django.conf import settings
from studygroups.utils import render_to_string_ctx
from studygroups.email_helper import render_html_with_css
from django.core.mail import EmailMultiAlternatives, send_mail

import random
import string

from studygroups.models import Profile
from studygroups.utils import html_body_to_text

def create_user(email, first_name, last_name, password, communication_opt_in=False, interested_in_learning=''):
    """ Create a new user using the email as the username  """

    if password == None:
        password = "".join([random.choice(string.letters) for i in range(64)])

    user = User.objects.create_user(
        email.lower(), #use lowercase email as username
        email, #keep email in original case supplied
        password
    )
    user.first_name = first_name
    user.last_name = last_name
    user.save()

    profile = Profile(user=user)
    profile.communication_opt_in = communication_opt_in
    profile.interested_in_learning = interested_in_learning
    profile.save()
    return user

class EmailConfirmTokenGenerator(PasswordResetTokenGenerator):
    def _make_hash_value(self, user, timestamp):
        # Remove last login timestamp from hash and replace with date
        # that email address was confirmed
        confirm_timestamp = '' if user.profile.email_confirmed_at is None else user.profile.email_confirmed_at.replace(microsecond=0, tzinfo=None)
        return (
            six.text_type(user.pk) + user.password + six.text_type(timestamp) +
            six.text_type(confirm_timestamp)
        )

def generate_user_token(user):
    """ generate token for user to validate user email address """
    return EmailConfirmTokenGenerator().make_token(user)

def check_user_token(user, token):
    return EmailConfirmTokenGenerator().check_token(user, token)

def confirm_user_email(user):
    user.profile.email_confirmed_at = timezone.now()
    user.profile.save()

def send_email_confirm_email(user):
    context = {
        "user": user,
        "profile": user.profile,
        "uid": urlsafe_base64_encode(force_bytes(user.pk)),
        "token": generate_user_token(user),
    }

    subject_template = 'custom_registration/email_confirm-subject.txt'
    email_template = 'custom_registration/email_confirm.txt'
    html_email_template = 'custom_registration/email_confirm.html'

    subject = render_to_string_ctx(subject_template, context).strip(' \n')
    text_body = render_to_string_ctx(email_template, context)
    html_body = render_html_with_css(html_email_template, context)

    to = [user.email]
    email = EmailMultiAlternatives(
        subject,
        text_body,
        settings.DEFAULT_FROM_EMAIL,
        to,
    )
    email.attach_alternative(html_body, 'text/html')
    email.send()

def send_new_user_email(user):
    context = {
        "user": user,
    }

    subject_template = 'custom_registration/new_user_confirmed-subject.txt'
    html_email_template = 'custom_registration/new_user_confirmed.html'

    subject = render_to_string_ctx(subject_template, context).strip(' \n')
    html_body = render_html_with_css(html_email_template, context)
    text_body = html_body_to_text(html_body)

    to = [user.email]
    email = EmailMultiAlternatives(
        subject,
        text_body,
        settings.DEFAULT_FROM_EMAIL,
        to,
    )
    email.attach_alternative(html_body, 'text/html')
    email.send()
<commit_msg>add function to detect email domain and create invitation<commit_after>from django.contrib.auth.models import User
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
from django.utils import timezone
from django.utils.http import urlsafe_base64_encode
from django.utils.encoding import force_bytes
from django.conf import settings
from studygroups.utils import render_to_string_ctx
from studygroups.email_helper import render_html_with_css
from django.core.mail import EmailMultiAlternatives, send_mail

import random
import string
import re

from studygroups.models import Profile
from studygroups.models import Team
from studygroups.models import TeamInvitation
from studygroups.utils import html_body_to_text

def create_user(email, first_name, last_name, password, communication_opt_in=False, interested_in_learning=''):
    """ Create a new user using the email as the username  """

    if password == None:
        password = "".join([random.choice(string.letters) for i in range(64)])

    user = User.objects.create_user(
        email.lower(), #use lowercase email as username
        email, #keep email in original case supplied
        password
    )
    user.first_name = first_name
    user.last_name = last_name
    user.save()

    profile = Profile(user=user)
    profile.communication_opt_in = communication_opt_in
    profile.interested_in_learning = interested_in_learning
    profile.save()
    return user

class EmailConfirmTokenGenerator(PasswordResetTokenGenerator):
    def _make_hash_value(self, user, timestamp):
        # Remove last login timestamp from hash and replace with date
        # that email address was confirmed
        confirm_timestamp = '' if user.profile.email_confirmed_at is None else user.profile.email_confirmed_at.replace(microsecond=0, tzinfo=None)
        return (
            six.text_type(user.pk) + user.password + six.text_type(timestamp) +
            six.text_type(confirm_timestamp)
        )

def generate_user_token(user):
    """ generate token for user to validate user email address """
    return EmailConfirmTokenGenerator().make_token(user)

def check_user_token(user, token):
    return EmailConfirmTokenGenerator().check_token(user, token)

def confirm_user_email(user):
    user.profile.email_confirmed_at = timezone.now()
    user.profile.save()
    generate_team_invitation_by_email_domain(user)

def generate_team_invitation_by_email_domain(user):
    domain = re.search("@([\w.]+)$", user.email)
    team = Team.objects.filter(email_domain=domain.group()[0]).first()
    organizer = team.teammembership_set.filter(role=TeamMembership.ORGANIZER).first()
    if organizer is None:
        logger.error('Team must have an organizer to add other members.')
        return

    if team:
        invitation = TeamInvitation.objects.create(
            team=team,
            organizer=organizer.user,
            email=user.email,
            role=TeamMembership.MEMBER
        )

def send_email_confirm_email(user):
    context = {
        "user": user,
        "profile": user.profile,
        "uid": urlsafe_base64_encode(force_bytes(user.pk)),
        "token": generate_user_token(user),
    }

    subject_template = 'custom_registration/email_confirm-subject.txt'
    email_template = 'custom_registration/email_confirm.txt'
    html_email_template = 'custom_registration/email_confirm.html'

    subject = render_to_string_ctx(subject_template, context).strip(' \n')
    text_body = render_to_string_ctx(email_template, context)
    html_body = render_html_with_css(html_email_template, context)

    to = [user.email]
    email = EmailMultiAlternatives(
        subject,
        text_body,
        settings.DEFAULT_FROM_EMAIL,
        to,
    )
    email.attach_alternative(html_body, 'text/html')
    email.send()

def send_new_user_email(user):
    context = {
        "user": user,
    }

    subject_template = 'custom_registration/new_user_confirmed-subject.txt'
    html_email_template = 'custom_registration/new_user_confirmed.html'

    subject = render_to_string_ctx(subject_template, context).strip(' \n')
    html_body = render_html_with_css(html_email_template, context)
    text_body = html_body_to_text(html_body)

    to = [user.email]
    email = EmailMultiAlternatives(
        subject,
        text_body,
        settings.DEFAULT_FROM_EMAIL,
        to,
    )
    email.attach_alternative(html_body, 'text/html')
    email.send()
<|endoftext|>


<commit_before>#coding: spec

from harpoon.option_spec.harpoon_specs import HarpoonSpec
from harpoon.errors import FailedImage, BadImage
from harpoon.ship.runner import Runner

from tests.helpers import HarpoonCase

from option_merge.converter import Converter
from option_merge import MergedOptions
from input_algorithms.meta import Meta
import codecs
import nose
import mock
import six
import os
import re

mtime = 1431170923

describe HarpoonCase, "Building docker images":
    def make_image(self, options, harpoon_options=None):
        config_root = self.make_temp_dir()
        if harpoon_options is None:
            harpoon_options = {}
        harpoon_options["docker_context"] = self.docker_client
        harpoon_options["docker_context_maker"] = self.new_docker_client

        harpoon = HarpoonSpec().harpoon_spec.normalise(Meta({}, []), harpoon_options)
        if "harpoon" not in options:
            options["harpoon"] = harpoon

        everything = MergedOptions.using({"harpoon": harpoon, "mtime": mtime, "_key_name_1": "awesome_image", "config_root": config_root})

        harpoon_converter = Converter(convert=lambda *args: harpoon, convert_path=["harpoon"])
        everything.add_converter(harpoon_converter)
        everything.converters.activate()

        if "configuration" not in options:
            options["configuration"] = everything
        return HarpoonSpec().image_spec.normalise(Meta(everything, []), options)

    it "can intervene a broken build":
        if six.PY3:
            raise nose.SkipTest()

        called = []
        original_commit_and_run = Runner.commit_and_run
        def commit_and_run(*args, **kwargs):
            kwargs["command"] = "echo 'intervention_goes_here'"
            called.append("commit_and_run")
            return original_commit_and_run(Runner(), *args, **kwargs)
        fake_commit_and_run = mock.Mock(name="commit_and_run", side_effect=commit_and_run)

        commands = ["FROM {0}".format(os.environ["BASE_IMAGE"]), "RUN exit 1"]

        try:
            fake_sys_stdout = self.make_temp_file()
            fake_sys_stderr = self.make_temp_file()
            with mock.patch("harpoon.ship.builder.Runner.commit_and_run", fake_commit_and_run):
                with mock.patch("harpoon.ship.runner.input", lambda *args: 'y\n'):
                    with self.a_built_image({"context": False, "commands": commands}, {"stdout": fake_sys_stdout, "tty_stdout": fake_sys_stdout, "tty_stderr": fake_sys_stderr}) as (cached, conf):
                        pass
        except FailedImage as error:
            expected = re.compile("The command [\[']/bin/sh -c exit 1[\]'] returned a non-zero code: 1")
            assert expected.match(str(error.kwargs["msg"])), "Expected {0} to match {1}".format(str(error.kwargs["msg"]), expected.pattern)
            self.assertEqual(error.kwargs["image"], "awesome_image")

        self.assertEqual(called, ["commit_and_run"])

        with open(fake_sys_stdout.name) as fle:
            output = fle.read().strip()

        if isinstance(output, six.binary_type):
            output = output.decode('utf-8')
        output = '\n'.join([line for line in output.split('\n') if "lxc-start" not in line])

        expected = """
         Step 1 : FROM busybox:buildroot-2014.02
          ---> 8c2e06607696
         Step 2 : RUN exit 1
          ---> Running in .+
         !!!!
         It would appear building the image failed
         Do you want to run /bin/bash where the build to help debug why it failed?
         intervention_goes_here
        """

        self.assertReMatchLines(expected, output)

    it "can intervene a broken container":
        if six.PY3:
            raise nose.SkipTest()

        called = []
        original_commit_and_run = Runner.commit_and_run
        def commit_and_run(*args, **kwargs):
            kwargs["command"] = "echo 'intervention_goes_here'"
            called.append("commit_and_run")
            return original_commit_and_run(Runner(), *args, **kwargs)
        fake_commit_and_run = mock.Mock(name="commit_and_run", side_effect=commit_and_run)

        commands = ["FROM {0}".format(os.environ["BASE_IMAGE"]), "CMD /bin/sh -c 'exit 1'"]

        try:
            fake_sys_stdout = self.make_temp_file()
            fake_sys_stderr = self.make_temp_file()
            with mock.patch("harpoon.ship.builder.Runner.commit_and_run", fake_commit_and_run):
                with mock.patch("harpoon.ship.runner.input", lambda *args: 'y\n'):
                    with self.a_built_image({"context": False, "commands": commands}, {"stdout": fake_sys_stdout, "tty_stdout": fake_sys_stdout, "tty_stderr": fake_sys_stderr}) as (cached, conf):
                        Runner().run_container(conf, {conf.name: conf})
        except BadImage as error:
            assert "Failed to run container" in str(error)

        self.assertEqual(called, ["commit_and_run"])

        with codecs.open(fake_sys_stdout.name) as fle:
            output = fle.read().strip()

        if isinstance(output, six.binary_type):
            output = output.decode('utf-8')
        output = '\n'.join([line for line in output.split('\n') if "lxc-start" not in line])

        expected = """
         Step 1 : FROM busybox:buildroot-2014.02
          ---> 8c2e06607696
         Step 2 : CMD ['/bin/sh', '-c', 'exit 1']
          ---> Running in .+
          --->
         Removing intermediate container .+
         Successfully built .+
         !!!!
         Failed to run the container!
         Do you want commit the container in it's current state and /bin/bash into it to debug?
         intervention_goes_here
        """

        self.assertReMatchLines(expected, output)

    it "can intervene a broken container with the tty starting":
        if six.PY3:
            raise nose.SkipTest()

        called = []
        original_commit_and_run = Runner.commit_and_run
        def commit_and_run(*args, **kwargs):
            kwargs["command"] = "echo 'intervention_goes_here'"
            called.append("commit_and_run")
            return original_commit_and_run(Runner(), *args, **kwargs)
        fake_commit_and_run = mock.Mock(name="commit_and_run", side_effect=commit_and_run)

        commands = ["FROM {0}".format(os.environ["BASE_IMAGE"]), '''CMD echo 'hi'; sleep 1; exit 1''']

        try:
            fake_sys_stdout = self.make_temp_file()
            fake_sys_stderr = self.make_temp_file()
            with mock.patch("harpoon.ship.builder.Runner.commit_and_run", fake_commit_and_run):
                with mock.patch("harpoon.ship.runner.input", lambda *args: 'y\n'):
                    with self.a_built_image({"context": False, "commands": commands}, {"stdout": fake_sys_stdout, "tty_stdout": fake_sys_stdout, "tty_stderr": fake_sys_stderr}) as (cached, conf):
                        Runner().run_container(conf, {conf.name: conf})
        except BadImage as error:
            print(error)
            assert "Failed to run container" in str(error)

        self.assertEqual(called, ["commit_and_run"])

        with codecs.open(fake_sys_stdout.name) as fle:
            output = fle.read().strip()

        if isinstance(output, six.binary_type):
            output = output.decode('utf-8')
        output = '\n'.join([line for line in output.split('\n') if "lxc-start" not in line])

        expected = """
         Step 1 : FROM busybox:buildroot-2014.02
          ---> 8c2e06607696
         Step 2 : CMD echo 'hi'; sleep 1; exit 1
          ---> Running in .+
          ---> .+
         Removing intermediate container .+
         Successfully built .+
         hi
         !!!!
         Failed to run the container!
         Do you want commit the container in it's current state and /bin/bash into it to debug?
         intervention_goes_here
        """

        self.assertReMatchLines(expected, output)

<commit_msg>The hash changed<commit_after>#coding: spec

from harpoon.option_spec.harpoon_specs import HarpoonSpec
from harpoon.errors import FailedImage, BadImage
from harpoon.ship.runner import Runner

from tests.helpers import HarpoonCase

from option_merge.converter import Converter
from option_merge import MergedOptions
from input_algorithms.meta import Meta
import codecs
import nose
import mock
import six
import os
import re

mtime = 1431170923

describe HarpoonCase, "Building docker images":
    def make_image(self, options, harpoon_options=None):
        config_root = self.make_temp_dir()
        if harpoon_options is None:
            harpoon_options = {}
        harpoon_options["docker_context"] = self.docker_client
        harpoon_options["docker_context_maker"] = self.new_docker_client

        harpoon = HarpoonSpec().harpoon_spec.normalise(Meta({}, []), harpoon_options)
        if "harpoon" not in options:
            options["harpoon"] = harpoon

        everything = MergedOptions.using({"harpoon": harpoon, "mtime": mtime, "_key_name_1": "awesome_image", "config_root": config_root})

        harpoon_converter = Converter(convert=lambda *args: harpoon, convert_path=["harpoon"])
        everything.add_converter(harpoon_converter)
        everything.converters.activate()

        if "configuration" not in options:
            options["configuration"] = everything
        return HarpoonSpec().image_spec.normalise(Meta(everything, []), options)

    it "can intervene a broken build":
        if six.PY3:
            raise nose.SkipTest()

        called = []
        original_commit_and_run = Runner.commit_and_run
        def commit_and_run(*args, **kwargs):
            kwargs["command"] = "echo 'intervention_goes_here'"
            called.append("commit_and_run")
            return original_commit_and_run(Runner(), *args, **kwargs)
        fake_commit_and_run = mock.Mock(name="commit_and_run", side_effect=commit_and_run)

        commands = ["FROM {0}".format(os.environ["BASE_IMAGE"]), "RUN exit 1"]

        try:
            fake_sys_stdout = self.make_temp_file()
            fake_sys_stderr = self.make_temp_file()
            with mock.patch("harpoon.ship.builder.Runner.commit_and_run", fake_commit_and_run):
                with mock.patch("harpoon.ship.runner.input", lambda *args: 'y\n'):
                    with self.a_built_image({"context": False, "commands": commands}, {"stdout": fake_sys_stdout, "tty_stdout": fake_sys_stdout, "tty_stderr": fake_sys_stderr}) as (cached, conf):
                        pass
        except FailedImage as error:
            expected = re.compile("The command [\[']/bin/sh -c exit 1[\]'] returned a non-zero code: 1")
            assert expected.match(str(error.kwargs["msg"])), "Expected {0} to match {1}".format(str(error.kwargs["msg"]), expected.pattern)
            self.assertEqual(error.kwargs["image"], "awesome_image")

        self.assertEqual(called, ["commit_and_run"])

        with open(fake_sys_stdout.name) as fle:
            output = fle.read().strip()

        if isinstance(output, six.binary_type):
            output = output.decode('utf-8')
        output = '\n'.join([line for line in output.split('\n') if "lxc-start" not in line])

        expected = """
         Step 1 : FROM busybox:buildroot-2014.02
          ---> [a-zA-Z0-9]{12}
         Step 2 : RUN exit 1
          ---> Running in .+
         !!!!
         It would appear building the image failed
         Do you want to run /bin/bash where the build to help debug why it failed?
         intervention_goes_here
        """

        self.assertReMatchLines(expected, output)

    it "can intervene a broken container":
        if six.PY3:
            raise nose.SkipTest()

        called = []
        original_commit_and_run = Runner.commit_and_run
        def commit_and_run(*args, **kwargs):
            kwargs["command"] = "echo 'intervention_goes_here'"
            called.append("commit_and_run")
            return original_commit_and_run(Runner(), *args, **kwargs)
        fake_commit_and_run = mock.Mock(name="commit_and_run", side_effect=commit_and_run)

        commands = ["FROM {0}".format(os.environ["BASE_IMAGE"]), "CMD /bin/sh -c 'exit 1'"]

        try:
            fake_sys_stdout = self.make_temp_file()
            fake_sys_stderr = self.make_temp_file()
            with mock.patch("harpoon.ship.builder.Runner.commit_and_run", fake_commit_and_run):
                with mock.patch("harpoon.ship.runner.input", lambda *args: 'y\n'):
                    with self.a_built_image({"context": False, "commands": commands}, {"stdout": fake_sys_stdout, "tty_stdout": fake_sys_stdout, "tty_stderr": fake_sys_stderr}) as (cached, conf):
                        Runner().run_container(conf, {conf.name: conf})
        except BadImage as error:
            assert "Failed to run container" in str(error)

        self.assertEqual(called, ["commit_and_run"])

        with codecs.open(fake_sys_stdout.name) as fle:
            output = fle.read().strip()

        if isinstance(output, six.binary_type):
            output = output.decode('utf-8')
        output = '\n'.join([line for line in output.split('\n') if "lxc-start" not in line])

        expected = """
         Step 1 : FROM busybox:buildroot-2014.02
          ---> [a-zA-Z0-9]{12}
         Step 2 : CMD ['/bin/sh', '-c', 'exit 1']
          ---> Running in .+
          --->
         Removing intermediate container .+
         Successfully built .+
         !!!!
         Failed to run the container!
         Do you want commit the container in it's current state and /bin/bash into it to debug?
         intervention_goes_here
        """

        self.assertReMatchLines(expected, output)

    it "can intervene a broken container with the tty starting":
        if six.PY3:
            raise nose.SkipTest()

        called = []
        original_commit_and_run = Runner.commit_and_run
        def commit_and_run(*args, **kwargs):
            kwargs["command"] = "echo 'intervention_goes_here'"
            called.append("commit_and_run")
            return original_commit_and_run(Runner(), *args, **kwargs)
        fake_commit_and_run = mock.Mock(name="commit_and_run", side_effect=commit_and_run)

        commands = ["FROM {0}".format(os.environ["BASE_IMAGE"]), '''CMD echo 'hi'; sleep 1; exit 1''']

        try:
            fake_sys_stdout = self.make_temp_file()
            fake_sys_stderr = self.make_temp_file()
            with mock.patch("harpoon.ship.builder.Runner.commit_and_run", fake_commit_and_run):
                with mock.patch("harpoon.ship.runner.input", lambda *args: 'y\n'):
                    with self.a_built_image({"context": False, "commands": commands}, {"stdout": fake_sys_stdout, "tty_stdout": fake_sys_stdout, "tty_stderr": fake_sys_stderr}) as (cached, conf):
                        Runner().run_container(conf, {conf.name: conf})
        except BadImage as error:
            print(error)
            assert "Failed to run container" in str(error)

        self.assertEqual(called, ["commit_and_run"])

        with codecs.open(fake_sys_stdout.name) as fle:
            output = fle.read().strip()

        if isinstance(output, six.binary_type):
            output = output.decode('utf-8')
        output = '\n'.join([line for line in output.split('\n') if "lxc-start" not in line])

        expected = """
         Step 1 : FROM busybox:buildroot-2014.02
          ---> [a-zA-Z0-9]{12}
         Step 2 : CMD echo 'hi'; sleep 1; exit 1
          ---> Running in .+
          ---> .+
         Removing intermediate container .+
         Successfully built .+
         hi
         !!!!
         Failed to run the container!
         Do you want commit the container in it's current state and /bin/bash into it to debug?
         intervention_goes_here
        """

        self.assertReMatchLines(expected, output)

<|endoftext|>


<commit_before>"""
Copyright 2013 Steven Diamond

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

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

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

from cvxpy.atoms import quad_form, quad_over_lin, matrix_frac, sum_squares, norm, max
from cvxpy.atoms.affine.vstack import vstack
from cvxpy.atoms.elementwise.power import power
from cvxpy.expressions.expression import *
from cvxpy.expressions.variable import Variable
from cvxpy import Problem, Minimize
import unittest
from cvxpy.tests.base_test import BaseTest
import numpy as np
import warnings

class TestExpressions(BaseTest):
    """ Unit tests for the expression/expression module. """

    def setUp(self):
        pass

    # Test elementwise power
    def test_power(self):
        x = Variable(3)
        y = Variable(3)
        self.assertFalse(x.is_constant())
        self.assertTrue(x.is_affine())
        self.assertTrue(x.is_quadratic())

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = power(x.T*y, 0)
            self.assertTrue(s.is_constant())
            self.assertTrue(s.is_affine())
            self.assertTrue(s.is_quadratic())

        t = power(x-y, 1)
        self.assertFalse(t.is_constant())
        self.assertTrue(t.is_affine())
        self.assertTrue(t.is_quadratic())

        u = power(x+2*y, 2)
        self.assertFalse(u.is_constant())
        self.assertFalse(u.is_affine())
        self.assertTrue(u.is_quadratic())
        self.assertTrue(u.is_dcp())

        w = (x+2*y)**2
        self.assertFalse(w.is_constant())
        self.assertFalse(w.is_affine())
        self.assertTrue(w.is_quadratic())
        self.assertTrue(w.is_dcp())

    def test_matrix_multiplication(self):
        x = Variable((3, 5))
        y = Variable((3, 5))
        self.assertFalse(x.is_constant())
        self.assertTrue(x.is_affine())
        self.assertTrue(x.is_quadratic())

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = x.T*y
        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertTrue(s.is_quadratic())
        self.assertFalse(s.is_dcp())

    def test_quad_over_lin(self):
        x = Variable((3, 5))
        y = Variable((3, 5))
        z = Variable()
        s = quad_over_lin(x-y, z)
        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertFalse(s.is_quadratic())
        self.assertTrue(s.is_dcp())

        t = quad_over_lin(x+2*y, 5)
        self.assertFalse(t.is_constant())
        self.assertFalse(t.is_affine())
        self.assertTrue(t.is_quadratic())
        self.assertTrue(t.is_dcp())

    def test_matrix_frac(self):
        x = Variable(5)
        M = np.eye(5)
        P = M.T*M
        s = matrix_frac(x, P)
        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertTrue(s.is_quadratic())
        self.assertTrue(s.is_dcp())

    def test_quadratic_form(self):
        x = Variable(5)
        P = np.eye(5) - 2*np.ones((5, 5))
        q = np.asmatrix(np.ones((5, 1)))
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = x.T*P*x + q.T*x
        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertTrue(s.is_quadratic())
        self.assertFalse(s.is_dcp())

    def test_sum_squares(self):
        X = Variable((5, 4))
        P = np.ones((3, 5))
        Q = np.ones((4, 7))
        M = np.ones((3, 7))

        y = P*X*Q + M
        self.assertFalse(y.is_constant())
        self.assertTrue(y.is_affine())
        self.assertTrue(y.is_quadratic())
        self.assertTrue(y.is_dcp())

        s = sum_squares(y)
        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertTrue(s.is_quadratic())
        self.assertTrue(s.is_dcp())

        # Frobenius norm squared is indeed quadratic
        # but can't show quadraticity using recursive rules
        t = norm(y, 'fro')**2
        self.assertFalse(t.is_constant())
        self.assertFalse(t.is_affine())
        self.assertFalse(t.is_quadratic())
        self.assertTrue(t.is_dcp())

    def test_indefinite_quadratic(self):
        x = Variable()
        y = Variable()
        z = Variable()
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = y*z
            self.assertTrue(s.is_quadratic())
            self.assertFalse(s.is_dcp())

            t = (x+y)**2 - s - z*z
            self.assertTrue(t.is_quadratic())
            self.assertFalse(t.is_dcp())

    def test_non_quadratic(self):
        x = Variable()
        y = Variable()
        z = Variable()

        s = max(vstack([x, y, z]))**2
        self.assertFalse(s.is_quadratic())

        t = max(vstack([x**2, power(y, 2), z]))
        self.assertFalse(t.is_quadratic())

    def test_affine_prod(self):
        x = Variable((3, 5))
        y = Variable(5, 4)

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = x*y

        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertTrue(s.is_quadratic())
        self.assertFalse(s.is_dcp())
<commit_msg>Fixed quadratic test<commit_after>"""
Copyright 2013 Steven Diamond

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

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

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

from cvxpy.atoms import quad_form, quad_over_lin, matrix_frac, sum_squares, norm, max
from cvxpy.atoms.affine.vstack import vstack
from cvxpy.atoms.elementwise.power import power
from cvxpy.expressions.expression import *
from cvxpy.expressions.variable import Variable
from cvxpy import Problem, Minimize
import unittest
from cvxpy.tests.base_test import BaseTest
import numpy as np
import warnings

class TestExpressions(BaseTest):
    """ Unit tests for the expression/expression module. """

    def setUp(self):
        pass

    # Test elementwise power
    def test_power(self):
        x = Variable(3)
        y = Variable(3)
        self.assertFalse(x.is_constant())
        self.assertTrue(x.is_affine())
        self.assertTrue(x.is_quadratic())

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = power(x.T*y, 0)
            self.assertTrue(s.is_constant())
            self.assertTrue(s.is_affine())
            self.assertTrue(s.is_quadratic())

        t = power(x-y, 1)
        self.assertFalse(t.is_constant())
        self.assertTrue(t.is_affine())
        self.assertTrue(t.is_quadratic())

        u = power(x+2*y, 2)
        self.assertFalse(u.is_constant())
        self.assertFalse(u.is_affine())
        self.assertTrue(u.is_quadratic())
        self.assertTrue(u.is_dcp())

        w = (x+2*y)**2
        self.assertFalse(w.is_constant())
        self.assertFalse(w.is_affine())
        self.assertTrue(w.is_quadratic())
        self.assertTrue(w.is_dcp())

    def test_matrix_multiplication(self):
        x = Variable((3, 5))
        y = Variable((3, 5))
        self.assertFalse(x.is_constant())
        self.assertTrue(x.is_affine())
        self.assertTrue(x.is_quadratic())

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = x.T*y
        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertTrue(s.is_quadratic())
        self.assertFalse(s.is_dcp())

    def test_quad_over_lin(self):
        x = Variable((3, 5))
        y = Variable((3, 5))
        z = Variable()
        s = quad_over_lin(x-y, z)
        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertFalse(s.is_quadratic())
        self.assertTrue(s.is_dcp())

        t = quad_over_lin(x+2*y, 5)
        self.assertFalse(t.is_constant())
        self.assertFalse(t.is_affine())
        self.assertTrue(t.is_quadratic())
        self.assertTrue(t.is_dcp())

    def test_matrix_frac(self):
        x = Variable(5)
        M = np.eye(5)
        P = M.T*M
        s = matrix_frac(x, P)
        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertTrue(s.is_quadratic())
        self.assertTrue(s.is_dcp())

    def test_quadratic_form(self):
        x = Variable(5)
        P = np.eye(5) - 2*np.ones((5, 5))
        q = np.asmatrix(np.ones((5, 1)))
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = x.T*P*x + q.T*x
        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertTrue(s.is_quadratic())
        self.assertFalse(s.is_dcp())

    def test_sum_squares(self):
        X = Variable((5, 4))
        P = np.ones((3, 5))
        Q = np.ones((4, 7))
        M = np.ones((3, 7))

        y = P*X*Q + M
        self.assertFalse(y.is_constant())
        self.assertTrue(y.is_affine())
        self.assertTrue(y.is_quadratic())
        self.assertTrue(y.is_dcp())

        s = sum_squares(y)
        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertTrue(s.is_quadratic())
        self.assertTrue(s.is_dcp())

        # Frobenius norm squared is indeed quadratic
        # but can't show quadraticity using recursive rules
        t = norm(y, 'fro')**2
        self.assertFalse(t.is_constant())
        self.assertFalse(t.is_affine())
        self.assertFalse(t.is_quadratic())
        self.assertTrue(t.is_dcp())

    def test_indefinite_quadratic(self):
        x = Variable()
        y = Variable()
        z = Variable()
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = y*z
            self.assertTrue(s.is_quadratic())
            self.assertFalse(s.is_dcp())

            t = (x+y)**2 - s - z*z
            self.assertTrue(t.is_quadratic())
            self.assertFalse(t.is_dcp())

    def test_non_quadratic(self):
        x = Variable()
        y = Variable()
        z = Variable()

        s = max(vstack([x, y, z]))**2
        self.assertFalse(s.is_quadratic())

        t = max(vstack([x**2, power(y, 2), z]))
        self.assertFalse(t.is_quadratic())

    def test_affine_prod(self):
        x = Variable((3, 5))
        y = Variable((5, 4))

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = x*y

        self.assertFalse(s.is_constant())
        self.assertFalse(s.is_affine())
        self.assertTrue(s.is_quadratic())
        self.assertFalse(s.is_dcp())
<|endoftext|>


<commit_before>"""
Smart contract API to easily react to events from specific smart contracts.
"""
import time

from collections import defaultdict
from functools import wraps

from neo.EventHub import events, SmartContractEvent

class SmartContract:
    """
    SmartContract is a helper for dApps to interact with smart contracts.
    In the current version it allows to get event callbackes, for instance
    on smart contract calls to `Runtime.Notify` or `Runtime.Log`:

        from neo.contrib.smartcontract import SmartContract

        smart_contract = SmartContract("6537b4bd100e514119e3a7ab49d520d20ef2c2a4")

        @smart_contract.on_notify
        def sc_notify(event):
            print("SmartContract Runtime.Notify event:", event)
            if len(event.event_payload):
                print(event.event_payload[0].decode("utf-8"))

    Handlers receive as `event` argument an instance of the `neo.EventHub.SmartContractEvent`
    object. It has the following properties:

    - event_type (str)
    - contract_hash (UInt160)
    - tx_hash (UInt256)
    - block_number (int)
    - event_payload (object[])
    - execution_success (bool)
    - test_mode (bool)

    `event_payload` is always a list of objects, depending on what data types you
    sent in the smart contract.
    """
    contract_hash = None
    event_handlers = None

    def __init__(self, contract_hash):
        assert contract_hash
        self.contract_hash = str(contract_hash)
        self.event_handlers = defaultdict(list)

        # Handle EventHub events for SmartContract decorators
        @events.on(SmartContractEvent.RUNTIME_NOTIFY)
        @events.on(SmartContractEvent.RUNTIME_LOG)
        @events.on(SmartContractEvent.EXECUTION_SUCCESS)
        @events.on(SmartContractEvent.EXECUTION_FAIL)
        @events.on(SmartContractEvent.STORAGE)
        def call_on_event(sc_event):
            # Make sure this event is for this specific smart contract
            if str(sc_event.contract_hash) != self.contract_hash:
                return

            # call event handlers
            handlers = set(self.event_handlers["*"] + self.event_handlers[sc_event.event_type.rpartition('.')[0] + ".*"] + self.event_handlers[sc_event.event_type])  # set(..) removes duplicates
            [event_handler(sc_event) for event_handler in handlers]

    def on_any(self, func):
        """ @on_any decorator: calls method on any event for this smart contract """
        return self._add_decorator("*", func)

    def on_notify(self, func):
        """ @on_notify decorator: calls method on Runtime.Notify events """
        return self._add_decorator(SmartContractEvent.RUNTIME_NOTIFY, func)

    def on_log(self, func):
        """ @on_log decorator: calls method on Runtime.Log events """
        return self._add_decorator(SmartContractEvent.RUNTIME_LOG, func)

    def on_storage(self, func):
        """ @on_storage decorator: calls method on Neo.Storage.* events """
        return self._add_decorator(SmartContractEvent.STORAGE, func)

    def on_execution(self, func):
        """ @on_execution decorator: calls method on Neo.Execution.* events """
        return self._add_decorator(SmartContractEvent.EXECUTION, func)

    def _add_decorator(self, event_type, func):
        # First, add handler function to handlers
        self.event_handlers[event_type].append(func)

        # Return the wrapper
        @wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)
        return wrapper
<commit_msg>Allow SmartContract to use a hash with or without 0x prefix<commit_after>"""
Smart contract API to easily react to events from specific smart contracts.
"""
import time

from collections import defaultdict
from functools import wraps

from neo.EventHub import events, SmartContractEvent

class SmartContract:
    """
    SmartContract is a helper for dApps to interact with smart contracts.
    In the current version it allows to get event callbackes, for instance
    on smart contract calls to `Runtime.Notify` or `Runtime.Log`:

        from neo.contrib.smartcontract import SmartContract

        smart_contract = SmartContract("6537b4bd100e514119e3a7ab49d520d20ef2c2a4")

        @smart_contract.on_notify
        def sc_notify(event):
            print("SmartContract Runtime.Notify event:", event)
            if len(event.event_payload):
                print(event.event_payload[0].decode("utf-8"))

    Handlers receive as `event` argument an instance of the `neo.EventHub.SmartContractEvent`
    object. It has the following properties:

    - event_type (str)
    - contract_hash (UInt160)
    - tx_hash (UInt256)
    - block_number (int)
    - event_payload (object[])
    - execution_success (bool)
    - test_mode (bool)

    `event_payload` is always a list of objects, depending on what data types you
    sent in the smart contract.
    """
    contract_hash = None
    event_handlers = None

    def __init__(self, contract_hash):
        """
        Instantiate a new SmartContract for a specific contract_hash
        contract_hash can optionally be prefixed with '0x'
        """
        assert contract_hash
        self.contract_hash = str(contract_hash)

        if self.contract_hash[:2] == "0x":
            self.contract_hash = self.contract_hash[2:]

        # Handle EventHub events for SmartContract decorators
        self.event_handlers = defaultdict(list)
        @events.on(SmartContractEvent.RUNTIME_NOTIFY)
        @events.on(SmartContractEvent.RUNTIME_LOG)
        @events.on(SmartContractEvent.EXECUTION_SUCCESS)
        @events.on(SmartContractEvent.EXECUTION_FAIL)
        @events.on(SmartContractEvent.STORAGE)
        def call_on_event(sc_event):
            # Make sure this event is for this specific smart contract
            if str(sc_event.contract_hash) != self.contract_hash:
                return

            # call event handlers
            handlers = set(self.event_handlers["*"] + self.event_handlers[sc_event.event_type.rpartition('.')[0] + ".*"] + self.event_handlers[sc_event.event_type])  # set(..) removes duplicates
            [event_handler(sc_event) for event_handler in handlers]

    def on_any(self, func):
        """ @on_any decorator: calls method on any event for this smart contract """
        return self._add_decorator("*", func)

    def on_notify(self, func):
        """ @on_notify decorator: calls method on Runtime.Notify events """
        return self._add_decorator(SmartContractEvent.RUNTIME_NOTIFY, func)

    def on_log(self, func):
        """ @on_log decorator: calls method on Runtime.Log events """
        return self._add_decorator(SmartContractEvent.RUNTIME_LOG, func)

    def on_storage(self, func):
        """ @on_storage decorator: calls method on Neo.Storage.* events """
        return self._add_decorator(SmartContractEvent.STORAGE, func)

    def on_execution(self, func):
        """ @on_execution decorator: calls method on Neo.Execution.* events """
        return self._add_decorator(SmartContractEvent.EXECUTION, func)

    def _add_decorator(self, event_type, func):
        # First, add handler function to handlers
        self.event_handlers[event_type].append(func)

        # Return the wrapper
        @wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)
        return wrapper
<|endoftext|>


<commit_before>import cybox.utils as utils
import cybox.bindings.cybox_common_types_1_0 as common_types_binding
import cybox.bindings.mutex_object_1_3 as mutex_binding
from cybox.common.baseobjectattribute import Base_Object_Attribute

class Mutex(object):
    def __init__(self):
        pass

    @classmethod
    def object_from_dict(cls, mutex_dict, mutex_obj = None):
        """Create the Mutex Object object representation from an input dictionary"""
        if mutex_obj == None:
            mutex_obj = mutex_binding.MutexObjectType()
            mutex_obj.set_anyAttributes_({'xsi:type' : 'MutexObj:MutexObjectType'})

        for key, value in mutex_dict.items():
            if key == 'name' and utils.test_value(value):
                mutex_obj.set_Name(Base_Object_Attribute.object_from_dict(common_types_binding.StringObjectAttributeType(datatype='String'),value))
                mutex_obj.set_named(True)
            elif key == 'named' and utils.test_value(value):
                mutex_obj.set_named(value)

        return mutex_obj

    @classmethod
    def dict_from_object(cls, mutex_obj):
        """Parse and return a dictionary for a Mutex Object object"""
        mutex_dict = {}
        if mutex_obj.get_Name() is not None: mutex_dict['name'] = Base_Object_Attribute.dict_from_object(mutex_obj.get_Name())
        if mutex_obj.get_named() is not None: mutex_dict['named'] = mutex_obj.get_named()

        return mutex_dict
<commit_msg>Added simple API implementation for mutex object<commit_after>import cybox
import cybox.utils as utils
import cybox.bindings.mutex_object_1_3 as mutex_binding
from cybox.common import DefinedObject, String

class Mutex(DefinedObject):
    def __init__(self):
        self.named = None
        self.name = None

    def to_obj(self, mutex_obj = None):
        if mutex_obj == None:
            mutex_obj = mutex_binding.MutexObjectType()
            mutex_obj.set_anyAttributes_({'xsi:type' : 'MutexObj:MutexObjectType'})

        if self.named is not None: mutex_obj.set_named(self.named)
        if self.name is not None: mutex_obj.set_Name(self.name.to_obj())

        return mutex_obj

    def to_dict(self):
        mutex_dict = {}

        if self.named is not None: mutex_dict['named'] = self.named
        if self.name is not None: mutex_dict['name'] = self.name.to_dict()

        return mutex_dict

    @staticmethod
    def from_dict(mutex_dict, mutex_cls = None):
        if not mutex_dict:
            return None
        if mutex_cls == None:
            mutex_ = Mutex()
        else:
            mutex_ = mutex_cls

        mutex_.named = mutex_dict.get('named')
        mutex_.name = String.from_dict(mutex_dict.get('name'))

        return mutex_

    @staticmethod
    def from_obj(mutex_obj, mutex_cls = None):
        if not mutex_obj:
            return None
        if mutex_cls == None:
            mutex_ = Mutex()
        else:
            mutex_ = mutex_cls

        mutex_.named = mutex_obj.get_named()
        mutex_.name = String.from_dict(mutex_obj.get_Name())

        return mutex_<|endoftext|>


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

import requests

class FirefoxLocale:

    def __init__(self, locale, version_info):
        self.lang = locale
        self.versions = self.parse_versions(version_info)

    def parse_versions(self, version_info):
        """Parses a dict and returns a list of Firefox versions,
        ignoring Aurora and ESR builds.

        :param version_info: {string:string} A dictionary with k,v of version and
        os.

        :returns: [string] A list of Firefox versions
        """
        versions = []
        for version, os in version_info.items():
            # remove all aurora builds and esr builds
            if 'a' not in version and 'esr' not in version:
                versions.append(version)
        return versions

    def __repr__(self):
        return self.locale

_product_details_url = 'https://product-details.mozilla.org/1.0/%s'  # mirror of ship-it, not behind vpn
_ship_it_url = 'https://ship-it.mozilla.org/json/1.0/%s'  # behind vpn
_firefox_primary_builds_uri = 'firefox_primary_builds.json'
_firefox_versions_uri = 'firefox_versions.json'
# mappings adapted off of https://github.com/mozilla-releng/ship-it/blob/master/kickoff/config.py
releng_to_bouncer_alias_dict = {
    'FIREFOX_AURORA': 'firefox-aurora-latest',
    'FIREFOX_ESR_NEXT': 'firefox-esr-latest',
    'LATEST_FIREFOX_VERSION': 'firefox-latest',
    'FIREFOX_ESR': None,  # no checks to run on this product
    'FIREFOX_NIGHTLY': 'firefox-nightly-latest',
    'LATEST_FIREFOX_OLDER_VERSION': None,  # no checks to run on this product
    'LATEST_FIREFOX_RELEASED_DEVEL_VERSION': 'firefox-beta-latest',
    'LATEST_FIREFOX_DEVEL_VERSION': 'firefox-beta-latest'
}

def generate_fx_alias_ver_mappings(releng_products, alias_map=releng_to_bouncer_alias_dict):
    """Parses a dictionary that contains releng product/versions and returns
    a new dictionary that maps go-bouncer aliases to expected Firefox product
    versions.

    As the alias_map is walked, aliases with a value
    of type None will not be included in the returned dictionary.

    The key/val mappings that currently represent the system are
    stored in releng_to_bouncer_alias_dict. Currently there are 2 releng products
    that do not have associated go-bouncer aliases and have val = None.

    :param product_versions: {string:string} the releng object to be walked.
        releng_product/product_version
    :param alias_map: {string:string} releng to Bouncer alias mappings.
        The default, releng_to_bouncer_alias_dict is adapted from
        https://github.com/mozilla-releng/ship-it/blob/master/kickoff/config.py

    :returns: {string:string} with product aliases and their version numbers"""
    aliases_and_versions = {}
    # create a dict that has {alias: expected_version_num}
    for product, alias in alias_map.iteritems():
        # values of None represent products that don't have aliases
        if alias is not None:
            aliases_and_versions[alias] = releng_products[product]
    return aliases_and_versions

def fetch_current_fx_product_details(base_url=_product_details_url):
    """Fetches JSON containing key/val pairings of the current releng aliases
    for Firefox and version numbers as known by Mozilla's Release Engineering Team.

    Release Engineering maintains an up-to-date JSON file with the current
    Firefox release values - https://product-details.mozilla.org/1.0/firefox_versions.json.

    :param base_url: The address of the server to pull product details from.

    :returns: {string:string} dictionary with releng aliases and version numbers"""
    url = base_url % _firefox_versions_uri
    response = requests.get(url)
    response.raise_for_status()
    releng_products = response.json()
    return releng_products

def get_version_info_for_alias(alias, base_url=_product_details_url):
    """Using up-to-date information drawn from product details service, find the current version of
    a Firefox product.

    releng_to_bouncer_alias_dict lists the available aliases.

    :param alias: The go-bouncer alias to get the version of. For example, 'firefox-latest'
    :param base_url: The url of the service that hosts the product details.
    :return String: The version number of Firefox
    """
    products = get_product_mappings()
    return products[alias]

def get_firefox_locales():
    """Fetches build versions for each localization of Firefox from Mozilla's Release
    Engineering Team.

    Release Engineering maintains an up-to-date JSON file with the current
    Firefox release values - https://product-details.mozilla.org/1.0/firefox_primary_builds.json.

    :returns list: [FirefoxLocale objects] a list of FirefoxLocale objects.
    """
    locale_objs = []
    url = _product_details_url % _firefox_primary_builds_uri
    response = requests.get(url)
    response.raise_for_status()
    locale_data = response.json()
    for locale in locale_data:
        versions = locale_data[locale]
        locale_objs.append(FirefoxLocale(locale, versions))
    return locale_objs

def get_product_mappings(base_url=_product_details_url):
    """Get a current list of product details, primary Firefox aliases and the
    current version numbers.
    The default url that product information is pulled from is https://product-details.mozilla.org/1.0/firefox_versions.json

    :param base_url: The url of the service that hosts the product details.
    :return dictionary: Product aliases and version numbers
    """
    releng_aliases = fetch_current_fx_product_details(base_url)
    return generate_fx_alias_ver_mappings(releng_aliases)
<commit_msg>pass in base_url var<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import requests

class FirefoxLocale:

    def __init__(self, locale, version_info):
        self.lang = locale
        self.versions = self.parse_versions(version_info)

    def parse_versions(self, version_info):
        """Parses a dict and returns a list of Firefox versions,
        ignoring Aurora and ESR builds.

        :param version_info: {string:string} A dictionary with k,v of version and
        os.

        :returns: [string] A list of Firefox versions
        """
        versions = []
        for version, os in version_info.items():
            # remove all aurora builds and esr builds
            if 'a' not in version and 'esr' not in version:
                versions.append(version)
        return versions

    def __repr__(self):
        return self.locale

_product_details_url = 'https://product-details.mozilla.org/1.0/%s'  # mirror of ship-it, not behind vpn
_ship_it_url = 'https://ship-it.mozilla.org/json/1.0/%s'  # behind vpn
_firefox_primary_builds_uri = 'firefox_primary_builds.json'
_firefox_versions_uri = 'firefox_versions.json'
# mappings adapted off of https://github.com/mozilla-releng/ship-it/blob/master/kickoff/config.py
releng_to_bouncer_alias_dict = {
    'FIREFOX_AURORA': 'firefox-aurora-latest',
    'FIREFOX_ESR_NEXT': 'firefox-esr-latest',
    'LATEST_FIREFOX_VERSION': 'firefox-latest',
    'FIREFOX_ESR': None,  # no checks to run on this product
    'FIREFOX_NIGHTLY': 'firefox-nightly-latest',
    'LATEST_FIREFOX_OLDER_VERSION': None,  # no checks to run on this product
    'LATEST_FIREFOX_RELEASED_DEVEL_VERSION': 'firefox-beta-latest',
    'LATEST_FIREFOX_DEVEL_VERSION': 'firefox-beta-latest'
}

def generate_fx_alias_ver_mappings(releng_products, alias_map=releng_to_bouncer_alias_dict):
    """Parses a dictionary that contains releng product/versions and returns
    a new dictionary that maps go-bouncer aliases to expected Firefox product
    versions.

    As the alias_map is walked, aliases with a value
    of type None will not be included in the returned dictionary.

    The key/val mappings that currently represent the system are
    stored in releng_to_bouncer_alias_dict. Currently there are 2 releng products
    that do not have associated go-bouncer aliases and have val = None.

    :param product_versions: {string:string} the releng object to be walked.
        releng_product/product_version
    :param alias_map: {string:string} releng to Bouncer alias mappings.
        The default, releng_to_bouncer_alias_dict is adapted from
        https://github.com/mozilla-releng/ship-it/blob/master/kickoff/config.py

    :returns: {string:string} with product aliases and their version numbers"""
    aliases_and_versions = {}
    # create a dict that has {alias: expected_version_num}
    for product, alias in alias_map.iteritems():
        # values of None represent products that don't have aliases
        if alias is not None:
            aliases_and_versions[alias] = releng_products[product]
    return aliases_and_versions

def fetch_current_fx_product_details(base_url=_product_details_url):
    """Fetches JSON containing key/val pairings of the current releng aliases
    for Firefox and version numbers as known by Mozilla's Release Engineering Team.

    Release Engineering maintains an up-to-date JSON file with the current
    Firefox release values - https://product-details.mozilla.org/1.0/firefox_versions.json.

    :param base_url: The address of the server to pull product details from.

    :returns: {string:string} dictionary with releng aliases and version numbers"""
    url = base_url % _firefox_versions_uri
    response = requests.get(url)
    response.raise_for_status()
    releng_products = response.json()
    return releng_products

def get_version_info_for_alias(alias, base_url=_product_details_url):
    """Using up-to-date information drawn from product details service, find the current version of
    a Firefox product.

    releng_to_bouncer_alias_dict lists the available aliases.

    :param alias: The go-bouncer alias to get the version of. For example, 'firefox-latest'
    :param base_url: The url of the service that hosts the product details.
    :return String: The version number of Firefox
    """
    products = get_product_mappings(base_url)
    return products[alias]

def get_firefox_locales():
    """Fetches build versions for each localization of Firefox from Mozilla's Release
    Engineering Team.

    Release Engineering maintains an up-to-date JSON file with the current
    Firefox release values - https://product-details.mozilla.org/1.0/firefox_primary_builds.json.

    :returns list: [FirefoxLocale objects] a list of FirefoxLocale objects.
    """
    locale_objs = []
    url = _product_details_url % _firefox_primary_builds_uri
    response = requests.get(url)
    response.raise_for_status()
    locale_data = response.json()
    for locale in locale_data:
        versions = locale_data[locale]
        locale_objs.append(FirefoxLocale(locale, versions))
    return locale_objs

def get_product_mappings(base_url=_product_details_url):
    """Get a current list of product details, primary Firefox aliases and the
    current version numbers.
    The default url that product information is pulled from is https://product-details.mozilla.org/1.0/firefox_versions.json

    :param base_url: The url of the service that hosts the product details.
    :return dictionary: Product aliases and version numbers
    """
    releng_aliases = fetch_current_fx_product_details(base_url)
    return generate_fx_alias_ver_mappings(releng_aliases)
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Compiler tool.
"""

from optparse import OptionParser
import sys
import os
import fnmatch
import logging

logger = logging.getLogger("pymola")

# Parse command line arguments
usage = "usage: %prog [options] MODEL_FOLDER MODEL_NAME"
parser = OptionParser(usage)
parser.add_option("-c", "--casadi", dest="casadi_folder",
                  help="CasADi installation folder")
parser.add_option("-f", "--flatten_only",
                  action="store_true", dest="flatten_only")
parser.add_option("-v", "--verbose",
                  action="store_true", dest="verbose")
(options, args) = parser.parse_args()
if len(args) != 2:
    parser.error("incorrect number of arguments")

model_folder = args[0]
model_name = args[1]

# Set log level
logging.basicConfig(level=logging.DEBUG if options.verbose else logging.INFO)

# Import rest of pymola
from pymola import parser, tree

# Compile
if options.flatten_only:
    # Load folder
    ast = None
    for root, dir, files in os.walk(model_folder, followlinks=True):
        for item in fnmatch.filter(files, "*.mo"):
            logger.info("Parsing {}".format(item))

            with open(os.path.join(root, item), 'r') as f:
                if ast is None:
                    ast = parser.parse(f.read())
                else:
                    ast.extend(parser.parse(f.read()))

    logger.info("Flattening")

    ast = tree.flatten(ast, model_name)
    print(ast)
else:
    # Set CasADi installation folder
    if options.casadi_folder is not None:
        sys.path.append(options.casadi_folder)

    from pymola.backends.casadi.api import transfer_model
    import casadi as ca

    logger.info("Generating CasADi model")

    compiler_options = \
        {'replace_constants': True,
         'replace_parameter_expressions': True,
         'eliminable_variable_expression': r'_\w+',
         'detect_aliases': True,
         'expand': False,
         'cache': True}

    model = transfer_model(model_folder, model_name, compiler_options)
    print(model)
<commit_msg>Fix flattening mode in compiler.py.<commit_after>#!/usr/bin/env python
"""
Compiler tool.
"""

from optparse import OptionParser
import sys
import os
import fnmatch
import logging

logger = logging.getLogger("pymola")

# Parse command line arguments
usage = "usage: %prog [options] MODEL_FOLDER MODEL_NAME"
parser = OptionParser(usage)
parser.add_option("-c", "--casadi", dest="casadi_folder",
                  help="CasADi installation folder")
parser.add_option("-f", "--flatten_only",
                  action="store_true", dest="flatten_only")
parser.add_option("-v", "--verbose",
                  action="store_true", dest="verbose")
(options, args) = parser.parse_args()
if len(args) != 2:
    parser.error("incorrect number of arguments")

model_folder = args[0]
model_name = args[1]

# Set log level
logging.basicConfig(level=logging.DEBUG if options.verbose else logging.INFO)

# Import rest of pymola
from pymola import parser, tree, ast

# Compile
if options.flatten_only:
    # Load folder
    _ast = None
    for root, dir, files in os.walk(model_folder, followlinks=True):
        for item in fnmatch.filter(files, "*.mo"):
            logger.info("Parsing {}".format(item))

            with open(os.path.join(root, item), 'r') as f:
                if _ast is None:
                    _ast = parser.parse(f.read())
                else:
                    _ast.extend(parser.parse(f.read()))

    logger.info("Flattening")

    _ast = tree.flatten(_ast, ast.ComponentRef(name=model_name))
    print(_ast)
else:
    # Set CasADi installation folder
    if options.casadi_folder is not None:
        sys.path.append(options.casadi_folder)

    from pymola.backends.casadi.api import transfer_model
    import casadi as ca

    logger.info("Generating CasADi model")

    compiler_options = \
        {'replace_constants': True,
         'replace_parameter_expressions': True,
         'eliminable_variable_expression': r'_\w+',
         'detect_aliases': True,
         'expand': False,
         'cache': True}

    model = transfer_model(model_folder, model_name, compiler_options)
    print(model)
<|endoftext|>


<commit_before>"""
Functions for analysis and plotting
"""

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

from future import standard_library
standard_library.install_aliases()
import warnings
warnings.filterwarnings("ignore")

# -------------------------------------------------------------------------------------------------------------------
## Wrapper to run analysis functions in simConfig
# -------------------------------------------------------------------------------------------------------------------
from .wrapper import plotData

# -------------------------------------------------------------------------------------------------------------------
# Import utils methods
# -------------------------------------------------------------------------------------------------------------------
from .utils import exception, _showFigure, _saveFigData, getCellsInclude, getCellsIncludeTags, _roundFigures, \
     _smooth1d, syncMeasure, invertDictMapping

# -------------------------------------------------------------------------------------------------------------------
# Import connectivity-related functions
# -------------------------------------------------------------------------------------------------------------------
from .network import plotConn, _plotConnCalculateFromSim, _plotConnCalculateFromFile, plot2Dnet, plotShape, calculateDisynaptic

# -------------------------------------------------------------------------------------------------------------------
# Import spike-related functions
# -------------------------------------------------------------------------------------------------------------------
from .spikes import calculateRate, plotRates, plotSyncs, plotRaster, plotSpikeHist, plotSpikeStats, \
    plotRatePSD, plotRateSpectrogram, popAvgRates, plotfI

# -------------------------------------------------------------------------------------------------------------------
# Import traces-related functions
# -------------------------------------------------------------------------------------------------------------------
from .traces import plotTraces, plotEPSPAmp

# -------------------------------------------------------------------------------------------------------------------
# Import LFP-related functions
# -------------------------------------------------------------------------------------------------------------------
from .lfp import plotLFP

# -------------------------------------------------------------------------------------------------------------------
# Import information theory-related functions
# -------------------------------------------------------------------------------------------------------------------
from .info import nTE, granger

# -------------------------------------------------------------------------------------------------------------------
# Import RxD-related functions
# -------------------------------------------------------------------------------------------------------------------
from .rxd import plotRxDConcentration

# -------------------------------------------------------------------------------------------------------------------
# Import interactive functions functions
# -------------------------------------------------------------------------------------------------------------------
try:
    from .interactive import iplotDipole, iplotDipoleSpectrogram, iplotDipolePSD, iplotRaster, iplotSpikeHist, \
        iplotRatePSD, iplotTraces, iplotLFP, iplotConn
except:
    print('Warning: could not import interactive plotting functions; make sure the "bokeh" packages is installed.'<commit_msg>try except for bokeh; to avoid error on CentOS where cannot install bokeh<commit_after>"""
Functions for analysis and plotting
"""

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

from future import standard_library
standard_library.install_aliases()
import warnings
warnings.filterwarnings("ignore")

# -------------------------------------------------------------------------------------------------------------------
## Wrapper to run analysis functions in simConfig
# -------------------------------------------------------------------------------------------------------------------
from .wrapper import plotData

# -------------------------------------------------------------------------------------------------------------------
# Import utils methods
# -------------------------------------------------------------------------------------------------------------------
from .utils import exception, _showFigure, _saveFigData, getCellsInclude, getCellsIncludeTags, _roundFigures, \
     _smooth1d, syncMeasure, invertDictMapping

# -------------------------------------------------------------------------------------------------------------------
# Import connectivity-related functions
# -------------------------------------------------------------------------------------------------------------------
from .network import plotConn, _plotConnCalculateFromSim, _plotConnCalculateFromFile, plot2Dnet, plotShape, calculateDisynaptic

# -------------------------------------------------------------------------------------------------------------------
# Import spike-related functions
# -------------------------------------------------------------------------------------------------------------------
from .spikes import calculateRate, plotRates, plotSyncs, plotRaster, plotSpikeHist, plotSpikeStats, \
    plotRatePSD, plotRateSpectrogram, popAvgRates, plotfI

# -------------------------------------------------------------------------------------------------------------------
# Import traces-related functions
# -------------------------------------------------------------------------------------------------------------------
from .traces import plotTraces, plotEPSPAmp

# -------------------------------------------------------------------------------------------------------------------
# Import LFP-related functions
# -------------------------------------------------------------------------------------------------------------------
from .lfp import plotLFP

# -------------------------------------------------------------------------------------------------------------------
# Import information theory-related functions
# -------------------------------------------------------------------------------------------------------------------
from .info import nTE, granger

# -------------------------------------------------------------------------------------------------------------------
# Import RxD-related functions
# -------------------------------------------------------------------------------------------------------------------
from .rxd import plotRxDConcentration

# -------------------------------------------------------------------------------------------------------------------
# Import interactive functions functions
# -------------------------------------------------------------------------------------------------------------------
try:
    from .interactive import iplotDipole, iplotDipoleSpectrogram, iplotDipolePSD, iplotRaster, iplotSpikeHist, \
        iplotRatePSD, iplotTraces, iplotLFP, iplotConn
except:
    print('Warning: could not import interactive plotting functions; make sure the "bokeh" packages is installed.')<|endoftext|>


<commit_before>#!/usr/bin/env python

"""
Logging tools.
"""

import copy
import sys
import traceback

import mpi4py.MPI
import twiggy

import neurokernel.tools.comm

def log_exception(type, value, tb, logger=twiggy.log, multiline=False):
    """
    Log the specified exception data using twiggy.

    Parameters
    ----------
    value, tb : object
        Parameters expected by traceback.print_exception.
    logger : twiggy.logger.Logger
        Logger to use. twiggy.log is assumed by default.
    multiline : bool
        If True, print exception using multiple log lines.
    """

    if multiline:
        for x in traceback.format_exception(type, value, tb)[1:]:
            for y in x.strip('\n ').split('\n'):
                logger.error(y)
    else:
        msg = '|'.join([': '.join([y.strip() for y in x.strip('\n ').split('\n')]) for x in \
                        traceback.format_exception(type, value, tb)[1:]])
        logger.error('Uncaught exception: %s' % str(msg))

def set_excepthook(logger, multiline=False):
    """
    Set the exception hook to use the specified logger.

    Parameters
    ----------
    logger : twiggy.logger.Logger
        Configured logger.
    multiline : bool
        If True, log exception messages on multiple lines.
    """

    sys.excepthook = \
        lambda type, value, tb: log_exception(type, value, tb, logger, multiline)

def setup_logger(name='', level=twiggy.levels.DEBUG,
                 fmt=twiggy.formats.line_format,
                 fmt_name=('{0:%s}' % 10).format,
                 screen=None, file_name=None,
                 mpi_comm=None, zmq_addr=None,
                 log_exceptions=True, multiline=False):
    """
    Setup a twiggy logger.

    Parameters
    ----------
    name : str
        Logger name.
    level : twiggy.levels.LogLevel
        Logging level.
    fmt : twiggy.formats.LineFormat
        Logging formatter class instance.
    fmt_name : function
        Function with one parameter that formats the message name.
    screen : bool
        Create output stream handler to the screen if True.
    file_name : str
        Create output handler to specified file.
    mpi_comm : mpi4py.MPI.Intracomm
        If not None, use MPI I/O with the specified communicator for
        output file handler. Ignored if the `file_name` parameter
        is not specified.
    zmq_addr : str
        ZeroMQ socket address.
    log_exceptions : bool
        If True, exception messages are written to the logger.
    multiline : bool
        If True, log exception messages on multiple lines.

    Returns
    -------
    logger : twiggy.logger.Logger
        Configured logger.

    Bug
    ---
    To use the ZeroMQ output class with multiprocessing, it must be added
    as an emitter within each process.
    """

    fmt = copy.copy(fmt)
    fmt.conversion.delete('name')

    # Apply name format to the value (i.e., the name), not the key (i.e., the
    # field name "name"):
    fmt.conversion.add('name', str, lambda k, v: fmt_name(v))

    if file_name:
        if mpi_comm:
            assert isinstance(mpi_comm, mpi4py.MPI.Intracomm)
            file_output = \
                neurokernel.tools.comm.MPIOutput(file_name, fmt, mpi_comm)
        else:
            file_output = \
                twiggy.outputs.FileOutput(file_name, fmt, 'w')
        twiggy.add_mitters(('file', level, None, file_output))

    if screen:
        screen_output = \
          twiggy.outputs.StreamOutput(fmt, stream=sys.stdout)
        twiggy.add_emitters(('screen', level, None, screen_output))

    if zmq_addr:
        zmq_output = neurokernel.tools.comm.ZMQOutput(zmq_addr, fmt)
        twiggy.add_emitters(('zmq', level, None, zmq_output))

    logger = twiggy.log.name(fmt_name(name))
    if log_exceptions:
        set_excepthook(logger, multiline)

    return logger
<commit_msg>Fix typo in logging.py.<commit_after>#!/usr/bin/env python

"""
Logging tools.
"""

import copy
import sys
import traceback

import mpi4py.MPI
import twiggy

import neurokernel.tools.comm

def log_exception(type, value, tb, logger=twiggy.log, multiline=False):
    """
    Log the specified exception data using twiggy.

    Parameters
    ----------
    value, tb : object
        Parameters expected by traceback.print_exception.
    logger : twiggy.logger.Logger
        Logger to use. twiggy.log is assumed by default.
    multiline : bool
        If True, print exception using multiple log lines.
    """

    if multiline:
        for x in traceback.format_exception(type, value, tb)[1:]:
            for y in x.strip('\n ').split('\n'):
                logger.error(y)
    else:
        msg = '|'.join([': '.join([y.strip() for y in x.strip('\n ').split('\n')]) for x in \
                        traceback.format_exception(type, value, tb)[1:]])
        logger.error('Uncaught exception: %s' % str(msg))

def set_excepthook(logger, multiline=False):
    """
    Set the exception hook to use the specified logger.

    Parameters
    ----------
    logger : twiggy.logger.Logger
        Configured logger.
    multiline : bool
        If True, log exception messages on multiple lines.
    """

    sys.excepthook = \
        lambda type, value, tb: log_exception(type, value, tb, logger, multiline)

def setup_logger(name='', level=twiggy.levels.DEBUG,
                 fmt=twiggy.formats.line_format,
                 fmt_name=('{0:%s}' % 10).format,
                 screen=None, file_name=None,
                 mpi_comm=None, zmq_addr=None,
                 log_exceptions=True, multiline=False):
    """
    Setup a twiggy logger.

    Parameters
    ----------
    name : str
        Logger name.
    level : twiggy.levels.LogLevel
        Logging level.
    fmt : twiggy.formats.LineFormat
        Logging formatter class instance.
    fmt_name : function
        Function with one parameter that formats the message name.
    screen : bool
        Create output stream handler to the screen if True.
    file_name : str
        Create output handler to specified file.
    mpi_comm : mpi4py.MPI.Intracomm
        If not None, use MPI I/O with the specified communicator for
        output file handler. Ignored if the `file_name` parameter
        is not specified.
    zmq_addr : str
        ZeroMQ socket address.
    log_exceptions : bool
        If True, exception messages are written to the logger.
    multiline : bool
        If True, log exception messages on multiple lines.

    Returns
    -------
    logger : twiggy.logger.Logger
        Configured logger.

    Bug
    ---
    To use the ZeroMQ output class with multiprocessing, it must be added
    as an emitter within each process.
    """

    fmt = copy.copy(fmt)
    fmt.conversion.delete('name')

    # Apply name format to the value (i.e., the name), not the key (i.e., the
    # field name "name"):
    fmt.conversion.add('name', str, lambda k, v: fmt_name(v))

    if file_name:
        if mpi_comm:
            assert isinstance(mpi_comm, mpi4py.MPI.Intracomm)
            file_output = \
                neurokernel.tools.comm.MPIOutput(file_name, fmt, mpi_comm)
        else:
            file_output = \
                twiggy.outputs.FileOutput(file_name, fmt, 'w')
        twiggy.add_emitters(('file', level, None, file_output))

    if screen:
        screen_output = \
          twiggy.outputs.StreamOutput(fmt, stream=sys.stdout)
        twiggy.add_emitters(('screen', level, None, screen_output))

    if zmq_addr:
        zmq_output = neurokernel.tools.comm.ZMQOutput(zmq_addr, fmt)
        twiggy.add_emitters(('zmq', level, None, zmq_output))

    logger = twiggy.log.name(fmt_name(name))
    if log_exceptions:
        set_excepthook(logger, multiline)

    return logger
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
feed works to get feed meta info into database
"""
# @author chengdujin
# @contact chengdujin@gmail.com
# @created Jan 1, 2013

import sys
reload(sys)
sys.setdefaultencoding('UTF-8')
sys.path.append("..")

from config.settings import logger
import database as db_feeds
import feedparser
from spider import scraper
import socket
socket.setdefaulttimeout(10)  # 10 seconds

def _read_source(d=None, feed_link=None, language=None, categories=None):
    """
    parse the feed for meta information
    """
    if not d or not feed_link or not language or not categories:
        logger.error('Method malformed!')
        return None

    try:
        if 'feed' in d:
            feed = {}
            # read in passed values
            feed['feed_link'] = feed_link
            feed['categories'] = categories
            feed['language'] = language

            if 'title' in d.feed:
                feed['feed_title'] = d.feed.title.strip()
            if 'status' in d:
                feed['status'] = d.status
            if 'rights' in d.feed:
                feed['rights'] = d.feed.rights.strip()
            if 'etag' in d:
                feed['etag'] = d.feed.etag.strip()
            if 'modified' in d:
                feed['modified'] = d.feed.modified.strip()
            # the final return
            return feed
        else:
            logger.error('Feed %s is malformed!' % feed_link)
            return None
    except Exception as k:
        logger.error(str(k))
        return None

# TODO: implement _link_cleaner!
def add(feed_link=None, language=None, categories=None, transcoder_type="chengdujin"):
    """
    read rss/atom meta information from a given feed
    """
    if not feed_link or not language:
        logger.error("[feed.add] ERROR: Method not well formed!")
        return None
    if not isinstance(categories, dict):
        logger.error('categories should be a dictionary')
        return None

    try:
        d = feedparser.parse(feed_link)
        if d:
            # feed level
            feed = _read_source(d, feed_link, language, categories)
            if feed:
                feed_id = db_feeds.save(feed)
                if feed_id:
                    # add entries of this feed
                    # the FINAL return
                    return scraper.update(feed_link=feed_link, feed_id=feed_id, language=language, categories=categories, transcoder_type=transcoder_type)
                else:
                    logger.error('Cannot save feed in database')
                    return None
            else:
                logger.error('Cannot read source!')
                return None
        else:
            logger.error(
                "RSS source %s cannot be interpreted!" % feed_link)
            return None
    except Exception as k:
        logger.error(str(k))
        return None
<commit_msg>Change spider import to parser<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
feed works to get feed meta info into database
"""
# @author chengdujin
# @contact chengdujin@gmail.com
# @created Jan 1, 2013

import sys
reload(sys)
sys.setdefaultencoding('UTF-8')
sys.path.append("..")

from config.settings import logger
import database as db_feeds
import feedparser
from parser import scraper
import socket
socket.setdefaulttimeout(10)  # 10 seconds

def _read_source(d=None, feed_link=None, language=None, categories=None):
    """
    parse the feed for meta information
    """
    if not d or not feed_link or not language or not categories:
        logger.error('Method malformed!')
        return None

    try:
        if 'feed' in d:
            feed = {}
            # read in passed values
            feed['feed_link'] = feed_link
            feed['categories'] = categories
            feed['language'] = language

            if 'title' in d.feed:
                feed['feed_title'] = d.feed.title.strip()
            if 'status' in d:
                feed['status'] = d.status
            if 'rights' in d.feed:
                feed['rights'] = d.feed.rights.strip()
            if 'etag' in d:
                feed['etag'] = d.feed.etag.strip()
            if 'modified' in d:
                feed['modified'] = d.feed.modified.strip()
            # the final return
            return feed
        else:
            logger.error('Feed %s is malformed!' % feed_link)
            return None
    except Exception as k:
        logger.error(str(k))
        return None

# TODO: implement _link_cleaner!
def add(feed_link=None, language=None, categories=None, transcoder_type="chengdujin"):
    """
    read rss/atom meta information from a given feed
    """
    if not feed_link or not language:
        logger.error("[feed.add] ERROR: Method not well formed!")
        return None
    if not isinstance(categories, dict):
        logger.error('categories should be a dictionary')
        return None

    try:
        d = feedparser.parse(feed_link)
        if d:
            # feed level
            feed = _read_source(d, feed_link, language, categories)
            if feed:
                feed_id = db_feeds.save(feed)
                if feed_id:
                    # add entries of this feed
                    # the FINAL return
                    return scraper.update(feed_link=feed_link, feed_id=feed_id, language=language, categories=categories, transcoder_type=transcoder_type)
                else:
                    logger.error('Cannot save feed in database')
                    return None
            else:
                logger.error('Cannot read source!')
                return None
        else:
            logger.error(
                "RSS source %s cannot be interpreted!" % feed_link)
            return None
    except Exception as k:
        logger.error(str(k))
        return None
<|endoftext|>