From 4d77f9beb5a7d1f7cfee53829ca7746c21072711 Mon Sep 17 00:00:00 2001 From: John Mwenda Date: Mon, 22 Jul 2019 14:43:54 +0300 Subject: [PATCH 1/3] debug python code --- Bugs Documentation | 26 ++++++++++++++++++++++++++ BugsDocumentation.md | 26 ++++++++++++++++++++++++++ snippets/__init__.py | 14 +++++++------- snippets/foobar.py | 3 ++- snippets/io.py | 42 +++++++++++++++++++++++++----------------- snippets/loop.py | 6 +++--- 6 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 Bugs Documentation create mode 100644 BugsDocumentation.md diff --git a/Bugs Documentation b/Bugs Documentation new file mode 100644 index 0000000..8a20f50 --- /dev/null +++ b/Bugs Documentation @@ -0,0 +1,26 @@ +Documentaion of Bugs found +1. snippets.__init__.py + - import code in snippets.__init__ had been commented out and so the import were not happening +2. snippets.loop.py + - line 9: initialize dict instead of list +3. snippets.loop.py + - line 11: forgot to add range(10) + - line 13: NameError cause of using undefined variable + - line 13: use list.push instead of list.append +4. snippets.io.py + - line 16: list comprehension should use the [] symbols instead of {} + - line 18: syntax error, there is no such operator as !===, instead use '==' operator + - line 20: use python inbuilt sum + - line 29: use python inbuilt sum + - line 38: use python inbuilt sum + - line 39: use python inbuilt len + - line 15, 24, 34 + - use [] to access the values of the dict + - line 18: should avoid using is operator for immutable types such as strings and numbers, the result is unpredictable, instead use == + - line 45: test for division by zero, by checking if sum is zero + +5. snippets.foobar.py + - line 8: remove default list argument and initialize it inside the function (explanation below) + + Please note: Any expressions in default arguments are always calculated when the function is defined and not when it is called. So when function is called the first time the 'bar' list is created, any subsequntial calls will still use the same list. To avoid this just define bar inside the function and not as a default argument. + \ No newline at end of file diff --git a/BugsDocumentation.md b/BugsDocumentation.md new file mode 100644 index 0000000..8a20f50 --- /dev/null +++ b/BugsDocumentation.md @@ -0,0 +1,26 @@ +Documentaion of Bugs found +1. snippets.__init__.py + - import code in snippets.__init__ had been commented out and so the import were not happening +2. snippets.loop.py + - line 9: initialize dict instead of list +3. snippets.loop.py + - line 11: forgot to add range(10) + - line 13: NameError cause of using undefined variable + - line 13: use list.push instead of list.append +4. snippets.io.py + - line 16: list comprehension should use the [] symbols instead of {} + - line 18: syntax error, there is no such operator as !===, instead use '==' operator + - line 20: use python inbuilt sum + - line 29: use python inbuilt sum + - line 38: use python inbuilt sum + - line 39: use python inbuilt len + - line 15, 24, 34 + - use [] to access the values of the dict + - line 18: should avoid using is operator for immutable types such as strings and numbers, the result is unpredictable, instead use == + - line 45: test for division by zero, by checking if sum is zero + +5. snippets.foobar.py + - line 8: remove default list argument and initialize it inside the function (explanation below) + + Please note: Any expressions in default arguments are always calculated when the function is defined and not when it is called. So when function is called the first time the 'bar' list is created, any subsequntial calls will still use the same list. To avoid this just define bar inside the function and not as a default argument. + \ No newline at end of file diff --git a/snippets/__init__.py b/snippets/__init__.py index 637acd7..c95361b 100644 --- a/snippets/__init__.py +++ b/snippets/__init__.py @@ -1,8 +1,8 @@ -# from .loop import lambda_array -# from .io import ( -# read_file, -# calculate_unpaid_loans, -# calculate_paid_loans, -# average_paid_loans -# ) +from .loop import lambda_array +from .io import ( + read_file, + calculate_unpaid_loans, + calculate_paid_loans, + average_paid_loans +) from .foobar import foo diff --git a/snippets/foobar.py b/snippets/foobar.py index 61c5243..f0277e8 100644 --- a/snippets/foobar.py +++ b/snippets/foobar.py @@ -5,6 +5,7 @@ """ -def foo(bar=[]): +def foo(): + bar = [] bar.append("baz") return bar diff --git a/snippets/io.py b/snippets/io.py index c3f92bc..484a182 100644 --- a/snippets/io.py +++ b/snippets/io.py @@ -12,30 +12,38 @@ def read_file(): def calculate_unpaid_loans(data): - loans = data("loans") - unpaid_loans = { - loan.amount for loan in loans - if loan.status !== "unpaid" - } - return sun(unpaid_loans) + try: + loans = data["loans"] + unpaid_loans = [ + loan["amount"] for loan in loans + if loan["status"] == "unpaid" + ] + except KeyError as error: + print(error, 'could not access some keys from the json format') + return sum(unpaid_loans) def calculate_paid_loans(data): - loans = data("loans") - paid_loans = [ - loan.amount for loan in loans - if loan.status is "paid" - ] - return sun(paid_loans) + try: + loans = data["loans"] + paid_loans = [ + loan["amount"] for loan in loans + if loan["status"] == "paid" + ] + except KeyError as error: + print(error, 'could not access some keys from the json format') + return sum(paid_loans) def average_paid_loans(data): - loans = data("loans") + loans = data["loans"] paid_loans = [ - loan.amount for loan in loans - if loan.status is "paid" + loan["amount"] for loan in loans + if loan["status"] == "paid" ] - sum_paid_loans = sun(paid_loans) - number_paid_loans = length(paid_loans) + sum_paid_loans = sum(paid_loans) + number_paid_loans = len(paid_loans) + if(sum_paid_loans == 0): + return 0 average = (sum_paid_loans/number_paid_loans) return average diff --git a/snippets/loop.py b/snippets/loop.py index 08a2d33..fac5495 100644 --- a/snippets/loop.py +++ b/snippets/loop.py @@ -6,10 +6,10 @@ def lambda_array(): # initialize an empty array - lambda_methods = {} + lambda_methods = [] # implement a for loop to count from 0 to 9 - for i in 10: + for i in range(10): # append the lambda function to the array defined above - lambdamethods.push(lambda x: x + i) + lambda_methods.append(lambda x: x + i) return lambda_methods From 4fa35e31465aedc79147b972736217a21a0f77a1 Mon Sep 17 00:00:00 2001 From: John Mwenda Date: Mon, 22 Jul 2019 14:44:46 +0300 Subject: [PATCH 2/3] Delete Bugs Documentation --- Bugs Documentation | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 Bugs Documentation diff --git a/Bugs Documentation b/Bugs Documentation deleted file mode 100644 index 8a20f50..0000000 --- a/Bugs Documentation +++ /dev/null @@ -1,26 +0,0 @@ -Documentaion of Bugs found -1. snippets.__init__.py - - import code in snippets.__init__ had been commented out and so the import were not happening -2. snippets.loop.py - - line 9: initialize dict instead of list -3. snippets.loop.py - - line 11: forgot to add range(10) - - line 13: NameError cause of using undefined variable - - line 13: use list.push instead of list.append -4. snippets.io.py - - line 16: list comprehension should use the [] symbols instead of {} - - line 18: syntax error, there is no such operator as !===, instead use '==' operator - - line 20: use python inbuilt sum - - line 29: use python inbuilt sum - - line 38: use python inbuilt sum - - line 39: use python inbuilt len - - line 15, 24, 34 - - use [] to access the values of the dict - - line 18: should avoid using is operator for immutable types such as strings and numbers, the result is unpredictable, instead use == - - line 45: test for division by zero, by checking if sum is zero - -5. snippets.foobar.py - - line 8: remove default list argument and initialize it inside the function (explanation below) - - Please note: Any expressions in default arguments are always calculated when the function is defined and not when it is called. So when function is called the first time the 'bar' list is created, any subsequntial calls will still use the same list. To avoid this just define bar inside the function and not as a default argument. - \ No newline at end of file From edbd7769a0aa93c8e3b26bac089a5b878b61a72e Mon Sep 17 00:00:00 2001 From: John Mwenda Date: Mon, 22 Jul 2019 14:46:01 +0300 Subject: [PATCH 3/3] Update BugsDocumentation.md --- BugsDocumentation.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/BugsDocumentation.md b/BugsDocumentation.md index 8a20f50..1f494c8 100644 --- a/BugsDocumentation.md +++ b/BugsDocumentation.md @@ -1,13 +1,13 @@ Documentaion of Bugs found -1. snippets.__init__.py +1. snippets/__init__.py - import code in snippets.__init__ had been commented out and so the import were not happening -2. snippets.loop.py +2. loop.py - line 9: initialize dict instead of list -3. snippets.loop.py +3. loop.py - line 11: forgot to add range(10) - line 13: NameError cause of using undefined variable - line 13: use list.push instead of list.append -4. snippets.io.py +4. io.py - line 16: list comprehension should use the [] symbols instead of {} - line 18: syntax error, there is no such operator as !===, instead use '==' operator - line 20: use python inbuilt sum @@ -19,8 +19,8 @@ Documentaion of Bugs found - line 18: should avoid using is operator for immutable types such as strings and numbers, the result is unpredictable, instead use == - line 45: test for division by zero, by checking if sum is zero -5. snippets.foobar.py +5. foobar.py - line 8: remove default list argument and initialize it inside the function (explanation below) Please note: Any expressions in default arguments are always calculated when the function is defined and not when it is called. So when function is called the first time the 'bar' list is created, any subsequntial calls will still use the same list. To avoid this just define bar inside the function and not as a default argument. - \ No newline at end of file +