<commit_before>from datetime import timedelta
from django import template
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.utils.importlib import import_module
from django.utils.safestring import mark_safe

from reminders.models import Dismissal

register = template.Library()
REMINDERS_IGNORE_URLS = getattr(settings, 'REMINDERS_IGNORE_URLS', [])

def load_callable(path_to_callable):
    try:
        mod_name, func_name = path_to_callable.rsplit(".", 1)
    except ValueError:
        raise Exception("Improperly configured.")
    try:
        mod = import_module(mod_name)
    except ImportError:
        raise Exception("Could not import %s" % mod_name)
    try:
        func = getattr(mod, func_name)
    except AttributeError:
        raise Exception("The module '%s' does not contain '%s'." % (mod_name, func_name))
    return func

def is_dismissed(label, dismissal_type, request):
    if dismissal_type == "session":
        dismissed = label in request.session
    elif dismissal_type == "permanent":
        dismissed = Dismissal.objects.filter(user=request.user, label=label).exists()
    else:
        dismissed = False
    return dismissed

def recently_dismissed_something(user):
    time_ago = timezone.now() - timedelta(days=1)
    return Dismissal.objects.filter(user=user, dismissed_at__gt=time_ago).exists()

class RemindersNode(template.Node):

    @classmethod
    def handle_token(cls, parser, token):
        bits = token.split_contents()
        if len(bits) != 3:
            raise template.TemplateSyntaxError
        return cls(as_var=bits[2])

    def __init__(self, as_var):
        self.as_var = as_var

    def render(self, context):
        request = context["request"]
        show_reminder = None

        ignore_url = False
        for s in REMINDERS_IGNORE_URLS:
            if s in request.path:
                ignore_url = True

        # Don't bother if they aren't logged in or if they just recently dismissed another message
        if not ignore_url and request.user.is_authenticated() and not recently_dismissed_something(request.user):

            # Order by specified priority
            for label in sorted(settings.REMINDERS, key=lambda x: settings.REMINDERS[x]['priority']):
                reminder = settings.REMINDERS[label]

                if not is_dismissed(label, reminder.get("dismissable"), request):
                    message = reminder["message"]
                    if not callable(message):
                        message = load_callable(message)

                    url = None
                    if reminder.get("dismissable") != "no":
                        url = reverse("reminders_dismiss", kwargs={"label": label})

                    result = message(request.user)
                    if result and isinstance(result, basestring):
                        show_reminder = {"message": mark_safe(result),
                                         "dismiss_url": url}
                        # just one reminder at a time, spaced according to time_ago in recently_dismissed_something
                        break

        context[self.as_var] = show_reminder
        return ""

@register.tag
def reminders(parser, token):
    """
    Usage::
        {% reminders as var %}

    Returns a list of reminders
    """
    return RemindersNode.handle_token(parser, token)
<commit_msg>pass host into reminders<commit_after>from datetime import timedelta
from django import template
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.utils.importlib import import_module
from django.utils.safestring import mark_safe

from reminders.models import Dismissal

register = template.Library()
REMINDERS_IGNORE_URLS = getattr(settings, 'REMINDERS_IGNORE_URLS', [])

def load_callable(path_to_callable):
    try:
        mod_name, func_name = path_to_callable.rsplit(".", 1)
    except ValueError:
        raise Exception("Improperly configured.")
    try:
        mod = import_module(mod_name)
    except ImportError:
        raise Exception("Could not import %s" % mod_name)
    try:
        func = getattr(mod, func_name)
    except AttributeError:
        raise Exception("The module '%s' does not contain '%s'." % (mod_name, func_name))
    return func

def is_dismissed(label, dismissal_type, request):
    if dismissal_type == "session":
        dismissed = label in request.session
    elif dismissal_type == "permanent":
        dismissed = Dismissal.objects.filter(user=request.user, label=label).exists()
    else:
        dismissed = False
    return dismissed

def recently_dismissed_something(user):
    time_ago = timezone.now() - timedelta(days=1)
    return Dismissal.objects.filter(user=user, dismissed_at__gt=time_ago).exists()

class RemindersNode(template.Node):

    @classmethod
    def handle_token(cls, parser, token):
        bits = token.split_contents()
        if len(bits) != 3:
            raise template.TemplateSyntaxError
        return cls(as_var=bits[2])

    def __init__(self, as_var):
        self.as_var = as_var

    def render(self, context):
        request = context["request"]
        show_reminder = None

        ignore_url = False
        for s in REMINDERS_IGNORE_URLS:
            if s in request.path:
                ignore_url = True

        # Don't bother if they aren't logged in or if they just recently dismissed another message
        if not ignore_url and request.user.is_authenticated() and not recently_dismissed_something(request.user):

            # Order by specified priority
            for label in sorted(settings.REMINDERS, key=lambda x: settings.REMINDERS[x]['priority']):
                reminder = settings.REMINDERS[label]

                if not is_dismissed(label, reminder.get("dismissable"), request):
                    message = reminder["message"]
                    if not callable(message):
                        message = load_callable(message)

                    url = None
                    if reminder.get("dismissable") != "no":
                        url = reverse("reminders_dismiss", kwargs={"label": label})

                    try:
                        hostname = request.get_host()
                    except Exception as e:
                        hostname = str(e)

                    result = message(request.user, host=hostname)
                    if result and isinstance(result, basestring):
                        show_reminder = {"message": mark_safe(result),
                                         "dismiss_url": url}
                        # just one reminder at a time, spaced according to time_ago in recently_dismissed_something
                        break

        context[self.as_var] = show_reminder
        return ""

@register.tag
def reminders(parser, token):
    """
    Usage::
        {% reminders as var %}

    Returns a list of reminders
    """
    return RemindersNode.handle_token(parser, token)
<|endoftext|>


<commit_before>import numpy as np
import networkx as nx
import seaborn as sns
from matplotlib import pyplot as pp

from ..utils import msme_colors
from ..palettes import msme_rgb

__all__ = ['plot_pop_resids', 'plot_msm_network',
           'plot_timescales', 'plot_implied_timescales']

@msme_colors
def plot_pop_resids(msm, **kwargs):
    """
    Plot residuals between MSM populations and raw counts.

    Parameters
    ----------
    msm : msmbuilder.msm
        MSMBuilder MarkovStateModel
    **kwargs : dict, optional
        Extra arguments to pass to seaborn.jointplot

    Returns
    -------
    ax : matplotlib axis
        matplotlib figure axis

    """
    if hasattr(msm, 'all_populations_'):
        msm_pop = msm.populations_.mean(0)
    elif hasattr(msm, 'populations_'):
        msm_pop = msm.populations_

    raw_pop = msm.countsmat_.sum(1) / msm.countsmat_.sum()
    ax = sns.jointplot(np.log10(raw_pop), np.log10(msm_pop), kind='resid',
                       **kwargs)
    ax.ax_joint.set_xlabel('Raw Populations', size=20)
    ax.ax_joint.set_ylabel('Residuals', size=20)

    return ax

@msme_colors
def plot_msm_network(msm, pos=None, node_size=None, node_color='pomegranate',
                     edge_color='carbon', alpha=.7, ax=None, with_labels=True,
                     **kwargs):
    """
    Plot MSM network diagram.

    Parameters
    ----------
    msm : msmbuilder.msm
        MSMBuilder MarkovStateModel
    pos : dict, optional
        Node positions in dict format (e.g. {node_id : [x, y]})
    node_color : str or [r, g, b], optional
        networkx node colors
    node_size : int or list, optional
        networkx node size
    node_color : str, optional
        networkx edge color
    ax : matplotlib axis, optional (default: None)
        Axis to plot on, otherwise uses current axis.
    with_labels : boolean, optional
        Whether or not to include node labels (default: True)
    alpha : float, optional  (default: 0.7)
        Opacity of nodes
    **kwargs : dict, optional
        Extra arguments to pass to networkx.draw_networkx

    Returns
    -------
    ax : matplotlib axis
        matplotlib figure axis

    """
    if hasattr(msm, 'all_populations_'):
        tmat = msm.all_transmats_.mean(0)
        pop = msm.all_populations_.mean(0)
    elif hasattr(msm, 'populations_'):
        tmat = msm.transmat_
        pop = msm.populations_

    graph = nx.Graph(tmat)

    if not ax:
        ax = pp.gca()

    if pos is None:
        pos = nx.spring_layout(graph)

    if node_size is None:
        node_size = 5000. * pop

    if isinstance(node_size, (list, np.ndarray)):
        node_size = [node_size[i] for i in graph.nodes()]

    if isinstance(node_color, (list, np.ndarray)):
        node_color = [node_color[i] for i in graph.nodes()]

    nx.draw_networkx(graph, pos=pos, node_color=node_color,
                     node_size=node_size, edge_color=edge_color, ax=ax,
                     with_labels=with_labels, alpha=alpha, **kwargs)

    return ax

@msme_colors
def plot_timescales(msm, n_timescales=None, error=None, sigma=2,
                    color_palette=None, xlabel=None, ylabel=None, ax=None):
    """
    Plot MSM timescales spectral diagram.

    Parameters
    ----------
    msm : msmbuilder.msm
        MSMBuilder MarkovStateModel
    n_timescales : int, optional
        Number of timescales to plot
    error : array-like (float), optional
        associated errors for each timescales
    sigma : float, optional
        significance level for default error bars
    color_palette: list or dict, optional
        Color palette to apply
    xlabel : str, optional
        x-axis label
    ylabel : str, optional
        y-axis label
    ax : matplotlib axis, optional (default: None)
        Axis to plot on, otherwise uses current axis.

    Returns
    -------
    ax : matplotlib axis
        matplotlib figure axis

    """
    if hasattr(msm, 'all_timescales_'):
        timescales = msm.all_timescales_.mean(0)
        if not error:
            error = (msm.all_timescales_.std(0) /
                     msm.all_timescales_.shape[0] ** 0.5)
    elif hasattr(msm, 'timescales_'):
        timescales = msm.timescales_
        if not error:
            error = np.nan_to_num(msm.uncertainty_timescales())

    if n_timescales:
        timescales = timescales[:n_timescales]
        error = error[:n_timescales]
    else:
        n_timescales = timescales.shape[0]

    ymin = 10 ** np.floor(np.log10(np.nanmin(timescales)))
    ymax = 10 ** np.ceil(np.log10(np.nanmax(timescales)))

    if not ax:
        _, ax = pp.subplots(1, 1, figsize=(2, 8))
    if not color_palette:
        color_palette = list(msme_rgb.values())

    for i, item in enumerate(zip(timescales, error)):
        t, s = item
        color = color_palette[i % len(color_palette)]
        ax.errorbar([0, 1], [t, t], c=color)
        if s:
            for j in range(1, sigma + 1):
                ax.fill_between([0, 1], y1=[t - j * s, t - j * s],
                                y2=[t + j * s, t + j * s],
                                color=color, alpha=0.2 / j)

    ax.xaxis.set_ticks([])
    if xlabel:
        ax.xaxis.set_label_text(xlabel, size=18)
        ax.xaxis.labelpad = 18
    if ylabel:
        ax.yaxis.set_label_text(ylabel, size=18)
    ax.set_yscale('log')
    ax.set_ylim([ymin, ymax])

    autoAxis = ax.axis()
    rec = pp.Rectangle((autoAxis[0], 100),
                       (autoAxis[1] - autoAxis[0]),
                       ymax, fill=False, lw=2)
    rec = ax.add_patch(rec)

    for tick in ax.yaxis.get_major_ticks():
        tick.label.set_fontsize(16)

    return ax

