Code Search for Developers
 
 
  

yaptu.py from matplotlib at Krugle


Show yaptu.py syntax highlighted

import sys

# utility stuff to avoid tests in the mainline code
class _nevermatch:
    "Polymorphic with a regex that never matches"
    def match(self, line):
        return None
_never = _nevermatch()     # one reusable instance of it suffices
def identity(string, why):
    "A do-nothing-special-to-the-input, just-return-it function"
    return string
def nohandle(string):
    "A do-nothing handler that just re-raises the exception"
    raise

# and now the real thing
class copier:
    "Smart-copier (YAPTU) class"
    def copyblock(self, i=0, last=None):
        "Main copy method: process lines [i,last) of block"
        def repl(match, self=self):
            "return the eval of a found expression, for replacement"
            # uncomment for debug: print '!!! replacing',match.group(1)
            expr = self.preproc(match.group(1), 'eval')
            try: return str(eval(expr, self.globals, self.locals))
            except: return str(self.handle(expr))
        block = self.locals['_bl']
        if last is None: last = len(block)
        while i<last:
            line = block[i]
            match = self.restat.match(line)
            if match:   # a statement starts "here" (at line block[i])
                # i is the last line to _not_ process
                stat = match.string[match.end(0):].strip()
                j=i+1   # look for 'finish' from here onwards
                nest=1  # count nesting levels of statements
                while j<last:
                    line = block[j]
                    # first look for nested statements or 'finish' lines
                    if self.restend.match(line):    # found a statement-end
                        nest = nest - 1     # update (decrease) nesting
                        if nest==0: break   # j is first line to _not_ process
                    elif self.restat.match(line):   # found a nested statement
                        nest = nest + 1     # update (increase) nesting
                    elif nest==1:   # look for continuation only at this nesting
                        match = self.recont.match(line)
                        if match:                   # found a contin.-statement
                            nestat = match.string[match.end(0):].strip()
                            stat = '%s _cb(%s,%s)\n%s' % (stat,i+1,j,nestat)
                            i=j     # again, i is the last line to _not_ process
                    j=j+1
                stat = self.preproc(stat, 'exec')
                stat = '%s _cb(%s,%s)' % (stat,i+1,j)
                # for debugging, uncomment...: print "-> Executing: {"+stat+"}"
                exec stat in self.globals,self.locals
                i=j+1
            else:       # normal line, just copy with substitution
                self.ouf.write(self.regex.sub(repl,line))
                i=i+1
    def __init__(self, regex=_never, dict={},
            restat=_never, restend=_never, recont=_never, 
            preproc=identity, handle=nohandle, ouf=sys.stdout):
        "Initialize self's attributes"
        self.regex   = regex
        self.globals = dict
        self.locals  = { '_cb':self.copyblock }
        self.restat  = restat
        self.restend = restend
        self.recont  = recont
        self.preproc = preproc
        self.handle  = handle
        self.ouf     = ouf
    def copy(self, block=None, inf=sys.stdin):
        "Entry point: copy-with-processing a file, or a block of lines"
        if block is None: block = inf.readlines()
        self.locals['_bl'] = block
        self.copyblock()





See more files for this project here

matplotlib

Matplotlib is a pure python plotting library with the goal of making\r\npublication quality plots using a syntax familiar to matlab users. \r\nThe library uses Numeric for handling large\r\ndata sets and supports a variety of output backends

