<commit_before>import logging
import testresources
import testtools
import fixtures
import unittest2
import itertools
import traceback
import os
import sys
from threading import Thread
import subprocess

__all__ = [
    'ReportingTestResourceManager',
    'ErrorTolerantOptimisedTestSuite',
    'DetailCollector',
]

class RoundRobinList(list):

    def __init__(self, num):
        for i in range(num):
            self.append([])
        self._iter = itertools.cycle(self)

    def extend(self, list):
        self._iter.next().extend(list)

class DetailCollector(object):

    def __init__(self, TRM, result, appendix):
        self.TRM = TRM
        self.appendix = appendix
        if not isinstance(result, testtools.TestResult):
            raise Exception(
                "%s is not a testtools.TestResult" % result.__class__)
        self.result = result

    def __enter__(self):
        FORMAT = '%(asctime)s [%(levelname)s] %(name)s %(lineno)d: %(message)s'  # noqa
        self.log_fixture = fixtures.FakeLogger(format=FORMAT)
        self.log_fixture.setUp()
        self.TRM.appendix = self.appendix
        self.result.startFixture(self.TRM)

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_val:
            self.log_fixture.addDetail(
                'traceback', testtools.content.TracebackContent(
                    (exc_type, exc_val, exc_tb), self.TRM))
            self.result.addWarning(
                str(self.TRM), details=self.log_fixture.getDetails())
        else:
            self.result.addInfo(str(self.TRM))
        self.result.stopFixture(self.TRM)
        self.log_fixture.cleanUp()
        self.TRM.appendix = ''

class ReportingTestResourceManager(testresources.TestResourceManager):
    """Fix some problems where testresources.TestResourceManager doesn't
    actually sent the result all the way through the process.

    Use context manager to report results of finishedWith and getResource
    to the result."""
    collector_class = DetailCollector

    def __init__(self, level=logging.INFO):
        super(ReportingTestResourceManager, self).__init__()
        self.log_level = level
        self.logger = logging.getLogger(self.__class__.__name__)
        self.appendix = ''
        self.worker = os.getenv('WRT_WORKER_ID', None)

    def __str__(self):
        return self.appendix + ' ' + self.__class__.__name__

    # fix _make_all, _clean_all, and isDirty to use result and
    # inject the context manager to manage results
    def _make_all(self, result):
        """Make the dependencies of this resource and this resource."""
        dependency_resources = {}
        for name, resource in self.resources:
            dependency_resources[name] = resource.getResource(result)
        with self.collector_class(self, result, 'Creating'):
            resource = self.make(dependency_resources)
        for name, value in dependency_resources.items():
            setattr(resource, name, value)
        return resource

    def _clean_all(self, resource, result):
        """Clean the dependencies from resource, and then resource itself."""
        # with DetailCollector(self, result, '-clean-all'):
        with self.collector_class(self, result, 'Destroying'):
            self.clean(resource)
        for name, manager in self.resources:
            manager.finishedWith(getattr(resource, name), result)

    def reset(self, old_resource, result=None):
        """Core logic stays the same as in parent class, just
        wrap _reset in DetailCollector. We can get rid of signalling the
        result because the DetailCollector does that for us.
        """
        # Core logic:
        #  - if neither we nor any parent is dirty, do nothing.
        # otherwise
        #  - emit a signal to the test result
        #  - reset all dependencies all, getting new attributes.
        #  - call self._reset(old_resource, dependency_attributes)
        #    [the default implementation does a clean + make]
        if not self.isDirty():
            return old_resource
        dependency_resources = {}
        for name, mgr in self.resources:
            dependency_resources[name] = mgr.reset(
                getattr(old_resource, name), result)
        with self.collector_class(self, result, 'Resetting'):
            resource = self._reset(old_resource, dependency_resources)
        for name, value in dependency_resources.items():
            setattr(resource, name, value)
        return resource

    def isDirty(self, result=None):
        """Return True if this managers cached resource is dirty.

        Calling when the resource is not currently held has undefined
        behaviour.
        """
        if self._dirty:
            return True
        for name, mgr in self.resources:
            if mgr.isDirty(result):
                return True
            res = mgr.getResource(result)
            try:
                if res is not getattr(self._currentResource, name):
                    return True
            finally:
                mgr.finishedWith(res, result)

class ParallelSuite(unittest2.TestSuite):

    def __init__(self, tests, worker, testNames, debug=False):
        self._tests = tests
        from loader import AutoDiscoveringTestLoader
        self.worker = worker
        self.testNames = testNames
        self.debug = debug

    def run(self, result):
        if not self._tests:
            return result

        # write the tests out to a file so the command line won't be too long
        with open('.worker%s' % self.worker, 'wb') as f:
            f.write('\n'.join(self._tests))

        # build command -
        # don't pipe stderr to stdout, or the dots won't be visible in real-time
        command = [
            'WRT_WORKER_ID=%s' % self.worker,
            result.progName,
        ]
        if result.dots:
            command.append('--dots')
        elif result.early_details:
            command.append('--early-details')
        elif result.showAll:
            command.append('-v')
        else:
            command.append('-q')
        if result.color:
            command.append('--color')
        # TODO: send run url to worker
        command.append('--from-file .worker%s' % self.worker)
        command.append(' '.join(self.testNames))
        command = ' '.join(command)
        if self.debug:
            result.stream.writeln(command)

        try:
            # output will contain dumped json of the worker's results
            output = subprocess.check_output(command, shell=True)
        except subprocess.CalledProcessError as e:
            output = e.output
        if self.debug:
            result.stream.writeln(output)
        result.absorbResult(output)

class ErrorTolerantOptimisedTestSuite(testresources.OptimisingTestSuite, unittest2.TestSuite):
    # TODO: --update-last-wrt-run
    # TODO: abort suite if running too long
    # TODO: implement create-or-fetch of well-rested-tests run
    # TODO: --parallel
    # TODO: notice KeyboardInterupt and replace .failing.bak
    """
    Catch errors thrown by resource creation / destruction,
    fail the affected test, and keep going.
    This requires dynamic tracking of the current resources
    """

    current_resources = set()
    list_tests = False
    debug = False
    parallel = False
    concurrency = 2

    @staticmethod
    def parserOptions(parser):
        group = parser.add_argument_group('ErrorTolerantOptimisedTestSuite')
        group.add_argument('--list', dest='list_tests',
                           action='store_true',
                           help='Output list of tests, then exist.')
        group.add_argument('--reverse', dest='reverse',
                           action='store_true',
                           help='Reverse order of tests.')
        group.add_argument('--debug', dest='debug',
                           action='store_true',
                           help='Debug the suite functionality.')
        group.add_argument('--parallel', dest='parallel',
                           action='store_true',
                           help='Run tests in parallel (up to --concurrency threads).')
        group.add_argument('--concurrency', dest='concurrency', default=2,
                           help='Number of parallel threads (default 2).')
        return parser

    @staticmethod
    def expectedHelpText(cls):
        return """
%s:
  --list                Output list of tests, then exist.
  --reverse             Reverse order of tests.
  --debug               Debug the suite functionality.
  --parallel            Run tests in parallel (up to --concurrency threads).
  --concurrency CONCURRENCY
                        Number of parallel threads (default 2).
""" % cls.__name__

    @staticmethod
    def factory(cls, object):
        return cls()

    def __init__(self, tests):
        super(ErrorTolerantOptimisedTestSuite, self).__init__(tests)
        self.list_tests = False
        self.debug = False
        self.reverse = False
        self.worker = os.getenv('WRT_WORKER_ID', None)

    def switch(self, new_resource_set, result):
        """Switch from self.current_resources to 'new_resource_set'.

        Tear down resources in self.current_resources that aren't in
        new_resource_set and set up resources that are in new_resource_set but
        not in self.current_resources.

        :param result: TestResult object to report activity on.
        """
        new_resources = new_resource_set - self.current_resources
        old_resources = self.current_resources - new_resource_set
        for resource in old_resources:
            resource.finishedWith(resource._currentResource, result)
            self.current_resources.discard(resource)
        for resource in new_resources:
            resource.getResource(result)
            self.current_resources.add(resource)

    def list(self):
        return [test.id() for test in self._tests]

    def run(self, result):
        self.sortTests()  # will sub-divide for parallelization and list if parallel
        if self.reverse:
            self._tests.reverse()
        if self.list_tests:
            for test in self.list():
                result.stream.writeln(test)
            exit(0)
        elif self.parallel:
            threads = [Thread(
                target=suite.run,
                args=(result,))
                for suite in self._tests]
            map(lambda t: t.start(), threads)
            map(lambda t: t.join(), threads)
        else:
            if result.wrt_client:
                result.wrt_client.registerTests(self._tests)
            for test in self._tests:
                if result.shouldStop:
                    break
                resources = getattr(test, 'resources', [])
                new_resources = set()
                for name, resource in resources:
                    new_resources.update(resource.neededResources())
                try:
                    self.switch(new_resources, result)
                except Exception:
                    if self.debug:
                        result.stream.writeln(traceback.format_exc())
                    # the exception has been reported on the fixture,
                    # but we still want to report failed for the test itself
                    # so that it will show up in --failing
                    result.startTest(test)
                    result.addError(test, details={
                        'reason': testtools.content.text_content(
                            'Error handling fixtures')})
                    result.stopTest(test)
                    continue  # next test
                test(result)
            try:
                self.switch(set(), result)
            except Exception:
                # this exception has already been reported, ignore it.
                pass
        return result

    def addTest(self, test_case_or_suite):
        """Add `test_case_or_suite`, unwrapping standard TestSuites.

        This means that any containing unittest.TestSuites will be removed,
        while any custom test suites will be 'distributed' across their
        members. Thus addTest(CustomSuite([a, b])) will result in
        CustomSuite([a]) and CustomSuite([b]) being added to this suite.
        """
        try:
            tests = iter(test_case_or_suite)
        except TypeError:
            unittest2.TestSuite.addTest(self, test_case_or_suite)
            return
        if test_case_or_suite.__class__ in (unittest2.TestSuite,
                                            testresources.OptimisingTestSuite,
                                            self.__class__):
            for test in tests:
                self.addTest(test)
        else:
            for test in tests:
                unittest2.TestSuite.addTest(
                    self, test_case_or_suite.__class__([test]))

    def filter_by_ids(suite, test_ids):
        """
        Used by loader when --failing is set.
        Will ignore any fixture names that are in the failing_file.
        Will ignore any previously failing tests not also discovered from the testNames
        """
        filtered = []
        for test in suite._tests:
            if test.id() in test_ids:
                filtered.append(test)
        suite._tests = filtered
        return suite

    def sortTests(self):
        """Attempt to topographically sort the contained tests.

        This function biases to reusing a resource: it assumes that resetting
        a resource is usually cheaper than a teardown + setup; and that most
        resources are not dirtied by most tests.

        Feel free to override to improve the sort behaviour.
        """
        # We group the tests by the resource combinations they use,
        # since there will usually be fewer resource combinations than
        # actual tests and there can never be more: This gives us 'nodes' or
        # 'resource_sets' that represent many tests using the same set of
        # resources.
        resource_set_tests = testresources.split_by_resources(self._tests)
        # Partition into separate sets of resources, there is no ordering
        # preference between sets that do not share members. Rationale:
        # If resource_set A and B have no common resources, AB and BA are
        # equally good - the global setup/teardown sums are identical. Secondly
        # if A shares one or more resources with C, then pairing AC|CA is
        # better than having B between A and C, because the shared resources
        # can be reset or reused. Having partitioned we can use connected graph
        # logic on each partition.
        resource_set_graph = testresources._resource_graph(resource_set_tests)
        no_resources = frozenset()
        # A list of resource_set_tests, all fully internally connected.
        partitions = testresources._strongly_connected_components(
            resource_set_graph, no_resources)

        if self.parallel:
            result = RoundRobinList(self.concurrency)
        else:
            result = []

        for partition in partitions:
            # we process these at the end for no particularly good reason (it
            # makes testing slightly easier).
            if partition == [no_resources]:
                continue
            order = self._makeOrder(partition)
            # Spit this partition out into result
            for resource_set in order:
                result.extend(resource_set_tests[resource_set])
        result.extend(resource_set_tests[no_resources])

        if self.parallel:
            # sys.stderr.write('%s\n\n' % self._tests)
            self._tests = []
            for worker, batch in enumerate(result):
                worker += 1
                if self.reverse:
                    batch.reverse()
                tests = [test.id() for test in batch]
                if self.list_tests:
                    sys.stderr.write('WORKER %s:\n' % worker)
                    sys.stderr.write('    ')
                    sys.stderr.write('\n    '.join(tests))
                    sys.stderr.write('\n')
                self._tests.append(ParallelSuite(tests, worker, self.testNames, debug=self.debug))
            if self.list_tests:
                exit(0)
        else:
            self._tests = result