@msme_colors
def plot_implied_timescales(msm_list, n_timescales=None, show_error=True,
                            color_palette=None, xlabel=None, ylabel=None, ax=None):
    """
    Plot implied timescales as a function of the MSM lag time

    Parameters
    ----------
    msm_list: list of msmbuilder.msm.MarkovStateModel objects
        A list of msm objects, calculated at different lag times
    n_timescales: int, optional
        Number of timescales to display. If None, all will be displayed.
    show_error: bool, optional
        Wether to display the uncertainty estimation of the timescales as a
        shadowed region
    color_palette: list or dict, optional
        Color palette to apply
    xlabel : str, optional
        x-axis label
    ylabel : str, optional
        y-axis label
    ax : matplotlib axis, optional (default: None)
        Axis to plot on, otherwise uses current axis.

    Returns
    -------
    ax : matplotlib axis
        matplotlib figure axis

    """
    if n_timescales is None:
        n_timescales = len(msm_list[0].timescales_)
    elif n_timescales > len(msm_list[0].timescales_):
        n_timescales = len(msm_list[0].timescales_)

    lag_times = [msm.lag_time for msm in msm_list]

    long_ts = [msm.timescales_[0] for msm in msm_list]
    short_ts = [msm.timescales_[-1] for msm in msm_list]
    ymin = 10 ** np.floor(np.log10(np.nanmin(short_ts)))
    ymax = 10 ** np.ceil(np.log10(np.nanmax(long_ts)))

    if not ax:
        _, ax = pp.subplots(1, 1)
    if not color_palette:
        color_palette = list(msme_rgb.values())

    for ts in range(n_timescales):
        timescales = [msm.timescales_[ts] for msm in msm_list]
        color = color_palette[ts % len(color_palette)]
        pp.scatter(x=lag_times, y=timescales, color=color)
        if show_error:
            errors = [msm.uncertainty_timescales()[ts] for msm in msm_list]
            pp.fill_between(x=lag_times,
                            y1=[ts - err for ts, err in zip(timescales, errors)],
                            y2=[ts + err for ts, err in zip(timescales, errors)],
                            color=color)
    ax.set_yscale('log')
    if xlabel:
        ax.xaxis.set_label_text(xlabel)
    if ylabel:
        ax.yaxis.set_label_text(ylabel)
    ax.set_yscale('log')
    ax.set_ylim([ymin, ymax])
    return ax
<commit_msg>don't set log y twice<commit_after>import numpy as np
import networkx as nx
import seaborn as sns
from matplotlib import pyplot as pp

from ..utils import msme_colors
from ..palettes import msme_rgb

__all__ = ['plot_pop_resids', 'plot_msm_network',
           'plot_timescales', 'plot_implied_timescales']

@msme_colors
def plot_pop_resids(msm, **kwargs):
    """
    Plot residuals between MSM populations and raw counts.

    Parameters
    ----------
    msm : msmbuilder.msm
        MSMBuilder MarkovStateModel
    **kwargs : dict, optional
        Extra arguments to pass to seaborn.jointplot

    Returns
    -------
    ax : matplotlib axis
        matplotlib figure axis

    """
    if hasattr(msm, 'all_populations_'):
        msm_pop = msm.populations_.mean(0)
    elif hasattr(msm, 'populations_'):
        msm_pop = msm.populations_

    raw_pop = msm.countsmat_.sum(1) / msm.countsmat_.sum()
    ax = sns.jointplot(np.log10(raw_pop), np.log10(msm_pop), kind='resid',
                       **kwargs)
    ax.ax_joint.set_xlabel('Raw Populations', size=20)
    ax.ax_joint.set_ylabel('Residuals', size=20)

    return ax

@msme_colors
def plot_msm_network(msm, pos=None, node_size=None, node_color='pomegranate',
                     edge_color='carbon', alpha=.7, ax=None, with_labels=True,
                     **kwargs):
    """
    Plot MSM network diagram.

    Parameters
    ----------
    msm : msmbuilder.msm
        MSMBuilder MarkovStateModel
    pos : dict, optional
        Node positions in dict format (e.g. {node_id : [x, y]})
    node_color : str or [r, g, b], optional
        networkx node colors
    node_size : int or list, optional
        networkx node size
    node_color : str, optional
        networkx edge color
    ax : matplotlib axis, optional (default: None)
        Axis to plot on, otherwise uses current axis.
    with_labels : boolean, optional
        Whether or not to include node labels (default: True)
    alpha : float, optional  (default: 0.7)
        Opacity of nodes
    **kwargs : dict, optional
        Extra arguments to pass to networkx.draw_networkx

    Returns
    -------
    ax : matplotlib axis
        matplotlib figure axis

    """
    if hasattr(msm, 'all_populations_'):
        tmat = msm.all_transmats_.mean(0)
        pop = msm.all_populations_.mean(0)
    elif hasattr(msm, 'populations_'):
        tmat = msm.transmat_
        pop = msm.populations_

    graph = nx.Graph(tmat)

    if not ax:
        ax = pp.gca()

    if pos is None:
        pos = nx.spring_layout(graph)

    if node_size is None:
        node_size = 5000. * pop

    if isinstance(node_size, (list, np.ndarray)):
        node_size = [node_size[i] for i in graph.nodes()]

    if isinstance(node_color, (list, np.ndarray)):
        node_color = [node_color[i] for i in graph.nodes()]

    nx.draw_networkx(graph, pos=pos, node_color=node_color,
                     node_size=node_size, edge_color=edge_color, ax=ax,
                     with_labels=with_labels, alpha=alpha, **kwargs)

    return ax

@msme_colors
def plot_timescales(msm, n_timescales=None, error=None, sigma=2,
                    color_palette=None, xlabel=None, ylabel=None, ax=None):
    """
    Plot MSM timescales spectral diagram.

    Parameters
    ----------
    msm : msmbuilder.msm
        MSMBuilder MarkovStateModel
    n_timescales : int, optional
        Number of timescales to plot
    error : array-like (float), optional
        associated errors for each timescales
    sigma : float, optional
        significance level for default error bars
    color_palette: list or dict, optional
        Color palette to apply
    xlabel : str, optional
        x-axis label
    ylabel : str, optional
        y-axis label
    ax : matplotlib axis, optional (default: None)
        Axis to plot on, otherwise uses current axis.

    Returns
    -------
    ax : matplotlib axis
        matplotlib figure axis

    """
    if hasattr(msm, 'all_timescales_'):
        timescales = msm.all_timescales_.mean(0)
        if not error:
            error = (msm.all_timescales_.std(0) /
                     msm.all_timescales_.shape[0] ** 0.5)
    elif hasattr(msm, 'timescales_'):
        timescales = msm.timescales_
        if not error:
            error = np.nan_to_num(msm.uncertainty_timescales())

    if n_timescales:
        timescales = timescales[:n_timescales]
        error = error[:n_timescales]
    else:
        n_timescales = timescales.shape[0]

    ymin = 10 ** np.floor(np.log10(np.nanmin(timescales)))
    ymax = 10 ** np.ceil(np.log10(np.nanmax(timescales)))

    if not ax:
        _, ax = pp.subplots(1, 1, figsize=(2, 8))
    if not color_palette:
        color_palette = list(msme_rgb.values())

    for i, item in enumerate(zip(timescales, error)):
        t, s = item
        color = color_palette[i % len(color_palette)]
        ax.errorbar([0, 1], [t, t], c=color)
        if s:
            for j in range(1, sigma + 1):
                ax.fill_between([0, 1], y1=[t - j * s, t - j * s],
                                y2=[t + j * s, t + j * s],
                                color=color, alpha=0.2 / j)

    ax.xaxis.set_ticks([])
    if xlabel:
        ax.xaxis.set_label_text(xlabel, size=18)
        ax.xaxis.labelpad = 18
    if ylabel:
        ax.yaxis.set_label_text(ylabel, size=18)
    ax.set_yscale('log')
    ax.set_ylim([ymin, ymax])

    autoAxis = ax.axis()
    rec = pp.Rectangle((autoAxis[0], 100),
                       (autoAxis[1] - autoAxis[0]),
                       ymax, fill=False, lw=2)
    rec = ax.add_patch(rec)

    for tick in ax.yaxis.get_major_ticks():
        tick.label.set_fontsize(16)

    return ax

@msme_colors
def plot_implied_timescales(msm_list, n_timescales=None, show_error=True,
                            color_palette=None, xlabel=None, ylabel=None, ax=None):
    """
    Plot implied timescales as a function of the MSM lag time

    Parameters
    ----------
    msm_list: list of msmbuilder.msm.MarkovStateModel objects
        A list of msm objects, calculated at different lag times
    n_timescales: int, optional
        Number of timescales to display. If None, all will be displayed.
    show_error: bool, optional
        Wether to display the uncertainty estimation of the timescales as a
        shadowed region
    color_palette: list or dict, optional
        Color palette to apply
    xlabel : str, optional
        x-axis label
    ylabel : str, optional
        y-axis label
    ax : matplotlib axis, optional (default: None)
        Axis to plot on, otherwise uses current axis.

    Returns
    -------
    ax : matplotlib axis
        matplotlib figure axis

    """
    if n_timescales is None:
        n_timescales = len(msm_list[0].timescales_)
    elif n_timescales > len(msm_list[0].timescales_):
        n_timescales = len(msm_list[0].timescales_)

    lag_times = [msm.lag_time for msm in msm_list]

    long_ts = [msm.timescales_[0] for msm in msm_list]
    short_ts = [msm.timescales_[-1] for msm in msm_list]
    ymin = 10 ** np.floor(np.log10(np.nanmin(short_ts)))
    ymax = 10 ** np.ceil(np.log10(np.nanmax(long_ts)))

    if not ax:
        _, ax = pp.subplots(1, 1)
    if not color_palette:
        color_palette = list(msme_rgb.values())

    for ts in range(n_timescales):
        timescales = [msm.timescales_[ts] for msm in msm_list]
        color = color_palette[ts % len(color_palette)]
        pp.scatter(x=lag_times, y=timescales, color=color)
        if show_error:
            errors = [msm.uncertainty_timescales()[ts] for msm in msm_list]
            pp.fill_between(x=lag_times,
                            y1=[ts - err for ts, err in zip(timescales, errors)],
                            y2=[ts + err for ts, err in zip(timescales, errors)],
                            color=color)

    if xlabel:
        ax.xaxis.set_label_text(xlabel)
    if ylabel:
        ax.yaxis.set_label_text(ylabel)
    ax.set_yscale('log')
    ax.set_ylim([ymin, ymax])
    return ax
