<commit_before>"""
Multiple Plugin Agent for the New Relic Platform

"""
import helper
import importlib
import json
import logging
import os
import requests
import socket
import sys
import Queue as queue
import threading
import time

from newrelic_plugin_agent import __version__
from newrelic_plugin_agent import plugins

LOGGER = logging.getLogger(__name__)

class NewRelicPluginAgent(helper.Controller):
    """The NewRelicPluginAgent class implements a agent that polls plugins
    every minute and reports the state to NewRelic.

    """
    IGNORE_KEYS = ['license_key', 'poll_interval', 'proxy', 'endpoint']
    MAX_METRICS_PER_REQUEST = 10000
    PLATFORM_URL = 'https://platform-api.newrelic.com/platform/v1/metrics'
    WAKE_INTERVAL = 60

    def __init__(self, args, operating_system):
        """Initialize the NewRelicPluginAgent object.

        :param argparse.Namespace args: Command line arguments
        :param str operating_system: The operating_system name

        """
        super(NewRelicPluginAgent, self).__init__(args, operating_system)
        self.derive_last_interval = dict()
        self.endpoint = self.PLATFORM_URL
        self.http_headers = {'Accept': 'application/json',
                             'Content-Type': 'application/json'}
        self.last_interval_start = None
        self.min_max_values = dict()
        self._wake_interval = (self.config.application.get('wake_interval') or
                               self.config.application.get('poll_interval') or
                               self.WAKE_INTERVAL)
        self.next_wake_interval = self._wake_interval
        self.publish_queue = queue.Queue()
        self.threads = list()
        info = tuple([__version__] + list(self.system_platform))
        LOGGER.info('Agent v%s initialized, %s %s v%s', *info)

    def setup(self):
        """Setup the internal state for the controller class. This is invoked
        on Controller.run().

        Items requiring the configuration object should be assigned here due to
        startup order of operations.

        """
        if hasattr(self.config.application, 'endpoint'):
            self.endpoint = self.config.application.endpoint
        self.http_headers['X-License-Key'] = self.license_key
        self.last_interval_start = time.time()

    @property
    def agent_data(self):
        """Return the agent data section of the NewRelic Platform data payload

        :rtype: dict

        """
        return {'host': socket.gethostname(),
                'pid': os.getpid(),
                'version': __version__}

    @property
    def license_key(self):
        """Return the NewRelic license key from the configuration values.

        :rtype: str

        """
        return self.config.application.license_key

    def poll_plugin(self, plugin_name, plugin, config):
        """Kick off a background thread to run the processing task.

        :param newrelic_plugin_agent.plugins.base.Plugin plugin: The plugin
        :param dict config: The config for the plugin

        """

        if not isinstance(config, (list, tuple)):
            config = [config]

        for instance in config:
            thread = threading.Thread(target=self.thread_process,
                                      kwargs={'config': instance,
                                              'name': plugin_name,
                                              'plugin': plugin,
                                              'poll_interval':
                                                  self.wake_interval})
            thread.run()
            self.threads.append(thread)

    def process(self):
        """This method is called after every sleep interval. If the intention
        is to use an IOLoop instead of sleep interval based daemon, override
        the run method.

        """
        start_time = time.time()
        self.start_plugin_polling()
        while self.threads_running:
            self._sleep()
        self.threads = list()
        self.send_data_to_newrelic()
        duration = time.time() - start_time
        self.next_wake_interval = self._wake_interval - duration
        if self.next_wake_interval < 0:
            LOGGER.warning('Poll interval took greater than %i seconds',
                           duration)
            self.next_wake_interval = self._wake_interval
        LOGGER.info('All stats processed in %.2f seconds, next wake in %.2f',
                    duration, self.next_wake_interval)

    def process_min_max_values(self, component):
        """Agent keeps track of previous values, so compute the differences for
        min/max values.

        :param dict component: The component to calc min/max values for

        """
        guid = component['guid']
        name = component['name']

        if guid not in self.min_max_values.keys():
            self.min_max_values[guid] = dict()

        if name not in self.min_max_values[guid].keys():
            self.min_max_values[guid][name] = dict()

        for metric in component['metrics']:
            min_val, max_val = self.min_max_values[guid][name].get(metric,
                                                                   (None, None))
            value = component['metrics'][metric]['total']
            if min_val is not None and min_val > value:
                min_val = value

            if max_val is None or max_val < value:
                max_val = value

            if component['metrics'][metric]['min'] is None:
                component['metrics'][metric]['min'] = min_val or value

            if component['metrics'][metric]['max'] is None:
                component['metrics'][metric]['max'] = max_val

            self.min_max_values[guid][name][metric] = min_val, max_val

    @property
    def proxies(self):
        """Return the proxy used to access NewRelic.

        :rtype: dict

        """
        if 'proxy' in self.config.application:
            return {
                'http': self.config.application['proxy'],
                'https': self.config.application['proxy']
            }
        return None

    def send_data_to_newrelic(self):
        metrics = 0
        components = list()
        while self.publish_queue.qsize():
            (name, data, last_values) = self.publish_queue.get()
            self.derive_last_interval[name] = last_values
            if isinstance(data, list):
                for component in data:
                    self.process_min_max_values(component)
                    metrics += len(component['metrics'].keys())
                    if metrics >= self.MAX_METRICS_PER_REQUEST:
                        self.send_components(components, metrics)
                        components = list()
                        metrics = 0
                    components.append(component)

            elif isinstance(data, dict):
                self.process_min_max_values(data)
                metrics += len(data['metrics'].keys())
                if metrics >= self.MAX_METRICS_PER_REQUEST:
                    self.send_components(components, metrics)
                    components = list()
                    metrics = 0
                components.append(data)

        LOGGER.debug('Done, will send remainder of %i metrics', metrics)
        self.send_components(components, metrics)

    def send_components(self, components, metrics):
        """Create the headers and payload to send to NewRelic platform as a
        JSON encoded POST body.

        """
        if not metrics:
            LOGGER.warning('No metrics to send to NewRelic this interval')
            return

        LOGGER.info('Sending %i metrics to NewRelic', metrics)
        body = {'agent': self.agent_data, 'components': components}
        LOGGER.debug(body)
        try:
            response = requests.post(self.endpoint,
                                     headers=self.http_headers,
                                     proxies=self.proxies,
                                     data=json.dumps(body, ensure_ascii=False),
                                     verify=self.config.get('verify_ssl_cert',
                                                            True))
            LOGGER.debug('Response: %s: %r',
                         response.status_code,
                         response.content.strip())
        except requests.ConnectionError as error:
            LOGGER.error('Error reporting stats: %s', error)

    def _get_plugin(self, plugin_path):
        """Given a qualified class name (eg. foo.bar.Foo), return the class

        :rtype: object

        """
        try:
            package, class_name = plugin_path.rsplit('.', 1)
        except ValueError:
            return None

        try:
            module_handle = importlib.import_module(package)
            class_handle = getattr(module_handle, class_name)
            return class_handle
        except ImportError:
            LOGGER.exception('Attempting to import %s', plugin_path)
            return None

    def start_plugin_polling(self):
        """Iterate through each plugin and start the polling process."""
        for plugin in [key for key in self.config.application.keys()
                       if key not in self.IGNORE_KEYS]:
            LOGGER.info('Enabling plugin: %s', plugin)
            plugin_class = None

            # If plugin is part of the core agent plugin list
            if plugin in plugins.available:
                plugin_class = self._get_plugin(plugins.available[plugin])

            # If plugin is in config and a qualified class name
            elif '.' in plugin:
                plugin_class = self._get_plugin(plugin)

            # If plugin class could not be imported
            if not plugin_class:
                LOGGER.error('Enabled plugin %s not available', plugin)
                continue

            self.poll_plugin(plugin, plugin_class,
                             self.config.application.get(plugin))

    @property
    def threads_running(self):
        """Return True if any of the child threads are alive

        :rtype: bool

        """
        for thread in self.threads:
            if thread.is_alive():
                return True
        return False

    def thread_process(self, name, plugin, config, poll_interval):
        """Created a thread process for the given name, plugin class,
        config and poll interval. Process is added to a Queue object which
        used to maintain the stack of running plugins.

        :param str name: The name of the plugin
        :param newrelic_plugin_agent.plugin.Plugin plugin: The plugin class
        :param dict config: The plugin configuration
        :param int poll_interval: How often the plugin is invoked

        """
        instance_name = "%s:%s" % (name, config.get('name', 'unnamed'))
        obj = plugin(config, poll_interval,
                     self.derive_last_interval.get(instance_name))
        obj.poll()
        self.publish_queue.put((instance_name, obj.values(),
                                obj.derive_last_interval))

    @property
    def wake_interval(self):
        """Return the wake interval in seconds as the number of seconds
        until the next minute.

        :rtype: int

        """
        return self.next_wake_interval

