text:
import asyncio
import inspect
import logging
import functools
from copy import copy
from enum import Enum
from uuid import uuid4
from datetime import datetime, timezone

from tukio.dag import DAG
from tukio.utils import FutureState, Listen, SkipTask, TimeoutHandle
from tukio.broker import get_broker, workflow_exec_topics
from tukio.event import Event, EventSource
from tukio.task import (
    TaskTemplate, TaskRegistry, UnknownTaskName, TukioTask, TukioTaskError,
)

log = logging.getLogger(__name__)

class WorkflowError(Exception):
    pass

class WorkflowRootTaskError(WorkflowError):

    def __init__(self, value):
        super().__init__()
        self._value = value

    def __str__(self):
        return 'expected one root task, found {}'.format(self._value)

class TemplateGraphError(WorkflowError):

    def __init__(self, key):
        super().__init__()
        self._key = key

    def __str__(self):
        return 'graph error on task id: {}'.format(self._key)

class RescueError(WorkflowError):

    def __init__(self, uid, reason):
        super().__init__()
        self._uid = uid
        self._reason = reason

    def __str__(self):
        return (
            'cannot rescue workflow id {} from its last execution report. '
            'reason: {}'.format(self._uid, self._reason)
        )

class WorkflowExecState(Enum):

    BEGIN = 'workflow-begin'
    END = 'workflow-end'
    ERROR = 'workflow-error'
    PROGRESS = 'workflow-progress'
    SUSPEND = 'workflow-suspend'
    RESUME = 'workflow-resume'

    @classmethod
    def values(cls):
        return [member.value for member in cls]

class OverrunPolicy(Enum):

    """
    The overrun policy defines what to do if the previous execution of a
    workflow didn't finish yet and a new event must be dispatched by the
    workflow engine.
    This class also defines policy handlers that can create new instances
    of workflow execution objects according to the current overrun policy and
    to the list of running workflow instances (with the same template).
    """

    # Skip until all running instances are finished
    SKIP = 'skip'
    # Start a new workflow instance whatever the running instances
    START_NEW = 'start-new'
    # Skip until all running instances had been unlocked
    SKIP_UNTIL_UNLOCK = 'skip-until-unlock'
    # Abort all running instances before creating a new once
    ABORT_RUNNING = 'abort-running'

    @classmethod
    def get_default_policy(cls):
        """
        Returns the default overrun policy.
        """
        return cls('skip-until-unlock')

    @classmethod
    def get(cls, policy=None):
        if policy is None:
            return cls.get_default_policy()
        if isinstance(policy, cls):
            return policy
        return cls(policy)

class OverrunPolicyHandler:

    """
    This class defines overrun policy handlers to create new instances
    of workflow execution objects according to the current overrun policy and
    to the list of running workflow instances (with the same template).
    """

    __slots__ = ('_loop', 'template', 'policy')

    def __init__(self, template, loop=None):
        self._loop = loop
        # Store the workflow template for further use in policy handlers.
        self.template = template
        self.policy = template.policy

    def new_workflow(self, running=None):
        method = getattr(self, '_' + self.policy.name.lower())
        return method(running or [])

    def _check_wflow(self, wflow):
        return wflow.template.uid == self.template.uid

    def _new_wflow(self):
        return Workflow(self.template, loop=self._loop)

    def _skip(self, running):
        """
        Run a new instance of workflow only if there's no instance already
        running with the same template ID.
        """
        for instance in running:
            if self._check_wflow(instance):
                return None
        return self._new_wflow()

    def _start_new(self, _):
        """
        Always run a new instance of workflow.
        """
        return self._new_wflow()

    def _skip_until_unlock(self, running):
        """
        Run a new instance of workflow only if all the instances already
        running have been unlocked. Refer to the `Workflow` docstring for more
        details about locked/unlocked workflows.
        """
        for instance in running:
            if self._check_wflow(instance):
                if instance.lock.locked():
                    # There's at least 1 locked workflow instance
                    return None
        return self._new_wflow()

    def _abort_running(self, running):
        """
        Abort all running instances of the workflow before creating a new one.
        """
        for instance in running:
            if self._check_wflow(instance):
                instance.cancel()
        return self._new_wflow()

class WorkflowTemplate:

    """
    A workflow template is a DAG (Directed Acyclic Graph) made up of task
    template objects (`TaskTemplate`). This class is not a workflow execution
    engine.
    It provides an API to easily build and update a consistent workflow.
    """

    __slots__ = ('uid', 'topics', 'policy', 'dag', 'timeout', 'schema')

    def __init__(self, uid=None, policy=None, topics=None, timeout=None,
                 schema=None):
        self.uid = uid or str(uuid4())
        self.topics = topics
        self.policy = OverrunPolicy.get(policy)
        self.dag = DAG()
        self.timeout = timeout
        self.schema = schema or 1

    @property
    def tasks(self):
        return list(self.dag.graph.keys())

    @property
    def listen(self):
        return Listen.get(self.topics)

    def add(self, task_tmpl):
        """
        Adds a new task template to the workflow. The task will remain orphan
        until it is linked to upstream/downstream tasks.
        This method must be passed a `TaskTemplate()` instance.
        """
        if not isinstance(task_tmpl, TaskTemplate):
            raise TypeError("expected a 'TaskTemplate' instance")
        self.dag.add_node(task_tmpl)

    def delete(self, task_tmpl):
        """
        Remove a task template from the workflow and delete the links to
        upstream/downstream tasks.
        """
        self.dag.delete_node(task_tmpl)

    def root(self):
        """
        Returns the root task. If no root task or several root tasks were found
        raises `WorkflowValidationError`.
        """
        root_task = self.dag.root_nodes()
        nb = len(root_task)
        if nb == 1:
            return root_task[0]
        raise WorkflowRootTaskError(nb)

    def link(self, up_task_tmpl, down_task_tmpl):
        """
        Create a directed link from an upstream to a downstream task.
        """
        self.dag.add_edge(up_task_tmpl, down_task_tmpl)

    def unlink(self, task_tmpl1, task_tmpl2):
        """
        Remove the link between two tasks.
        """
        try:
            self.dag.delete_edge(task_tmpl1, task_tmpl2)
        except KeyError:
            self.dag.delete_edge(task_tmpl2, task_tmpl1)

    @classmethod
    def from_dict(cls, wf_dict):
        """
        Build a new workflow description from the given dictionary.
        The dictionary takes the form of:
            {
                "id": <workflow-uid>,
                "topics": [<a-topic>, <another-topic>],
                "policy": <policy>,
                "timeout": <timeout>,
                "schema": <int>,
                "tasks": [
                    {"id": <task-uid>, "name": <name>, "config": <cfg-dict>},
                    ...
                ],
                "graph": {
                    <t1-uid>: [t2-uid, <t3-uid>],
                    <t2-uid>: [],
                    ...
                }
            }

        See below the conditions applied to trigger a workflow according to the
        value of 'topics':
            {"topics": None}
            try to trigger a workflow each time data is received by the engine
            ** default behavior **

            {"topics": []}
            never try to trigger a workflow when data is received by the engine

            {"topics": ["blob", "foo"]}
            try to trigger a workflow when data is received by the engine in
            topics "blob" and "foo" only
        """
        wf_tmpl = cls(
            uid=wf_dict.get('id'),
            policy=wf_dict.get('policy'),
            topics=wf_dict.get('topics'),
            timeout=wf_dict.get('timeout'),
            schema=wf_dict.get('schema'),
        )

        # Tasks
        task_ids = dict()
        for task_dict in wf_dict.get('tasks', []):
            task_tmpl = TaskTemplate.from_dict(task_dict)
            wf_tmpl.add(task_tmpl)
            task_ids[task_tmpl.uid] = task_tmpl

        # Graph
        for up_id, down_ids_set in wf_dict.get('graph', {}).items():
            up_tmpl = task_ids[up_id]
            for down_id in down_ids_set:
                down_tmpl = task_ids[down_id]
                wf_tmpl.link(up_tmpl, down_tmpl)

        # Graph validation
        try:
            wf_tmpl.dag.validate()
        except KeyError as exc:
            raise TemplateGraphError(exc.args[0]) from exc

        return wf_tmpl

    def as_dict(self):
        """
        Builds and returns a dictionary that represents the current workflow
        template object.
        """
        wf_dict = {
            'id': self.uid,
            'policy': self.policy.value,
            'topics': self.topics,
            'timeout': self.timeout,
            'schema': self.schema,
            'tasks': [],
            'graph': {},
        }
        for task_tmpl in self.tasks:
            wf_dict['tasks'].append(task_tmpl.as_dict())
        for up_tmpl, down_tmpls in self.dag.graph.items():
            entry = {up_tmpl.uid: list(map(lambda x: x.uid, down_tmpls))}
            wf_dict['graph'].update(entry)
        return wf_dict

    def copy(self):
        """
        Returns a copy of the current instance of workflow template.
        """
        wf_tmpl = self.__class__(uid=self.uid)
        wf_tmpl.dag = self.dag.copy()
        return wf_tmpl

    def validate(self):
        """
        Validate the current workflow template. At that point, we already know
        the underlying DAG is valid. This method ensures there's a single root
        task and all task names are registered tasks.
        If not valid, this method should raise either `WorkflowRootTaskError`
        or `UnknownTaskName` exceptions.
        """
        root_nodes = len(self.dag.root_nodes())
        if root_nodes != 1:
            raise WorkflowRootTaskError(root_nodes)
        for task in self.tasks:
            TaskRegistry.get(task.name)
        return True

    def __str__(self):
        """
        Human readable string representation of a workflow template.
        """
        return '<WorkflowTemplate uid={}>'.format(self.uid)

