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/08 18:13] – [Boucles - tuples - listes] joris | doc:user:tutorials:tuto0 [2020/09/08 16:05] – boman | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Python in a nutshell ====== | ||
+ | This section shows how to use [[doc: | ||
+ | |||
+ | 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. | ||
+ | |||
+ | |||
+ | ===== Introduction ===== | ||
+ | |||
+ | When Metafor is started, a window appears: | ||
+ | |||
+ | {{ doc: | ||
+ | |||
+ | This window displays the command-line interpreter. This is a Python interpreter integrated within Metafor, allowing the user to type commands, in a similar way as in [[http:// | ||
+ | |||
+ | Above the interpreter, | ||
+ | |||
+ | Once Python is initialized, | ||
+ | |||
+ | A Metafor [[doc: | ||
+ | |||
+ | Another interesting aspect of Python is its ability to interface compiled [[doc: | ||
+ | |||
+ | Python is quite simple. For example, it can be used as a calculator in interactive mode: | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | 5 | ||
+ | >>> | ||
+ | |||
+ | In a script, the correct syntax to get the value of '' | ||
+ | |||
+ | 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 '' | ||
+ | |||
+ | >>>> | ||
+ | [' | ||
+ | ' | ||
+ | >>> | ||
+ | |||
+ | The three variables that were just created can be seen, and so can the '' | ||
+ | |||
+ | >>> | ||
+ | [' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | |||
+ | ===== Types ===== | ||
+ | |||
+ | The type of a variable is obtained with the command '' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | <class ' | ||
+ | >>> | ||
+ | >>> | ||
+ | <class ' | ||
+ | >>> | ||
+ | |||
+ | Metafor uses the basic python types (float, string, integer, tuple, ...), but also its own types, which are defined by custom classes. | ||
+ | |||
+ | Finally, variables can be converted from one type to another. For example, '' | ||
+ | |||
+ | |||
+ | |||
+ | ===== Functions ===== | ||
+ | |||
+ | 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. For example: | ||
+ | |||
+ | >>> | ||
+ | ... | ||
+ | >>> | ||
+ | 36 | ||
+ | >>> | ||
+ | |||
+ | 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 or even from other ones. For example, if the numerical model contains several similar [[doc: | ||
+ | |||
+ | Default values for the parameters of python functions may be set: | ||
+ | |||
+ | <code python> | ||
+ | def myfun(x, a=1, b=1, c=0): | ||
+ | return a*x*x+b*x+c | ||
+ | </ | ||
+ | |||
+ | where '' | ||
+ | |||
+ | y = myfun(3, b=2) | ||
+ | |||
+ | Here, '' | ||
+ | |||
+ | 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 | ||
+ | |||
+ | ===== Modules ===== | ||
+ | |||
+ | It is quite useful to gather several functions into a file, to form a [[doc: | ||
+ | |||
+ | By default, Python already contains many modules. For example, the module called '' | ||
+ | |||
+ | >> import math | ||
+ | >>> | ||
+ | [' | ||
+ | ' | ||
+ | ' | ||
+ | ... | ||
+ | ' | ||
+ | >>> | ||
+ | 0.99500416527802582 | ||
+ | >>> | ||
+ | |||
+ | The online help is accessed with the command '' | ||
+ | |||
+ | >>> | ||
+ | Help on built-in function cos in module math: | ||
+ | | ||
+ | cos(...) | ||
+ | cos(x) | ||
+ | | ||
+ | Return the cosine of x (measured in radians). | ||
+ | |||
+ | '' | ||
+ | |||
+ | It is also possible to import all functions found in '' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | 0.99500416527802582 | ||
+ | >>> | ||
+ | |||
+ | With this syntax, the '' | ||
+ | |||
+ | To use mathematical functions, the module '' | ||
+ | |||
+ | ===== Objects ===== | ||
+ | |||
+ | Python is [[doc: | ||
+ | |||
+ | <code python> | ||
+ | def vectLength(x, | ||
+ | import math | ||
+ | return math.sqrt(x*x + y*y + z*z) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | At this point, it should be noted that a block of several instructions is defined with the indentation, | ||
+ | </ | ||
+ | |||
+ | The function called '' | ||
+ | |||
+ | <code python> | ||
+ | class vector: | ||
+ | def __init__(self, | ||
+ | self.x = x | ||
+ | self.y = y | ||
+ | self.z = z | ||
+ | def view(self): | ||
+ | print(' | ||
+ | def length(self): | ||
+ | import math | ||
+ | return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z) | ||
+ | </ | ||
+ | |||
+ | Three methods (or member functions) have been defined: '' | ||
+ | |||
+ | To compute the length of a vector, this vector is first created using the command | ||
+ | |||
+ | >>> | ||
+ | |||
+ | This command calls the constructor (method '' | ||
+ | |||
+ | >>> | ||
+ | <class ' | ||
+ | >>> | ||
+ | x= 1 y= 1 z= 1 | ||
+ | >>> | ||
+ | 1.7320508075688772 | ||
+ | >>> | ||
+ | |||
+ | When creating an input file in Metafor, the user manipulates classes defined in the modules found in the folder '' | ||
+ | |||
+ | |||
+ | ===== Loop - tuples - lists ===== | ||
+ | |||
+ | In a Metafor [[doc: | ||
+ | |||
+ | <code python> | ||
+ | for i in (1, 6, 8, 12): | ||
+ | print(' | ||
+ | </ | ||
+ | |||
+ | where the values in parentheses constitute a " | ||
+ | |||
+ | Instead of tuples, lists may also be used. To do so, brackets are used instead of parentheses. Unlike tuples, lists may be modified. | ||
+ | |||
+ | To create a list, the function '' | ||
+ | |||
+ | >>> | ||
+ | [1, 3, 5, 7, 9] | ||
+ | |||
+ | A list may also be defined as : | ||
+ | |||
+ | [ x for x in range(100) if x%3==0 ] | ||
+ | |||
+ | leading to all numbers from 0 to 99 which can be divided by 3. | ||
+ | |||
+ | |||
+ | ===== Conditional statements ===== | ||
+ | |||
+ | It may be useful to parametrize an input file in a complex way using conditional statements. For example, a boolean '' | ||
+ | |||
+ | <code python> | ||
+ | if x>100: | ||
+ | print (" | ||
+ | elif x>10: | ||
+ | print (" | ||
+ | else: | ||
+ | print (" | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Important notes ===== | ||
+ | |||
+ | |||
+ | * The '' | ||
+ | |||
+ | * A number starting with 0 corresponds to a number defined using the octal numeral system. For example, 012 corresponds to 10 in decimal. | ||
+ | |||
+ | * The command '' | ||
+ | |||
+ | * The function '' | ||
+ | |||
+ | * The null command is named '' | ||
+ | |||
+ | * Several commands may be written on the same line if separated by a semicolon, as in C. | ||
+ | |||
+ | * Strings are defined with simple ('), double (") or triple quotation marks (""" | ||
+ | |||
+ | |||
+ | ===== 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: | ||
+ | |||
+ | => [[doc: | ||
+ | |||
+ | |||
+ | [[test_python]] |
doc/user/tutorials/tuto0.txt · Last modified: 2020/09/08 16:26 by boman