formations/python/python3/fr/_build/html/prompt.html

271 lines
18 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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" lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Interactions avec lutilisateur &#8212; Documentation Formation Python 1</title>
<link rel="stylesheet" href="_static/classic.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',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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>
<script type="text/javascript" src="_static/translations.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Recherche" href="search.html" />
<link rel="next" title="Programmation python courante" href="specialmethods.html" />
<link rel="prev" title="Définir et manipuler des classes" href="classes.html" />
</head>
<body>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<table><tr><td>
<img src="_static/sphinx.png" alt="logo" />
</td><td>
<h1>&nbsp; &nbsp; Programmation python</h1>
</td></tr></table>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="Index général"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Index des modules Python"
>modules</a> |</li>
<li class="right" >
<a href="specialmethods.html" title="Programmation python courante"
accesskey="N">suivant</a> |</li>
<li class="right" >
<a href="classes.html" title="Définir et manipuler des classes"
accesskey="P">précédent</a> |</li>
<a href="index.html">Programmation python </a> &raquo;
</ul>
</div>
<p>&nbsp;</p>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="interactions-avec-l-utilisateur">
<h1>Interactions avec lutilisateur<a class="headerlink" href="#interactions-avec-l-utilisateur" title="Lien permanent vers ce titre"></a></h1>
<div class="section" id="les-prompts">
<h2>les prompts<a class="headerlink" href="#les-prompts" title="Lien permanent vers ce titre"></a></h2>
<p><cite>raw_input</cite> ou <cite>input</cite></p>
<p>(raw_input renvoie une string, input essayes dévaluer, soyez prudent…)</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">subprocess</span> <span class="k">import</span> <span class="n">call</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">filename</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">&quot;quel fichier voulez-vous afficher ?</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">call</span><span class="p">(</span><span class="s2">&quot;cat &quot;</span> <span class="o">+</span> <span class="n">filename</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="le-module-cmd-et-les-interpreteurs">
<span id="cmdlabel"></span><h2>le module <a class="reference internal" href="#module-cmd" title="cmd: interpréteur ligne de commande"><code class="xref py py-mod docutils literal"><span class="pre">cmd</span></code></a> et les interpréteurs<a class="headerlink" href="#le-module-cmd-et-les-interpreteurs" title="Lien permanent vers ce titre"></a></h2>
<p>le monde des interpréteur ligne de commande…</p>
<p>Peu après lâge de bronze vint le temps de linterpréteur ligne de commande,
cest-à-dire quelque chose de plus spécifique que <strong>lapplication ligne de commande</strong>,
ou que lutilitaire ligne de commande.</p>
<p>Un interpréteur ligne de commande est un programme qui :</p>
<ul class="simple">
<li>est forcément plein texte</li>
<li>vous donne un prompt</li>
<li>prends toutes ses entrées dun coup</li>
<li>produit une sortie (typiquement des lignes de texte)</li>
<li>vous redonne un prompt</li>
</ul>
<p>Le shell unix est un bon exemple dinterpréteur ligne de commande.</p>
<p>Un utilitaire ligne de commande est un programme unix-like qui prend toutes
les entrées dun coup, et qui vous renvoie une sortie dun coup.</p>
<p>le module <a class="reference internal" href="#module-cmd" title="cmd: interpréteur ligne de commande"><code class="xref py py-mod docutils literal"><span class="pre">cmd</span></code></a> : exemple dutilisation</p>
<span class="target" id="module-cmd"></span><div class="highlight-default"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf8 -*-</span>
<span class="sd">&quot;&quot;&quot;Command line interpreter</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">cmd</span>
<span class="c1"># ____________________________________________________________</span>
<span class="c1"># this Cli is a model of a basic use of a simple cmd</span>
<span class="k">class</span> <span class="nc">Cli</span><span class="p">(</span><span class="n">cmd</span><span class="o">.</span><span class="n">Cmd</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">cmd</span><span class="o">.</span><span class="n">Cmd</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">doc_header</span> <span class="o">=</span> <span class="s2">&quot;Documented commands (type help &lt;command&gt;):&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">undoc_header</span> <span class="o">=</span> <span class="s2">&quot;Undocumented commands&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">prompt</span> <span class="o">=</span> <span class="s2">&quot;#Prompt&gt; &quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">intro</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;cli (command line interpreter)</span>
<span class="s2">(type help or ? for commands list)&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">ruler</span> <span class="o">=</span> <span class="s2">&quot;-&quot;</span>
<span class="k">def</span> <span class="nf">emptyline</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span> <span class="s2">&quot;Type &#39;exit&#39; to finish withe the session or type ? for help.&quot;</span>
<span class="k">def</span> <span class="nf">default</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="nb">print</span> <span class="s2">&quot;unknown command prefix&quot;</span>
<span class="nb">print</span> <span class="s2">&quot;*** unknown syntax : </span><span class="si">%s</span><span class="s2"> (type &#39;help&#39; for help for a list of valid commands)&quot;</span><span class="o">%</span><span class="n">line</span>
<span class="bp">self</span><span class="o">.</span><span class="n">emptyline</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">do_exit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Exits from the console&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="k">def</span> <span class="nf">do_quit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="k">def</span> <span class="nf">do_EOF</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Exit on system end of file character&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="c1"># ____________________________________________________________</span>
<span class="c1"># commands pre and post actions</span>
<span class="c1"># def precmd(self, line):</span>
<span class="c1"># return line</span>
<span class="c1"># def postcmd(self, stop, line):</span>
<span class="c1"># # if there is a problem, just return True : it stops everythings</span>
<span class="c1"># stop = True</span>
<span class="c1"># return stop # quit if stop == True</span>
<span class="c1"># ____________________________________________________________</span>
<span class="c1"># program pre and post actions</span>
<span class="c1"># def preloop(self):</span>
<span class="c1"># # action for the beginning of the program</span>
<span class="c1"># pass</span>
<span class="c1"># def postloop(self):</span>
<span class="c1"># # action for the end of the program</span>
<span class="c1"># print &quot;exit cli&quot;</span>
<span class="c1"># ____________________________________________________________</span>
<span class="k">class</span> <span class="nc">HelloCli</span><span class="p">(</span><span class="n">Cli</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">input_hello</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="k">return</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;,&quot;</span><span class="p">,</span> <span class="s2">&quot; and &quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">output_hello</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">):</span>
<span class="nb">print</span> <span class="n">result</span>
<span class="k">def</span> <span class="nf">do_hello</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">output_hello</span><span class="p">(</span><span class="s2">&quot;hello, &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_hello</span><span class="p">(</span><span class="n">line</span><span class="p">))</span>
<span class="c1">#return False # if you want to stay into the cli</span>
<span class="k">return</span> <span class="kc">True</span> <span class="c1"># if you want to exit</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
<span class="n">prompt</span> <span class="o">=</span> <span class="n">HelloCli</span><span class="p">()</span>
<span class="n">prompt</span><span class="o">.</span><span class="n">cmdloop</span><span class="p">(</span><span class="s2">&quot;intro, modifies Cmd.intro&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p><a class="reference download internal" href="_downloads/cli.py" download=""><code class="xref download docutils literal"><span class="pre">telecharger</span> <span class="pre">cmd</span></code></a></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">cli</span> <span class="k">import</span> <span class="n">Cli</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prompt</span> <span class="o">=</span> <span class="n">Cli</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prompt</span><span class="o">.</span><span class="n">cmdloop</span><span class="p">()</span>
<span class="go">cli (command line interpreter)</span>
<span class="go">(type help or ? for commands list)</span>
<span class="go">#Prompt&gt; ?</span>
<span class="go">Documented commands (type help &lt;command&gt;):</span>
<span class="go">==========================================</span>
<span class="go">EOF exit</span>
<span class="go">Undocumented commands:</span>
<span class="go">======================</span>
<span class="go">cmd help quit</span>
<span class="go">#Prompt&gt;</span>
</pre></div>
</div>
<p>pour ajouter une commande, utiliser simplement lhéritage:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">cli</span> <span class="k">import</span> <span class="n">Cli</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Prompt</span><span class="p">(</span><span class="n">Cli</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">do_hello</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="s2">&quot;hello </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">line</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prompt</span> <span class="o">=</span> <span class="n">Prompt</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prompt</span><span class="o">.</span><span class="n">cmdloop</span><span class="p">()</span>
<span class="go">cli (command line interpreter)</span>
<span class="go">(type help or ? for commands list)</span>
<span class="go">#Prompt&gt; ?</span>
<span class="go">Documented commands (type help &lt;command&gt;):</span>
<span class="go">==========================================</span>
<span class="go">EOF exit</span>
<span class="go">Undocumented commands:</span>
<span class="go">======================</span>
<span class="go">cmd hello help quit</span>
<span class="go">#Prompt&gt; hello world</span>
</pre></div>
</div>
<div class="admonition-todo admonition" id="index-0">
<p class="first admonition-title">À faire</p>
<p class="last">faire un petit projet dinterpréteur ligne de commande du jeu C+/C-</p>
</div>
</div>
<div class="section" id="lire-et-ecrire-dans-un-fichier">
<h2>lire et écrire dans un fichier<a class="headerlink" href="#lire-et-ecrire-dans-un-fichier" title="Lien permanent vers ce titre"></a></h2>
<p>les <strong>handle de fichier</strong> (file handles)</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fh</span> <span class="o">=</span> <span class="n">file</span><span class="p">(</span><span class="s1">&#39;test&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fh</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;hello world&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fh</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">content</span> <span class="o">=</span> <span class="n">file</span><span class="p">(</span><span class="s1">&#39;test&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">content</span>
<span class="go">&#39;hello world&#39;</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<a href="search.html"> Recherche</a> | &nbsp;
<!-- a href="genindex.html"> Genindex</a-->
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="Index général"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Index des modules Python"
>modules</a> |</li>
<li class="right" >
<a href="specialmethods.html" title="Programmation python courante"
>suivant</a> |</li>
<li class="right" >
<a href="classes.html" title="Définir et manipuler des classes"
>précédent</a> |</li>
<a href="index.html">Programmation python </a> &raquo;
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2015, cadoles (www.cadoles.com).
Créé avec <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.7.
</div>
</body>
</html>