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

"""system.py Unit Tests

..moduleauthor:: Timothy Helton <timothy.j.helton@gmail.com>
"""

from contextlib import redirect_stdout
import io
import os
import os.path as osp
import pytest
import shutil
import subprocess
import numpy as np
from strumenti import system

lines = ['a\tb\tc\td\n', '\n', '1\t2\t3\t4\n', '5\t6\t7\t8\n']

@pytest.fixture(scope='session')
def fixture_get_header():
    with open('test.txt', 'w') as f:
        f.write(''.join(lines))

    with open('test_no_header.txt', 'w') as f:
        f.write(''.join(lines[2:]))

@pytest.mark.usefixtures('fixture_get_header')
@pytest.mark.parametrize('name, skip, expected', [
    ('test.txt', 0, ['a', 'b', 'c', 'd']),
    ('test.txt', 2, ['1', '2', '3', '4']),
    ('test.txt', '2', ['1', '2', '3', '4']),
    ])
def test__get_header(name, skip, expected):
    assert system.get_header(name, skip) == expected

def test__flatten_empty():
    with pytest.raises(TypeError):
        system.flatten()

@pytest.mark.parametrize('matrix, expected', [
    ([[1, 2, 3], [4, 5, 6], [7., 8., 9.]], [1, 2, 3, 4, 5, 6, 7, 8, 9]),
    ([['this'], ['is'], ['a'], ['test']], ['this', 'is', 'a', 'test']),
    ([[1, 2, 3], 4, 'test'], [1, 2, 3, 4, 'test']),
    ([[1, 2, 3], [], [7, 8, 9]], [1, 2, 3, 7, 8, 9]),
    ([(1, 2, 3), (4, 5, 6), (7, 8, 9)], [1, 2, 3, 4, 5, 6, 7, 8, 9]),
    ])
def test__flatten(matrix, expected):
    assert system.flatten(matrix) == expected

@pytest.fixture(scope='session')
def load_lines_setup():
    file_name = 'test.txt'
    with open(file_name, 'w') as f:
        f.write('line one\n')
        f.write('line two\n')
        f.write('line three\n')
    return file_name

@pytest.mark.usefixtures('load_lines_setup')
@pytest.mark.parametrize('path, all_lines, first_n_lines, expected', [
    ('test.txt', True, 0, ['line one\n', 'line two\n', 'line three\n']),
    ('test.txt', False, 0, 'line one\nline two\nline three\n'),
    ('test.txt', False, 2, ['line one\n', 'line two\n']),
])
def test__load_file(path, all_lines, first_n_lines, expected):
    actual = system.load_file(path, all_lines, first_n_lines)
    assert actual == expected

@pytest.fixture()
def load_record_setup():
    with open('test.txt', 'w') as f:
        f.write(''.join(lines))

    with open('test_no_header.txt', 'w') as f:
        f.write(''.join(lines[2:]))

@pytest.mark.usefixtures('load_record_setup')
@pytest.mark.parametrize(('path, header, skip, cols, names, formats,'
                          'a_key, a_expect, d_key, d_expect'), [
    ('test.txt', 0, 2, ('all',), None, ('f8', ),
     'a', np.array([1.0, 5.0]), 'd', np.array([4.0, 8.0])),
    ('test.txt', 0, 2, (0, 3), None, ('f8', ),
     'a', np.array([1.0, 5.0]), 'd', np.array([4.0, 8.0])),
    ('test.txt', 0, 2, ('all',), None, ('f8', 'i4', 'f8', 'i4'),
     'a', np.array([1.0, 5.0]), 'd', np.array([4, 8])),
    ('test_no_header.txt', None, 0, (0, 3), None, ('f8', ),
     '0', np.array([1.0, 5.0]), '3', np.array([4.0, 8.0])),
    ('test_no_header.txt', None, 0, ('all', ), ('one', 'two', 'three', 'four'),
     ('f8', ), 'one', np.array([1.0, 5.0]), 'four', np.array([4.0, 8.0])),
])
def test__load_records(path, header, skip, cols, names, formats,
                       a_key, a_expect, d_key, d_expect):
    output = system.load_records(path, header, skip, cols, names, formats)
    assert np.all(output[a_key] == a_expect)
    assert np.all(output[d_key] == d_expect)

@pytest.fixture()
def preserve_cwd_setup(request):
    original_dir = os.getcwd()
    working_dir = osp.join(original_dir, 'junk')
    file_name = 'junk.txt'

    os.makedirs(working_dir, exist_ok=True)

    def teardown():
        shutil.rmtree(working_dir)
    request.addfinalizer(teardown)
    return {'original_dir': original_dir, 'working_dir': working_dir,
            'file_name': file_name}

def test__preserve_cwd(preserve_cwd_setup):

    @system.preserve_cwd(preserve_cwd_setup['working_dir'])
    def test():
        with open(preserve_cwd_setup['file_name'], 'w') as f:
            f.close()

    test()
    assert osp.isfile(osp.join(preserve_cwd_setup['working_dir'],
                               preserve_cwd_setup['file_name']))
    assert os.getcwd() == preserve_cwd_setup['original_dir']

def test__status():

    @system.status()
    def print_num():
        print('1, 2, 3')

    f = io.StringIO()
    with redirect_stdout(f):
        print_num()

    assert (f.getvalue().split()[:-3] ==
            ['Execute:', 'print_num', '1,', '2,', '3', 'Completed:',
             'print_num'])

@pytest.fixture(scope='function')
def unzip_setup(request):
    file_name = 'junk.txt'
    with open(file_name, 'w') as f:
        f.write('Test file')
    subprocess.call(['gzip', file_name])

    def teardown():
        os.remove(file_name)
    request.addfinalizer(teardown)
    return file_name

def test__unzip(unzip_setup):
    system.unzip_file('{}.gz'.format(unzip_setup))
    with open(unzip_setup, 'r') as f:
        text = f.read()
    assert 'Test file' == text

class TestOSWalkDir:

    @pytest.fixture(autouse=True)
    def setup(self, request, tmpdir):
        tmpdir.chdir()

        self.main_dir = osp.join(os.getcwd(), 'test_walk_dir')
        self.extra_dir = osp.join(self.main_dir, 'extra')

        os.makedirs(self.extra_dir, exist_ok=True)

        @system.preserve_cwd(self.main_dir)
        def make_files_1():
            with open('main.png', 'w') as f:
                f.write('.png file in main directory.')
            with open('main.jpeg', 'w') as f:
                f.write('.jpeg file in main directory.')

        @system.preserve_cwd(self.extra_dir)
        def make_files_2():
            with open('extra.png', 'w') as f:
                f.write('.png file in extra directory.')
            with open('extra.inp', 'w') as f:
                f.write('.inp file in extra directory.')

        os.chdir(self.main_dir)
        make_files_1()
        make_files_2()

        def teardown():
            tmpdir.chdir()
            shutil.rmtree(self.main_dir)
        request.addfinalizer(teardown)

    def test__no_files_to_find(self):
        assert system.walk_dir('.txt') == []

    def test__find_main_dir_only(self):
        assert system.walk_dir('.jpeg') == [osp.join(self.main_dir,
                                                     'main.jpeg')]

    def test__find_extra_dir_only(self):
        assert (system.walk_dir('.inp') ==
                [osp.join(self.main_dir, self.extra_dir, 'extra.inp')])

    def test__find_both_dirs(self):
        assert (system.walk_dir('.png') ==
                [osp.join(self.main_dir, self.extra_dir, 'extra.png'),
                 osp.join(self.main_dir, 'main.png')])

@pytest.fixture(scope='function')
def zip_setup(request):
    file_name = 'junk.txt'
    with open(file_name, 'w') as f:
        f.write('Test file')

    def teardown():
        try:
            os.remove(file_name)
        except FileNotFoundError:
            os.remove('{}.gz'.format(file_name))
    request.addfinalizer(teardown)
    return file_name

def test__zip_file(zip_setup):
    system.zip_file(zip_setup)
    subprocess.call(['gunzip', '{}.gz'.format(zip_setup)])
    with open(zip_setup, 'r') as f:
        text = f.read()
    assert 'Test file' == text
<commit_msg>Added ids to pytest parameters<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""system.py Unit Tests

