Commit 5262a67d authored by Roelofsen, Hans's avatar Roelofsen, Hans
Browse files

Merge branch 'hns' into 'master'

progress bar functions for checking input validity

See merge request !2
parents 39608941 9dd79d60
......@@ -3,6 +3,7 @@ Hieronder de helper functies. Elke functie **moet** een docstring hebben waarin
beschreven.
"""
import rasterio as rio
import shapely
import geopandas as gp
import affine
......@@ -11,6 +12,7 @@ import pandas as pd
import random
import numbers
import os
import argparse
def rand_web_color_hex():
......@@ -280,3 +282,87 @@ def mnp_csvlister(folder, notwanted=False, extension=False):
file_list.sort()
return file_list
def progress_bar(iterable, prefix='', suffix='', decimals=1, length=100, fill='#', printEnd="\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
credits: https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console
"""
total = len(iterable)
def printProgressBar(iteration):
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print('\r{0} |{1}| {2}% {3}'.format(prefix, bar, percent, suffix), end=printEnd)
# Initial Call
printProgressBar(0)
# Update Progress Bar
for i, item in enumerate(iterable):
yield item
printProgressBar(i + 1)
# Print New Line on Complete
print()
def geospatial_raster(x):
"""
Is x a geospatial raster?
:param x: file path
:return: x if True else argparse.ArgumentTypeError
"""
if os.path.isfile(x):
try:
rast = rio.open(x)
return x
except(rio.errors.RasterioIOError, rio.errors.FileError, rio._err.CPLE_OpenFailed) as e:
raise argparse.ArgumentTypeError(e)
else:
raise argparse.ArgumentTypeError('Not a file: {0}'.format(x))
def readable_dir(prospective_dir):
"""
Check is a prospective dir is valid and readable.
:param prospective_dir: directory path
:return: prospective dir or argparse.ArgumentTypeError
"""
if not os.path.isdir(prospective_dir):
raise argparse.ArgumentTypeError("readable_dir:{0} is not a valid path".format(prospective_dir))
if os.access(prospective_dir, os.R_OK):
return prospective_dir
else:
raise Exception("readable_dir:{0} is not a readable dir".format(prospective_dir))
def file_of_type(x, ext):
"""
Is x a file of type ext?
:param x: file path
:param ext: extension (without dot)
:return: x if x exists and has required extension, else argparse TypeError
"""
exists = os.path.isfile(x)
ext_match = os.path.splitext(os.path.basename(x))[1] == '.{}'.format(ext)
if exists and ext_match:
return x
if not exists:
raise argparse.ArgumentTypeError('{} is not a file'.format(x))
if not ext_match:
raise argparse.ArgumentTypeError('{} is not a correct file'.format(x))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment