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

import logging

from seqlog.structured_logging import StructuredLogger, StructuredRootLogger
from seqlog.structured_logging import SeqLogHandler, ConsoleStructuredLogHandler
from seqlog.structured_logging import get_global_log_properties as _get_global_log_properties
from seqlog.structured_logging import set_global_log_properties as _set_global_log_properties
from seqlog.structured_logging import clear_global_log_properties as _clear_global_log_properties
from seqlog.structured_logging import reset_global_log_properties as _reset_global_log_properties

__author__ = 'Adam Friedman'
__email__ = 'tintoy@tintoy.io'
__version__ = "0.3.4"

def log_to_seq(server_url, api_key=None, level=logging.WARNING,
               batch_size=10, auto_flush_timeout=None,
               additional_handlers=None, override_root_logger=False,
               **kwargs):
    """
    Configure the logging system to send log entries to Seq.

    Note that the root logger will not log to Seq by default.

    :param server_url: The Seq server URL.
    :param api_key: The Seq API key (optional).
    :param level: The minimum level at which to log.
    :param batch_size: The number of log entries to collect before publishing to Seq.
    :param auto_flush_timeout: If specified, the time (in seconds) before
                               the current batch is automatically flushed.
    :param additional_handlers: Additional `LogHandler`s (if any).
    :param override_root_logger: Override the root logger, too?
                                 Note - this might cause problems if third-party components try to be clever
                                 when using the logging.XXX functions.
    :return: The `SeqLogHandler` that sends events to Seq. Can be used to forcibly flush records to Seq.
    :rtype: SeqLogHandler
    """

    logging.setLoggerClass(StructuredLogger)

    if override_root_logger:
        _override_root_logger()

    log_handlers = [
        SeqLogHandler(server_url, api_key, batch_size, auto_flush_timeout)
    ]

    if additional_handlers:
        for additional_handler in additional_handlers:
            log_handlers.append(additional_handler)

    logging.basicConfig(
        style='{',
        handlers=log_handlers,
        level=level,
        **kwargs
    )

    return log_handlers[0]

def log_to_console(level=logging.WARNING, override_root_logger=False, **kwargs):
    """
    Configure the logging system to send log entries to the console.

    Note that the root logger will not log to Seq by default.

    :param level: The minimum level at which to log.
    :param override_root_logger: Override the root logger, too?
                                 Note - this might cause problems if third-party components try to be clever
                                 when using the logging.XXX functions.
    """

    logging.setLoggerClass(StructuredLogger)

    if override_root_logger:
        _override_root_logger()

    logging.basicConfig(
        style='{',
        handlers=[
            ConsoleStructuredLogHandler()
        ],
        level=level,
        **kwargs
    )

def get_global_log_properties():
    """
    Get the properties to be added to all structured log entries.

    :return: A copy of the global log properties.
    :rtype: dict
    """

    return _get_global_log_properties()

def set_global_log_properties(**properties):
    """
    Configure the properties to be added to all structured log entries.

    :param properties: Keyword arguments representing the properties.
    :type properties: dict
    """

    _set_global_log_properties(**properties)

def reset_global_log_properties():
    """
    Initialize global log properties to their default values.
    """

    _reset_global_log_properties()

def clear_global_log_properties():
    """
    Remove all global properties.
    """

    _clear_global_log_properties()

def _override_root_logger():
    """
    Override the root logger with a `StructuredRootLogger`.
    """

    logging.root = StructuredRootLogger(logging.WARNING)
    logging.Logger.root = logging.root
    logging.Logger.manager = logging.Manager(logging.Logger.root)
<commit_msg>Add functions to configure logging from a YAML-format file or a dict.<commit_after># -*- coding: utf-8 -*-

import logging
import yaml

from seqlog.structured_logging import StructuredLogger, StructuredRootLogger
from seqlog.structured_logging import SeqLogHandler, ConsoleStructuredLogHandler
from seqlog.structured_logging import get_global_log_properties as _get_global_log_properties
from seqlog.structured_logging import set_global_log_properties as _set_global_log_properties
from seqlog.structured_logging import clear_global_log_properties as _clear_global_log_properties
from seqlog.structured_logging import reset_global_log_properties as _reset_global_log_properties

__author__ = 'Adam Friedman'
__email__ = 'tintoy@tintoy.io'
__version__ = "0.3.4"

def configure_from_file(file_name, override_root_logger=True):
    """
    Configure Seq logging using YAML-format configuration file.

    Uses `logging.config.dictConfig()`.

    :param file_name: The name of the configuration file to use.
    :type file_name: str
    :param override_root_logger: Override the root logger to use a Seq-specific implementation? (default: True)
    :type override_root_logger: bool
    """

    with open(file_name) as config_file:
        config = yaml.load(config_file)

    configure_from_dict(config, override_root_logger)

def configure_from_dict(config, override_root_logger=True):
    """
    Configure Seq logging using a dictionary.

    Uses `logging.config.dictConfig()`.

    :param config: A dict containing the configuration.
    :type config: dict
    :param override_root_logger: Override the root logger to use a Seq-specific implementation? (default: True)
    :type override_root_logger: bool
    """

    if override_root_logger:
        _override_root_logger()

    logging.config.dictConfig(config)

def log_to_seq(server_url, api_key=None, level=logging.WARNING,
               batch_size=10, auto_flush_timeout=None,
               additional_handlers=None, override_root_logger=False,
               **kwargs):
    """
    Configure the logging system to send log entries to Seq.

    Note that the root logger will not log to Seq by default.

    :param server_url: The Seq server URL.
    :param api_key: The Seq API key (optional).
    :param level: The minimum level at which to log.
    :param batch_size: The number of log entries to collect before publishing to Seq.
    :param auto_flush_timeout: If specified, the time (in seconds) before
                               the current batch is automatically flushed.
    :param additional_handlers: Additional `LogHandler`s (if any).
    :param override_root_logger: Override the root logger, too?
                                 Note - this might cause problems if third-party components try to be clever
                                 when using the logging.XXX functions.
    :return: The `SeqLogHandler` that sends events to Seq. Can be used to forcibly flush records to Seq.
    :rtype: SeqLogHandler
    """

    logging.setLoggerClass(StructuredLogger)

    if override_root_logger:
        _override_root_logger()

    log_handlers = [
        SeqLogHandler(server_url, api_key, batch_size, auto_flush_timeout)
    ]

    if additional_handlers:
        for additional_handler in additional_handlers:
            log_handlers.append(additional_handler)

    logging.basicConfig(
        style='{',
        handlers=log_handlers,
        level=level,
        **kwargs
    )

    return log_handlers[0]

def log_to_console(level=logging.WARNING, override_root_logger=False, **kwargs):
    """
    Configure the logging system to send log entries to the console.

    Note that the root logger will not log to Seq by default.

    :param level: The minimum level at which to log.
    :param override_root_logger: Override the root logger, too?
                                 Note - this might cause problems if third-party components try to be clever
                                 when using the logging.XXX functions.
    """

    logging.setLoggerClass(StructuredLogger)

    if override_root_logger:
        _override_root_logger()

    logging.basicConfig(
        style='{',
        handlers=[
            ConsoleStructuredLogHandler()
        ],
        level=level,
        **kwargs
    )

def get_global_log_properties():
    """
    Get the properties to be added to all structured log entries.

    :return: A copy of the global log properties.
    :rtype: dict
    """

    return _get_global_log_properties()

def set_global_log_properties(**properties):
    """
    Configure the properties to be added to all structured log entries.

    :param properties: Keyword arguments representing the properties.
    :type properties: dict
    """

    _set_global_log_properties(**properties)

def reset_global_log_properties():
    """
    Initialize global log properties to their default values.
    """

    _reset_global_log_properties()

def clear_global_log_properties():
    """
    Remove all global properties.
    """

    _clear_global_log_properties()

def _override_root_logger():
    """
    Override the root logger with a `StructuredRootLogger`.
    """

    logging.root = StructuredRootLogger(logging.WARNING)
    logging.Logger.root = logging.root
    logging.Logger.manager = logging.Manager(logging.Logger.root)
<|endoftext|>


<commit_before>import pipes

from starcluster import threadpool
from starcluster.clustersetup import ClusterSetup
from starcluster.logger import log

class WorkerSetup(ClusterSetup):

    @property
    def pool(self):
        if not getattr(self, '_pool', None):
            self._pool = threadpool.get_thread_pool(size=20, disable_threads=False)
        return self._pool