def main():
    helper.parser.description('The NewRelic Plugin Agent polls various '
                              'services and sends the data to the NewRelic '
                              'Platform')
    helper.parser.name('newrelic_plugin_agent')
    argparse = helper.parser.get()
    argparse.add_argument('-C',
                          action='store_true',
                          dest='configure',
                          help='Run interactive configuration')
    args = helper.parser.parse()
    if args.configure:
        print('Configuration')
        sys.exit(0)
    helper.start(NewRelicPluginAgent)

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    main()
<commit_msg>Fix the wake interval issue with calling _sleep (which kicks off another wake interval timer)<commit_after>"""
Multiple Plugin Agent for the New Relic Platform

"""
import helper
import importlib
import json
import logging
import os
import requests
import socket
import sys
import Queue as queue
import threading
import time

from newrelic_plugin_agent import __version__
from newrelic_plugin_agent import plugins

LOGGER = logging.getLogger(__name__)

class NewRelicPluginAgent(helper.Controller):
    """The NewRelicPluginAgent class implements a agent that polls plugins
    every minute and reports the state to NewRelic.

    """
    IGNORE_KEYS = ['license_key', 'poll_interval', 'proxy', 'endpoint']
    MAX_METRICS_PER_REQUEST = 10000
    PLATFORM_URL = 'https://platform-api.newrelic.com/platform/v1/metrics'
    WAKE_INTERVAL = 60

    def __init__(self, args, operating_system):
        """Initialize the NewRelicPluginAgent object.

        :param argparse.Namespace args: Command line arguments
        :param str operating_system: The operating_system name

        """
        super(NewRelicPluginAgent, self).__init__(args, operating_system)
        self.derive_last_interval = dict()
        self.endpoint = self.PLATFORM_URL
        self.http_headers = {'Accept': 'application/json',
                             'Content-Type': 'application/json'}
        self.last_interval_start = None
        self.min_max_values = dict()
        self._wake_interval = (self.config.application.get('wake_interval') or
                               self.config.application.get('poll_interval') or
                               self.WAKE_INTERVAL)
        self.next_wake_interval = int(self._wake_interval)
        self.publish_queue = queue.Queue()
        self.threads = list()
        info = tuple([__version__] + list(self.system_platform))
        LOGGER.info('Agent v%s initialized, %s %s v%s', *info)

    def setup(self):
        """Setup the internal state for the controller class. This is invoked
        on Controller.run().

        Items requiring the configuration object should be assigned here due to
        startup order of operations.

        """
        if hasattr(self.config.application, 'endpoint'):
            self.endpoint = self.config.application.endpoint
        self.http_headers['X-License-Key'] = self.license_key
        self.last_interval_start = time.time()

    @property
    def agent_data(self):
        """Return the agent data section of the NewRelic Platform data payload

        :rtype: dict

        """
        return {'host': socket.gethostname(),
                'pid': os.getpid(),
                'version': __version__}

    @property
    def license_key(self):
        """Return the NewRelic license key from the configuration values.

        :rtype: str

        """
        return self.config.application.license_key

    def poll_plugin(self, plugin_name, plugin, config):
        """Kick off a background thread to run the processing task.

        :param newrelic_plugin_agent.plugins.base.Plugin plugin: The plugin
        :param dict config: The config for the plugin

        """

        if not isinstance(config, (list, tuple)):
            config = [config]

        for instance in config:
            thread = threading.Thread(target=self.thread_process,
                                      kwargs={'config': instance,
                                              'name': plugin_name,
                                              'plugin': plugin,
                                              'poll_interval':
                                                  int(self._wake_interval)})
            thread.run()
            self.threads.append(thread)

    def process(self):
        """This method is called after every sleep interval. If the intention
        is to use an IOLoop instead of sleep interval based daemon, override
        the run method.

        """
        start_time = time.time()
        self.start_plugin_polling()

        # Sleep for a second while threads are running
        while self.threads_running:
            time.sleep(1)

        self.threads = list()
        self.send_data_to_newrelic()
        duration = time.time() - start_time
        self.next_wake_interval = self._wake_interval - duration
        if self.next_wake_interval < 1:
            LOGGER.warning('Poll interval took greater than %i seconds',
                           duration)
            self.next_wake_interval = int(self._wake_interval)
        LOGGER.info('Stats processed in %.2f seconds, next wake in %i seconds',
                    duration, self.next_wake_interval)

    def process_min_max_values(self, component):
        """Agent keeps track of previous values, so compute the differences for
        min/max values.

        :param dict component: The component to calc min/max values for

        """
        guid = component['guid']
        name = component['name']

        if guid not in self.min_max_values.keys():
            self.min_max_values[guid] = dict()

        if name not in self.min_max_values[guid].keys():
            self.min_max_values[guid][name] = dict()

        for metric in component['metrics']:
            min_val, max_val = self.min_max_values[guid][name].get(metric,
                                                                   (None, None))
            value = component['metrics'][metric]['total']
            if min_val is not None and min_val > value:
                min_val = value

            if max_val is None or max_val < value:
                max_val = value

            if component['metrics'][metric]['min'] is None:
                component['metrics'][metric]['min'] = min_val or value

            if component['metrics'][metric]['max'] is None:
                component['metrics'][metric]['max'] = max_val

            self.min_max_values[guid][name][metric] = min_val, max_val

    @property
    def proxies(self):
        """Return the proxy used to access NewRelic.

        :rtype: dict

        """
        if 'proxy' in self.config.application:
            return {
                'http': self.config.application['proxy'],
                'https': self.config.application['proxy']
            }
        return None

    def send_data_to_newrelic(self):
        metrics = 0
        components = list()
        while self.publish_queue.qsize():
            (name, data, last_values) = self.publish_queue.get()
            self.derive_last_interval[name] = last_values
            if isinstance(data, list):
                for component in data:
                    self.process_min_max_values(component)
                    metrics += len(component['metrics'].keys())
                    if metrics >= self.MAX_METRICS_PER_REQUEST:
                        self.send_components(components, metrics)
                        components = list()
                        metrics = 0
                    components.append(component)

            elif isinstance(data, dict):
                self.process_min_max_values(data)
                metrics += len(data['metrics'].keys())
                if metrics >= self.MAX_METRICS_PER_REQUEST:
                    self.send_components(components, metrics)
                    components = list()
                    metrics = 0
                components.append(data)

        LOGGER.debug('Done, will send remainder of %i metrics', metrics)
        self.send_components(components, metrics)

    def send_components(self, components, metrics):
        """Create the headers and payload to send to NewRelic platform as a
        JSON encoded POST body.

        """
        if not metrics:
            LOGGER.warning('No metrics to send to NewRelic this interval')
            return

        LOGGER.info('Sending %i metrics to NewRelic', metrics)
        body = {'agent': self.agent_data, 'components': components}
        LOGGER.debug(body)
        try:
            response = requests.post(self.endpoint,
                                     headers=self.http_headers,
                                     proxies=self.proxies,
                                     data=json.dumps(body, ensure_ascii=False),
                                     verify=self.config.get('verify_ssl_cert',
                                                            True))
            LOGGER.debug('Response: %s: %r',
                         response.status_code,
                         response.content.strip())
        except requests.ConnectionError as error:
            LOGGER.error('Error reporting stats: %s', error)

    @staticmethod
    def _get_plugin(plugin_path):
        """Given a qualified class name (eg. foo.bar.Foo), return the class

        :rtype: object

        """
        try:
            package, class_name = plugin_path.rsplit('.', 1)
        except ValueError:
            return None

        try:
            module_handle = importlib.import_module(package)
            class_handle = getattr(module_handle, class_name)
            return class_handle
        except ImportError:
            LOGGER.exception('Attempting to import %s', plugin_path)
            return None

    def start_plugin_polling(self):
        """Iterate through each plugin and start the polling process."""
        for plugin in [key for key in self.config.application.keys()
                       if key not in self.IGNORE_KEYS]:
            LOGGER.info('Enabling plugin: %s', plugin)
            plugin_class = None

            # If plugin is part of the core agent plugin list
            if plugin in plugins.available:
                plugin_class = self._get_plugin(plugins.available[plugin])

            # If plugin is in config and a qualified class name
            elif '.' in plugin:
                plugin_class = self._get_plugin(plugin)

            # If plugin class could not be imported
            if not plugin_class:
                LOGGER.error('Enabled plugin %s not available', plugin)
                continue

            self.poll_plugin(plugin, plugin_class,
                             self.config.application.get(plugin))

    @property
    def threads_running(self):
        """Return True if any of the child threads are alive

        :rtype: bool

        """
        for thread in self.threads:
            if thread.is_alive():
                return True
        return False

    def thread_process(self, name, plugin, config, poll_interval):
        """Created a thread process for the given name, plugin class,
        config and poll interval. Process is added to a Queue object which
        used to maintain the stack of running plugins.

        :param str name: The name of the plugin
        :param newrelic_plugin_agent.plugin.Plugin plugin: The plugin class
        :param dict config: The plugin configuration
        :param int poll_interval: How often the plugin is invoked

        """
        instance_name = "%s:%s" % (name, config.get('name', 'unnamed'))
        obj = plugin(config, poll_interval,
                     self.derive_last_interval.get(instance_name))
        obj.poll()
        self.publish_queue.put((instance_name, obj.values(),
                                obj.derive_last_interval))

    @property
    def wake_interval(self):
        """Return the wake interval in seconds as the number of seconds
        until the next minute.

        :rtype: int

        """
        return self.next_wake_interval