..moduleauthor:: Timothy Helton <timothy.j.helton@gmail.com>
"""

from contextlib import redirect_stdout
import io
import os
import os.path as osp
import pytest
import shutil
import subprocess
import numpy as np
from strumenti import system

lines = ['a\tb\tc\td\n', '\n', '1\t2\t3\t4\n', '5\t6\t7\t8\n']

# Test get_header
get_header = {'defaults': ('test.txt', 0, ['a', 'b', 'c', 'd']),
              'row 2': ('test.txt', 2, ['1', '2', '3', '4']),
              'row 2 str': ('test.txt', '2', ['1', '2', '3', '4']),
              }

@pytest.fixture(scope='session')
def fixture_get_header():
    with open('test.txt', 'w') as f:
        f.write(''.join(lines))

    with open('test_no_header.txt', 'w') as f:
        f.write(''.join(lines[2:]))

@pytest.mark.usefixtures('fixture_get_header')
@pytest.mark.parametrize('name, skip, expected',
                         list(get_header.values()),
                         ids=list(get_header.keys()))
def test__get_header(name, skip, expected):
    assert system.get_header(name, skip) == expected

# Test flatten
flatten = {'lists ints floats': ([[1, 2, 3], [4, 5, 6], [7., 8., 9.]],
                                 [1, 2, 3, 4, 5, 6, 7, 8, 9]),
           'lists str': ([['this'], ['is'], ['a'], ['test']],
                         ['this', 'is', 'a', 'test']),
           'list int str': ([[1, 2, 3], 4, 'test'], [1, 2, 3, 4, 'test']),
           'lists empty': ([[1, 2, 3], [], [7, 8, 9]], [1, 2, 3, 7, 8, 9]),
           'tuples floats': ([(1, 2, 3), (4, 5, 6), (7, 8, 9)],
                             [1, 2, 3, 4, 5, 6, 7, 8, 9]),
           }

@pytest.mark.parametrize('matrix, expected',
                         list(flatten.values()),
                         ids=list(flatten.keys()))
def test__flatten(matrix, expected):
    assert system.flatten(matrix) == expected

def test__flatten_empty():
    with pytest.raises(TypeError):
        system.flatten()

# Test load_file
load_file = {'lines': ('test.txt', True, 0,
                       ['line one\n', 'line two\n', 'line three\n']),
             'str': ('test.txt', False, 0, 'line one\nline two\nline three\n'),
             'first n lines': ('test.txt', False, 2,
                               ['line one\n', 'line two\n']),
             }

@pytest.fixture(scope='session')
def load_file_setup():
    file_name = 'test.txt'
    with open(file_name, 'w') as f:
        f.write('line one\n')
        f.write('line two\n')
        f.write('line three\n')
    return file_name

@pytest.mark.usefixtures('load_file_setup')
@pytest.mark.parametrize('path, all_lines, first_n_lines, expected',
                         list(load_file.values()),
                         ids=list(load_file.keys()))
def test__load_file(path, all_lines, first_n_lines, expected):
    actual = system.load_file(path, all_lines, first_n_lines)
    assert actual == expected

# Test load_record
load_record = {'header all cols': ('test.txt', 0, 2, ('all',), None, ('f8', ),
                                   'a', np.array([1.0, 5.0]),
                                   'd', np.array([4.0, 8.0])),
               'header some cols': ('test.txt', 0, 2, (0, 3), None, ('f8', ),
                                    'a', np.array([1.0, 5.0]),
                                    'd', np.array([4.0, 8.0])),
               'header all cols formats': ('test.txt', 0, 2, ('all',), None,
                                           ('f8', 'i4', 'f8', 'i4'),
                                           'a', np.array([1.0, 5.0]),
                                           'd', np.array([4, 8])),
               'no header some cols': ('test_no_header.txt', None, 0, (0, 3),
                                       None, ('f8', ),
                                       '0', np.array([1.0, 5.0]),
                                       '3', np.array([4.0, 8.0])),
               'no header all cols formats': ('test_no_header.txt', None, 0,
                                              ('all', ),
                                              ('one', 'two', 'three', 'four'),
                                              ('f8', ),
                                              'one', np.array([1.0, 5.0]),
                                              'four', np.array([4.0, 8.0])),
               }

@pytest.fixture()
def load_record_setup():
    with open('test.txt', 'w') as f:
        f.write(''.join(lines))

    with open('test_no_header.txt', 'w') as f:
        f.write(''.join(lines[2:]))

@pytest.mark.usefixtures('load_record_setup')
@pytest.mark.parametrize(('path, header, skip, cols, names, formats,'
                          'a_key, a_expect, d_key, d_expect'),
                         list(load_record.values()),
                         ids=list(load_record.keys()))
def test__load_records(path, header, skip, cols, names, formats,
                       a_key, a_expect, d_key, d_expect):
    output = system.load_records(path, header, skip, cols, names, formats)
    assert np.all(output[a_key] == a_expect)
    assert np.all(output[d_key] == d_expect)

# Test preserve_cwd
@pytest.fixture()
def preserve_cwd_setup(request):
    original_dir = os.getcwd()
    working_dir = osp.join(original_dir, 'junk')
    file_name = 'junk.txt'

    os.makedirs(working_dir, exist_ok=True)

    def teardown():
        shutil.rmtree(working_dir)
    request.addfinalizer(teardown)
    return {'original_dir': original_dir, 'working_dir': working_dir,
            'file_name': file_name}

def test__preserve_cwd(preserve_cwd_setup):

    @system.preserve_cwd(preserve_cwd_setup['working_dir'])
    def test():
        with open(preserve_cwd_setup['file_name'], 'w') as f:
            f.close()

    test()
    assert osp.isfile(osp.join(preserve_cwd_setup['working_dir'],
                               preserve_cwd_setup['file_name']))
    assert os.getcwd() == preserve_cwd_setup['original_dir']

# Test status
def test__status():

    @system.status()
    def print_num():
        print('1, 2, 3')

    f = io.StringIO()
    with redirect_stdout(f):
        print_num()

    assert (f.getvalue().split()[:-3] ==
            ['Execute:', 'print_num', '1,', '2,', '3', 'Completed:',
             'print_num'])

# Test unzip
@pytest.fixture(scope='function')
def unzip_setup(request):
    file_name = 'junk.txt'
    with open(file_name, 'w') as f:
        f.write('Test file')
    subprocess.call(['gzip', file_name])

    def teardown():
        os.remove(file_name)
    request.addfinalizer(teardown)
    return file_name

def test__unzip(unzip_setup):
    system.unzip_file('{}.gz'.format(unzip_setup))
    with open(unzip_setup, 'r') as f:
        text = f.read()
    assert 'Test file' == text

# Test walk_dir
class TestOSWalkDir:

    @pytest.fixture(autouse=True)
    def setup(self, request, tmpdir):
        tmpdir.chdir()

        self.main_dir = osp.join(os.getcwd(), 'test_walk_dir')
        self.extra_dir = osp.join(self.main_dir, 'extra')

        os.makedirs(self.extra_dir, exist_ok=True)

        @system.preserve_cwd(self.main_dir)
        def make_files_1():
            with open('main.png', 'w') as f:
                f.write('.png file in main directory.')
            with open('main.jpeg', 'w') as f:
                f.write('.jpeg file in main directory.')

        @system.preserve_cwd(self.extra_dir)
        def make_files_2():
            with open('extra.png', 'w') as f:
                f.write('.png file in extra directory.')
            with open('extra.inp', 'w') as f:
                f.write('.inp file in extra directory.')

        os.chdir(self.main_dir)
        make_files_1()
        make_files_2()

        def teardown():
            tmpdir.chdir()
            shutil.rmtree(self.main_dir)
        request.addfinalizer(teardown)

    def test__no_files_to_find(self):
        assert system.walk_dir('.txt') == []

    def test__find_main_dir_only(self):
        assert system.walk_dir('.jpeg') == [osp.join(self.main_dir,
                                                     'main.jpeg')]

    def test__find_extra_dir_only(self):
        assert (system.walk_dir('.inp') ==
                [osp.join(self.main_dir, self.extra_dir, 'extra.inp')])

    def test__find_both_dirs(self):
        assert (system.walk_dir('.png') ==
                [osp.join(self.main_dir, self.extra_dir, 'extra.png'),
                 osp.join(self.main_dir, 'main.png')])

# Test zip_file
@pytest.fixture(scope='function')
def zip_setup(request):
    file_name = 'junk.txt'
    with open(file_name, 'w') as f:
        f.write('Test file')

    def teardown():
        try:
            os.remove(file_name)
        except FileNotFoundError:
            os.remove('{}.gz'.format(file_name))
    request.addfinalizer(teardown)
    return file_name

def test__zip_file(zip_setup):
    system.zip_file(zip_setup)
    subprocess.call(['gunzip', '{}.gz'.format(zip_setup)])
    with open(zip_setup, 'r') as f:
        text = f.read()
    assert 'Test file' == text
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# (c) 2015-2018, ETH Zurich, Institut fuer Theoretische Physik
# Author: Dominik Gresch <greschd@gmx.ch>
"""A tool for creating / loading and manipulating tight-binding models."""

__version__ = "1.4.0.rc1"

# import order is important due to circular imports
from . import helpers
from . import exceptions
from ._tb_model import Model

from . import kdotp
from . import io

__all__ = ("helpers", "exceptions", "Model", "kdotp", "io")
<commit_msg>Update version to second release candidate<commit_after># -*- coding: utf-8 -*-

# (c) 2015-2018, ETH Zurich, Institut fuer Theoretische Physik
# Author: Dominik Gresch <greschd@gmx.ch>
"""A tool for creating / loading and manipulating tight-binding models."""

__version__ = "1.4.0.rc2"

# import order is important due to circular imports
from . import helpers
from . import exceptions
from ._tb_model import Model

from . import kdotp
from . import io

__all__ = ("helpers", "exceptions", "Model", "kdotp", "io")
<|endoftext|>


<commit_before># coding=utf-8
import sys
import datetime

class TeamcityServiceMessages(object):
    quote = {"'": "|'", "|": "||", "\n": "|n", "\r": "|r", ']': '|]'}

    def __init__(self, output=sys.stdout, now=datetime.datetime.now):
        self.output = output
        self.now = now

    def escapeValue(self, value):
        return "".join([self.quote.get(x, x) for x in value])

    def message(self, messageName, **properties):
        timestamp = self.now().strftime("%Y-%m-%dT%H:%M:%S.") + "%03d" % (self.now().microsecond / 1000)
        message = ("\n##teamcity[%s timestamp='%s'" % (messageName, timestamp))

        for k in sorted(properties.keys()):
            value = properties[k]
            if value is None:
                continue

            message += (" %s='%s'" % (k, self.escapeValue(value)))

        message += ("]\n")

        # Python may buffer it for a long time, flushing helps to see real-time result
        self.output.write(message)
        self.output.flush()

    def _single_value_message(self, messageName, value):
        self.output.write("\n##teamcity[%s '%s']\n" % (messageName, self.escapeValue(value)))

    def testSuiteStarted(self, suiteName, flowId=None):
        self.message('testSuiteStarted', name=suiteName, flowId=flowId)

    def testSuiteFinished(self, suiteName, flowId=None):
        self.message('testSuiteFinished', name=suiteName, flowId=flowId)

    def testStarted(self, testName, captureStandardOutput=None, flowId=None):
        self.message('testStarted', name=testName, captureStandardOutput=captureStandardOutput, flowId=flowId)

    def testFinished(self, testName, testDuration=None, flowId=None):
        if testDuration is not None:
            duration_ms = testDuration.days * 86400000 + \
                testDuration.seconds * 1000 + \
                int(testDuration.microseconds / 1000)
            self.message('testFinished', name=testName, duration=str(duration_ms), flowId=flowId)
        else:
            self.message('testFinished', name=testName, flowId=flowId)

    def testIgnored(self, testName, message='', flowId=None):
        self.message('testIgnored', name=testName, message=message, flowId=flowId, details=message, text=message)

    def testFailed(self, testName, message='', details='', flowId=None):
        self.message('testFailed', name=testName, message=message, details=details, flowId=flowId)

    def testStdOut(self, testName, out, flowId=None):
        self.message('testStdOut', name=testName, out=out, flowId=flowId)

    def testStdErr(self, testName, out, flowId=None):
        self.message('testStdErr', name=testName, out=out, flowId=flowId)

    def publishArtifacts(self, path, flowId=None):
        self._single_value_message('publishArtifacts', path)

    def buildStatisticLinesCovered(self, linesCovered):
        self.message('buildStatisticValue', key='CodeCoverageAbsLCovered', value=str(linesCovered))

    def buildStatisticTotalLines(self, totalLines):
        self.message('buildStatisticValue', key='CodeCoverageAbsLTotal', value=str(totalLines))

    def buildStatisticLinesUncovered(self, linesUncovered):
        self.message('buildStatisticValue', key='CodeCoverageAbsLUncovered', value=str(linesUncovered))

    def customMessage(self, text, status, errorDetails='', flowId=None):
        self.message('message', text=text, status=status, errorDetails=errorDetails, flowId=flowId)
<commit_msg>updating to be correct<commit_after># coding=utf-8
import sys
import datetime

class TeamcityServiceMessages(object):
    quote = {"'": "|'", "|": "||", "\n": "|n", "\r": "|r", ']': '|]'}

    def __init__(self, output=sys.stdout, now=datetime.datetime.now):
        self.output = output
        self.now = now

    def escapeValue(self, value):
        return "".join([self.quote.get(x, x) for x in value])

    def message(self, messageName, **properties):
        timestamp = self.now().strftime("%Y-%m-%dT%H:%M:%S.") + "%03d" % (self.now().microsecond / 1000)
        message = ("\n##teamcity[%s timestamp='%s'" % (messageName, timestamp))

        for k in sorted(properties.keys()):
            value = properties[k]
            if value is None:
                continue

            message += (" %s='%s'" % (k, self.escapeValue(value)))

        message += ("]\n")

        # Python may buffer it for a long time, flushing helps to see real-time result
        self.output.write(message)
        self.output.flush()

    def _single_value_message(self, messageName, value):
        self.output.write("\n##teamcity[%s '%s']\n" % (messageName, self.escapeValue(value)))

    def testSuiteStarted(self, suiteName, flowId=None):
        self.message('testSuiteStarted', name=suiteName, flowId=flowId)

    def testSuiteFinished(self, suiteName, flowId=None):
        self.message('testSuiteFinished', name=suiteName, flowId=flowId)

    def testStarted(self, testName, captureStandardOutput=None, flowId=None):
        self.message('testStarted', name=testName, captureStandardOutput=captureStandardOutput, flowId=flowId)

    def testFinished(self, testName, testDuration=None, flowId=None):
        if testDuration is not None:
            duration_ms = testDuration.days * 86400000 + \
                testDuration.seconds * 1000 + \
                int(testDuration.microseconds / 1000)
            self.message('testFinished', name=testName, duration=str(duration_ms), flowId=flowId)
        else:
            self.message('testFinished', name=testName, flowId=flowId)

    def testIgnored(self, testName, message='', flowId=None):
        self.message('testIgnored', name=testName, message=message, flowId=flowId)

    def testFailed(self, testName, message='', details='', flowId=None):
        self.message('testFailed', name=testName, message=message, details=details, flowId=flowId)

    def testStdOut(self, testName, out, flowId=None):
        self.message('testStdOut', name=testName, out=out, flowId=flowId)

    def testStdErr(self, testName, out, flowId=None):
        self.message('testStdErr', name=testName, out=out, flowId=flowId)

    def publishArtifacts(self, path, flowId=None):
        self._single_value_message('publishArtifacts', path)

    def buildStatisticLinesCovered(self, linesCovered):
        self.message('buildStatisticValue', key='CodeCoverageAbsLCovered', value=str(linesCovered))

    def buildStatisticTotalLines(self, totalLines):
        self.message('buildStatisticValue', key='CodeCoverageAbsLTotal', value=str(totalLines))

    def buildStatisticLinesUncovered(self, linesUncovered):
        self.message('buildStatisticValue', key='CodeCoverageAbsLUncovered', value=str(linesUncovered))

    def customMessage(self, text, status, errorDetails='', flowId=None):
        self.message('message', text=text, status=status, errorDetails=errorDetails, flowId=flowId)
<|endoftext|>


<commit_before>from tehbot.plugins import *
import wolframalpha
import re
import random

class AiHandler(ChannelHandler):
    @staticmethod
    def decision_answers():
        return [
                "Yes",
                "No",
                "Sure",
                "Absolutely",
                "For sure",
                "No, you are!",
                "Will you ever get sick of this?",
                "Certainly not",
                "I haven't made up my mind yet."
                ]

    def initialize(self, dbconn):
        ChannelHandler.initialize(self, dbconn)

        try:
            self.client = wolframalpha.Client(self.settings["wolframalpha_app_id"])
        except:
            self.client = None

        try:
            import plugins.SolversPlugin
            self.solvers_plugin_found = True
        except:
            self.solvers_plugin_found = False

        try:
            from tehbot.plugins.duckduckgo import ddgsearch
        except:
            print "bar!?"
            pass

    def ddg_handler(self, what):
        return ddgsearch(what)

    def wolfram_handler(self, what):
        if not self.client:
            return "No Wolfram"

        for p in self.client.query(what).pods:
            if p.id == "Result" and p.text:
                txt = " ".join(p.text.splitlines())
                return Plugin.shorten("%s: %s" % (event.source.nick, txt), 350)

        for p in self.client.query(what).pods:
            if p.id == "Misc" and p.text:
                txt = " ".join(p.text.splitlines())
                print txt

        raise Exception("hm")

    def execute(self, connection, event, extra, dbconn):
        botname = connection.get_nickname()
        decide_regex = [
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,?\s*(?:is|are|can|has|was|were|should|do|does)\s+\S[\s\S]*\?''' % botname, re.I),
                ]
        regex = [
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,\s*(?P<what>.+?)\s*\??$''' % botname, re.I),
                ]
        solved_regex = [
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,?\s*has\s+(?P<who>\S+)\s+solved\s+(?P<chall>\S[\s\S]*?|"[^"]+"|'[^']+')(?:\s+on\s+(?P<site>\S[\s\S]*?|"[^"]+"|'[^']+'))?\s*\??$''' % botname, re.I),
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,?\s*did\s+(?P<who>\S+)\s+solve\s+(?P<chall>\S[\s\S]*?|"[^"]+"|'[^']+')(?:\s+on\s+(?P<site>\S[\s\S]*?|"[^"]+"|'[^']+'))?\s*\??$''' % botname, re.I)
                ]

        backend = self.settings.get("backend", "wolfram")
        aihandler = getattr(self, "%s_handler" % backend)

        for r in solved_regex:
            match = r.search(extra["msg"])
            if match is not None:
                user = match.group(1)
                chall = match.group(2)
                site = match.group(3)

                chall = " ".join(Plugin.mysplit(chall))
                if site is not None:
                    site = " ".join(Plugin.mysplit(site))

                return [("solvers", (site, chall, False, user))]

        for r in decide_regex:
            match = r.search(extra["msg"])
            if match is not None:
                return random.choice(AiHandler.decision_answers())

        for r in regex:
            match = r.search(extra["msg"])
            if match is not None:
                what = match.group(1).strip()
                if not what:
                    return

                try:
                    return aihandler(what)
                except:
                    return [("me", "shrugs")]
<commit_msg>ai module: fix imports<commit_after>from __future__ import absolute_import
from tehbot.plugins import *
import wolframalpha
import re
import random

class AiHandler(ChannelHandler):
    @staticmethod
    def decision_answers():
        return [
                "Yes",
                "No",
                "Sure",
                "Absolutely",
                "For sure",
                "No, you are!",
                "Will you ever get sick of this?",
                "Certainly not",
                "I haven't made up my mind yet."
                ]

    def initialize(self, dbconn):
        ChannelHandler.initialize(self, dbconn)

        try:
            self.client = wolframalpha.Client(self.settings["wolframalpha_app_id"])
        except:
            self.client = None

        try:
            import plugins.SolversPlugin
            self.solvers_plugin_found = True
        except:
            self.solvers_plugin_found = False

        try:
            from tehbot.plugins.duckduckgo import ddgsearch
        except:
            print "bar!?"
            pass

    def ddg_handler(self, what):
        return ddgsearch(what)

    def wolfram_handler(self, what):
        if not self.client:
            return "No Wolfram|Alpha"

        for p in self.client.query(what).pods:
            if p.id == "Result" and p.text:
                txt = " ".join(p.text.splitlines())
                return Plugin.shorten("%s: %s" % (event.source.nick, txt), 350)

        for p in self.client.query(what).pods:
            if p.id == "Misc" and p.text:
                txt = " ".join(p.text.splitlines())
                print txt

        raise Exception("hm")

    def execute(self, connection, event, extra, dbconn):
        botname = connection.get_nickname()
        decide_regex = [
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,?\s*(?:is|are|can|has|was|were|should|do|does)\s+\S[\s\S]*\?''' % botname, re.I),
                ]
        regex = [
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,\s*(?P<what>.+?)\s*\??$''' % botname, re.I),
                ]
        solved_regex = [
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,?\s*has\s+(?P<who>\S+)\s+solved\s+(?P<chall>\S[\s\S]*?|"[^"]+"|'[^']+')(?:\s+on\s+(?P<site>\S[\s\S]*?|"[^"]+"|'[^']+'))?\s*\??$''' % botname, re.I),
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,?\s*did\s+(?P<who>\S+)\s+solve\s+(?P<chall>\S[\s\S]*?|"[^"]+"|'[^']+')(?:\s+on\s+(?P<site>\S[\s\S]*?|"[^"]+"|'[^']+'))?\s*\??$''' % botname, re.I)
                ]

        backend = self.settings.get("backend", "wolfram")
        aihandler = getattr(self, "%s_handler" % backend)

        for r in solved_regex:
            match = r.search(extra["msg"])
            if match is not None:
                user = match.group(1)
                chall = match.group(2)
                site = match.group(3)

                chall = " ".join(Plugin.mysplit(chall))
                if site is not None:
                    site = " ".join(Plugin.mysplit(site))

                return [("solvers", (site, chall, False, user))]

        for r in decide_regex:
            match = r.search(extra["msg"])
            if match is not None:
                return random.choice(AiHandler.decision_answers())

        for r in regex:
            match = r.search(extra["msg"])
            if match is not None:
                what = match.group(1).strip()
                if not what:
                    return

                try:
                    return aihandler(what)
                except:
                    return [("me", "shrugs")]
<|endoftext|>


<commit_before>import ai_random

# Available AI algorithms.
# Each should be a function that takes as input an instance of Game
# and returns as output an instance of Edge.
random = ai_random.main

# Look up an AI algorithm by a specified name
ai_list = {'random' : random}

def lookup_ai(name):
    if name in ai_list:
        return ai_list[name]
    else:
        return None
<commit_msg>Registered greed.py<commit_after>import ai_random
import ai_greedy

# Available AI algorithms.
# Each should be a function that takes as input an instance of Game
# and returns as output an instance of Edge.
random = ai_random.main
greedy = ai_greedy.main

# Look up an AI algorithm by a specified name
ai_list = {'random' : random,
        'greedy' : greedy}

def lookup_ai(name):
    if name in ai_list:
        return ai_list[name]
    else:
        return None
<|endoftext|>


<commit_before>
from twilio.rest import TwilioRestClient

from alerta.app import app
from alerta.plugins import PluginBase

LOG = app.logger

TWILIO_ACCOUNT_SID = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
TWILIO_AUTH_TOKEN = ''

TWILIO_TO_NUMBER = ''
TWILIO_FROM_NUMBER = ''

class SendSMSMessage(PluginBase):

    def pre_receive(self, alert):

        return alert

    def post_receive(self, alert):

        if alert.repeat:
            return

        message = "%s: %s alert for %s - %s is %s" % (
            alert.environment, alert.severity.capitalize(),
            ','.join(alert.service), alert.resource, alert.event
        )

        client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
        message = client.messages.create(body=message, to=TWILIO_TO_NUMBER, from_=TWILIO_FROM_NUMBER)

        LOG.info("Twilio SMS Message ID: %s", message.sid)
<commit_msg>Use env vars to config twilio sms plugin<commit_after>
import os

from twilio.rest import TwilioRestClient

from alerta.app import app
from alerta.plugins import PluginBase

LOG = app.logger

TWILIO_ACCOUNT_SID = os.environ.get('TWILIO_ACCOUNT_SID')
TWILIO_AUTH_TOKEN = os.environ.get('TWILIO_AUTH_TOKEN')

TWILIO_TO_NUMBER = os.environ.get('TWILIO_TO_NUMBER')
TWILIO_FROM_NUMBER = os.environ.get('TWILIO_FROM_NUMBER')

class SendSMSMessage(PluginBase):

    def pre_receive(self, alert):

        return alert

    def post_receive(self, alert):

        if alert.repeat:
            return

        message = "%s: %s alert for %s - %s is %s" % (
            alert.environment, alert.severity.capitalize(),
            ','.join(alert.service), alert.resource, alert.event
        )

        client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
        message = client.messages.create(body=message, to=TWILIO_TO_NUMBER, from_=TWILIO_FROM_NUMBER)

        LOG.info("Twilio SMS Message ID: %s", message.sid)
<|endoftext|>


<commit_before>from poap.controller import ScriptedController

# from poap.strategy import RetryStrategy
from poap.strategy import FixedSampleStrategy
from poap.strategy import CoroutineStrategy
from poap.strategy import CoroutineBatchStrategy
from poap.strategy import PromiseStrategy
from poap.strategy import ThreadStrategy
from poap.strategy import CheckWorkerStrategy
from poap.strategy import SimpleMergedStrategy
from poap.strategy import MultiStartStrategy
from poap.strategy import MaxEvalStrategy
# from poap.strategy import InputStrategy

import time

def test_fixed_sample1():
    "Check FixedStrategy in case of no kills or failures"
    c = ScriptedController()
    c.strategy = FixedSampleStrategy([1, 2, 3, 4])
    c.accept_eval(args=(1,)).complete(1)
    c.accept_eval(args=(2,)).complete(2)
    c.accept_eval(args=(3,)).complete(3)
    c.accept_eval(args=(4,)).complete(4)
    c.accept_terminate()
    c.terminate()

def test_fixed_sample2():
    "Check that FixedStrategy does retries and None proposals right"
    def run(strategy):
        c = ScriptedController()
        c.strategy = strategy
        r1 = c.accept_eval(args=(1,))
        r2 = c.accept_eval(args=(2,))
        c.reject_eval(args=(3,))
        r1.kill()
        r2.running()
        c.accept_eval(args=(1,)).complete(1)
        c.accept_eval(args=(3,)).complete(3)
        c.accept_eval(args=(4,)).complete(4)
        c.no_proposal()
        r2.complete(2)
        c.reject_terminate()
        c.accept_terminate()
        c.terminate()

    def g():
        for k in range(1, 5):
            yield k
    run(FixedSampleStrategy([1, 2, 3, 4]))
    run(FixedSampleStrategy(g()))

def coroutine_optimizer(pts):
    for p in pts:
        f = yield p
        assert f == p

def test_coroutine1():
    "Check coroutine strategy with immediate exit"
    c = ScriptedController()
    c.strategy = CoroutineStrategy(coroutine_optimizer([]))
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_coroutine2():
    "Check coroutine strategy for correct retry behavior"
    c = ScriptedController()
    c.strategy = CoroutineStrategy(coroutine_optimizer([1, 2, 3]))

    # Do we re-try a rejected proposal?
    c.reject_eval(args=(1,))
    r = c.accept_eval(args=(1,))
    c.no_proposal()
    r.complete(1)

    # Do we retry on kill?
    r = c.accept_eval(args=(2,))
    c.no_proposal()
    r.kill()
    r = c.accept_eval(args=(2,))
    r.running()
    c.no_proposal()
    r.complete(2)

    # And check the simple case where everything goes without incident
    c.accept_eval(args=(3,)).complete(3)
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def coroutine_batch_optimizer(batches):
    for b in batches:
        f = yield b
        assert f == b, "Expect {0}, saw {1}".format(b, f)

def test_coroutine_batch1():
    "Check coroutine batch strategy on immediate termination"
    c = ScriptedController()
    c.strategy = CoroutineBatchStrategy(coroutine_batch_optimizer([]))
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_coroutine_batch2():
    "Check coroutine batch strategy with empty list"
    c = ScriptedController()
    c.strategy = CoroutineBatchStrategy(coroutine_batch_optimizer([[]]))
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_coroutine_batch3():
    "Check coroutine batch strategy"
    c = ScriptedController()
    o = coroutine_batch_optimizer([[1, 2], [3, 4]])
    c.strategy = CoroutineBatchStrategy(o)

    # Make trouble for batch [1,2]
    c.reject_eval(args=(2,))
    r1 = c.accept_eval(args=(2,))
    c.reject_eval(args=(1,))
    r2 = c.accept_eval(args=(1,))
    c.no_proposal()
    r1.kill()
    c.accept_eval(args=(2,))
    r2.complete(1)
    c.no_proposal()
    r1.complete(2)

    # Let batch [3,4] go through without incident
    r = c.accept_eval(args=(4,))
    r.complete(4)
    r = c.accept_eval(args=(3,))
    r.complete(3)

    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_promise1():
    "Check promise batch strategy"
    c = ScriptedController()
    s = PromiseStrategy()
    c.strategy = s

    # Start promise and check not ready
    p = s.promise_eval(1)
    assert not p.ready()

    # Give the eval a hard time before completing
    c.reject_eval(args=(1,))
    r = c.accept_eval(args=(1,))
    r.kill()
    r = c.accept_eval(args=(1,))
    assert not p.ready()
    r.complete(1)

    # Check to make sure the value came through
    assert p.value == 1

    # Terminate
    s.terminate()
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def thread_optimizer(strategy, vs, v):
    assert vs == strategy.blocking_evals(vs)
    assert v == strategy.blocking_eval(v)

def test_thread1():
    "Check promise batch strategy"
    c = ScriptedController()
    c.strategy = ThreadStrategy(c, lambda s: thread_optimizer(s, [1, 2], 3))

    # Make some trouble for the first two points
    r1 = c.accept_eval(args=(1,), skip=True)
    c.reject_eval(args=(2,), skip=True)
    r2 = c.accept_eval(args=(2,), skip=True)
    c.no_proposal()
    r1.kill()
    r1 = c.accept_eval(args=(1,), skip=True)
    c.no_proposal()
    r1.complete(1)
    r2.complete(2)

    # Let the third proposal go through easy
    r3 = c.accept_eval(args=(3,), skip=True)
    c.no_proposal()
    r3.complete(3)

    # Reject two termination messages before we quit
    c.reject_terminate(skip=True)
    c.reject_terminate(skip=True)
    c.accept_terminate(skip=True)
    c.terminate()

def test_check_worker0():
    "Check: Without CheckWorkerStrategy, FixedStrategy ignores availability"
    c = ScriptedController()
    c.strategy = FixedSampleStrategy([1, 2])
    c.set_worker(False)
    r1 = c.accept_eval(args=(1,))
    r1.complete(1)
    r2 = c.accept_eval(args=(2,))
    r2.complete(2)
    c.accept_terminate()
    c.terminate()

def test_check_worker1():
    "Test CheckWorkerStrategy"
    c = ScriptedController()
    strategy = FixedSampleStrategy([1, 2])
    c.strategy = CheckWorkerStrategy(c, strategy)
    r1 = c.accept_eval(args=(1,))
    c.set_worker(False)
    r2 = c.no_proposal()
    r1.complete(1)
    c.set_worker(True)
    r2 = c.accept_eval(args=(2,))
    r2.complete(2)
    c.accept_terminate()
    c.terminate()

def test_simple_merge1():
    "Test SimpleMergeStrategy"
    c = ScriptedController()
    strategy1 = PromiseStrategy(block=False)
    strategy2 = FixedSampleStrategy([1, 2, 3])
    c.strategy = SimpleMergedStrategy(c, [strategy1, strategy2])

    # Allow strategy2 to get one in, then strategy1 pre-empts
    r1 = c.accept_eval(args=(1,))
    p = strategy1.promise_eval(100)
    r2 = c.accept_eval(args=(100,))
    r1.complete(1)
    assert not p.ready()
    r2.complete(100)
    assert p.value == 100
    strategy1.terminate()

    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_multistart1():
    "Test MultistartStrategy"
    c = ScriptedController()
    strategy1 = PromiseStrategy(block=False)
    strategy2 = FixedSampleStrategy([1, 2, 3])
    c.strategy = MultiStartStrategy(c, [strategy1, strategy2])

    # Allow strategy2 to get one in, then strategy1 pre-empts
    r1 = c.accept_eval(args=(1,))
    p = strategy1.promise_eval(100)
    time.sleep(0.1)
    r2 = c.accept_eval(args=(100,))
    r1.complete(1)
    assert not p.ready()
    r2.complete(100)
    assert p.value == 100
    strategy1.terminate()

    # Now strategy2 finishes
    c.accept_eval(args=(2,)).complete(2)
    c.accept_eval(args=(3,)).complete(3)

    # Now we should see termination
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_maxeval1():
    "Test MaxEvalStrategy"
    c = ScriptedController()
    strategy = FixedSampleStrategy([1, 2, 3, 4, 5])
    c.strategy = MaxEvalStrategy(c, strategy, 2)
    c.accept_eval(args=(1,)).complete(1)
    c.accept_eval(args=(2,)).complete(2)
    c.accept_terminate()
    c.terminate()

def test_maxeval2():
    "Test MaxEvalStrategy"
    c = ScriptedController()
    strategy1 = MaxEvalStrategy(c, 2)
    strategy2 = FixedSampleStrategy([1, 2, 3, 4, 5])
    c.strategy = SimpleMergedStrategy(c, [strategy1, strategy2])
    c.accept_eval(args=(1,)).complete(1)
    c.accept_eval(args=(2,)).complete(2)
    c.accept_terminate()
    c.terminate()

def main():
    test_fixed_sample1()
    test_fixed_sample2()
    test_coroutine1()
    test_coroutine2()
    test_coroutine_batch1()
    test_coroutine_batch2()
    test_coroutine_batch3()
    test_promise1()
    test_thread1()
    test_check_worker0()
    test_check_worker1()
    test_simple_merge1()
    test_multistart1()
    test_maxeval1()

if __name__ == "__main__":
    main()
<commit_msg>Killed bogus test on MaxEvalStrategy (left over from playing with alternate variant)<commit_after>from poap.controller import ScriptedController

# from poap.strategy import RetryStrategy
from poap.strategy import FixedSampleStrategy
from poap.strategy import CoroutineStrategy
from poap.strategy import CoroutineBatchStrategy
from poap.strategy import PromiseStrategy
from poap.strategy import ThreadStrategy
from poap.strategy import CheckWorkerStrategy
from poap.strategy import SimpleMergedStrategy
from poap.strategy import MultiStartStrategy
from poap.strategy import MaxEvalStrategy
# from poap.strategy import InputStrategy

import time

def test_fixed_sample1():
    "Check FixedStrategy in case of no kills or failures"
    c = ScriptedController()
    c.strategy = FixedSampleStrategy([1, 2, 3, 4])
    c.accept_eval(args=(1,)).complete(1)
    c.accept_eval(args=(2,)).complete(2)
    c.accept_eval(args=(3,)).complete(3)
    c.accept_eval(args=(4,)).complete(4)
    c.accept_terminate()
    c.terminate()

def test_fixed_sample2():
    "Check that FixedStrategy does retries and None proposals right"
    def run(strategy):
        c = ScriptedController()
        c.strategy = strategy
        r1 = c.accept_eval(args=(1,))
        r2 = c.accept_eval(args=(2,))
        c.reject_eval(args=(3,))
        r1.kill()
        r2.running()
        c.accept_eval(args=(1,)).complete(1)
        c.accept_eval(args=(3,)).complete(3)
        c.accept_eval(args=(4,)).complete(4)
        c.no_proposal()
        r2.complete(2)
        c.reject_terminate()
        c.accept_terminate()
        c.terminate()

    def g():
        for k in range(1, 5):
            yield k
    run(FixedSampleStrategy([1, 2, 3, 4]))
    run(FixedSampleStrategy(g()))

def coroutine_optimizer(pts):
    for p in pts:
        f = yield p
        assert f == p

def test_coroutine1():
    "Check coroutine strategy with immediate exit"
    c = ScriptedController()
    c.strategy = CoroutineStrategy(coroutine_optimizer([]))
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_coroutine2():
    "Check coroutine strategy for correct retry behavior"
    c = ScriptedController()
    c.strategy = CoroutineStrategy(coroutine_optimizer([1, 2, 3]))

    # Do we re-try a rejected proposal?
    c.reject_eval(args=(1,))
    r = c.accept_eval(args=(1,))
    c.no_proposal()
    r.complete(1)

    # Do we retry on kill?
    r = c.accept_eval(args=(2,))
    c.no_proposal()
    r.kill()
    r = c.accept_eval(args=(2,))
    r.running()
    c.no_proposal()
    r.complete(2)

    # And check the simple case where everything goes without incident
    c.accept_eval(args=(3,)).complete(3)
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def coroutine_batch_optimizer(batches):
    for b in batches:
        f = yield b
        assert f == b, "Expect {0}, saw {1}".format(b, f)

def test_coroutine_batch1():
    "Check coroutine batch strategy on immediate termination"
    c = ScriptedController()
    c.strategy = CoroutineBatchStrategy(coroutine_batch_optimizer([]))
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_coroutine_batch2():
    "Check coroutine batch strategy with empty list"
    c = ScriptedController()
    c.strategy = CoroutineBatchStrategy(coroutine_batch_optimizer([[]]))
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_coroutine_batch3():
    "Check coroutine batch strategy"
    c = ScriptedController()
    o = coroutine_batch_optimizer([[1, 2], [3, 4]])
    c.strategy = CoroutineBatchStrategy(o)

    # Make trouble for batch [1,2]
    c.reject_eval(args=(2,))
    r1 = c.accept_eval(args=(2,))
    c.reject_eval(args=(1,))
    r2 = c.accept_eval(args=(1,))
    c.no_proposal()
    r1.kill()
    c.accept_eval(args=(2,))
    r2.complete(1)
    c.no_proposal()
    r1.complete(2)

    # Let batch [3,4] go through without incident
    r = c.accept_eval(args=(4,))
    r.complete(4)
    r = c.accept_eval(args=(3,))
    r.complete(3)

    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_promise1():
    "Check promise batch strategy"
    c = ScriptedController()
    s = PromiseStrategy()
    c.strategy = s

    # Start promise and check not ready
    p = s.promise_eval(1)
    assert not p.ready()

    # Give the eval a hard time before completing
    c.reject_eval(args=(1,))
    r = c.accept_eval(args=(1,))
    r.kill()
    r = c.accept_eval(args=(1,))
    assert not p.ready()
    r.complete(1)

    # Check to make sure the value came through
    assert p.value == 1

    # Terminate
    s.terminate()
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def thread_optimizer(strategy, vs, v):
    assert vs == strategy.blocking_evals(vs)
    assert v == strategy.blocking_eval(v)

def test_thread1():
    "Check promise batch strategy"
    c = ScriptedController()
    c.strategy = ThreadStrategy(c, lambda s: thread_optimizer(s, [1, 2], 3))

    # Make some trouble for the first two points
    r1 = c.accept_eval(args=(1,), skip=True)
    c.reject_eval(args=(2,), skip=True)
    r2 = c.accept_eval(args=(2,), skip=True)
    c.no_proposal()
    r1.kill()
    r1 = c.accept_eval(args=(1,), skip=True)
    c.no_proposal()
    r1.complete(1)
    r2.complete(2)

    # Let the third proposal go through easy
    r3 = c.accept_eval(args=(3,), skip=True)
    c.no_proposal()
    r3.complete(3)

    # Reject two termination messages before we quit
    c.reject_terminate(skip=True)
    c.reject_terminate(skip=True)
    c.accept_terminate(skip=True)
    c.terminate()

def test_check_worker0():
    "Check: Without CheckWorkerStrategy, FixedStrategy ignores availability"
    c = ScriptedController()
    c.strategy = FixedSampleStrategy([1, 2])
    c.set_worker(False)
    r1 = c.accept_eval(args=(1,))
    r1.complete(1)
    r2 = c.accept_eval(args=(2,))
    r2.complete(2)
    c.accept_terminate()
    c.terminate()

def test_check_worker1():
    "Test CheckWorkerStrategy"
    c = ScriptedController()
    strategy = FixedSampleStrategy([1, 2])
    c.strategy = CheckWorkerStrategy(c, strategy)
    r1 = c.accept_eval(args=(1,))
    c.set_worker(False)
    r2 = c.no_proposal()
    r1.complete(1)
    c.set_worker(True)
    r2 = c.accept_eval(args=(2,))
    r2.complete(2)
    c.accept_terminate()
    c.terminate()

def test_simple_merge1():
    "Test SimpleMergeStrategy"
    c = ScriptedController()
    strategy1 = PromiseStrategy(block=False)
    strategy2 = FixedSampleStrategy([1, 2, 3])
    c.strategy = SimpleMergedStrategy(c, [strategy1, strategy2])

    # Allow strategy2 to get one in, then strategy1 pre-empts
    r1 = c.accept_eval(args=(1,))
    p = strategy1.promise_eval(100)
    r2 = c.accept_eval(args=(100,))
    r1.complete(1)
    assert not p.ready()
    r2.complete(100)
    assert p.value == 100
    strategy1.terminate()

    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_multistart1():
    "Test MultistartStrategy"
    c = ScriptedController()
    strategy1 = PromiseStrategy(block=False)
    strategy2 = FixedSampleStrategy([1, 2, 3])
    c.strategy = MultiStartStrategy(c, [strategy1, strategy2])

    # Allow strategy2 to get one in, then strategy1 pre-empts
    r1 = c.accept_eval(args=(1,))
    p = strategy1.promise_eval(100)
    time.sleep(0.1)
    r2 = c.accept_eval(args=(100,))
    r1.complete(1)
    assert not p.ready()
    r2.complete(100)
    assert p.value == 100
    strategy1.terminate()

    # Now strategy2 finishes
    c.accept_eval(args=(2,)).complete(2)
    c.accept_eval(args=(3,)).complete(3)

    # Now we should see termination
    c.reject_terminate()
    c.reject_terminate()
    c.accept_terminate()
    c.terminate()

def test_maxeval1():
    "Test MaxEvalStrategy"
    c = ScriptedController()
    strategy1 = MaxEvalStrategy(c, 2)
    strategy2 = FixedSampleStrategy([1, 2, 3, 4, 5])
    c.strategy = SimpleMergedStrategy(c, [strategy1, strategy2])
    c.accept_eval(args=(1,)).complete(1)
    c.accept_eval(args=(2,)).complete(2)
    c.accept_terminate()
    c.terminate()

def main():
    test_fixed_sample1()
    test_fixed_sample2()
    test_coroutine1()
    test_coroutine2()
    test_coroutine_batch1()
    test_coroutine_batch2()
    test_coroutine_batch3()
    test_promise1()
    test_thread1()
    test_check_worker0()
    test_check_worker1()
    test_simple_merge1()
    test_multistart1()
    test_maxeval1()

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before>import os
import subprocess

from astropy import units as u

from ..utils import current_time
from ..utils import error
from .camera import AbstractGPhotoCamera

class Camera(AbstractGPhotoCamera):

    def __init__(self, config, **kwargs):
        super().__init__(config, **kwargs)
        self.logger.debug("Initializing GPhoto2 camera")
        self.connect()

    def connect(self):
        """
        For Canon DSLRs using gphoto2, this just means confirming that there is
        a camera on that port and that we can communicate with it.
        """
        self.logger.debug('Connecting to camera')

        self.set_property('/main/actions/viewfinder', 0)       # Screen off
        self.set_property('/main/settings/autopoweroff', 0)     # Don't power off
        self.set_property('/main/settings/reviewtime', 0)       # Screen off
        self.set_property('/main/settings/capturetarget', 0)    # Internal RAM
        self.set_property('/main/settings/artist', 'Project PANOPTES')
        self.set_property('/main/settings/ownername', 'Project PANOPTES')
        self.set_property('/main/settings/copyright', 'Project PANOPTES 2016')
        self.set_property('/main/imgsettings/imageformat', 9)       # RAW
        self.set_property('/main/imgsettings/imageformatsd', 9)     # RAW
        self.set_property('/main/imgsettings/imageformatcf', 9)     # RAW
        self.set_property('/main/imgsettings/iso', 1)               # ISO 100
        self.set_property('/main/capturesettings/focusmode', 0)         # Manual
        self.set_property('/main/capturesettings/continuousaf', 0)         # No AF
        self.set_property('/main/capturesettings/autoexposuremode', 3)  # 3 - Manual; 4 - Bulb
        self.set_property('/main/capturesettings/drivemode', 0)         # Single exposure
        self.set_property('/main/capturesettings/shutterspeed', 0)      # Bulb
        self.set_property('/main/actions/syncdatetime', 1)  # Sync date and time to computer
        self.set_property('/main/actions/uilock', 1)        # Don't let the UI change

        # Get serial number
        self._serial_number = self.get_property('serialnumber')

        self._connected = True

    @property
    def uid(self):
        return self._serial_number[0:6]

    def construct_filename(self):
        """
        Use the filename_pattern from the camera config file to construct the
        filename for an image from this camera

        Returns:
            str:    Filename format
        """

        now = current_time().datetime.strftime("%Y/%m/%dT%H%M%S")
        date, time = now.split('T')

        filename = os.path.join(
            self._image_dir, self._serial_number, date, "{}.cr2".format(time))

        return filename

    @property
    def is_connected(self):
        """ Is the camera available vai gphoto2 """
        return self._connected

    def take_exposure(self, seconds=1.0 * u.second, filename=None, **kwargs):
        """ Take an exposure for given number of seconds

        Note:
            See `scripts/take_pic.sh`

            Tested With:
                * Canon EOS 100D

        Args:
            seconds(float):     Exposure time, defaults to 0.05 seconds
        """
        assert filename is not None, self.logger.warning("Must pass filename for take_exposure")

        self.logger.debug('Taking {} second exposure on {}'.format(seconds, self.name))

        if not isinstance(seconds, u.Quantity):
            seconds = seconds * u.second

        script_path = '{}/scripts/take_pic.sh'.format(os.getenv('POCS'))

        run_cmd = [script_path, self.port, str(seconds.value), filename]

        # Send command to camera
        try:
            proc = subprocess.Popen(run_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
        except error.InvalidCommand as e:
            self.logger.warning(e)

        return proc
<commit_msg>viewfinder=1 == off<commit_after>import os
import subprocess

from astropy import units as u

from ..utils import current_time
from ..utils import error
from .camera import AbstractGPhotoCamera

class Camera(AbstractGPhotoCamera):

    def __init__(self, config, **kwargs):
        super().__init__(config, **kwargs)
        self.logger.debug("Initializing GPhoto2 camera")
        self.connect()

    def connect(self):
        """
        For Canon DSLRs using gphoto2, this just means confirming that there is
        a camera on that port and that we can communicate with it.
        """
        self.logger.debug('Connecting to camera')

        self.set_property('/main/actions/viewfinder', 1)       # Screen off
        self.set_property('/main/settings/autopoweroff', 0)     # Don't power off
        self.set_property('/main/settings/reviewtime', 0)       # Screen off
        self.set_property('/main/settings/capturetarget', 0)    # Internal RAM
        self.set_property('/main/settings/artist', 'Project PANOPTES')
        self.set_property('/main/settings/ownername', 'Project PANOPTES')
        self.set_property('/main/settings/copyright', 'Project PANOPTES 2016')
        self.set_property('/main/imgsettings/imageformat', 9)       # RAW
        self.set_property('/main/imgsettings/imageformatsd', 9)     # RAW
        self.set_property('/main/imgsettings/imageformatcf', 9)     # RAW
        self.set_property('/main/imgsettings/iso', 1)               # ISO 100
        self.set_property('/main/capturesettings/focusmode', 0)         # Manual
        self.set_property('/main/capturesettings/continuousaf', 0)         # No AF
        self.set_property('/main/capturesettings/autoexposuremode', 3)  # 3 - Manual; 4 - Bulb
        self.set_property('/main/capturesettings/drivemode', 0)         # Single exposure
        self.set_property('/main/capturesettings/shutterspeed', 0)      # Bulb
        self.set_property('/main/actions/syncdatetime', 1)  # Sync date and time to computer
        self.set_property('/main/actions/uilock', 1)        # Don't let the UI change

        # Get serial number
        self._serial_number = self.get_property('serialnumber')

        self._connected = True

    @property
    def uid(self):
        return self._serial_number[0:6]

    def construct_filename(self):
        """
        Use the filename_pattern from the camera config file to construct the
        filename for an image from this camera

        Returns:
            str:    Filename format
        """

        now = current_time().datetime.strftime("%Y/%m/%dT%H%M%S")
        date, time = now.split('T')

        filename = os.path.join(
            self._image_dir, self._serial_number, date, "{}.cr2".format(time))

        return filename

    @property
    def is_connected(self):
        """ Is the camera available vai gphoto2 """
        return self._connected

    def take_exposure(self, seconds=1.0 * u.second, filename=None, **kwargs):
        """ Take an exposure for given number of seconds

        Note:
            See `scripts/take_pic.sh`

            Tested With:
                * Canon EOS 100D

        Args:
            seconds(float):     Exposure time, defaults to 0.05 seconds
        """
        assert filename is not None, self.logger.warning("Must pass filename for take_exposure")

        self.logger.debug('Taking {} second exposure on {}'.format(seconds, self.name))

        if not isinstance(seconds, u.Quantity):
            seconds = seconds * u.second

        script_path = '{}/scripts/take_pic.sh'.format(os.getenv('POCS'))

        run_cmd = [script_path, self.port, str(seconds.value), filename]

        # Send command to camera
        try:
            proc = subprocess.Popen(run_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
        except error.InvalidCommand as e:
            self.logger.warning(e)

        return proc
<|endoftext|>


<commit_before>"""Views for UserNotifications"""
from flask import abort, jsonify, Blueprint, current_app
from sqlalchemy.exc import DataError

from ..database import db
from ..extensions import oauth
from ..models.notification import UserNotification
from ..models.user import current_user
from .portal import check_int

notification_api = Blueprint('notification_api', __name__, url_prefix='/api')

@notification_api.route(
    '/user/<int:user_id>/notification/<int:notification_id>',
    methods=('DELETE',))
@oauth.require_oauth()
def delete_user_notification(user_id, notification_id):
    """Remove the corresponding UserNotification for given User & Notification
    ---
    tags:
      - Notification
      - User
    operationId: deleteUserNotification
    parameters:
      - name: user_id
        in: path
        description: TrueNTH user ID
        required: true
        type: integer
        format: int64
      - name: notification_id
        in: path
        description: General Notification ID
        required: true
        type: integer
        format: int64
    produces:
      - application/json
    responses:
      200:
        description: successful operation
        schema:
          id: response_ok
          required:
            - message
          properties:
            message:
              type: string
              description: Result, typically "ok"
      400:
        description: if the request includes invalid data
      401:
        description:
          if missing valid OAuth token or if the authorized user lacks
          permission to edit requested user_id
      404:
        description: if user_id or notification_id don't exist

    """
    current_app.logger.debug(
        'delete user notification called for user {} and '
        'notification {}'.format(user_id, notification_id))

    check_int(user_id)
    check_int(notification_id)

    user = current_user()
    if user.id != user_id:
        current_user().check_role(permission='edit', other_id=user_id)
        user = get_user(user_id)
    if user.deleted:
        abort(400, "deleted user - operation not permitted")

    un = UserNotification.query.filter_by(
        user_id=user_id, notification_id=notification_id).first()
    if not un:
        abort(404, 'notification {} not found for '
                   'user {}'.format(user_id, notification_id))

    db.session.delete(un)
    db.session.commit()

    return jsonify(message="ok")
<commit_msg>adding new GET endpoint for notifications<commit_after>"""Views for UserNotifications"""
from flask import abort, jsonify, Blueprint, current_app
from sqlalchemy.exc import DataError

from ..database import db
from ..extensions import oauth
from ..models.notification import UserNotification
from ..models.user import current_user
from .portal import check_int

notification_api = Blueprint('notification_api', __name__, url_prefix='/api')

@notification_api.route('/user/<int:user_id>/notification')
@oauth.require_oauth()
def get_user_notification(user_id):
    """Retrieve Notifications for the given User
    ---
    tags:
      - Notification
      - User
    operationId: getUserNotification
    parameters:
      - name: user_id
        in: path
        description: TrueNTH user ID
        required: true
        type: integer
        format: int64
    produces:
      - application/json
    responses:
      200:
        description: successful operation
        schema:
          id: user_notifications
          properties:
            notifications:
              type: array
              items:
                type: object
                required:
                  - id
                  - resourceType
                  - name
                  - content
                  - created_at
                properties:
                  id:
                    type: integer
                    format: int64
                    description: identifier for the Notification
                  resourceType:
                    type: string
                    description: JSON object resource type
                  name:
                    type: string
                    description: identifying name of the Notification
                  content:
                    type: string
                    description: content to be displayed
                  created_at:
                    type: string
                    description:
                      original UTC date-time of the Notification creation
      400:
        description: if the request includes invalid data
      401:
        description:
          if missing valid OAuth token or if the authorized user lacks
          permission to edit requested user_id
      404:
        description: if user_id don't exist

    """
    check_int(user_id)

    user = current_user()
    if user.id != user_id:
        current_user().check_role(permission='edit', other_id=user_id)
        user = get_user(user_id)
    if user.deleted:
        abort(400, "deleted user - operation not permitted")

    notifs = [notif.as_json() for notif in user.notifications]

    return jsonify(notifications=notifs)

@notification_api.route(
    '/user/<int:user_id>/notification/<int:notification_id>',
    methods=('DELETE',))
@oauth.require_oauth()
def delete_user_notification(user_id, notification_id):
    """Remove the corresponding UserNotification for given User & Notification
    ---
    tags:
      - Notification
      - User
    operationId: deleteUserNotification
    parameters:
      - name: user_id
        in: path
        description: TrueNTH user ID
        required: true
        type: integer
        format: int64
      - name: notification_id
        in: path
        description: General Notification ID
        required: true
        type: integer
        format: int64
    produces:
      - application/json
    responses:
      200:
        description: successful operation
        schema:
          id: response_ok
          required:
            - message
          properties:
            message:
              type: string
              description: Result, typically "ok"
      400:
        description: if the request includes invalid data
      401:
        description:
          if missing valid OAuth token or if the authorized user lacks
          permission to edit requested user_id
      404:
        description: if user_id or notification_id don't exist

    """
    current_app.logger.debug(
        'delete user notification called for user {} and '
        'notification {}'.format(user_id, notification_id))

    check_int(user_id)
    check_int(notification_id)

    user = current_user()
    if user.id != user_id:
        current_user().check_role(permission='edit', other_id=user_id)
        user = get_user(user_id)
    if user.deleted:
        abort(400, "deleted user - operation not permitted")

    un = UserNotification.query.filter_by(
        user_id=user_id, notification_id=notification_id).first()
    if not un:
        abort(404, 'notification {} not found for '
                   'user {}'.format(user_id, notification_id))

    db.session.delete(un)
    db.session.commit()

    return jsonify(message="ok")
<|endoftext|>


<commit_before>"""
`InterLap` does fast interval overlap testing with a simple python data
structure.