Project homepage: http://sourceforge.net/projects/matplotlib
Programming language(s): C,C++,Python
License: other

  examples/
    units/
    widgets/
    __init__.py
    accented_text.py
    agg_oo.py
    agg_resize.py
    agg_test.py
    alignment_test.py
    anim.py
    anim_tk.py
    anscombe.py
    arctest.py
    axes_demo.py
    axes_props.py
    axhspan_demo.py
    backend_driver.py
    bar_stacked.py
    barchart_demo.py
    barh_demo.py
    boxplot_demo.py
    break.py
    color_by_yvalue.py
    color_demo.py
    colours.py
    contour_demo.py
    contour_image.py
    contourf_demo.py
    coords_demo.py
    coords_report.py
    csd_demo.py
    cursor_demo.py
    custom_ticker1.py
    customize_rc.py
    dash_control.py
    dashpointlabel.py
    dashtick.py
    data_helper.py
    date_demo1.py
    date_demo2.py
    date_demo_convert.py
    date_demo_rrule.py
    dynamic_demo.py
    dynamic_demo_wx.py
    dynamic_image_gtkagg.py
    dynamic_image_wxagg.py
    dynamic_image_wxagg2.py
    embedding_in_gtk.py
    embedding_in_gtk2.py
    embedding_in_gtk3.py
    embedding_in_qt.py
    embedding_in_tk.py
    embedding_in_tk2.py
    embedding_in_wx.py
    embedding_in_wx2.py
    embedding_in_wx3.py
    embedding_in_wx4.py
    errorbar_demo.py
    figimage_demo.py
    figlegend_demo.py
    figtext.py
    fill_between.py
    fill_between_posneg.py
    fill_demo.py
    fill_demo2.py
    fill_spiral.py
    finance_demo.py
    font_indexing.py
    font_table_ttf.py
    fonts_demo.py
    fonts_demo_kw.py
    ftface_props.py
    ganged_plots.py
    gdtest.py
    glyph_to_path.py
    gtk_spreadsheet.py
    histogram_demo.py
    histogram_demo_canvasagg.py
    image_demo.py
    image_demo2.py
    image_demo3.py
    image_demo_na.py
    image_interp.py
  gd/
  screenshots/
  tut/
  API_CHANGES
  CHANGELOG
  INSTALL
  NUMARRAY_ISSUES
  README
  __init__.py
  api.pdf
  backends.html.template
  classdocs.html.template
  convert.py
  credits.html.template
  exclude.txt
  faq.html.template
  fonts.html.template
  footer.html.template
  goals.html.template
  goals.txt
  header.html.template
  hthelpers.py
  index.html.template
  installing.html.template
  interactive.html.template
  leftwich_tut.txt
  license.fmt
  license.html.template
  make.py
  matlab_to_pylab.py
  matplotlib.afm.html.template
  matplotlib.artist.html.template
  matplotlib.axes.html.template
  matplotlib.axis.html.template
  matplotlib.backend_bases.html.template
  matplotlib.backends.backend_agg.html.template
  matplotlib.backends.backend_cairo.html.template
  matplotlib.backends.backend_fltkagg.html.template
  matplotlib.backends.backend_gd.html.template
  matplotlib.backends.backend_gtk.html.template
  matplotlib.backends.backend_gtkagg.html.template
  matplotlib.backends.backend_gtkcairo.html.template
  matplotlib.backends.backend_gtkgd.html.template
  matplotlib.backends.backend_paint.html.template
  matplotlib.backends.backend_ps.html.template
  matplotlib.backends.backend_qt.html.template
  matplotlib.backends.backend_qtagg.html.template
  matplotlib.backends.backend_svg.html.template
  matplotlib.backends.backend_template.html.template
  matplotlib.backends.backend_tkagg.html.template
  matplotlib.backends.backend_wx.html.template
  matplotlib.backends.backend_wxagg.html.template
  matplotlib.cbook.html.template
  matplotlib.cm.html.template
  matplotlib.collections.html.template
  matplotlib.colors.html.template
  matplotlib.dates.html.template
  matplotlib.figure.html.template
  matplotlib.finance.html.template
  matplotlib.font_manager.html.template
  matplotlib.ft2font.html.template
  matplotlib.image.html.template
  matplotlib.legend.html.template
  matplotlib.lines.html.template
  matplotlib.mathtext.html
  matplotlib.mathtext.html.template
  matplotlib.mlab.html.template
  matplotlib.numerix.html.template
  matplotlib.patches.html.template
  matplotlib.pylab.html.template
  matplotlib.table.html.template
  matplotlib.texmanager.html.template
  matplotlib.text.html.template
  matplotlib.ticker.html.template
  matplotlib.toolkits.basemap.basemap.html.template
  matplotlib.toolkits.basemap.html.template
  matplotlib.transforms.html.template
  matplotlib.widgets.html.template
  matplotlibrc
  process_docs.py
  pylab_commands.html.template
  screenshots.html.template
  set_begone.py
  toolkits.html.template
  tutorial.html.template
  whats_new.html.template
  whats_new_text.py
  win32_static.tar.gz
  yaptu.py