<commit_msg>fix --timestamp for --parallel. think about how to avoid these issues in the future<commit_after>import logging
import testresources
import testtools
import fixtures
import unittest2
import itertools
import traceback
import os
import sys
from threading import Thread
import subprocess

__all__ = [
    'ReportingTestResourceManager',
    'ErrorTolerantOptimisedTestSuite',
    'DetailCollector',
]

class RoundRobinList(list):

    def __init__(self, num):
        for i in range(num):
            self.append([])
        self._iter = itertools.cycle(self)

    def extend(self, list):
        self._iter.next().extend(list)

class DetailCollector(object):

    def __init__(self, TRM, result, appendix):
        self.TRM = TRM
        self.appendix = appendix
        if not isinstance(result, testtools.TestResult):
            raise Exception(
                "%s is not a testtools.TestResult" % result.__class__)
        self.result = result

    def __enter__(self):
        FORMAT = '%(asctime)s [%(levelname)s] %(name)s %(lineno)d: %(message)s'  # noqa
        self.log_fixture = fixtures.FakeLogger(format=FORMAT)
        self.log_fixture.setUp()
        self.TRM.appendix = self.appendix
        self.result.startFixture(self.TRM)

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_val:
            self.log_fixture.addDetail(
                'traceback', testtools.content.TracebackContent(
                    (exc_type, exc_val, exc_tb), self.TRM))
            self.result.addWarning(
                str(self.TRM), details=self.log_fixture.getDetails())
        else:
            self.result.addInfo(str(self.TRM))
        self.result.stopFixture(self.TRM)
        self.log_fixture.cleanUp()
        self.TRM.appendix = ''

class ReportingTestResourceManager(testresources.TestResourceManager):
    """Fix some problems where testresources.TestResourceManager doesn't
    actually sent the result all the way through the process.

    Use context manager to report results of finishedWith and getResource
    to the result."""
    collector_class = DetailCollector

    def __init__(self, level=logging.INFO):
        super(ReportingTestResourceManager, self).__init__()
        self.log_level = level
        self.logger = logging.getLogger(self.__class__.__name__)
        self.appendix = ''
        self.worker = os.getenv('WRT_WORKER_ID', None)

    def __str__(self):
        return self.appendix + ' ' + self.__class__.__name__

    # fix _make_all, _clean_all, and isDirty to use result and
    # inject the context manager to manage results
    def _make_all(self, result):
        """Make the dependencies of this resource and this resource."""
        dependency_resources = {}
        for name, resource in self.resources:
            dependency_resources[name] = resource.getResource(result)
        with self.collector_class(self, result, 'Creating'):
            resource = self.make(dependency_resources)
        for name, value in dependency_resources.items():
            setattr(resource, name, value)
        return resource

    def _clean_all(self, resource, result):
        """Clean the dependencies from resource, and then resource itself."""
        # with DetailCollector(self, result, '-clean-all'):
        with self.collector_class(self, result, 'Destroying'):
            self.clean(resource)
        for name, manager in self.resources:
            manager.finishedWith(getattr(resource, name), result)

    def reset(self, old_resource, result=None):
        """Core logic stays the same as in parent class, just
        wrap _reset in DetailCollector. We can get rid of signalling the
        result because the DetailCollector does that for us.
        """
        # Core logic:
        #  - if neither we nor any parent is dirty, do nothing.
        # otherwise
        #  - emit a signal to the test result
        #  - reset all dependencies all, getting new attributes.
        #  - call self._reset(old_resource, dependency_attributes)
        #    [the default implementation does a clean + make]
        if not self.isDirty():
            return old_resource
        dependency_resources = {}
        for name, mgr in self.resources:
            dependency_resources[name] = mgr.reset(
                getattr(old_resource, name), result)
        with self.collector_class(self, result, 'Resetting'):
            resource = self._reset(old_resource, dependency_resources)
        for name, value in dependency_resources.items():
            setattr(resource, name, value)
        return resource

    def isDirty(self, result=None):
        """Return True if this managers cached resource is dirty.

        Calling when the resource is not currently held has undefined
        behaviour.
        """
        if self._dirty:
            return True
        for name, mgr in self.resources:
            if mgr.isDirty(result):
                return True
            res = mgr.getResource(result)
            try:
                if res is not getattr(self._currentResource, name):
                    return True
            finally:
                mgr.finishedWith(res, result)

class ParallelSuite(unittest2.TestSuite):

    def __init__(self, tests, worker, testNames, debug=False):
        self._tests = tests
        from loader import AutoDiscoveringTestLoader
        self.worker = worker
        self.testNames = testNames
        self.debug = debug

    def run(self, result):
        if not self._tests:
            return result

        # write the tests out to a file so the command line won't be too long
        with open('.worker%s' % self.worker, 'wb') as f:
            f.write('\n'.join(self._tests))

        # build command -
        # don't pipe stderr to stdout, or the dots won't be visible in real-time
        command = [
            'WRT_WORKER_ID=%s' % self.worker,
            result.progName,
        ]
        if result.dots:
            command.append('--dots')
        elif result.early_details:
            command.append('--early-details')
        elif result.showAll:
            command.append('-v')
        else:
            command.append('-q')
        if result.color:
            command.append('--color')
        if result.timestamp:
            command.append('--timestamp')
        # TODO: send run url to worker
        command.append('--from-file .worker%s' % self.worker)
        command.append(' '.join(self.testNames))
        command = ' '.join(command)
        if self.debug:
            result.stream.writeln(command)

        try:
            # output will contain dumped json of the worker's results
            output = subprocess.check_output(command, shell=True)
        except subprocess.CalledProcessError as e:
            output = e.output
        if self.debug:
            result.stream.writeln(output)
        result.absorbResult(output)