It works well on the types of querying done in genomic datasets where we have
10's of thousands of intervals and we check for overlap millions of times. It
is very simple and has no dependencies.

It takes tuples or lists where the first 2 elements are start, end and the
remaining elements can be anything.

>>> from interlap import InterLap
>>> inter = InterLap()

Here create 10K random intervals:

>>> import random
>>> sites = random.sample(range(22, 100000000, 12), 50000)
>>> ranges = [(i, i + random.randint(2000, 20000)) for i in sites]

Add them to the interval tree (this takes < 0.5 seconds):

>>> inter.update(ranges)

We can also add one at a time:

>>> inter.add((20, 22, {'info': 'hi'}))

Now do overlap testing:

>>> [20, 21] in inter
True

>>> next(inter.find((21, 21)))
(20, 22, {'info': 'hi'})

>>> inter.add((2, 3, {'info': 'hello'}))

*NOTE*: below shows how edge-cases are handled.

>>> list(inter.find((2, 2)))
[(2, 3, {'info': 'hello'})]
>>> list(inter.find((3, 3)))
[(2, 3, {'info': 'hello'})]

Test every item in the InterLap. These 50K queries take < 0.5 seconds:

>>> for s, e in ranges:
...     assert (s, e) in inter

>>> for i, se in enumerate(inter):
...     if i > 10: break
...     assert se[0] < se[1]
...

