271 lines
18 KiB
HTML
271 lines
18 KiB
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" lang="fr">
|
|||
|
<head>
|
|||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|||
|
<title>Interactions avec l’utilisateur — 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> 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> »
|
|||
|
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
|
|||
|
<p> </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 l’utilisateur<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">>>> </span><span class="kn">from</span> <span class="nn">subprocess</span> <span class="k">import</span> <span class="n">call</span>
|
|||
|
<span class="gp">>>> </span><span class="n">filename</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">"quel fichier voulez-vous afficher ?</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">call</span><span class="p">(</span><span class="s2">"cat "</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 l’interpréteur ligne de commande,
|
|||
|
c’est-à-dire quelque chose de plus spécifique que <strong>l’application ligne de commande</strong>,
|
|||
|
ou que l’utilitaire 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 d’un 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 d’interpréteur ligne de commande.</p>
|
|||
|
<p>Un utilitaire ligne de commande est un programme unix-like qui prend toutes
|
|||
|
les entrées d’un coup, et qui vous renvoie une sortie d’un 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 d’utilisation</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">"""Command line interpreter</span>
|
|||
|
<span class="sd">"""</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">"Documented commands (type help <command>):"</span>
|
|||
|
<span class="bp">self</span><span class="o">.</span><span class="n">undoc_header</span> <span class="o">=</span> <span class="s2">"Undocumented commands"</span>
|
|||
|
<span class="bp">self</span><span class="o">.</span><span class="n">prompt</span> <span class="o">=</span> <span class="s2">"#Prompt> "</span>
|
|||
|
<span class="bp">self</span><span class="o">.</span><span class="n">intro</span> <span class="o">=</span> <span class="s2">"""cli (command line interpreter)</span>
|
|||
|
<span class="s2">(type help or ? for commands list)"""</span>
|
|||
|
<span class="bp">self</span><span class="o">.</span><span class="n">ruler</span> <span class="o">=</span> <span class="s2">"-"</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">"Type 'exit' to finish withe the session or type ? for help."</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">"unknown command prefix"</span>
|
|||
|
<span class="nb">print</span> <span class="s2">"*** unknown syntax : </span><span class="si">%s</span><span class="s2"> (type 'help' for help for a list of valid commands)"</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">"""Exits from the console"""</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">"""Exit on system end of file character"""</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 "exit cli"</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">","</span><span class="p">,</span> <span class="s2">" and "</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">"hello, "</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">'__main__'</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">"intro, modifies Cmd.intro"</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">>>> </span><span class="kn">from</span> <span class="nn">cli</span> <span class="k">import</span> <span class="n">Cli</span>
|
|||
|
<span class="gp">>>> </span><span class="n">prompt</span> <span class="o">=</span> <span class="n">Cli</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </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> ?</span>
|
|||
|
|
|||
|
<span class="go">Documented commands (type help <command>):</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></span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>pour ajouter une commande, utiliser simplement l’héritage:</p>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">cli</span> <span class="k">import</span> <span class="n">Cli</span>
|
|||
|
<span class="gp">>>> </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">"hello </span><span class="si">%s</span><span class="s2">"</span><span class="p">,</span> <span class="n">line</span>
|
|||
|
<span class="gp">...</span>
|
|||
|
<span class="gp">>>> </span><span class="n">prompt</span> <span class="o">=</span> <span class="n">Prompt</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </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> ?</span>
|
|||
|
|
|||
|
<span class="go">Documented commands (type help <command>):</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> 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 d’interpré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">>>></span>
|
|||
|
<span class="gp">>>> </span><span class="n">fh</span> <span class="o">=</span> <span class="n">file</span><span class="p">(</span><span class="s1">'test'</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">fh</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'hello world'</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">fh</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </span><span class="n">content</span> <span class="o">=</span> <span class="n">file</span><span class="p">(</span><span class="s1">'test'</span><span class="p">,</span> <span class="s1">'r'</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </span><span class="n">content</span>
|
|||
|
<span class="go">'hello world'</span>
|
|||
|
<span class="go">>>></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> |
|
|||
|
<!-- 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> »
|
|||
|
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="footer" role="contentinfo">
|
|||
|
© Copyright 2015, cadoles (www.cadoles.com).
|
|||
|
Créé avec <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.7.
|
|||
|
</div>
|
|||
|
</body>
|
|||
|
</html>
|