algo avancée
This commit is contained in:
@ -1,6 +1,143 @@
|
||||
Définition d'un programme
|
||||
==========================
|
||||
|
||||
Ce pourrait être dit abstraitement, mais ce serait dommage.
|
||||
Construisons un programme en python
|
||||
|
||||
La CLI (command line interface)
|
||||
---------------------------------
|
||||
|
||||
.. code-block:: ocaml
|
||||
|
||||
let echo () =
|
||||
let len = Array.length Sys.argv in
|
||||
if len > 1 then
|
||||
begin
|
||||
for i=0 to len-1 do
|
||||
print_string Sys.argv.(i) ;
|
||||
print_char ' ';
|
||||
done
|
||||
end
|
||||
let _ = echo()
|
||||
|
||||
|
||||
::
|
||||
|
||||
ocaml cli.ml arg1 arg2 arg3
|
||||
cli.ml arg1 arg2 arg3
|
||||
|
||||
en python:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import sys
|
||||
print sys.argv
|
||||
|
||||
::
|
||||
|
||||
python toto.py arg1 arg2
|
||||
['toto.py', 'arg1', 'arg2']
|
||||
|
||||
c'est plus simple...
|
||||
|
||||
|
||||
La REPL (boucle d'interaction)
|
||||
-------------------------------
|
||||
|
||||
.. glossary::
|
||||
|
||||
REPL
|
||||
|
||||
Read Eval Print Loop : outil principal de communication avec un programme
|
||||
ou avec un système. Exemples : la console python, le prompt OCaml.
|
||||
|
||||
interface
|
||||
|
||||
outil de communication avec un programme.
|
||||
|
||||
- interface texte
|
||||
- interface graphique
|
||||
|
||||
**Exemples de REPL**
|
||||
|
||||
Le prompt python::
|
||||
|
||||
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
|
||||
[GCC 5.4.0 20160609] on linux2
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>>
|
||||
>>> dir()
|
||||
['__builtins__', '__doc__', '__name__', readline', 'rlcompleter']
|
||||
>>>
|
||||
|
||||
Le prompt ipython::
|
||||
|
||||
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
|
||||
Type "copyright", "credits" or "license" for more information.
|
||||
|
||||
IPython 2.4.1 -- An enhanced Interactive Python.
|
||||
? -> Introduction and overview of IPython's features.
|
||||
%quickref -> Quick reference.
|
||||
help -> Python's own help system.
|
||||
object? -> Details about 'object', use 'object??' for extra details.
|
||||
|
||||
In [1]:
|
||||
|
||||
Le prompt OCaml (utop)::
|
||||
|
||||
Type #utop_help for help about using utop.
|
||||
|
||||
─( 09:21:24 )─< command 0 >──
|
||||
utop #
|
||||
# let x = 1 in x + 2;;
|
||||
- : int = 3
|
||||
# let y = 1 + 2;;
|
||||
val y : int = 3
|
||||
# y * y;;
|
||||
- : int = 9
|
||||
|
||||
Construire une boucle d'interaction avec l'utilisateur en python::
|
||||
|
||||
#!/usr/bin/env python3
|
||||
error = True
|
||||
while error:
|
||||
try:
|
||||
entier = int(input('donnez un entier : '))
|
||||
error = False
|
||||
except:
|
||||
print('une valeur entiere est attendue')
|
||||
print(entier)
|
||||
|
||||
Lire et écrire dans un fichier
|
||||
------------------------------
|
||||
|
||||
Les descripteurs de fichiers (file handle)
|
||||
|
||||
Exemple en python
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> fh = file("test.txt", "w")
|
||||
>>> fh.write("un contenu exemple")
|
||||
>>> fh.close()
|
||||
>>>
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> fh.read()
|
||||
'un contenu exemple'
|
||||
>>> fh.close()
|
||||
>>>
|
||||
|
||||
Linéarisation (serialisation) de données par exemple en json
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import json
|
||||
data = dict(a='essai', b='essai2', c=range(3))
|
||||
with open('data.txt', 'w') as outfile:
|
||||
json.dump(data, outfile)
|
||||
|
||||
Qu'est-ce qu'un programme ?
|
||||
----------------------------
|
||||
|
||||
@ -431,99 +568,3 @@ Pour résumer en ocaml, nous avons vu
|
||||
- qu'il n'y a pas de distinction entre expressions et instructions.
|
||||
|
||||
|
||||
La REPL (boucle d'interaction)
|
||||
-------------------------------
|
||||
|
||||
.. glossary::
|
||||
|
||||
REPL
|
||||
|
||||
Read Eval Print Loop : outil principal de communication avec un programme
|
||||
ou avec un système. Exemples : la console python, le prompt OCaml.
|
||||
|
||||
interface
|
||||
|
||||
outil de communication avec un programme.
|
||||
|
||||
- interface texte
|
||||
- interface graphique
|
||||
|
||||
**Exemples de REPL**
|
||||
|
||||
Le prompt python::
|
||||
|
||||
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
|
||||
[GCC 5.4.0 20160609] on linux2
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>>
|
||||
>>> dir()
|
||||
['__builtins__', '__doc__', '__name__', readline', 'rlcompleter']
|
||||
>>>
|
||||
|
||||
Le prompt ipython::
|
||||
|
||||
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
|
||||
Type "copyright", "credits" or "license" for more information.
|
||||
|
||||
IPython 2.4.1 -- An enhanced Interactive Python.
|
||||
? -> Introduction and overview of IPython's features.
|
||||
%quickref -> Quick reference.
|
||||
help -> Python's own help system.
|
||||
object? -> Details about 'object', use 'object??' for extra details.
|
||||
|
||||
In [1]:
|
||||
|
||||
Le prompt OCaml (utop)::
|
||||
|
||||
Type #utop_help for help about using utop.
|
||||
|
||||
─( 09:21:24 )─< command 0 >──
|
||||
utop #
|
||||
# let x = 1 in x + 2;;
|
||||
- : int = 3
|
||||
# let y = 1 + 2;;
|
||||
val y : int = 3
|
||||
# y * y;;
|
||||
- : int = 9
|
||||
|
||||
Construire une boucle d'interaction avec l'utilisateur en python::
|
||||
|
||||
#!/usr/bin/env python3
|
||||
error = True
|
||||
while error:
|
||||
try:
|
||||
entier = int(input('donnez un entier : '))
|
||||
error = False
|
||||
except:
|
||||
print('une valeur entiere est attendue')
|
||||
print(entier)
|
||||
|
||||
Lire et écrire dans un fichier
|
||||
------------------------------
|
||||
|
||||
Les descripteurs de fichiers (file handle)
|
||||
|
||||
Exemple en python
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> fh = file("test.txt", "w")
|
||||
>>> fh.write("un contenu exemple")
|
||||
>>> fh.close()
|
||||
>>>
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> fh.read()
|
||||
'un contenu exemple'
|
||||
>>> fh.close()
|
||||
>>>
|
||||
|
||||
Linéarisation (serialisation) de données par exemple en json
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import json
|
||||
data = dict(a='essai', b='essai2', c=range(3))
|
||||
with open('data.txt', 'w') as outfile:
|
||||
json.dump(data, outfile)
|
||||
|
Reference in New Issue
Block a user