def _current_workflow(func):
    """
    A decorator to maintain the dictionary of currently running workflows.
    Inspired from the implementation of `asyncio.Task`.
    """
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        self.__class__._current_workflows[self._loop] = self
        try:
            return func(self, *args, **kwargs)
        finally:
            self.__class__._current_workflows.pop(self._loop)
    return wrapper

def _get_workflow_from_task(task):
    """
    Looks for an instance of `Workflow` linked to the task or a method of
    `Workflow` among the done callbacks of the asyncio task. Returns None if
    not found.
    If the task was triggered from within a workflow it MUST have a `workflow`
    attribute that points to it and at least one done callback that is a method
    of `Workflow`.
    """
    if isinstance(task, TukioTask):
        workflow = task.workflow
    if workflow:
        return workflow

    for cb in task._callbacks:
        # inspect.getcallargs() gives access to the implicit 'self' arg of
        # the bound method but it is marked as deprecated since
        # Python 3.5.1 and the new `inspect.Signature` object does NOT do
        # the job :((
        if inspect.ismethod(cb):
            inst = cb.__self__
        elif isinstance(cb, functools.partial):
            try:
                inst = cb.func.__self__
            except AttributeError:
                continue
        else:
            continue
        if isinstance(inst, Workflow):
            return inst
    return None