class ErrorTolerantOptimisedTestSuite(testresources.OptimisingTestSuite, unittest2.TestSuite):
    # TODO: --update-last-wrt-run
    # TODO: abort suite if running too long
    # TODO: implement create-or-fetch of well-rested-tests run
    # TODO: --parallel
    # TODO: notice KeyboardInterupt and replace .failing.bak
    """
    Catch errors thrown by resource creation / destruction,
    fail the affected test, and keep going.
    This requires dynamic tracking of the current resources
    """

    current_resources = set()
    list_tests = False
    debug = False
    parallel = False
    concurrency = 2

    @staticmethod
    def parserOptions(parser):
        group = parser.add_argument_group('ErrorTolerantOptimisedTestSuite')
        group.add_argument('--list', dest='list_tests',
                           action='store_true',
                           help='Output list of tests, then exist.')
        group.add_argument('--reverse', dest='reverse',
                           action='store_true',
                           help='Reverse order of tests.')
        group.add_argument('--debug', dest='debug',
                           action='store_true',
                           help='Debug the suite functionality.')
        group.add_argument('--parallel', dest='parallel',
                           action='store_true',
                           help='Run tests in parallel (up to --concurrency threads).')
        group.add_argument('--concurrency', dest='concurrency', default=2,
                           help='Number of parallel threads (default 2).')
        return parser

    @staticmethod
    def expectedHelpText(cls):
        return """
%s:
  --list                Output list of tests, then exist.
  --reverse             Reverse order of tests.
  --debug               Debug the suite functionality.
  --parallel            Run tests in parallel (up to --concurrency threads).
  --concurrency CONCURRENCY
                        Number of parallel threads (default 2).
""" % cls.__name__

    @staticmethod
    def factory(cls, object):
        return cls()

    def __init__(self, tests):
        super(ErrorTolerantOptimisedTestSuite, self).__init__(tests)
        self.list_tests = False
        self.debug = False
        self.reverse = False
        self.worker = os.getenv('WRT_WORKER_ID', None)

    def switch(self, new_resource_set, result):
        """Switch from self.current_resources to 'new_resource_set'.

        Tear down resources in self.current_resources that aren't in
        new_resource_set and set up resources that are in new_resource_set but
        not in self.current_resources.

        :param result: TestResult object to report activity on.
        """
        new_resources = new_resource_set - self.current_resources
        old_resources = self.current_resources - new_resource_set
        for resource in old_resources:
            resource.finishedWith(resource._currentResource, result)
            self.current_resources.discard(resource)
        for resource in new_resources:
            resource.getResource(result)
            self.current_resources.add(resource)

    def list(self):
        return [test.id() for test in self._tests]

    def run(self, result):
        self.sortTests()  # will sub-divide for parallelization and list if parallel
        if self.reverse:
            self._tests.reverse()
        if self.list_tests:
            for test in self.list():
                result.stream.writeln(test)
            exit(0)
        elif self.parallel:
            threads = [Thread(
                target=suite.run,
                args=(result,))
                for suite in self._tests]
            map(lambda t: t.start(), threads)
            map(lambda t: t.join(), threads)
        else:
            if result.wrt_client:
                result.wrt_client.registerTests(self._tests)
            for test in self._tests:
                if result.shouldStop:
                    break
                resources = getattr(test, 'resources', [])
                new_resources = set()
                for name, resource in resources:
                    new_resources.update(resource.neededResources())
                try:
                    self.switch(new_resources, result)
                except Exception:
                    if self.debug:
                        result.stream.writeln(traceback.format_exc())
                    # the exception has been reported on the fixture,
                    # but we still want to report failed for the test itself
                    # so that it will show up in --failing
                    result.startTest(test)
                    result.addError(test, details={
                        'reason': testtools.content.text_content(
                            'Error handling fixtures')})
                    result.stopTest(test)
                    continue  # next test
                test(result)
            try:
                self.switch(set(), result)
            except Exception:
                # this exception has already been reported, ignore it.
                pass
        return result

    def addTest(self, test_case_or_suite):
        """Add `test_case_or_suite`, unwrapping standard TestSuites.

        This means that any containing unittest.TestSuites will be removed,
        while any custom test suites will be 'distributed' across their
        members. Thus addTest(CustomSuite([a, b])) will result in
        CustomSuite([a]) and CustomSuite([b]) being added to this suite.
        """
        try:
            tests = iter(test_case_or_suite)
        except TypeError:
            unittest2.TestSuite.addTest(self, test_case_or_suite)
            return
        if test_case_or_suite.__class__ in (unittest2.TestSuite,
                                            testresources.OptimisingTestSuite,
                                            self.__class__):
            for test in tests:
                self.addTest(test)
        else:
            for test in tests:
                unittest2.TestSuite.addTest(
                    self, test_case_or_suite.__class__([test]))

    def filter_by_ids(suite, test_ids):
        """
        Used by loader when --failing is set.
        Will ignore any fixture names that are in the failing_file.
        Will ignore any previously failing tests not also discovered from the testNames
        """
        filtered = []
        for test in suite._tests:
            if test.id() in test_ids:
                filtered.append(test)
        suite._tests = filtered
        return suite

    def sortTests(self):
        """Attempt to topographically sort the contained tests.

        This function biases to reusing a resource: it assumes that resetting
        a resource is usually cheaper than a teardown + setup; and that most
        resources are not dirtied by most tests.

        Feel free to override to improve the sort behaviour.
        """
        # We group the tests by the resource combinations they use,
        # since there will usually be fewer resource combinations than
        # actual tests and there can never be more: This gives us 'nodes' or
        # 'resource_sets' that represent many tests using the same set of
        # resources.
        resource_set_tests = testresources.split_by_resources(self._tests)
        # Partition into separate sets of resources, there is no ordering
        # preference between sets that do not share members. Rationale:
        # If resource_set A and B have no common resources, AB and BA are
        # equally good - the global setup/teardown sums are identical. Secondly
        # if A shares one or more resources with C, then pairing AC|CA is
        # better than having B between A and C, because the shared resources
        # can be reset or reused. Having partitioned we can use connected graph
        # logic on each partition.
        resource_set_graph = testresources._resource_graph(resource_set_tests)
        no_resources = frozenset()
        # A list of resource_set_tests, all fully internally connected.
        partitions = testresources._strongly_connected_components(
            resource_set_graph, no_resources)

        if self.parallel:
            result = RoundRobinList(self.concurrency)
        else:
            result = []

        for partition in partitions:
            # we process these at the end for no particularly good reason (it
            # makes testing slightly easier).
            if partition == [no_resources]:
                continue
            order = self._makeOrder(partition)
            # Spit this partition out into result
            for resource_set in order:
                result.extend(resource_set_tests[resource_set])
        result.extend(resource_set_tests[no_resources])

        if self.parallel:
            # sys.stderr.write('%s\n\n' % self._tests)
            self._tests = []
            for worker, batch in enumerate(result):
                worker += 1
                if self.reverse:
                    batch.reverse()
                tests = [test.id() for test in batch]
                if self.list_tests:
                    sys.stderr.write('WORKER %s:\n' % worker)
                    sys.stderr.write('    ')
                    sys.stderr.write('\n    '.join(tests))
                    sys.stderr.write('\n')
                self._tests.append(ParallelSuite(tests, worker, self.testNames, debug=self.debug))
            if self.list_tests:
                exit(0)
        else:
            self._tests = result
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import httplib
#import xml.dom.minidom as minidom
#import urllib
import logging
import requests
import time
import sys
import xml.etree.ElementTree as ET
from text.document import Document
"""
Get texts from PubMed
"""

class PubmedDocument(Document):
    def __init__(self, pmid, **kwargs):
        title, abstract, status = self.get_pubmed_abs(pmid)
        self.abstract = abstract
        super(PubmedDocument, self).__init__(title + "\n" + abstract, ssplit=True, title=title,
                                             did="PMID" + pmid, **kwargs)

    def get_pubmed_abs(self, pmid):
        logging.info("gettting {}".format(pmid))
        #conn = httplib.HTTPConnection("eutils.ncbi.nlm.nih.gov")
        #conn.request("GET", '/entrez/eutils/efetch.fcgi?db=pubmed&id={}&retmode=xml&rettype=xml'.format(pmid))
        payload = {"db": "pubmed", "id": pmid, "retmode": "xml", "rettype": "xml"}
        r = requests.get('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi', payload)
        logging.debug("Request Status: " + str(r.status_code))
        response = r.text

        # logging.info(response)
        title, abstract = self.parse_pubmed_xml(response)
        return title, abstract, str(r.status_code)

    def parse_pubmed_xml(self, xml):
        if xml.strip() == '':
            print "PMID not found"
            sys.exit()
        else:
            root = ET.fromstring(xml.encode("utf-8"))
            title = root.find('.//ArticleTitle').text
            abstext = root.findall('.//AbstractText')
            if len(abstext) > 0:
                abstext = abstext[0].text
            else:
                print "Abstract not found"
                abstext = ""
                #print xml
                #sys.exit()
        return title, abstext

def main():
    pubmeddoc = PubmedDocument(sys.argv[1])
    print pubmeddoc

if __name__ == "__main__":
    main()
    <commit_msg>Retrieve every section of the abstract<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import httplib
#import xml.dom.minidom as minidom
#import urllib
import logging
import requests
import time
import sys
import xml.etree.ElementTree as ET
from text.document import Document
"""
Get texts from PubMed
"""

class PubmedDocument(Document):
    def __init__(self, pmid, **kwargs):
        title, abstract, status = self.get_pubmed_abs(pmid)
        self.abstract = abstract
        super(PubmedDocument, self).__init__(title + "\n" + abstract, ssplit=True, title=title,
                                             did="PMID" + pmid, **kwargs)

    def get_pubmed_abs(self, pmid):
        logging.info("gettting {}".format(pmid))
        #conn = httplib.HTTPConnection("eutils.ncbi.nlm.nih.gov")
        #conn.request("GET", '/entrez/eutils/efetch.fcgi?db=pubmed&id={}&retmode=xml&rettype=xml'.format(pmid))
        payload = {"db": "pubmed", "id": pmid, "retmode": "xml", "rettype": "xml"}
        r = requests.get('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi', payload)
        logging.debug("Request Status: " + str(r.status_code))
        response = r.text

        # logging.info(response)
        title, abstract = self.parse_pubmed_xml(response)
        return title, abstract, str(r.status_code)

    def parse_pubmed_xml(self, xml):
        if xml.strip() == '':
            print "PMID not found"
            sys.exit()
        else:
            root = ET.fromstring(xml.encode("utf-8"))
            title = root.find('.//ArticleTitle').text
            abstext = root.findall('.//AbstractText')
            if len(abstext) > 0:
                abstext = [a.text for a in abstext]
                abstext = '\n'.join(abstext)
            else:
                print "Abstract not found"
                abstext = ""
                #print xml
                #sys.exit()
        return title, abstext

def main():
    pubmeddoc = PubmedDocument(sys.argv[1])
    print pubmeddoc

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


<commit_before>__author__ = 'Mikhail Vilgelm'
import pprint

