Python : Black
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.
"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'>
- !/usr/bin/env python3
- 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
- Getting the args...
args=getArgs()
- Tell that we are starting in a clear way
log("#\n#\nSTARTING at {}".format(timeNow()))
- 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()
- Send a test mail?
if args.testmail:
if mailinfo != {}: sendTestmail() else: log("Could not send test mail ! mailinfo are empty")
- 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))
- These threads must run together, no RLocks here !
threadMonitor.start() threadRaise.start()
- Only send logs if the client wants it
if log_send_interval > 0:
threadLogSend.start()
</syntaxhighlight>
Après:
<syntaxhighlight lang='python'>
- !/usr/bin/env python3
- 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
- Getting the args...
args = getArgs()
- Tell that we are starting in a clear way
log("#\n#\nSTARTING at {}".format(timeNow()))
- 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() )
- Send a test mail?
if args.testmail:
if mailinfo != {}: sendTestmail() else: log("Could not send test mail ! mailinfo are empty")
- 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)
)
- These threads must run together, no RLocks here !
threadMonitor.start() threadRaise.start()
- Only send logs if the client wants it
if log_send_interval > 0:
threadLogSend.start()
</syntaxhighlight>