def main():
    helper.parser.description('The NewRelic Plugin Agent polls various '
                              'services and sends the data to the NewRelic '
                              'Platform')
    helper.parser.name('newrelic_plugin_agent')
    argparse = helper.parser.get()
    argparse.add_argument('-C',
                          action='store_true',
                          dest='configure',
                          help='Run interactive configuration')
    args = helper.parser.parse()
    if args.configure:
        print('Configuration')
        sys.exit(0)
    helper.start(NewRelicPluginAgent)

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    main()
<|endoftext|>


<commit_before>import wx
import os

class TutorialView(wx.Panel):
    def __init__(self, parent, size, name, musicPath="TwoMandolins.mp3"):
        wx.Panel.__init__(self, size=size, parent=parent)
        self.Bind(wx.EVT_SHOW, self.onShow, self)
        self.parent = parent
        self.name = name
        self.size = size
        self.musicPath = musicPath
        self.initButtons()
        self.SetBackgroundColour((255, 255, 255))
        self.pageID = 0
        self.tutorial_info = [
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus eu sapien non felis mattis\n " \
            "luctus. Nulla sed sapien neque. Mauris vitae urna ac tellus cursus efficitur et egestas turpis.\n" \
            " Etiam vel justo scelerisque, tincidunt dui ac, iaculis felis. In hac habitasse platea dictum\n" \
            "st. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Ut \n" \
            "ante metus, vestibulum nec commodo ac, eleifend porta tortor. Etiam diam orci, luctus ac alique\n" \
            "t at, porttitor nec risus. Fusce fermentum lacinia mauris, eu hendrerit ligula convallis non. \n" \
            "Quisque faucibus tellus ac lacus fringilla malesuada. Nulla id neque eget nisi vulputate accum\n" \
            "san. Quisque euismod metus pretium justo imperdiet iaculis. Cras a ante nisi. Aliquam erat vol\n" \
            "utpat. Nulla rutrum ut velit a placerat. Maecenas laoreet ornare lacinia. Cras ultrices mi nis\n" \
            "i. Etiam euismod et magna ac mattis. Cras et quam dictum, lobortis mauris eget, pretium metus\n" \
            ". Integer pulvinar, sem non suscipit congue, erat ipsum tincidunt mi, et aliquam lectus leo eu\n" \
            " mi. Fusce blandit metus at odio consequat, a suscipit urna pharetra. Sed ullamcorper orci id \n" \
            "lacinia bibendum.",
            "Suspendisse accumsan tincidunt sagittis. Etiam tempor lacus id ante interdum, vitae faucibus \n"
            "nulla aliquet. Donec enim risus, tincidunt eu est nec, suscipit imperdiet nisi. Quisque \n"
            "laoreet magna consectetur porta gravida. Nullam id felis sapien. Suspendisse orci mi, commodo\n"
            " in lorem quis, tincidunt vehicula metus. Morbi laoreet, lectus eu blandit pharetra, tortor\n"
            " ante gravida turpis, quis mollis nisi mi vitae massa. Duis tincidunt nisi a nisi luctus tempus.\n"
            " Vivamus euismod a tortor at tempus.",
            "Aliquam pulvinar ac dui ut iaculis. Nullam ut rutrum odio. Etiam accumsan in dui in auctor. Praesent \n"
            "cursus lacus nec nisl blandit ullamcorper. Proin non est efficitur ligula ultrices pellentesque. Cras \n"
            "aliquet, ante varius commodo fermentum, nisi elit dictum lacus, ac feugiat libero felis eu mi. Nulla \n"
            "porttitor faucibus dui quis gravida. Suspendisse potenti."
            ]

        self.centerSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.ctrlMsgField = wx.StaticText(self, label=self.tutorial_info[self.pageID])
        self.centerSizer.Add(self.ctrlMsgField, 0, wx.EXPAND, 5)

    def onShow(self, event):
        # print "Menu on show"
        global pygame
        if event.GetShow():
            # print "menu: setting up music"
            import pygame
            pygame.init()
            pygame.mixer.init()
            pygame.mixer.music.load(self.musicPath)
            pygame.mixer.music.play()
        else:
            try:
                # print "Menu, quitting"
                pygame.quit()
            except Exception:
                # print "menu: problem with pygame quit"
                pass

    def initButtons(self):
        """ This function creates buttons, sets theirs positions and size and
            binds logic to them."""
        menu_btn = wx.Button(self, label="Menu", pos=(130, 225), size=(60, 30))
        next_btn = wx.Button(self, label="Next", pos=(100, 195), size=(60, 30))
        prev_btn = wx.Button(self, label="Prev", pos=(160, 195), size=(60, 30))
        self.Bind(wx.EVT_BUTTON, self.retToMenu, menu_btn)
        self.Bind(wx.EVT_BUTTON, self.nextPage, next_btn)
        self.Bind(wx.EVT_BUTTON, self.prevPage, prev_btn)

    def nextPage(self, event):
        """ This function displays next page of tutorial """
        if self.pageID + 1 < self.tutorial_info.__len__():
            self.pageID += 1
            self.ctrlMsgField.Destroy()
            self.Refresh()
            self.centerSizer = wx.BoxSizer(wx.HORIZONTAL)
            self.ctrlMsgField = wx.StaticText(self, label=self.tutorial_info[self.pageID])
            self.centerSizer.Add(self.ctrlMsgField, 0, wx.EXPAND, 5)

    def prevPage(self, event):
        """ This function displays previous page of tutorial """
        if self.pageID > 0:
            self.pageID -= 1
            self.ctrlMsgField.Destroy()
            self.Refresh()
            self.centerSizer = wx.BoxSizer(wx.HORIZONTAL)
            self.ctrlMsgField = wx.StaticText(self, label=self.tutorial_info[self.pageID])
            self.centerSizer.Add(self.ctrlMsgField, 0, wx.EXPAND, 5)

    def retToMenu(self, event):
        """ This function returns to Menu view """
        self.parent.setView("Menu")

    def initMenuBar(self):
        status = self.CreateStatusBar()
        menuBar = wx.MenuBar()

        first = wx.Menu()
        second = wx.Menu()

        first.Append(wx.NewId(), "New window", "This is a new Window")
        first.Append(wx.NewId(), "Open...", "This will open a new Window")

        menuBar.Append(first, "File")
        menuBar.Append(second, "Edit")

        self.SetMenuBar(menuBar)
<commit_msg>Switched positions of next / prev page in TutorialView.<commit_after>import wx
import os