def load(filename):

    simulation = {}

    f = open('in/'+filename)
    lines = f.readlines()
    f.close()

    i = 0
    while True:
        if i==0:
            line_data = lines[i].split(' ')
            simulation['rows'] = int(line_data[0])
            simulation['cols'] = int(line_data[1])
            simulation['drones'] = [{'coords': (float('nan'),float('nan')), 'load': 0}]*int(line_data[2])
            simulation['time_limit'] = int(line_data[3])
            simulation['max_payload'] = int(line_data[4])
            i+=1
            continue

        if i==1:
            simulation['product_types'] = [None]*int(lines[i])
            i+=1
            continue

        if i==2:
            simulation['product_types'] = list(map(int, lines[i].split(' ')))
            i+=1
            continue

        if i==3:
            simulation['warehouses'] = [None]*int(lines[i])
            i+=1
            continue

        if i==4:
            # read in every warehouse
            for j in range(len(simulation['warehouses'])):
                # read warehouse data
                wh = {'coords': tuple(map(int, lines[i].split(' ')))}
                wh['products'] = list(map(int, lines[i+1].split(' ')))
                # print(wh)
                simulation['warehouses'][j] = wh
                i+=2
            continue

        else:
            # read in every order
            simulation['orders'] = []
            number_of_orders = int(lines[i])
            i+=1
            for j in range(number_of_orders):
                # for every order
                order = {'coords': (tuple(map(int, lines[i].split(' '))))}
                order['products'] = list(map(int, lines[i+2].split(' ')))
                simulation['orders'].append(order)
                i+=3
            break

    for drone in simulation['drones']:
        drone['coords'] = simulation['warehouses'][0]['coords']

    return simulation

if __name__=='__main__':

    sim = input('busy_day.in')

    print(sim['drones'])
    print(len(sim['drones']))

    pp = pprint.PrettyPrinter(indent=2)
    # pp.pprint(sim['warehouses'])
    print(sim['warehouses'])
    print(sim['orders'])
    print(sim['drones'])
<commit_msg>load update<commit_after>__author__ = 'Mikhail Vilgelm'
# import pprint

def load(filename):

    simulation = {}

    f = open('in/'+filename)
    lines = f.readlines()
    f.close()

    i = 0
    while True:
        if i==0:
            line_data = lines[i].split(' ')
            simulation['rows'] = int(line_data[0])
            simulation['cols'] = int(line_data[1])
            simulation['drones'] = [{'coords': (float('nan'),float('nan')), 'load': 0}]*int(line_data[2])
            simulation['time_limit'] = int(line_data[3])
            simulation['max_payload'] = int(line_data[4])
            i+=1
            continue

        if i==1:
            simulation['product_types'] = [None]*int(lines[i])
            i+=1
            continue

        if i==2:
            simulation['product_types'] = list(map(int, lines[i].split(' ')))
            i+=1
            continue

        if i==3:
            simulation['warehouses'] = [None]*int(lines[i])
            i+=1
            continue

        if i==4:
            # read in every warehouse
            for j in range(len(simulation['warehouses'])):
                # read warehouse data
                wh = {'coords': tuple(map(int, lines[i].split(' ')))}
                wh['products'] = list(map(int, lines[i+1].split(' ')))
                # print(wh)
                simulation['warehouses'][j] = wh
                i+=2
            continue

        else:
            # read in every order
            simulation['orders'] = []
            number_of_orders = int(lines[i])
            i+=1
            for j in range(number_of_orders):
                # for every order
                order = {'coords': (tuple(map(int, lines[i].split(' '))))}
                order['products'] = list(map(int, lines[i+2].split(' ')))
                simulation['orders'].append(order)
                i+=3
            break

    for drone in simulation['drones']:
        drone['coords'] = simulation['warehouses'][0]['coords']
        drone['load'] = simulation['max_payload']

    return simulation

if __name__=='__main__':

    sim = load('busy_day.in')

    print(sim['drones'])
    print(len(sim['drones']))

    # pp = pprint.PrettyPrinter(indent=2)
    # pp.pprint(sim['warehouses'])
    print(sim['warehouses'])
    print(sim['orders'])
    print(sim['drones'])
<|endoftext|>


<commit_before>import time

class User:
    """
    A class representing a users information.
    NOTE: Defaults are attributes that pinylib expects
    """
    def __init__(self, **kwargs):
        # Default's.
        self.lf = kwargs.get('lf')
        self.account = kwargs.get('account', '')
        self.is_owner = kwargs.get('own', False)
        self.gp = kwargs.get('gp', 0)
        self.alevel = kwargs.get('alevel', '')
        self.bf = kwargs.get('bf', False)
        self.nick = kwargs.get('nick')
        self.btype = kwargs.get('btype', '')
        self.id = kwargs.get('id', -1)
        self.stype = kwargs.get('stype', 0)
        self.is_mod = kwargs.get('mod', False)
        self.join_time = time.time()
        self.tinychat_id = None
        self.last_login = None
        self.user_level = 0
        # Extras.
        self.last_msg = None

class Users:
    """
    This class represents the users in the room.

    Each user name is a dict key where the value of the key is represented by the User class.
    It contains methods to do various user based operations with.
    """
    def __init__(self):
        # Create a dictionary to store each user key value in.
        self._users = dict()

    @property
    def all(self):
        """
        All the users in the room.
        :return: dict key value (User)
        """
        return self._users

    @property
    def mods(self):
        """
        All the moderators in the room.
        :return: list all of the moderators objects (User) in the room.
        """
        _mods = []
        for user in self.all:
            if self.all[user].is_mod:
                _mods.append(self.all[user])
        return _mods

    @property
    def lurkers(self):
        """
        All the lurkers in the room.
        :return: list of all the lurker objects (User) in the room.
        """
        _lurkers = []
        for user in self.all:
            if self.all[user].lf:
                _lurkers.append(self.all[user])
        return _lurkers

    @property
    def norms(self):
        """
        All the normal users in the room.
        e.g users that are not moderators or lurkers.
        :return: list of all the normal users objects (User) in the room.
        """
        _regs = []
        for user in self.all:
            if not self.all[user].is_mod and not self.all[user].lf:
                _regs.append(self.all[user])
        return _regs

    def clear(self):
        """ Delete all the users. """
        self._users.clear()

    def add(self, user_info):
        """
        Add a user to the users dict.
        :param user_info dict, tinychat user info.
        :return user info object (User)
        """
        if user_info['nick'] not in self.all:
            self._users[user_info['nick']] = User(**user_info)
        return self.all[user_info['nick']]

    def change(self, old_nick, new_nick, user_info):
        """
        Change a user nickname.
        :param old_nick: str the user's old nickname.
        :param new_nick: str the user's new nickname.
        :param user_info: object, the user's user info (User)
        :return: True if changed, else False.
        """
        if self.delete(old_nick):
            if new_nick not in self.all:
                self._users[new_nick] = user_info
                return True
            return False
        return False

    def delete(self, user_name):
        """
        Delete a user from the Users class.
        :param user_name: str the user to delete.
        :return: True if deleted, else False.
        """
        if user_name in self.all:
            del self._users[user_name]
            return True
        return False

    def search(self, user_name):
        """
        Search the Users class by nick name for a user.
        :param user_name: str the user to find.
        :return: if user name is found, object (User) else None
        """
        if user_name in self.all:
            return self.all[user_name]
        return None

    def search_by_id(self, user_id):
        """
        Search for a user by id.
        :param user_id: str the users ID
        :return if user id is found, object (User) else None
        """
        for user in self.all:
            if str(self.all[user].id) == user_id:
                return self.all[user]
            return None
<commit_msg>trying to get rid of those annoing unsuported line endings<commit_after>import time

class User:
    """
    A class representing a users information.
    NOTE: Defaults are attributes that pinylib expects
    """
    def __init__(self, **kwargs):
        # Default's.
        self.lf = kwargs.get('lf')
        self.account = kwargs.get('account', '')
        self.is_owner = kwargs.get('own', False)
        self.gp = kwargs.get('gp', 0)
        self.alevel = kwargs.get('alevel', '')
        self.bf = kwargs.get('bf', False)
        self.nick = kwargs.get('nick')
        self.btype = kwargs.get('btype', '')
        self.id = kwargs.get('id', -1)
        self.stype = kwargs.get('stype', 0)
        self.is_mod = kwargs.get('mod', False)
        self.join_time = time.time()
        self.tinychat_id = None
        self.last_login = None
        self.user_level = 0
        # Extras.
        self.last_msg = None

class Users:
    """
    This class represents the users in the room.

    Each user name is a dict key where the value of the key is represented by the User class.
    It contains methods to do various user based operations with.
    """
    def __init__(self):
        # Create a dictionary to store each user key value in.
        self._users = dict()

    @property
    def all(self):
        """
        All the users in the room.
        :return: dict key value (User)
        """
        return self._users

    @property
    def mods(self):
        """
        All the moderators in the room.
        :return: list all of the moderators objects (User) in the room.
        """
        _mods = []
        for user in self.all:
            if self.all[user].is_mod:
                _mods.append(self.all[user])
        return _mods

    @property
    def lurkers(self):
        """
        All the lurkers in the room.
        :return: list of all the lurker objects (User) in the room.
        """
        _lurkers = []
        for user in self.all:
            if self.all[user].lf:
                _lurkers.append(self.all[user])
        return _lurkers

    @property
    def norms(self):
        """
        All the normal users in the room.
        e.g users that are not moderators or lurkers.
        :return: list of all the normal users objects (User) in the room.
        """
        _regs = []
        for user in self.all:
            if not self.all[user].is_mod and not self.all[user].lf:
                _regs.append(self.all[user])
        return _regs

    def clear(self):
        """ Delete all the users. """
        self._users.clear()

    def add(self, user_info):
        """
        Add a user to the users dict.
        :param user_info dict, tinychat user info.
        :return user info object (User)
        """
        if user_info['nick'] not in self.all:
            self._users[user_info['nick']] = User(**user_info)
        return self.all[user_info['nick']]

    def change(self, old_nick, new_nick, user_info):
        """
        Change a user nickname.
        :param old_nick: str the user's old nickname.
        :param new_nick: str the user's new nickname.
        :param user_info: object, the user's user info (User)
        :return: True if changed, else False.
        """
        if self.delete(old_nick):
            if new_nick not in self.all:
                self._users[new_nick] = user_info
                return True
            return False
        return False

    def delete(self, user_name):
        """
        Delete a user from the Users class.
        :param user_name: str the user to delete.
        :return: True if deleted, else False.
        """
        if user_name in self.all:
            del self._users[user_name]
            return True
        return False

    def search(self, user_name):
        """
        Search the Users class by nick name for a user.
        :param user_name: str the user to find.
        :return: if user name is found, object (User) else None
        """
        if user_name in self.all:
            return self.all[user_name]
        return None

    def search_by_id(self, user_id):
        """
        Search for a user by id.
        :param user_id: str the users ID
        :return if user id is found, object (User) else None
        """
        for user in self.all:
            if str(self.all[user].id) == user_id:
                return self.all[user]
            return None
