Last active
April 11, 2025 21:27
-
-
Save monkut/e60eea811ef085a6540f to your computer and use it in GitHub Desktop.
Revisions
-
monkut revised this gist
Aug 29, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ def valid_datetime_type(arg_datetime_str): if __name__ == '__main__': import argparse parser = argparse.ArgumentParser(description='Example of custom type usage') parser.add_argument('-s', '--start-datetime', dest='start_datetime', type=valid_datetime_type, -
monkut revised this gist
Mar 10, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ def valid_datetime_type(arg_datetime_str): try: return datetime.datetime.strptime(arg_datetime_str, "%Y-%m-%d %H:%M") except ValueError: msg = "Given Datetime ({0}) not valid! Expected format, 'YYYY-MM-DD HH:mm'!".format(arg_datetime_str) raise argparse.ArgumentTypeError(msg) -
monkut revised this gist
Mar 10, 2016 . 1 changed file with 14 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,4 +13,17 @@ def valid_datetime_type(arg_datetime_str): except ValueError: msg = "Given Date ({0}) not valid! Expected format, 'YYYY-MM-DD HH:mm'!".format(arg_datetime_str) raise argparse.ArgumentTypeError(msg) if __name__ == '__main__': import argparse parser = argparse.parser(description='Example of custom type usage') parser.add_argument('-s', '--start-datetime', dest='start_datetime', type=valid_datetime_type, default=None, required=True, help='start datetime in format "YYYY-MM-DD HH:mm"') args = parser.parse_args() start_datetime_object = args.start_datetime print(start_datetime_object) -
monkut revised this gist
Mar 10, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,8 +9,8 @@ def valid_date_type(arg_date_str): def valid_datetime_type(arg_datetime_str): """custom argparse type for user datetime values given from the command line""" try: return datetime.datetime.strptime(arg_datetime_str, "%Y-%m-%d %H:%M") except ValueError: msg = "Given Date ({0}) not valid! Expected format, 'YYYY-MM-DD HH:mm'!".format(arg_datetime_str) raise argparse.ArgumentTypeError(msg)
-
monkut created this gist
Mar 10, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ def valid_date_type(arg_date_str): """custom argparse *date* type for user dates values given from the command line""" try: return datetime.datetime.strptime(arg_date_str, "%Y-%m-%d") except ValueError: msg = "Given Date ({0}) not valid! Expected format, YYYY-MM-DD!".format(arg_date_str) raise argparse.ArgumentTypeError(msg) def valid_datetime_type(arg_datetime_str): """custom argparse type for user datetime values given from the command line""" try: return datetime.datetime.strptime(arg_date_str, "%Y-%m-%d %H:%M") except ValueError: msg = "Given Date ({0}) not valid! Expected format, 'YYYY-MM-DD HH:mm'!".format(arg_date_str) raise argparse.ArgumentTypeError(msg)