class TutorialView(wx.Panel):
    def __init__(self, parent, size, name, musicPath="TwoMandolins.mp3"):
        wx.Panel.__init__(self, size=size, parent=parent)
        self.Bind(wx.EVT_SHOW, self.onShow, self)
        self.parent = parent
        self.name = name
        self.size = size
        self.musicPath = musicPath
        self.initButtons()
        self.SetBackgroundColour((255, 255, 255))
        self.pageID = 0
        self.tutorial_info = [
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus eu sapien non felis mattis\n " \
            "luctus. Nulla sed sapien neque. Mauris vitae urna ac tellus cursus efficitur et egestas turpis.\n" \
            " Etiam vel justo scelerisque, tincidunt dui ac, iaculis felis. In hac habitasse platea dictum\n" \
            "st. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Ut \n" \
            "ante metus, vestibulum nec commodo ac, eleifend porta tortor. Etiam diam orci, luctus ac alique\n" \
            "t at, porttitor nec risus. Fusce fermentum lacinia mauris, eu hendrerit ligula convallis non. \n" \
            "Quisque faucibus tellus ac lacus fringilla malesuada. Nulla id neque eget nisi vulputate accum\n" \
            "san. Quisque euismod metus pretium justo imperdiet iaculis. Cras a ante nisi. Aliquam erat vol\n" \
            "utpat. Nulla rutrum ut velit a placerat. Maecenas laoreet ornare lacinia. Cras ultrices mi nis\n" \
            "i. Etiam euismod et magna ac mattis. Cras et quam dictum, lobortis mauris eget, pretium metus\n" \
            ". Integer pulvinar, sem non suscipit congue, erat ipsum tincidunt mi, et aliquam lectus leo eu\n" \
            " mi. Fusce blandit metus at odio consequat, a suscipit urna pharetra. Sed ullamcorper orci id \n" \
            "lacinia bibendum.",
            "Suspendisse accumsan tincidunt sagittis. Etiam tempor lacus id ante interdum, vitae faucibus \n"
            "nulla aliquet. Donec enim risus, tincidunt eu est nec, suscipit imperdiet nisi. Quisque \n"
            "laoreet magna consectetur porta gravida. Nullam id felis sapien. Suspendisse orci mi, commodo\n"
            " in lorem quis, tincidunt vehicula metus. Morbi laoreet, lectus eu blandit pharetra, tortor\n"
            " ante gravida turpis, quis mollis nisi mi vitae massa. Duis tincidunt nisi a nisi luctus tempus.\n"
            " Vivamus euismod a tortor at tempus.",
            "Aliquam pulvinar ac dui ut iaculis. Nullam ut rutrum odio. Etiam accumsan in dui in auctor. Praesent \n"
            "cursus lacus nec nisl blandit ullamcorper. Proin non est efficitur ligula ultrices pellentesque. Cras \n"
            "aliquet, ante varius commodo fermentum, nisi elit dictum lacus, ac feugiat libero felis eu mi. Nulla \n"
            "porttitor faucibus dui quis gravida. Suspendisse potenti."
            ]

        self.centerSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.ctrlMsgField = wx.StaticText(self, label=self.tutorial_info[self.pageID])
        self.centerSizer.Add(self.ctrlMsgField, 0, wx.EXPAND, 5)

    def onShow(self, event):
        # print "Menu on show"
        global pygame
        if event.GetShow():
            # print "menu: setting up music"
            import pygame
            pygame.init()
            pygame.mixer.init()
            pygame.mixer.music.load(self.musicPath)
            pygame.mixer.music.play()
        else:
            try:
                # print "Menu, quitting"
                pygame.quit()
            except Exception:
                # print "menu: problem with pygame quit"
                pass

    def initButtons(self):
        """ This function creates buttons, sets theirs positions and size and
            binds logic to them."""
        menu_btn = wx.Button(self, label="Menu", pos=(130, 225), size=(60, 30))
        next_btn = wx.Button(self, label="Next", pos=(160, 195), size=(60, 30))
        prev_btn = wx.Button(self, label="Prev", pos=(100, 195), size=(60, 30))
        self.Bind(wx.EVT_BUTTON, self.retToMenu, menu_btn)
        self.Bind(wx.EVT_BUTTON, self.nextPage, next_btn)
        self.Bind(wx.EVT_BUTTON, self.prevPage, prev_btn)

    def nextPage(self, event):
        """ This function displays next page of tutorial """
        if self.pageID + 1 < self.tutorial_info.__len__():
            self.pageID += 1
            self.ctrlMsgField.Destroy()
            self.Refresh()
            self.centerSizer = wx.BoxSizer(wx.HORIZONTAL)
            self.ctrlMsgField = wx.StaticText(self, label=self.tutorial_info[self.pageID])
            self.centerSizer.Add(self.ctrlMsgField, 0, wx.EXPAND, 5)

    def prevPage(self, event):
        """ This function displays previous page of tutorial """
        if self.pageID > 0:
            self.pageID -= 1
            self.ctrlMsgField.Destroy()
            self.Refresh()
            self.centerSizer = wx.BoxSizer(wx.HORIZONTAL)
            self.ctrlMsgField = wx.StaticText(self, label=self.tutorial_info[self.pageID])
            self.centerSizer.Add(self.ctrlMsgField, 0, wx.EXPAND, 5)

    def retToMenu(self, event):
        """ This function returns to Menu view """
        self.parent.setView("Menu")

    def initMenuBar(self):
        status = self.CreateStatusBar()
        menuBar = wx.MenuBar()

        first = wx.Menu()
        second = wx.Menu()

        first.Append(wx.NewId(), "New window", "This is a new Window")
        first.Append(wx.NewId(), "Open...", "This will open a new Window")

        menuBar.Append(first, "File")
        menuBar.Append(second, "Edit")

        self.SetMenuBar(menuBar)
<|endoftext|>


<commit_before># Natural Language Toolkit: Corpus Readers
#
# Copyright (C) 2001-2015 NLTK Project
# Author: Steven Bird <stevenbird1@gmail.com>
#         Edward Loper <edloper@gmail.com>
# URL: <http://nltk.org/>
# For license information, see LICENSE.TXT

"""
NLTK corpus readers.  The modules in this package provide functions
that can be used to read corpus fileids in a variety of formats.  These
functions can be used to read both the corpus fileids that are
distributed in the NLTK corpus package, and corpus fileids that are part
of external corpora.

Corpus Reader Functions
=======================
Each corpus module defines one or more "corpus reader functions",
which can be used to read documents from that corpus.  These functions
take an argument, ``item``, which is used to indicate which document
should be read from the corpus:

- If ``item`` is one of the unique identifiers listed in the corpus
  module's ``items`` variable, then the corresponding document will
  be loaded from the NLTK corpus package.
- If ``item`` is a fileid, then that file will be read.

Additionally, corpus reader functions can be given lists of item
names; in which case, they will return a concatenation of the
corresponding documents.

Corpus reader functions are named based on the type of information
they return.  Some common examples, and their return types, are:

- words(): list of str
- sents(): list of (list of str)
- paras(): list of (list of (list of str))
- tagged_words(): list of (str,str) tuple
- tagged_sents(): list of (list of (str,str))
- tagged_paras(): list of (list of (list of (str,str)))
- chunked_sents(): list of (Tree w/ (str,str) leaves)
- parsed_sents(): list of (Tree with str leaves)
- parsed_paras(): list of (list of (Tree with str leaves))
- xml(): A single xml ElementTree
- raw(): unprocessed corpus contents

For example, to read a list of the words in the Brown Corpus, use
``nltk.corpus.brown.words()``:

    >>> from nltk.corpus import brown
    >>> print(", ".join(brown.words()))
    The, Fulton, County, Grand, Jury, said, ...

"""

from nltk.corpus.reader.plaintext import *
from nltk.corpus.reader.util import *
from nltk.corpus.reader.api import *
from nltk.corpus.reader.tagged import *
from nltk.corpus.reader.cmudict import *
from nltk.corpus.reader.conll import *
from nltk.corpus.reader.chunked import *
from nltk.corpus.reader.wordlist import *
from nltk.corpus.reader.xmldocs import *
from nltk.corpus.reader.ppattach import *
from nltk.corpus.reader.senseval import *
from nltk.corpus.reader.ieer import *
from nltk.corpus.reader.sinica_treebank import *
from nltk.corpus.reader.bracket_parse import *
from nltk.corpus.reader.indian import *
from nltk.corpus.reader.toolbox import *
from nltk.corpus.reader.timit import *
from nltk.corpus.reader.ycoe import *
from nltk.corpus.reader.rte import *
from nltk.corpus.reader.string_category import *
from nltk.corpus.reader.propbank import *
from nltk.corpus.reader.verbnet import *
from nltk.corpus.reader.bnc import *
from nltk.corpus.reader.nps_chat import *
from nltk.corpus.reader.wordnet import *
from nltk.corpus.reader.switchboard import *
from nltk.corpus.reader.dependency import *
from nltk.corpus.reader.nombank import *
from nltk.corpus.reader.ipipan import *
from nltk.corpus.reader.pl196x import *
from nltk.corpus.reader.knbc import *
from nltk.corpus.reader.chasen import *
from nltk.corpus.reader.childes import *
from nltk.corpus.reader.aligned import *
from nltk.corpus.reader.lin import *
from nltk.corpus.reader.semcor import *
from nltk.corpus.reader.framenet import *
from nltk.corpus.reader.udhr import *
from nltk.corpus.reader.bnc import *
from nltk.corpus.reader.sentiwordnet import *
from nltk.corpus.reader.twitter import *
from nltk.corpus.reader.nkjp import *
from nltk.corpus.reader.crubadan import *
from nltk.corpus.reader.product_reviews import *

# Make sure that nltk.corpus.reader.bracket_parse gives the module, not
# the function bracket_parse() defined in nltk.tree:
from nltk.corpus.reader import bracket_parse