<|endoftext|>


<commit_before>#!/usr/bin/env python2.7
import argparse # new in Python2.7
import open_bci_v3 as bci
import os
import time
import string
import atexit

from yapsy.PluginManager import PluginManager

import logging
logging.basicConfig(level=logging.CRITICAL) # DEBUG for dev

# Load the plugins from the plugin directory.
manager = PluginManager()
manager.setPluginPlaces(["plugins"])
manager.collectPlugins()

if __name__ == '__main__':

	print "			USER.py"
	parser = argparse.ArgumentParser(description="OpenBCI 'user'")
	parser.add_argument('-l', '--list', action='store_true',
				help="List available plugins.")
	parser.add_argument('-i', '--info', metavar='PLUGIN',
				help="Show more information about a plugin.")
	parser.add_argument('-p', '--port',
				help="Port to connect to OpenBCI Dongle " +
				"( ex /dev/ttyUSB0 or /dev/tty.usbserial-* )")
	# baud rate is not currently used
	parser.add_argument('-b', '--baud', default=115200, type=int,
				help="Baud rate (not currently used)")
	parser.add_argument('--no-filtering', dest='filtering', action='store_false',
				help="Disable notch filtering")
	parser.set_defaults(filtering=True)
	parser.add_argument('-d', '--daisy', dest='daisy', action='store_true',
				help="Force daisy mode (beta feature)")
	parser.set_defaults(daisy=False)
	# first argument: plugin name, then parameters for plugin
	parser.add_argument('-a', '--add', metavar=('PLUGIN', 'PARAM'), action='append', nargs='+',
			help="Select which plugins to activate and set parameters.")

	args = parser.parse_args()

	if not (args.port or args.list or args.info):
		parser.error('No action requested. Use `--port serial_port` to connect to the bord; `--list` to show available plugins or `--info [plugin_name]` to get more information.')

	# Print list of available plugins and exit
	if args.list:
		print "Available plugins:"
		for plugin in manager.getAllPlugins():
			print "\t-", plugin.name
		exit()

	# User wants more info about a plugin...
	if args.info:
		plugin=manager.getPluginByName(args.info)
		if plugin == None:
			# eg: if an import fail inside a plugin, yapsy skip it
			print "Error: [", args.info, "] not found or could not be loaded. Check name and requirements."
		else:
			print plugin.description
			plugin.plugin_object.show_help()
		exit()

	print "\n------------SETTINGS-------------"
	print "Notch filtering:", args.filtering

	print "\n-------INSTANTIATING BOARD-------"
	board = bci.OpenBCIBoard(port=args.port, daisy=args.daisy, filter_data=args.filtering)

	#  Info about effective number of channels and sampling rate
	if board.daisy:
		print "Force daisy mode:",
	else:
		print "No daisy:",
	print board.getNbEEGChannels(), "EEG channels and", board.getNbAUXChannels(), "AUX channels at", board.getSampleRate(), "Hz."

	print "\n------------PLUGINS--------------"
	# Loop round the plugins and print their names.
	print "Found plugins:",
	for plugin in manager.getAllPlugins():
		print "[", plugin.name, "]",
	print

	# Fetch plugins, try to activate them, add to the list if OK
	plug_list = []
	callback_list = []
	if args.add:
		for plug_candidate in args.add:
			# first value: plugin name, then optional arguments
			plug_name=plug_candidate[0]
			plug_args=plug_candidate[1:]
			# Try to find name
			plug=manager.getPluginByName(plug_name)
			if plug == None:
				# eg: if an import fail inside a plugin, yapsy skip it
				print "Error: [", plug_name, "] not found or could not be loaded. Check name and requirements."
			else:
				print "\nActivating [", plug_name, "] plugin..."
				if not plug.plugin_object.pre_activate(plug_args, sample_rate=board.getSampleRate(), eeg_channels=board.getNbEEGChannels(), aux_channels=board.getNbAUXChannels()):
					print "Error while activating [", plug_name, "], check output for more info."
				else:
					print "Plugin [", plug_name, "] added to the list"
					plug_list.append(plug.plugin_object)
					callback_list.append(plug.plugin_object)

	if len(plug_list) == 0:
		print "WARNING: no plugin selected, you will only be able to communicate with the board."
		fun = None
	else:
		fun = callback_list

	def cleanUp():
		board.disconnect()
		print "Deactivating Plugins..."
		for plug in plug_list:
			plug.deactivate()
		print "User.py exiting..."

	atexit.register(cleanUp)

	print "--------------INFO---------------"
	print "User serial interface enabled...\n\
View command map at http://docs.openbci.com.\n\
Type start to run. Type /exit to exit. \n\
Board outputs are automatically printed as: \n\
%  <tab>  message\n\
$$$ signals end of message"

	print("\n-------------BEGIN---------------")

	#Start by restoring default settings
	s = 'd'

	while(s != "/exit"):
		#Send char and wait for registers to set
		if (not s): pass

		elif("help" in s): print "View command map at: \
http://docs.openbci.com/software/01-OpenBCI_SDK.\n\
For user interface: read README or view \
https://github.com/OpenBCI/OpenBCI_Python"

		elif('/' == s[0]):
			s = s[1:]
			rec = False

			if("T:" in s):
				lapse = int(s[string.find(s,"T:")+2:])
				rec = True
			elif("t:" in s):
				lapse = int(s[string.find(s,"t:")+2:])
				rec = True
			else:
				lapse = -1

			if("start" in s):
				if(fun != None):
					board.start_streaming(fun, lapse)
				else:
					print "No function loaded"
				rec = True

			elif('test' in s):
				test = int(s[string.find(s,"test")+4:])
				board.test_signal(test)
				rec = True

			if rec == False:
				print("Command not recognized...")

		elif s:
			for c in s:
				board.ser.write(c)
				time.sleep(0.100)

		line = ''
		time.sleep(0.1) #Wait to see if the board has anything to report
		while board.ser.inWaiting():
			c = board.ser.read()
			line += c
			time.sleep(0.001)
			if (c == '\n'):
				print('%\t'+line[:-1])
				line = ''
		print(line)

		#Take user input
		s = raw_input('--> ');
<commit_msg>Start board streaming in a separate thread.<commit_after>#!/usr/bin/env python2.7
import argparse # new in Python2.7
import open_bci_v3 as bci
import os
import time
import string
import atexit
import threading

from yapsy.PluginManager import PluginManager

import logging
logging.basicConfig(level=logging.CRITICAL) # DEBUG for dev

# Load the plugins from the plugin directory.
manager = PluginManager()
manager.setPluginPlaces(["plugins"])
manager.collectPlugins()

if __name__ == '__main__':

	print "			USER.py"
	parser = argparse.ArgumentParser(description="OpenBCI 'user'")
	parser.add_argument('-l', '--list', action='store_true',
				help="List available plugins.")
	parser.add_argument('-i', '--info', metavar='PLUGIN',
				help="Show more information about a plugin.")
	parser.add_argument('-p', '--port',
				help="Port to connect to OpenBCI Dongle " +
				"( ex /dev/ttyUSB0 or /dev/tty.usbserial-* )")
	# baud rate is not currently used
	parser.add_argument('-b', '--baud', default=115200, type=int,
				help="Baud rate (not currently used)")
	parser.add_argument('--no-filtering', dest='filtering', action='store_false',
				help="Disable notch filtering")
	parser.set_defaults(filtering=True)
	parser.add_argument('-d', '--daisy', dest='daisy', action='store_true',
				help="Force daisy mode (beta feature)")
	parser.set_defaults(daisy=False)
	# first argument: plugin name, then parameters for plugin
	parser.add_argument('-a', '--add', metavar=('PLUGIN', 'PARAM'), action='append', nargs='+',
			help="Select which plugins to activate and set parameters.")

	args = parser.parse_args()

	if not (args.port or args.list or args.info):
		parser.error('No action requested. Use `--port serial_port` to connect to the bord; `--list` to show available plugins or `--info [plugin_name]` to get more information.')

	# Print list of available plugins and exit
	if args.list:
		print "Available plugins:"
		for plugin in manager.getAllPlugins():
			print "\t-", plugin.name
		exit()

	# User wants more info about a plugin...
	if args.info:
		plugin=manager.getPluginByName(args.info)
		if plugin == None:
			# eg: if an import fail inside a plugin, yapsy skip it
			print "Error: [", args.info, "] not found or could not be loaded. Check name and requirements."
		else:
			print plugin.description
			plugin.plugin_object.show_help()
		exit()

	print "\n------------SETTINGS-------------"
	print "Notch filtering:", args.filtering

	print "\n-------INSTANTIATING BOARD-------"
	board = bci.OpenBCIBoard(port=args.port, daisy=args.daisy, filter_data=args.filtering)

	#  Info about effective number of channels and sampling rate
	if board.daisy:
		print "Force daisy mode:",
	else:
		print "No daisy:",
	print board.getNbEEGChannels(), "EEG channels and", board.getNbAUXChannels(), "AUX channels at", board.getSampleRate(), "Hz."

	print "\n------------PLUGINS--------------"
	# Loop round the plugins and print their names.
	print "Found plugins:",
	for plugin in manager.getAllPlugins():
		print "[", plugin.name, "]",
	print

	# Fetch plugins, try to activate them, add to the list if OK
	plug_list = []
	callback_list = []
	if args.add:
		for plug_candidate in args.add:
			# first value: plugin name, then optional arguments
			plug_name=plug_candidate[0]
			plug_args=plug_candidate[1:]
			# Try to find name
			plug=manager.getPluginByName(plug_name)
			if plug == None:
				# eg: if an import fail inside a plugin, yapsy skip it
				print "Error: [", plug_name, "] not found or could not be loaded. Check name and requirements."
			else:
				print "\nActivating [", plug_name, "] plugin..."
				if not plug.plugin_object.pre_activate(plug_args, sample_rate=board.getSampleRate(), eeg_channels=board.getNbEEGChannels(), aux_channels=board.getNbAUXChannels()):
					print "Error while activating [", plug_name, "], check output for more info."
				else:
					print "Plugin [", plug_name, "] added to the list"
					plug_list.append(plug.plugin_object)
					callback_list.append(plug.plugin_object)

	if len(plug_list) == 0:
		print "WARNING: no plugin selected, you will only be able to communicate with the board."
		fun = None
	else:
		fun = callback_list

	def cleanUp():
		board.disconnect()
		print "Deactivating Plugins..."
		for plug in plug_list:
			plug.deactivate()
		print "User.py exiting..."

	atexit.register(cleanUp)

	print "--------------INFO---------------"
	print "User serial interface enabled...\n\
View command map at http://docs.openbci.com.\n\
Type start to run. Type /exit to exit. \n\
Board outputs are automatically printed as: \n\
%  <tab>  message\n\
$$$ signals end of message"

	print("\n-------------BEGIN---------------")

	#Start by restoring default settings
	s = 'd'

	while(s != "/exit"):
		#Send char and wait for registers to set
		if (not s): pass

		elif("help" in s): print "View command map at: \
http://docs.openbci.com/software/01-OpenBCI_SDK.\n\
For user interface: read README or view \
https://github.com/OpenBCI/OpenBCI_Python"

		elif('/' == s[0]):
			s = s[1:]
			rec = False

			if("T:" in s):
				lapse = int(s[string.find(s,"T:")+2:])
				rec = True
			elif("t:" in s):
				lapse = int(s[string.find(s,"t:")+2:])
				rec = True
			else:
				lapse = -1

			if("start" in s):
				if(fun != None):
					# start streaming in a separate thread so we could always send commands in here
					boardThread = threading.Thread(target=board.start_streaming, args=(fun, lapse))
					boardThread.daemon = True # will stop on exit
					boardThread.start()
				else:
					print "No function loaded"
				rec = True
			elif('test' in s):
				test = int(s[string.find(s,"test")+4:])
				board.test_signal(test)
				rec = True
			if rec == False:
				print("Command not recognized...")

		elif s:
			for c in s:
				board.ser.write(c)
				time.sleep(0.100)

		line = ''
		time.sleep(0.1) #Wait to see if the board has anything to report
		while board.ser.inWaiting():
			c = board.ser.read()
			line += c
			time.sleep(0.001)
			if (c == '\n'):
				print('%\t'+line[:-1])
				line = ''
		print(line)

		#Take user input
		s = raw_input('--> ');