<|endoftext|>


<commit_before>import tensorflow as tf

def project_biases(tf_features, n_features):
    """
    Projects the biases from the feature space to calculate bias per actor
    :param tf_features:
    :param n_features:
    :return:
    """
    tf_feature_biases = tf.Variable(tf.zeros([n_features, 1]))

    # The reduce sum is to perform a rank reduction
    tf_projected_biases = tf.reduce_sum(
        tf.sparse_tensor_dense_matmul(tf_features, tf_feature_biases),
        axis=1
    )

    return tf_feature_biases, tf_projected_biases

def split_sparse_tensor_indices(tf_sparse_tensor, n_dimensions):
    """
    Separates the each dimension of a sparse tensor's indices into independent tensors
    :param tf_sparse_tensor:
    :param n_dimensions:
    :return:
    """
    tf_transposed_indices = tf.transpose(tf_sparse_tensor.indices)
    return (tf_transposed_indices[i] for i in range(n_dimensions))

def bias_prediction_dense(tf_prediction, tf_projected_user_biases, tf_projected_item_biases):
    """
    Broadcasts user and item biases across their respective axes of the dense predictions
    :param tf_prediction:
    :param tf_projected_user_biases:
    :param tf_projected_item_biases:
    :return:
    """
    return tf_prediction + tf.expand_dims(tf_projected_user_biases, 1) + tf.expand_dims(tf_projected_item_biases, 0)

def bias_prediction_serial(tf_prediction_serial, tf_projected_user_biases, tf_projected_item_biases, tf_x_user,
                           tf_x_item):
    """
    Calculates the bias per user/item pair and adds it to the serial predictions
    :param tf_prediction_serial:
    :param tf_projected_user_biases:
    :param tf_projected_item_biases:
    :param tf_x_user:
    :param tf_x_item:
    :return:
    """
    gathered_user_biases = tf.gather(tf_projected_user_biases, tf_x_user)
    gathered_item_biases = tf.gather(tf_projected_item_biases, tf_x_item)
    return tf_prediction_serial + gathered_user_biases + gathered_item_biases

def densify_sampled_item_predictions(tf_sample_predictions_serial, tf_n_sampled_items, tf_n_users):
    """
    Turns the serial predictions of the sample items in to a dense matrix of shape [ n_users, n_sampled_items ]
    :param tf_sample_predictions_serial:
    :param tf_n_sampled_items:
    :param tf_n_users:
    :return:
    """
    densified_shape = tf.cast(tf.stack([tf_n_users, tf_n_sampled_items]), tf.int32)
    densified_predictions = tf.reshape(tf_sample_predictions_serial, shape=densified_shape)
    return densified_predictions

def rank_predictions(tf_prediction):
    """
    Double-sortation serves as a ranking process
    The +1 is so the top-ranked has a non-zero rank
    :param tf_prediction:
    :return:
    """
    tf_prediction_item_size = tf.shape(tf_prediction)[1]
    tf_indices_of_ranks = tf.nn.top_k(tf_prediction, k=tf_prediction_item_size)[1]
    return tf.nn.top_k(-tf_indices_of_ranks, k=tf_prediction_item_size)[1] + 1

def collapse_mixture_of_tastes(tastes_predictions, tastes_attentions):
    """
    Collapses a list of prediction nodes in to a single prediction node.
    :param tastes_predictions:
    :param tastes_attentions:
    :return:
    """
    stacked_predictions = tf.stack(tastes_predictions)

    # If there is attention, the attentions are used to weight each prediction
    if tastes_attentions is not None:

        # Stack the attentions and perform softmax across the tastes
        stacked_attentions = tf.stack(tastes_attentions)
        softmax_attentions = tf.nn.softmax(stacked_attentions, axis=0)

        # The softmax'd attentions serve as weights for the taste predictiones
        weighted_predictions = tf.multiply(stacked_predictions, softmax_attentions)
        result_prediction = tf.reduce_sum(weighted_predictions, axis=0)

    # If there is no attention, the max prediction is returned
    else:
        result_prediction = tf.reduce_max(stacked_predictions, axis=0)

    return result_prediction

def relative_cosine(tf_tensor_1, tf_tensor_2):
    """
    Returns the cosine of every row in tensor_1 against every row in tensor_2.
    :param tf_tensor_1:
    :param tf_tensor_2:
    :return:
    """
    normalized_t1 = tf.nn.l2_normalize(tf_tensor_1, 1)
    normalized_t2 = tf.nn.l2_normalize(tf_tensor_2, 1)
    return tf.matmul(normalized_t1, normalized_t2, transpose_b=True)

def predict_similar_items(prediction_graph_factory, tf_item_representation, tf_similar_items_ids):
    """
    Calculates the similarity between the given item ids and all other items using the prediction graph.
    :param prediction_graph_factory:
    :param tf_item_representation:
    :param tf_similar_items_ids:
    :return:
    """
    gathered_items = tf.gather(tf_item_representation, tf_similar_items_ids)
    sims = prediction_graph_factory.connect_dense_prediction_graph(
        tf_user_representation=gathered_items,
        tf_item_representation=tf_item_representation
    )
    return sims
<commit_msg>Fix simple typo: predictiones -> predictions<commit_after>import tensorflow as tf

def project_biases(tf_features, n_features):
    """
    Projects the biases from the feature space to calculate bias per actor
    :param tf_features:
    :param n_features:
    :return:
    """
    tf_feature_biases = tf.Variable(tf.zeros([n_features, 1]))

    # The reduce sum is to perform a rank reduction
    tf_projected_biases = tf.reduce_sum(
        tf.sparse_tensor_dense_matmul(tf_features, tf_feature_biases),
        axis=1
    )

    return tf_feature_biases, tf_projected_biases

def split_sparse_tensor_indices(tf_sparse_tensor, n_dimensions):
    """
    Separates the each dimension of a sparse tensor's indices into independent tensors
    :param tf_sparse_tensor:
    :param n_dimensions:
    :return:
    """
    tf_transposed_indices = tf.transpose(tf_sparse_tensor.indices)
    return (tf_transposed_indices[i] for i in range(n_dimensions))

def bias_prediction_dense(tf_prediction, tf_projected_user_biases, tf_projected_item_biases):
    """
    Broadcasts user and item biases across their respective axes of the dense predictions
    :param tf_prediction:
    :param tf_projected_user_biases:
    :param tf_projected_item_biases:
    :return:
    """
    return tf_prediction + tf.expand_dims(tf_projected_user_biases, 1) + tf.expand_dims(tf_projected_item_biases, 0)

def bias_prediction_serial(tf_prediction_serial, tf_projected_user_biases, tf_projected_item_biases, tf_x_user,
                           tf_x_item):
    """
    Calculates the bias per user/item pair and adds it to the serial predictions
    :param tf_prediction_serial:
    :param tf_projected_user_biases:
    :param tf_projected_item_biases:
    :param tf_x_user:
    :param tf_x_item:
    :return:
    """
    gathered_user_biases = tf.gather(tf_projected_user_biases, tf_x_user)
    gathered_item_biases = tf.gather(tf_projected_item_biases, tf_x_item)
    return tf_prediction_serial + gathered_user_biases + gathered_item_biases

def densify_sampled_item_predictions(tf_sample_predictions_serial, tf_n_sampled_items, tf_n_users):
    """
    Turns the serial predictions of the sample items in to a dense matrix of shape [ n_users, n_sampled_items ]
    :param tf_sample_predictions_serial:
    :param tf_n_sampled_items:
    :param tf_n_users:
    :return:
    """
    densified_shape = tf.cast(tf.stack([tf_n_users, tf_n_sampled_items]), tf.int32)
    densified_predictions = tf.reshape(tf_sample_predictions_serial, shape=densified_shape)
    return densified_predictions

def rank_predictions(tf_prediction):
    """
    Double-sortation serves as a ranking process
    The +1 is so the top-ranked has a non-zero rank
    :param tf_prediction:
    :return:
    """
    tf_prediction_item_size = tf.shape(tf_prediction)[1]
    tf_indices_of_ranks = tf.nn.top_k(tf_prediction, k=tf_prediction_item_size)[1]
    return tf.nn.top_k(-tf_indices_of_ranks, k=tf_prediction_item_size)[1] + 1

def collapse_mixture_of_tastes(tastes_predictions, tastes_attentions):
    """
    Collapses a list of prediction nodes in to a single prediction node.
    :param tastes_predictions:
    :param tastes_attentions:
    :return:
    """
    stacked_predictions = tf.stack(tastes_predictions)

    # If there is attention, the attentions are used to weight each prediction
    if tastes_attentions is not None:

        # Stack the attentions and perform softmax across the tastes
        stacked_attentions = tf.stack(tastes_attentions)
        softmax_attentions = tf.nn.softmax(stacked_attentions, axis=0)

        # The softmax'd attentions serve as weights for the taste predictions
        weighted_predictions = tf.multiply(stacked_predictions, softmax_attentions)
        result_prediction = tf.reduce_sum(weighted_predictions, axis=0)

    # If there is no attention, the max prediction is returned
    else:
        result_prediction = tf.reduce_max(stacked_predictions, axis=0)

    return result_prediction

def relative_cosine(tf_tensor_1, tf_tensor_2):
    """
    Returns the cosine of every row in tensor_1 against every row in tensor_2.
    :param tf_tensor_1:
    :param tf_tensor_2:
    :return:
    """
    normalized_t1 = tf.nn.l2_normalize(tf_tensor_1, 1)
    normalized_t2 = tf.nn.l2_normalize(tf_tensor_2, 1)
    return tf.matmul(normalized_t1, normalized_t2, transpose_b=True)