__all__ = [
    'CorpusReader', 'CategorizedCorpusReader',
    'PlaintextCorpusReader', 'find_corpus_fileids',
    'TaggedCorpusReader', 'CMUDictCorpusReader',
    'ConllChunkCorpusReader', 'WordListCorpusReader',
    'PPAttachmentCorpusReader', 'SensevalCorpusReader',
    'IEERCorpusReader', 'ChunkedCorpusReader',
    'SinicaTreebankCorpusReader', 'BracketParseCorpusReader',
    'IndianCorpusReader', 'ToolboxCorpusReader',
    'TimitCorpusReader', 'YCOECorpusReader',
    'MacMorphoCorpusReader', 'SyntaxCorpusReader',
    'AlpinoCorpusReader', 'RTECorpusReader',
    'StringCategoryCorpusReader','EuroparlCorpusReader',
    'CategorizedBracketParseCorpusReader',
    'CategorizedTaggedCorpusReader',
    'CategorizedPlaintextCorpusReader',
    'PortugueseCategorizedPlaintextCorpusReader',
    'tagged_treebank_para_block_reader',
    'PropbankCorpusReader', 'VerbnetCorpusReader',
    'BNCCorpusReader', 'ConllCorpusReader',
    'XMLCorpusReader', 'NPSChatCorpusReader',
    'SwadeshCorpusReader', 'WordNetCorpusReader',
    'WordNetICCorpusReader', 'SwitchboardCorpusReader',
    'DependencyCorpusReader', 'NombankCorpusReader',
    'IPIPANCorpusReader', 'Pl196xCorpusReader',
    'TEICorpusView', 'KNBCorpusReader', 'ChasenCorpusReader',
    'CHILDESCorpusReader', 'AlignedCorpusReader',
    'TimitTaggedCorpusReader', 'LinThesaurusCorpusReader',
    'SemcorCorpusReader', 'FramenetCorpusReader', 'UdhrCorpusReader',
    'BNCCorpusReader', 'SentiWordNetCorpusReader', 'SentiSynset',
    'TwitterCorpusReader', 'NKJPCorpusReader', 'CrubadanCorpusReader',
    'ProductReviewsCorpusReader'
]
<commit_msg>add ProsConsCorpusReader and OpinionLexiconCorpusReader to init<commit_after># Natural Language Toolkit: Corpus Readers
#
# Copyright (C) 2001-2015 NLTK Project
# Author: Steven Bird <stevenbird1@gmail.com>
#         Edward Loper <edloper@gmail.com>
# URL: <http://nltk.org/>
# For license information, see LICENSE.TXT

"""
NLTK corpus readers.  The modules in this package provide functions
that can be used to read corpus fileids in a variety of formats.  These
functions can be used to read both the corpus fileids that are
distributed in the NLTK corpus package, and corpus fileids that are part
of external corpora.

Corpus Reader Functions
=======================
Each corpus module defines one or more "corpus reader functions",
which can be used to read documents from that corpus.  These functions
take an argument, ``item``, which is used to indicate which document
should be read from the corpus:

- If ``item`` is one of the unique identifiers listed in the corpus
  module's ``items`` variable, then the corresponding document will
  be loaded from the NLTK corpus package.
- If ``item`` is a fileid, then that file will be read.

Additionally, corpus reader functions can be given lists of item
names; in which case, they will return a concatenation of the
corresponding documents.

Corpus reader functions are named based on the type of information
they return.  Some common examples, and their return types, are:

- words(): list of str
- sents(): list of (list of str)
- paras(): list of (list of (list of str))
- tagged_words(): list of (str,str) tuple
- tagged_sents(): list of (list of (str,str))
- tagged_paras(): list of (list of (list of (str,str)))
- chunked_sents(): list of (Tree w/ (str,str) leaves)
- parsed_sents(): list of (Tree with str leaves)
- parsed_paras(): list of (list of (Tree with str leaves))
- xml(): A single xml ElementTree
- raw(): unprocessed corpus contents

For example, to read a list of the words in the Brown Corpus, use
``nltk.corpus.brown.words()``:

    >>> from nltk.corpus import brown
    >>> print(", ".join(brown.words()))
    The, Fulton, County, Grand, Jury, said, ...

"""

from nltk.corpus.reader.plaintext import *
from nltk.corpus.reader.util import *
from nltk.corpus.reader.api import *
from nltk.corpus.reader.tagged import *
from nltk.corpus.reader.cmudict import *
from nltk.corpus.reader.conll import *
from nltk.corpus.reader.chunked import *
from nltk.corpus.reader.wordlist import *
from nltk.corpus.reader.xmldocs import *
from nltk.corpus.reader.ppattach import *
from nltk.corpus.reader.senseval import *
from nltk.corpus.reader.ieer import *
from nltk.corpus.reader.sinica_treebank import *
from nltk.corpus.reader.bracket_parse import *
from nltk.corpus.reader.indian import *
from nltk.corpus.reader.toolbox import *
from nltk.corpus.reader.timit import *
from nltk.corpus.reader.ycoe import *
from nltk.corpus.reader.rte import *
from nltk.corpus.reader.string_category import *
from nltk.corpus.reader.propbank import *
from nltk.corpus.reader.verbnet import *
from nltk.corpus.reader.bnc import *
from nltk.corpus.reader.nps_chat import *
from nltk.corpus.reader.wordnet import *
from nltk.corpus.reader.switchboard import *
from nltk.corpus.reader.dependency import *
from nltk.corpus.reader.nombank import *
from nltk.corpus.reader.ipipan import *
from nltk.corpus.reader.pl196x import *
from nltk.corpus.reader.knbc import *
from nltk.corpus.reader.chasen import *
from nltk.corpus.reader.childes import *
from nltk.corpus.reader.aligned import *
from nltk.corpus.reader.lin import *
from nltk.corpus.reader.semcor import *
from nltk.corpus.reader.framenet import *
from nltk.corpus.reader.udhr import *
from nltk.corpus.reader.bnc import *
from nltk.corpus.reader.sentiwordnet import *
from nltk.corpus.reader.twitter import *
from nltk.corpus.reader.nkjp import *
from nltk.corpus.reader.crubadan import *
from nltk.corpus.reader.product_reviews import *
from nltk.corpus.reader.opinion_lexicon import *
from nltk.corpus.reader.pros_cons import *

# Make sure that nltk.corpus.reader.bracket_parse gives the module, not
# the function bracket_parse() defined in nltk.tree:
from nltk.corpus.reader import bracket_parse

__all__ = [
    'CorpusReader', 'CategorizedCorpusReader',
    'PlaintextCorpusReader', 'find_corpus_fileids',
    'TaggedCorpusReader', 'CMUDictCorpusReader',
    'ConllChunkCorpusReader', 'WordListCorpusReader',
    'PPAttachmentCorpusReader', 'SensevalCorpusReader',
    'IEERCorpusReader', 'ChunkedCorpusReader',
    'SinicaTreebankCorpusReader', 'BracketParseCorpusReader',
    'IndianCorpusReader', 'ToolboxCorpusReader',
    'TimitCorpusReader', 'YCOECorpusReader',
    'MacMorphoCorpusReader', 'SyntaxCorpusReader',
    'AlpinoCorpusReader', 'RTECorpusReader',
    'StringCategoryCorpusReader','EuroparlCorpusReader',
    'CategorizedBracketParseCorpusReader',
    'CategorizedTaggedCorpusReader',
    'CategorizedPlaintextCorpusReader',
    'PortugueseCategorizedPlaintextCorpusReader',
    'tagged_treebank_para_block_reader',
    'PropbankCorpusReader', 'VerbnetCorpusReader',
    'BNCCorpusReader', 'ConllCorpusReader',
    'XMLCorpusReader', 'NPSChatCorpusReader',
    'SwadeshCorpusReader', 'WordNetCorpusReader',
    'WordNetICCorpusReader', 'SwitchboardCorpusReader',
    'DependencyCorpusReader', 'NombankCorpusReader',
    'IPIPANCorpusReader', 'Pl196xCorpusReader',
    'TEICorpusView', 'KNBCorpusReader', 'ChasenCorpusReader',
    'CHILDESCorpusReader', 'AlignedCorpusReader',
    'TimitTaggedCorpusReader', 'LinThesaurusCorpusReader',
    'SemcorCorpusReader', 'FramenetCorpusReader', 'UdhrCorpusReader',
    'BNCCorpusReader', 'SentiWordNetCorpusReader', 'SentiSynset',
    'TwitterCorpusReader', 'NKJPCorpusReader', 'CrubadanCorpusReader',
    'ProductReviewsCorpusReader', 'OpinionLexiconCorpusReader',
    'ProsConsCorpusReader'
]
<|endoftext|>


<commit_before>import engine

VALID_COORDS = [(x, y) for x in xrange(97, 105) for y in xrange(49, 57)]
INVALID_COORDS = [
    (0, 0), (-1, -1),
    (96, 49), (96, 48),
    (105, 49), (104, 48),
    (96, 56), (97, 57),
    (105, 56), (104, 57)
    ]
VALID_A1 = [chr(x) + chr(y) for x in xrange(97, 105) for y in xrange(49, 57)]
INVALID_A1 = ['a0', 'a9', 'h0', 'h9', 'z1', 'z8']
EMPTY_BOARD = dict([((x, y), None) for x in xrange(97, 105) for y in xrange(49, 57)])

def test_coord_to_a1():
    u"""Assert the coord-to-a1 dictionary has only the desired keypairs."""
    for coord in VALID_COORDS:
        assert engine._coord_to_a1.get(coord, False) is not False
    for coord in INVALID_COORDS:
        assert engine._coord_to_a1.get(coord, False) is False

def test_a1_to_coord():
    u"""Assert the a1-to-coord dictionary has only the desired keypairs."""
    for a1 in VALID_A1:
        assert engine._a1_to_coord.get(a1, False) is not False
    for a1 in INVALID_A1:
        assert engine._a1_to_coord.get(a1, False) is False

