예제 #1
0
    def add_color_choosers(self):
        """ Adds color choosers for relevant properties starting
        with a prefix such as axes.
        """
        logger = log_api.env_logger()
        logger.debug('self.keys {}'.format(self.keys))
        color_keys = collect.filter_list(lambda x: x.endswith('color'),
                                         self.keys)
        logger.debug('Color keys {}'.format(color_keys))

        for key in color_keys:
            self.widgets[key] = self.factory.color_chooser(key)
예제 #2
0
    def render(self, equation):
        """ Renders an equation.

        :param equation: A string containing the equation.
        """
        number = self.number_equation()
        self.numbers.append(self.curr)
        logger = log_api.env_logger()

        if self.context:
            logger.debug(self.numbers)
            self.context.update_latex(self.numbers)

        display(Math(r'%s' % (number + equation)))
        self.curr += 1
예제 #3
0
def where_first(session, column, search_val):
    ''' Gets the first row as in the following SQL \
        `SELECT * from atable where atable = 'search_val'`

    :param session: A `SQLAlchemy` session.
    :param column: A table column as attribute in a Python class.
    :param search_val: Value to search for.

    :returns: The first row of the query.
    '''
    logger = log_api.env_logger()
    result = session.query(entity_from_column(column)).\
        filter(column == search_val).first()
    logger.debug('search_val=%s result=%s', search_val, result)

    return result
예제 #4
0
    def grid_page(self):
        """ Creates a tab page for the `matplotlib.rcParams`
        keys which start with **grid.**"""
        logger = log_api.env_logger()
        logger.debug('Created grid page')
        linewidth = create_linewidth_slider('grid.linewidth')

        def update_linewidth(name, value):
            self.process(linewidth.description, value)

        linewidth.on_trait_change(update_linewidth, 'value')

        page = PageBuilder('grid.', self.factory)
        page.add(linewidth)

        return page.build()
예제 #5
0
파일: collect.py 프로젝트: ecustzhy/test
def filter_dict_keys(func, adict):
    """ Filters the keys of a dictionary.

    :param func: A function used to filter the keys.
    :param adict: A dictionary.

    :returns: A list of keys selected by the filter.

    >>> from dautil import collect
    >>> adict = {'a.latex': 1, 'b.latex': 2, 'c': 3}
    >>> collect.filter_dict_keys(lambda x: x.endswith('.latex'), adict)
    ['a.latex', 'b.latex']
    """
    logger = log_api.env_logger()
    logger.debug('adict {}'.format(adict))

    return [k for k in adict.keys() if func(k)]
예제 #6
0
    def read_rc(self):
        """ Reads the current configuration
        settings related to `matplotlib.rcParams`,
        which are used by `RcWidget`.

        :returns: The current configuration settings or \
        an empty dict.
        """
        config = conf.read_rc()

        if config:
            config = config.get(self.fname, {})

        logger = log_api.env_logger()
        logger.debug('config %s', config)

        return config
예제 #7
0
    def process(self, param, value):
        """ Processes changes to the GUI and updates `matplotlib.rcParams`.

        :param param: A key in the `matplotlib.rcParams` dictionary.
        :param value: A value in the `matplotlib.rcParams` dictionary.
        """
        logger = log_api.env_logger()
        logger.debug('name={0}, value={1}'.format(param, value))
        self.params_text.value = ''
        mpl.rcParams[param] = value
        updates = collect.dict_updates(self.old, mpl.rcParams)

        if self.context:
            self.context.update_rc(updates)

        self.updates_text.value = (
            '<p>mpl.RcParams updates {}</p>'.format(updates))
예제 #8
0
    def color_chooser(self, property):
        """ Creates a box with widgets related to choosing a color.

        :param property: A color related key in matplotlib.rcParams.

        :returns: A box with widgets.
        """
        cc = ColorConverter()
        rgb = cc.to_rgb(mpl.rcParams[property])
        logger = log_api.env_logger()
        logger.debug('{0} {1}'.format(property, rgb))

        r = widgets.FloatSlider(min=0, max=1, value=rgb[0], description='Red')
        r.border_color = 'red'

        g = widgets.FloatSlider(min=0,
                                max=1,
                                value=rgb[1],
                                description='Green')
        g.border_color = 'green'

        b = widgets.FloatSlider(min=0, max=1, value=rgb[2], description='Blue')
        b.border_color = 'blue'

        h = widgets.widget_string.HTML(property)
        # TODO put this in a func
        hex = rgb2hex((rgb[0], rgb[1], rgb[2]))
        h.value = '<p style="background-color: {0};">{0}</p>'.format(hex)

        def update(name, value):
            hex = rgb2hex((r.value, g.value, b.value))
            h.value = '<p style="background-color: {0};">{0}</p>'.format(hex)
            self.rc_widget.process(property, hex)

        r.on_trait_change(update, 'value')
        g.on_trait_change(update, 'value')
        b.on_trait_change(update, 'value')

        box = widgets.VBox(children=(r, g, b, h))
        box.border_style = 'dotted'
        box.description = property

        return box
예제 #9
0
    def plot(self, x, y=None, *args, **kwargs):
        """ A facade for the matplotlib `plot()` method.

        :param x: Array of 'x' values for the plot.
        :param y: Array of 'y' values for the plot.
        """
        logger = log_api.env_logger()

        if y is None:
            y = x
            x = list(range(len(x)))

        logger.debug('len(x) %s', len(x))
        logger.debug('len(y) %s', len(y))
        self.ax.plot(x,
                     y,
                     self.cycler.style(),
                     lw=self.cycler.lw(),
                     *args,
                     **kwargs)
예제 #10
0
파일: collect.py 프로젝트: ecustzhy/test
def flatten(iterable):
    """ Flattens an iterable, where strings and dicts
    are not considered iterable.

    :param iterable: The iterable to flatten.

    :returns: The iterable flattened as a flat list.

    >>> from dautil import collect
    >>> collect.flatten([[1, 2]])
    [1, 2]
    """
    logger = log_api.env_logger()
    logger.debug('Iterable {}'.format(iterable))
    assert isiterable(iterable), 'Not iterable {}'.format(iterable)
    flat = iterable

    if isiterable(iterable[0]):
        flat = [i for i in chain.from_iterable(iterable)]

    return flat
예제 #11
0
def map_to_id(session, column):
    ''' Assuming that a table has a primary key column 'id'. \
        This function creates dictionary with column value as key \
        and id as value.

    :param session: A `SQLAlchemy` session.
    :param column: A table column as attribute in a Python class.

    :returns: A dictionary with column values mapped to id.
    '''
    id_map = {}
    logger = log_api.env_logger()
    table = column.parent.entity
    query = session.query(table.id, column)
    logger.debug('Query %s', query)

    for id, val in query.all():
        id_map[val] = id

    logger.debug('Id map %s', id_map)

    return id_map