def predict_similar_items(prediction_graph_factory, tf_item_representation, tf_similar_items_ids):
    """
    Calculates the similarity between the given item ids and all other items using the prediction graph.
    :param prediction_graph_factory:
    :param tf_item_representation:
    :param tf_similar_items_ids:
    :return:
    """
    gathered_items = tf.gather(tf_item_representation, tf_similar_items_ids)
    sims = prediction_graph_factory.connect_dense_prediction_graph(
        tf_user_representation=gathered_items,
        tf_item_representation=tf_item_representation
    )
    return sims
<|endoftext|>


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

# Copyright (c) 2014-2019, Matthew Brennan Jones <matthew.brennan.jones@gmail.com>
# Py-cpuinfo gets CPU info with pure Python 2 & 3
# It uses the MIT License
# It is hosted at: https://github.com/workhorsy/py-cpuinfo
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import os, sys
import glob
import platform
import multiprocessing
import subprocess

try:
	import _winreg as winreg
except ImportError as err:
	try:
		import winreg
	except ImportError as err:
		pass

IS_PY2 = sys.version_info[0] == 2

out_file_name = 'system_info.txt'
out_file = open(out_file_name, 'w')

def run_and_get_stdout(command, pipe_command=None):
	if not pipe_command:
		p1 = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
		output = p1.communicate()[0]
		if not IS_PY2:
			output = output.decode(encoding='UTF-8')
		return p1.returncode, output
	else:
		p1 = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
		p2 = subprocess.Popen(pipe_command, stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
		p1.stdout.close()
		output = p2.communicate()[0]
		if not IS_PY2:
			output = output.decode(encoding='UTF-8')
		return p2.returncode, output

def program_paths(program_name):
	paths = []
	exts = filter(None, os.environ.get('PATHEXT', '').split(os.pathsep))
	path = os.environ['PATH']
	for p in os.environ['PATH'].split(os.pathsep):
		p = os.path.join(p, program_name)
		if os.access(p, os.X_OK):
			paths.append(p)
		for e in exts:
			pext = p + e
			if os.access(pext, os.X_OK):
				paths.append(pext)
	return paths

def print_output(name, output):
	line = "=" * 79
	out_file.write('{0}:\n{1}\n{2}\n\n\n\n'.format(name, line, output))

print_output('sys.executable', sys.executable)

print_output('sys.version_info', sys.version_info)

if hasattr(sys, 'maxsize'):
	sizes = {
		2**31-1: '32 bit',
		2**63-1: '64 bit',
	}
	friendly_maxsize = sizes.get(sys.maxsize) or 'unknown'
	print_output('sys.maxsize', "{0} ({1})".format(sys.maxsize, friendly_maxsize))

print_output('multiprocessing.cpu_count', multiprocessing.cpu_count())

print_output('platform.uname', platform.uname())

print_output('platform.architecture', platform.architecture())

print_output('platform.system', platform.system())

print_output('platform.machine', platform.machine())

if program_paths('cpufreq-info'):
	returncode, output = run_and_get_stdout(['cpufreq-info'])
	print_output('cpufreq-info', output)

if program_paths('sestatus'):
	returncode, output = run_and_get_stdout(['sestatus', '-b'])
	print_output('sestatus', output)

if os.path.exists('/proc/cpuinfo'):
	returncode, output = run_and_get_stdout(['cat', '/proc/cpuinfo'])
	print_output('cat /proc/cpuinfo', output)

if program_paths('sysctl'):
	returncode, output = run_and_get_stdout(['sysctl', 'machdep.cpu', 'hw.cpufrequency'])
	print_output('sysctl machdep.cpu hw.cpufrequency', output)

if program_paths('sysctl'):
	returncode, output = run_and_get_stdout(['sysctl', 'hw.model', 'hw.machine'])
	print_output('sysctl hw.model hw.machine', output)

if program_paths('isainfo'):
	returncode, output = run_and_get_stdout(['isainfo', '-vb'])
	print_output('isainfo -vb', output)

if program_paths('kstat'):
	returncode, output = run_and_get_stdout(['kstat', '-m', 'cpu_info'])
	print_output('kstat -m cpu_info', output)

if program_paths('lscpu'):
	returncode, output = run_and_get_stdout(['lscpu'])
	print_output('lscpu', output)

if program_paths('dmesg'):
	returncode, output = run_and_get_stdout(['dmesg', '-a'])
	if returncode != 0:
		returncode, output = run_and_get_stdout(['dmesg'])
	if len(output) > 20480:
		output = output[0 : 20480]
	print_output('dmesg', output)

if os.path.exists('/var/run/dmesg.boot'):
	returncode, output = run_and_get_stdout(['cat', '/var/run/dmesg.boot'])
	if len(output) > 20480:
		output = output[0 : 20480]
	print_output('/var/run/dmesg.boot', output)

if program_paths('sysinfo'):
	returncode, output = run_and_get_stdout(['sysinfo', '-cpu'])
	print_output('sysinfo -cpu', output)

if program_paths('lsprop'):
	ibm_features = glob.glob('/proc/device-tree/cpus/*/ibm,pa-features')
	if ibm_features:
		returncode, output = run_and_get_stdout(['lsprop', ibm_features[0]])
		print_output('lsprop /proc/device-tree/cpus/*/ibm,pa-features', output)

if program_paths('wmic'):
	returncode, output = run_and_get_stdout(['wmic', 'os', 'get', 'Version'])
	if returncode == 0 and len(output) > 0:
		returncode, output = run_and_get_stdout(['wmic', 'cpu', 'get', 'Name,CurrentClockSpeed,L2CacheSize,L3CacheSize,Description,Caption,Manufacturer', '/format:list'])
		if returncode == 0 and len(output) > 0:
			print_output('wmic cpu get Name,CurrentClockSpeed,L2CacheSize,L3CacheSize,Description,Caption,Manufacturer /format:list', output)

if 'winreg' in sys.modules or '_winreg' in sys.modules:
	key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"Hardware\Description\System\CentralProcessor\0")
	processor_brand = winreg.QueryValueEx(key, "ProcessorNameString")[0]
	winreg.CloseKey(key)
	print_output('winreg processor_brand', processor_brand)

	key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"Hardware\Description\System\CentralProcessor\0")
	vendor_id = winreg.QueryValueEx(key, "VendorIdentifier")[0]
	winreg.CloseKey(key)
	print_output('winreg vendor_id', vendor_id)

	key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")
	raw_arch_string = winreg.QueryValueEx(key, "PROCESSOR_ARCHITECTURE")[0]
	winreg.CloseKey(key)
	print_output('winreg raw_arch_string', raw_arch_string)

	key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"Hardware\Description\System\CentralProcessor\0")
	hz_actual = winreg.QueryValueEx(key, "~Mhz")[0]
	winreg.CloseKey(key)
	print_output('winreg hz_actual', hz_actual)

	key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"Hardware\Description\System\CentralProcessor\0")
	feature_bits = winreg.QueryValueEx(key, "FeatureSet")[0]
	winreg.CloseKey(key)
	print_output('winreg feature_bits', feature_bits)

out_file.close()
print('System info written to "{0}"'.format(out_file_name))
<commit_msg>Added more cpu counts to syste info getter.<commit_after>#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Copyright (c) 2014-2019, Matthew Brennan Jones <matthew.brennan.jones@gmail.com>
# Py-cpuinfo gets CPU info with pure Python 2 & 3
# It uses the MIT License
# It is hosted at: https://github.com/workhorsy/py-cpuinfo
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import os, sys
import glob
import platform
import multiprocessing
import subprocess

try:
	import _winreg as winreg
except ImportError as err:
	try:
		import winreg
	except ImportError as err:
		pass

IS_PY2 = sys.version_info[0] == 2

out_file_name = 'system_info.txt'
out_file = open(out_file_name, 'w')