class StartCeleryWorker(WorkerSetup):

    def __init__(
            self,
            git_sync_dir,
            worker_dir,
            kill_existing='True',
            delete_pyc_files='True',
            remount_dir='',
            queue='celery',
            celery_cmd='celery',
            concurrency='',
            app='',
            broker='',
            ld_library_path='/usr/local/lib',
            heartbeat_interval='5',
            maxtasksperchild='1',
            Ofair='True',
            loglevel='info',
            user='ubuntu',
            tmux_history_limit='8000',
            worker_setup_cmd='',
            master_setup_cmd='',
    ):

        self._user = user

        # error checking
        kill_existing = to_bool(kill_existing)
        delete_pyc_files = to_bool(delete_pyc_files)
        Ofair = to_bool(Ofair)

        # build master sync command
        sync_cmd_list = []
        if git_sync_dir:
            if remount_dir.strip():
                sync_cmd_list += ["sudo mount -o remount %s" % qd(remount_dir)]
            sync_cmd_list += [
                "cd %s" % qd(git_sync_dir),
                "git pull",
                "git submodule init",
                "git submodule update",
            ]
        if delete_pyc_files:
            delete_pyc_files += ["find %s -name '*.pyc' -delete" % qd(worker_dir)]
        if master_setup_cmd:
            sync_cmd_list += [master_setup_cmd]
        if sync_cmd_list:
            self._sync_cmd = "; ".join(sync_cmd_list)

        # build worker node command
        celery_args = [
            qs(celery_cmd), 'worker',
            '--hostname', qs('%%h-%s' % queue),
            '--queues', qs(queue),
        ]
        if app.strip():
            celery_args += ['--app', qs(app)]
        if broker.strip():
            celery_args += ['--broker', qs(broker)]
        if concurrency.strip():
            celery_args += ['--concurrency', int(concurrency)]
        if maxtasksperchild.strip():
            celery_args += ['--maxtasksperchild', int(maxtasksperchild)]
        if heartbeat_interval.strip():
            celery_args += ['--heartbeat-interval', int(heartbeat_interval)]
        if loglevel.strip():
            celery_args += ['--loglevel', qs(loglevel)]
        if Ofair:
            celery_args += ['-Ofair']

        # session_cmd: command that runs inside the tmux session
        session_cmd_list = [
            # (use double quotes so that bash expands $LD_LIBRARY_PATH)
            'export LD_LIBRARY_PATH="' + ld_library_path + ':$LD_LIBRARY_PATH"'
        ]
        if worker_setup_cmd:
            session_cmd_list += [worker_setup_cmd]
        session_cmd_list += [
            'cd %s' % qd(worker_dir),
            ' '.join(str(x) for x in celery_args),
        ]
        session_cmd = "; ".join(session_cmd_list)

        # build final start command
        tmux_session = "celery-" + queue
        start_cmd_list = []
        if kill_existing:
            start_cmd_list += ["tmux kill-session -t %s" % qs(tmux_session)]
        if remount_dir.strip() and kill_existing:
            start_cmd_list += ["sudo mount -o remount %s" % qd(remount_dir)]
        start_cmd_list += [
            "tmux new-session -s %s -d %s" % (qs(tmux_session), qs(session_cmd)),
            "tmux set-option -t %s history-limit %s" % (qs(tmux_session), int(tmux_history_limit)),
        ]
        self._start_cmd = "; ".join(start_cmd_list)

    def on_add_node(self, node, nodes, master, user, user_shell, volumes):
        run_cmd(node, self._start_cmd, self._user)

    def run(self, nodes, master, user, user_shell, volumes):
        if self._sync_cmd:
            run_cmd(master, self._sync_cmd, self._user, silent=False)
        for node in nodes:
            self.pool.simple_job(
                run_cmd, args=(node, self._start_cmd, self._user), jobid=node.alias)
        self.pool.wait(len(nodes))

class KillCeleryWorker(WorkerSetup):

    def __init__(self, user='ubuntu', queue='celery'):
        tmux_session = "celery-" + queue
        self._user = user
        self._kill_cmd = "tmux kill-session -t '%s'" % tmux_session

    def run(self, nodes, master, user, user_shell, volumes):
        for node in nodes:
            self.pool.simple_job(
                run_cmd,
                args=(node, self._kill_cmd, self._user),
                jobid=node.alias,
            )
        self.pool.wait(len(nodes))

def to_bool(s):
    if s:
        s = s.strip()
        if s == 'True':
            return True
        elif s == 'False':
            return False
        else:
            raise ValueError("Expected True or False, got: '%s'" % s)
    return False

def qd(s):
    """ Quote a directory """
    if s is not None:
        s = str(s).strip()
        if s.startswith('~/') and '"' not in s and "'" not in s:
            return '"$HOME/%s"' % s[2:]
        else:
            return pipes.quote(s)
    else:
        return ''

def qs(s):
    """ Strip and quote-escape a string """
    if s is not None:
        s = str(s).strip()
        return pipes.quote(s)
    else:
        return ''

def run_cmd(node, cmd, user, silent=True):
    log.info("%s@%s: %s" % (user, node.alias, cmd))
    if user != 'root':
        node.ssh.switch_user(user)
    node.ssh.execute(cmd, silent=silent)
    if user != 'root':
        node.ssh.switch_user('root')
<commit_msg>clear pyc files by default<commit_after>import pipes

from starcluster import threadpool
from starcluster.clustersetup import ClusterSetup
from starcluster.logger import log

class WorkerSetup(ClusterSetup):

    @property
    def pool(self):
        if not getattr(self, '_pool', None):
            self._pool = threadpool.get_thread_pool(size=20, disable_threads=False)
        return self._pool

class StartCeleryWorker(WorkerSetup):

    def __init__(
            self,
            git_sync_dir,
            worker_dir,
            kill_existing='True',
            delete_pyc_files='True',
            remount_dir='',
            queue='celery',
            celery_cmd='celery',
            concurrency='',
            app='',
            broker='',
            ld_library_path='/usr/local/lib',
            heartbeat_interval='5',
            maxtasksperchild='1',
            Ofair='True',
            loglevel='info',
            user='ubuntu',
            tmux_history_limit='8000',
            worker_setup_cmd='',
            master_setup_cmd='',
    ):

        self._user = user

        # error checking
        kill_existing = to_bool(kill_existing)
        delete_pyc_files = to_bool(delete_pyc_files)
        Ofair = to_bool(Ofair)

        # build master sync command
        sync_cmd_list = []
        if git_sync_dir:
            if remount_dir.strip():
                sync_cmd_list += ["sudo mount -o remount %s" % qd(remount_dir)]
            sync_cmd_list += [
                "cd %s" % qd(git_sync_dir),
                "git pull",
                "git submodule init",
                "git submodule update",
            ]
        if delete_pyc_files:
            sync_cmd_list += ["find %s -name '*.pyc' -delete" % qd(worker_dir)]
        if master_setup_cmd:
            sync_cmd_list += [master_setup_cmd]
        if sync_cmd_list:
            self._sync_cmd = "; ".join(sync_cmd_list)

        # build worker node command
        celery_args = [
            qs(celery_cmd), 'worker',
            '--hostname', qs('%%h-%s' % queue),
            '--queues', qs(queue),
        ]
        if app.strip():
            celery_args += ['--app', qs(app)]
        if broker.strip():
            celery_args += ['--broker', qs(broker)]
        if concurrency.strip():
            celery_args += ['--concurrency', int(concurrency)]
        if maxtasksperchild.strip():
            celery_args += ['--maxtasksperchild', int(maxtasksperchild)]
        if heartbeat_interval.strip():
            celery_args += ['--heartbeat-interval', int(heartbeat_interval)]
        if loglevel.strip():
            celery_args += ['--loglevel', qs(loglevel)]
        if Ofair:
            celery_args += ['-Ofair']

        # session_cmd: command that runs inside the tmux session
        session_cmd_list = [
            # (use double quotes so that bash expands $LD_LIBRARY_PATH)
            'export LD_LIBRARY_PATH="' + ld_library_path + ':$LD_LIBRARY_PATH"'
        ]
        if worker_setup_cmd:
            session_cmd_list += [worker_setup_cmd]
        session_cmd_list += [
            'cd %s' % qd(worker_dir),
            ' '.join(str(x) for x in celery_args),
        ]
        session_cmd = "; ".join(session_cmd_list)

        # build final start command
        tmux_session = "celery-" + queue
        start_cmd_list = []
        if kill_existing:
            start_cmd_list += ["tmux kill-session -t %s" % qs(tmux_session)]
        if remount_dir.strip() and kill_existing:
            start_cmd_list += ["sudo mount -o remount %s" % qd(remount_dir)]
        start_cmd_list += [
            "tmux new-session -s %s -d %s" % (qs(tmux_session), qs(session_cmd)),
            "tmux set-option -t %s history-limit %s" % (qs(tmux_session), int(tmux_history_limit)),
        ]
        self._start_cmd = "; ".join(start_cmd_list)

    def on_add_node(self, node, nodes, master, user, user_shell, volumes):
        run_cmd(node, self._start_cmd, self._user)

    def run(self, nodes, master, user, user_shell, volumes):
        if self._sync_cmd:
            run_cmd(master, self._sync_cmd, self._user, silent=False)
        for node in nodes:
            self.pool.simple_job(
                run_cmd, args=(node, self._start_cmd, self._user), jobid=node.alias)
        self.pool.wait(len(nodes))