<|endoftext|>


<commit_before>import re
import random
import bisect
import numpy as np

#http://www.gefeg.com/edifact/d03a/s3/codes/cl1h.htm
#This is a terrible method, but it works for now
state_names = ['Andhra Pradesh', 'Arunachal Pradesh', 'Assam', 'Bihar', 'Chhattisgarh', 'Delhi', 'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh', 'Jammu and Kashmir', 'Jharkhand', 'Karnataka', 'Kerala', 'Madhya Pradesh', 'Maharashtra', 'Manipur', 'Meghalaya', 'Mizoram', 'Nagaland', 'Odisha', 'Punjab', 'Rajasthan', 'Sikkim', 'Tamil Nadu', 'Tripura', 'Uttar Pradesh', 'Uttarakhand', 'West Bengal']
state_abbreviations = ['AP', 'AR', 'AS', 'BR', 'CT', 'DL', 'GA', 'GJ', 'HR', 'HP', 'JK', 'JH', 'KA', 'KL', 'MP', 'MH', 'MN', 'ML', 'MZ', 'NL', 'OR', 'PB', 'RJ', 'SK', 'TN', 'TR', 'UP', 'UT', 'WB']

#Other utility methods (that aren't input/output) which multiple
#files might need go here

def clean_state_filename(filename):
    #The name is the first thing in the filename
    #it is always followed by a non-word character
    #sometimes & is in the name, so allow that
    state = re.sub(r'\.CSV', '', filename)
    state = re.sub(r'\([A-Z&]*\)', '', state)
    state = re.sub(r'[()\d]', '', state)
    return clean_state_name(state)

def clean_state_name(state):
    state = re.sub('Nct of Delhi', 'Delhi', state)
    state = re.sub('JAMMU', 'Jammu', state)
    state = re.sub('Utter', 'Uttar Pradesh', state)
    state = re.sub('Himacahl', 'Himachal', state)
    state = re.sub('&', 'and', state)
    state = state.strip()
    if state not in state_names:
        print 'Warning: state name not found while cleaning:', state
    return state

def avg(x):
    return sum(x)/float(len(x))

class FilterPopulation(object):

    def __init__(self, cost_threshold):
        self.cost_threshold = cost_threshold
        self.eye_health_treatment_thresholds = eye_health_treatment_thresholds

    def filter_all(self, person):
        return self.filter_health(person) and self.filter_money(person)

    def filter_health(self, person):
        return self.filter_eye_health(person)

    def filter_eye_health(self, person):
        #We only care about the largest treatment threshold
        return person.eye_health <= max(
                self.eye_health_treatment_thresholds.values())

    """
    def filter_eye_health_surgery(self, person):
        # filter for those who need surgery who is about 10 % of the population
        return person.eye_health <= self.eye_health_threshold_surgery

    def filter_eye_health_glasses(self, person):
        # filter for those who need glasses who is about 30 % of the population
        return person.eye_health <= self.eye_health_threshold_glasses
    """

    def filter_money(self, person):
        return person.money>=self.cost_threshold

class Location(object):
    def __init__(self, state_name, district_name, classification):
        self.state_name = state_name
        self.district_name = district_name
        self.classification = classification

    def is_in(self, location):
        if not self.district_name == location.district_name:
            return False
        if not self.state_name == location.state:
            return False
        if location.classification == 'total' or self.classification == location.classification:
            return True

def calc_eye_health_treatment_thresholds(population):
    eye_health_treatment_thresholds = dict()
    # thresholds for surgery and glasses in filtering the people
    eye_health_treatment_thresholds['surgery'] = np.percentile(
            [person.eye_health for person in population], 10)
    eye_health_treatment_thresholds['glasses'] = np.percentile(
            [person.eye_health for person in population], 30)
    return eye_health_treatment_thresholds

#http://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice
#Given a set of choices and corresponding weights, choose a random option
#using the appropriate weight
def weighted_choice(choices, weights):
    #Convert the weights into cumulative weights
    cumulative_weights = []
    total = 0
    for weight in weights:
        total += weight
        cumulative_weights.append(total)
    #total should 1 if weights is a proper distribution
    rnd = random.random() * total
    i = bisect.bisect(cumulative_weights, rnd) #faster search
    return choices[i]

def init_life_expectancy_distribution():
    #One per five years of life
    life_expectancy = dict()
    life_expectancy_anchor_points_male = [63.8, 62.7, 58.1, 53.4, 52.9, 44.2,
            39.7, 35.3, 31.1, 26.9, 23, 19.3, 15.7, 12.7, 10.2, 8.3, 6.7,5.2,
            3.9, 2.8, 2.0]
    life_expectancy['M'] = list()
    for i in xrange(20):
        current_spread = np.linspace(life_expectancy_anchor_points_male[i],
                life_expectancy_anchor_points_male[i+1], num=5)
        for life_expectancy_at_age in current_spread:
            life_expectancy['M'].append(life_expectancy_at_age)
    life_expectancy['M'].append(life_expectancy_anchor_points_male[-1])

    life_expectancy_anchor_points_female = [67.3, 66.8, 62.2, 57.5, 52.9, 48.4,
            43.8, 39.2, 34.7, 30.2, 25.8, 25.8, 17.7, 14.3, 11.3, 9.0, 7.0, 5.3,
            3.9, 2.8, 2.0]
    life_expectancy['F'] = list()
    for i in xrange(20):
        current_spread = np.linspace(life_expectancy_anchor_points_female[i],
                life_expectancy_anchor_points_female[i+1], num=5)
        for life_expectancy_at_age in current_spread:
            life_expectancy['F'].append(life_expectancy_at_age)
    life_expectancy['F'].append(life_expectancy_anchor_points_female[-1])

    return life_expectancy

life_expectancy = init_life_expectancy_distribution()

def calculate_life_exp(gender, age):
    # life-expectancy data from
    # http://www.worldlifeexpectancy.com/country-health-profile/india
    return life_expectancy[gender][age]

def calculate_qaly(person):
    age = person.age
    life_exp = calculate_life_exp(person.gender, age)
    util = person.health_utility
    return life_exp * util

#Perform analytics on the treated population
def analyze_populations(population, treated_population):
    print 'Average health utility in original population', avg(
            [person.health_utility for person in population])
    print 'Average health utility in treated population', avg(
            [person.health_utility for person in treated_population if person.get_is_treated()])
    print 'Average age of original population:', avg(
            [person.age for person in population])
    print 'average age of treated population:', avg([person.age
        for person in treated_population if person.get_is_treated()])

    ##### QALY for Glasses #####

    # generate before and after population
    glasses_problem = Problem.from_problem_name('glasses')
    glasses_population_before = [person for person in population if
            person.has_problem(glasses_problem)]
    glasses_population_after = [person for person in treated_population if
            person.has_problem(glasses_problem)]
    print 'No. of people who needed glasses: ', len(glasses_population_before)
    # QALY_before
    glasses_untreated_qaly = 0
    for person in glasses_population_before:
        glasses_untreated_qaly += calculate_qaly(person)
    glasses_average_untreated_qaly = glasses_untreated_qaly/len(glasses_population_before)
    print 'QALY-Glasses: untreated:', glasses_average_untreated_qaly

    # QALY_after
    glasses_treated_qaly = 0
    for person in glasses_population_after:
        glasses_treated_qaly += calculate_qaly(person)
    glasses_average_treated_qaly = glasses_treated_qaly/len(glasses_population_after)
    print 'QALY-Glasses: treated:', glasses_average_treated_qaly

    print '* QALY-Glasses: change by:', glasses_average_treated_qaly - glasses_average_untreated_qaly

     ##### QALY for Cataracts #####

    # generate before and after population
    cataracts_problem = Problem.from_problem_name('cataracts')
    cataracts_population_before = [person for person in population if
            person.has_problem(cataracts_problem)]
    cataracts_population_after = [person for person in treated_population if
            person.has_problem(cataracts_problem)]
    print 'No. of patients who had cataracts: ', len(cataracts_population_before)
    # QALY_before
    cataracts_untreated_qaly = 0
    for person in cataracts_population_before:
        cataracts_untreated_qaly += calculate_qaly(person)
    cataracts_average_untreated_qaly = cataracts_untreated_qaly/len(cataracts_population_before)
    print 'QALY-Cataracts: untreated:', cataracts_average_untreated_qaly

    # QALY_after
    cataracts_treated_qaly = 0
    for person in cataracts_population_after:
        cataracts_treated_qaly += calculate_qaly(person)
    cataracts_average_treated_qaly = cataracts_treated_qaly/len(cataracts_population_after)
    print 'QALY-Cataracts: treated:', cataracts_average_treated_qaly

    print '* QALY-Cataracts: change by:', cataracts_average_treated_qaly - cataracts_average_untreated_qaly

