Une librairie Python utile pour la météo : https://pypi.org/project/pyowm/
Installation
$ pip install pyowm If
https://pyowm.readthedocs.io/en/latest/v3/code-recipes.html#library_init
Exemple :
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=++=+=+=+=+=+=+
Une autre pour les appels d’API : request
import requests
res = requests.get('http://api.open-notify.org/astros.json')
print(res.status_code)
"""
res = code réponse :
1XX - Information
2XX - Success
3XX - Redirect
4XX - Client Error (you made an error)
5XX - Server Error (they made an error)
"""
print(res.text)
json = res.json()
print(json['people'][0]['name'])
Installation
$ pip install schedule
Exemple :
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=++=+=+=+=+=+=+
Pour la planification d’actions : https://www.geeksforgeeks.org/python-schedule-library/
Installation
$ pip install schedule
Exemple :
# Schedule Library imported
import schedule
import time
# Functions setup
def sudo_placement():
print("Get ready for Sudo Placement at Geeksforgeeks")
def good_luck():
print("Good Luck for Test")
def work():
print("Study and work hard")
def bedtime():
print("It is bed time go rest")
def geeks():
print("Shaurya says Geeksforgeeks")
# Task scheduling
# After every 10mins geeks() is called.
schedule.every(1).minutes.do(geeks)
# After every hour geeks() is called.
schedule.every().hour.do(geeks)
# Every day at 12am or 00:00 time bedtime() is called.
schedule.every().day.at("00:00").do(bedtime)
# After every 5 to 10mins in between run work()
schedule.every(5).to(10).minutes.do(work)
# Every monday good_luck() is called
schedule.every().monday.do(good_luck)
# Every tuesday at 18:00 sudo_placement() is called
schedule.every().tuesday.at("18:00").do(sudo_placement)
# Loop so that the scheduling task
# keeps on running all time.
while True:
# Checks whether a scheduled task
# is pending to run or not
schedule.run_pending()
time.sleep(1)
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=+
Une pour la traduction de textes : Deep-Translator
Installation
$ pip install deep-translator
Exemple : https://medium.com/analytics-vidhya/how-to-translate-text-with-python-9d203139dcf5
from deep_translator import GoogleTranslator
to_translate = 'I want to translate this text'
translated = GoogleTranslator(source='auto', target='de').translate(to_translate)
+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=++=+=+=+=+=+=+
Démarrer un script Python automatiquement :
Sous Ubuntu : https://stackoverflow.com/questions/24518522/run-python-script-at-startup-in-ubuntu
- Copier le script Python dans /bin:
sudo cp -i /path/to/your_script.py /bin
- Ajouter un Cron Job :
sudo crontab -e
- Scroll to the bottom and add the following line (after all the
#'s
) :@reboot sudo python3 /bin/your_script.py &
- The “&” at the end of the line means the command is run in the background and it won’t stop the system booting up.
- Test :
sudo reboot
+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=++=+=+=+=+=+=+
Gestion des exceptions :
La gestion avec les mots clefs except else (cf. ce lien)
Liste des exceptions standard Python (cf ce lien)
print(dir(locals()['__builtins__']))
Construire ses exceptions
: (cf. ce lien)
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=++=+=+=+=+=+=+
Gestion des logs : https://docs.python.org/3/howto/logging.html
Dans un fichier : https://www.delftstack.com/fr/howto/python/python-log-to-file/
import logging
logger = logging.getLogger()
handler = logging.FileHandler('logfile.log')
logger.addHandler(handler)
logger.error('Our First Log Message')
Pour écrire toutes les sorties du script dans un log : sudo python3 botmeteo.py >> botmeteo.log &
Références :
https://stackoverflow.com/questions/15111249/how-to-log-a-python-crash
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=++=+=+=+=+=+=+
Références :
Cython : transformer du Python en C pour plus de rapidité : https://medium.com/@ferdina.kusumah/run-python-code-hundreds-of-times-faster-716f4da20669
Modules et snippets Python : https://towardsdatascience.com/all-about-python-100-code-snippets-tricks-concepts-and-important-modules-9a9fda489b6b
######################
Utilisation de la librairie easygui et de l’utilitaire rembg pour retirer le fond des images.
Code :
from rembg import remove
from PIL import Image
import easygui as eg
input_path = eg.fileopenbox(title='Select image file')
output_path = eg.filesavebox(title='Save file to..')
input = Image.open(input_path)
output = remove(input)
output.save(output_path)
Référence : https://www.tomshardware.com/how-to/python-remove-image-backgrounds
Editeur de code : https://thonny.org/