class KillCeleryWorker(WorkerSetup):

    def __init__(self, user='ubuntu', queue='celery'):
        tmux_session = "celery-" + queue
        self._user = user
        self._kill_cmd = "tmux kill-session -t '%s'" % tmux_session

    def run(self, nodes, master, user, user_shell, volumes):
        for node in nodes:
            self.pool.simple_job(
                run_cmd,
                args=(node, self._kill_cmd, self._user),
                jobid=node.alias,
            )
        self.pool.wait(len(nodes))

def to_bool(s):
    if s:
        s = s.strip()
        if s == 'True':
            return True
        elif s == 'False':
            return False
        else:
            raise ValueError("Expected True or False, got: '%s'" % s)
    return False

def qd(s):
    """ Quote a directory """
    if s is not None:
        s = str(s).strip()
        if s.startswith('~/') and '"' not in s and "'" not in s:
            return '"$HOME/%s"' % s[2:]
        else:
            return pipes.quote(s)
    else:
        return ''

def qs(s):
    """ Strip and quote-escape a string """
    if s is not None:
        s = str(s).strip()
        return pipes.quote(s)
    else:
        return ''

def run_cmd(node, cmd, user, silent=True):
    log.info("%s@%s: %s" % (user, node.alias, cmd))
    if user != 'root':
        node.ssh.switch_user(user)
    node.ssh.execute(cmd, silent=silent)
    if user != 'root':
        node.ssh.switch_user('root')
<|endoftext|>


<commit_before>import time
import datetime
from jinja2 import Environment
from jinja2 import PackageLoader
from InformationFetcher import  InformationFetcher
from Room import Room

class TemplateMatcher():

    def __init__(self):
        self._env                = Environment(loader=PackageLoader('Template', 'tts-templates'))
        self._informationFetcher = InformationFetcher()

    def getHourlyTime(self):
        template = self._env.get_template('time-hourly.txt')
        hour = datetime.datetime.now().time().hour
        _,_,temp,_,_,_,_ = self._informationFetcher.getOutdoor()
        return template.render(hour=hour, temp=temp)

    def getAcknowledgeStartWashingMachine(self):
        template = self._env.get_template('acknowledge-start-washing-machine.txt')
        return template.render()

    def getAcknowledgeEndWashingMachine(self):
        template = self._env.get_template('acknowledge-end-washing-machine.txt')
        return template.render()

    def getAcknowledgeEmtyingWashingMachine(self):
        template = self._env.get_template('acknowledge-empty-washing-machine.txt')
        return template.render()

    def getAcknowledgeStartShower(self, name):
        template = self._env.get_template('acknowledge-start-shower.txt')
        return template.render(name=name)

    def getAcknowledgeEndShower(self, name):
        template = self._env.get_template('acknowledge-end-shower.txt')
        return template.render(name=name)

    def getAcknowledgeStartBath(self, name):
        template = self._env.get_template('acknowledge-start-bath.txt')
        return template.render(name=name)

    def getAcknowledgeEndBath(self, name):
        template = self._env.get_template('acknowledge-end-bath.txt')
        return template.render(name=name)

    def getAcknowledgeDeactivateBath(self, name):
        template = self._env.get_template('acknowledge-deactivate-everything.txt')
        return template.render(name=name)

    def getWateringTheFlower(self, level):
        template = self._env.get_template('watering-the-flower.txt')
        return template.render(level=level)

    def getWashingMachineReady(self, endtime):
        template = self._env.get_template('washingmachine-ready.txt')
        diff = time.time() - endtime
        hours = int(diff / (60.0 * 60.0) )
        minutes = int( (diff - (hours * 60.0 * 60.0)) / 60.0 )
        return template.render(hours=hours, minutes=minutes)

    def getBathShowerUpdate(self):
        name=self._informationFetcher.getWhoIsInBathShower()
        showerbath=self._informationFetcher.getBathOrShower()
        minutes=self._informationFetcher.getTimeInBathShower()
        outtemp,_,_,condition,_,_,_ = self._informationFetcher.getOutdoor()
        temp=self._informationFetcher.getRoomTemp(Room.BATH_ROOM)
        humidity=self._informationFetcher.getRoomHumidity(Room.BATH_ROOM)
        timehour=datetime.datetime.now().time().hour
        timemin=datetime.datetime.now().time().minute
        template = self._env.get_template('bath-shower-update.txt')
        return template.render(name=name, showerbath=showerbath,minutes=minutes,outtemp=outtemp,condition=condition,temp=temp,humidity=humidity,timehour=timehour,timemin=timemin )

    def getTimeToGoToBed(self):
        template = self._env.get_template('time-to-go-to-bed.txt')
        hour = datetime.datetime.now().time().hour + 1
        return template.render(hour=hour)

    def getWakeupText(self, name):
        template   = self._env.get_template('wakeup.txt')
        name       = name
        fraction, degrees, minutes, seconds = self._informationFetcher.getEarthRotationTime()
        messages   = self._informationFetcher.getNumEmailMessages()
        bathtemp   = self._informationFetcher.getRoomTemp(Room.BATH_ROOM)
        bathhum    = self._informationFetcher.getRoomHumidity(Room.BATH_ROOM)
        planettemp, planethum, planetfeels, conditions, winddir, windspeed, gust = self._informationFetcher.getOutdoor()
        prediction = self._informationFetcher.getPrediction()
        astronauts = self._informationFetcher.getAstronauts()
        return template.render(name=name, degrees=degrees, minutes=minutes, seconds=seconds, messages=messages, bathtemp=bathtemp, bathhum=bathhum, planettemp=planettemp, planethum=planethum, planetfeels=planetfeels, conditions=conditions, winddir=winddir, windspeed=windspeed, gust=gust, prediction=prediction, astronauts=astronauts )

    def getBathToMoisty(self):
        template = self._env.get_template('bath-still-to-moisty.txt')
        return template.render()

    def getWorfsTemperature(self, current, delta):
        template = self._env.get_template('worf-temperature.txt')
        return template.render(current=current, delta=delta)

    def getCo2ToHigh(self, room):
        template = self._env.get_template('co2-to-high.txt')
        return template.render(co2=self._informationFetcher.getRoomCo2Level(room))

    def getRadiationToHigh(self, value):
        template = self._env.get_template('radiation-to-high.txt')
        return template.render(value=value)

    def getRadiationHigherThenAverage(self, valuehere, valueavr):
        template = self._env.get_template('radiation-higher-then-average.txt')
        return template.render(valuehere=valuehere, valueavr=valueavr)

    def getParticulateMatterHigherThenAverage(self, p1, p2):
        template = self._env.get_template('particulate-matter-to-high.txt')
        return template.render(p1=p1, p2=p2)

    def getWaterlevelLow(self):
        template = self._env.get_template('tank-waterlevel-low.txt')
        return template.render()

    def getWaterlevelNormal(self):
        template = self._env.get_template('tank-waterlevel-normal.txt')
        return template.render()

if __name__ == '__main__':
    print "Test templates"
    t = TemplateMatcher()
    #print t.getHourlyTime()
    #print t.getAcknowledgeStartWashingMachine()
    #print t.getWashingMachineReady(234234)
    #print t.getTimeToGoToBed()
    print t.getWakeupText('Ansi')
    #print t.getAcknowledgeStartShower('Ansi')
    #print t.getAcknowledgeEndShower('Ansi')
    #print t.getBathShowerUpdate()
    #print t.getWorfsTemperature(12.12, 01.21)
    #print t.getWorfsTemperature(12.12, -1.21)
    #print t.getWorfsTemperature(12.12, 0.0)
    #print t.getCo2ToHigh(Room.ANSI_ROOM)
    #print t.getRadiationToHigh(0.232)
    #print t.getRadiationHigherThenAverage(0.1, 0.09)
    #print t.getWaterlevelLow()
    #print t.getWaterlevelNormal()
    print t.getParticulateMatterHigherThenAverage(2.2, 3.3)<commit_msg>import cleanup<commit_after>import time
