diff --git a/exercises/week5/series/sources-solution/main.py b/exercises/week5/series/sources-solution/main.py index c67e12f..4fa51d4 100644 --- a/exercises/week5/series/sources-solution/main.py +++ b/exercises/week5/series/sources-solution/main.py @@ -1,55 +1,63 @@ ################################################################ from __future__ import print_function import sys +import argparse from compute_pi import ComputePi from compute_arithmetic import ComputeArithmetic from print_series import PrintSeries from plot_series import PlotSeries ################################################################ def main(): - argc = len(sys.argv) - if argc < 5: - print('Usage: python3 main.py series_type dumper_type maxiter freq\n\n' - '\tseries_type: pi/arithmetic\n' - '\tdumper_type: print/plot\n' - '\tmaxiter: number of loop iteration to compute the series\n' - '\tfreq: frequency at which dumps/plots are made\n\n') - sys.exit(-1) - - series_type = sys.argv[1] - dumper_type = sys.argv[2] - maxiter = int(sys.argv[3]) - freq = int(sys.argv[4]) - - outfile = "" - if argc == 6: - outfile = sys.argv[5] - + parser = argparse.ArgumentParser( + description='Series exercise in python') + parser.add_argument('-s', '--series_type', type=str, + help=('pi or arithmetic'), + required=True) + parser.add_argument('-d', '--dumper_type', type=str, + help=('print or plot'), + required=True) + parser.add_argument('-n', '--maxiter', type=int, required=True, + help='number of loop iteration to compute the series') + parser.add_argument('-f', '--frequency', type=int, default=1, + help='frequency at which dumps/plots are made') + parser.add_argument('-o', '--outfile', type=str, default="", + help='file where to store the output') + + args = parser.parse_args() + series_type = args.series_type + dumper_type = args.dumper_type + maxiter = args.maxiter + freq = args.frequency + outfile = args.outfile + + # creation of ojects if series_type == "pi": serie_object = ComputePi() elif series_type == "arithmetic": serie_object = ComputeArithmetic() else: raise RuntimeError("unknown series type: " + series_type) if dumper_type == "print": dumper_object = PrintSeries(serie_object, maxiter, freq) elif dumper_type == "plot": dumper_object = PlotSeries(serie_object, maxiter, freq) else: print("unknown dumper type: " + dumper_type) sys.exit(-1) dumper_object.setPrecision(20) + # start of the run if outfile != "": _file = open(outfile, 'w') _file.write(str(dumper_object)) else: print(dumper_object) -main() +if __name__ == "__main__": + main() diff --git a/exercises/week5/series/sujet.pdf b/exercises/week5/series/sujet.pdf index 4470262..a9744de 100644 Binary files a/exercises/week5/series/sujet.pdf and b/exercises/week5/series/sujet.pdf differ