Modifications

Sauter à la navigation Sauter à la recherche
1 420 octets ajoutés ,  11 novembre 2016 à 17:42
aucun résumé de modification
Ligne 1 : Ligne 1 :  
{{FEATHER-CHARGER-MODULE-NAV}}
 
{{FEATHER-CHARGER-MODULE-NAV}}
   −
{{traduction}}
   
== Importer un fichier de code ==
 
== Importer un fichier de code ==
 
Comme pour une version standard de Python, vous pouvez importer et exécuter du code provenant de fichiers python dans votre script MicroPython. Cette approche est idéal pour décomposer un long script ou script complexe en des morceaux de codes plus petits. Cela permet de réutiliser ou partager du code entre plusieurs project. Cela facilite également le processus de maintenance.
 
Comme pour une version standard de Python, vous pouvez importer et exécuter du code provenant de fichiers python dans votre script MicroPython. Cette approche est idéal pour décomposer un long script ou script complexe en des morceaux de codes plus petits. Cela permet de réutiliser ou partager du code entre plusieurs project. Cela facilite également le processus de maintenance.
Ligne 105 : Ligne 104 :  
Notez que l'instruction d'importation référence le fichier '''add.py''' en utilisant un "''nom d'importation absolu''". Par exemple, le fonction add est importée depuis '''test.add'''.  Le 'test.' est le nom du paquet/package, par exemple: le nom du répertoire du paquet/package. Le 'add' derrière le 'test.add' est le nom du fichier python (add.py) où l'interpréteur Python trouvera la fonction à importer.  
 
Notez que l'instruction d'importation référence le fichier '''add.py''' en utilisant un "''nom d'importation absolu''". Par exemple, le fonction add est importée depuis '''test.add'''.  Le 'test.' est le nom du paquet/package, par exemple: le nom du répertoire du paquet/package. Le 'add' derrière le 'test.add' est le nom du fichier python (add.py) où l'interpréteur Python trouvera la fonction à importer.  
   −
Suivant l'instruction '''{{fname|1=from <font color="teal">test</font>.<font color="purple">add</font> import <font color="Orange">add</font> }}''', nous importons donc une fonction '''<font color="Orange">add</font>''' depuis le fichier '''<font color="purple">add.py</font>''' se trouvant dans le répertoire "'''<font color="teal">test</font>'''" (le '''<font color="teal">test</font>.<font color="purple">add</font>''').  
+
Suivant l'instruction '''{{fname|1=from <font color="teal">test</font>.<font color="purple">add</font> import <font color="Orange">add</font> }}''', nous importons donc une fonction '''<font color="Orange">add</font>''' depuis le fichier '''<font color="purple">add.py</font>''' se trouvant dans le répertoire "'''<font color="teal">test</font>'''" (comme stipulé par '''<font color="teal">test</font>.<font color="purple">add</font>''').  
   −
Vous  You can't run a command like "'''from add import add'''" in the __init__.py since Python would get confused if it should load an add.py from inside the module or from elsewhereUsing the full name '''test.add''' tells Python to use the '''add.py''' module inside the '''test''' package directory.
+
Dans le script {{fname|__init__.py}}, vous {{underline|ne pourrez pas exécuter une commande}} tel que "'''from add import add'''" étant donné que cette instruction est totalement confuse pour Python. En effet, il essayera de charger la fonction {{fname|add}} depuis le module add.py (le fichier add.py) se trouvant dans le même répertoire où est exécuté le script principal (''comme expliqué en début de cette section'')En utilisant le nom complet '''test.add''' nous indiquons à Python d'utiliser le fichier/module '''add.py''' à l'intérieur du répertoire paquet/package '''test''' .
   −
Double check you have a test package structure that looks like the following:
+
Vérifiez que le paquet/package test soit confirme avec la structure suivant:
   −
* '''test''' directory
+
* Répertoire '''test'''
** '''__init__.py''' file which imports the add and subtract functions
+
** Le fichier '''__init__.py''' qui importe les fonctions {{fname|add()}} et {{fname|substract()}}
** '''add.py''' which exposes the add function
+
** Le fichier '''add.py''' qui expose la fonction {{fname|add()}}
** '''subtract.py''' which exposes the subtract function
+
** Le fichier '''subtract.py''' qui expose la fonction {{fname|subtract()}}
   −
Now try importing and using the package with desktop Python. In a terminal navigate to the '''parent of the test package directory''' (i.e. one folder above it). Importing and using a package is just like importing a .py file however Python treats the entire test directory as the package itself. This means you need to run Python from above the test directory so Python can find the package.
+
Essayez maintenant d'importer et d'utiliser le paquet/package avec la version PC de Python. Utilisez un terminal et naviguez jusqu'au répertoire '''parent du répertoire "test" contenant notre paquet/package''' (ex. le répertoire juste avant le répertoire "test"). Importer et utiliser un paquet/package se fait de façon identique à l'importation d'un fichier .py, cependant Python traitera le répertoire ''test'' entier comme le paquet/package lui-même. Cela signifie surtout que vous aurez besoin d'exécuter Python depuis le répertoire parent du répertoire "''test''" (et Python sera capable de trouver le paquet).
   −
'''Also note be sure you don't have a test.py file in the same directory as the test package'''! If you do Python could get confused and import the test.py file instead of the test packageDelete test.py if it exists next to the test package folder!
+
'''Assurez-vous de ne pas avoir un fichier test.py et un paquet/package (répertoire) test''' dans le même répertoire! Sinon Python ne s’emmêlera les pinceaux et importera le fichier/module test.py à la place du paquet/package "test"Effacez le fichier "test.py" s'il existe un paquet/package test dans le même répertoire!
   −