def run_and_get_stdout(command, pipe_command=None):
	if not pipe_command:
		p1 = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
		output = p1.communicate()[0]
		if not IS_PY2:
			output = output.decode(encoding='UTF-8')
		return p1.returncode, output
	else:
		p1 = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
		p2 = subprocess.Popen(pipe_command, stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
		p1.stdout.close()
		output = p2.communicate()[0]
		if not IS_PY2:
			output = output.decode(encoding='UTF-8')
		return p2.returncode, output

def program_paths(program_name):
	paths = []
	exts = filter(None, os.environ.get('PATHEXT', '').split(os.pathsep))
	path = os.environ['PATH']
	for p in os.environ['PATH'].split(os.pathsep):
		p = os.path.join(p, program_name)
		if os.access(p, os.X_OK):
			paths.append(p)
		for e in exts:
			pext = p + e
			if os.access(pext, os.X_OK):
				paths.append(pext)
	return paths

def print_output(name, output):
	line = "=" * 79
	out_file.write('{0}:\n{1}\n{2}\n\n\n\n'.format(name, line, output))

print_output('sys.executable', sys.executable)

print_output('sys.version_info', sys.version_info)

if hasattr(sys, 'maxsize'):
	sizes = {
		2**31-1: '32 bit',
		2**63-1: '64 bit',
	}
	friendly_maxsize = sizes.get(sys.maxsize) or 'unknown'
	print_output('sys.maxsize', "{0} ({1})".format(sys.maxsize, friendly_maxsize))

print_output('multiprocessing.cpu_count', multiprocessing.cpu_count())

if hasattr(os, 'cpu_count'):
	print_output('os.cpu_count', os.cpu_count())

if 'NUMBER_OF_PROCESSORS' in os.environ:
	print_output("os.environ['NUMBER_OF_PROCESSORS']", os.environ['NUMBER_OF_PROCESSORS'])

if hasattr(os, 'sysconf'):
	print_output("os.sysconf('SC_NPROCESSORS_ONLN')", os.sysconf('SC_NPROCESSORS_ONLN'))

if program_paths('sysctl'):
	returncode, output = run_and_get_stdout(['sysctl', '-n', 'hw.ncpu'])
	print_output('sysctl -n hw.ncpu', output)

print_output('platform.uname', platform.uname())

print_output('platform.architecture', platform.architecture())

print_output('platform.system', platform.system())

print_output('platform.machine', platform.machine())

if program_paths('cpufreq-info'):
	returncode, output = run_and_get_stdout(['cpufreq-info'])
	print_output('cpufreq-info', output)

if program_paths('sestatus'):
	returncode, output = run_and_get_stdout(['sestatus', '-b'])
	print_output('sestatus', output)

if os.path.exists('/proc/cpuinfo'):
	returncode, output = run_and_get_stdout(['cat', '/proc/cpuinfo'])
	print_output('cat /proc/cpuinfo', output)

if program_paths('sysctl'):
	returncode, output = run_and_get_stdout(['sysctl', 'machdep.cpu', 'hw.cpufrequency'])
	print_output('sysctl machdep.cpu hw.cpufrequency', output)

if program_paths('sysctl'):
	returncode, output = run_and_get_stdout(['sysctl', 'hw.model', 'hw.machine'])
	print_output('sysctl hw.model hw.machine', output)

if program_paths('isainfo'):
	returncode, output = run_and_get_stdout(['isainfo', '-vb'])
	print_output('isainfo -vb', output)

if program_paths('kstat'):
	returncode, output = run_and_get_stdout(['kstat', '-m', 'cpu_info'])
	print_output('kstat -m cpu_info', output)

if program_paths('lscpu'):
	returncode, output = run_and_get_stdout(['lscpu'])
	print_output('lscpu', output)

if program_paths('dmesg'):
	returncode, output = run_and_get_stdout(['dmesg', '-a'])
	if returncode != 0:
		returncode, output = run_and_get_stdout(['dmesg'])
	if len(output) > 20480:
		output = output[0 : 20480]
	print_output('dmesg', output)

if os.path.exists('/var/run/dmesg.boot'):
	returncode, output = run_and_get_stdout(['cat', '/var/run/dmesg.boot'])
	if len(output) > 20480:
		output = output[0 : 20480]
	print_output('/var/run/dmesg.boot', output)

if program_paths('sysinfo'):
	returncode, output = run_and_get_stdout(['sysinfo', '-cpu'])
	print_output('sysinfo -cpu', output)

if program_paths('lsprop'):
	ibm_features = glob.glob('/proc/device-tree/cpus/*/ibm,pa-features')
	if ibm_features:
		returncode, output = run_and_get_stdout(['lsprop', ibm_features[0]])
		print_output('lsprop /proc/device-tree/cpus/*/ibm,pa-features', output)

if program_paths('wmic'):
	returncode, output = run_and_get_stdout(['wmic', 'os', 'get', 'Version'])
	if returncode == 0 and len(output) > 0:
		returncode, output = run_and_get_stdout(['wmic', 'cpu', 'get', 'Name,CurrentClockSpeed,L2CacheSize,L3CacheSize,Description,Caption,Manufacturer', '/format:list'])
		if returncode == 0 and len(output) > 0:
			print_output('wmic cpu get Name,CurrentClockSpeed,L2CacheSize,L3CacheSize,Description,Caption,Manufacturer /format:list', output)

if 'winreg' in sys.modules or '_winreg' in sys.modules:
	key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"Hardware\Description\System\CentralProcessor\0")
	processor_brand = winreg.QueryValueEx(key, "ProcessorNameString")[0]
	winreg.CloseKey(key)
	print_output('winreg processor_brand', processor_brand)

	key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"Hardware\Description\System\CentralProcessor\0")
	vendor_id = winreg.QueryValueEx(key, "VendorIdentifier")[0]
	winreg.CloseKey(key)
	print_output('winreg vendor_id', vendor_id)

	key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")
	raw_arch_string = winreg.QueryValueEx(key, "PROCESSOR_ARCHITECTURE")[0]
	winreg.CloseKey(key)
	print_output('winreg raw_arch_string', raw_arch_string)

	key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"Hardware\Description\System\CentralProcessor\0")
	hz_actual = winreg.QueryValueEx(key, "~Mhz")[0]
	winreg.CloseKey(key)
	print_output('winreg hz_actual', hz_actual)

	key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"Hardware\Description\System\CentralProcessor\0")
	feature_bits = winreg.QueryValueEx(key, "FeatureSet")[0]
	winreg.CloseKey(key)
	print_output('winreg feature_bits', feature_bits)

out_file.close()
print('System info written to "{0}"'.format(out_file_name))
<|endoftext|>


<commit_before>
# example:
# python get_time.py exec 4 -1 /path/to/benchs /path/to/lc "test;--max-versions 5;..." "other;--max-versions 10;..."

import re
import os
import sys
import glob
import signal
import subprocess

# EXEC TIME
# exec only (no comp, no gc) using 2nd execution
def get_exec(result):

    def get_exec_cpu():
        m = re.findall('CPU time: ' + NUMBER_REGEX + '\n', result)
        assert len(m) == 2, 'Invalid regexp'
        return float(m[1][0])

    def get_gc_cpu():
        muser = re.findall('GC user time: '+ NUMBER_REGEX + '\n',result)
        msys = re.findall('GC sys time: '+ NUMBER_REGEX + '\n',result)
        assert len(muser) == 2 and len(msys) == 2, 'Invalid regexp'
        ruser = float(muser[1][0])
        rsys = float(msys[1][0])
        return ruser + rsys

    return get_exec_cpu() - get_gc_cpu()

# COMP TIME
# compilation time only (no exec, no gc and no gc used for compilation) using 1st execution
def get_comp(result):
    m = re.findall('Compilation time \(user time\):' + NUMBER_REGEX + '\n',result)
    assert len(m) == 1, 'Invalid regexp'
    result = float(m[0][0])
    return result

# TOTAL TIME
# total time including compilation time, gc time for compilation & execution, and execution time
def get_total(result):
    m = re.findall('CPU time: '+ NUMBER_REGEX + '\n',result)
    assert len(m) == 1, 'Invalid regexp'
    result = float(m[0][0])
    return result

# Check cmd line args
if len(sys.argv) < 7:
    print('Invalid arguments')
    print('Usage:')
    print('   python thisscript.py time nb_iters bench_path lc_path [execs]')
    print('Each exec has the form:')
    print('exec_name;arg1;arg2;...;argn')
    print('time is "compil", "exec" or "total"')
    sys.exit(0)

# From: https://docs.python.org/3/library/re.html#simulating-scanf
NUMBER_REGEX = '([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)'

TIME       = sys.argv[1]
NITERS     = int(sys.argv[2])
TIMEOUT    = int(sys.argv[3]) # timeout in seconds. 0 for no timeout
BENCH_PATH = os.path.abspath(sys.argv[4]) + '/' # Benchmarks path (contains .scm files)
LC_PATH    = os.path.abspath(sys.argv[5])       # LC path executable path
EXECS      = []
BASE_CMD   = []
GET_FUNC   = False

if TIME == 'exec':
    BASE_CMD = ['--time']
    GET_FUNC = get_exec
elif TIME == 'compil':
    BASE_CMD = ['--ctime']
    GET_FUNC = get_comp
elif TIME == 'total':
    BASE_CMD = []
    GET_FUNC = get_total
else:
    print('ERROR: invalid time value')
    sys.exit(0)

for i in range(6,len(sys.argv)):
    els = sys.argv[i].split(';')
    name = els[0]
    args = els[1:] + BASE_CMD
    EXECS.append([name,args])

if NITERS < 3:
    print('ERROR: niters is too small')
    sys.exit(0)

benchmarks = sorted(glob.glob(BENCH_PATH + '*.scm'))

def run_and_get(cmd):
    if TIMEOUT <= 0:
        with subprocess.Popen(cmd, stdout=subprocess.PIPE, preexec_fn=os.setsid) as process:
            out, errs = process.communicate()
            strresult = out.decode('utf-8')
            return GET_FUNC(strresult)
    else:
        with subprocess.Popen(cmd, stdout=subprocess.PIPE, preexec_fn=os.setsid) as process:
            try:
                out, errs = process.communicate(timeout=1)
                strresult = out.decode('utf-8')
                return GET_FUNC(strresult)
            except subprocess.TimeoutExpired:
                os.killpg(process.pid, signal.SIGINT) # kill the group
                out, errs = process.communicate()
                return -1

RESULT = []

# For each benchmark
bench_i=1
for benchmark in benchmarks:

    bench_name = os.path.basename(benchmark).replace('.scm','')
    bench_res = [bench_name]

    # Handle cpt
    bench_istr = str(bench_i) if bench_i >=10 else '0' + str(bench_i)
    print('(' + str(bench_istr) + '/' + str(len(benchmarks)) + ') Running benchmark ' + bench_name + '...')

    # For each exec
    for exec in EXECS:

        print('  exec ' + exec[0], end='')
        sys.stdout.flush()
        ex_args = exec[1]
        ex_cmd = [LC_PATH,benchmark] + exec[1]
        ex_res = []

        for i in range(0,NITERS):
            print('.',end='')
            sys.stdout.flush()
            ex_res += [run_and_get(ex_cmd)]
        print('')

        ex_res.remove(max(ex_res))
        ex_res.remove(min(ex_res))
        mean = sum(ex_res) / float(len(ex_res))

        bench_res += [mean]

    print('')
    bench_i += 1

    # Update global result
    RESULT.append(bench_res)

# Pretty print csv
names = list(map(lambda x:str(x[0]),EXECS))
print('benchmark;'+';'.join(names))
for res in RESULT:
    strs = list(map(lambda x:str(x),res))
    print(';'.join(strs))
<commit_msg>Use the right timeout value and stop exec if timeout occurs<commit_after>
# example:
# python get_time.py exec 4 -1 /path/to/benchs /path/to/lc "test;--max-versions 5;..." "other;--max-versions 10;..."

import re
import os
import sys
import glob
import signal
import subprocess

# EXEC TIME
# exec only (no comp, no gc) using 2nd execution
def get_exec(result):

    def get_exec_cpu():
        m = re.findall('CPU time: ' + NUMBER_REGEX + '\n', result)
        assert len(m) == 2, 'Invalid regexp'
        return float(m[1][0])

    def get_gc_cpu():
        muser = re.findall('GC user time: '+ NUMBER_REGEX + '\n',result)
        msys = re.findall('GC sys time: '+ NUMBER_REGEX + '\n',result)
        assert len(muser) == 2 and len(msys) == 2, 'Invalid regexp'
        ruser = float(muser[1][0])
        rsys = float(msys[1][0])
        return ruser + rsys

    return get_exec_cpu() - get_gc_cpu()

# COMP TIME
# compilation time only (no exec, no gc and no gc used for compilation) using 1st execution
def get_comp(result):
    m = re.findall('Compilation time \(user time\):' + NUMBER_REGEX + '\n',result)
    assert len(m) == 1, 'Invalid regexp'
    result = float(m[0][0])
    return result

# TOTAL TIME
# total time including compilation time, gc time for compilation & execution, and execution time
def get_total(result):
    m = re.findall('CPU time: '+ NUMBER_REGEX + '\n',result)
    assert len(m) == 1, 'Invalid regexp'
    result = float(m[0][0])
    return result