class Workflow(asyncio.Future):

    """
    This class handles the execution of a workflow. Tasks are created along the
    way of workflow execution.
    """

    # Inspired from the implementation of `asyncio.Task`
    _current_workflows = {}

    __slots__ = (
        'uid', '_template', '_start', '_end', 'tasks', '_tasks_by_id',
        '_updated_next_tasks', '_done_tasks', '_internal_exc', '_must_cancel',
        'lock', '_broker', '_source', '_committed', '_timed_out',
    )

    @classmethod
    def current_workflow(cls, loop=None):
        """
        Returns the currently running workflow in an event loop or None.
        By default the current workflow for the current event loop is returned.
        None is returned when called not in the context of a Workflow.
        """
        loop = loop or asyncio.get_event_loop()
        task = asyncio.current_task(loop)
        workflow = None
        if task:
            workflow = _get_workflow_from_task(task)
        if not workflow:
            workflow = cls._current_workflows.get(loop)
        return workflow

    def __init__(self, wf_tmpl, *, loop=None, broker=None):
        super().__init__(loop=loop)
        self.uid = str(uuid4())
        self._template = wf_tmpl
        # Start and end datetime (UTC) of the execution of the workflow
        self._start, self._end = None, None
        # Set of tasks executed at some point. Items of that set are
        # instances of `asyncio.Task`
        self.tasks = set()
        self._tasks_by_id = dict()
        # This dict references all tasks that updated the set of their
        # downstream tasks at runtime. Keys are `asyncio.Task` objects and
        # values are sets of task template IDs.
        self._updated_next_tasks = dict()
        self._done_tasks = set()
        self._internal_exc = None
        self._must_cancel = False
        self.lock = asyncio.Lock()
        # Create the workflow in the 'locked' state when its overrun policy is
        # 'skip-until-unlock'.
        if self.policy is OverrunPolicy.SKIP_UNTIL_UNLOCK:
            self.lock._locked = True
        # Work with an event broker
        self._broker = broker or get_broker(self._loop)
        self._source = EventSource(
            workflow_template_id=self._template.uid,
            workflow_exec_id=self.uid
        )
        # A 'committed' workflow is a pending workflow not suspended
        self._committed = asyncio.Event()
        self._timed_out = False

    @property
    def template(self):
        return self._template

    @property
    def policy(self):
        return self._template.policy

    @property
    def committed(self):
        return self._committed.is_set()

    @property
    def timed_out(self):
        return self._timed_out

    @_current_workflow
    def _unlock(self, _):
        """
        The concept of 'locked workflow' only applies when the overrun policy
        is set to 'skip-until-unlock'. Once the workflow is unlocked, a new
        execution from the same template ID can be triggered.
        """
        if self.lock.locked():
            self.lock.release()

    def unlock_when_task_done(self, task=None):
        """
        Adds a done callback to the passed or current task so as to unlock the
        workflow when the task gets done.
        """
        task = task or asyncio.current_task()
        task.add_done_callback(self._unlock)

    def _register_to_broker(self, task_tmpl, task):
        """
        Registers the `data_received` callback of the task (if defined) into
        the event broker to handle new data received during task execution.
        """
        listen = task_tmpl.listen
        # Task is configured to receive no data during execution
        if listen is Listen.nothing:
            return

        # A tukio task always has a 'data_received' method
        try:
            callback = task.data_received
        except AttributeError as exc:
            task.set_exception(exc)
            raise

        # Register the callback in the event broker
        if listen is Listen.everything:
            self._broker.register(callback)
        else:
            for topic in task_tmpl.topics:
                self._broker.register(callback, topic=topic)

        # Unregister this callback as soon as the task will be done.
        done_cb = functools.partial(self._unregister_from_broker, callback,
                                    topics=task_tmpl.topics)
        task.add_done_callback(done_cb)

    @_current_workflow
    def _unregister_from_broker(self, callback, _, topics=None):
        """
        A very simple wrapper around `Broker.unregister()` to ignore the future
        object passed as argument by asyncio to all done callbacks.
        """
        if topics is None:
            itertopics = [None]
        else:
            itertopics = topics
        for topic in itertopics:
            try:
                self._broker.unregister(callback, topic=topic)
            except KeyError as exc:
                log.error('failed to unregister callback: %s', exc)
                self._internal_exc = exc
        if self._internal_exc:
            self._cancel_all_tasks()

    @_current_workflow
    def run(self, event):
        """
        Execute the workflow following the description passed at init.
        """
        # A workflow can be ran only once
        if self.tasks:
            raise RuntimeError('A workflow can be run only once!')

        # Run the root task
        self._committed.set()
        try:
            root_tmpl = self._template.root()
        except WorkflowRootTaskError as exc:
            self._internal_exc = exc
            self._try_mark_done()
            task = None
        else:
            # Automatically wrap input data into an event object
            if isinstance(event, dict):
                event = Event(event)
            self._dispatch_exec_event(WorkflowExecState.BEGIN, copy(event))
            task = self._new_task(root_tmpl, event)
            self._start = datetime.now(timezone.utc)
            # The workflow may fail to start at once
            if not task:
                self._try_mark_done()

        # Handle the workflow's timeout, if any.
        if self._template.timeout:
            TimeoutHandle(self, self._template.timeout).start()

        return task

    def _new_task(self, task_tmpl, event):
        """
        Each new task must be created successfully, else the whole workflow
        shall stop running (wrong workflow config or bug).
        It is assumed all tasks are tukio tasks (aka `TukioTask` objects).
        """
        try:
            task = task_tmpl.new_task(event, loop=self._loop)
            # Register the `data_received` callback (if required) as soon as
            # the execution of the task is scheduled.
            self._register_to_broker(task_tmpl, task)
        except (UnknownTaskName, TypeError, ValueError, AttributeError) as exc:
            log.error('failed to create task from template: %s', task_tmpl)
            self._internal_exc = exc
            self._cancel_all_tasks()
            return None

        # Link the task to this workflow and send a broker BEGIN event.
        task.setup_workflow(self, task_tmpl)
        log.debug('New task created for %s', task_tmpl)

        def next_tasks(future):
            asyncio.ensure_future(self._run_next_tasks(future))

        task.add_done_callback(next_tasks)
        self.tasks.add(task)
        # Create the exec dict of the task
        self._tasks_by_id[task_tmpl.uid] = task
        return task

    def _join_task(self, next_task, event):
        """
        Pass an event to a downstream task that has already been started (by
        another parent). In such a situation, it is known to be a join task.
        """
        # Push event into next_task's event queue
        asyncio.ensure_future(next_task.data_received(event))

    def _get_next_task_templates(self, task_tmpl, task):
        """
        Retrieves the downstream tasks of `task_tmpl` and filters it with the
        template IDs that (may) have been provided at runtime by `task`.
        """
        succ_tmpls = self._template.dag.successors(task_tmpl)
        try:
            tmpl_ids = self._updated_next_tasks[task]
        except KeyError:
            return succ_tmpls
        filtered_tmpls = []
        for tid in tmpl_ids:
            for tmpl in succ_tmpls:
                if tmpl.uid == tid:
                    filtered_tmpls.append(tmpl)
                    break
            else:
                # This is a misconfiguration from the task. Ignore it to
                # leave the opportunity to execute the other next tasks.
                log.error('ID %s not in downstream tasks of %s', tid, task)
        log.debug('%s filtered next tasks to: %s', task, filtered_tmpls)
        return filtered_tmpls

    def _dispatch_exec_event(self, etype, data=None):
        """
        A shorthand to dispatch information about the workflow execution along
        the way.
        """
        if isinstance(data, Event):
            data = data.data
        self._broker.dispatch(
            {'type': etype.value, 'content': data},
            topics=workflow_exec_topics(self.uid),
            source=self._source,
        )

    @_current_workflow
    async def _run_next_tasks(self, task):
        """
        A callback to be added to each task in order to select and schedule
        asynchronously downstream tasks once the parent task is done.
        """
        self._done_tasks.add(task)
        if self._must_cancel:
            self._try_mark_done()
            return

        # Don't execute downstream tasks if the task's result is an exception
        # (may include task cancellation) but don't stop executing the other
        # branches of the workflow.
        try:
            result = task.result()
        except SkipTask:
            # If the task has been skipped, we just forward the previous task
            # inputs to the next one.
            result = task.inputs
            log.info(
                'Task execution %s has been skipped (%s)',
                task.uid[:8], task.template,
            )
        except asyncio.TimeoutError:
            if task.timed_out is False:
                raise
            # Task timed out, go on with the workflow
            result = task.outputs
            log.info(
                'Task execution %s has timed out (%s)',
                task.uid[:8], task.template,
            )
        except asyncio.CancelledError:
            # Workflow cancelled
            log.info('Task %s has been cancelled', task.template)
            self._try_mark_done()
            return
        except TukioTaskError as exc:
            log.warning(
                'Task %s ended on error: %s', task.template, exc.task_outputs
            )
            self._try_mark_done()
            return
        except Exception as exc:
            log.warning('Task %s ended on exception', task.template)
            log.exception(exc)
            self._try_mark_done()
            return

        # Ensure the result is not None (task ended without a return)
        if result is None:
            result = task.inputs

        # Ensure the workflow is not suspended, else wait for a resume.
        try:
            await self._committed.wait()
        except asyncio.CancelledError:
            # If cancelled, the workflow will return with the statement below.
            log.debug('Suspended workflow cancelled')
            return

        source = EventSource(
            workflow_template_id=self.template.uid,
            workflow_exec_id=self.uid,
            task_template_id=task.template.uid,
            task_exec_id=task.uid
        )
        # Go through each child task
        for tmpl in self._get_next_task_templates(task.template, task):
            # Wrap result from parent task into an event object
            event = Event(result, source=source)
            next_task = self._tasks_by_id.get(tmpl.uid)
            if next_task:
                # Ignore done tasks
                if next_task.done():
                    continue
                # Downstream task already running, join it!
                self._join_task(next_task, event)
                continue

            # Create new task
            try:
                next_task = self._new_task(tmpl, event)
            except Exception as exc:
                # Something wrong happened during the task instantiation.
                # We log the exception and move on since we can't decide
                # what to do at this point.
                log.exception(exc)
                continue

            if not next_task:
                break

        self._try_mark_done()

    def _try_mark_done(self):
        """
        If nothing left to execute, the workflow must be marked as done. The
        result is set to either the exec graph (represented by a dict) or to an
        exception raised at task creation.
        """
        if not self._committed.is_set():
            # Won't mark a workflow 'done' if it's suspended
            return

        # Note: the result of the workflow may already have been set by another
        # done callback from another task executed in the same iteration of the
        # event loop.
        if self._all_tasks_done() and not self.done():
            exec_event = WorkflowExecState.END
            if self._internal_exc:
                self.set_exception(self._internal_exc)
                exec_event = WorkflowExecState.ERROR
                data = self._internal_exc
            elif self._must_cancel:
                super().cancel()
                data = {'cancel': True}
            else:
                self.set_result(self.tasks)
                data = None
            self._end = datetime.now(timezone.utc)
            self._dispatch_exec_event(exec_event, data=data)

    def _all_tasks_done(self):
        """
        Returns True if all tasks are done, else returns False.
        Here, a task is considered as 'done' only if it is marked as done and
        its `_run_next_tasks()` done callback has been called.
        """
        if self.tasks == self._done_tasks:
            return True
        else:
            return False

    def _cancel_all_tasks(self):
        """
        Cancels all pending tasks and returns the number of tasks cancelled.
        """
        self._must_cancel = True
        cancelled = 0
        pending = self.tasks - self._done_tasks
        for task in pending:
            is_cancelled = task.cancel()
            if is_cancelled:
                cancelled += 1
                try:
                    task.holder.teardown()
                except AttributeError:
                    pass
        return cancelled

    def set_next_tasks(self, task_tmpl_ids):
        """
        By default the workflow runs all downstream tasks once the current task
        is done. This method allows to select the tasks that will be actually
        ran and disables other tasks (unless they're already running).

        This method is intended to be called at runtime by the task itself.
        `task_tmpl_ids` must be a list (can be empty) of task template IDs.
        """
        task = asyncio.current_task(self._loop)
        if task not in self.tasks:
            raise RuntimeError('task {} not executed by {}'.format(task, self))
        self._updated_next_tasks[task] = task_tmpl_ids

    def suspend(self):
        """
        Suspend the execution of this workflow prevents any new task from
        starting.
        """
        if not self._committed.is_set():
            return
        self._committed.clear()

        # Suspend a task means the task will be cancelled with the state
        # 'suspended', which can be used later to resume the workflow
        for task in (self.tasks - self._done_tasks):
            task.suspend()

        self._dispatch_exec_event(WorkflowExecState.SUSPEND)
        log.info('workflow %s has been suspended', self)

    def resume(self):
        """
        Resume the execution of this workflow allows new tasks to start.
        """
        if self._committed.is_set():
            log.error('Cannot resume a workflow that has not been suspended')
            return
        self._committed.set()

        # Next tasks are done waiting for '_committed' asyncio event
        # The 'suspended' ones needs to be re-executed
        for task in self._done_tasks:
            if FutureState.get(task) is not FutureState.suspended:
                continue
            event = Event(task.inputs, source=task.event_source)
            self._new_task(task.template, event)

        self._dispatch_exec_event(WorkflowExecState.RESUME)
        log.info('workflow %s has been resumed', self)

    def cancel(self):
        """
        Cancel the workflow by cancelling all pending tasks (aka all tasks not
        marked as done). We must wait for all tasks to be actually done before
        marking the workflow as cancelled (hence done).
        """
        if not self._committed.is_set():
            self._committed.set()
            self._try_mark_done()

        cancelled = self._cancel_all_tasks()
        if cancelled == 0:
            # Tasks where already all done.If not,
            # the last task will end the workflow already set as _must_cancel
            super().cancel()
        return True

    def timeout(self):
        """
        Called automatically after reaching the 'timeout' threshold,
        if it exists for this workflow.
        """
        self.cancel()
        self._timed_out = True

    def __str__(self):
        """
        Readable string representation of a workflow execution object.
        """
        string = '<Workflow template.uid={}, uid={}, start={}, end={}>'
        return string.format(self._template.uid, self.uid,
                             self._start, self._end)

    def report(self):
        """
        Creates and returns a complete execution report, including workflow and
        tasks templates and execution details.
        """
        report = self._template.as_dict()
        report['exec'] = {
            'id': self.uid,
            'start': self._start,
            'end': self._end,
            'state': FutureState.get(self).value
        }
        # Update task descriptions to add info about their execution.
        for task_dict in report['tasks']:
            try:
                task = self._tasks_by_id[task_dict['id']]
            except KeyError:
                task_dict['exec'] = None
                continue
            task_dict['exec'] = task.as_dict()
            # If the task is linked to a task holder, try to use its own report
            if hasattr(task.holder, 'report'):
                try:
                    task_dict['exec']['reporting'] = task.holder.report()
                except Exception as exc:
                    # Unexpected error from task reporting.
                    log.error('Exception on task reporting: %s', exc)
                    self._internal_exc = exc
                    self._try_mark_done()
        return report

    def fast_forward(self, report):
        """
        Restore a workflow and its tasks states, parsing an execution report.
        """
        self.uid = report['exec']['id']
        self._start = report['exec']['start']
        self._end = report['exec'].get('end')
        self._source = EventSource(
            workflow_template_id=self._template.uid,
            workflow_exec_id=self.uid
        )
        log.info('fast forward workflow %s', self)

        # Build a list of task in the graph that need to be executed
        resume = list()

        def browse(entry, parent=None):
            """
            Dive into the dag to:
                - ensure executed task contexts are restored.
                - find tasks that need to be executed.
            """
            t_template = next(
                t for t in report['tasks'] if t['id'] == entry.uid
            )
            t_report = t_template.get('exec')
            if not t_report:
                if not parent:
                    raise RescueError(self.uid, 'root task never been started')
                # No execution report found, the task needs to be executed
                resume.append((entry, None, parent))
                return

            t_state = FutureState(t_report['state'])
            if not t_state.done():
                # Pending, suspended or cancelled tasks need to be executed
                resume.append((entry, t_template, parent))
                return

            if t_state.done():
                # We won't manually create an asyncio task since it won't be
                # run (the task is 'done'). Yet, we need a memory print of its
                # execution (mainly for reporting purposes).
                t_shadow = type('TukioTaskShadow', (object,), {
                    'done': lambda: True,
                    'cancelled': lambda: False,
                    '_exception': None,
                    'as_dict': lambda: t_report
                })

                # Add this task to the tracking sets
                self.tasks.add(t_shadow)
                self._done_tasks.add(t_shadow)
                self._tasks_by_id[entry.uid] = t_shadow

                # Recursive browing
                for t_next in self._template.dag.successors(entry):
                    browse(t_next, t_template)
        browse(self._template.root())

        # Resume/start tasks that need to be executed
        for task_template, task_report, parent_report in resume:
            if parent_report:
                inputs = parent_report['exec']['outputs']
                source = EventSource(
                    workflow_template_id=self.template.uid,
                    workflow_exec_id=self.uid,
                    task_template_id=parent_report['id'],
                    task_exec_id=parent_report['exec']['id']
                )
                event = Event(inputs, source=source)
            else:
                # No parent report = root task
                event = Event(task_report['exec']['inputs'])
            self._new_task(task_template, event)

        self._dispatch_exec_event(WorkflowExecState.BEGIN)