def old_qaly_calc(population, treated_population):
    # generate before and after population
    glasses_problem = Problem.from_problem_name('glasses')
    glasses_population_before = [person for person in population if
            person.has_problem(glasses_problem)]
    glasses_population_after = [person for person in treated_population if
            person.has_problem(glasses_problem)]
    # calculate ave_age
    glasses_age_before = avg([person.age for person in glasses_population_before])
    glasses_age_after = avg([person.age for person in glasses_population_after])
    # calculate life_exp
    glasses_life_expectancy_before = 68
    glasses_life_expectancy_after = 72
    glasses_utility_before = avg([person.health_utility for person in glasses_population_before])
    glasses_utility_after = avg([person.health_utility for person in glasses_population_after])
    glasses_utility_change = glasses_utility_after - glasses_utility_before
    qaly_glasses_before = glasses_life_expectancy_before + glasses_utility_before
    qaly_glasses_after = glasses_life_expectancy_after + glasses_utility_after
    qaly_change_glasses = qaly_glasses_after - qaly_glasses_before
    print 'QALY_Glasses: before treatment', qaly_glasses_before
    print 'QALY_Glasses: after treatment', qaly_glasses_after
    print 'QALY Glasses: increased by', qaly_change_glasses
    ##### QALY for cataracts #####
    # generate before and after population
    cataracts_problem = Problem.from_problem_name('cataracts')
    cataracts_population_before = [person for person in population if
            person.has_problem(cataracts_problem)]
    cataracts_population_after = [person for person in treated_population if
            person.has_problem(cataracts_problem)]
    # calculate ave_age, ave_life_expectancy, and average utility
    cataracts_age_before = avg([person.age for person in cataracts_population_before])
    cataracts_age_after = avg([person.age for person in cataracts_population_after])
    cataracts_life_expectancy_before = 64
    cataracts_life_expectancy_after = 78
    cataracts_utility_before = avg([person.health_utility for person in cataracts_population_before])
    cataracts_utility_after = avg([person.health_utility for person in cataracts_population_after])
    cataracts_utility_change = cataracts_utility_after - cataracts_utility_before
    qaly_cataracts_before = cataracts_life_expectancy_before + cataracts_utility_before
    qaly_cataracts_after = cataracts_life_expectancy_after + cataracts_utility_after
    qaly_change_cataracts = qaly_cataracts_after - qaly_cataracts_before
    print 'QALY_Cataracts: before treatment', qaly_cataracts_before
    print 'QALY_Cataracts: after treatment', qaly_cataracts_after
    print 'QALY Cataracts: increased by', qaly_change_cataracts
    print 'Average health_utility increase:', (glasses_utility_change + cataracts_utility_change)/2

class Problem(object):
    def __init__(self, problem_name, health_utility, cost_full, cost_subsidized):
        self.name = problem_name
        self.health_utility = health_utility
        self.cost_full = cost_full
        self.cost_subsidized = cost_subsidized

    def equals(self, problem):
        return self.name == problem.name

    @classmethod #[health_utility, cost_full, cost_subsidized]
    def from_problem_name(cls, problem_name):
        attributes  = {
                'cataracts': [0.14, 4750, 750], #FROM DATA
                'glasses': [0.07, 120, 120]     #ASSUMPTION
                }[problem_name]
        return cls(problem_name, attributes[0], attributes[1], attributes[2])

if __name__ == "__main__":
    args = initialize_argument_parser()
    data = io_data.Database(import_data=args['import_data'])
    #Only run the test if we didn't try to recreate the database
    # Not strictly necessary, but helps separate workflow
    if not args['import_data']:
        if args['profile']:
            cProfile.run('test(data, args)')
        else:
            test(data, args)

<commit_msg>Silence printed warning - unnecessary<commit_after>import re
import random
import bisect
import numpy as np

#http://www.gefeg.com/edifact/d03a/s3/codes/cl1h.htm
#This is a terrible method, but it works for now
state_names = ['Andhra Pradesh', 'Arunachal Pradesh', 'Assam', 'Bihar', 'Chhattisgarh', 'Delhi', 'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh', 'Jammu and Kashmir', 'Jharkhand', 'Karnataka', 'Kerala', 'Madhya Pradesh', 'Maharashtra', 'Manipur', 'Meghalaya', 'Mizoram', 'Nagaland', 'Odisha', 'Punjab', 'Rajasthan', 'Sikkim', 'Tamil Nadu', 'Tripura', 'Uttar Pradesh', 'Uttarakhand', 'West Bengal']
state_abbreviations = ['AP', 'AR', 'AS', 'BR', 'CT', 'DL', 'GA', 'GJ', 'HR', 'HP', 'JK', 'JH', 'KA', 'KL', 'MP', 'MH', 'MN', 'ML', 'MZ', 'NL', 'OR', 'PB', 'RJ', 'SK', 'TN', 'TR', 'UP', 'UT', 'WB']

#Other utility methods (that aren't input/output) which multiple
#files might need go here

def clean_state_filename(filename):
    #The name is the first thing in the filename
    #it is always followed by a non-word character
    #sometimes & is in the name, so allow that
    state = re.sub(r'\.CSV', '', filename)
    state = re.sub(r'\([A-Z&]*\)', '', state)
    state = re.sub(r'[()\d]', '', state)
    return clean_state_name(state)

def clean_state_name(state):
    state = re.sub('Nct of Delhi', 'Delhi', state)
    state = re.sub('JAMMU', 'Jammu', state)
    state = re.sub('Utter', 'Uttar Pradesh', state)
    state = re.sub('Himacahl', 'Himachal', state)
    state = re.sub('&', 'and', state)
    state = state.strip()
    #if state not in state_names:
    #    print 'Warning: state name not found while cleaning:', state
    return state

def avg(x):
    return sum(x)/float(len(x))

class FilterPopulation(object):

    def __init__(self, cost_threshold):
        self.cost_threshold = cost_threshold
        self.eye_health_treatment_thresholds = eye_health_treatment_thresholds

    def filter_all(self, person):
        return self.filter_health(person) and self.filter_money(person)

    def filter_health(self, person):
        return self.filter_eye_health(person)

    def filter_eye_health(self, person):
        #We only care about the largest treatment threshold
        return person.eye_health <= max(
                self.eye_health_treatment_thresholds.values())

    """
    def filter_eye_health_surgery(self, person):
        # filter for those who need surgery who is about 10 % of the population
        return person.eye_health <= self.eye_health_threshold_surgery

    def filter_eye_health_glasses(self, person):
        # filter for those who need glasses who is about 30 % of the population
        return person.eye_health <= self.eye_health_threshold_glasses
    """

    def filter_money(self, person):
        return person.money>=self.cost_threshold

class Location(object):
    def __init__(self, state_name, district_name, classification):
        self.state_name = state_name
        self.district_name = district_name
        self.classification = classification

    def is_in(self, location):
        if not self.district_name == location.district_name:
            return False
        if not self.state_name == location.state:
            return False
        if location.classification == 'total' or self.classification == location.classification:
            return True

def calc_eye_health_treatment_thresholds(population):
    eye_health_treatment_thresholds = dict()
    # thresholds for surgery and glasses in filtering the people
    eye_health_treatment_thresholds['surgery'] = np.percentile(
            [person.eye_health for person in population], 10)
    eye_health_treatment_thresholds['glasses'] = np.percentile(
            [person.eye_health for person in population], 30)
    return eye_health_treatment_thresholds

#http://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice
#Given a set of choices and corresponding weights, choose a random option
#using the appropriate weight
def weighted_choice(choices, weights):
    #Convert the weights into cumulative weights
    cumulative_weights = []
    total = 0
    for weight in weights:
        total += weight
        cumulative_weights.append(total)
    #total should 1 if weights is a proper distribution
    rnd = random.random() * total
    i = bisect.bisect(cumulative_weights, rnd) #faster search
    return choices[i]

def init_life_expectancy_distribution():
    #One per five years of life
    life_expectancy = dict()
    life_expectancy_anchor_points_male = [63.8, 62.7, 58.1, 53.4, 52.9, 44.2,
            39.7, 35.3, 31.1, 26.9, 23, 19.3, 15.7, 12.7, 10.2, 8.3, 6.7,5.2,
            3.9, 2.8, 2.0]
    life_expectancy['M'] = list()
    for i in xrange(20):
        current_spread = np.linspace(life_expectancy_anchor_points_male[i],
                life_expectancy_anchor_points_male[i+1], num=5)
        for life_expectancy_at_age in current_spread:
            life_expectancy['M'].append(life_expectancy_at_age)
    life_expectancy['M'].append(life_expectancy_anchor_points_male[-1])

    life_expectancy_anchor_points_female = [67.3, 66.8, 62.2, 57.5, 52.9, 48.4,
            43.8, 39.2, 34.7, 30.2, 25.8, 25.8, 17.7, 14.3, 11.3, 9.0, 7.0, 5.3,
            3.9, 2.8, 2.0]
    life_expectancy['F'] = list()
    for i in xrange(20):
        current_spread = np.linspace(life_expectancy_anchor_points_female[i],
                life_expectancy_anchor_points_female[i+1], num=5)
        for life_expectancy_at_age in current_spread:
            life_expectancy['F'].append(life_expectancy_at_age)
    life_expectancy['F'].append(life_expectancy_anchor_points_female[-1])

    return life_expectancy

