issue200:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue200:python [2023/12/30 10:37] – créée auntiee | issue200:python [2024/01/02 16:26] (Version actuelle) – d52fr | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Greetings again, fellow Beings. | + | **Greetings again, fellow Beings. |
- | How many times have you had a great idea for a program, but before you can really get it together, you found that you need some specialized data to actually get it properly programmed and tested? | + | How many times have you had a great idea for a program, but before you can really get it together, you found that you need some specialized data to actually get it properly programmed and tested? Data like user information, |
Enter Faker, a library for Python that does most of the ‘heavy lifting; for you. | Enter Faker, a library for Python that does most of the ‘heavy lifting; for you. | ||
Ligne 9: | Ligne 9: | ||
$ pip3 install Faker | $ pip3 install Faker | ||
- | Now that you’ve gotten it installed, let’s look at how it can help you. | + | Now that you’ve gotten it installed, let’s look at how it can help you.** |
- | First, we need to import the library and then create an instance of Faker. | + | Je vous salue à nouveau, chers amis. Tout d' |
+ | |||
+ | Combien de fois avez-vous eu une idée géniale pour un programme, mais avant de pouvoir vraiment le mettre au point, vous avez découvert que vous aviez besoin de certaines données spécialisées pour le programmer et le tester correctement ? Des données telles que des informations sur les utilisateurs, | ||
+ | |||
+ | Voici Faker, une bibliothèque pour Python qui se charge de la plus grande partie du travail à votre place. | ||
+ | |||
+ | Bien sûr, vous devez installer Faker dans Python et, comme d' | ||
+ | |||
+ | pip3 install Faker | ||
+ | |||
+ | Maintenant que vous l'avez installée, voyons comment elle peut vous aider. | ||
+ | |||
+ | |||
+ | **First, we need to import the library and then create an instance of Faker. | ||
>>> | >>> | ||
>>> | >>> | ||
- | Now we can use the ‘fake’ instance to get some data. Let’s say we want to get a fake name for a fictitious user. | + | Now we can use the ‘fake’ instance to get some data. Let’s say we want to get a fake name for a fictitious user. |
>>> | >>> | ||
Ligne 37: | Ligne 50: | ||
>>> | >>> | ||
+ | 'VISA 16 digit\nKevin Smith\n4731880480844912 04/29\nCVC: 849\n' | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | |||
+ | Nous pouvons maintenant utiliser l' | ||
+ | |||
+ | >>> | ||
+ | 'Ruth Duarte' | ||
+ | |||
+ | Et pourquoi pas une adresse pour notre nouvel utilisateur Ruth ? | ||
+ | |||
+ | >>> | ||
+ | '19373 Amanda Manors Suite 530\nWest William, MD 04391' | ||
+ | |||
+ | Bien sûr, Ruth aura besoin d'un numéro de téléphone et d'une adresse e-mail, non ? | ||
+ | |||
+ | |||
+ | >>> | ||
+ | ' | ||
+ | >>> | ||
+ | ' | ||
+ | |||
+ | Et Ruth aura besoin d'une carte de crédit. | ||
+ | |||
+ | >>> | ||
'VISA 16 digit\nKevin Smith\n4731880480844912 04/29\nCVC: 849\n' | 'VISA 16 digit\nKevin Smith\n4731880480844912 04/29\nCVC: 849\n' | ||
- | Let’s not forget that Ruth will probably need to have a car, so we’ll need to give her a license plate. | + | |
+ | **Let’s not forget that Ruth will probably need to have a car, so we’ll need to give her a license plate. | ||
>>> | >>> | ||
'780 VZD' | '780 VZD' | ||
- | That’s fantastic, right? | + | That’s fantastic, right? So far, all of this information has been for the U.S. What if you need to have data for the UK? No problem. Just create an instance of Faker using the localization attribute. |
>>> | >>> | ||
Ligne 53: | Ligne 95: | ||
What about people who need data for, let’s say, Norway? | What about people who need data for, let’s say, Norway? | ||
+ | |||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | ' | ||
+ | >>> | ||
+ | ' | ||
+ | >>> | ||
+ | ' | ||
+ | |||
+ | N' | ||
+ | |||
+ | >>> | ||
+ | '780 VZD' | ||
+ | |||
+ | C'est fantastique, | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | ' | ||
+ | >>> | ||
+ | ' | ||
+ | |||
+ | Qu'en est-il des personnes qui ont besoin de données pour, disons, la Norvège ? | ||
Ligne 63: | Ligne 129: | ||
' | ' | ||
- | Remember, none of this data is meant to be any more realistic for anything outside of creating dummy data for testing your programs. | + | |
+ | **Remember, none of this data is meant to be any more realistic for anything outside of creating dummy data for testing your programs. However, if you are an author, who needs a number of new character names for your newest cozy mystery novel. Let’s say we need 15 people. | ||
>>> | >>> | ||
- | ... | + | ... |
- | ... | + | ... |
Heidi Lucas | Heidi Lucas | ||
Catherine Davis | Catherine Davis | ||
Ligne 85: | Ligne 152: | ||
>>> | >>> | ||
- | We can also have Faker generate enums, booleans, dictionaries, | + | We can also have Faker generate enums, booleans, dictionaries, |
+ | |||
+ | >>> | ||
+ | 722.58** | ||
+ | |||
+ | Rappelez-vous qu' | ||
+ | |||
+ | >>> | ||
+ | ... personname=fake.name() | ||
+ | ... print(personname) | ||
+ | Heidi Lucas | ||
+ | Catherine Davis | ||
+ | Andrea Williams | ||
+ | Sierra Carpenter PhD | ||
+ | Tammy Berg | ||
+ | Diane Johnson | ||
+ | Brandi Brown | ||
+ | Jeffrey Flores | ||
+ | Kevin Roy | ||
+ | Jennifer Leonard | ||
+ | Brian Mason | ||
+ | Joshua Herrera | ||
+ | Eric Mitchell | ||
+ | Kelly Park | ||
+ | Joseph Harris | ||
+ | >>> | ||
+ | |||
+ | Nous pouvons également demander à Faker de générer des enums, des booléens, des dictionnaires, | ||
>>> | >>> | ||
722.58 | 722.58 | ||
- | The pyfloat method has the following syntax… | + | |
+ | **The pyfloat method has the following syntax… | ||
pyfloat(left_digits=None, | pyfloat(left_digits=None, | ||
- | You can check the documentation for more detailed information on the python provider as well as all the “standard” providers as well as specialized providers provided by the Faker community. | + | You can check the documentation for more detailed information on the python provider as well as all the "standard" |
- | So far, everything we’ve done has been in the Python terminal. | + | So far, everything we’ve done has been in the Python terminal. But reality is that we will need to write a program to do some heavier data faking. |
Let’s assume we have to create a user structure that has the following fields… | Let’s assume we have to create a user structure that has the following fields… | ||
- | User ID, User Name, User Email, User password, User phone number, Available balance, frozen balance and a status flag. | + | User ID, User Name, User Email, User password, User phone number, Available balance, frozen balance and a status flag. |
- | In addition, we need to create, say 30 of these user data groups to test the program we are writing. | + | In addition, we need to create, say 30 of these user data groups to test the program we are writing. Of course, we need to import Faker and a couple of things… |
+ | |||
+ | from faker import Faker | ||
+ | import pprint | ||
+ | import random** | ||
+ | |||
+ | La méthode pyfloat a la syntaxe suivante... | ||
+ | |||
+ | pyfloat(left_digits=None, | ||
+ | |||
+ | Vous pouvez consulter la documentation pour obtenir des informations plus détaillées sur le fournisseur python ainsi que sur tous les fournisseurs « standard » et les fournisseurs spécialisés fournis par la communauté Faker. | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | Supposons que nous devions créer une structure d' | ||
+ | |||
+ | ID de l' | ||
+ | |||
+ | En outre, nous devons créer, disons, 30 de ces groupes de données utilisateur pour tester le programme que nous sommes en train d' | ||
from faker import Faker | from faker import Faker | ||
Ligne 108: | Ligne 221: | ||
import random | import random | ||
- | Now, let’s create a function that will create our user data for us and return it as a dictionary (top right). | + | |
+ | **Now, let’s create a function that will create our user data for us and return it as a dictionary (top right). | ||
Now that we have all the data we need for this simple exercise, we’ll create a dictionary (right) and return it. | Now that we have all the data we need for this simple exercise, we’ll create a dictionary (right) and return it. | ||
- | So, here (top right) is what the data looks like. I’ll only show a couple of the user dictionary data sets instead of the 30 that we created. | + | So, here (top right) is what the data looks like. I’ll only show a couple of the user dictionary data sets instead of the 30 that we created. |
+ | |||
+ | You can find the Faker distribution homepage at https:// | ||
+ | |||
+ | That’s it for this month. I hope your December has been nice and happy and your New Year will bring happiness and prosperity. May the best day of your 2023 be the worst day of your 2024! | ||
+ | |||
+ | Until next time, as always; stay safe, healthy, positive and creative!** | ||
+ | |||
+ | Créons maintenant une fonction qui créera nos données utilisateur et les renverra sous la forme d'un dictionnaire (en haut à droite). | ||
+ | |||
+ | Maintenant que nous avons toutes les données dont nous avons besoin pour cet exercice simple, nous allons créer un dictionnaire (à droite) et le renvoyer. | ||
+ | |||
+ | Voici donc (en haut à droite) à quoi ressemblent les données. Je ne montrerai que quelques ensembles de données du dictionnaire de l' | ||
+ | |||
+ | Vous pouvez trouver la page d' | ||
+ | |||
+ | C'est tout pour ce mois-ci. J' | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | // Traduction des lignes noires de l' | ||
- | You can find the Faker distribution homepage at https:// | + | **Now, let’s setup the structures |
+ | Maintenant, paramétrons les structures et les variables qui nous sont nécessaires pour appeler la fonction. | ||
- | That’s it for this month. | + | **And finally we’ll use pprint to print out the data in the terminal window.** |
+ | Et, enfin, nous utiliserons pprint pour afficher les données sur l' | ||
- | Until next time, as always; stay safe, healthy, positive and creative! |
issue200/python.1703929063.txt.gz · Dernière modification : 2023/12/30 10:37 de auntiee