import datetime
from   jinja2             import Environment
from   jinja2             import PackageLoader
from   Room               import Room
from   InformationFetcher import  InformationFetcher

class TemplateMatcher():

    def __init__(self):
        self._env                = Environment(loader=PackageLoader('Template', 'tts-templates'))
        self._informationFetcher = InformationFetcher()

    def getHourlyTime(self):
        template = self._env.get_template('time-hourly.txt')
        hour = datetime.datetime.now().time().hour
        _,_,temp,_,_,_,_ = self._informationFetcher.getOutdoor()
        return template.render(hour=hour, temp=temp)

    def getAcknowledgeStartWashingMachine(self):
        template = self._env.get_template('acknowledge-start-washing-machine.txt')
        return template.render()

    def getAcknowledgeEndWashingMachine(self):
        template = self._env.get_template('acknowledge-end-washing-machine.txt')
        return template.render()

    def getAcknowledgeEmtyingWashingMachine(self):
        template = self._env.get_template('acknowledge-empty-washing-machine.txt')
        return template.render()

    def getAcknowledgeStartShower(self, name):
        template = self._env.get_template('acknowledge-start-shower.txt')
        return template.render(name=name)

    def getAcknowledgeEndShower(self, name):
        template = self._env.get_template('acknowledge-end-shower.txt')
        return template.render(name=name)

    def getAcknowledgeStartBath(self, name):
        template = self._env.get_template('acknowledge-start-bath.txt')
        return template.render(name=name)

    def getAcknowledgeEndBath(self, name):
        template = self._env.get_template('acknowledge-end-bath.txt')
        return template.render(name=name)

    def getAcknowledgeDeactivateBath(self, name):
        template = self._env.get_template('acknowledge-deactivate-everything.txt')
        return template.render(name=name)

    def getWateringTheFlower(self, level):
        template = self._env.get_template('watering-the-flower.txt')
        return template.render(level=level)

    def getWashingMachineReady(self, endtime):
        template = self._env.get_template('washingmachine-ready.txt')
        diff = time.time() - endtime
        hours = int(diff / (60.0 * 60.0) )
        minutes = int( (diff - (hours * 60.0 * 60.0)) / 60.0 )
        return template.render(hours=hours, minutes=minutes)

    def getBathShowerUpdate(self):
        name=self._informationFetcher.getWhoIsInBathShower()
        showerbath=self._informationFetcher.getBathOrShower()
        minutes=self._informationFetcher.getTimeInBathShower()
        outtemp,_,_,condition,_,_,_ = self._informationFetcher.getOutdoor()
        temp=self._informationFetcher.getRoomTemp(Room.BATH_ROOM)
        humidity=self._informationFetcher.getRoomHumidity(Room.BATH_ROOM)
        timehour=datetime.datetime.now().time().hour
        timemin=datetime.datetime.now().time().minute
        template = self._env.get_template('bath-shower-update.txt')
        return template.render(name=name, showerbath=showerbath,minutes=minutes,outtemp=outtemp,condition=condition,temp=temp,humidity=humidity,timehour=timehour,timemin=timemin )

    def getTimeToGoToBed(self):
        template = self._env.get_template('time-to-go-to-bed.txt')
        hour = datetime.datetime.now().time().hour + 1
        return template.render(hour=hour)

    def getWakeupText(self, name):
        template   = self._env.get_template('wakeup.txt')
        name       = name
        fraction, degrees, minutes, seconds = self._informationFetcher.getEarthRotationTime()
        messages   = self._informationFetcher.getNumEmailMessages()
        bathtemp   = self._informationFetcher.getRoomTemp(Room.BATH_ROOM)
        bathhum    = self._informationFetcher.getRoomHumidity(Room.BATH_ROOM)
        planettemp, planethum, planetfeels, conditions, winddir, windspeed, gust = self._informationFetcher.getOutdoor()
        prediction = self._informationFetcher.getPrediction()
        astronauts = self._informationFetcher.getAstronauts()
        return template.render(name=name, degrees=degrees, minutes=minutes, seconds=seconds, messages=messages, bathtemp=bathtemp, bathhum=bathhum, planettemp=planettemp, planethum=planethum, planetfeels=planetfeels, conditions=conditions, winddir=winddir, windspeed=windspeed, gust=gust, prediction=prediction, astronauts=astronauts )

    def getBathToMoisty(self):
        template = self._env.get_template('bath-still-to-moisty.txt')
        return template.render()

    def getWorfsTemperature(self, current, delta):
        template = self._env.get_template('worf-temperature.txt')
        return template.render(current=current, delta=delta)

    def getCo2ToHigh(self, room):
        template = self._env.get_template('co2-to-high.txt')
        return template.render(co2=self._informationFetcher.getRoomCo2Level(room))

    def getRadiationToHigh(self, value):
        template = self._env.get_template('radiation-to-high.txt')
        return template.render(value=value)

    def getRadiationHigherThenAverage(self, valuehere, valueavr):
        template = self._env.get_template('radiation-higher-then-average.txt')
        return template.render(valuehere=valuehere, valueavr=valueavr)

    def getParticulateMatterHigherThenAverage(self, p1, p2):
        template = self._env.get_template('particulate-matter-to-high.txt')
        return template.render(p1=p1, p2=p2)

    def getWaterlevelLow(self):
        template = self._env.get_template('tank-waterlevel-low.txt')
        return template.render()

    def getWaterlevelNormal(self):
        template = self._env.get_template('tank-waterlevel-normal.txt')
        return template.render()

if __name__ == '__main__':
    print "Test templates"
    t = TemplateMatcher()
    #print t.getHourlyTime()
    #print t.getAcknowledgeStartWashingMachine()
    #print t.getWashingMachineReady(234234)
    #print t.getTimeToGoToBed()
    print t.getWakeupText('Ansi')
    #print t.getAcknowledgeStartShower('Ansi')
    #print t.getAcknowledgeEndShower('Ansi')
    #print t.getBathShowerUpdate()
    #print t.getWorfsTemperature(12.12, 01.21)
    #print t.getWorfsTemperature(12.12, -1.21)
    #print t.getWorfsTemperature(12.12, 0.0)
    #print t.getCo2ToHigh(Room.ANSI_ROOM)
    #print t.getRadiationToHigh(0.232)
    #print t.getRadiationHigherThenAverage(0.1, 0.09)
    #print t.getWaterlevelLow()
    #print t.getWaterlevelNormal()
    print t.getParticulateMatterHigherThenAverage(2.2, 3.3)<|endoftext|>


<commit_before>"""
This project is part of the Platypus project.
Copyright 2016 David W. Hogg (NYU).
"""
import os
import pickle as cp
import numpy as np
import sklearn.cluster as cl
from astropy.io import fits
import pylab as plt

def pickle_to_file(fn, stuff):
    fd = open(fn, "wb")
    cp.dump(stuff, fd)
    print("writing", fn)
    fd.close()

def read_pickle_file(fn):
    fd = open(fn, "rb")
    stuff = cp.load(fd)
    fd.close()
    return stuff

def get_data(fn):
    data_labels = np.array(["FE_H",
                            "AL_H", "CA_H", "C_H", "K_H",  "MG_H", "MN_H", "NA_H",
                            "NI_H", "N_H",  "O_H", "SI_H", "S_H",  "TI_H", "V_H"])
    metadata_labels = np.array(["APOGEE_ID", "RA", "DEC", "GLON", "GLAT", "VHELIO_AVG", "DIST_Padova",
                                "TEFF_ASPCAP", "LOGG_ASPCAP"])

    # read FITS file
    print("get_data(): reading table from", fn)
    hdulist = fits.open(fn)
    hdu = hdulist[1]
    cols = hdu.columns
    table = hdu.data
    mask = ((table.field("TEFF_ASPCAP") > 3500.) *
            (table.field("TEFF_ASPCAP") < 5500.) *
            (table.field("LOGG_ASPCAP") > 0.) *
            (table.field("LOGG_ASPCAP") < 3.9) *
            (table.field("DIST_Padova") > 0.))
    data = np.vstack((table.field(label) for label in data_labels)).T
    metadata = np.vstack((table.field(label) for label in metadata_labels)).T
    mask *= np.all(np.isfinite(data), axis=1)
    data = data[mask]
    metadata = metadata[mask]

    # make distances and Galactic coordinates
    distances = (metadata[:, metadata_labels == "DIST_Padova"]).flatten().astype(float)
    ls = (np.pi / 180.) * (metadata[:, metadata_labels == "GLON"]).flatten().astype(float)
    bs = (np.pi / 180.) * (metadata[:, metadata_labels == "GLAT"]).flatten().astype(float)
    print(np.cos(1.2))
    GXs = distances * np.cos(ls) * np.cos(bs) + 8.0 # kpc MAGIC
    GYs = distances * np.sin(ls) * np.cos(bs)
    GZs = distances * np.sin(bs)
    metadata = np.vstack((metadata.T, GXs, GYs, GZs)).T # T craziness
    metadata_labels = np.append(metadata_labels, ["GX", "GY", "GZ"])

    # done!
    print("get_data()", fn, data.shape, metadata.shape)
    return data, data_labels, metadata, metadata_labels

