doc:user:tutorials:tuto0
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
doc:user:tutorials:tuto0 [2017/10/10 17:18] – [Conclusions] boman | doc:user:tutorials:tuto0 [2020/09/08 12:17] – [Modules] boman | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | | ||
====== Python in a nutshell ====== | ====== Python in a nutshell ====== | ||
- | |||
- | |||
- | ===== Introduction ===== | ||
This section shows how to use [[doc: | This section shows how to use [[doc: | ||
- | Python is a powerful language allowing the creation of data sets in a way very convenient for the developer, but it might look a bit tedious for the code user. | + | Python is a powerful |
- | ===== The interpreter ===== | + | |
- | + | ===== Introduction | |
- | ==== Introduction ==== | + | |
When Metafor is started, a window appears: | When Metafor is started, a window appears: | ||
Line 22: | Line 16: | ||
Above the interpreter, | Above the interpreter, | ||
- | Once Python is initialized, | + | Once Python is initialized, |
- | A Metafor [[doc: | + | A Metafor [[doc: |
- | Another interesting aspect of Python is its ability to interface compiled [[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 : | + | Python is quite simple. For example, it can be used as a calculator |
>>> | >>> | ||
Line 37: | Line 31: | ||
>>> | >>> | ||
- | Careful : this sequence is only correct when using Metafor without its graphical interface. With Metafor GUI, the correct syntax to get the value of '' | + | In a script, the correct syntax to get the value of '' |
- | The definition of variables allows the parametrization of Metafor | + | The definition of variables allows the parametrization of Metafor |
To display memory contents, use '' | To display memory contents, use '' | ||
Line 56: | Line 50: | ||
' | ' | ||
- | ==== Types ==== | + | ===== Types ===== |
- | The type of a variable is obtained with the command '' | + | The type of a variable is obtained with the command '' |
>>> | >>> | ||
>>> | >>> | ||
- | <type ' | + | <class ' |
>>> | >>> | ||
>>> | >>> | ||
- | <type ' | + | <class ' |
>>> | >>> | ||
- | Metafor uses the basic types (float, string, integer, tuple, ...), but also its own types, which are defined | + | Metafor uses the basic python |
Finally, variables can be converted from one type to another. For example, '' | Finally, variables can be converted from one type to another. For example, '' | ||
Line 74: | Line 68: | ||
- | ==== Functions ==== | + | ===== 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 : | + | 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: |
>>> | >>> | ||
Line 84: | Line 78: | ||
>>> | >>> | ||
- | Functions are essential in the proper writing of a data set. This way, complex operations may be defined and called several times, from the same module or even from other ones. For example, if the test case contains several similar [[doc: | + | 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 these parameters may be set : | + | Default values for the parameters |
<code python> | <code python> | ||
Line 103: | Line 97: | ||
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 ==== | + | ===== Modules |
It is quite useful to gather several functions into a file, to form a [[doc: | 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 '' | + | By default, Python already contains many modules. For example, the module called '' |
>> import math | >> import math | ||
>>> | >>> | ||
- | [' | + | [' |
- | | + | |
- | | + | |
+ | ... | ||
+ | | ||
>>> | >>> | ||
0.99500416527802582 | 0.99500416527802582 | ||
Line 121: | Line 117: | ||
>>> | >>> | ||
- | Help on built-in function cos in module math : | + | Help on built-in function cos in module math: |
| | ||
cos(...) | cos(...) | ||
Line 130: | Line 126: | ||
'' | '' | ||
- | It is also possible to import all functions found in '' | + | It is also possible to import all functions found in '' |
>>> | >>> | ||
Line 139: | Line 135: | ||
With this syntax, the '' | With this syntax, the '' | ||
- | To use mathematical functions, the module '' | + | To use mathematical functions, the module '' |
- | A list of materials can also be imported using the command '' | + | |
- | In this case, the module no longer contains functions but objects. | + | |
- | ==== Objects ==== | + | ===== Objects |
Python is [[doc: | Python is [[doc: | ||
<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) |
</ | </ | ||
Line 157: | Line 151: | ||
</ | </ | ||
- | The function called '' | + | 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) |
</ | </ | ||
- | Three methods (or member functions) | + | Three methods (or member functions) |
To compute the length of a vector, this vector is first created using the command | To compute the length of a vector, this vector is first created using the command | ||
- | >>> | + | >>> |
This command calls the constructor (method '' | This command calls the constructor (method '' | ||
>>> | >>> | ||
- | <type 'instance'> | + | <class '__main__.vector'> |
>>> | >>> | ||
x= 1 y= 1 z= 1 | x= 1 y= 1 z= 1 | ||
Line 188: | Line 182: | ||
>>> | >>> | ||
- | When creating | + | When creating |
- | ==== Loop - tuple - list ==== | + | |
- | In a Metafor [[doc: | + | |
+ | ===== Loop - tuples - lists ===== | ||
+ | |||
+ | In a Metafor [[doc: | ||
<code python> | <code python> | ||
- | for i in (1, | + | for i in (1, 6, 8, 12): |
- | print ' | + | print(' |
</ | </ | ||
Line 202: | Line 198: | ||
Instead of tuples, lists may also be used. To do so, brackets are used instead of parentheses. Unlike tuples, lists may be modified. | 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 '' | + | To create a list, the function '' |
- | >>> | + | >>> |
[1, 3, 5, 7, 9] | [1, 3, 5, 7, 9] | ||
Line 214: | Line 210: | ||
- | ==== Conditional | + | ===== Conditional |
- | It may be useful to parametrize | + | It may be useful to parametrize |
<code python> | <code python> | ||
if x>100: | if x>100: | ||
- | print " | + | print (" |
elif x>10: | elif x>10: | ||
- | print " | + | print (" |
else: | else: | ||
- | print " | + | print (" |
</ | </ | ||
- | ==== Important notes ==== | + | ===== Important notes ===== |
- | * The '' | + | * The '' |
- | + | ||
- | * If two integers are divided, Python will do an INTERGER division, as most compiled languages (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. | * 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 command '' |
- | * The function '' | + | * The function '' |
- | * The null command is called | + | * The null command is named '' |
* Several commands may be written on the same line if separated by a semicolon, as in C. | * Several commands may be written on the same line if separated by a semicolon, as in C. | ||
Line 248: | Line 242: | ||
- | |||
- | |||
===== Conclusions ===== | ===== Conclusions ===== | ||
- | In this page, the basic use of [[doc: | + | In this page, the basic use of [[doc: |
- | After reading this introduction to Python, this language can be used to define a first data set in Metafor : | + | After reading this introduction to Python, this language can be used to define a first input file in Metafor: |
=> [[doc: | => [[doc: |
doc/user/tutorials/tuto0.txt · Last modified: 2020/09/08 16:26 by boman