Run the following code in the Python REPL to import and use the add and subtract functions from the test package:
+
Exécutez le code suivant dans une invite REPL de votre carte MicroPython pour importer et utiliser les fonctions {{fname|add()}} et {{fname|substract()}} disponibles dans le paquet/package:
    
  <syntaxhighlight lang="python">
 
  <syntaxhighlight lang="python">
Ligne 130 : Ligne 129 :  
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT-20.png}}
 
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT-20.png}}
   −
You should see the package imported and the functions work exactly as you saw beforeIf you see an ImportError that the test module doesn't exist be sure you're running Python from the '''parent''' of the test package directory!
+
Vous devriez pouvoir importer le paquet/package et les fonctions fonctionner exactement comme vu ci-dessusSi vous constatez un message {{fname|ImportError}} indiquant que le module n'existe pas alors vérifiez que vous exécutez bien Python depuis le répertoire '''parent''' de votre répertoire du paquet/package "test"!
   −
Now try copying the package to your MicroPython board and using it exactly as you did with desktop Python. '''Again make sure a test.py file doesn't exist on the board or else MicroPython will be confused about what to import'''!  If you have test.py on your board already you can delete it using the following ampy command:
+
Essayez maintenant de copier le paquet/package sur votre carte MicroPython et utilisez le exactement comme vous l'avez fait dans votre environnement PC. '''Encore une fois, assurez-vous qu'il n'y a pas de fichier test.py sur la carte sinon MicroPython ne saura pas lequel, du fichier "test.py" ou répertoire "test", il faudra importer le code'''!  Si vous avez déjà un fichier test.py sur votre carte, vous pouvez l'effacer une utilisant une commande ampy comme celle-ci:
    
  <nowiki>ampy --port /board/serial/port rm test.py</nowiki>
 
  <nowiki>ampy --port /board/serial/port rm test.py</nowiki>
   −
Now create the test package directory in the root of the board:
+
Créons maintenant la paquet/package test en créant le répertoire "test" à la racine du système de fichier:
    
  <nowiki>ampy --port /board/serial/port mkdir test</nowiki>
 
  <nowiki>ampy --port /board/serial/port mkdir test</nowiki>
   −
And copy inside the three .py files above that define the package (run these commands from '''inside''' the test package folder):
+
Copions ensuite les 3 fichiers .py qui définissent le paquet/package (voir ci-dessus) à '''l'intérieur''' du répertoire "test" (donc à '''l'intérieur''' de notre paquet/package):
    
  <nowiki>ampy --port /board/serial/port put __init__.py test/__init__.py
 
  <nowiki>ampy --port /board/serial/port put __init__.py test/__init__.py
Ligne 146 : Ligne 145 :  
ampy --port /board/serial/port put subtract.py test/subtract.py</nowiki>
 
ampy --port /board/serial/port put subtract.py test/subtract.py</nowiki>
   −
These commands will copy the .py files to the test folder on the board's filesystem. Use the {{fname|ls}} command to double check there's a test folder with these three files on your board:
+
Ces commande copierons les fichiers .py dans le répertoire "test" du système de fichier de la carte MicroPython. Utilisez la commande {{fname|ls}} pour vérifier la présence du répertoire {{fname|test}} et qu'il contient les trois fichiers nécessaires:
    
  <nowiki>ampy --port /board/serial/port ls test</nowiki>
 
  <nowiki>ampy --port /board/serial/port ls test</nowiki>
Ligne 152 : Ligne 151 :  
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT-21.png}}
 
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT-21.png}}
   −
Connect to the board's REPL and try importing the test package and using its functions as before:
+
Connectez vous ensuite en REPL sur la carte et essayez d'importer le paquet/package test pour utiliser ses différentes fonctions (comme nous l'avons déjà fait):
    
  <syntaxhighlight lang="python">
 
  <syntaxhighlight lang="python">
Ligne 162 : Ligne 161 :  
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT-22.png}}
 
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT-22.png}}
   −
You should see the package import and the functions work exactly as before when they were in a single test.py file!
+
Vous devriez voir le paquet/package s'importer et les fonctions fonctionner exactement comme c'était le cas avec le fichier test.py !
   −
Breaking a complex module apart into multiple files with a package is a great way to simplify and structure code. In this simple example it seems like a bit of unnecessary work, but as scripts get more complex and re-use common code it will help immensely to break them into modules and packages. You can even start to share code with others by giving them your modules and packages to load and use!
+
Subdiviser un module complexe en plusieurs fichiers contenu dans un packet/package est une bonne façon de simplifier la structure du code (et parfois le code lui-même). Dans cet exemple un peu simplicte nous avons déployé beaucoup de zèle (et de travail inutile). Dans le cas présent, le paquet/package est, au final, est "plus complexe" que le simple fichier "test.py". Le but est avant tout de comprendre le fonctionnement d'un paquet/package, chose que vous trouverez utile lorsque vous voudrez réutiliser du code, le partager, ou en réduire la complexité.
    
{{FEATHER-CHARGER-MODULE-TRAILER}}
 
{{FEATHER-CHARGER-MODULE-TRAILER}}
29 917

modifications

Menu de navigation