def stats_in_slices(data, xs):
    M = 32
    N = len(xs)
    assert len(data) == N
    ranks = np.zeros(N)
    ranks[np.argsort(xs)] = np.arange(N)
    ranklims = np.arange(N / M, N, N / M)
    xmedians = []
    medians = []
    rmses = []
    for rmin, rmax in zip(ranklims[:-2], ranklims[2:]):
        mask = (ranks > rmin) * (ranks < rmax)
        xmedians.append(np.median(xs[mask]))
        medians.append(np.median(data[mask], axis=0))
    return np.array(xmedians), np.array(medians)

def hogg_savefig(fn):
    print("hogg_savefig():", fn)
    return plt.savefig(fn)

if __name__ == "__main__":
    print("Hello World!")

    # read data
    dfn = "./data/cannon-distances.fits"
    pfn = "./data/gradients.pkl"
    try:
        print("attempting to read pickle", pfn)
        data, data_labels, metadata, metadata_labels = read_pickle_file(pfn)
        print(pfn, data.shape, metadata.shape)
    except:
        print("failed to read pickle", pfn)
        data, data_labels, metadata, metadata_labels = get_data(dfn)
        pickle_to_file(pfn, (data, data_labels, metadata, metadata_labels))
        print(pfn, data.shape, metadata.shape)

    # compute shit
    Rs = (np.sqrt(metadata[:, metadata_labels == "GX"].astype(float) ** 2 +
                  metadata[:, metadata_labels == "GY"].astype(float) ** 2 +
                  metadata[:, metadata_labels == "GZ"].astype(float) ** 2)).flatten()
    Rmedians, datamedians = stats_in_slices(data, Rs)

    # plot whole sample
    plt.clf()
    plt.plot(metadata[:, metadata_labels == "GX"],
             metadata[:, metadata_labels == "GY"], "k.", alpha=0.25)
    plt.xlabel("Galactic X (kpc)")
    plt.ylabel("Galactic Y (kpc)")
    hogg_savefig("GX_GY.png")

    # plot slices
    for i in range(len(data_labels)):
        pfn = data_labels[i] + "_GR.png"
        plt.clf()
        plt.plot(Rmedians, datamedians[:,i], "ko")
        plt.xlabel("Galactic R (kpc)")
        plt.ylabel(data_labels[i] + " (dex)")
        hogg_savefig(pfn)
<commit_msg>works<commit_after>"""
This project is part of the Platypus project.
Copyright 2016 David W. Hogg (NYU).
"""
import os
import pickle as cp
import numpy as np
import sklearn.cluster as cl
from astropy.io import fits
import pylab as plt

def pickle_to_file(fn, stuff):
    fd = open(fn, "wb")
    cp.dump(stuff, fd)
    print("writing", fn)
    fd.close()

def read_pickle_file(fn):
    fd = open(fn, "rb")
    stuff = cp.load(fd)
    fd.close()
    return stuff

def get_data(fn):
    data_labels = ["FE_H",
                   "AL_H", "CA_H", "C_H", "K_H",  "MG_H", "MN_H", "NA_H",
                   "NI_H", "N_H",  "O_H", "SI_H", "S_H",  "TI_H", "V_H"]
    metadata_labels = np.array(["APOGEE_ID", "RA", "DEC", "GLON", "GLAT", "VHELIO_AVG", "DIST_Padova",
                                "TEFF_ASPCAP", "LOGG_ASPCAP"])

    # read FITS file
    print("get_data(): reading table from", fn)
    hdulist = fits.open(fn)
    hdu = hdulist[1]
    cols = hdu.columns
    table = hdu.data
    mask = ((table.field("TEFF_ASPCAP") > 3500.) *
            (table.field("TEFF_ASPCAP") < 5500.) *
            (table.field("LOGG_ASPCAP") > 0.) *
            (table.field("LOGG_ASPCAP") < 3.9) *
            (table.field("DIST_Padova") > 0.))
    data = np.vstack((table.field(label) for label in data_labels)).T
    metadata = np.vstack((table.field(label) for label in metadata_labels)).T
    mask *= np.all(np.isfinite(data), axis=1)
    data = data[mask]
    metadata = metadata[mask]

    # make distances and Galactic coordinates
    distances = (metadata[:, metadata_labels == "DIST_Padova"]).flatten().astype(float)
    ls = (np.pi / 180.) * (metadata[:, metadata_labels == "GLON"]).flatten().astype(float)
    bs = (np.pi / 180.) * (metadata[:, metadata_labels == "GLAT"]).flatten().astype(float)
    print(np.cos(1.2))
    GXs = distances * np.cos(ls) * np.cos(bs) + 8.0 # kpc MAGIC
    GYs = distances * np.sin(ls) * np.cos(bs)
    GZs = distances * np.sin(bs)
    metadata = np.vstack((metadata.T, GXs, GYs, GZs)).T # T craziness
    metadata_labels = np.append(metadata_labels, ["GX", "GY", "GZ"])

    # done!
    print("get_data()", fn, data.shape, metadata.shape)
    return data, data_labels, metadata, metadata_labels

def stats_in_slices(data, xs):
    M = 32
    N = len(xs)
    NN, D = data.shape
    assert NN == N
    ranks = np.zeros(N)
    ranks[np.argsort(xs)] = np.arange(N)
    ranklims = np.arange(N / M, N, N / M)
    M = len(ranklims)
    xmedians = np.zeros(M-2)
    medians = np.zeros((M-2, D))
    rmses = np.zeros((M-2, D))
    for i in range(M-2):
        rmin, rmax = ranklims[i], ranklims[i+2]
        mask = (ranks > rmin) * (ranks < rmax)
        xmedians[i] = np.median(xs[mask])
        medians[i] = np.median(data[mask], axis=0)
        for d in range(D):
            diffs = data[mask, d] - medians[i, d]
            mad = np.median(np.abs(diffs))
            dmask = (np.abs(diffs) < (5. * mad)) # WRONG
            rmses[i, d] = np.sqrt(np.mean(diffs[dmask] ** 2))
    return xmedians, medians, rmses

def hogg_savefig(fn):
    print("hogg_savefig():", fn)
    return plt.savefig(fn)

if __name__ == "__main__":
    print("Hello World!")
    dir = "./gradients_figs"
    if not os.path.exists(dir):
        os.mkdir(dir)

    # read data
    dfn = "./data/cannon-distances.fits"
    pfn = "./data/gradients.pkl"
    try:
        print("attempting to read pickle", pfn)
        data, data_labels, metadata, metadata_labels = read_pickle_file(pfn)
        print(pfn, data.shape, metadata.shape)
    except:
        print("failed to read pickle", pfn)
        data, data_labels, metadata, metadata_labels = get_data(dfn)
        pickle_to_file(pfn, (data, data_labels, metadata, metadata_labels))
        print(pfn, data.shape, metadata.shape)

    # check and adjust data
    N, D = data.shape
    assert len(data_labels) == D
    NN, DD = metadata.shape
    assert N == NN
    assert len(metadata_labels) == DD
    plotdata = data.copy()
    plotdata_labels = data_labels.copy()
    for d in np.arange(1,D):
        plotdata[:,d] = data[:,d] - data[:,0]
        plotdata_labels[d] = data_labels[d] + " - " + data_labels[0]
    print(plotdata_labels)
    plotmetadata = metadata.copy()

    # cut in vertical direction
    zcut = np.abs(metadata[:, metadata_labels == "GZ"].astype(float).flatten()) < 0.1 # kpc
    plotdata = plotdata[zcut]
    plotmetadata = plotmetadata[zcut]

    # compute shit
    Rs = (np.sqrt(plotmetadata[:, metadata_labels == "GX"].astype(float) ** 2 +
                  plotmetadata[:, metadata_labels == "GY"].astype(float) ** 2 +
                  plotmetadata[:, metadata_labels == "GZ"].astype(float) ** 2)).flatten()
    Rmedians, datamedians, rmses = stats_in_slices(plotdata, Rs)

    # plot whole sample
    plt.clf()
    plt.plot(plotmetadata[:, metadata_labels == "GX"],
             plotmetadata[:, metadata_labels == "GY"], "k.", alpha=0.25)
    plt.xlabel("Galactic X (kpc)")
    plt.ylabel("Galactic Y (kpc)")
    hogg_savefig(dir + "/GX_GY.png")

    label_dict = {"FE_H": "[Fe/H] (dex)",
                  "AL_H - FE_H": "[Al/Fe] (dex)",
                  "CA_H - FE_H": "[Ca/Fe] (dex)",
                  "C_H - FE_H":  "[C/Fe] (dex)",
                  "K_H - FE_H":  "[K/Fe] (dex)",
                  "MG_H - FE_H": "[Mg/Fe] (dex)",
                  "MN_H - FE_H": "[Mn/Fe] (dex)",
                  "NA_H - FE_H": "[Na/Fe] (dex)",
                  "NI_H - FE_H": "[Ni/Fe] (dex)",
                  "N_H - FE_H":  "[N/Fe] (dex)",
                  "O_H - FE_H":  "[O/Fe] (dex)",
                  "SI_H - FE_H": "[Si/Fe] (dex)",
                  "S_H - FE_H":  "[S/Fe] (dex)",
                  "TI_H - FE_H": "[Ti/Fe] (dex)",
                  "V_H - FE_H":  "[V/Fe] (dex)",}

    # plot slices
    for d in range(len(plotdata_labels)):
        plotfn = dir + "/" + (plotdata_labels[d] + "_GR.png").replace(" ", "")
        plt.clf()
        plt.plot(Rmedians, datamedians[:,d], "ko")
        for j in range(len(Rmedians)):
            plt.plot([Rmedians[j], Rmedians[j]],
                     [datamedians[j,d] - rmses[j,d], datamedians[j,d] + rmses[j,d]],
                     "k-")
        plt.xlabel("Galactic R (kpc)")
        plt.ylabel(label_dict[plotdata_labels[d]])
        hogg_savefig(plotfn)