def test_is_coord_on_board():
    u"""Assert that only valid coords are considered in the board."""
    for coord in VALID_COORDS:
        assert engine._is_coord_on_board(coord) is True
    for coord in INVALID_COORDS:
        assert engine._is_coord_on_board(coord) is False

def test_instantiate_Piece():
    u"""Assert an instance of Piece has the expected attributes."""
    for coord in VALID_COORDS:
        p = engine.Piece(coord)
        assert isinstance(p, engine.Piece)
        assert p.x, p.y == coord

def test_instantiate_SimpleUnit():
    u"""Assert an instance of SimpleUnit has the expected attributes."""
    for color in ['white', 'black']:
        for coord in VALID_COORDS:
            p = engine.SimpleUnit(coord, color)
            assert isinstance(p, engine.SimpleUnit)
            assert p.x, p.y == coord
            assert p.color == color

def test_SimpleUnit_possible_moves_empty_space():
    u"""Assert only valid moves are returned."""
    p = engine.SimpleUnit(VALID_COORDS[0], 'white')
    board = EMPTY_BOARD.copy()
    board[(p.x, p.y)] = p
    assert p.possible_moves(board) == [(p.x, p.y + 1)]

def test_SimpleUnit_possible_moves_into_ally_space():
    u"""Assert moves into allied units are not in returned move list."""
    p = engine.SimpleUnit(VALID_COORDS[0], 'white')
    q = engine.SimpleUnit(VALID_COORDS[1], 'white')
    board = EMPTY_BOARD.copy()
    board[(p.x, p.y)] = p
    board[(q.x, q.y)] = q
    assert p.possible_moves(board) == []

def test_SimpleUnit_possible_moves_off_board():
    u"""Assert that moves off the board are not in returned move list."""
    p = engine.SimpleUnit(VALID_COORDS[-1], 'white')
    board = EMPTY_BOARD.copy()
    board[(p.x, p.y)] = p
    assert p.possible_moves(board) == []
<commit_msg>Add test_SimpleUnit_possible_moves_into_enemy() to assert that moves into an enemy unit is in the returned move list.<commit_after>import engine

VALID_COORDS = [(x, y) for x in xrange(97, 105) for y in xrange(49, 57)]
INVALID_COORDS = [
    (0, 0), (-1, -1),
    (96, 49), (96, 48),
    (105, 49), (104, 48),
    (96, 56), (97, 57),
    (105, 56), (104, 57)
    ]
VALID_A1 = [chr(x) + chr(y) for x in xrange(97, 105) for y in xrange(49, 57)]
INVALID_A1 = ['a0', 'a9', 'h0', 'h9', 'z1', 'z8']
EMPTY_BOARD = dict([((x, y), None) for x in xrange(97, 105) for y in xrange(49, 57)])

def test_coord_to_a1():
    u"""Assert the coord-to-a1 dictionary has only the desired keypairs."""
    for coord in VALID_COORDS:
        assert engine._coord_to_a1.get(coord, False) is not False
    for coord in INVALID_COORDS:
        assert engine._coord_to_a1.get(coord, False) is False

def test_a1_to_coord():
    u"""Assert the a1-to-coord dictionary has only the desired keypairs."""
    for a1 in VALID_A1:
        assert engine._a1_to_coord.get(a1, False) is not False
    for a1 in INVALID_A1:
        assert engine._a1_to_coord.get(a1, False) is False

def test_is_coord_on_board():
    u"""Assert that only valid coords are considered in the board."""
    for coord in VALID_COORDS:
        assert engine._is_coord_on_board(coord) is True
    for coord in INVALID_COORDS:
        assert engine._is_coord_on_board(coord) is False

def test_instantiate_Piece():
    u"""Assert an instance of Piece has the expected attributes."""
    for coord in VALID_COORDS:
        p = engine.Piece(coord)
        assert isinstance(p, engine.Piece)
        assert p.x, p.y == coord

def test_instantiate_SimpleUnit():
    u"""Assert an instance of SimpleUnit has the expected attributes."""
    for color in ['white', 'black']:
        for coord in VALID_COORDS:
            p = engine.SimpleUnit(coord, color)
            assert isinstance(p, engine.SimpleUnit)
            assert p.x, p.y == coord
            assert p.color == color

def test_SimpleUnit_possible_moves_empty_space():
    u"""Assert only valid moves are returned."""
    p = engine.SimpleUnit(VALID_COORDS[0], 'white')
    board = EMPTY_BOARD.copy()
    board[(p.x, p.y)] = p
    assert p.possible_moves(board) == [(p.x, p.y + 1)]

def test_SimpleUnit_possible_moves_into_ally():
    u"""Assert moves into allied units are not in returned move list."""
    p = engine.SimpleUnit(VALID_COORDS[0], 'white')
    q = engine.SimpleUnit(VALID_COORDS[1], 'white')
    board = EMPTY_BOARD.copy()
    board[(p.x, p.y)] = p
    board[(q.x, q.y)] = q
    assert p.possible_moves(board) == []

def test_SimpleUnit_possible_moves_off_board():
    u"""Assert that moves off the board are not in returned move list."""
    p = engine.SimpleUnit(VALID_COORDS[-1], 'white')
    board = EMPTY_BOARD.copy()
    board[(p.x, p.y)] = p
    assert p.possible_moves(board) == []

def test_SimpleUnit_possible_moves_into_enemy():
    u"""Assert that moves into an enemy unit is in the returned move list."""
    p = engine.SimpleUnit(VALID_COORDS[0], 'white')
    q = engine.SimpleUnit(VALID_COORDS[1], 'black')
    board = EMPTY_BOARD.copy()
    board[(p.x, p.y)] = p
    board[(q.x, q.y)] = q
    assert p.possible_moves(board) == [(p.x, p.y + 1)]<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Created on Tue Nov 22 16:38:15 2016