# Check cmd line args
if len(sys.argv) < 7:
    print('Invalid arguments')
    print('Usage:')
    print('   python thisscript.py time nb_iters bench_path lc_path [execs]')
    print('Each exec has the form:')
    print('exec_name;arg1;arg2;...;argn')
    print('time is "compil", "exec" or "total"')
    sys.exit(0)

# From: https://docs.python.org/3/library/re.html#simulating-scanf
NUMBER_REGEX = '([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)'

TIME       = sys.argv[1]
NITERS     = int(sys.argv[2])
TIMEOUT    = int(sys.argv[3]) # timeout in seconds. 0 for no timeout
BENCH_PATH = os.path.abspath(sys.argv[4]) + '/' # Benchmarks path (contains .scm files)
LC_PATH    = os.path.abspath(sys.argv[5])       # LC path executable path
EXECS      = []
BASE_CMD   = []
GET_FUNC   = False

if TIME == 'exec':
    BASE_CMD = ['--time']
    GET_FUNC = get_exec
elif TIME == 'compil':
    BASE_CMD = ['--ctime']
    GET_FUNC = get_comp
elif TIME == 'total':
    BASE_CMD = []
    GET_FUNC = get_total
else:
    print('ERROR: invalid time value')
    sys.exit(0)

for i in range(6,len(sys.argv)):
    els = sys.argv[i].split(';')
    name = els[0]
    args = els[1:] + BASE_CMD
    EXECS.append([name,args])

if NITERS < 3:
    print('ERROR: niters is too small')
    sys.exit(0)

benchmarks = sorted(glob.glob(BENCH_PATH + '*.scm'))

def run_and_get(cmd):
    if TIMEOUT <= 0:
        with subprocess.Popen(cmd, stdout=subprocess.PIPE, preexec_fn=os.setsid) as process:
            out, errs = process.communicate()
            strresult = out.decode('utf-8')
            return GET_FUNC(strresult)
    else:
        with subprocess.Popen(cmd, stdout=subprocess.PIPE, preexec_fn=os.setsid) as process:
            try:
                out, errs = process.communicate(timeout=TIMEOUT)
                strresult = out.decode('utf-8')
                return GET_FUNC(strresult)
            except subprocess.TimeoutExpired:
                os.killpg(process.pid, signal.SIGINT) # kill the group
                out, errs = process.communicate()
                return -1

RESULT = []

# For each benchmark
bench_i=1
for benchmark in benchmarks:

    bench_name = os.path.basename(benchmark).replace('.scm','')
    bench_res = [bench_name]

    # Handle cpt
    bench_istr = str(bench_i) if bench_i >=10 else '0' + str(bench_i)
    print('(' + str(bench_istr) + '/' + str(len(benchmarks)) + ') Running benchmark ' + bench_name + '...')

    # For each exec
    for exec in EXECS:

        print('  exec ' + exec[0], end='')
        sys.stdout.flush()
        ex_args = exec[1]
        ex_cmd = [LC_PATH,benchmark] + exec[1]
        ex_res = []

        for i in range(0,NITERS):
            print('.',end='')
            sys.stdout.flush()
            r = run_and_get(ex_cmd)
            if r < 0:
                ex_res = False
                break
            else:
                ex_res += [r]
        print('')

        if not ex_res:
            bench_res += ["TIMEOUT"]
        else:
            ex_res.remove(max(ex_res))
            ex_res.remove(min(ex_res))
            mean = sum(ex_res) / float(len(ex_res))
            bench_res += [mean]

    print('')
    bench_i += 1

    # Update global result
    RESULT.append(bench_res)

# Pretty print csv
names = list(map(lambda x:str(x[0]),EXECS))
print('benchmark;'+';'.join(names))
for res in RESULT:
    strs = list(map(lambda x:str(x),res))
    print(';'.join(strs))
<|endoftext|>


<commit_before>import bisect

def bisect_left(a, x, lo=0, hi=None):
    """
    """
    if hi is None:
        hi = len(a)

    while (lo < hi):
        mid = (lo + hi) // 2
        if x <= a[mid]:
            hi = mid
        else:
            lo = mid + 1
    return lo

def bisect_right(a, x, lo=0, hi=None):
    """
    """
    if hi is None:
        hi = len(a)

    while (lo < hi):
        mid = (lo + hi) // 2
        if a[mid] <= x:
            lo = mid + 1
        else:
            hi = mid
    return lo
<commit_msg>minor update<commit_after>import bisect

def bisect_left(a, x, lo=0, hi=None):
    """
    """
    if hi is None:
        hi = len(a)

    while (lo < hi):
        mid = (lo + hi) // 2
        if x <= a[mid]:
            hi = mid
        else:
            lo = mid + 1
    return lo

def bisect_right(a, x, lo=0, hi=None):
    """
    """
    if hi is None:
        hi = len(a)

    while (lo < hi):
        mid = (lo + hi) // 2
        if x < a[mid]:
            hi = mid
        else:
            lo = mid + 1
    return lo
<|endoftext|>


<commit_before><commit_msg>fix the grd scanner to only include files that we're using<commit_after><|endoftext|>


<commit_before>#!/usr/bin/python2.4
# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

'''SCons integration for GRIT.
'''

# NOTE:  DO NOT IMPORT ANY GRIT STUFF HERE - we import lazily so that grit and
# its dependencies aren't imported until actually needed.

import os
import types

def _IsDebugEnabled():
  return 'GRIT_DEBUG' in os.environ and os.environ['GRIT_DEBUG'] == '1'

def _SourceToFile(source):
  '''Return the path to the source file, given the 'source' argument as provided
  by SCons to the _Builder or _Emitter functions.
  '''
  # Get the filename of the source.  The 'source' parameter can be a string,
  # a "node", or a list of strings or nodes.
  if isinstance(source, types.ListType):
    # TODO(gspencer):  Had to add the .rfile() method to the following
    # line to get this to work with Repository() directories.
    # Get this functionality folded back into the upstream grit tool.
    #source = str(source[0])
    for s in source:
      if str(s.rfile()).endswith('.grd'):
        return str(s.rfile())
  else:
    # TODO(gspencer):  Had to add the .rfile() method to the following
    # line to get this to work with Repository() directories.
    # Get this functionality folded back into the upstream grit tool.
    #source = str(source))
    source = str(source.rfile())
  return source

def _Builder(target, source, env):
  # We fork GRIT into a separate process so we can use more processes between
  # scons and GRIT. This already runs as separate threads, but because of the
  # python GIL, all these threads have to share the same process.  By using
  # fork, we can use multiple processes and processors.
  pid = os.fork()
  if pid != 0:
    os.waitpid(pid, 0)
    return
  from grit import grit_runner
  from grit.tool import build
  options = grit_runner.Options()
  # This sets options to default values.
  options.ReadOptions(['-v'])
  options.input = _SourceToFile(source)

  # TODO(joi) Check if we can get the 'verbose' option from the environment.

  builder = build.RcBuilder()

  # Get the CPP defines from the environment.
  for flag in env.get('RCFLAGS', []):
    if flag.startswith('/D'):
      flag = flag[2:]
    name, val = build.ParseDefine(flag)
    # Only apply to first instance of a given define
    if name not in builder.defines:
      builder.defines[name] = val

  # To ensure that our output files match what we promised SCons, we
  # use the list of targets provided by SCons and update the file paths in
  # our .grd input file with the targets.
  builder.scons_targets = [str(t) for t in target]
  builder.Run(options, [])

  # Exit the child process.
  os._exit(0)

def _Emitter(target, source, env):
  '''A SCons emitter for .grd files, which modifies the list of targes to
  include all files in the <outputs> section of the .grd file as well as
  any other files output by 'grit build' for the .grd file.
  '''
  from grit import grd_reader
  from grit import util

  # TODO(gspencer):  Had to use .abspath, not str(target[0]), to get
  # this to work with Repository() directories.
  # Get this functionality folded back into the upstream grit tool.
  #base_dir = util.dirname(str(target[0]))
  base_dir = util.dirname(target[0].abspath)

  grd = grd_reader.Parse(_SourceToFile(source), debug=_IsDebugEnabled())

  target = []
  lang_folders = {}
  # Add all explicitly-specified output files
  for output in grd.GetOutputFiles():
    path = os.path.join(base_dir, output.GetFilename())
    target.append(path)

    if path.endswith('.h'):
      path, filename = os.path.split(path)
    if _IsDebugEnabled():
      print "GRIT: Added target %s" % path
    if output.attrs['lang'] != '':
      lang_folders[output.attrs['lang']] = os.path.dirname(path)

  # Add all generated files, once for each output language.
  for node in grd:
    if node.name == 'structure':
      # TODO(joi) Should remove the "if sconsdep is true" thing as it is a
      # hack - see grit/node/structure.py
      if node.HasFileForLanguage() and node.attrs['sconsdep'] == 'true':
        for lang in lang_folders:
          path = node.FileForLanguage(lang, lang_folders[lang],
                                      create_file=False,
                                      return_if_not_generated=False)
          if path:
            target.append(path)
            if _IsDebugEnabled():
              print "GRIT: Added target %s" % path

  # return target and source lists
  return (target, source)

def _Scanner(file_node, env, path):
  '''A SCons scanner function for .grd files, which outputs the list of files
  that changes in could change the output of building the .grd file.
  '''
  if not str(file_node.rfile()).endswith('.grd'):
    return []

  from grit import grd_reader
  # TODO(gspencer):  Had to add the .rfile() method to the following
  # line to get this to work with Repository() directories.
  # Get this functionality folded back into the upstream grit tool.
  #grd = grd_reader.Parse(str(file_node)), debug=_IsDebugEnabled())
  grd = grd_reader.Parse(os.path.abspath(_SourceToFile(file_node)),
                         debug=_IsDebugEnabled())
  files = []
  for node in grd:
    if (node.name == 'structure' or node.name == 'skeleton' or
        (node.name == 'file' and node.parent and
         node.parent.name == 'translations')):
      files.append(os.path.abspath(node.GetFilePath()))
    elif node.name == 'include':
      # Only include files that we actually plan on using.
      if node.SatisfiesOutputCondition():
        files.append(node.FilenameToOpen())

  # Add in the grit source files.  If one of these change, we want to re-run
  # grit.
  grit_root_dir = env.subst('$CHROME_SRC_DIR/tools/grit')
  for root, dirs, filenames in os.walk(grit_root_dir):
    grit_src = [os.path.join(root, f) for f in filenames if f.endswith('.py')]
    files.extend(grit_src)

  return files

def _BuildStr(targets, sources, env):
  '''This message gets printed each time the builder runs.'''
  return "Running GRIT on %s" % str(sources[0].rfile())