<|endoftext|>


<commit_before># First we need to get the pgn file.
import sys
pgnfile = open(sys.argv[1], 'r')
pgn = pgnfile.read()

# Next we need to parse the pgn file for the evaluation scores.  The
# pgn files that this script is programed to parse are generated by
# the program Scid vs PC which encloses evaluation scores in curly
# brackets.  The variation should be stripped to ensure proper
# parsing.
def evalstring2int(es): # Turns evalstring into an integer
    digits = ""
    for char in es:
        if char in "1234567890-":
            digits += char
    return(int(digits))

def getevals(game):
    scores = []
    evalstring = ""
    recording = False
    for char in game:
        if recording:
            if char in "1234567890.-+\n":
                evalstring += char
            else:
                recording = False
                scores.append(evalstring2int(evalstring))
                evalstring = ""
        else:
            if char == "{":
                recording = True
    return(scores)

# Now we do the actual math.
def calcacl(evals):
    whiteloss = []
    blackloss = []
    white = True
    lastscore = 0
    for score in evals:
        if white:
            loss = lastscore - score
            if loss > 0:
                whiteloss.append(loss)
            else:
                whiteloss.append(0)
        else:
            loss = (lastscore - score) * -1
            if loss > 0:
                blackloss.append(loss)
            else:
                blackloss.append(0)
        white = not white
        lastscore = score

    avgwl = round(sum(whiteloss) / len(whiteloss))
    avgbl = round(sum(blackloss) / len(blackloss))
    return((avgwl, avgbl))

def gamespliter(pgndata):
    startingpoints = []
    point = 0
    beginstr = '[Event "'
    while point < len(pgndata):
        if pgndata[point:point + len(beginstr)] == beginstr:
            startingpoints.append(point)
        point += 1

    if len(startingpoints) == 1:
        return([pgndata])
    else:
        games = []
        counter = 0
        while counter < len(startingpoints) - 1:
            games.append(pgndata[startingpoints[counter]:startingpoints[counter + 1]])
            counter += 1
        games.append(pgndata[startingpoints[-1]:])
        return(games)

def parsepgntags(game):
    rawtags = []
    rawtag = ""
    intag = False
    for char in game:
        if intag:
            if char == ']':
                rawtags.append(rawtag)
                rawtag = ""
                intag = False
            else:
                rawtag += char
        else:
            if char == '[':
                intag = True

    tags = []
    for entry in rawtags:
        stage = 0
        tag = ""
        data = ""
        for char in entry:
            if stage == 0:
                if char != " ":
                    tag += char
                else:
                    stage += 1
            elif stage == 1:
                if char == '"':
                    stage += 1
            elif stage == 2:
                if char != '"':
                    data += char
                else:
                    tags.append((tag, data))

    return(tags)

def gettag(taglist, tag):
    for entry in taglist:
        if entry[0] == tag:
            return(entry[1])
    return("Tag not found")

players = []
def playeracl(player, acl):
    global players
    for entry in players:
        if entry[0] == player:
            entry[1].append(acl)
            return()
    players.append([player, [acl]])
    return()

def processgame(game):
    gametags = parsepgntags(game)
    gameevals = getevals(game)
    gameacl = calcacl(gameevals)
    whiteacl = gameacl[0]
    blackacl = gameacl[1]
    playeracl(gettag(gametags, "White"), whiteacl)
    playeracl(gettag(gametags, "Black"), blackacl)
    return

for game in gamespliter(pgn):
    processgame(game)
    acls = calcacl(getevals(game))
    whiteacl = acls[0]
    blackacl = acls[1]
    tags = parsepgntags(game)
    print("Round:", gettag(tags, "Round"))
    print("White:", gettag(tags, "White"))
    print("Black:", gettag(tags, "Black"))
    print("Result:", gettag(tags, "Result"))
    print("Average Centipawn Loss (ACL)")
    print("White ACL:", whiteacl)
    print("Black ACL:", blackacl)
    print("\n")

print("Normal centipawn loss accross all game in file")
for entry in players:
    print(entry[0] + ' -', round(sum(entry[1]) / len(entry[1])))
<commit_msg>changed final output to csv format and fixed a spelling error<commit_after># First we need to get the pgn file.
import sys
pgnfile = open(sys.argv[1], 'r')
pgn = pgnfile.read()

# Next we need to parse the pgn file for the evaluation scores.  The
# pgn files that this script is programed to parse are generated by
# the program Scid vs PC which encloses evaluation scores in curly
# brackets.  The variation should be stripped to ensure proper
# parsing.
def evalstring2int(es): # Turns evalstring into an integer
    digits = ""
    for char in es:
        if char in "1234567890-":
            digits += char
    return(int(digits))

def getevals(game):
    scores = []
    evalstring = ""
    recording = False
    for char in game:
        if recording:
            if char in "1234567890.-+\n":
                evalstring += char
            else:
                recording = False
                scores.append(evalstring2int(evalstring))
                evalstring = ""
        else:
            if char == "{":
                recording = True
    return(scores)

# Now we do the actual math.
def calcacl(evals):
    whiteloss = []
    blackloss = []
    white = True
    lastscore = 0
    for score in evals:
        if white:
            loss = lastscore - score
            if loss > 0:
                whiteloss.append(loss)
            else:
                whiteloss.append(0)
        else:
            loss = (lastscore - score) * -1
            if loss > 0:
                blackloss.append(loss)
            else:
                blackloss.append(0)
        white = not white
        lastscore = score

    avgwl = round(sum(whiteloss) / len(whiteloss))
    avgbl = round(sum(blackloss) / len(blackloss))
    return((avgwl, avgbl))

def gamespliter(pgndata):
    startingpoints = []
    point = 0
    beginstr = '[Event "'
    while point < len(pgndata):
        if pgndata[point:point + len(beginstr)] == beginstr:
            startingpoints.append(point)
        point += 1

    if len(startingpoints) == 1:
        return([pgndata])
    else:
        games = []
        counter = 0
        while counter < len(startingpoints) - 1:
            games.append(pgndata[startingpoints[counter]:startingpoints[counter + 1]])
            counter += 1
        games.append(pgndata[startingpoints[-1]:])
        return(games)

def parsepgntags(game):
    rawtags = []
    rawtag = ""
    intag = False
    for char in game:
        if intag:
            if char == ']':
                rawtags.append(rawtag)
                rawtag = ""
                intag = False
            else:
                rawtag += char
        else:
            if char == '[':
                intag = True

    tags = []
    for entry in rawtags:
        stage = 0
        tag = ""
        data = ""
        for char in entry:
            if stage == 0:
                if char != " ":
                    tag += char
                else:
                    stage += 1
            elif stage == 1:
                if char == '"':
                    stage += 1
            elif stage == 2:
                if char != '"':
                    data += char
                else:
                    tags.append((tag, data))

    return(tags)