@author: smudd
"""

import numpy as np
import pandas as pd

def load_MIDAS_data():
    #fname = "station_data-196101010000-196112312359.csv"
    fname = "station_data-201001010000-201611161701.csv"
    dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M')

    yo  = pd.read_csv(fname,parse_dates=True,date_parser=dateparse)

    a = list(yo.columns.values)

    #print a

    yo.columns = yo.columns.str.strip()

    b = list(yo.columns.values)

    #print b

    #print yo

    #print yo['ob_date']

    yo['ob_date'] =  pd.to_datetime(yo['ob_date'], format='%Y-%m-%d %H:%M')
    yo = yo.drop_duplicates()
    yo = yo[yo.ob_day_cnt == 1]
    #print yo['ob_date']

    # nested organisation
    for station, df_station in yo.groupby('src_id'):
        print(station)
        print(df_station)

if __name__ == "__main__":
    #compare_linear_to_loop()
    #test_FoS()
    load_MIDAS_data()

<commit_msg>Removed the testing file<commit_after><|endoftext|>


<commit_before>import unittest
import warnings
import parmap
import time
import multiprocessing

# The fact that parallelization is happening is controlled via reasonable
# guesses of the parmap overhead and the CPU speeds

# Overhead of map_async, should be less than TIME_PER_TEST
TIME_OVERHEAD = 0.25

# The time each call takes to return in the _wait test
TIME_PER_TEST = 0.5

def _wait(x):
    """ Dummy function to not do anything"""
    time.sleep(TIME_PER_TEST)
    return x

def _identity(*x):
    return x

_DEFAULT_B = 1
def _fun_with_keywords(x, a = 0, b = _DEFAULT_B):
    return x + a + b

class TestParmap(unittest.TestCase):
    def test_map_without_parallel_timings(self):
        NUM_TASKS = 6
        items = range(NUM_TASKS)
        mytime = time.time()
        pfalse = parmap.map(_wait, items, pm_parallel=False)
        elapsed = time.time() - mytime
        self.assertTrue(elapsed >= TIME_PER_TEST*NUM_TASKS)
        self.assertEqual(pfalse, list(range(NUM_TASKS)))

    def test_map_with_parallel_timings(self):
        NUM_TASKS = 6
        items = range(NUM_TASKS)
        mytime = time.time()
        ptrue = parmap.map(_wait, items, pm_processes=NUM_TASKS,
                           pm_parallel=True)
        elapsed = time.time() - mytime
        self.assertTrue(elapsed >= TIME_PER_TEST)
        self.assertTrue(elapsed < TIME_PER_TEST*(NUM_TASKS-1))
        self.assertEqual(ptrue, list(range(NUM_TASKS)))

    def test_map_kwargs(self):
        items = range(2)
        pfalse = parmap.map(_fun_with_keywords, items, pm_parallel=False, a=10)
        ptrue = parmap.map(_fun_with_keywords, items, pm_parallel=True, a=10)
        noparmap = [ x + 10 + _DEFAULT_B for x in items]
        self.assertEqual(pfalse, ptrue)
        self.assertEqual(pfalse, noparmap)

    def test_map_progress(self):
        items = range(4)
        pfalse = parmap.map(_wait, items, pm_pbar=False)
        ptrue = parmap.map(_wait, items, pm_pbar=True)
        noparmap = list(map(_wait, items))
        self.assertEqual(pfalse, ptrue)
        self.assertEqual(pfalse, noparmap)

    def test_map_async_started_simultaneously_timings(self):
        items = list(range(4))
        mytime0 = time.time()
        # These are started in parallel:
        with parmap.map_async(_wait, items, pm_processes=4) as compute1:
            elapsed1 = time.time() - mytime0
            mytime = time.time()
            with parmap.map_async(_wait, items, pm_processes=4) as compute2:
                elapsed2 = time.time() - mytime
                mytime = time.time()
                result1 = compute1.get()
                elapsed3 = time.time() - mytime0
                mytime = time.time()
                result2 = compute2.get()
                elapsed4 = time.time() - mytime0
        self.assertTrue(elapsed1 < TIME_OVERHEAD)
        self.assertTrue(elapsed2 < TIME_OVERHEAD)
        self.assertTrue(elapsed3 < 4*TIME_PER_TEST+2*TIME_OVERHEAD)
        self.assertTrue(elapsed4 < 4*TIME_PER_TEST+2*TIME_OVERHEAD)
        self.assertEqual(result1, result2)
        self.assertEqual(result1, items)

    def test_map_async_noparallel_started_simultaneously_timings(self):
        NTASKS = 4
        items = list(range(NTASKS))
        mytime = time.time()
        # These are started in parallel:
        with parmap.map_async(_wait, items, pm_parallel=False) as compute1:
            elapsed1 = time.time() - mytime
            mytime = time.time()
            with parmap.map_async(_wait, items, pm_parallel=False) as compute2:
                elapsed2 = time.time() - mytime
                mytime = time.time()
                result1 = compute1.get()
                result2 = compute2.get()
                finished = time.time() - mytime
        self.assertTrue(elapsed1 >= NTASKS*TIME_PER_TEST)
        self.assertTrue(elapsed2 >= NTASKS*TIME_PER_TEST)
        self.assertTrue(finished <= 2*TIME_OVERHEAD)
        self.assertEqual(result1, result2)
        self.assertEqual(result1, items)

    def test_map_async(self):
        NUM_TASKS = 6
        NCPU = 6
        items = range(NUM_TASKS)
        mytime = time.time()
        pfalse = parmap.map_async(_wait, items, pm_parallel=False).get()
        elapsed_false = time.time() - mytime
        mytime = time.time()
        with parmap.map_async(_wait, items, pm_processes=NCPU) as ptrue:
            elap_true_async = time.time() - mytime
            mytime = time.time()
            ptrue_result = ptrue.get()
            elap_true_get = time.time() - mytime
        noparmap = list(items)
        self.assertEqual(pfalse, ptrue_result)
        self.assertEqual(pfalse, noparmap)
        self.assertTrue(elapsed_false > TIME_PER_TEST*(NUM_TASKS-1))
        self.assertTrue(elap_true_async < TIME_OVERHEAD)
        self.assertTrue(elap_true_get < TIME_PER_TEST*(NUM_TASKS-1))

    def test_starmap(self):
        items = [(1, 2), (3, 4), (5, 6)]
        pfalse = parmap.starmap(_identity, items, 5, 6, pm_parallel=False)
        ptrue = parmap.starmap(_identity, items, 5, 6, pm_parallel=True)
        self.assertEqual(pfalse, ptrue)

    def test_starmap_async(self):
        items = [(1, 2), (3, 4), (5, 6)]
        pfalse = parmap.starmap_async(_identity, items, 5, 6, pm_parallel=False)
        ptrue = parmap.starmap_async(_identity, items, 5, 6, pm_parallel=True)
        self.assertEqual(pfalse.get(), ptrue.get())

    def test_warn_wrong_argument_map(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            parmap.map(range, [1, 2], pm_processes=-3)
            self.assertTrue(len(w) > 0)

if __name__ == '__main__':
    multiprocessing.freeze_support()
    unittest.main()
<commit_msg>Make tests last longer so travis does not fail<commit_after>import unittest
import warnings
import parmap
import time
import multiprocessing

# The fact that parallelization is happening is controlled via reasonable
# guesses of the parmap overhead and the CPU speeds

# Overhead of map_async, should be less than TIME_PER_TEST
TIME_OVERHEAD = 0.4

# The time each call takes to return in the _wait test
TIME_PER_TEST = 0.8

def _wait(x):
    """ Dummy function to not do anything"""
    time.sleep(TIME_PER_TEST)
    return x

def _identity(*x):
    return x

_DEFAULT_B = 1
def _fun_with_keywords(x, a = 0, b = _DEFAULT_B):
    return x + a + b

class TestParmap(unittest.TestCase):
    def test_map_without_parallel_timings(self):
        NUM_TASKS = 6
        items = range(NUM_TASKS)
        mytime = time.time()
        pfalse = parmap.map(_wait, items, pm_parallel=False)
        elapsed = time.time() - mytime
        self.assertTrue(elapsed >= TIME_PER_TEST*NUM_TASKS)
        self.assertEqual(pfalse, list(range(NUM_TASKS)))

    def test_map_with_parallel_timings(self):
        NUM_TASKS = 6
        items = range(NUM_TASKS)
        mytime = time.time()
        ptrue = parmap.map(_wait, items, pm_processes=NUM_TASKS,
                           pm_parallel=True)
        elapsed = time.time() - mytime
        self.assertTrue(elapsed >= TIME_PER_TEST)
        self.assertTrue(elapsed < TIME_PER_TEST*(NUM_TASKS-1))
        self.assertEqual(ptrue, list(range(NUM_TASKS)))

    def test_map_kwargs(self):
        items = range(2)
        pfalse = parmap.map(_fun_with_keywords, items, pm_parallel=False, a=10)
        ptrue = parmap.map(_fun_with_keywords, items, pm_parallel=True, a=10)
        noparmap = [ x + 10 + _DEFAULT_B for x in items]
        self.assertEqual(pfalse, ptrue)
        self.assertEqual(pfalse, noparmap)

    def test_map_progress(self):
        items = range(4)
        pfalse = parmap.map(_wait, items, pm_pbar=False)
        ptrue = parmap.map(_wait, items, pm_pbar=True)
        noparmap = list(map(_wait, items))
        self.assertEqual(pfalse, ptrue)
        self.assertEqual(pfalse, noparmap)

    def test_map_async_started_simultaneously_timings(self):
        items = list(range(4))
        mytime0 = time.time()
        # These are started in parallel:
        with parmap.map_async(_wait, items, pm_processes=4) as compute1:
            elapsed1 = time.time() - mytime0
            mytime = time.time()
            with parmap.map_async(_wait, items, pm_processes=4) as compute2:
                elapsed2 = time.time() - mytime
                mytime = time.time()
                result1 = compute1.get()
                elapsed3 = time.time() - mytime0
                mytime = time.time()
                result2 = compute2.get()
                elapsed4 = time.time() - mytime0
        self.assertTrue(elapsed1 < TIME_OVERHEAD)
        self.assertTrue(elapsed2 < TIME_OVERHEAD)
        self.assertTrue(elapsed3 < 4*TIME_PER_TEST+2*TIME_OVERHEAD)
        self.assertTrue(elapsed4 < 4*TIME_PER_TEST+2*TIME_OVERHEAD)
        self.assertEqual(result1, result2)
        self.assertEqual(result1, items)

    def test_map_async_noparallel_started_simultaneously_timings(self):
        NTASKS = 4
        items = list(range(NTASKS))
        mytime = time.time()
        # These are started in parallel:
        with parmap.map_async(_wait, items, pm_parallel=False) as compute1:
            elapsed1 = time.time() - mytime
            mytime = time.time()
            with parmap.map_async(_wait, items, pm_parallel=False) as compute2:
                elapsed2 = time.time() - mytime
                mytime = time.time()
                result1 = compute1.get()
                result2 = compute2.get()
                finished = time.time() - mytime
        self.assertTrue(elapsed1 >= NTASKS*TIME_PER_TEST)
        self.assertTrue(elapsed2 >= NTASKS*TIME_PER_TEST)
        self.assertTrue(finished <= 2*TIME_OVERHEAD)
        self.assertEqual(result1, result2)
        self.assertEqual(result1, items)

    def test_map_async(self):
        NUM_TASKS = 6
        NCPU = 6
        items = range(NUM_TASKS)
        mytime = time.time()
        pfalse = parmap.map_async(_wait, items, pm_parallel=False).get()
        elapsed_false = time.time() - mytime
        mytime = time.time()
        with parmap.map_async(_wait, items, pm_processes=NCPU) as ptrue:
            elap_true_async = time.time() - mytime
            mytime = time.time()
            ptrue_result = ptrue.get()
            elap_true_get = time.time() - mytime
        noparmap = list(items)
        self.assertEqual(pfalse, ptrue_result)
        self.assertEqual(pfalse, noparmap)
        self.assertTrue(elapsed_false > TIME_PER_TEST*(NUM_TASKS-1))
        self.assertTrue(elap_true_async < TIME_OVERHEAD)
        self.assertTrue(elap_true_get < TIME_PER_TEST*(NUM_TASKS-1))

    def test_starmap(self):
        items = [(1, 2), (3, 4), (5, 6)]
        pfalse = parmap.starmap(_identity, items, 5, 6, pm_parallel=False)
        ptrue = parmap.starmap(_identity, items, 5, 6, pm_parallel=True)
        self.assertEqual(pfalse, ptrue)

    def test_starmap_async(self):
        items = [(1, 2), (3, 4), (5, 6)]
        pfalse = parmap.starmap_async(_identity, items, 5, 6, pm_parallel=False)
        ptrue = parmap.starmap_async(_identity, items, 5, 6, pm_parallel=True)
        self.assertEqual(pfalse.get(), ptrue.get())

    def test_warn_wrong_argument_map(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            parmap.map(range, [1, 2], pm_processes=-3)
            self.assertTrue(len(w) > 0)

if __name__ == '__main__':
    multiprocessing.freeze_support()
    unittest.main()
<|endoftext|>


<commit_before>import unittest
import mock

from record import record, RECORD_FN_NAME, find_indent_level, strip_indent

class TestRecord(unittest.TestCase):

    # Patch path refers to current module because the decorator injects the
    # record fn in here.
    record_state_fn_path = 'test_record.%s' % RECORD_FN_NAME

    def test_simple(self):
        """Simple function, no loop, no return, no conditional."""

        @record
        def foo():
            x = 5
            y = 6

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4])

    def test_conditional(self):
        """Fn with a simple conditional."""

        @record
        def foo():
            x = 3
            y = 2
            if x == 3:
                y = 5

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6])

    def test_conditional_else(self):
        """Fn with conditional having else."""

        @record
        def foo():
            x = 3
            y = 2
            if x != 3:
                y = 5
            else:
                y = 6

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        # Note: `else` does not have a lineno, using `if`'s lineno.
        self._check_record_calls(record_mock, [3, 4, 5, 8])

    def test_while(self):
        """Fn with a while."""

        @record
        def foo():
            x = 3
            y = 6

            while x < y:
                x += 1

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 6, 7, 6, 7, 6, 7, 6])

    def test_for(self):
        """Fn with a for."""

        @record
        def foo():
            x = 3
            s = 0
            for i in range(x):
                s = s + i

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6, 5, 6, 5, 6, 5])

    def test_find_indent_level(self):
        source = '    def foo()'
        self.assertEqual(find_indent_level(source), 4)

        source = '    '
        self.assertEqual(find_indent_level(source), 4)

        source = ''
        self.assertEqual(find_indent_level(source), 0)

    @mock.patch('record.find_indent_level')
    def test_strip_indent(self, find_indent_mock):
        find_indent_mock.return_value = 4

        indented_source = """
    def foo():
        x = 3

        y = 4
        # Comment here
        if x == 3:
            y = 5
        return x + y