# Function name is mandated by newer versions of SCons.
def generate(env):
  # The varlist parameter tells SCons that GRIT needs to be invoked again
  # if RCFLAGS has changed since last compilation.

  # TODO(gspencer):  change to use the public SCons API Action()
  # and Builder(), instead of reaching directly into internal APIs.
  # Get this change folded back into the upstream grit tool.
  action = env.Action(_Builder, _BuildStr, varlist=['RCFLAGS'])

  scanner = env.Scanner(function=_Scanner, name='GRIT scanner', skeys=['.grd'])

  builder = env.Builder(action=action, emitter=_Emitter,
                        source_scanner=scanner,
                        src_suffix='.grd')

  # add our builder and scanner to the environment
  env.Append(BUILDERS = {'GRIT': builder})

# Function name is mandated by newer versions of SCons.
def exists(env):
  return 1
<commit_msg>Properly handle errors in GRIT in the scons build.<commit_after>#!/usr/bin/python2.4
# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

'''SCons integration for GRIT.
'''

# NOTE:  DO NOT IMPORT ANY GRIT STUFF HERE - we import lazily so that grit and
# its dependencies aren't imported until actually needed.

import os
import types

import SCons.Errors

def _IsDebugEnabled():
  return 'GRIT_DEBUG' in os.environ and os.environ['GRIT_DEBUG'] == '1'

def _SourceToFile(source):
  '''Return the path to the source file, given the 'source' argument as provided
  by SCons to the _Builder or _Emitter functions.
  '''
  # Get the filename of the source.  The 'source' parameter can be a string,
  # a "node", or a list of strings or nodes.
  if isinstance(source, types.ListType):
    # TODO(gspencer):  Had to add the .rfile() method to the following
    # line to get this to work with Repository() directories.
    # Get this functionality folded back into the upstream grit tool.
    #source = str(source[0])
    for s in source:
      if str(s.rfile()).endswith('.grd'):
        return str(s.rfile())
  else:
    # TODO(gspencer):  Had to add the .rfile() method to the following
    # line to get this to work with Repository() directories.
    # Get this functionality folded back into the upstream grit tool.
    #source = str(source))
    source = str(source.rfile())
  return source

def _Builder(target, source, env):
  # We fork GRIT into a separate process so we can use more processes between
  # scons and GRIT. This already runs as separate threads, but because of the
  # python GIL, all these threads have to share the same process.  By using
  # fork, we can use multiple processes and processors.
  pid = os.fork()
  if pid != 0:
    pid, exit_code = os.waitpid(pid, 0)
    if exit_code != 0:
      raise SCons.Errors.BuildError(errstr="see grit error")
    return
  try:
    child_exit_code = 0
    from grit import grit_runner
    from grit.tool import build
    options = grit_runner.Options()
    # This sets options to default values.
    options.ReadOptions(['-v'])
    options.input = _SourceToFile(source)

    # TODO(joi) Check if we can get the 'verbose' option from the environment.

    builder = build.RcBuilder()

    # Get the CPP defines from the environment.
    for flag in env.get('RCFLAGS', []):
      if flag.startswith('/D'):
        flag = flag[2:]
      name, val = build.ParseDefine(flag)
      # Only apply to first instance of a given define
      if name not in builder.defines:
        builder.defines[name] = val

    # To ensure that our output files match what we promised SCons, we
    # use the list of targets provided by SCons and update the file paths in
    # our .grd input file with the targets.
    builder.scons_targets = [str(t) for t in target]
    builder.Run(options, [])
  except:
    child_exit_code = -1
  finally:
    # Exit the child process.
    os._exit(child_exit_code)

def _Emitter(target, source, env):
  '''A SCons emitter for .grd files, which modifies the list of targes to
  include all files in the <outputs> section of the .grd file as well as
  any other files output by 'grit build' for the .grd file.
  '''
  from grit import grd_reader
  from grit import util

  # TODO(gspencer):  Had to use .abspath, not str(target[0]), to get
  # this to work with Repository() directories.
  # Get this functionality folded back into the upstream grit tool.
  #base_dir = util.dirname(str(target[0]))
  base_dir = util.dirname(target[0].abspath)

  grd = grd_reader.Parse(_SourceToFile(source), debug=_IsDebugEnabled())

  target = []
  lang_folders = {}
  # Add all explicitly-specified output files
  for output in grd.GetOutputFiles():
    path = os.path.join(base_dir, output.GetFilename())
    target.append(path)

    if path.endswith('.h'):
      path, filename = os.path.split(path)
    if _IsDebugEnabled():
      print "GRIT: Added target %s" % path
    if output.attrs['lang'] != '':
      lang_folders[output.attrs['lang']] = os.path.dirname(path)

  # Add all generated files, once for each output language.
  for node in grd:
    if node.name == 'structure':
      # TODO(joi) Should remove the "if sconsdep is true" thing as it is a
      # hack - see grit/node/structure.py
      if node.HasFileForLanguage() and node.attrs['sconsdep'] == 'true':
        for lang in lang_folders:
          path = node.FileForLanguage(lang, lang_folders[lang],
                                      create_file=False,
                                      return_if_not_generated=False)
          if path:
            target.append(path)
            if _IsDebugEnabled():
              print "GRIT: Added target %s" % path

  # return target and source lists
  return (target, source)

def _Scanner(file_node, env, path):
  '''A SCons scanner function for .grd files, which outputs the list of files
  that changes in could change the output of building the .grd file.
  '''
  if not str(file_node.rfile()).endswith('.grd'):
    return []

  from grit import grd_reader
  # TODO(gspencer):  Had to add the .rfile() method to the following
  # line to get this to work with Repository() directories.
  # Get this functionality folded back into the upstream grit tool.
  #grd = grd_reader.Parse(str(file_node)), debug=_IsDebugEnabled())
  grd = grd_reader.Parse(os.path.abspath(_SourceToFile(file_node)),
                         debug=_IsDebugEnabled())
  files = []
  for node in grd:
    if (node.name == 'structure' or node.name == 'skeleton' or
        (node.name == 'file' and node.parent and
         node.parent.name == 'translations')):
      files.append(os.path.abspath(node.GetFilePath()))
    elif node.name == 'include':
      # Only include files that we actually plan on using.
      if node.SatisfiesOutputCondition():
        files.append(node.FilenameToOpen())

  # Add in the grit source files.  If one of these change, we want to re-run
  # grit.
  grit_root_dir = env.subst('$CHROME_SRC_DIR/tools/grit')
  for root, dirs, filenames in os.walk(grit_root_dir):
    grit_src = [os.path.join(root, f) for f in filenames if f.endswith('.py')]
    files.extend(grit_src)

  return files

def _BuildStr(targets, sources, env):
  '''This message gets printed each time the builder runs.'''
  return "Running GRIT on %s" % str(sources[0].rfile())

# Function name is mandated by newer versions of SCons.
def generate(env):
  # The varlist parameter tells SCons that GRIT needs to be invoked again
  # if RCFLAGS has changed since last compilation.

  # TODO(gspencer):  change to use the public SCons API Action()
  # and Builder(), instead of reaching directly into internal APIs.
  # Get this change folded back into the upstream grit tool.
  action = env.Action(_Builder, _BuildStr, varlist=['RCFLAGS'])

  scanner = env.Scanner(function=_Scanner, name='GRIT scanner', skeys=['.grd'])

  builder = env.Builder(action=action, emitter=_Emitter,
                        source_scanner=scanner,
                        src_suffix='.grd')

  # add our builder and scanner to the environment
  env.Append(BUILDERS = {'GRIT': builder})

# Function name is mandated by newer versions of SCons.
def exists(env):
  return 1
<|endoftext|>


<commit_before>#!/usr/bin/python2.4
# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

'''Adaptation of the extern.tclib classes for our needs.
'''

import re
import types

from grit import exception
import grit.extern.tclib

def Identity(i):
  return i

class BaseMessage(object):
  '''Base class with methods shared by Message and Translation.
  '''

  def __init__(self, text='', placeholders=[], description='', meaning=''):
    self.parts = []
    self.placeholders = []
    self.description = description
    self.meaning = meaning
    self.dirty = True  # True if self.id is (or might be) wrong
    self.id = 0

    if text != '':
      if not placeholders or placeholders == []:
        self.AppendText(text)
      else:
        tag_map = {}
        for placeholder in placeholders:
          tag_map[placeholder.GetPresentation()] = [placeholder, 0]
        tag_re = '(' + '|'.join(tag_map.keys()) + ')'
        # This creates a regexp like '(TAG1|TAG2|TAG3)'
        chunked_text = re.split(tag_re, text)
        for chunk in chunked_text:
          if chunk: # ignore empty chunk
            if tag_map.has_key(chunk):
              self.AppendPlaceholder(tag_map[chunk][0])
              tag_map[chunk][1] += 1 # increase placeholder use count
            else:
              self.AppendText(chunk)
        for key in tag_map.keys():
          assert tag_map[key][1] != 0

  def GetRealContent(self, escaping_function=Identity):
    '''Returns the original content, i.e. what your application and users
    will see.

    Specify a function to escape each translateable bit, if you like.
    '''
    bits = []
    for item in self.parts:
      if isinstance(item, types.StringTypes):
        bits.append(escaping_function(item))
      else:
        bits.append(item.GetOriginal())
    return ''.join(bits)

  def GetPresentableContent(self):
    presentable_content = []
    for part in self.parts:
      if isinstance(part, Placeholder):
        presentable_content.append(part.GetPresentation())
      else:
        presentable_content.append(part)
    return ''.join(presentable_content)

  def AppendPlaceholder(self, placeholder):
    assert isinstance(placeholder, Placeholder)
    dup = False
    for other in self.GetPlaceholders():
      if other.presentation == placeholder.presentation:
        assert other.original == placeholder.original
        dup = True

    if not dup:
      self.placeholders.append(placeholder)
    self.parts.append(placeholder)
    self.dirty = True

  def AppendText(self, text):
    assert isinstance(text, types.StringTypes)
    assert text != ''

    self.parts.append(text)
    self.dirty = True

  def GetContent(self):
    '''Returns the parts of the message.  You may modify parts if you wish.
    Note that you must not call GetId() on this object until you have finished
    modifying the contents.
    '''
    self.dirty = True  # user might modify content
    return self.parts

  def GetDescription(self):
    return self.description

  def SetDescription(self, description):
    self.description = description

  def GetMeaning(self):
    return self.meaning

  def GetId(self):
    if self.dirty:
      self.id = self.GenerateId()
      self.dirty = False
    return self.id

  def GenerateId(self):
    # Must use a UTF-8 encoded version of the presentable content, along with
    # the meaning attribute, to match the TC.
    return grit.extern.tclib.GenerateMessageId(
      self.GetPresentableContent().encode('utf-8'), self.meaning)

  def GetPlaceholders(self):
    return self.placeholders

  def FillTclibBaseMessage(self, msg):
    msg.SetDescription(self.description.encode('utf-8'))

    for part in self.parts:
      if isinstance(part, Placeholder):
        ph = grit.extern.tclib.Placeholder(
          part.presentation.encode('utf-8'),
          part.original.encode('utf-8'),
          part.example.encode('utf-8'))
        msg.AppendPlaceholder(ph)
      else:
        msg.AppendText(part.encode('utf-8'))