def new_workflow(wf_tmpl, running=None, loop=None):
    """
    Returns a new workflow execution object if a new instance can be run.
    It depends on the template's overrun policy and the list of running
    workflow instances (given by `running`).
    """
    policy_handler = OverrunPolicyHandler(wf_tmpl, loop=loop)
    return policy_handler.new_workflow(running)


text:
<?php

function input_filename($$create_path = false) {
  $$path = get_session_path();
  if ($$create_path) system("mkdir -p $$path");
  return "$$path/input";
}

$$inputFile=input_filename(true);

if (get_field('exampleList','none') !== 'none' ) {
   copy(get_field('exampleList','none'),$$inputFile);
} else if (isset($$_FILES['userfile']) && $$_FILES['userfile']['name']) {
   move_uploaded_file($$_FILES['userfile']['tmp_name'], $$inputFile);
} else if (get_field('textInput','empty') !== 'empty' ) {
   file_put_contents($$inputFile, get_field('textInput','empty'));
} else {
   copy('$defaultInput$',$$inputFile);
};

$$problem = file_get_contents($$inputFile);

print_r($$_POST);
   ?>

  <form name="runForm" enctype="multipart/form-data" action="#output" method="POST">

      Input (in trs format)

	<select class='input-list' name='exampleList' onchange="this.form.submit();">
	  <option value='none'>select example …</option>
	  $for(examples)$
          <option value='$filepath$' <?php if (field_set_as('exampleList','$filepath$')) {print 'selected';}; ?>>$name$</option>
	  $endfor$
	</select>
	or upload file
	<input label="browse" name="userfile" type="file" size="20" onchange="this.form.submit()" title="Upload a file in trs format."/>

	<textarea name="textInput" spellcheck="false"><?=$$problem?></textarea>

    $partial("templates/strategy-list.html")$

  </form>

redpajama_set_name: RedPajamaGithub


text:
<?php

namespace RightSignature;

use RightSignature\Util\ArrayHelpers as ArrayHelpers;
use RightSignature\Util\XmlHelpers as XmlHelpers;

/**
 * An intermediate document returned from a Prepackage Template call.
 *
 * @see https://rightsignature.com/apidocs/api_calls?api_method=prepackageTemplate
 */
class PrepackagedDocument
    extends \RightSignature\Util\ArrayDecorator
{
    const ACTION_SEND = 'send';
    const ACTION_PREFILL = 'prefill';

    const EMBEDDED_SIGNING_EMAIL = 'noemail@rightsignature.com';

    private $_client;

    public function __construct($client, $data)
    {
        $this->_client = $client;
        parent::__construct($data);
    }

    /**
     * Creates a prefilled document via the Prefill Template call.
     *
     * $args is an array like:
     *
     *   array(
     *     'subject' => '...',
     *     'roles' => array(
     *       array(
     *         'role_name' => '...',         // or role_id
     *         'name' => '...',
     *         'email' => '...',             // omit for embedded signing
     *       ),
     *       // ...
     *     ),
     *     'merge_fields' => array(
     *       array(
     *         'merge_field_name' => '...', // or merge_field_id
     *         'value' => '...',
     *         'locked => true,             // optional
     *       ),
     *     ),
     *     // other args per API documentatation
     *   )
     *
     * @see https://rightsignature.com/apidocs/api_calls?api_method=prefillTemplate
     *
     * @param array $args
     *
     * @return RightSignature\Document
     */
    public function prefill($args)
    {
        $payload = array_merge($args, array(
            'guid' => $this->guid,
            'action' => self::ACTION_PREFILL,
        ));
        $body = self::_preparePrefillRequest($payload);
        $response = $this->_client->post('/api/templates.xml', $body);

        return new Document(self::_parsePrefillResponse($response));
    }

    /**
     * Creates a prefilled document via the Prefill Template call and sends it for
     * signing.
     *
     * See prefill() for expected argument structure.
     *
     * @see https://rightsignature.com/apidocs/api_calls?api_method=prefillTemplate
     *
     * @param array $args
     *
     * @return string sent document GUID
     */
    public function prefillAndSend($args)
    {
        $payload = array_merge($args, array(
            'guid' => $this->guid,
            'action' => self::ACTION_SEND,
        ));

        foreach ($payload['roles'] as &$role) {
            if (!isset($role['email'])) {
                $role['email'] = self::EMBEDDED_SIGNING_EMAIL;
            }
        }

        $body = self::_preparePrefillRequest($payload);

        $xml = $this->_client->post('/api/templates.xml', $body);
        $data = XmlHelpers::toArray($xml);
        assert($data['status'] == 'sent');

        return $data['guid'];
    }

    // ----------------------------------------
    // Private

    /**
     * Transform an array-tree of args into a Prefill Template XML payload.
     *
     * @param array $args
     *
     * @return string
     */
    private static function _preparePrefillRequest($args)
    {
        foreach (array('guid', 'action', 'subject', 'roles') as $k) {
            ArrayHelpers::ensureIsSet($args, $k);
        }

        $roles = $args['roles'];
        foreach ($roles as &$role) {
            $idKey = isset($role['role_name']) ? 'role_name' : 'role_id';
            $role['@attributes'] = array($idKey => $role[$idKey]);
            unset($role[$idKey]);
        }
        $args['roles'] = array('role' => $roles);

        if (isset($args['merge_fields'])) {
            $mergeFields = $args['merge_fields'];
            foreach ($mergeFields as &$mergeField) {
                $idKey = isset($mergeField['merge_field_name']) ? 'merge_field_name' : 'merge_field_id';
                $mergeField['@attributes'] = array($idKey => $mergeField[$idKey]);
                unset($mergeField[$idKey]);
            }
            $args['merge_fields'] = array('merge_field' => $mergeFields);
        }

        // TODO: tags

        return XmlHelpers::toXml(array('template' => $args));
    }

    /**
     * Parse the XML response of a Prefill Template call.
     */
    private static function _parsePrefillResponse($xml)
    {
        $array = ArrayHelpers::normaliseKeys(XmlHelpers::toArray($xml));
        $array = ArrayHelpers::collapseGroup($array, 'merge_fields');
        $array = ArrayHelpers::collapseGroup($array, 'roles');
        $array = ArrayHelpers::collapseGroup($array, 'pages');

        return $array;
    }
}