"""
        stripped_source = """
def foo():
    x = 3

    y = 4
    # Comment here
    if x == 3:
        y = 5
    return x + y
"""

        self.assertEqual(strip_indent(indented_source), stripped_source,
                         "Incorrectly stripped indentation.")

    def _check_record_calls(self, record_mock, expected_linenos):
        self.assertEqual(record_mock.call_count, len(expected_linenos))
        for i, lineno in enumerate(expected_linenos):
            self.assertEqual(record_mock.call_args_list[i][0][0], lineno)
<commit_msg>Nested if in for.<commit_after>import unittest
import mock

from record import record, RECORD_FN_NAME, find_indent_level, strip_indent

class TestRecord(unittest.TestCase):

    # Patch path refers to current module because the decorator injects the
    # record fn in here.
    record_state_fn_path = 'test_record.%s' % RECORD_FN_NAME

    def test_simple(self):
        """Simple function, no loop, no return, no conditional."""

        @record
        def foo():
            x = 5
            y = 6

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4])

    def test_conditional(self):
        """Fn with a simple conditional."""

        @record
        def foo():
            x = 3
            y = 2
            if x == 3:
                y = 5

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6])

    def test_conditional_else(self):
        """Fn with conditional having else."""

        @record
        def foo():
            x = 3
            y = 2
            if x != 3:
                y = 5
            else:
                y = 6

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        # Note: `else` does not have a lineno, using `if`'s lineno.
        self._check_record_calls(record_mock, [3, 4, 5, 8])

    def test_while(self):
        """Fn with a while."""

        @record
        def foo():
            x = 3
            y = 6

            while x < y:
                x += 1

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 6, 7, 6, 7, 6, 7, 6])

    def test_for(self):
        """Fn with a for."""

        @record
        def foo():
            x = 3
            s = 0
            for i in range(x):
                s = s + i

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6, 5, 6, 5, 6, 5])

    def test_nested_if_in_for(self):
        """Fn with a for containing an if."""

        @record
        def foo():
            x = 3
            s = 0
            for i in range(x):
                if s > -1:
                    s += i

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5])

    def test_find_indent_level(self):
        source = '    def foo()'
        self.assertEqual(find_indent_level(source), 4)

        source = '    '
        self.assertEqual(find_indent_level(source), 4)

        source = ''
        self.assertEqual(find_indent_level(source), 0)

    @mock.patch('record.find_indent_level')
    def test_strip_indent(self, find_indent_mock):
        find_indent_mock.return_value = 4

        indented_source = """
    def foo():
        x = 3

        y = 4
        # Comment here
        if x == 3:
            y = 5
        return x + y
"""
        stripped_source = """
def foo():
    x = 3

    y = 4
    # Comment here
    if x == 3:
        y = 5
    return x + y
"""

        self.assertEqual(strip_indent(indented_source), stripped_source,
                         "Incorrectly stripped indentation.")

    def _check_record_calls(self, record_mock, expected_linenos):
        self.assertEqual(record_mock.call_count, len(expected_linenos))
        for i, lineno in enumerate(expected_linenos):
            self.assertEqual(record_mock.call_args_list[i][0][0], lineno)
<|endoftext|>


<commit_before># Copyright 2018 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Extract from notebook for Serving Optimization """

from __future__ import print_function

import tensorflow as tf
from datetime import datetime

def inference_test(saved_model_dir,
                   eval_data,
                   signature="predict",
                   repeat=10):
  tf.logging.set_verbosity(tf.logging.ERROR)

  time_start = datetime.utcnow()

  predictor = tf.contrib.predictor.from_saved_model(
      export_dir=saved_model_dir,
      signature_def_key=signature
  )
  time_end = datetime.utcnow()

  time_elapsed = time_end - time_start

  print("", "Model loading time: {} seconds".format(
      time_elapsed.total_seconds()), "")

  time_start = datetime.utcnow()
  output = None
  for i in range(repeat):
    output = predictor(
        {'input_image': eval_data[:10]}
    )

  time_end = datetime.utcnow()

  print("Prediction produced for {} instances repeated {} times".format(
      len(output['class_ids']), repeat), "")

  time_elapsed = (time_end - time_start).total_seconds()
  print("Inference elapsed time: {} seconds".format(
      time_elapsed), "")

  print("Prediction output for the last instance:")
  for key in output.keys():
    print("{}: {}".format(key,output[key][0]))

  return time_elapsed
<commit_msg>cleanup / updates for inference code<commit_after># Copyright 2018 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Extract from notebook for Serving Optimization """

from __future__ import print_function

import tensorflow as tf
from datetime import datetime

BATCH_SIZE = 10

def inference_test(saved_model_dir,
                   eval_data,
                   signature='predict',
                   repeat=10):
  tf.logging.set_verbosity(tf.logging.ERROR)

  # load model
  time_start = datetime.utcnow()
  for i in range(repeat):
    predictor = tf.contrib.predictor.from_saved_model(
        export_dir=saved_model_dir,
        signature_def_key=signature
    )
  time_end = datetime.utcnow()

  loading_time = (time_end - time_start).total_seconds()

  print('', 'Model loading time: {} seconds'.format(
      loading_time), '')

  # serve a batch of
  time_start = datetime.utcnow()
  output = None
  for i in range(repeat):
    output = predictor(
        {'input_image': eval_data[:BATCH_SIZE]}
    )
  time_end = datetime.utcnow()

  print('Prediction produced for {} instances repeated {} times'.format(
      BATCH_SIZE, repeat), '')

  serving_time = (time_end - time_start).total_seconds()
  print('Inference elapsed time: {} seconds'.format(
      serving_time), '')

  print('Prediction output for the last instance:')
  for key in output.keys():
    print('{}: {}'.format(key,output[key][0]))

  return loading_time, serving_time
<|endoftext|>