def gettag(taglist, tag):
    for entry in taglist:
        if entry[0] == tag:
            return(entry[1])
    return("Tag not found")

players = []
def playeracl(player, acl):
    global players
    for entry in players:
        if entry[0] == player:
            entry[1].append(acl)
            return()
    players.append([player, [acl]])
    return()

def processgame(game):
    gametags = parsepgntags(game)
    gameevals = getevals(game)
    gameacl = calcacl(gameevals)
    whiteacl = gameacl[0]
    blackacl = gameacl[1]
    playeracl(gettag(gametags, "White"), whiteacl)
    playeracl(gettag(gametags, "Black"), blackacl)
    return

for game in gamespliter(pgn):
    processgame(game)
    acls = calcacl(getevals(game))
    whiteacl = acls[0]
    blackacl = acls[1]
    tags = parsepgntags(game)
    print("Round:", gettag(tags, "Round"))
    print("White:", gettag(tags, "White"))
    print("Black:", gettag(tags, "Black"))
    print("Result:", gettag(tags, "Result"))
    print("Average Centipawn Loss (ACL)")
    print("White ACL:", whiteacl)
    print("Black ACL:", blackacl)
    print("\n")

print("Normal centipawn loss accross all games in file")
for entry in players:
    print('"' + entry[0] + '", ', round(sum(entry[1]) / len(entry[1])))
<|endoftext|>


<commit_before>import glob
import hashlib
import os.path
try:
    import urllib2
except ImportError:
    import urllib.request as urllib2

from libearth.compat import text, binary
from libearth.feed import Feed
from libearth.feedlist import Feed as OutLine, FeedList
from libearth.parser.autodiscovery import autodiscovery, FeedUrlNotFoundError
from libearth.parser.heuristic import get_format
from libearth.schema import read, write

from flask import Flask, abort, jsonify, request, url_for

app = Flask(__name__)

app.config.update(dict(
    REPOSITORY='repo/',
    OPML='earthreader.opml'
))

@app.route('/feeds/', methods=['GET'])
def feeds():
    REPOSITORY = app.config['REPOSITORY']
    feedlist = glob.glob(REPOSITORY+'*')
    feeds = []
    for xml in feedlist:
        if not xml.endswith('.xml'):
            continue
        with open(xml) as f:
            feed = read(Feed, f)
            feeds.append({
                'title': text(feed.title),
                'feed_url': url_for(
                    'entries',
                    feed_id=hashlib.sha1(binary(feed.id)).hexdigest(),
                    _external=True)
                })
    return jsonify(feeds=feeds)

@app.route('/feeds/', methods=['POST'])
def add_feed():
    REPOSITORY = app.config['REPOSITORY']
    OPML = app.config['OPML']
    if not os.path.exists(REPOSITORY + OPML):
        if not os.path.isdir(REPOSITORY):
            os.mkdir(REPOSITORY)
        feed_list = FeedList()
    else:
        feed_list = FeedList(REPOSITORY + OPML)
    url = request.form['url']
    f = urllib2.urlopen(url)
    document = f.read()
    try:
        feed_url = autodiscovery(document, url)
    except FeedUrlNotFoundError:
        return 'error'
    if not feed_url == url:
        f.close()
        f = urllib2.urlopen(feed_url)
        xml = f.read()
    else:
        xml = document
    format = get_format(xml)
    result = format(xml, feed_url)
    feed = result[0]
    for link in feed.links:
            if link.relation == 'alternate' and link.mimetype == 'text/html':
                blog_url = link.uri
    outline = OutLine('atom', feed.title.value, feed_url, blog_url)
    feed_list.append(outline)
    feed_list.save_file(REPOSITORY + OPML)
    file_name = hashlib.sha1(binary(feed_url)).hexdigest() + '.xml'
    with open(os.path.join(REPOSITORY, file_name), 'w') as f:
        for chunk in write(feed, indent='    ', canonical_order=True):
            f.write(chunk)
    return 'success'

@app.route('/feeds/<feed_id>/', methods=['DELETE'])
def delete_feed(feed_id):
    REPOSITORY = app.config['REPOSITORY']
    OPML = app.config['OPML']
    if not os.path.exists(REPOSITORY + OPML):
        return 'opml not found'
    else:
        feed_list = FeedList(REPOSITORY + OPML)
    for feed in feed_list:
        if feed_id == hashlib.sha1(binary(feed.xml_url)).hexdigest():
            feed_list.remove(feed)
    feed_list.save_file(REPOSITORY + OPML)
    xml_list = glob.glob(REPOSITORY + '*')
    for xml in xml_list:
        if xml == REPOSITORY + feed_id + '.xml':
            os.remove(xml)
    return 'delete success'

@app.route('/feeds/<feed_id>/')
def entries(feed_id):
    REPOSITORY = app.config['REPOSITORY']
    try:
        with open(os.path.join(REPOSITORY, feed_id + '.xml')) as f:
            feed = read(Feed, f)
            entries = []
            for entry in feed.entries:
                entries.append({
                    'title': text(entry.title),
                    'entry_url': url_for(
                        'entry',
                        feed_id=feed_id,
                        entry_id=hashlib.sha1(binary(entry.id)).hexdigest(),
                        _external=True
                    )
                })
        return jsonify(entries=entries)
    except IOError:
        abort(404)

@app.route('/feeds/<feed_id>/<entry_id>/')
def entry(feed_id, entry_id):
    REPOSITORY = app.config['REPOSITORY']
    try:
        with open(os.path.join(REPOSITORY, feed_id + '.xml')) as f:
            feed = read(Feed, f)
            for entry in feed.entries:
                if entry_id == hashlib.sha1(binary(entry.id)).hexdigest():
                    return jsonify(
                        content=text(entry.content)
                    )
            abort(404)
    except:
        abort(404)
<commit_msg>Add errrorhandler for test<commit_after>import glob
import hashlib
import os.path
try:
    import urllib2
except ImportError:
    import urllib.request as urllib2

from libearth.compat import text, binary
from libearth.feed import Feed
from libearth.feedlist import Feed as OutLine, FeedList
from libearth.parser.autodiscovery import autodiscovery, FeedUrlNotFoundError
from libearth.parser.heuristic import get_format
from libearth.schema import read, write

from flask import Flask, abort, jsonify, request, url_for

import traceback

app = Flask(__name__)

@app.errorhandler(400)
def bad_request_handler_for_testing(exception):
    """Custom error handler of :http:statuscode:`400` for unit testing
    to know how it's going in the application.

    """
    traceback.print_exc(exception)
    return (
        traceback.format_exc(exception),
        400,
        {'Content-Type': 'text/plain; charset=utf-8'}
    )

@app.errorhandler(500)
def server_error_handler_for_testing(exception):
    """Custom error handler of :http:statuscode:`500` for unit testing
    to know how it's going in the application.

    """
    traceback.print_exc(exception)
    return (
        traceback.format_exc(exception),
        500,
        {'Content-Type': 'text/plain; charset=utf-8'}
    )

app.config.update(dict(
    REPOSITORY='repo/',
    OPML='earthreader.opml'
))

@app.route('/feeds/', methods=['GET'])
def feeds():
    REPOSITORY = app.config['REPOSITORY']
    feedlist = glob.glob(REPOSITORY+'*')
    feeds = []
    for xml in feedlist:
        if not xml.endswith('.xml'):
            continue
        with open(xml) as f:
            feed = read(Feed, f)
            feeds.append({
                'title': text(feed.title),
                'feed_url': url_for(
                    'entries',
                    feed_id=hashlib.sha1(binary(feed.id)).hexdigest(),
                    _external=True)
                })
    return jsonify(feeds=feeds)

@app.route('/feeds/', methods=['POST'])
def add_feed():
    REPOSITORY = app.config['REPOSITORY']
    OPML = app.config['OPML']
    if not os.path.exists(REPOSITORY + OPML):
        if not os.path.isdir(REPOSITORY):
            os.mkdir(REPOSITORY)
        feed_list = FeedList()
    else:
        feed_list = FeedList(REPOSITORY + OPML)
    url = request.form['url']
    f = urllib2.urlopen(url)
    document = f.read()
    try:
        feed_url = autodiscovery(document, url)
    except FeedUrlNotFoundError:
        return 'error'
    if not feed_url == url:
        f.close()
        f = urllib2.urlopen(feed_url)
        xml = f.read()
    else:
        xml = document
    format = get_format(xml)
    result = format(xml, feed_url)
    feed = result[0]
    for link in feed.links:
            if link.relation == 'alternate' and link.mimetype == 'text/html':
                blog_url = link.uri
    outline = OutLine('atom', feed.title.value, feed_url, blog_url)
    feed_list.append(outline)
    feed_list.save_file(REPOSITORY + OPML)
    file_name = hashlib.sha1(binary(feed_url)).hexdigest() + '.xml'
    with open(os.path.join(REPOSITORY, file_name), 'w') as f:
        for chunk in write(feed, indent='    ', canonical_order=True):
            f.write(chunk)
    return 'success'