text:
package com.zeppamobile.frontend.webpages;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LogoutHomeServlet
 */
public class LogoutHomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("home.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<TITLE>
UltrasoundDAO
</TITLE>

<META NAME="keywords" CONTENT="org.cidrz.webapp.dynasite.dao.UltrasoundDAO class">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../../stylesheet.css" TITLE="Style">

      Overview
      Package
    Class
      Use
      Tree
      Deprecated
      Index
      Help

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  All Classes

  SUMMARY: NESTED | FIELD | CONSTR | METHOD

DETAIL: FIELD | CONSTR | METHOD

org.cidrz.webapp.dynasite.dao

Class UltrasoundDAO

java.lang.Object
  org.cidrz.webapp.dynasite.dao.UltrasoundDAO

public class UltrasoundDAO
extends java.lang.Object

Author:

Chris Kelley
         Date: Oct 10, 2005
         Time: 1:59:25 PM

Constructor Summary

UltrasoundDAO()

Method Summary

static java.util.List
getExams(java.lang.Integer examSequence,
         java.lang.Long patientId,
         java.lang.Long pregnancyId,
         java.lang.Long formId,
         java.lang.Class clazz)

          Fetch all fetal ultrasound exams for this patient/pregnancy/ultrasound encounter using the exam sequence number.

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail

UltrasoundDAO

public UltrasoundDAO()

Method Detail

getExams

public static java.util.List getExams(java.lang.Integer examSequence,
                                      java.lang.Long patientId,
                                      java.lang.Long pregnancyId,
                                      java.lang.Long formId,
                                      java.lang.Class clazz)
                               throws java.io.IOException,
                                      javax.servlet.ServletException,
                                      java.sql.SQLException

Fetch all fetal ultrasound exams for this patient/pregnancy/ultrasound encounter using the exam sequence number.

Parameters:
patientId -
pregnancyId -
formId -
clazz -

Returns:

Throws:

java.io.IOException

javax.servlet.ServletException

java.sql.SQLException

      Overview
      Package
    Class
      Use
      Tree
      Deprecated
      Index
      Help

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  All Classes

  SUMMARY: NESTED | FIELD | CONSTR | METHOD

DETAIL: FIELD | CONSTR | METHOD


text:
package com.murach.newsreader;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ItemsActivity extends Activity
implements OnItemClickListener {

    private final String URL_STRING = "http://rss.cnn.com/rss/cnn_tech.rss";
    private final String FILENAME = "news_feed.xml";

    private RSSFeed feed;

    private TextView titleTextView;
    private ListView itemsListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_items);

        titleTextView = (TextView) findViewById(R.id.titleTextView);
        itemsListView = (ListView) findViewById(R.id.itemsListView);

        itemsListView.setOnItemClickListener(this);

        new DownloadFeed().execute();
    }

    class DownloadFeed extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {

            try{
                // get the URL
                URL url = new URL(URL_STRING);

                // get the input stream
                InputStream in = url.openStream();

                // get the output stream
                Context context = ItemsActivity.this;
                FileOutputStream out =
                    context.openFileOutput(FILENAME, Context.MODE_PRIVATE);

                // read input and write output
                byte[] buffer = new byte[1024];
                int bytesRead = in.read(buffer);
                while (bytesRead != -1)
                {
                    out.write(buffer, 0, bytesRead);
                    bytesRead = in.read(buffer);
                }
                out.close();
                in.close();
            }
            catch (IOException e) {
                Log.e("News reader", e.toString());
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.d("News reader", "Feed downloaded: " + new Date());
            new ReadFeed().execute();
        }
    }

    class ReadFeed extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                // get the XML reader
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser parser = factory.newSAXParser();
                XMLReader xmlreader = parser.getXMLReader();

                // set content handler
                RSSFeedHandler theRssHandler = new RSSFeedHandler();
                xmlreader.setContentHandler(theRssHandler);

                // read the file from internal storage
                FileInputStream in = openFileInput(FILENAME);

                // parse the data
                InputSource is = new InputSource(in);
                xmlreader.parse(is);

                // set the feed in the activity
                ItemsActivity.this.feed = theRssHandler.getFeed();
            }
            catch (Exception e) {
                Log.e("News reader", e.toString());
            }
            return null;
        }

        // This is executed after the feed has been read
        @Override
        protected void onPostExecute(Void result) {
            Log.d("News reader", "Feed read: " + new Date());

            // update the display for the activity
            ItemsActivity.this.updateDisplay();
        }
    }

    public void updateDisplay()
    {
        if (feed == null) {
            titleTextView.setText("Unable to get RSS feed");
            return;
        }

        // set the title for the feed
        titleTextView.setText(feed.getTitle());

        // get the items for the feed
        ArrayList<RSSItem> items = feed.getAllItems();

        // create a List of Map<String, ?> objects
        ArrayList<HashMap<String, String>> data =
                new ArrayList<HashMap<String, String>>();
        for (RSSItem item : items) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("date", item.getPubDateFormatted());
            map.put("title", item.getTitle());
            data.add(map);
        }

        // create the resource, from, and to variables
        int resource = R.layout.listview_item;
        String[] from = {"date", "title"};
        int[] to = {R.id.pubDateTextView, R.id.titleTextView};

        // create and set the adapter
        SimpleAdapter adapter =
            new SimpleAdapter(this, data, resource, from, to);
        itemsListView.setAdapter(adapter);

        Log.d("News reader", "Feed displayed: " + new Date());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {

        // get the item at the specified position
        RSSItem item = feed.getItem(position);

        // create an intent
        Intent intent = new Intent(this, ItemActivity.class);

        intent.putExtra("pubdate", item.getPubDate());
        intent.putExtra("title", item.getTitle());
        intent.putExtra("description", item.getDescription());
        intent.putExtra("link", item.getLink());

        this.startActivity(intent);
    }
}


text:
"""Code to read a model, and test attention switching.

This code reads a precomputed model file, and tests the AAD/SSD performance
using a test file. The code creates summary plots and calculates final accuracy.
"""

import collections
import numbers
import os
from typing import Dict, List, Optional, Tuple, Union

from absl import app
from absl import flags
from absl import logging

import matplotlib
# The next change breaks colab, so add "%matplotlib inline" after importing
# this file.
# pylint: disable=g-import-not-at-top
matplotlib.use('Agg')    # Needed for plotting to a file, before the next import
import matplotlib.pyplot as plt

import numpy as np

from telluride_decoding import attention_decoder
from telluride_decoding import brain_data
from telluride_decoding import brain_model
from telluride_decoding import cca
from telluride_decoding import infer_decoder

import tensorflow as tf

base_tf_dir = '/tmp/data/aad'
default_tf_dir = os.path.join(base_tf_dir, '001_2019-09-19/tfrecords/all')

base_summary_dir = '/tmp/infer_results'

default_model_dir = os.path.join(base_summary_dir,
                                 'model')
default_plot_dir = os.path.join(base_summary_dir,
                                'plots')

default_test_file = os.path.join(base_tf_dir,
                                 'test', 'Trial 05.tfrecords')

flags.DEFINE_string('tf_dir', default_tf_dir,
                    'Location of the training data for evaluation.')
flags.DEFINE_string('model_dir', default_model_dir,
                    'Location of the saved BrainModel Keras model')
flags.DEFINE_string('plot_dir', default_plot_dir,
                    'Where to store result plots')
flags.DEFINE_string('save_results_csv', None,
                    'Path to results csv file')

flags.DEFINE_multi_string('train_files', [],
                          'Training files to calculate parameters for the final'
                          'decoding test.')
flags.DEFINE_multi_string('test_files', default_test_file,
                          'Testing files to evaluate with which to verify '
                          'performance.')

flags.DEFINE_integer('window_width', 1000,
                     'Number of frames of data to use when estimating '
                     'correlation.')
flags.DEFINE_integer('window_step', 500,
                     'Number of frames to step the window when estimating the '
                     'correlation.')
flags.DEFINE_float('window_overlap', 0.5,
                   'Factor of window width for overlapping test windows.')
if 'frame_rate' not in flags.FLAGS:
  flags.DEFINE_float('frame_rate', 100,
                     'EEG and audio frame rates in Hz, after decoding.')
flags.DEFINE_enum('reduction', 'lda',
                  ['first', 'second', 'lda', 'mean', 'mean-squared', 'all'],
                  'How to reduce decoder dimensionality to a scalar.')

