doc:user:tutorials:tuto0
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
doc:user:tutorials:tuto0 [2013/07/12 15:42] – external edit 127.0.0.1 | doc:user:tutorials:tuto0 [2020/09/08 16:05] – boman | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
+ | This section shows how to use [[doc: | ||
- | ===== Introduction ===== | + | Python is a powerful scripting language allowing the creation of input files in a very convenient way for the developer, but it might look a bit tedious for a new user. |
- | Cette section montre comment utiliser de manière basique l' | ||
+ | ===== Introduction ===== | ||
- | ===== L' | + | When Metafor |
- | + | ||
- | + | ||
- | + | ||
- | ==== Introduction ==== | + | |
- | + | ||
- | + | ||
- | Lorsque | + | |
{{ doc: | {{ doc: | ||
- | On arrive alors dans " | + | This window displays the command-line interpreter. This is a Python |
- | Au dessus, on remarque la liste des restrictions | + | Above the interpreter, a list of all the restrictions |
- | Après initialisation de Python, Metafor | + | Once Python |
- | Un [[doc: | + | A Metafor |
- | Un autre aspect | + | Another interesting |
- | Python | + | Python |
>>> | >>> | ||
Line 38: | Line 31: | ||
>>> | >>> | ||
- | (attention: cela ne fonctionne sous cette forme que si vous n' | + | In a script, the correct syntax to get the value of '' |
- | Ce système permet de paramétrer les jeux de données de Metafor. Il est donc très important de définir (comme dans n'importe quel programme) un ensemble de variables (les paramètres du modèle) et de travailler avec ces entités algébriques. | + | |
- | Pour voir le contenu de la mémoire, utilisez | + | The definition of variables allows the parametrization of Metafor input files. Therefore, as with every other piece of software, it is important to first define a set of variables and then work with these algebraic variables. |
+ | |||
+ | To display memory contents, use '' | ||
>>>> | >>>> | ||
Line 48: | Line 42: | ||
>>> | >>> | ||
- | On y voit les variables | + | The three variables |
>>> | >>> | ||
Line 56: | Line 50: | ||
' | ' | ||
- | ==== Types ==== | + | ===== Types ===== |
- | Le type des variables s' | + | The type of a variable is obtained with the command |
>>> | >>> | ||
>>> | >>> | ||
- | <type ' | + | <class ' |
>>> | >>> | ||
>>> | >>> | ||
- | <type ' | + | <class ' |
>>> | >>> | ||
- | Des types peuvent être créés par l' | + | Metafor |
+ | Finally, variables can be converted from one type to another. For example, '' | ||
- | ==== Fonctions ==== | ||
+ | ===== Functions ===== | ||
- | L' | + | Simple functions can be created using the interpreter. This is quite useful to avoid any cut and paste, or simply to write algorithms as scripts. |
>>> | >>> | ||
Line 84: | Line 78: | ||
>>> | >>> | ||
- | Les fonctions sont un élément important dans l' | + | Functions are essential in the proper writing of an input file. This way, complex operations may be defined and called several times, from the same module |
- | Python permet de définir des valeurs par défaut pour les paramètres d'une fonction: | + | Default values for the parameters of python functions may be set: |
<code python> | <code python> | ||
Line 93: | Line 87: | ||
</ | </ | ||
- | où '' | + | where '' |
y = myfun(3, b=2) | y = myfun(3, b=2) | ||
- | Dans ce cas, '' | + | Here, '' |
- | Une autre manière d' | + | This function may be written in another way, using the '' |
myfun = lambda x, a=1, b=1, c=0 : a*x*x+b*x+c | myfun = lambda x, a=1, b=1, c=0 : a*x*x+b*x+c | ||
+ | ===== Modules ===== | ||
+ | It is quite useful to gather several functions into a file, to form a [[doc: | ||
- | + | By default, Python | |
- | + | ||
- | + | ||
- | + | ||
- | ==== Modules ==== | + | |
- | + | ||
- | Il est souvent utile de regrouper une série de fonctions ensemble dans un fichier pour former un [[doc: | + | |
- | + | ||
- | Python possède d' | + | |
>> import math | >> import math | ||
>>> | >>> | ||
- | [' | + | [' |
- | | + | |
- | | + | |
+ | ... | ||
+ | | ||
>>> | >>> | ||
0.99500416527802582 | 0.99500416527802582 | ||
>>> | >>> | ||
- | Pour voir l'aide en ligne, c'est la commande | + | The online help is accessed with the command |
>>> | >>> | ||
Line 134: | Line 124: | ||
Return the cosine of x (measured in radians). | Return the cosine of x (measured in radians). | ||
- | '' | + | '' |
- | Il est possible | + | It is also possible |
>>> | >>> | ||
Line 143: | Line 133: | ||
>>> | >>> | ||
- | Dans ce cas, le prefixe | + | With this syntax, the '' |
- | Dans le cas de Metafor, pour importer les classes du module | + | To use mathematical functions, the module '' |
+ | ===== Objects ===== | ||
- | + | Python | |
- | + | ||
- | ==== Objets ==== | + | |
- | + | ||
- | Python | + | |
<code python> | <code python> | ||
- | def vectLength(x, | + | def vectLength(x, |
import math | import math | ||
- | return math.sqrt(x*x+y*y+z*z) | + | return math.sqrt(x*x + y*y + z*z) |
</ | </ | ||
- | Remarquons ici que les blocs d'instructions | + | < |
+ | At this point, it should be noted that a block of several | ||
+ | </ | ||
- | La fonction | + | The function called |
<code python> | <code python> | ||
class vector: | class vector: | ||
def __init__(self, | def __init__(self, | ||
- | self.x=x | + | self.x = x |
- | self.y=y | + | self.y = y |
- | self.z=z | + | self.z = z |
def view(self): | def view(self): | ||
- | print ' | + | print(' |
def length(self): | def length(self): | ||
import math | import math | ||
- | return math.sqrt(self.x*self.x+self.y*self.y+self.z*self.z) | + | return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z) |
</ | </ | ||
- | Nous avons défini 3 [[doc: | + | Three methods (or member functions) have been defined: '' |
- | Calculer la longueur d'un vecteur revient à créer un vecteur par la commande : | + | To compute the length of a vector, this vector is first created using the command |
- | >>> | + | >>> |
- | Cette commande appelle le constructeur de la classe | + | This command calls the constructor (method |
>>> | >>> | ||
- | <type 'instance'> | + | <class '__main__.vector'> |
>>> | >>> | ||
x= 1 y= 1 z= 1 | x= 1 y= 1 z= 1 | ||
Line 193: | Line 182: | ||
>>> | >>> | ||
- | Dans le cas de Metafor, | + | When creating an input file in Metafor, |
- | ==== Boucles - tuples - listes ==== | ||
- | Dans un [[doc: | + | ===== Loop - tuples - lists ===== |
+ | |||
+ | In a Metafor | ||
<code python> | <code python> | ||
- | for i in (1, | + | for i in (1, 6, 8, 12): |
- | print ' | + | print(' |
</ | </ | ||
- | où les valeurs entre parenthèses forment un " | + | where the values in parentheses constitute a " |
- | Il est possible également d' | + | Instead of tuples, lists may also be used. To do so, brackets are used instead of parentheses. Unlike |
- | Pour créer une liste, il est souvent intéressant d' | + | To create a list, the function |
- | >>> | + | >>> |
[1, 3, 5, 7, 9] | [1, 3, 5, 7, 9] | ||
- | Une liste peut être créée par définition: | + | A list may also be defined as : |
[ x for x in range(100) if x%3==0 ] | [ x for x in range(100) if x%3==0 ] | ||
- | donne tous les nombres divisibles par 3 de 0 à 99. | + | leading to all numbers from 0 to 99 which can be divided by 3. |
- | ==== Structure conditionnelle | + | ===== Conditional statements ===== |
- | Il est parfois intéressant de paramétrer un jeu de données de manière complexe en utilisant des conditions. Par exemple, on peut définir par exemple un paramètre booléen | + | It may be useful to parametrize an input file in a complex way using conditional statements. For example, a boolean |
<code python> | <code python> | ||
if x>100: | if x>100: | ||
- | print " | + | print (" |
elif x>10: | elif x>10: | ||
- | print " | + | print (" |
else: | else: | ||
- | print " | + | print (" |
</ | </ | ||
- | ==== Remarques importantes | + | ===== Important notes ===== |
- | * Les booléens " | + | * The '' |
- | * Si on effectue une opération de division entre 2 entiers, Python réagit comme la plupart des langages compilés (C, [[doc: | + | * A number starting with 0 corresponds to a number defined using the octal numeral system. For example, 012 corresponds to 10 in decimal. |
- | * Un nombre commençant par 0 est interprété en base octale: par exemple 012 donne 10 en décimal. | + | * The command '' |
- | * La commande | + | * The function |
- | * La fonction | + | * The null command is named '' |
- | * La commande nulle s' | + | * Several commands may be written on the same line if separated by a semicolon, as in C. |
- | * Les commandes peuvent être écrites sur la même ligne en étant séparées par un point-virgule (comme en C). | + | * Strings are defined with simple |
- | + | ||
- | * Les chaînes sont délimitées soit par des simples | + | |
+ | ===== Conclusions ===== | ||
+ | In this page, the basic use of [[doc: | ||
+ | After reading this introduction to Python, this language can be used to define a first input file in Metafor: | ||
- | ===== Conclusions ===== | + | => [[doc: |
- | Ce document a montré la base de l' | + | [[test_python]] |
- | + | ||
- | Maintenant, vous en savez assez pour apprendre à utiliser | + | |
- | + | ||
- | => [[doc: | + |
doc/user/tutorials/tuto0.txt · Last modified: 2020/09/08 16:26 by boman