@app.route('/feeds/<feed_id>/', methods=['DELETE'])
def delete_feed(feed_id):
    REPOSITORY = app.config['REPOSITORY']
    OPML = app.config['OPML']
    if not os.path.exists(REPOSITORY + OPML):
        return 'opml not found'
    else:
        feed_list = FeedList(REPOSITORY + OPML)
    for feed in feed_list:
        if feed_id == hashlib.sha1(binary(feed.xml_url)).hexdigest():
            feed_list.remove(feed)
    feed_list.save_file(REPOSITORY + OPML)
    xml_list = glob.glob(REPOSITORY + '*')
    for xml in xml_list:
        if xml == REPOSITORY + feed_id + '.xml':
            os.remove(xml)
    return 'delete success'

@app.route('/feeds/<feed_id>/')
def entries(feed_id):
    REPOSITORY = app.config['REPOSITORY']
    try:
        with open(os.path.join(REPOSITORY, feed_id + '.xml')) as f:
            feed = read(Feed, f)
            entries = []
            for entry in feed.entries:
                entries.append({
                    'title': text(entry.title),
                    'entry_url': url_for(
                        'entry',
                        feed_id=feed_id,
                        entry_id=hashlib.sha1(binary(entry.id)).hexdigest(),
                        _external=True
                    )
                })
        return jsonify(entries=entries)
    except IOError:
        abort(404)

@app.route('/feeds/<feed_id>/<entry_id>/')
def entry(feed_id, entry_id):
    REPOSITORY = app.config['REPOSITORY']
    try:
        with open(os.path.join(REPOSITORY, feed_id + '.xml')) as f:
            feed = read(Feed, f)
            for entry in feed.entries:
                if entry_id == hashlib.sha1(binary(entry.id)).hexdigest():
                    return jsonify(
                        content=text(entry.content)
                    )
            abort(404)
    except:
        abort(404)
<|endoftext|>


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

import os
from subprocess import Popen, PIPE, STDOUT

class Virtualenv(object):

    def __init__(self, config):
        '''Install a new virtual environment for the new project.'''

        self.config = config

        if self.check_virtualenv_installed():
            self.create_virtualenv()

    @property
    def venv_name(self):
        '''Build venv_name.'''
        prefix = getattr(self.config, 'venv_prefix', None)
        if not prefix:
            return self.config.project_name
        else:
            name = ''.join([prefix, self.config.project_name])
            return name

    @property
    def venv_path(self):
        '''Return path to new virtualenvironment.'''

        return os.path.join(self.config.venv_path, self.venv_name)

    def check_virtualenv_installed(self):
        '''Check virtualenv is installed.'''

        error_msg = 'Virtualenv does not appear to be installed on your '\
                    'system. Please install python-virtualenv to use '\
                    'this feature of Skeletor.'
        try:
            import virtualenv
        except ImportError:
            self.config.cli_opts.error(error_msg)
            return False
        else:
            # Lets double check
            cmd = 'which virtualenv'
            p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
                      close_fds=True)
            output = p.stdout.read()
            if output == '':
                self.config.cli_opts.error(error_msg)
                return False
        return True

    def validate(self):
        '''Validate virtualenv creattion.'''

        path = self.config.venv_path
        full_path = self.venv_path

        if not os.path.exists(self.config.venv_path):
            self.config.cli_opts.error('Specififed virtual environment path '\
                                       'does not exist: %s' % path)

        if os.path.exists(self.venv_path):
            self.config.cli_opts.error('A virtual environment at %s '\
                                       'already exists' % full_path)

        return True

    def create_virtualenv(self):
        '''Create python virtualevironment.'''

        if self.validate():
            print 'Creating python virtual environment at %s ...' % (
                self.venv_path)
            cmd = 'cd %s && virtualenv %s' % (self.config.venv_path,
                                              self.venv_path)
            if not getattr(self.config, 'venv_use_site_packages', False):
                cmd += ' --no-site-packages'
            print 'Runing: ' + cmd

            os.system(cmd)
<commit_msg>Tidying up virtualenv module<commit_after>#!/usr/bin/env python
# encoding: utf-8

import os
from subprocess import Popen, PIPE, STDOUT

class Virtualenv(object):

    def __init__(self, config):
        '''Install a new virtual environment for the new project.'''

        self.config = config

        if self.check_virtualenv_installed():
            self.create_virtualenv()

    @property
    def venv_name(self):
        '''Build venv_name.'''
        prefix = getattr(self.config, 'venv_prefix', None)
        if not prefix:
            return self.config.project_name
        else:
            name = ''.join([prefix, self.config.project_name])
            return name

    @property
    def venv_path(self):
        '''Return path to new virtualenvironment.'''

        return os.path.join(self.config.venv_path, self.venv_name)

    def check_virtualenv_installed(self):
        '''Check virtualenv is installed.'''

        error_msg = 'Virtualenv does not appear to be installed on your '\
                    'system. Please install python-virtualenv to use '\
                    'this feature of Skeletor.'
        try:
            import virtualenv
        except ImportError:
            self.config.cli_opts.error(error_msg)
            return False
        else:
            # Lets double check
            cmd = 'which virtualenv'
            p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
                      close_fds=True)
            output = p.stdout.read()
            if output == '':
                self.config.cli_opts.error(error_msg)
                return False
        return True

    def validate(self):
        '''Validate virtualenv creattion.'''

        path = self.config.venv_path
        full_path = self.venv_path

        if not os.path.exists(self.config.venv_path):
            self.config.cli_opts.error('Specififed virtual environment path '
                                       'does not exist: %s' % path)

        if os.path.exists(self.venv_path):
            self.config.cli_opts.error('A virtual environment at %s '
                                       'already exists' % full_path)

        return True

    def create_virtualenv(self):
        '''Create python virtualevironment.'''

        if self.validate():
            print 'Creating python virtual environment at %s ...' % (
                self.venv_path)
            cmd = 'cd %s && virtualenv %s' % (self.config.venv_path,
                                              self.venv_path)
            if not getattr(self.config, 'venv_use_site_packages', False):
                cmd += ' --no-site-packages'
            print 'Runing: ' + cmd

            os.system(cmd)
<|endoftext|>


<commit_before>"""Convert access tokens to user records."""

# pylint: disable=no-member

from app import models
from app import app
from app.authenticator import AuthenticationException

from google.appengine.api import memcache as mc

from flask import request

from google.appengine.api import users

MC_NAMESPACE = "access-token"

def authenticate():
    """Returns the user which made this request."""
    authenticator = app.config["AUTHENTICATOR"]
    user = users.get_current_user()
    if user:
        return models.User.get_or_insert(user.email().lower())

    if 'access_token' not in request.args:
        print 'ANON user'
        return models.User.get_or_insert("<anon>")
    else:
        access_token = request.args['access_token']
        print 'USING ACCESS TOKEN', access_token
        user = mc.get("%s-%s" % (MC_NAMESPACE, access_token))
        if user:
            return user
        try:
            email = authenticator.authenticate(access_token)
        except AuthenticationException:
            return models.User.get_or_insert('_anon')
        user = models.User.get_or_insert(email)
        mc.set("%s-%s" % (MC_NAMESPACE, access_token), user)
    return user
<commit_msg>Do not print access token<commit_after>"""Convert access tokens to user records."""

# pylint: disable=no-member

from app import models
from app import app
from app.authenticator import AuthenticationException

from google.appengine.api import memcache as mc

from flask import request

from google.appengine.api import users

MC_NAMESPACE = "access-token"

def authenticate():
    """Returns the user which made this request."""
    authenticator = app.config["AUTHENTICATOR"]
    user = users.get_current_user()
    if user:
        return models.User.get_or_insert(user.email().lower())

    if 'access_token' not in request.args:
        print 'ANON user'
        return models.User.get_or_insert("<anon>")
    else:
        access_token = request.args['access_token']
        # print 'USING ACCESS TOKEN', access_token
        user = mc.get("%s-%s" % (MC_NAMESPACE, access_token))
        if user:
            return user
        try:
            email = authenticator.authenticate(access_token)
        except AuthenticationException:
            return models.User.get_or_insert('_anon')
        user = models.User.get_or_insert(email)
        mc.set("%s-%s" % (MC_NAMESPACE, access_token), user)
    return user
<|endoftext|>