# Define variable so we can use for valid flags and iterate over the valid
# options in run_comparison_test()
allowable_decoder_types = ['wta', 'stepped', 'ssd']

flags.DEFINE_enum('decoder', 'wta', allowable_decoder_types,
                  'How to summarize multiple correlation windows.')
flags.DEFINE_bool('window_test', False,
                  'Run a test with different window sizes')
flags.DEFINE_bool('comparison_test', False,
                  'Run a test with all decoders and infers')
flags.DEFINE_string('audio_label', 'loudness',
                    'TFRecord field containing the audio signal.'
                    'The second speaker is this label with 2 appended.')

FLAGS = flags.FLAGS

def create_brain_data(tf_dir: str,
                      train_files: Union[str, List[str]],
                      test_files: Union[str, List[str]],
                      params: Dict[str, Union[int, str]],
                      audio_label: str) -> brain_data.BrainData:
  """Creates a braindata object from which we can get train and test datasets.

  Args:
    tf_dir: Where to find the training and testing files.
    train_files: A list of files to be used for training (within tf_dir).
      Can also be a regular expression.
    test_files: A list of files to be used for testing (within tf_dir).
      Can also be a regular expression.
    params: A dictionary with context parameters needed for preprocessing,
      and field names for feature dictionary.
    audio_label: Which feature from the tfrecord file contains the audio
      data for this experiment. Typically loudness or loudness2.

  Returns:
    A BrainData object, from which we can then select train or testing files.
  """
  if isinstance(train_files, str):
    train_files = [train_files,]
  train_file_re = '|'.join(['%s' % s for s in train_files])

  if isinstance(test_files, str):
    test_files = [test_files,]
  test_file_re = '|'.join(['%s' % s for s in test_files])

  print('Training data from %s -> %s with label %s.' %
        (tf_dir, train_file_re, audio_label))
  print('Testing data from %s -> %s with label %s.' %
        (tf_dir, test_file_re, audio_label))

  if 'attended_field' in params:
    attended = params['attended_field']
  else:
    # This is the default name for the attention signal during ingestion.
    attended = 'attend'

  exp_brain_data = brain_data.TFExampleData(
      params['input_field'],    # Network input
      audio_label,              # Network output
      100,                      # Frame rate
      pre_context=params['pre_context'],
      post_context=params['post_context'],
      in2_fields=audio_label,
      in2_pre_context=params['input2_pre_context'],
      in2_post_context=params['input2_post_context'],
      attended_field=attended,
      final_batch_size=200,
      repeat_count=1,
      # Shuffle_buffer_size must be 0 so we don't shuffle in test mode.
      # Otherwise the inference stage won't see data in the right order!!!
      shuffle_buffer_size=0,
      data_dir=tf_dir,
      data_pattern='',
      train_file_pattern=train_file_re,
      validate_file_pattern='',
      test_file_pattern=test_file_re,
      )
  return exp_brain_data

def calculate_time_axis(
    data: Union[numbers.Number, float, List[float], np.ndarray],
    window_step: int, window_width: int, frame_rate: float) -> np.ndarray:
  """Calculates the time axis (in minutes) for a windowed signal.

  Data originates by windowing data with the given window width and step.
  The returned time corresponds to the *center* of the analysis window.

  Args:
    data: Sample data frame count. Can also be an array for sizing.
    window_step: Number of samples by which the window is advanced.
    window_width: Analysis window width.
    frame_rate: The underlying sample rate, before windowing, in samples/second.

  Returns:
    A one-dimensional numpy array, with the true time (in minutes) of the start
    of each analysis window.
  """
  if isinstance(data, numbers.Number):
    num_points = int(data)
  elif isinstance(data, list):
    num_points = len(data)
  elif isinstance(data, np.ndarray):
    num_points = data.shape[0]
  else:
    raise TypeError('Unknown type passed as input argument.')
  return (np.arange(num_points)*window_step+window_width/2.0)/frame_rate/60.0

def get_data_for_model(
    tf_dir: str, train_files: str, test_files: str,
    model_object: infer_decoder.Decoder,
    audio_label_1: str, audio_label_2: str) -> Tuple[tf.data.Dataset,
                                                     tf.data.Dataset,
                                                     tf.data.Dataset,
                                                     tf.data.Dataset]:
  """Gets all the brain_data objects needed for training and testing.

  Args:
    tf_dir: where to find all the .tfrecords files.
    train_files: a regular expression matching the training files in tf_dir.
    test_files: a regular expression matching the testing files in tf_dir.
    model_object: a brain_model object which we use to make sure the sizes
      of the train and test data are compatible.
    audio_label_1: Field name for the audio from speaker 1
    audio_label_2: Field name for the audio from speaker 2

  Returns:
    Four brain_data objects:
      training file for speaker 1 (loudness feature)
      test file for speaker 1 (loudness feature)
      training file for speaker 2 (loudness2 feature)
      test file for speaker 2 (loudness2 feature)
  """
  brain_data_1 = create_brain_data(tf_dir, train_files, test_files,
                                   model_object.decoding_model_params,
                                   audio_label_1)  # Speaker 1
  brain_data_2 = create_brain_data(tf_dir, train_files, test_files,
                                   model_object.decoding_model_params,
                                   audio_label_2)  # Speaker 2

  bd1_train = brain_data_1.create_dataset(mode='train')
  bd1_test = brain_data_1.create_dataset(mode='test')
  bd2_train = brain_data_2.create_dataset(mode='train')
  bd2_test = brain_data_2.create_dataset(mode='test')

  model_object.check_model_and_data(bd1_train)
  model_object.check_model_and_data(bd1_test)
  model_object.check_model_and_data(bd2_train)
  model_object.check_model_and_data(bd2_test)
  print('Finished the model checking.')
  return bd1_train, bd1_test, bd2_train, bd2_test

def regress_and_correlate(model_object: infer_decoder.Decoder,
                          test_data: tf.data.Dataset,
                          window_size: int) -> Tuple[List[float], List[float]]:
  """Run the model, combining frames of results, and averaging for each window.

  Args:
    model_object: The decoder object (after picking the appropriate dimensions.)
    test_data: Dataset stream to use for testing this model
    window_size: How many frames to use for computing the window's results.

  Returns:
    Two lists, one of correlations and the other of the averaged label over the
    window.
  """
  full_results = []
  labels = []
  for results, label in model_object.test_by_window(test_data, window_size):
    full_results.append(np.mean(results))
    labels.append(np.mean(label))
  return full_results, labels

def load_model(model_dir: str,
               reducer: str) -> infer_decoder.Decoder:
  """Loads a TF model from disk.

  Args:
    model_dir: Where to find the TF model on disk.
    reducer: What type of correlation reducer to use, a string.

  Returns:
    An infer_decoder.Decoder object.

  Raises:
    IOError: Raises error if we can't load the model parameters for decoding.
  """
  model_object = infer_decoder.create_decoder(model_dir.lower(),
                                              reduction=reducer)

  brain_object_dict = {'pearson_correlation': brain_model.pearson_correlation,
                       'BrainCcaLayer': cca.BrainCcaLayer,
                       'cca_pearson_correlation_first':
                           cca.cca_pearson_correlation_first,
                      }
  model_object.load_decoding_model(model_dir, brain_object_dict)
  decoder_param_filename = os.path.join(model_dir, 'decoder_model.json')
  if tf.io.gfile.exists(decoder_param_filename):
    model_object.restore_parameters(decoder_param_filename)
  else:
    raise IOError('Can not load decoder model parameters from %s' %
                  decoder_param_filename)
  return model_object

def find_first_segment(labels: Union[List[float], np.ndarray]) -> int:
  """Finds the first section of data based on the ground truth.

  This is used to train the SSD's probability models, by looking for the first
  segment, all with the subject attending to the same speaker.

  Args:
    labels: An np array of segment labels, generally 0 for speaker 1 and 1 for
      speaker 2.

  Returns:
    The frame number at the end of the first segment with all the same label.
  """
  if isinstance(labels, list):
    labels = np.asarray(labels)
  if not isinstance(labels, np.ndarray):
    raise TypeError('Labels input must be an ndarray, not %s' % type(labels))
  if labels.ndim != 1:
    raise TypeError('Labels input must be one-dimensional, not %s' %
                    str(labels.shape))
  end_section = np.nonzero(np.logical_xor(labels, labels[0]))
  if end_section[0].shape[0]:
    return end_section[0][0]
  return 0

