예제 #1
0
파일: template.py 프로젝트: oberix/star
    def _load_bags(self, path, **kwargs):
        ''' Load bag files and generates TeX code from them.

        @ param path: directory where .pickle files lives.
        @ param template: a TexSreTemplate instance to extract placeholders
            from.
        @ return: dictionary of output TeX code; the dictionary is indexed by
            bag.COD

        '''
        ret = dict()
        # Make a placeholders list to fetch only needed files and to let access
        # to single Pickle attributes.
        ph_list = [ph[2] for ph in self.pattern.findall(self.template)]
        try:
            ph_list.remove(str()) # Remove placeholder command definition.
        except ValueError:
            pass
        # In case of multiple instance of a placeholder, evaluate only once.
        ph_list = list(set(ph_list))
        self._logger.info("Reading pickles...")
        bags = dict() # Pickle file's cache (never load twice the same file!)
        for ph in ph_list:
            ph_parts = ph.split('.')
            base = ph_parts[0]
            if not bags.get(base, False):
                # Load and add to cache
                self._logger.debug('Now loading: %s', os.path.join(
                        path, '.'.join([base, 'pickle'])))
                try:
                    bags[base] = Bag.load(os.path.join(path, '.'.join(
                                [base, 'pickle'])))
                except IOError, err:
                    self._logger.warning('%s; skipping...', err)
                    continue
            # Generate string to substitute to the placeholder
            if bags[base].stype == 'tab':
                ret[base] = self._make_table(bags[base], **kwargs).out()
            elif bags[base].stype == 'ltab':
                ret[base] = self._make_table(bags[base], tab_type='ltab', **kwargs).out()
            elif bags[base].stype == 'bodytab':
                ret[base] = self._make_table(bags[base], tab_type='bodytab', **kwargs).out()
            elif bags[base].stype == 'graph':
                ret[base], fd = self._make_graph(bags[base], **kwargs).out()
                self._fds.append(fd)
            elif bags[base].stype == 'desc':
                ret[base] = self._make_des(bags[base], **kwargs).out()
            else: # TODO: handle other types
                self._logger.debug('bags = %s', bags)
                self._logger.warning(
                    "Unhandled bag stype '%s' found in %s, skipping...",
                    bags[base].stype, base)
                continue
            if len(ph_parts) > 1 and \
                    hasattr(bags[base], '.'.join(ph_parts[1:])):
                # extract attribute
                # TODO: apply translation
                ret[ph] = bags[base].__getattribute__('.'.join(ph_parts[1:]))
예제 #2
0
파일: test_table.py 프로젝트: oberix/star
                      'vsep': 'l', 
                      'headers' : [u'Milioni di €', 'AAAAAAAAAAAAAAA']},
                'B': {'order': 1, 
                      'align': 'c', 
                      'vsep': 'b', 
                      'headers' : [u"""Milioni di €""", 'BBBBBBBBBBBBBBBBB']},
                'C': {'order': 2, 
                      'align': 'r', 
                      'vsep': 'r', 
                      'headers' : ['@v1', 'CCCCCCCCCCCCCCCCCCCC']},
            }
          }
    }

    df = pandas.DataFrame({
        'A': np.random.rand(10) * 100000,
        'B': np.random.rand(10) * 100000,
        'C': np.random.rand(10) * 100000,
    })

    base = '/tmp/test/'
    if not os.path.isdir(base):
        os.makedirs(base)

    mybag = Bag(df, md=md, stype='tab')
    mybag.save(os.path.join(base, 'mybag.pickle'))

    assert test_latex() == 0
    
    assert test_html(mybag) == 0
예제 #3
0
파일: test_graph.py 프로젝트: oberix/star
                 'vars': {
                 'a': {'type': 'lax',
                       'ax': 'sx',
                       'label': 'AAA'},
                 'b': {'type': 'scatter',
                       'ax': 'sx',
                       'label': 'BBB',
                       'color': 'b'},
             }}}

    df_sc = pandas.DataFrame({
            'a': [59.9, 46.2, 0],
            'b': [13.6, 7.1, 2],
            })

    plot = Bag(df, md=lm_plot, stype='graph')
    bar = Bag(df, md=lm_bar, stype='graph')
    sidebarh = Bag(df, md=lm_sidebarh, stype='graph')
    sidebar = Bag(df, md=lm_sidebar, stype='graph')
    barh = Bag(df, md=lm_barh, stype='graph')
    scat = Bag(df_sc, md=lm_sc, stype='graph')

    base = '/tmp/test/'
    if not os.path.isdir(base):
        os.makedirs(base)

    plot.save(os.path.join(base, "plot.pickle"))
    bar.save(os.path.join(base, "bar.pickle"))
    barh.save(os.path.join(base, "barh.pickle"))
    sidebar.save(os.path.join(base, "sidebar.pickle"))
    sidebarh.save(os.path.join(base, "sidebarh.pickle"))