From f928969fa58b3675564460847eb1e2690369499e Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Fri, 23 Aug 2019 22:49:39 +0530 Subject: [PATCH 01/14] parser file added --- .../ayush-shah/argument-parser/parser.py | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 solutions/ayush-shah/argument-parser/parser.py diff --git a/solutions/ayush-shah/argument-parser/parser.py b/solutions/ayush-shah/argument-parser/parser.py new file mode 100644 index 00000000..1a8c9d51 --- /dev/null +++ b/solutions/ayush-shah/argument-parser/parser.py @@ -0,0 +1,96 @@ +import sys +Integer, String, Others, Required = set(), set(), set(), set() +errors, Json = dict(), dict() +class Parser: + def addArgument(self, argument, required, types): + if types == 'integer': + Integer.add(argument) + if required == 'yes': + Required.add(argument) + elif types == 'string': + String.add(argument) + if required == 'yes': + Required.add(argument) + else: + Others.add(argument) + if required == 'yes': + Required.add(argument) + def storeError(self, key, error): + errors.setdefault(key, []).append(error) + def storeResult(self, key, value): + Json[key] = value + def checkRequired(self, keys): + for check in Required: + flag = False + for key in keys: + if key == check: + flag = True + if not flag: + return False,check + return True, "ok" + def displayError(self): + for key in errors: + for error in errors[key]: + if error == "no-value" and not(key == "--local" or key == "--remote"): + print("Error: The value for argument '"+key+"' is missing") + if error == "required": + print("Error: The argument'"+key+"' is required, but missing from input") + if error == "local and remote": + print("Error: The --local and --remote arguments cannot be used together") + if error == "not-int": + print("Error: The value for argument '"+key+"' must be integer") + if error == "not-string": + print("Error: The value for argument '"+key+"' must be string") + if error == "invalid-argument": + print("Error: invalid argument "+key) + def displayResult(self): + print('{') + for key in Json: + print("'"+key+"' : '"+Json[key]+"',") + print('}') + +def main(): + parse = Parser() + parse.addArgument('--key', 'yes', 'integer') + parse.addArgument('--name', 'no', 'string') + parse.addArgument('--local', 'no', 'others') + parse.addArgument('--remote', 'no', 'others') + length_of_arguments = len(sys.argv) + if length_of_arguments == 1: + print("Error: no arguments given in input") + return 0 + check = False + keys = list() + for arguments in range(1, length_of_arguments): + args = sys.argv[arguments] + key = args.partition('=')[0] + value = args.partition('=')[2] + keys.append(key) + if key != "--remote" and key != "--local": + parse.storeResult(key, value) + if (key not in Integer) and (key not in String) and(key not in Others): + parse.storeError(key, "invalid-argument") + if '=' not in args: + parse.storeError(key, "no-value") + if key in Integer: + if not value.isdigit(): + parse.storeError(key, "not-int") + if key in String: + if not value.isalpha(): + parse.storeError(key, "not-string") + if key == '--local' and not check: + check = True + elif key == '--remote' and not check: + check = True + elif check and (key == '--local' or key == '--remote'): + parse.storeError(key, "local and remote") + response, key = parse.checkRequired(keys) + if not response: + parse.storeError(key, "required") + if bool(errors): + parse.displayError() + else: + parse.displayResult() + +if __name__ == '__main__': + main() \ No newline at end of file From ed3124c69bdd5d4edd2dfb2ef72dbd14e9b60c94 Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Sat, 24 Aug 2019 00:06:08 +0530 Subject: [PATCH 02/14] Updated parser.py --- .../ayush-shah/argument-parser/README.md | 2 + .../ayush-shah/argument-parser/parser.py | 62 ++++++++++--------- solutions/ayush-shah/argument-parser/test.py | 0 3 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 solutions/ayush-shah/argument-parser/README.md create mode 100644 solutions/ayush-shah/argument-parser/test.py diff --git a/solutions/ayush-shah/argument-parser/README.md b/solutions/ayush-shah/argument-parser/README.md new file mode 100644 index 00000000..65afe604 --- /dev/null +++ b/solutions/ayush-shah/argument-parser/README.md @@ -0,0 +1,2 @@ +# Examples + diff --git a/solutions/ayush-shah/argument-parser/parser.py b/solutions/ayush-shah/argument-parser/parser.py index 1a8c9d51..e0d32744 100644 --- a/solutions/ayush-shah/argument-parser/parser.py +++ b/solutions/ayush-shah/argument-parser/parser.py @@ -1,8 +1,9 @@ import sys Integer, String, Others, Required = set(), set(), set(), set() errors, Json = dict(), dict() -class Parser: - def addArgument(self, argument, required, types): +class Parser: + """Class for adding the arguments, storing errors, storing result and displaying them""" + def add_argument(self, argument, required, types): if types == 'integer': Integer.add(argument) if required == 'yes': @@ -15,23 +16,23 @@ def addArgument(self, argument, required, types): Others.add(argument) if required == 'yes': Required.add(argument) - def storeError(self, key, error): + def store_error(self, key, error): errors.setdefault(key, []).append(error) - def storeResult(self, key, value): + def store_result(self, key, value): Json[key] = value - def checkRequired(self, keys): + def check_required(self, keys): for check in Required: flag = False for key in keys: if key == check: flag = True if not flag: - return False,check + return False, check return True, "ok" - def displayError(self): + def display_error(self): for key in errors: for error in errors[key]: - if error == "no-value" and not(key == "--local" or key == "--remote"): + if error == "no-value" and key not in ("--local", "--remote"): print("Error: The value for argument '"+key+"' is missing") if error == "required": print("Error: The argument'"+key+"' is required, but missing from input") @@ -43,54 +44,55 @@ def displayError(self): print("Error: The value for argument '"+key+"' must be string") if error == "invalid-argument": print("Error: invalid argument "+key) - def displayResult(self): + def display_result(self): print('{') for key in Json: print("'"+key+"' : '"+Json[key]+"',") print('}') -def main(): +def main(argument): parse = Parser() - parse.addArgument('--key', 'yes', 'integer') - parse.addArgument('--name', 'no', 'string') - parse.addArgument('--local', 'no', 'others') - parse.addArgument('--remote', 'no', 'others') - length_of_arguments = len(sys.argv) + parse.add_argument('--key', 'yes', 'integer') + parse.add_argument('--name', 'no', 'string') + parse.add_argument('--local', 'no', 'others') + parse.add_argument('--remote', 'no', 'others') + length_of_arguments = len(argument) if length_of_arguments == 1: print("Error: no arguments given in input") return 0 check = False keys = list() for arguments in range(1, length_of_arguments): - args = sys.argv[arguments] + args = argument[arguments] key = args.partition('=')[0] value = args.partition('=')[2] keys.append(key) - if key != "--remote" and key != "--local": - parse.storeResult(key, value) + if key not in ("--remote", "--local"): + parse.store_result(key, value) if (key not in Integer) and (key not in String) and(key not in Others): - parse.storeError(key, "invalid-argument") + parse.store_error(key, "invalid-argument") if '=' not in args: - parse.storeError(key, "no-value") + parse.store_error(key, "no-value") if key in Integer: if not value.isdigit(): - parse.storeError(key, "not-int") + parse.store_error(key, "not-int") if key in String: if not value.isalpha(): - parse.storeError(key, "not-string") + parse.store_error(key, "not-string") if key == '--local' and not check: check = True elif key == '--remote' and not check: check = True - elif check and (key == '--local' or key == '--remote'): - parse.storeError(key, "local and remote") - response, key = parse.checkRequired(keys) + elif check and key in ('--local', '--remote'): + parse.store_error(key, "local and remote") + response, key = parse.check_required(keys) if not response: - parse.storeError(key, "required") + parse.store_error(key, "required") if bool(errors): - parse.displayError() + parse.display_error() else: - parse.displayResult() - + parse.display_result() + if __name__ == '__main__': - main() \ No newline at end of file + main(sys.argv) + \ No newline at end of file diff --git a/solutions/ayush-shah/argument-parser/test.py b/solutions/ayush-shah/argument-parser/test.py new file mode 100644 index 00000000..e69de29b From a11a9f5cac2e92ea5be05fecf65c3677c65f0d28 Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Sat, 24 Aug 2019 09:32:06 +0530 Subject: [PATCH 03/14] syntax of parser file modified --- solutions/ayush-shah/argument-parser/parser.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/solutions/ayush-shah/argument-parser/parser.py b/solutions/ayush-shah/argument-parser/parser.py index e0d32744..64cdd2ba 100644 --- a/solutions/ayush-shah/argument-parser/parser.py +++ b/solutions/ayush-shah/argument-parser/parser.py @@ -1,8 +1,8 @@ import sys Integer, String, Others, Required = set(), set(), set(), set() errors, Json = dict(), dict() -class Parser: - """Class for adding the arguments, storing errors, storing result and displaying them""" +class Parser: + """Class for adding the command line arguments, storing errors, storing results and displaying them""" def add_argument(self, argument, required, types): if types == 'integer': Integer.add(argument) @@ -35,15 +35,15 @@ def display_error(self): if error == "no-value" and key not in ("--local", "--remote"): print("Error: The value for argument '"+key+"' is missing") if error == "required": - print("Error: The argument'"+key+"' is required, but missing from input") + print("Error: The argument '"+key+"' is required, but missing from input") if error == "local and remote": - print("Error: The --local and --remote arguments cannot be used together") + print("Error: The '--local' and '--remote' arguments cannot be used together") if error == "not-int": print("Error: The value for argument '"+key+"' must be integer") if error == "not-string": print("Error: The value for argument '"+key+"' must be string") if error == "invalid-argument": - print("Error: invalid argument "+key) + print("Error: invalid argument '"+key+"'") def display_result(self): print('{') for key in Json: @@ -95,4 +95,4 @@ def main(argument): if __name__ == '__main__': main(sys.argv) - \ No newline at end of file + From 74ea32f85cc170baf03a85c9d3d19e55d4872b48 Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Sat, 24 Aug 2019 09:33:03 +0530 Subject: [PATCH 04/14] Examples added --- solutions/ayush-shah/argument-parser/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/solutions/ayush-shah/argument-parser/README.md b/solutions/ayush-shah/argument-parser/README.md index 65afe604..e99fbbd8 100644 --- a/solutions/ayush-shah/argument-parser/README.md +++ b/solutions/ayush-shah/argument-parser/README.md @@ -1,2 +1,20 @@ # Examples +`$ python3 parser.py --key=1234 --name=ayush + { + 'key': '1234', + 'name': 'ayush', + } + + $ python3 parser.py --key=ayush + Error: The value for argument '--key' must be integer + + $ python3 parser.py --name=ayush + Error: The argument '--key' is required, but missing from input + + $ python3 parser.py --local --remote + Error: The '--local' and '--remote' arguments cannot be used together + Error: The argument '--key' is required, but missing from input + + $ python3 parser.py --key=1234 --roll=10014 + Error: invalid argument '--roll'` From 9516e8b8c8086fe81a9bde8761dae6b6097166df Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Sat, 24 Aug 2019 09:36:46 +0530 Subject: [PATCH 05/14] modified README --- .../ayush-shah/argument-parser/README.md | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/solutions/ayush-shah/argument-parser/README.md b/solutions/ayush-shah/argument-parser/README.md index e99fbbd8..8ffc94e0 100644 --- a/solutions/ayush-shah/argument-parser/README.md +++ b/solutions/ayush-shah/argument-parser/README.md @@ -1,20 +1,20 @@ # Examples -`$ python3 parser.py --key=1234 --name=ayush - { - 'key': '1234', - 'name': 'ayush', - } +`$ python3 parser.py --key=1234 --name=ayush` + `{` + `'key': '1234',` + `'name': 'ayush',` + `}` - $ python3 parser.py --key=ayush - Error: The value for argument '--key' must be integer +`$ python3 parser.py --key=ayush` + `Error: The value for argument '--key' must be integer` - $ python3 parser.py --name=ayush - Error: The argument '--key' is required, but missing from input +`$ python3 parser.py --name=ayush` + `Error: The argument '--key' is required, but missing from input` - $ python3 parser.py --local --remote - Error: The '--local' and '--remote' arguments cannot be used together - Error: The argument '--key' is required, but missing from input +`$ python3 parser.py --local --remote` + `Error: The '--local' and '--remote' arguments cannot be used together` + `Error: The argument '--key' is required, but missing from input` - $ python3 parser.py --key=1234 --roll=10014 - Error: invalid argument '--roll'` +`$ python3 parser.py --key=1234 --roll=10014` + `Error: invalid argument '--roll'` From d18f5848cc70a0eab0dba6b0ff4f7b716a2c18c8 Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Sat, 24 Aug 2019 09:55:50 +0530 Subject: [PATCH 06/14] modified README --- .../ayush-shah/argument-parser/README.md | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/solutions/ayush-shah/argument-parser/README.md b/solutions/ayush-shah/argument-parser/README.md index 8ffc94e0..bceef9af 100644 --- a/solutions/ayush-shah/argument-parser/README.md +++ b/solutions/ayush-shah/argument-parser/README.md @@ -1,20 +1,27 @@ # Examples -`$ python3 parser.py --key=1234 --name=ayush` - `{` - `'key': '1234',` - `'name': 'ayush',` - `}` -`$ python3 parser.py --key=ayush` - `Error: The value for argument '--key' must be integer` - -`$ python3 parser.py --name=ayush` - `Error: The argument '--key' is required, but missing from input` - -`$ python3 parser.py --local --remote` - `Error: The '--local' and '--remote' arguments cannot be used together` - `Error: The argument '--key' is required, but missing from input` - -`$ python3 parser.py --key=1234 --roll=10014` - `Error: invalid argument '--roll'` +`$ python3 parser.py --key=1234 --name=ayush ` +` { ` +` 'key': '1234', ` +` 'name': 'ayush', ` +` } ` +` ` +`$ python3 parser.py --key=ayush ` +` Error: The value for argument '--key' must be integer ` +` ` +`$ python3 parser.py --name=ayush ` +` Error: The argument '--key' is required, but missing from input ` +` ` +`$ python3 parser.py ` +` Error: The argument '--key' is required, but missing from input ` +` ` +`$ python3 parser.py --local --remote ` +` Error: The '--local' and '--remote' arguments cannot be used together ` +` Error: The argument '--key' is required, but missing from input ` +` ` +`$ python3 parser.py --key=1234 --roll=10014 ` +` Error: invalid argument '--roll' ` +` ` +`$ python3 parser.py --key ` +` Error: The value for argument '--key' is missing ` From 3d563917b75e543a05cc086eab3793a89352bb23 Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Sat, 24 Aug 2019 09:57:26 +0530 Subject: [PATCH 07/14] modified README --- solutions/ayush-shah/argument-parser/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/ayush-shah/argument-parser/README.md b/solutions/ayush-shah/argument-parser/README.md index bceef9af..a44361ed 100644 --- a/solutions/ayush-shah/argument-parser/README.md +++ b/solutions/ayush-shah/argument-parser/README.md @@ -1,5 +1,5 @@ # Examples - +``` `$ python3 parser.py --key=1234 --name=ayush ` ` { ` ` 'key': '1234', ` @@ -24,4 +24,4 @@ ` ` `$ python3 parser.py --key ` ` Error: The value for argument '--key' is missing ` - +``` From b509bc00e5e2729333fda91d746882af66fc61b0 Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Sat, 24 Aug 2019 09:59:34 +0530 Subject: [PATCH 08/14] modified README --- .../ayush-shah/argument-parser/README.md | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/solutions/ayush-shah/argument-parser/README.md b/solutions/ayush-shah/argument-parser/README.md index a44361ed..fcf00991 100644 --- a/solutions/ayush-shah/argument-parser/README.md +++ b/solutions/ayush-shah/argument-parser/README.md @@ -1,27 +1,27 @@ # Examples ``` -`$ python3 parser.py --key=1234 --name=ayush ` -` { ` -` 'key': '1234', ` -` 'name': 'ayush', ` -` } ` -` ` -`$ python3 parser.py --key=ayush ` -` Error: The value for argument '--key' must be integer ` -` ` -`$ python3 parser.py --name=ayush ` -` Error: The argument '--key' is required, but missing from input ` -` ` -`$ python3 parser.py ` -` Error: The argument '--key' is required, but missing from input ` -` ` -`$ python3 parser.py --local --remote ` -` Error: The '--local' and '--remote' arguments cannot be used together ` -` Error: The argument '--key' is required, but missing from input ` -` ` -`$ python3 parser.py --key=1234 --roll=10014 ` -` Error: invalid argument '--roll' ` -` ` -`$ python3 parser.py --key ` -` Error: The value for argument '--key' is missing ` +$ python3 parser.py --key=1234 --name=ayush + { + 'key': '1234', + 'name': 'ayush', + } + +$ python3 parser.py --key=ayush + Error: The value for argument '--key' must be integer + +$ python3 parser.py --name=ayush + Error: The argument '--key' is required, but missing from input + +$ python3 parser.py + Error: The argument '--key' is required, but missing from input + +$ python3 parser.py --local --remote + Error: The '--local' and '--remote' arguments cannot be used together + Error: The argument '--key' is required, but missing from input + +$ python3 parser.py --key=1234 --roll=10014 + Error: invalid argument '--roll' + +$ python3 parser.py --key + Error: The value for argument '--key' is missing ``` From d6eb0a902f0ae7d79934a125e82aa3619d0fce99 Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Sun, 25 Aug 2019 02:07:57 +0530 Subject: [PATCH 09/14] modified files --- solutions/ayush-shah/argument-parser/parser.py | 17 +++++++++-------- solutions/ayush-shah/argument-parser/test.py | 0 2 files changed, 9 insertions(+), 8 deletions(-) delete mode 100644 solutions/ayush-shah/argument-parser/test.py diff --git a/solutions/ayush-shah/argument-parser/parser.py b/solutions/ayush-shah/argument-parser/parser.py index 64cdd2ba..e75306a5 100644 --- a/solutions/ayush-shah/argument-parser/parser.py +++ b/solutions/ayush-shah/argument-parser/parser.py @@ -32,6 +32,8 @@ def check_required(self, keys): def display_error(self): for key in errors: for error in errors[key]: + if error == "invalid-argument": + print("Error: invalid argument '"+key+"'") if error == "no-value" and key not in ("--local", "--remote"): print("Error: The value for argument '"+key+"' is missing") if error == "required": @@ -42,8 +44,6 @@ def display_error(self): print("Error: The value for argument '"+key+"' must be integer") if error == "not-string": print("Error: The value for argument '"+key+"' must be string") - if error == "invalid-argument": - print("Error: invalid argument '"+key+"'") def display_result(self): print('{') for key in Json: @@ -71,6 +71,12 @@ def main(argument): parse.store_result(key, value) if (key not in Integer) and (key not in String) and(key not in Others): parse.store_error(key, "invalid-argument") + if key == '--local' and not check: + check = True + elif key == '--remote' and not check: + check = True + elif check and key in ('--local', '--remote'): + parse.store_error(key, "local and remote") if '=' not in args: parse.store_error(key, "no-value") if key in Integer: @@ -79,12 +85,7 @@ def main(argument): if key in String: if not value.isalpha(): parse.store_error(key, "not-string") - if key == '--local' and not check: - check = True - elif key == '--remote' and not check: - check = True - elif check and key in ('--local', '--remote'): - parse.store_error(key, "local and remote") + response, key = parse.check_required(keys) if not response: parse.store_error(key, "required") diff --git a/solutions/ayush-shah/argument-parser/test.py b/solutions/ayush-shah/argument-parser/test.py deleted file mode 100644 index e69de29b..00000000 From 283091feba16d99beb41ab70eaf84473036114e9 Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Thu, 5 Sep 2019 00:19:45 +0530 Subject: [PATCH 10/14] Unit testing added --- .../ayush-shah/argument-parser/.gitignore | 1 + .../ayush-shah/argument-parser/README.md | 5 +- .../ayush-shah/argument-parser/parser.py | 133 +++++++++--------- solutions/ayush-shah/argument-parser/test.py | 48 +++++++ 4 files changed, 114 insertions(+), 73 deletions(-) create mode 100644 solutions/ayush-shah/argument-parser/.gitignore create mode 100644 solutions/ayush-shah/argument-parser/test.py diff --git a/solutions/ayush-shah/argument-parser/.gitignore b/solutions/ayush-shah/argument-parser/.gitignore new file mode 100644 index 00000000..0d20b648 --- /dev/null +++ b/solutions/ayush-shah/argument-parser/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/solutions/ayush-shah/argument-parser/README.md b/solutions/ayush-shah/argument-parser/README.md index fcf00991..6b3664de 100644 --- a/solutions/ayush-shah/argument-parser/README.md +++ b/solutions/ayush-shah/argument-parser/README.md @@ -1,10 +1,7 @@ # Examples ``` $ python3 parser.py --key=1234 --name=ayush - { - 'key': '1234', - 'name': 'ayush', - } + { "--key": "1234, "--name": "ayush" } $ python3 parser.py --key=ayush Error: The value for argument '--key' must be integer diff --git a/solutions/ayush-shah/argument-parser/parser.py b/solutions/ayush-shah/argument-parser/parser.py index e75306a5..c301ea93 100644 --- a/solutions/ayush-shah/argument-parser/parser.py +++ b/solutions/ayush-shah/argument-parser/parser.py @@ -1,27 +1,33 @@ import sys -Integer, String, Others, Required = set(), set(), set(), set() -errors, Json = dict(), dict() +import json + class Parser: - """Class for adding the command line arguments, storing errors, storing results and displaying them""" + """ + Class for adding the command line arguments + storing errors, storing results and displaying them + """ + Integer, String, Others, Required = set(), set(), set(), set() + Json = dict() + def add_argument(self, argument, required, types): if types == 'integer': - Integer.add(argument) + self.Integer.add(argument) if required == 'yes': - Required.add(argument) + self.Required.add(argument) elif types == 'string': - String.add(argument) + self.String.add(argument) if required == 'yes': - Required.add(argument) + self.Required.add(argument) else: - Others.add(argument) + self.Others.add(argument) if required == 'yes': - Required.add(argument) - def store_error(self, key, error): - errors.setdefault(key, []).append(error) + self.Required.add(argument) + def store_result(self, key, value): - Json[key] = value + self.Json[key] = value + def check_required(self, keys): - for check in Required: + for check in self.Required: flag = False for key in keys: if key == check: @@ -29,71 +35,60 @@ def check_required(self, keys): if not flag: return False, check return True, "ok" - def display_error(self): - for key in errors: - for error in errors[key]: - if error == "invalid-argument": - print("Error: invalid argument '"+key+"'") - if error == "no-value" and key not in ("--local", "--remote"): - print("Error: The value for argument '"+key+"' is missing") - if error == "required": - print("Error: The argument '"+key+"' is required, but missing from input") - if error == "local and remote": - print("Error: The '--local' and '--remote' arguments cannot be used together") - if error == "not-int": - print("Error: The value for argument '"+key+"' must be integer") - if error == "not-string": - print("Error: The value for argument '"+key+"' must be string") - def display_result(self): - print('{') - for key in Json: - print("'"+key+"' : '"+Json[key]+"',") - print('}') -def main(argument): - parse = Parser() - parse.add_argument('--key', 'yes', 'integer') - parse.add_argument('--name', 'no', 'string') - parse.add_argument('--local', 'no', 'others') - parse.add_argument('--remote', 'no', 'others') - length_of_arguments = len(argument) - if length_of_arguments == 1: - print("Error: no arguments given in input") - return 0 - check = False - keys = list() - for arguments in range(1, length_of_arguments): - args = argument[arguments] - key = args.partition('=')[0] - value = args.partition('=')[2] - keys.append(key) - if key not in ("--remote", "--local"): - parse.store_result(key, value) - if (key not in Integer) and (key not in String) and(key not in Others): - parse.store_error(key, "invalid-argument") + def check_local_and_remote(self, argument): + length_of_arguments = len(argument) + check = False + for arguments in range(1, length_of_arguments): + args = argument[arguments] + key = args.partition('=')[0] if key == '--local' and not check: check = True elif key == '--remote' and not check: check = True elif check and key in ('--local', '--remote'): - parse.store_error(key, "local and remote") + return True + return False + + def display_result(self): + to_json = json.dumps(self.Json) + return to_json + + def main(self, argument): + self.add_argument('--key', 'yes', 'integer') + self.add_argument('--name', 'no', 'string') + self.add_argument('--local', 'no', 'others') + self.add_argument('--remote', 'no', 'others') + length_of_arguments = len(argument) + if length_of_arguments == 1: + return "Error: no arguments given in input" + if self.check_local_and_remote(argument): + return "Error: The '--local' and '--remote' arguments cannot be used together" + keys = list() + for arguments in range(1, length_of_arguments): + args = argument[arguments] + key = args.partition('=')[0] + value = args.partition('=')[2] + keys.append(key) + if key not in ("--remote", "--local"): + self.store_result(key, value) + if (key not in self.Integer) and (key not in self.String) and(key not in self.Others): + return "Error: invalid argument '" + key + "'" if '=' not in args: - parse.store_error(key, "no-value") - if key in Integer: + return "Error: The value for argument '" + key + "' is missing" + if key in self.Integer: if not value.isdigit(): - parse.store_error(key, "not-int") - if key in String: + return "Error: The value for argument '" + key + "' must be integer" + if key in self.String: if not value.isalpha(): - parse.store_error(key, "not-string") + return "Error: The value for argument '" + key + "' must be string" - response, key = parse.check_required(keys) - if not response: - parse.store_error(key, "required") - if bool(errors): - parse.display_error() - else: - parse.display_result() + response, key = self.check_required(keys) + if not response: + return "Error : argument '" + key + "' is required but missing" + return self.display_result() if __name__ == '__main__': - main(sys.argv) - + Parse = Parser() + Parse.main(sys.argv) + \ No newline at end of file diff --git a/solutions/ayush-shah/argument-parser/test.py b/solutions/ayush-shah/argument-parser/test.py new file mode 100644 index 00000000..4a36e034 --- /dev/null +++ b/solutions/ayush-shah/argument-parser/test.py @@ -0,0 +1,48 @@ +import unittest +import parser as parse + +class UnitTesting(unittest.TestCase): + parser = parse.Parser() + def test_basic_usage(self): + command_line_parameters = ['./test', '--key=1234', '--name=ayush'] + result = self.parser.main(command_line_parameters) + self.assertEqual(result, '{"--key": "1234", "--name": "ayush"}') + + def test_invalid_key(self): + command_line_parameters = ['./test', '--key=ayush', '--name=ayush'] + result = self.parser.main(command_line_parameters) + self.assertEqual(result, "Error: The value for argument '--key' must be integer") + + def test_required_argument(self): + command_line_parameters = ['./test', '--name=ayush'] + result = self.parser.main(command_line_parameters) + self.assertEqual(result, "Error : argument '--key' is required but missing") + + def test_invalid_name(self): + command_line_parameters = ['./test', '--key=1234', '--name=1234'] + result = self.parser.main(command_line_parameters) + self.assertEqual(result, "Error: The value for argument '--name' must be string") + + def test_no_arguments(self): + command_line_parameters = ['./test'] + result = self.parser.main(command_line_parameters) + self.assertEqual(result, "Error: no arguments given in input") + + def test_local_and_remote(self): + command_line_parameters = ['./test', '--local', '--remote'] + result = self.parser.main(command_line_parameters) + self.assertEqual(result, "Error: The '--local' and '--remote' arguments cannot be used together") + + def test_unrecognized_command(self): + command_line_parameters = ['./test', '--roll'] + result = self.parser.main(command_line_parameters) + self.assertEqual(result, "Error: invalid argument '--roll'") + + def test_valueless_argument(self): + command_line_parameters = ['./test', '--key'] + result = self.parser.main(command_line_parameters) + self.assertEqual(result, "Error: The value for argument '--key' is missing") + + +if __name__ == '__main__': + unittest.main() From d8568a089d396a29879f39f6f3bd174bdf7052cb Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Thu, 5 Sep 2019 00:26:27 +0530 Subject: [PATCH 11/14] README modified --- solutions/ayush-shah/argument-parser/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solutions/ayush-shah/argument-parser/README.md b/solutions/ayush-shah/argument-parser/README.md index 6b3664de..91f95ba3 100644 --- a/solutions/ayush-shah/argument-parser/README.md +++ b/solutions/ayush-shah/argument-parser/README.md @@ -13,8 +13,7 @@ $ python3 parser.py Error: The argument '--key' is required, but missing from input $ python3 parser.py --local --remote - Error: The '--local' and '--remote' arguments cannot be used together - Error: The argument '--key' is required, but missing from input + Error: The '--local' and '--remote' arguments cannot be used together $ python3 parser.py --key=1234 --roll=10014 Error: invalid argument '--roll' From 1097ea5107ef7f6d150f3084097394fe5562da8d Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Fri, 6 Sep 2019 19:52:22 +0530 Subject: [PATCH 12/14] Arguments added in test.py --- solutions/ayush-shah/argument-parser/parser.py | 9 --------- solutions/ayush-shah/argument-parser/test.py | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/solutions/ayush-shah/argument-parser/parser.py b/solutions/ayush-shah/argument-parser/parser.py index c301ea93..cba2cb7c 100644 --- a/solutions/ayush-shah/argument-parser/parser.py +++ b/solutions/ayush-shah/argument-parser/parser.py @@ -1,4 +1,3 @@ -import sys import json class Parser: @@ -55,10 +54,6 @@ def display_result(self): return to_json def main(self, argument): - self.add_argument('--key', 'yes', 'integer') - self.add_argument('--name', 'no', 'string') - self.add_argument('--local', 'no', 'others') - self.add_argument('--remote', 'no', 'others') length_of_arguments = len(argument) if length_of_arguments == 1: return "Error: no arguments given in input" @@ -88,7 +83,3 @@ def main(self, argument): return "Error : argument '" + key + "' is required but missing" return self.display_result() -if __name__ == '__main__': - Parse = Parser() - Parse.main(sys.argv) - \ No newline at end of file diff --git a/solutions/ayush-shah/argument-parser/test.py b/solutions/ayush-shah/argument-parser/test.py index 4a36e034..8e368a6a 100644 --- a/solutions/ayush-shah/argument-parser/test.py +++ b/solutions/ayush-shah/argument-parser/test.py @@ -4,41 +4,58 @@ class UnitTesting(unittest.TestCase): parser = parse.Parser() def test_basic_usage(self): + self.parser.add_argument('--key', 'yes', 'integer') + self.parser.add_argument('--name', 'no', 'string') command_line_parameters = ['./test', '--key=1234', '--name=ayush'] result = self.parser.main(command_line_parameters) self.assertEqual(result, '{"--key": "1234", "--name": "ayush"}') def test_invalid_key(self): + self.parser.add_argument('--key', 'yes', 'integer') + self.parser.add_argument('--name', 'no', 'string') command_line_parameters = ['./test', '--key=ayush', '--name=ayush'] result = self.parser.main(command_line_parameters) self.assertEqual(result, "Error: The value for argument '--key' must be integer") def test_required_argument(self): + self.parser.add_argument('--key', 'yes', 'integer') + self.parser.add_argument('--name', 'no', 'string') command_line_parameters = ['./test', '--name=ayush'] result = self.parser.main(command_line_parameters) self.assertEqual(result, "Error : argument '--key' is required but missing") def test_invalid_name(self): + self.parser.add_argument('--key', 'yes', 'integer') + self.parser.add_argument('--name', 'no', 'string') command_line_parameters = ['./test', '--key=1234', '--name=1234'] result = self.parser.main(command_line_parameters) self.assertEqual(result, "Error: The value for argument '--name' must be string") def test_no_arguments(self): + self.parser.add_argument('--key', 'yes', 'integer') + self.parser.add_argument('--name', 'no', 'string') command_line_parameters = ['./test'] result = self.parser.main(command_line_parameters) self.assertEqual(result, "Error: no arguments given in input") def test_local_and_remote(self): + self.parser.add_argument('--local', 'no', 'others') + self.parser.add_argument('--remote', 'no', 'others') command_line_parameters = ['./test', '--local', '--remote'] result = self.parser.main(command_line_parameters) self.assertEqual(result, "Error: The '--local' and '--remote' arguments cannot be used together") def test_unrecognized_command(self): + self.parser.add_argument('--key', 'yes', 'integer') + self.parser.add_argument('--name', 'no', 'string') + self.parser.add_argument('--local', 'no', 'others') + self.parser.add_argument('--remote', 'no', 'others') command_line_parameters = ['./test', '--roll'] result = self.parser.main(command_line_parameters) self.assertEqual(result, "Error: invalid argument '--roll'") def test_valueless_argument(self): + self.parser.add_argument('--key', 'yes', 'integer') command_line_parameters = ['./test', '--key'] result = self.parser.main(command_line_parameters) self.assertEqual(result, "Error: The value for argument '--key' is missing") From b1774fb9d78bebb8ca5d67e7396669b564907cb6 Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Fri, 11 Oct 2019 15:22:16 +0530 Subject: [PATCH 13/14] Added exceptions --- .../ayush-shah/argument-parser/parser.py | 75 +++++++++++------- solutions/ayush-shah/argument-parser/test.py | 78 +++++++++---------- 2 files changed, 86 insertions(+), 67 deletions(-) diff --git a/solutions/ayush-shah/argument-parser/parser.py b/solutions/ayush-shah/argument-parser/parser.py index cba2cb7c..a4358e8f 100644 --- a/solutions/ayush-shah/argument-parser/parser.py +++ b/solutions/ayush-shah/argument-parser/parser.py @@ -1,38 +1,50 @@ import json +class ParseError(Exception): + """ + Class for raising exceptions + """ + + def __init__(self, message): + super(ParseError, self).__init__(message) + +class Options(object): + """ + Class for storing argument information + """ + + def __init__(self, argument, types, required): + self.argument = argument + self.types = types + self.required = required + class Parser: """ Class for adding the command line arguments storing errors, storing results and displaying them """ - Integer, String, Others, Required = set(), set(), set(), set() - Json = dict() + + def __init__(self): + self.arguments = [] + self.Json = {} def add_argument(self, argument, required, types): - if types == 'integer': - self.Integer.add(argument) - if required == 'yes': - self.Required.add(argument) - elif types == 'string': - self.String.add(argument) - if required == 'yes': - self.Required.add(argument) - else: - self.Others.add(argument) - if required == 'yes': - self.Required.add(argument) + add = Options(argument, types, required) + self.arguments.append(add) def store_result(self, key, value): self.Json[key] = value def check_required(self, keys): - for check in self.Required: + for check in self.arguments: flag = False + if check.required == False: + continue for key in keys: - if key == check: + if key == check.argument and check.required == True: flag = True if not flag: - return False, check + return False, check.argument return True, "ok" def check_local_and_remote(self, argument): @@ -56,9 +68,9 @@ def display_result(self): def main(self, argument): length_of_arguments = len(argument) if length_of_arguments == 1: - return "Error: no arguments given in input" + raise ParseError("Error: no arguments given in input") if self.check_local_and_remote(argument): - return "Error: The '--local' and '--remote' arguments cannot be used together" + raise ParseError("Error: The '--local' and '--remote' arguments cannot be used together") keys = list() for arguments in range(1, length_of_arguments): args = argument[arguments] @@ -67,19 +79,26 @@ def main(self, argument): keys.append(key) if key not in ("--remote", "--local"): self.store_result(key, value) - if (key not in self.Integer) and (key not in self.String) and(key not in self.Others): - return "Error: invalid argument '" + key + "'" + flag = False + for obj in self.arguments: + if obj.argument == key: + flag = True; + if not flag: + raise ParseError("Error: invalid argument '" + key + "'") if '=' not in args: - return "Error: The value for argument '" + key + "' is missing" - if key in self.Integer: + raise ParseError("Error: The value for argument '" + key + "' is missing") + flag = False + for obj in self.arguments: + if obj.types == "integer" and obj.argument == key: + flag = True; + if flag: if not value.isdigit(): - return "Error: The value for argument '" + key + "' must be integer" - if key in self.String: + raise ParseError("Error: The value for argument '" + key + "' must be integer") + if not flag: if not value.isalpha(): - return "Error: The value for argument '" + key + "' must be string" + raise ParseError("Error: The value for argument '" + key + "' must be string") response, key = self.check_required(keys) if not response: - return "Error : argument '" + key + "' is required but missing" + raise ParseError("Error : argument '" + key + "' is required but missing") return self.display_result() - diff --git a/solutions/ayush-shah/argument-parser/test.py b/solutions/ayush-shah/argument-parser/test.py index 8e368a6a..da43efc8 100644 --- a/solutions/ayush-shah/argument-parser/test.py +++ b/solutions/ayush-shah/argument-parser/test.py @@ -3,63 +3,63 @@ class UnitTesting(unittest.TestCase): parser = parse.Parser() + def test_basic_usage(self): - self.parser.add_argument('--key', 'yes', 'integer') - self.parser.add_argument('--name', 'no', 'string') + self.parser.add_argument('--key', True, 'integer') + self.parser.add_argument('--name', False, 'string') command_line_parameters = ['./test', '--key=1234', '--name=ayush'] result = self.parser.main(command_line_parameters) self.assertEqual(result, '{"--key": "1234", "--name": "ayush"}') def test_invalid_key(self): - self.parser.add_argument('--key', 'yes', 'integer') - self.parser.add_argument('--name', 'no', 'string') - command_line_parameters = ['./test', '--key=ayush', '--name=ayush'] - result = self.parser.main(command_line_parameters) - self.assertEqual(result, "Error: The value for argument '--key' must be integer") + self.parser.add_argument('--key', True, 'integer') + self.parser.add_argument('--name', False, 'string') + with self.assertRaises(parse.ParseError) as error: + self.parser.main(['./test', '--key=ayush', '--name=ayush']) + self.assertEqual("Error: The value for argument '--key' must be integer", str(error.exception)) def test_required_argument(self): - self.parser.add_argument('--key', 'yes', 'integer') - self.parser.add_argument('--name', 'no', 'string') - command_line_parameters = ['./test', '--name=ayush'] - result = self.parser.main(command_line_parameters) - self.assertEqual(result, "Error : argument '--key' is required but missing") + self.parser.add_argument('--key', True, 'integer') + self.parser.add_argument('--name', False, 'string') + with self.assertRaises(parse.ParseError) as error: + self.parser.main(['./test', '--name=ayush']) + self.assertEqual("Error : argument '--key' is required but missing", str(error.exception)) def test_invalid_name(self): - self.parser.add_argument('--key', 'yes', 'integer') - self.parser.add_argument('--name', 'no', 'string') - command_line_parameters = ['./test', '--key=1234', '--name=1234'] - result = self.parser.main(command_line_parameters) - self.assertEqual(result, "Error: The value for argument '--name' must be string") + self.parser.add_argument('--key', True, 'integer') + self.parser.add_argument('--name', False, 'string') + with self.assertRaises(parse.ParseError) as error: + self.parser.main(['./test', '--key=1234', '--name=1234']) + self.assertEqual("Error: The value for argument '--name' must be string", str(error.exception)) def test_no_arguments(self): - self.parser.add_argument('--key', 'yes', 'integer') - self.parser.add_argument('--name', 'no', 'string') - command_line_parameters = ['./test'] - result = self.parser.main(command_line_parameters) - self.assertEqual(result, "Error: no arguments given in input") + self.parser.add_argument('--key', True, 'integer') + self.parser.add_argument('--name', False, 'string') + with self.assertRaises(parse.ParseError) as error: + self.parser.main(['./test']) + self.assertEqual("Error: no arguments given in input", str(error.exception)) def test_local_and_remote(self): - self.parser.add_argument('--local', 'no', 'others') - self.parser.add_argument('--remote', 'no', 'others') - command_line_parameters = ['./test', '--local', '--remote'] - result = self.parser.main(command_line_parameters) - self.assertEqual(result, "Error: The '--local' and '--remote' arguments cannot be used together") + self.parser.add_argument('--local', False, 'others') + self.parser.add_argument('--remote', False, 'others') + with self.assertRaises(parse.ParseError) as error: + self.parser.main(['./test', '--local', '--remote']) + self.assertEqual("Error: The '--local' and '--remote' arguments cannot be used together", str(error.exception)) def test_unrecognized_command(self): - self.parser.add_argument('--key', 'yes', 'integer') - self.parser.add_argument('--name', 'no', 'string') - self.parser.add_argument('--local', 'no', 'others') - self.parser.add_argument('--remote', 'no', 'others') - command_line_parameters = ['./test', '--roll'] - result = self.parser.main(command_line_parameters) - self.assertEqual(result, "Error: invalid argument '--roll'") + self.parser.add_argument('--key', True, 'integer') + self.parser.add_argument('--name', False, 'string') + self.parser.add_argument('--local', False, 'others') + self.parser.add_argument('--remote', False, 'others') + with self.assertRaises(parse.ParseError) as error: + self.parser.main(['./test', '--roll']) + self.assertEqual("Error: invalid argument '--roll'", str(error.exception)) def test_valueless_argument(self): - self.parser.add_argument('--key', 'yes', 'integer') - command_line_parameters = ['./test', '--key'] - result = self.parser.main(command_line_parameters) - self.assertEqual(result, "Error: The value for argument '--key' is missing") - + self.parser.add_argument('--key', True, 'integer') + with self.assertRaises(parse.ParseError) as error: + self.parser.main(['./test', '--key']) + self.assertEqual("Error: The value for argument '--key' is missing", str(error.exception)) if __name__ == '__main__': unittest.main() From 0350c261924e4c44a27af10162ebcd63831bc874 Mon Sep 17 00:00:00 2001 From: shahayush457 Date: Fri, 11 Oct 2019 15:26:55 +0530 Subject: [PATCH 14/14] Removed semicolon --- solutions/ayush-shah/argument-parser/parser.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solutions/ayush-shah/argument-parser/parser.py b/solutions/ayush-shah/argument-parser/parser.py index a4358e8f..dc69ee7c 100644 --- a/solutions/ayush-shah/argument-parser/parser.py +++ b/solutions/ayush-shah/argument-parser/parser.py @@ -38,10 +38,10 @@ def store_result(self, key, value): def check_required(self, keys): for check in self.arguments: flag = False - if check.required == False: + if not check.required: continue for key in keys: - if key == check.argument and check.required == True: + if key == check.argument and check.required: flag = True if not flag: return False, check.argument @@ -82,7 +82,7 @@ def main(self, argument): flag = False for obj in self.arguments: if obj.argument == key: - flag = True; + flag = True if not flag: raise ParseError("Error: invalid argument '" + key + "'") if '=' not in args: @@ -90,7 +90,7 @@ def main(self, argument): flag = False for obj in self.arguments: if obj.types == "integer" and obj.argument == key: - flag = True; + flag = True if flag: if not value.isdigit(): raise ParseError("Error: The value for argument '" + key + "' must be integer")