def run_reduction_test(model_dir: str, tf_dir: str, train_files: str,
                       test_files: str, reduction: str, decoder_type: str,
                       audio_label_1: str, audio_label_2: str,
                       plot_dir: Optional[str] = None) -> Dict[int, float]:
  """Runs a complete test for a given reduction and decoder type.

  For the given precomputed decoding model:
    Train the inference parameters (correlation means and LDA parameters) if
    necessary. Then for each window size:
      Compute likelihoods/correlations for the model given each audio signal.
      Find consistent section at the start to train SSD decoder probabilities.
      Calculate fraction of decoding results that are correct
      Plot results for this reducer and decoder type.

  Args:
    model_dir: Where to find the model used to initialize the data. If there is
      no model, then the train_file pattern is used to find training data.
    tf_dir: A directory containing .tfrecord files for training and/or testing.
    train_files: Which files to use for training, a regular expression for files
      in tf_dir.
    test_files: Which files to use for testing, a regular expression in tf_dir.
    reduction: One of the valid CCA reduction techniques
    decoder_type: One of the valid AttentionDecoder type names.
    audio_label_1: Field name for the audio from speaker 1
    audio_label_2: Field name for the audio from speaker 2
    plot_dir: Where to put a summary plot.

  Returns:
    A dictionary keyed by window length and giving fraction of correctly
    infered frames.
  """
  print('Running regression test with %s.' % reduction)
  model_object = load_model(model_dir, reduction)

  # Get brain_decoding (bd) dataset objects for each audio source and for
  # train (inference parameters) and test (performance) conditions.
  bd1_train, bd1_test, bd2_train, bd2_test = get_data_for_model(
      tf_dir, train_files, test_files, model_object,
      audio_label_1, audio_label_2)

  if model_object.decoding_model_params:
    print('Found saved model, no need to train the decoding model.')
    print(f' Decoding_params are: {model_object.decoding_model_params}')
  else:
    # Calculate correlation means and LDA parameters
    model_object.train(bd1_train, bd2_train)
    print('Finished the inference model training.')

  window_results = []
  window_list = [10, 100, 200, 400, 700, 1000]
  for window_size in window_list:
    window_step = window_size // 2

    d1_results, _ = regress_and_correlate(model_object, bd1_test,
                                          window_size)
    # The attention labels are embedded in the training data.
    d2_results, labels = regress_and_correlate(model_object, bd2_test,
                                               window_size)

    decoder = attention_decoder.create_attention_decoder(
        decoder_type, window_step=window_step, frame_rate=FLAGS.frame_rate)

    end_first_section = find_first_segment(labels)
    if end_first_section:
      # Grab just the decision score and drop the upper/lower confidence bounds.
      decoder.tune(d1_results[:end_first_section],
                   d2_results[:end_first_section])
    else:
      logging.info('Could not find both true and false values in the '
                   'attention signal: %s. Not tuning decoder', labels)

    # Calculate the decoded attention results.
    attention = np.array([decoder.attention(c1, c2) for c1, c2
                          in zip(d1_results, d2_results)])
    labels = np.reshape(np.asarray(labels), (-1, 1))
    # Note: Attention is a logical variable indicating attention to first stream

    # TODO: Really should always get a boolean.
    # Note: returned attention signal is a logical saying attending to speaker 0
    correct = np.logical_xor(attention[:, 0:1] >= 0.5, labels)
    frac_correct = np.sum(correct)/float(len(correct))
    window_results.append(frac_correct)

    d1_results = np.reshape(np.asarray(d1_results), (-1, 1))
    d2_results = np.reshape(np.asarray(d2_results), (-1, 1))

    if plot_dir:
      tf.io.gfile.makedirs(plot_dir)
      title = 'AAD Correlation on %s with %gs windows %g%% accuracy.' % (
          os.path.split(test_files[0])[1], window_size/100.0,
          frac_correct*100.0)
      t = calculate_time_axis(np.asarray(d1_results), window_step,
                              window_size, FLAGS.frame_rate)
      plt.clf()
      attention_decoder.plot_aad_results(np.asarray(d1_results), t=t,
                                         linecolor='blue')
      attention_decoder.plot_aad_results(np.asarray(d2_results), t=t,
                                         linecolor='red')
      scale = max(float(np.max(np.asarray(d1_results))),
                  float(np.max(np.asarray(d2_results))))
      attention_decoder.plot_aad_results(attention[:, 0]*scale/2.0, t=t,
                                         attention_flag=np.asarray(labels),
                                         decision_upper=attention[:, 1],
                                         decision_lower=attention[:, 2],
                                         linecolor='green',
                                         title=title)
      plt.legend(('Speaker 1', 'Speaker 2', 'Decision'))

      plot_file = os.path.join(plot_dir, 'test_results_%s_%s_%05d.png' %
                               (reduction, decoder_type, window_size))
      with tf.io.gfile.GFile(plot_file, 'wb') as fp:
        assert fp, 'Can not open %s for plotting AAD' % plot_file
        plt.savefig(fp)
      print('Saved final test attention switch result plot to', plot_file)

  print('Infer classification result for %s with %s and %s: %s' %
        (os.path.join(tf_dir, test_files[0]), reduction,
         decoder_type, window_results))
  if FLAGS.save_results_csv is not None:
    print('Saving results to {}'.format(FLAGS.save_results_csv))
    with open(FLAGS.save_results_csv, 'w') as f:
      f.write('Window size,Accuracy\n')
      for (wl, wr) in zip(window_list, window_results):
        f.write('{},{}\n'.format(wl, wr))
  if plot_dir:
    plt.clf()
    plt.semilogx(window_list, window_results)
    plt.xlabel('Window Size (frames)')
    plt.ylabel('Fraction correct')
    plt.title('Testing with %s, reducing with %s, decoding with %s' %
              (os.path.split(test_files[0])[1], reduction, decoder_type))
    plot_file = os.path.join(plot_dir, 'test_results_%s_%s.png' %
                             (reduction, FLAGS.decoder))
    with tf.io.gfile.GFile(plot_file, 'wb') as fp:
      assert fp, 'Can not open %s for plotting AAD' % plot_file
      plt.savefig(fp)
    print('Saved final test classification result plot to', plot_file)
  return dict(zip(window_list, window_results))

def run_comparison_test(model_dir: str, tf_dir: str,
                        train_files: str, test_files: str,
                        audio_label: str, audio_label_2: str, plot_dir: str,
                        reduction_list: List[str],
                        decoder_list: Optional[List[str]] = None):
  """Runs a test comparing all the different reducers and decoders.

  Calls run_reduction_test for each kind of decoder (wta, ssd, etc) and each
  kind of reducer (lda, first, etc).

  Args:
    model_dir: Directory containing the pretrained model.
    tf_dir: Directory containing the TFRecord data for training and testing.
    train_files: Regular expression that picks the training data.
    test_files: Regular expression that picks the testing data.
    audio_label: The feature name for the attended audio loudness data.
    audio_label_2: The feature name for the unattended audio loudness data.
    plot_dir: Where to store the output plots.
    reduction_list: A list of valid reduction types to test.
    decoder_list: A list of valid decoder types to test.

  Returns:
    A dictionary of test results, keyed by the reduction and decoder types.
  """
  all_results = collections.OrderedDict()
  for reduction in reduction_list:
    for decoder in decoder_list or allowable_decoder_types:
      print('Running the regression test with %s and %s.' %
            (reduction, decoder))
      results = run_reduction_test(model_dir, tf_dir,
                                   train_files, test_files,
                                   reduction, decoder,
                                   audio_label, audio_label_2,
                                   plot_dir)
      label = (reduction, decoder)
      all_results[label] = results

  plt.clf()
  for reduction_decoder, results in all_results.items():
    if reduction_decoder[0] != 'lda':
      style = '--'
    else:
      style = '-'
    sizes = sorted(results.keys())
    acc = [results[s] for s in sizes]
    plt.semilogx(sizes, acc, style, label='%s %s' % reduction_decoder)

  if plot_dir:
    plt.xlabel('Window Size (frames)')
    plt.ylabel('Fraction correct')
    plt.title('Testing with %s' % os.path.split(FLAGS.test_files[0])[1])
    plt.legend()

    plot_file = os.path.join(FLAGS.plot_dir, 'test_results-comparison.png')
    with tf.io.gfile.GFile(plot_file, 'wb') as fp:
      assert fp, 'Can not open %s for plotting AAD' % plot_file
      plt.savefig(fp)
    print('Saved find test result plot to', plot_file)
  return all_results