>>> list(inter.closest((2, 2)))
[(2, 3, {'info': 'hello'})]

>>> list(inter.closest((2, 21)))
[(2, 3, {'info': 'hello'}), (20, 22, {'info': 'hi'})]

>>> list(inter.closest((2, 21)))
[(2, 3, {'info': 'hello'}), (20, 22, {'info': 'hi'})]

>>> list(inter.closest((11, 12)))
[(2, 3, {'info': 'hello'}), (20, 22, {'info': 'hi'})]

>>> for i in range(10):
...     inter.add((20, 21))
>>> len(list(inter.closest((10, 13))))
12

>>> for i in range(10):
...     inter.add((18, 21))
>>> len(list(inter.closest((10, 13))))
10

>>> for i in range(10):
...     inter.add((5, 5))
>>> len(list(inter.closest((10, 13))))
20

>>> for i in range(10):
...     inter.add((3, 5))
>>> len(list(inter.closest((10, 13))))
30

>>> inter.add((9, 9))
>>> list(inter.closest((10, 13)))
[(9, 9)]

"""
from itertools import groupby
from operator import itemgetter

__all__ = ['InterLap']

__version__ = '0.1.2'

try:
    int_types = (int, long)
except NameError:
    int_types = (int,)

######################################
def binsearch_left_start(intervals, x, lo, hi):
    while lo < hi:
        mid = (lo + hi)//2
        f = intervals[mid]
        if f[0] < x: lo = mid + 1
        else: hi = mid
    return lo

# like python's bisect_right find the _highest_ index where the value x
# could be inserted to maintain order in the list intervals
def binsearch_right_end(intervals, x, lo, hi):
    while lo < hi:
        mid = (lo + hi)//2
        f = intervals[mid]
        if x < f[0]: hi = mid
        else: lo = mid + 1
    return lo

class InterLap(object):

    """Create an Interlap object. (See module docstring)."""

    def __init__(self, ranges=()):
        """The ranges are tuples of (start, end, *whatever)."""
        self._iset = sorted(ranges)
        self._maxlen = max(r[1] - r[0] for r in (ranges or [[0, 0]]))

    def add(self, ranges):
        r"""Add a single (or many) [start, end, \*] item to the tree."""
        if len(ranges) and isinstance(ranges[0], int_types):
            ranges = [ranges]
        iset = self._iset
        self._maxlen = max(self._maxlen, max(r[1] - r[0] + 1 for r in ranges))

        if len(ranges) > 30 or len(iset) < len(ranges):
            iset.extend(ranges)
            iset.sort()
        else:
            for o in ranges:
                iset.insert(binsearch_left_start(iset, o[0], 0, len(iset)), o)

    update = add

    def __len__(self):
        """Return number of intervals."""
        return len(self._iset)

    def find(self, other):
        """Return an interable of elements that overlap other in the tree."""
        iset = self._iset
        l = binsearch_left_start(iset, other[0] - self._maxlen, 0, len(iset))
        r = binsearch_right_end(iset, other[1], 0, len(iset))
        iopts = iset[l:r]
        iiter = (s for s in iopts if s[0] <= other[1] and s[1] >= other[0])
        for o in iiter: yield o

    def closest(self, other):
        iset = self._iset
        l = binsearch_left_start(iset, other[0] - self._maxlen, 0, len(iset))
        r = binsearch_right_end(iset, other[1], 0, len(iset))
        l, r = max(0, l - 1), min(len(iset), r + 2)

        while r < len(iset) and iset[r - 1][0] == iset[r][0]:
            r += 1

        while l > 1 and iset[l][1] == iset[l + 1][1]:
            l -= 1
        iopts = iset[l:r]
        ovls = [s for s in iopts if s[0] <= other[1] and s[1] >= other[0]]
        if ovls:
            for o in ovls: yield o
        else:
            iopts = sorted([(min(abs(i[0] - other[1]), abs(other[0] - i[1])), i) for i in iopts])
            for dist, g in groupby(iopts, itemgetter(0)):
                # only yield the closest intervals
                for d, ival in g:
                    yield ival
                break

    def __contains__(self, other):
        """Indicate whether `other` overlaps any elements in the tree."""
        iset = self._iset
        l = binsearch_left_start(iset, other[0] - self._maxlen, 0, len(iset))
        # since often the found interval will overlap, we short cut that
        # case of speed.
        max_search = 8
        if l == len(iset): return False
        for left in iset[l:l + max_search]:
            if left[1] >= other[0] and left[0] <= other[1]: return True
            if left[0] > other[1]: return False

        r = binsearch_right_end(iset, other[1], 0, len(iset))
        return any(s[0] <= other[1] and s[1] >= other[0]
                   for s in iset[l + max_search:r])

    def __iter__(self):
        return iter(self._iset)

if __name__ == "__main__":
    import time
    t0 = time.time()
    import doctest
    print(doctest.testmod(verbose=0,
          optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
    print(time.time() - t0)

<commit_msg>bump version<commit_after>"""
`InterLap` does fast interval overlap testing with a simple python data
structure.

