Current File : //usr/share/doc/python-py-1.4.32/html/misc.html |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Miscellaneous features of the py lib — py 1.4.32 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '1.4.32',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="py 1.4.32 documentation" href="index.html" />
<link rel="next" title="1.4.32" href="changelog.html" />
<link rel="prev" title="py.xml: simple pythonic xml/html file generation" href="xml.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="changelog.html" title="1.4.32"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="xml.html" title="py.xml: simple pythonic xml/html file generation"
accesskey="P">previous</a> |</li>
<li><a href="index.html">py 1.4.32 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="miscellaneous-features-of-the-py-lib">
<h1>Miscellaneous features of the py lib<a class="headerlink" href="#miscellaneous-features-of-the-py-lib" title="Permalink to this headline">¶</a></h1>
<div class="section" id="mapping-the-standard-python-library-into-py">
<h2>Mapping the standard python library into py<a class="headerlink" href="#mapping-the-standard-python-library-into-py" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">py.std</span></tt> object allows lazy access to
standard library modules. For example, to get to the print-exception
functionality of the standard library you can write:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">py</span><span class="o">.</span><span class="n">std</span><span class="o">.</span><span class="n">traceback</span><span class="o">.</span><span class="n">print_exc</span><span class="p">()</span>
</pre></div>
</div>
<p>without having to do anything else than the usual <tt class="docutils literal"><span class="pre">import</span> <span class="pre">py</span></tt>
at the beginning. You can access any other top-level standard
library module this way. This means that you will only trigger
imports of modules that are actually needed. Note that no attempt
is made to import submodules.</p>
</div>
<div class="section" id="support-for-interaction-with-system-utilities-binaries">
<h2>Support for interaction with system utilities/binaries<a class="headerlink" href="#support-for-interaction-with-system-utilities-binaries" title="Permalink to this headline">¶</a></h2>
<p>Currently, the py lib offers two ways to interact with
system executables. <tt class="docutils literal"><span class="pre">py.process.cmdexec()</span></tt> invokes
the shell in order to execute a string. The other
one, <tt class="docutils literal"><span class="pre">py.path.local</span></tt>‘s ‘sysexec()’ method lets you
directly execute a binary.</p>
<p>Both approaches will raise an exception in case of a return-
code other than 0 and otherwise return the stdout-output
of the child process.</p>
<div class="section" id="the-shell-based-approach">
<h3>The shell based approach<a class="headerlink" href="#the-shell-based-approach" title="Permalink to this headline">¶</a></h3>
<p>You can execute a command via your system shell
by doing something like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">out</span> <span class="o">=</span> <span class="n">py</span><span class="o">.</span><span class="n">process</span><span class="o">.</span><span class="n">cmdexec</span><span class="p">(</span><span class="s">'ls -v'</span><span class="p">)</span>
</pre></div>
</div>
<p>However, the <tt class="docutils literal"><span class="pre">cmdexec</span></tt> approach has a few shortcomings:</p>
<ul class="simple">
<li>it relies on the underlying system shell</li>
<li>it neccessitates shell-escaping for expressing arguments</li>
<li>it does not easily allow to “fix” the binary you want to run.</li>
<li>it only allows to execute executables from the local
filesystem</li>
</ul>
</div>
<div class="section" id="local-paths-have-sysexec">
<span id="sysexec"></span><h3>local paths have <tt class="docutils literal"><span class="pre">sysexec</span></tt><a class="headerlink" href="#local-paths-have-sysexec" title="Permalink to this headline">¶</a></h3>
<p>In order to synchronously execute an executable file you
can use <tt class="docutils literal"><span class="pre">sysexec</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">binsvn</span><span class="o">.</span><span class="n">sysexec</span><span class="p">(</span><span class="s">'ls'</span><span class="p">,</span> <span class="s">'http://codespeak.net/svn'</span><span class="p">)</span>
</pre></div>
</div>
<p>where <tt class="docutils literal"><span class="pre">binsvn</span></tt> is a path that points to the <tt class="docutils literal"><span class="pre">svn</span></tt> commandline
binary. Note that this function does not offer any shell-escaping
so you have to pass in already separated arguments.</p>
</div>
<div class="section" id="finding-an-executable-local-path">
<h3>finding an executable local path<a class="headerlink" href="#finding-an-executable-local-path" title="Permalink to this headline">¶</a></h3>
<p>Finding an executable is quite different on multiple platforms.
Currently, the <tt class="docutils literal"><span class="pre">PATH</span></tt> environment variable based search on
unix platforms is supported:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">py</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">local</span><span class="o">.</span><span class="n">sysfind</span><span class="p">(</span><span class="s">'svn'</span><span class="p">)</span>
</pre></div>
</div>
<p>which returns the first path whose <tt class="docutils literal"><span class="pre">basename</span></tt> matches <tt class="docutils literal"><span class="pre">svn</span></tt>.
In principle, <cite>sysfind</cite> deploys platform specific algorithms
to perform the search. On Windows, for example, it may look
at the registry (XXX).</p>
<p>To make the story complete, we allow to pass in a second <tt class="docutils literal"><span class="pre">checker</span></tt>
argument that is called for each found executable. For example, if
you have multiple binaries available you may want to select the
right version:</p>
<div class="highlight-python"><pre>def mysvn(p):
""" check that the given svn binary has version 1.1. """
line = p.execute('--version'').readlines()[0]
if line.find('version 1.1'):
return p
binsvn = py.path.local.sysfind('svn', checker=mysvn)</pre>
</div>
</div>
</div>
<div class="section" id="cross-python-version-compatibility-helpers">
<h2>Cross-Python Version compatibility helpers<a class="headerlink" href="#cross-python-version-compatibility-helpers" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">py.builtin</span></tt> namespace provides a number of helpers that help to write python code compatible across Python interpreters, mainly Python2 and Python3. Type <tt class="docutils literal"><span class="pre">help(py.builtin)</span></tt> on a Python prompt for a the selection of builtins.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Miscellaneous features of the py lib</a><ul>
<li><a class="reference internal" href="#mapping-the-standard-python-library-into-py">Mapping the standard python library into py</a></li>
<li><a class="reference internal" href="#support-for-interaction-with-system-utilities-binaries">Support for interaction with system utilities/binaries</a><ul>
<li><a class="reference internal" href="#the-shell-based-approach">The shell based approach</a></li>
<li><a class="reference internal" href="#local-paths-have-sysexec">local paths have <tt class="docutils literal"><span class="pre">sysexec</span></tt></a></li>
<li><a class="reference internal" href="#finding-an-executable-local-path">finding an executable local path</a></li>
</ul>
</li>
<li><a class="reference internal" href="#cross-python-version-compatibility-helpers">Cross-Python Version compatibility helpers</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="xml.html"
title="previous chapter">py.xml: simple pythonic xml/html file generation</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="changelog.html"
title="next chapter">1.4.32</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/misc.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="changelog.html" title="1.4.32"
>next</a> |</li>
<li class="right" >
<a href="xml.html" title="py.xml: simple pythonic xml/html file generation"
>previous</a> |</li>
<li><a href="index.html">py 1.4.32 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2010, holger krekel et. al..
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
</div>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7597274-14']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>