I have a good working function. Pretty long but starts with:
from timer import timer
@timer
def Klasse3():
while True:
jaar = input('Welk jaar wil je zien: ')
try:
jaar = int(jaar)
except ValueError:
print ('Er ging iets mis: het lijkt er op dat je geen jaartal invoerde.')
continue
if 2017 <= jaar < 2020:
print(f'{jaar} is beschikbaar in de database. Een klein moment geduld a.u.b.')
print('Processing request......')
break
else:
print ('Helaas, alleen de jaren 2017,2018 en 2019 zijn beschikbaar.')
etc., etc., etc., which means a lot of code in the middle part and then the finish(first print seems to be out of place (no idea why) but is in script in correct place:
print(f'In {jaar} waren er {abs(df_verschil_dag)} {meer_minder_dag} dagopnames dan in {prev_jaar}.')
print(f'In {jaar} waren er {abs(df_verschil_kort)} {meer_minder_kort} kortdurende opnames dan in {prev_jaar}.')
print(f'In {jaar} waren er {abs(df_verschil_middel)} {meer_minder_middel} middellange opnames dan in {prev_jaar}.')
print(f'In {jaar} waren er {abs(df_verschil_lang)} {meer_minder_lang} langdurige opnames dan in {prev_jaar}.')
print()
print('onderstaand de grafische weergave van wat u hierboven las:')
#groupby resultaat van multi index naar enkel df omzetten
val_cnt3 = val_cnt3.reset_index(name='Totaal')
#kolom met jaar toevoegen
val_cnt3['jaar'] = jaar
#groupby resultaat van multi index naar enkel df omzetten
val_cnt_prev = val_cnt_prev.reset_index(name='Totaal')
#kolom met jaar toevoegen aan voorgaande jaar overzicht
val_cnt_prev['jaar'] = prev_jaar
#beide df's jaar en prev_jaar samenvoegen met concat
frames = [val_cnt3,val_cnt_prev]
result = pd.concat(frames, ignore_index=True)
result.columns=('type', 'totaal', 'jaar')
sns.catplot(x="type", y="totaal", hue='jaar', kind="bar", data=result)
As can be seen in the beginning I use a decorator: @timer:
import sys
import time
import functools
def timer(func):
"""Print the runtime of the decorated function"""
@functools.wraps(func)
def wrapper_timer(*args, **kwargs):
start_time = time.perf_counter() # 1
value = func(*args, **kwargs)
end_time = time.perf_counter() # 2
run_time = end_time - start_time # 3
print(f"Deze functie nam {run_time:.2f} seconden in beslag.")
print('Dank u wel voor het gebruiken van onze online analyse tool. Graag tot een volgende keer.')
return value
return wrapper_timer
Function and decorator work well. No problem in functionality. But..... the seaborn plot (works well as well) is printed after the final prints in my timer decorator. And that is strange, not supposed too. The function should run (inside Timer decorator) and after finishing the complete function, the Timer decorator should print: this function took .... secs and the thank you print.
But instead the final part of Timer function is printed before the SNS plot as can be seen underneath. What can I do (Jupiter Notebook) to get the sns plot printed in the right place? So, the sentence with seconds and the thank you (dank u wel etc. in Dutch) gets printed after the plot?
Deze functie nam 14.08 seconden in beslag.
Dank u wel voor het gebruiken van onze online analyse tool. Graag tot een volgende keer.
matplotlib figures are displayed in a separate thread. Most graphics packages do drawing like that. The timer and printouts in the main thread are not affected by the processing of the display thread.