It works well on the types of querying done in genomic datasets where we have
10's of thousands of intervals and we check for overlap millions of times. It
is very simple and has no dependencies.

It takes tuples or lists where the first 2 elements are start, end and the
remaining elements can be anything.

>>> from interlap import InterLap
>>> inter = InterLap()

Here create 10K random intervals:

>>> import random
>>> sites = random.sample(range(22, 100000000, 12), 50000)
>>> ranges = [(i, i + random.randint(2000, 20000)) for i in sites]

Add them to the interval tree (this takes < 0.5 seconds):

>>> inter.update(ranges)

We can also add one at a time:

>>> inter.add((20, 22, {'info': 'hi'}))

Now do overlap testing:

>>> [20, 21] in inter
True

>>> next(inter.find((21, 21)))
(20, 22, {'info': 'hi'})

>>> inter.add((2, 3, {'info': 'hello'}))

*NOTE*: below shows how edge-cases are handled.

>>> list(inter.find((2, 2)))
[(2, 3, {'info': 'hello'})]
>>> list(inter.find((3, 3)))
[(2, 3, {'info': 'hello'})]

Test every item in the InterLap. These 50K queries take < 0.5 seconds:

>>> for s, e in ranges:
...     assert (s, e) in inter

>>> for i, se in enumerate(inter):
...     if i > 10: break
...     assert se[0] < se[1]
...