def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments: %s.' % argv)

  if FLAGS.tf_dir and not tf.io.gfile.exists(FLAGS.tf_dir):
    raise app.UsageError('Can not find tf_dir: %s' % FLAGS.tf_dir)
  if not tf.io.gfile.exists(FLAGS.model_dir):
    raise app.UsageError('Can not find model_dir: %s' % FLAGS.model_dir)

  print('FLAGS.reduction is', FLAGS.reduction)
  if FLAGS.comparison_test:
    run_comparison_test(FLAGS.model_dir, FLAGS.tf_dir,
                        FLAGS.train_files, FLAGS.test_files,
                        FLAGS.audio_label, FLAGS.audio_label + '2',
                        FLAGS.plot_dir,
                        reduction_list=['first', 'lda'])
  else:
    run_reduction_test(FLAGS.model_dir, FLAGS.tf_dir, FLAGS.train_files,
                       FLAGS.test_files, FLAGS.reduction, FLAGS.decoder,
                       FLAGS.audio_label, FLAGS.audio_label + '2',
                       FLAGS.plot_dir)

if __name__ == '__main__':
  app.run(main)


text:
# Classifieds Bumps
Bump trades on backpack.tf. You can decide the time interval before two bumps simply changing this:

```
/* SETTINGS */
  var maxRefreshTime = 60 * 60000,
      minRefreshTime = 30 * 60000;
/* END OF SETTINGS */
```
Time need to be in milliseconds. 60000 milliseconds = 1 minute so I suggest to multiply your time in minutes for 60000 like the code above.

## Instruction
1. Ensure that you have a Script Manager installed.
2. [Install](backpacktfClassifiedsBumps.user.js?raw=true) from github.com.
3. Confirm that you want to install the script.
4. The script should now be installed and ready to use.

## Supported Browsers

| ![Chrome ](https://www.w3schools.com/images/compatible_chrome.gif) | ![ Internet Explorer](https://www.w3schools.com/images/compatible_edge.gif) | ![FireFox](https://www.w3schools.com/images/compatible_firefox.gif) | ![Safari](https://www.w3schools.com/images/compatible_safari.gif) | ![Opera](https://www.w3schools.com/images/compatible_opera.gif) |
|     :---:      |     :---:      |     :---:      |     :---:      |     :---:      |
| 4+ | 9+ | 2+ | 3.1+ | 9+ |

Browser compatibility information is provided by [jscc.info](http://jscc.info/)
## Changelog
* **Release 1.1   - October   08, 2017**  - Minor improvements.
* **Release 1.0.1 - September 12, 2017**  - Added auto-updater.
* **Release 1.0   - September 12, 2017**  - "Hello World!"


text:
var stream = require('stream');

function getReadableStream(data, options) {
  var readableStream = new stream.Readable(options);

  readableStream._read = (function () { // eslint-disable-line no-underscore-dangle
    var d = data.slice();

    return function () {
      if (d.length > 0) {
        this.push(d.shift());
      } else {
        this.push(null);
      }
    };
  }());

  return readableStream;
}

module.exports = getReadableStream;


text:
package org.fusesource.fabric.commands;

import org.apache.felix.gogo.commands.Argument;
import org.apache.felix.gogo.commands.Command;
import org.apache.felix.gogo.commands.Option;
import org.fusesource.fabric.api.Version;
import org.fusesource.fabric.boot.commands.support.FabricCommand;

@Command(name = "version-create", scope = "fabric", description = "Create a new version, copying all of the profiles from the current latest version into the new version")
public class VersionCreate extends FabricCommand {

    @Option(name = "--parent", description = "The parent version. By default, use the latest version as the parent.")
    private String parentVersion;
    @Option(name = "--default", description = "Set the created version to be the new default version.")
    private Boolean defaultVersion;
    @Argument(index = 0, description = "The new version to create. If not specified, defaults to the next minor version.",  required = false)
    private String name;

    @Override
    protected Object doExecute() throws Exception {
        checkFabricAvailable();

        Version latestVersion = null;

        Version[] versions = fabricService.getVersions();
        int vlength = versions.length;
        if (vlength > 0) {
            latestVersion = versions[vlength - 1];
        }
        if (name == null) {
            if (latestVersion == null) {
                throw new IllegalArgumentException("Cannot default the new version name as there are no versions available");
            }
            name = latestVersion.getSequence().next().getName();
        }

        Version parent;
        if (parentVersion == null) {
            parent = latestVersion;
            // TODO we maybe want to choose the version which is less than the 'name' if it was specified
            // e.g. if you create a version 1.1 then it should use 1.0 if there is already a 2.0
        } else {
            parent = fabricService.getVersion(parentVersion);
            if (parent == null) {
                throw new IllegalArgumentException("Cannot find parent version: " + parentVersion);
            }
        }

        Version created;
        if (parent != null) {
            created = fabricService.createVersion(parent, name);
            System.out.println("Created version: " + name + " as copy of: " + parent.getId());
        } else {
            created = fabricService.createVersion(name);
            System.out.println("Created version: " + name);
        }

        if (defaultVersion != null && defaultVersion == true) {
            fabricService.setDefaultVersion(created);
        }

        return null;
    }
}


text:
myVariable = "okay";

  //////// MODEL

 function Cookie(name, bakeTime) {
    this.bakeTime = bakeTime;
    this.name = name;
    this.state = "raw";
    this.timeBaked = 0;
  }

  Cookie.prototype.bakeOneMin = function(){
    this.timeBaked++;
  }

  Cookie.prototype.checkStatus = function(){
    if(this.timeBaked === 0){
      this.state = "raw";
    };
    if(0 < this.timeBaked < this.bakeTime){
      this.state = "still gooey"
    };
    if(this.timeBaked === this.bakeTime){
      this.state = "just right"
    };
    if(this.timeBaked > this.bakeTime){
      this.state = "crispy"
    };
  }

  function Oven() {
    this.slots = 3;
    this.contents = []
  }

  Oven.prototype.isFull = function(){
    if(this.contents.length === this.slots){
      // don't put it in oven
    } else {
      // put it in oven
    }
  }

  Oven.prototype.bakeOneMin = function(){}

  // class Cookie
    // def initialize(name, bake_time)
    //   @name = name
    //   @bake_time = bake_time
    //   @state = "raw"
    //   @time_baked = 0
    // end
    // def bake_one_min
    //   @time_baked += 1
    // end
  // end

  // oven prototype
  // (holds cookies?)
  // ability to increment accumulated bake time in cookies

  /////////// VIEW

  // Replace DOM elements

  /////////// Controller

  // takes input (name, time, button press)


text:
namespace Kentico.Kontent.ImageTransformation
{
    /// <summary>
    /// Specifies the possible modes for fit transformation.
    /// </summary>
    public enum ImageFitMode
    {
        /// <summary>
        /// Resizes the image to fit within the width and height boundaries without cropping or distorting the image.
        /// </summary>
        Clip,

        /// <summary>
        /// Scales the image to fit the constraining dimensions exactly.
        /// </summary>
        Scale,

        /// <summary>
        /// Resizes the image to fill the width and height dimensions and crops any excess image data.
        /// </summary>
        Crop,
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.game.dhanraj.myownalexa">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service
            android:name=".DownChannel"
            android:enabled="true" />
        <service
            android:name=".Alarm.AlarmService"
            android:enabled="true" />

        <receiver
            android:name=".BootReceiver"
            android:enabled="true"
            android:exported="false">
            <intent-filter>

                <!--
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.BATTERY_LOW"/>
                -->
                <action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH" />
                <!--
                <action android:name="android.intent.action.REBOOT"/>
                <action android:name="android.intent.action.SCREEN_ON"/>
                <action android:name="android.intent.action.SCREEN_OFF"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
                -->
            </intent-filter>
        </receiver>
        <receiver android:name=".Alarm.AlarmReceiver" />

        <activity android:name=".MainActivity"
            android:screenOrientation="portrait"/>
        <activity android:name=".LoginWebViewActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <!-- host should be our application package // -->
                <data
                    android:host="www.amazon.com"
                    android:pathPattern="/ap/oa"
                    android:scheme="https" />
            </intent-filter>
        </activity>
        <activity android:name=".Alarm.MyAlarm" />
        <activity
            android:name="com.amazon.identity.auth.device.workflow.WorkflowActivity"
            android:allowTaskReparenting="true"
            android:launchMode="singleTask"
            android:theme="@android:style/Theme.NoDisplay">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <!-- host should be our application package // -->
                <data
                    android:host="com.game.dhanraj.myownalexa"
                    android:scheme="amzn" />
            </intent-filter>
        </activity>
        <activity android:name=".SendingAudio"
            android:screenOrientation="portrait"/>
        <activity android:name=".SplashActivitiy"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AboutActivity"
            android:screenOrientation="portrait"/>
        <activity android:name=".SettingsActivity"
            android:screenOrientation="portrait"/>
        <activity android:name=".RingtonesActivity"
            android:screenOrientation="portrait"></activity>
    </application>

</manifest>