life_expectancy = init_life_expectancy_distribution()

def calculate_life_exp(gender, age):
    # life-expectancy data from
    # http://www.worldlifeexpectancy.com/country-health-profile/india
    return life_expectancy[gender][age]

def calculate_qaly(person):
    age = person.age
    life_exp = calculate_life_exp(person.gender, age)
    util = person.health_utility
    return life_exp * util

#Perform analytics on the treated population
def analyze_populations(population, treated_population):
    print 'Average health utility in original population', avg(
            [person.health_utility for person in population])
    print 'Average health utility in treated population', avg(
            [person.health_utility for person in treated_population if person.get_is_treated()])
    print 'Average age of original population:', avg(
            [person.age for person in population])
    print 'average age of treated population:', avg([person.age
        for person in treated_population if person.get_is_treated()])

    ##### QALY for Glasses #####

    # generate before and after population
    glasses_problem = Problem.from_problem_name('glasses')
    glasses_population_before = [person for person in population if
            person.has_problem(glasses_problem)]
    glasses_population_after = [person for person in treated_population if
            person.has_problem(glasses_problem)]
    print 'No. of people who needed glasses: ', len(glasses_population_before)
    # QALY_before
    glasses_untreated_qaly = 0
    for person in glasses_population_before:
        glasses_untreated_qaly += calculate_qaly(person)
    glasses_average_untreated_qaly = glasses_untreated_qaly/len(glasses_population_before)
    print 'QALY-Glasses: untreated:', glasses_average_untreated_qaly

    # QALY_after
    glasses_treated_qaly = 0
    for person in glasses_population_after:
        glasses_treated_qaly += calculate_qaly(person)
    glasses_average_treated_qaly = glasses_treated_qaly/len(glasses_population_after)
    print 'QALY-Glasses: treated:', glasses_average_treated_qaly

    print '* QALY-Glasses: change by:', glasses_average_treated_qaly - glasses_average_untreated_qaly

     ##### QALY for Cataracts #####

    # generate before and after population
    cataracts_problem = Problem.from_problem_name('cataracts')
    cataracts_population_before = [person for person in population if
            person.has_problem(cataracts_problem)]
    cataracts_population_after = [person for person in treated_population if
            person.has_problem(cataracts_problem)]
    print 'No. of patients who had cataracts: ', len(cataracts_population_before)
    # QALY_before
    cataracts_untreated_qaly = 0
    for person in cataracts_population_before:
        cataracts_untreated_qaly += calculate_qaly(person)
    cataracts_average_untreated_qaly = cataracts_untreated_qaly/len(cataracts_population_before)
    print 'QALY-Cataracts: untreated:', cataracts_average_untreated_qaly

    # QALY_after
    cataracts_treated_qaly = 0
    for person in cataracts_population_after:
        cataracts_treated_qaly += calculate_qaly(person)
    cataracts_average_treated_qaly = cataracts_treated_qaly/len(cataracts_population_after)
    print 'QALY-Cataracts: treated:', cataracts_average_treated_qaly

    print '* QALY-Cataracts: change by:', cataracts_average_treated_qaly - cataracts_average_untreated_qaly

def old_qaly_calc(population, treated_population):
    # generate before and after population
    glasses_problem = Problem.from_problem_name('glasses')
    glasses_population_before = [person for person in population if
            person.has_problem(glasses_problem)]
    glasses_population_after = [person for person in treated_population if
            person.has_problem(glasses_problem)]
    # calculate ave_age
    glasses_age_before = avg([person.age for person in glasses_population_before])
    glasses_age_after = avg([person.age for person in glasses_population_after])
    # calculate life_exp
    glasses_life_expectancy_before = 68
    glasses_life_expectancy_after = 72
    glasses_utility_before = avg([person.health_utility for person in glasses_population_before])
    glasses_utility_after = avg([person.health_utility for person in glasses_population_after])
    glasses_utility_change = glasses_utility_after - glasses_utility_before
    qaly_glasses_before = glasses_life_expectancy_before + glasses_utility_before
    qaly_glasses_after = glasses_life_expectancy_after + glasses_utility_after
    qaly_change_glasses = qaly_glasses_after - qaly_glasses_before
    print 'QALY_Glasses: before treatment', qaly_glasses_before
    print 'QALY_Glasses: after treatment', qaly_glasses_after
    print 'QALY Glasses: increased by', qaly_change_glasses
    ##### QALY for cataracts #####
    # generate before and after population
    cataracts_problem = Problem.from_problem_name('cataracts')
    cataracts_population_before = [person for person in population if
            person.has_problem(cataracts_problem)]
    cataracts_population_after = [person for person in treated_population if
            person.has_problem(cataracts_problem)]
    # calculate ave_age, ave_life_expectancy, and average utility
    cataracts_age_before = avg([person.age for person in cataracts_population_before])
    cataracts_age_after = avg([person.age for person in cataracts_population_after])
    cataracts_life_expectancy_before = 64
    cataracts_life_expectancy_after = 78
    cataracts_utility_before = avg([person.health_utility for person in cataracts_population_before])
    cataracts_utility_after = avg([person.health_utility for person in cataracts_population_after])
    cataracts_utility_change = cataracts_utility_after - cataracts_utility_before
    qaly_cataracts_before = cataracts_life_expectancy_before + cataracts_utility_before
    qaly_cataracts_after = cataracts_life_expectancy_after + cataracts_utility_after
    qaly_change_cataracts = qaly_cataracts_after - qaly_cataracts_before
    print 'QALY_Cataracts: before treatment', qaly_cataracts_before
    print 'QALY_Cataracts: after treatment', qaly_cataracts_after
    print 'QALY Cataracts: increased by', qaly_change_cataracts
    print 'Average health_utility increase:', (glasses_utility_change + cataracts_utility_change)/2

class Problem(object):
    def __init__(self, problem_name, health_utility, cost_full, cost_subsidized):
        self.name = problem_name
        self.health_utility = health_utility
        self.cost_full = cost_full
        self.cost_subsidized = cost_subsidized

    def equals(self, problem):
        return self.name == problem.name

    @classmethod #[health_utility, cost_full, cost_subsidized]
    def from_problem_name(cls, problem_name):
        attributes  = {
                'cataracts': [0.14, 4750, 750], #FROM DATA
                'glasses': [0.07, 120, 120]     #ASSUMPTION
                }[problem_name]
        return cls(problem_name, attributes[0], attributes[1], attributes[2])

if __name__ == "__main__":
    args = initialize_argument_parser()
    data = io_data.Database(import_data=args['import_data'])
    #Only run the test if we didn't try to recreate the database
    # Not strictly necessary, but helps separate workflow
    if not args['import_data']:
        if args['profile']:
            cProfile.run('test(data, args)')
        else:
            test(data, args)

<|endoftext|>


<commit_before>def range(a,b=none,c=none):
	i_a=a
	i_b=b
	i_c=c
	if (b is none):
		i_b=a
		i_a=0
	if (c is none):
		i_c=1
	dim r[((i_b-i_a)/i_c)+1]
	i=i_a
	j=0
	while i<=i_b:
		r[j]=i
		if (c is none):
			i+=1
		else:
			i=i+c
		j=j+1
	return r

def xrange(a,b):
	return range(a,b)<commit_msg>Odd even sort in util<commit_after>def range(a,b=none,c=none):
	i_a=a
	i_b=b
	i_c=c
	if (b is none):
		i_b=a
		i_a=0
	if (c is none):
		i_c=1
	dim r[((i_b-i_a)/i_c)+1]
	i=i_a
	j=0
	while i<=i_b:
		r[j]=i
		if (c is none):
			i+=1
		else:
			i=i+c
		j=j+1
	return r

def xrange(a,b):
	return range(a,b)

def oddSort(x, length=none):
  l=length
  if (length is none):
    l=len(x)
  sorted=true
  while not sorted:
    sorted=true
    for i in range(0, l-1, 2):
      if x[i] > x[i+1]:
        temp= x[i]
        x[i]=x[i+1]
        x[i+1] = temp
        sorted=false
    for i in range(1, l-2, 2):
      if x[i] > x[i+1]:
        temp= x[i]
        x[i]=x[i+1]
        x[i+1] = temp
        sorted=false<|endoftext|>


<commit_before>#!/bin/env python
"""
    Utility functions
"""
import requests
import logging
import os

def download_file(url, outpath):
    """
        Download a file to a given location over HTTP
    :param url: The HTTP location to fetch.
    :param outpath: The location to save it to
    """
    # Don't bother re-downloading stuff
    if os.path.exists(outpath):
        logging.info("%s already exists, skipping...", outpath)
        return

    logging.info("Downloading %s => '%s'", url, outpath)
    r = requests.get(url)
    with open(outpath, 'w') as fout:
        fout.write(r.content)
<commit_msg>Correction to path traversal.<commit_after>#!/bin/env python
"""
    Utility functions
"""
import requests
import logging
import os

def download_file(url, outpath):
    """
        Download a file to a given location over HTTP
    :param url: The HTTP location to fetch.
    :param outpath: The location to save it to
    """
    # Don't bother re-downloading stuff
    if os.path.exists(outpath):
        logging.info("%s already exists, skipping...", outpath)
        return

    logging.info("Downloading %s => '%s'", url, outpath)
    r = requests.get(url)
    with open(outpath, 'w') as fout:
        fout.write(r.content)

def list_files_with_extension(directory, extension):
    """
    Return a list of files with a given extension.
    :param directory: The directory to explore.
    :param extension: The extension to filter on.
    :return: The list of matching files.
    """
    tmp = os.listdir(directory)
    files = [f for f in tmp if os.path.isfile(f)]
    extens = [f for f in files if os.path.splitext(f)[1] == "."+extension]
    return extens
<|endoftext|>