Python : Black

De Justine's wiki
Aller à la navigation Aller à la recherche

Black est un formatteur de code pour Python. Son but est de produire du code formatté de façon propre, en adéquation avec les recommandations. Même si le résultat n'est pas 100% pep8 friendly (il ne change pas les noms de variables ou de fonctions, par exemple), le résultat est simple.

https://github.com/psf/black

"Any style you want, as long as it's black"

Installation

python3 -m pip install black --user

Utilisation

<syntaxhighlight lang="bash"> [justine@localhost Python]$ black JuMonitor.py reformatted JuMonitor.py All done! ✨ 🍰 ✨ 1 file reformatted. </syntaxhighlight>

On a quelques options peu intéressantes.

Ce que ça donne

Avant :

<syntaxhighlight lang='python'>

  1. !/usr/bin/env python3
  2. encoding: utf-8

import os import datetime

def timeNow():

   This function returns current date and time formatted as a string.
   no args
   Returns : now : a str containing "XX month YEAR 12:00:00"
   
   now=datetime.datetime.now().strftime("%d %b %Y %H:%M:%S")
   return now


  1. Getting the args...

args=getArgs()

  1. Tell that we are starting in a clear way

log("#\n#\nSTARTING at {}".format(timeNow()))

  1. Is the launch dynamic or static?

if args.dynamic:

   log("Getting info dynamically as -d has been used. ")
   clients, dead_clients, log_send_interval, send_alerts, mailinfo, interval, interval_dead_clients = dynamicStart()

else:

   log("Configuration information loading from jumonitor.conf")
   clients, dead_clients, log_send_interval, send_alerts, mailinfo, interval, interval_dead_clients=loadConfig()
  1. Send a test mail?

if args.testmail:

   if mailinfo != {}:
       sendTestmail()
   else:
       log("Could not send test mail ! mailinfo are empty") 


      1. THREADS

threadMonitor = threading.Thread(target=monitor, args=(send_alerts, clients, dead_clients, interval, interval_dead_clients, mailinfo)) threadRaise = threading.Thread(target=raiseClient, args=(clients, dead_clients, interval_dead_clients, mailinfo)) threadLogSend = threading.Thread(target=sendLog, args=("jumonitor.log", mailinfo, log_send_interval))

  1. These threads must run together, no RLocks here !

threadMonitor.start() threadRaise.start()

  1. Only send logs if the client wants it

if log_send_interval > 0:

   threadLogSend.start()

</syntaxhighlight>

Après:

<syntaxhighlight lang='python'>

  1. !/usr/bin/env python3
  2. encoding: utf-8

import os import datetime


def timeNow():

   """This function returns current date and time formatted as a string.
   no args
   Returns : now : a str containing "XX month YEAR 12:00:00"
   """
   now = datetime.datetime.now().strftime("%d %b %Y %H:%M:%S")
   return now


  1. Getting the args...

args = getArgs()

  1. Tell that we are starting in a clear way

log("#\n#\nSTARTING at {}".format(timeNow()))

  1. Is the launch dynamic or static?

if args.dynamic:

   log("Getting info dynamically as -d has been used. ")
   clients, dead_clients, log_send_interval, send_alerts, mailinfo, interval, interval_dead_clients = (
       dynamicStart()
   )

else:

   log("Configuration information loading from jumonitor.conf")
   clients, dead_clients, log_send_interval, send_alerts, mailinfo, interval, interval_dead_clients = (
       loadConfig()
   )
  1. Send a test mail?

if args.testmail:

   if mailinfo != {}:
       sendTestmail()
   else:
       log("Could not send test mail ! mailinfo are empty")


      1. THREADS

threadMonitor = threading.Thread(

   target=monitor,
   args=(
       send_alerts,
       clients,
       dead_clients,
       interval,
       interval_dead_clients,
       mailinfo,
   ),

) threadRaise = threading.Thread(

   target=raiseClient, args=(clients, dead_clients, interval_dead_clients, mailinfo)

) threadLogSend = threading.Thread(

   target=sendLog, args=("jumonitor.log", mailinfo, log_send_interval)

)

  1. These threads must run together, no RLocks here !

threadMonitor.start() threadRaise.start()

  1. Only send logs if the client wants it

if log_send_interval > 0:

   threadLogSend.start()

</syntaxhighlight>