>>> list(inter.closest((2, 2)))
[(2, 3, {'info': 'hello'})]

>>> list(inter.closest((2, 21)))
[(2, 3, {'info': 'hello'}), (20, 22, {'info': 'hi'})]

>>> list(inter.closest((2, 21)))
[(2, 3, {'info': 'hello'}), (20, 22, {'info': 'hi'})]

>>> list(inter.closest((11, 12)))
[(2, 3, {'info': 'hello'}), (20, 22, {'info': 'hi'})]

>>> for i in range(10):
...     inter.add((20, 21))
>>> len(list(inter.closest((10, 13))))
12

>>> for i in range(10):
...     inter.add((18, 21))
>>> len(list(inter.closest((10, 13))))
10

>>> for i in range(10):
...     inter.add((5, 5))
>>> len(list(inter.closest((10, 13))))
20

>>> for i in range(10):
...     inter.add((3, 5))
>>> len(list(inter.closest((10, 13))))
30

>>> inter.add((9, 9))
>>> list(inter.closest((10, 13)))
[(9, 9)]

"""
from itertools import groupby
from operator import itemgetter

__all__ = ['InterLap']

__version__ = '0.1.3'

try:
    int_types = (int, long)
except NameError:
    int_types = (int,)

######################################
def binsearch_left_start(intervals, x, lo, hi):
    while lo < hi:
        mid = (lo + hi)//2
        f = intervals[mid]
        if f[0] < x: lo = mid + 1
        else: hi = mid
    return lo

# like python's bisect_right find the _highest_ index where the value x
# could be inserted to maintain order in the list intervals
def binsearch_right_end(intervals, x, lo, hi):
    while lo < hi:
        mid = (lo + hi)//2
        f = intervals[mid]
        if x < f[0]: hi = mid
        else: lo = mid + 1
    return lo

class InterLap(object):

    """Create an Interlap object. (See module docstring)."""

    def __init__(self, ranges=()):
        """The ranges are tuples of (start, end, *whatever)."""
        self._iset = sorted(ranges)
        self._maxlen = max(r[1] - r[0] for r in (ranges or [[0, 0]]))

    def add(self, ranges):
        r"""Add a single (or many) [start, end, \*] item to the tree."""
        if len(ranges) and isinstance(ranges[0], int_types):
            ranges = [ranges]
        iset = self._iset
        self._maxlen = max(self._maxlen, max(r[1] - r[0] + 1 for r in ranges))

        if len(ranges) > 30 or len(iset) < len(ranges):
            iset.extend(ranges)
            iset.sort()
        else:
            for o in ranges:
                iset.insert(binsearch_left_start(iset, o[0], 0, len(iset)), o)

    update = add

    def __len__(self):
        """Return number of intervals."""
        return len(self._iset)

    def find(self, other):
        """Return an interable of elements that overlap other in the tree."""
        iset = self._iset
        l = binsearch_left_start(iset, other[0] - self._maxlen, 0, len(iset))
        r = binsearch_right_end(iset, other[1], 0, len(iset))
        iopts = iset[l:r]
        iiter = (s for s in iopts if s[0] <= other[1] and s[1] >= other[0])
        for o in iiter: yield o

    def closest(self, other):
        iset = self._iset
        l = binsearch_left_start(iset, other[0] - self._maxlen, 0, len(iset))
        r = binsearch_right_end(iset, other[1], 0, len(iset))
        l, r = max(0, l - 1), min(len(iset), r + 2)

        while r < len(iset) and iset[r - 1][0] == iset[r][0]:
            r += 1

        while l > 1 and iset[l][1] == iset[l + 1][1]:
            l -= 1
        iopts = iset[l:r]
        ovls = [s for s in iopts if s[0] <= other[1] and s[1] >= other[0]]
        if ovls:
            for o in ovls: yield o
        else:
            iopts = sorted([(min(abs(i[0] - other[1]), abs(other[0] - i[1])), i) for i in iopts])
            for dist, g in groupby(iopts, itemgetter(0)):
                # only yield the closest intervals
                for d, ival in g:
                    yield ival
                break

    def __contains__(self, other):
        """Indicate whether `other` overlaps any elements in the tree."""
        iset = self._iset
        l = binsearch_left_start(iset, other[0] - self._maxlen, 0, len(iset))
        # since often the found interval will overlap, we short cut that
        # case of speed.
        max_search = 8
        if l == len(iset): return False
        for left in iset[l:l + max_search]:
            if left[1] >= other[0] and left[0] <= other[1]: return True
            if left[0] > other[1]: return False

        r = binsearch_right_end(iset, other[1], 0, len(iset))
        return any(s[0] <= other[1] and s[1] >= other[0]
                   for s in iset[l + max_search:r])

    def __iter__(self):
        return iter(self._iset)

if __name__ == "__main__":
    import time
    t0 = time.time()
    import doctest
    print(doctest.testmod(verbose=0,
          optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
    print(time.time() - t0)

<|endoftext|>


<commit_before>"""
Some Resources used by passthru.
"""
from twisted.internet import reactor, defer
from twisted.web import server, resource
import json
import pprint
import os

from twisted.web.client import Agent
from treq.client import HTTPClient
import treq

BASE_URL = os.environ.get("FLOCKER_CONTROL_SERVICE_BASE_URL")

class AdapterResource(resource.Resource):
    """
    A powerstrip adapter which integrates Docker with Flocker for portable
    volumes.
    """
    isLeaf = True

    def __init__(self, *args, **kw):
        self.agent = Agent(reactor) # no connectionpool
        self.client = HTTPClient(self.agent)
        return resource.Resource.__init__(self, *args, **kw)

    def render_POST(self, request):
        """
        Handle a pre-hook: either create a filesystem, or move it in place.
        """
        requestJson = json.loads(request.content.read())
        if requestJson["Type"] != "pre-hook":
            raise Exception("unsupported hook type %s" %
                (requestJson["Type"],))

        pprint.pprint(os.environ)
        # BASE_URL like http://control-service/v1/ ^
        jsonPayload = requestJson["ClientRequest"]["Body"]
        jsonParsed = json.loads(jsonPayload)

        self.baseURL = os.environ.get("FLOCKER_CONTROL_SERVICE_BASE_URL")
        self.ip = os.environ.get("MY_NETWORK_IDENTITY")

        # simplest possible implementation: always create a volume.
        fsCreateDeferreds = []
        if jsonParsed['HostConfig']['Binds'] is not None:
            # newBinds = []
            for bind in jsonParsed['HostConfig']['Binds']:
                host_path, remainder = bind.split(":", 1)
                if host_path.startswith("/flocker/"):
                    fs = host_path[len("/flocker/"):]
                    # new_host_path = "/hcfs/%s" % (fs,)
                    print "POST", self.baseURL + "/configuration/datasets",
                    print json.dumps({"primary": self.ip, "metadata": {"name": fs}})
                    d = self.client.post(self.baseURL + "/configuration/datasets",
                            json.dumps({"primary": self.ip, "metadata": {"name": fs}}))
                    d.addCallback(treq.json_content)
                    fsCreateDeferreds.append(d)
                    # newBinds.append("%s:%s" % (new_host_path, remainder))
            # xxx
        d = defer.gatherResults(fsCreateDeferreds)
        def gotCreatedDatasets(listNewDatasets):
            # TODO: poll /v1/state/datasets until the dataset appears
            print "<" * 80
            pprint.pprint(listNewDatasets)
            print "<" * 80
            request.write(json.dumps({
                "PowerstripProtocolVersion": 1,
                "ModifiedClientRequest": {
                    "Method": "POST",
                    "Request": request.uri,
                    "Body": json.dumps(jsonParsed)}}))
            request.finish()
        d.addCallback(gotCreatedDatasets)
        """
        gettingFilesystemsInPlace = []
        [...]
                    # TODO validation
                    if "/" in fs:
                        raise Exception("Not allowed flocker filesystems more than one level deep")
                    if fs not in self.sitejuggler.currentMasters:
                        # XXX could also require that the filesystem has been
                        # pre-created by e.g. some admin tool
                        d = self.sitejuggler.createFilesystem(fs)
                    else:
                        d = defer.succeed(None)
                    # try to move the filesystem here.
                    # TODO: and lock it, blow up with an error if this fails <- do this next
                    # (lease is already claimed somewhere else, etc). then
                    # don't pass through to docker.
                    def doneCreate(ignored):
                        # fs should be in currentMaster, because if it didn't,
                        # we just created it
                        print ("dockerapi", ">> currentMasters after create",
                                repr(self.sitejuggler.currentMasters[fs]))
                        if self.sitejuggler.currentMasters[fs] != self.sitejuggler.ip:
                            return self.sitejuggler.emit_move_site_to_server(
                                    dataset=fs, ip=self.sitejuggler.ip, forceMove=0)
                    d.addCallback(doneCreate)
                    def checkInPlace(ignored):
                        # Sanity check that the volume is now in place on this host.
                        if self.sitejuggler.currentMasters[fs] != self.sitejuggler.ip:
                            raise Exception("failed to migrate volume into position")
                    d.addCallback(checkInPlace)
                    gettingFilesystemsInPlace.append(d)
            newJsonParsed['HostConfig']['Binds'] = newBinds
        # outside the loop now; XXX check that gatherResults fails on any error
        print 'getting filesystems in place:', gettingFilesystemsInPlace
        dlist = defer.gatherResults(gettingFilesystemsInPlace)
        def doneMoves(ignored):
            request.write(json.dumps({
                "PowerstripProtocolVersion": 1,
                "ModifiedClientRequest": {
                    "Method": "POST",
                    "Request": request.uri,
                    "Body": json.dumps(newJson)}}))
            request.finish()
        dlist.addCallback(doneMoves)
        """
        return server.NOT_DONE_YET
<commit_msg>specify content-type<commit_after>"""
Some Resources used by passthru.
"""
from twisted.internet import reactor, defer
from twisted.web import server, resource
import json
import pprint
import os

from twisted.web.client import Agent
from treq.client import HTTPClient
import treq

BASE_URL = os.environ.get("FLOCKER_CONTROL_SERVICE_BASE_URL")

class AdapterResource(resource.Resource):
    """
    A powerstrip adapter which integrates Docker with Flocker for portable
    volumes.
    """
    isLeaf = True

    def __init__(self, *args, **kw):
        self.agent = Agent(reactor) # no connectionpool
        self.client = HTTPClient(self.agent)
        return resource.Resource.__init__(self, *args, **kw)

    def render_POST(self, request):
        """
        Handle a pre-hook: either create a filesystem, or move it in place.
        """
        requestJson = json.loads(request.content.read())
        if requestJson["Type"] != "pre-hook":
            raise Exception("unsupported hook type %s" %
                (requestJson["Type"],))

        pprint.pprint(os.environ)
        # BASE_URL like http://control-service/v1/ ^
        jsonPayload = requestJson["ClientRequest"]["Body"]
        jsonParsed = json.loads(jsonPayload)

        self.baseURL = os.environ.get("FLOCKER_CONTROL_SERVICE_BASE_URL")
        self.ip = os.environ.get("MY_NETWORK_IDENTITY")

        # simplest possible implementation: always create a volume.
        fsCreateDeferreds = []
        if jsonParsed['HostConfig']['Binds'] is not None:
            # newBinds = []
            for bind in jsonParsed['HostConfig']['Binds']:
                host_path, remainder = bind.split(":", 1)
                if host_path.startswith("/flocker/"):
                    fs = host_path[len("/flocker/"):]
                    # new_host_path = "/hcfs/%s" % (fs,)
                    print "POST", self.baseURL + "/configuration/datasets",
                    print json.dumps({"primary": self.ip, "metadata": {"name": fs}})
                    d = self.client.post(self.baseURL + "/configuration/datasets",
                            json.dumps({"primary": self.ip, "metadata": {"name": fs}}),
                            headers={'Content-Type': ['application/json']})
                    d.addCallback(treq.json_content)
                    fsCreateDeferreds.append(d)
                    # newBinds.append("%s:%s" % (new_host_path, remainder))
            # xxx
        d = defer.gatherResults(fsCreateDeferreds)
        def gotCreatedDatasets(listNewDatasets):
            # TODO: poll /v1/state/datasets until the dataset appears
            print "<" * 80
            pprint.pprint(listNewDatasets)
            print "<" * 80
            request.write(json.dumps({
                "PowerstripProtocolVersion": 1,
                "ModifiedClientRequest": {
                    "Method": "POST",
                    "Request": request.uri,
                    "Body": json.dumps(jsonParsed)}}))
            request.finish()
        d.addCallback(gotCreatedDatasets)
        """
        gettingFilesystemsInPlace = []
        [...]
                    # TODO validation
                    if "/" in fs:
                        raise Exception("Not allowed flocker filesystems more than one level deep")
                    if fs not in self.sitejuggler.currentMasters:
                        # XXX could also require that the filesystem has been
                        # pre-created by e.g. some admin tool
                        d = self.sitejuggler.createFilesystem(fs)
                    else:
                        d = defer.succeed(None)
                    # try to move the filesystem here.
                    # TODO: and lock it, blow up with an error if this fails <- do this next
                    # (lease is already claimed somewhere else, etc). then
                    # don't pass through to docker.
                    def doneCreate(ignored):
                        # fs should be in currentMaster, because if it didn't,
                        # we just created it
                        print ("dockerapi", ">> currentMasters after create",
                                repr(self.sitejuggler.currentMasters[fs]))
                        if self.sitejuggler.currentMasters[fs] != self.sitejuggler.ip:
                            return self.sitejuggler.emit_move_site_to_server(
                                    dataset=fs, ip=self.sitejuggler.ip, forceMove=0)
                    d.addCallback(doneCreate)
                    def checkInPlace(ignored):
                        # Sanity check that the volume is now in place on this host.
                        if self.sitejuggler.currentMasters[fs] != self.sitejuggler.ip:
                            raise Exception("failed to migrate volume into position")
                    d.addCallback(checkInPlace)
                    gettingFilesystemsInPlace.append(d)
            newJsonParsed['HostConfig']['Binds'] = newBinds
        # outside the loop now; XXX check that gatherResults fails on any error
        print 'getting filesystems in place:', gettingFilesystemsInPlace
        dlist = defer.gatherResults(gettingFilesystemsInPlace)
        def doneMoves(ignored):
            request.write(json.dumps({
                "PowerstripProtocolVersion": 1,
                "ModifiedClientRequest": {
                    "Method": "POST",
                    "Request": request.uri,
                    "Body": json.dumps(newJson)}}))
            request.finish()
        dlist.addCallback(doneMoves)
        """
        return server.NOT_DONE_YET
<|endoftext|>