class Message(BaseMessage):
  '''A message.'''

  def __init__(self, text='', placeholders=[], description='', meaning='',
               assigned_id=None):
    BaseMessage.__init__(self, text, placeholders, description, meaning)
    self.assigned_id = assigned_id

  def ToTclibMessage(self):
    msg = grit.extern.tclib.Message('utf-8', meaning=self.meaning)
    self.FillTclibBaseMessage(msg)
    return msg

  def GetId(self):
    '''Use the assigned id if we have one.'''
    if self.assigned_id:
      return self.assigned_id

    return BaseMessage.GetId(self)

class Translation(BaseMessage):
  '''A translation.'''

  def __init__(self, text='', id='', placeholders=[], description='', meaning=''):
    BaseMessage.__init__(self, text, placeholders, description, meaning)
    self.id = id

  def GetId(self):
    assert id != '', "ID has not been set."
    return self.id

  def SetId(self, id):
    self.id = id

  def ToTclibMessage(self):
    msg = grit.extern.tclib.Message(
      'utf-8', id=self.id, meaning=self.meaning)
    self.FillTclibBaseMessage(msg)
    return msg

class Placeholder(grit.extern.tclib.Placeholder):
  '''Modifies constructor to accept a Unicode string
  '''

  # Must match placeholder presentation names
  _NAME_RE = re.compile('[A-Za-z0-9_]+')

  def __init__(self, presentation, original, example):
    '''Creates a new placeholder.

    Args:
      presentation: 'USERNAME'
      original: '%s'
      example: 'Joi'
    '''
    assert presentation != ''
    assert original != ''
    assert example != ''
    if not self._NAME_RE.match(presentation):
      raise exception.InvalidPlaceholderName(presentation)
    self.presentation = presentation
    self.original = original
    self.example = example

  def GetPresentation(self):
    return self.presentation

  def GetOriginal(self):
    return self.original

  def GetExample(self):
    return self.example

<commit_msg>Enforce the restriction that placeholder names can only contain caps, numbers and underscores.<commit_after>#!/usr/bin/python2.4
# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

'''Adaptation of the extern.tclib classes for our needs.
'''

import re
import types

from grit import exception
import grit.extern.tclib

def Identity(i):
  return i

class BaseMessage(object):
  '''Base class with methods shared by Message and Translation.
  '''

  def __init__(self, text='', placeholders=[], description='', meaning=''):
    self.parts = []
    self.placeholders = []
    self.description = description
    self.meaning = meaning
    self.dirty = True  # True if self.id is (or might be) wrong
    self.id = 0

    if text != '':
      if not placeholders or placeholders == []:
        self.AppendText(text)
      else:
        tag_map = {}
        for placeholder in placeholders:
          tag_map[placeholder.GetPresentation()] = [placeholder, 0]
        tag_re = '(' + '|'.join(tag_map.keys()) + ')'
        # This creates a regexp like '(TAG1|TAG2|TAG3)'
        chunked_text = re.split(tag_re, text)
        for chunk in chunked_text:
          if chunk: # ignore empty chunk
            if tag_map.has_key(chunk):
              self.AppendPlaceholder(tag_map[chunk][0])
              tag_map[chunk][1] += 1 # increase placeholder use count
            else:
              self.AppendText(chunk)
        for key in tag_map.keys():
          assert tag_map[key][1] != 0

  def GetRealContent(self, escaping_function=Identity):
    '''Returns the original content, i.e. what your application and users
    will see.

    Specify a function to escape each translateable bit, if you like.
    '''
    bits = []
    for item in self.parts:
      if isinstance(item, types.StringTypes):
        bits.append(escaping_function(item))
      else:
        bits.append(item.GetOriginal())
    return ''.join(bits)

  def GetPresentableContent(self):
    presentable_content = []
    for part in self.parts:
      if isinstance(part, Placeholder):
        presentable_content.append(part.GetPresentation())
      else:
        presentable_content.append(part)
    return ''.join(presentable_content)

  def AppendPlaceholder(self, placeholder):
    assert isinstance(placeholder, Placeholder)
    dup = False
    for other in self.GetPlaceholders():
      if other.presentation == placeholder.presentation:
        assert other.original == placeholder.original
        dup = True

    if not dup:
      self.placeholders.append(placeholder)
    self.parts.append(placeholder)
    self.dirty = True

  def AppendText(self, text):
    assert isinstance(text, types.StringTypes)
    assert text != ''

    self.parts.append(text)
    self.dirty = True

  def GetContent(self):
    '''Returns the parts of the message.  You may modify parts if you wish.
    Note that you must not call GetId() on this object until you have finished
    modifying the contents.
    '''
    self.dirty = True  # user might modify content
    return self.parts

  def GetDescription(self):
    return self.description

  def SetDescription(self, description):
    self.description = description

  def GetMeaning(self):
    return self.meaning

  def GetId(self):
    if self.dirty:
      self.id = self.GenerateId()
      self.dirty = False
    return self.id

  def GenerateId(self):
    # Must use a UTF-8 encoded version of the presentable content, along with
    # the meaning attribute, to match the TC.
    return grit.extern.tclib.GenerateMessageId(
      self.GetPresentableContent().encode('utf-8'), self.meaning)

  def GetPlaceholders(self):
    return self.placeholders

  def FillTclibBaseMessage(self, msg):
    msg.SetDescription(self.description.encode('utf-8'))

    for part in self.parts:
      if isinstance(part, Placeholder):
        ph = grit.extern.tclib.Placeholder(
          part.presentation.encode('utf-8'),
          part.original.encode('utf-8'),
          part.example.encode('utf-8'))
        msg.AppendPlaceholder(ph)
      else:
        msg.AppendText(part.encode('utf-8'))

class Message(BaseMessage):
  '''A message.'''

  def __init__(self, text='', placeholders=[], description='', meaning='',
               assigned_id=None):
    BaseMessage.__init__(self, text, placeholders, description, meaning)
    self.assigned_id = assigned_id

  def ToTclibMessage(self):
    msg = grit.extern.tclib.Message('utf-8', meaning=self.meaning)
    self.FillTclibBaseMessage(msg)
    return msg

  def GetId(self):
    '''Use the assigned id if we have one.'''
    if self.assigned_id:
      return self.assigned_id

    return BaseMessage.GetId(self)

class Translation(BaseMessage):
  '''A translation.'''

  def __init__(self, text='', id='', placeholders=[], description='', meaning=''):
    BaseMessage.__init__(self, text, placeholders, description, meaning)
    self.id = id

  def GetId(self):
    assert id != '', "ID has not been set."
    return self.id

  def SetId(self, id):
    self.id = id

  def ToTclibMessage(self):
    msg = grit.extern.tclib.Message(
      'utf-8', id=self.id, meaning=self.meaning)
    self.FillTclibBaseMessage(msg)
    return msg

class Placeholder(grit.extern.tclib.Placeholder):
  '''Modifies constructor to accept a Unicode string
  '''

  # Must match placeholder presentation names
  _NAME_RE = re.compile('^[A-Za-z0-9_]+$')

  def __init__(self, presentation, original, example):
    '''Creates a new placeholder.

    Args:
      presentation: 'USERNAME'
      original: '%s'
      example: 'Joi'
    '''
    assert presentation != ''
    assert original != ''
    assert example != ''
    if not self._NAME_RE.match(presentation):
      raise exception.InvalidPlaceholderName(presentation)
    self.presentation = presentation
    self.original = original
    self.example = example

  def GetPresentation(self):
    return self.presentation

  def GetOriginal(self):
    return self.original

  def GetExample(self):
    return self.example

<|endoftext|>


<commit_before>from ggame import App, RectangleAsset, ImageAsset, Sprite, LineStyle, Color, Frame
from tank import Tank
import math

class Bullet(Sprite):
    asset = ImageAsset("images/300px-BM_Grenade.png", Frame(100,20,100,115), 1, 'vertical')
    asset2 = ImageAsset("images/GrenadeExplosion.png", Frame(0,0,51.2,128), 20, 'horizontal')

    def __init__(self, position):
        super().__init__(Bullet.asset, position)
        t = 1.00
        V = 4
        self.vx=V*math.cos(t)
        self.vy=-V*math.sin(t)
        self.scale=.5
        self.vr=0
        self.fxcenter = .5
        self.fycenter = .5
        self.away = False
        self.explode = True
        self.explodeframe = 1

    def step(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += .05
        self.rotation += .1
        colliding = self.collidingWithSprites(Tank)
        if colliding and self.away == True:
            self.destroy()
            self.setImage(self.explodeframe)
            self.explodeframe += 1
            if self.explodeframe == 20:
                self.explodeframe = 1
        elif not colliding and self.away == False:
            self.away = True
<commit_msg>Updated from Brython Server: December 11, 2015 2:22:13 PM EST<commit_after>from ggame import App, RectangleAsset, ImageAsset, Sprite, LineStyle, Color, Frame
from tank import Tank
import math

class Bullet(Sprite):
    asset = ImageAsset("images/300px-BM_Grenade.png", Frame(100,20,100,115), 1, 'vertical')
    asset2 = ImageAsset("images/GrenadeExplosion.png", Frame(0,0,51.2,128), 20, 'horizontal')

    def __init__(self, position):
        super().__init__(Bullet.asset, position)
        t = 1.00
        V = 4
        self.vx=V*math.cos(t)
        self.vy=-V*math.sin(t)
        self.scale=.5
        self.vr=0
        self.fxcenter = .5
        self.fycenter = .5
        self.away = False
        self.explode = True
        self.explodeframe = 1

    def step(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += .05
        self.rotation += .1
        colliding = self.collidingWithSprites(Tank)
        if colliding and self.away == True:
            self.setImage(self.explodeframe)
            self.explodeframe += 1
            if self.explodeframe == 20:
                self.explodeframe = 1
        elif not colliding and self.away == False:
            self.away = True
<|endoftext|>