From e6b432af4e3c20adb1b2cce562cb5fab2795226c Mon Sep 17 00:00:00 2001 From: SebF Date: Sun, 10 Oct 2021 18:53:37 +0200 Subject: [PATCH] ajout d'une barre de progression --- osm_vc63/utils.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/osm_vc63/utils.py b/osm_vc63/utils.py index 756ba23..5a2b54f 100644 --- a/osm_vc63/utils.py +++ b/osm_vc63/utils.py @@ -142,7 +142,8 @@ class Utils: def geocodage(self, data): """Renseigne une adresse pour chaque élément de data""" - for element in data["elements"]: + for element in self.progress_bar(data["elements"], prefix="Géocodage"): + if element["type"] == "node": rev_geocode = self.run_reverse_geocoding(element["lat"], element["lon"]) else: @@ -208,3 +209,43 @@ class Utils: element["tags"][tag] = dictionnaire[element["tags"][tag]] return data + + def progress_bar( + # pylint:disable=C0330 + self, + iterable, + decimals=1, + length=50, + prefix="", + fill="█", + print_end="\r", + ): + """ + Call in a loop to create terminal progress bar + @params: + iterable - Required : iterable object (Iterable) + decimals - Optional : positive number of decimals in percent complete (Int) + length - Optional : character length of bar (Int) + prefix - Optional : prefix string (Str) + fill - Optional : bar fill character (Str) + print_end - Optional : end character (e.g. "\r", "\r\n") (Str) + """ + total = len(iterable) + + if total == 0: + return + + # Initial Call + print(f"\r{prefix} |{'-' * length}| {0}%", end=print_end) + # Update Progress Bar + for i, item in enumerate(iterable): + yield item + percent = ("{0:." + str(decimals) + "f}").format( + 100 * ((i + 1) / float(total)) + ) + filled = int(length * (i + 1) // total) + progress = fill * filled + "-" * (length - filled) + print(f"\r{prefix} |{progress}| {percent}%", end=print_end) + + # Print New Line on Complete + print()