From 881438b9be49f700c1d97d6fe9ea26c61fe06cd2 Mon Sep 17 00:00:00 2001 From: slowy07 Date: Sun, 1 Aug 2021 07:44:07 +0700 Subject: [PATCH] fix: baseException and fixing unused imported modules --- CountMillionCharacter.py | 2 +- Flappy Bird - created with tkinter/Bird.py | 2 +- .../Flappy Bird.py | 2 +- .../Settings.py | 2 +- Flappy Bird - created with tkinter/Tubes.py | 2 +- binary_search_tree.py | 63 +++++++++++-------- calculator.py | 1 - check_internet_con.py | 15 ++--- fibonacci.py | 2 +- 9 files changed, 51 insertions(+), 40 deletions(-) diff --git a/CountMillionCharacter.py b/CountMillionCharacter.py index a6d0f090ca9..67b4b692886 100644 --- a/CountMillionCharacter.py +++ b/CountMillionCharacter.py @@ -301,7 +301,7 @@ try: if special_character.group(): #returns all matching groups wordlist[x] = y[:-1] - except: + except BaseException: continue wordfreq = [wordlist.count(w) for w in wordlist] #counts frequency of a letter in the given list diff --git a/Flappy Bird - created with tkinter/Bird.py b/Flappy Bird - created with tkinter/Bird.py index 9de87526260..cf3385d8927 100644 --- a/Flappy Bird - created with tkinter/Bird.py +++ b/Flappy Bird - created with tkinter/Bird.py @@ -102,7 +102,7 @@ def checkCollision(self): for _id in ignored_collisions: try: possible_collisions.remove(_id) - except: + except BaseException: continue # Se houver alguma colisão o pássaro morre diff --git a/Flappy Bird - created with tkinter/Flappy Bird.py b/Flappy Bird - created with tkinter/Flappy Bird.py index cfa0a6479d7..a07f49b8f22 100644 --- a/Flappy Bird - created with tkinter/Flappy Bird.py +++ b/Flappy Bird - created with tkinter/Flappy Bird.py @@ -305,7 +305,7 @@ def loadScore(self): file.close() # Se não for possível, será criado um arquivo para guardar o placar - except: + except BaseException: file = open(self.score_fp, 'w') file.write(bin(self.__bestScore)) file.close() diff --git a/Flappy Bird - created with tkinter/Settings.py b/Flappy Bird - created with tkinter/Settings.py index f3a9fee6ddb..f91b15bc5c8 100644 --- a/Flappy Bird - created with tkinter/Settings.py +++ b/Flappy Bird - created with tkinter/Settings.py @@ -85,7 +85,7 @@ def setOptions(self): setattr(Settings, attr, data[attr]) # Caso não exista um arquivo para obter as configurações, ele será criado - except: + except BaseException: # Caso não exista o diretório, o mesmo será criado. if not os.path.exists(os.path.split(self.settings_fp)[0]): diff --git a/Flappy Bird - created with tkinter/Tubes.py b/Flappy Bird - created with tkinter/Tubes.py index e6eb32ef69e..96623fba926 100644 --- a/Flappy Bird - created with tkinter/Tubes.py +++ b/Flappy Bird - created with tkinter/Tubes.py @@ -50,7 +50,7 @@ def __init__(self, background, bird, score_function=None, *screen_geometry, fp=( # Cria uma lista para guardar imagens dos tubos try: self.deleteAll() - except: + except BaseException: self.__background.tubeImages = [] # Cria uma lista somente para guardar as imagens futuras dos corpos dos tubos gerados diff --git a/binary_search_tree.py b/binary_search_tree.py index 8e601d369a0..327c45c722d 100755 --- a/binary_search_tree.py +++ b/binary_search_tree.py @@ -1,7 +1,6 @@ -import sys - class Node: """Class for node of a tree""" + def __init__(self, info): """Initialising a node""" self.info = info @@ -18,6 +17,7 @@ def __del__(self): class BinarySearchTree: """Class for BST""" + def __init__(self): """Initialising a BST""" self.root = None @@ -46,7 +46,7 @@ def insert(self, val): else: break - def search(self, val, to_delete = False): + def search(self, val, to_delete=False): current = self.root prev = -1 while current: @@ -58,12 +58,12 @@ def search(self, val, to_delete = False): current = current.right elif current.info == val: if not to_delete: - return 'Match Found' + return "Match Found" return prev else: break if not to_delete: - return 'Not Found' + return "Not Found" # Method to delete a tree-node if it exists, else error message will be returned. def delete(self, val): @@ -83,21 +83,21 @@ def delete(self, val): else: prev2.right = None self.root.info = temp.info - print('Deleted Root ', val) + print("Deleted Root ", val) # Check if node is to left of its parent elif prev.left and prev.left.info == val: # Check if node is leaf node if prev.left.left is prev.left.right: prev.left = None - print('Deleted Node ', val) + print("Deleted Node ", val) # Check if node has child at left and None at right elif prev.left.left and prev.left.right is None: prev.left = prev.left.left - print('Deleted Node ', val) + print("Deleted Node ", val) # Check if node has child at right and None at left elif prev.left.left is None and prev.left.right: prev.left = prev.left.right - print('Deleted Node ', val) + print("Deleted Node ", val) # Here node to be deleted has 2 children elif prev.left.left and prev.left.right: temp = prev.left @@ -106,10 +106,9 @@ def delete(self, val): temp = temp.right prev2.right = None prev.left.info = temp.info - print('Deleted Node ', val) + print("Deleted Node ", val) else: - print('Error Left') - + print("Error Left") # Check if node is to right of its parent elif prev.right.info == val: @@ -118,15 +117,15 @@ def delete(self, val): if prev.right.left is prev.right.right: prev.right = None flag = 1 - print('Deleted Node ', val) + print("Deleted Node ", val) # Check if node has left child at None at right if prev.right and prev.right.left and prev.right.right is None: prev.right = prev.right.left - print('Deleted Node ', val) + print("Deleted Node ", val) # Check if node has right child at None at left elif prev.right and prev.right.left is None and prev.right.right: prev.right = prev.right.right - print('Deleted Node ', val) + print("Deleted Node ", val) elif prev.right and prev.right.left and prev.right.right: temp = prev.right while temp.left is not None: @@ -134,7 +133,7 @@ def delete(self, val): temp = temp.left prev2.left = None prev.right.info = temp.info - print('Deleted Node ', val) + print("Deleted Node ", val) else: if flag == 0: print("Error") @@ -142,7 +141,8 @@ def delete(self, val): print("Node doesn't exists") def __str__(self): - return 'Not able to print tree yet' + return "Not able to print tree yet" + def is_bst(node, lower_lim=None, upper_lim=None): """Function to find is a binary tree is a binary search tree.""" @@ -158,6 +158,7 @@ def is_bst(node, lower_lim=None, upper_lim=None): is_right_bst = is_bst(node.right, node.info, upper_lim) return is_left_bst and is_right_bst + def postorder(node): # L R N : Left , Right, Node if node is None: @@ -179,6 +180,7 @@ def inorder(node): if node.right: inorder(node.right) + def preorder(node): # N L R : Node , Left, Right if node is None: @@ -189,6 +191,7 @@ def preorder(node): if node.right: preorder(node.right) + # Levelwise def bfs(node): queue = [] @@ -202,6 +205,7 @@ def bfs(node): if temp.right: queue.append(temp.right) + def preorder_itr(node): # N L R : Node, Left , Right stack = [node] @@ -216,29 +220,31 @@ def preorder_itr(node): stack.append(temp.left) return values + def inorder_itr(node): # L N R : Left, Node, Right # 1) Create an empty stack S. # 2) Initialize current node as root # 3) Push the current node to S and set current = current->left until current is NULL - # 4) If current is NULL and stack is not empty then + # 4) If current is NULL and stack is not empty then # a) Pop the top item from stack. - # b) Print the popped item, set current = popped_item->right + # b) Print the popped item, set current = popped_item->right # c) Go to step 3. # 5) If current is NULL and stack is empty then we are done. stack = [] current = node while True: if current != None: - stack.append(current) # L + stack.append(current) # L current = current.left elif stack != []: temp = stack.pop() - print(temp.info) # N - current = temp.right # R + print(temp.info) # N + current = temp.right # R else: break + def postorder_itr(node): # L R N # 1. Push root to first stack. @@ -256,6 +262,7 @@ def postorder_itr(node): s1.append(temp.right) print(*(s2[::-1])) + def bst_frm_pre(pre_list): box = Node(pre_list[0]) if len(pre_list) > 1: @@ -272,11 +279,12 @@ def bst_frm_pre(pre_list): else: all_less = True if i != 1: - box.left = bst_frm_pre(pre_list[1 : i]) + box.left = bst_frm_pre(pre_list[1:i]) if not all_less: box.right = bst_frm_pre(pre_list[i:]) return box + # Function to find the lowest common ancestor of nodes with values c1 and c2. # It return value in the lowest common ancestor, -1 indicates value returned for None. # Note that both values v1 and v2 should be present in the bst. @@ -293,9 +301,10 @@ def lca(t_node, c1, c2): return current.info return -1 + # Function to print element vertically which lie just below the root node def vertical_middle_level(t_node): - e = (t_node, 0) # 0 indicates level 0, to left we have -ve and to right +ve + e = (t_node, 0) # 0 indicates level 0, to left we have -ve and to right +ve queue = [e] ans = [] # Do a level-order traversal and assign level-value to each node @@ -307,7 +316,8 @@ def vertical_middle_level(t_node): queue.append((temp.left, level - 1)) if temp.right: queue.append((temp.right, level + 1)) - return ' '.join(ans) + return " ".join(ans) + def get_level(n, val): c_level = 0 @@ -319,10 +329,11 @@ def get_level(n, val): n = n.right c_level += 1 if n is None: - return -1 + return -1 return c_level + def depth(node): if node is None: return 0 diff --git a/calculator.py b/calculator.py index c77b73046c5..ff18064c1a3 100644 --- a/calculator.py +++ b/calculator.py @@ -24,7 +24,6 @@ """ import sys -import math ## Imported math library to run sin(), cos(), tan() and other such functions in the calculator from fileinfo import raw_input diff --git a/check_internet_con.py b/check_internet_con.py index c6b8b602614..07378a4ad58 100644 --- a/check_internet_con.py +++ b/check_internet_con.py @@ -1,4 +1,5 @@ from sys import argv + try: # For Python 3.0 and later from urllib.error import URLError @@ -11,14 +12,14 @@ def checkInternetConnectivity(): try: url = argv[1] - if 'https://' or 'http://' not in url: - url = 'https://' + url - except: - url = 'https://google.com' + if "https://" or "http://" not in url: + url = "https://" + url + except BaseException: + url = "https://google.com" try: - urlopen(url, timeout=2) - print("Connection to \""+ url + "\" is working") - + urlopen(url, timeout=2) + print(f'Connection to "{url}" is working') + except URLError as E: print("Connection error:%s" % E.reason) diff --git a/fibonacci.py b/fibonacci.py index ce54ecf9924..33058b7b1b9 100644 --- a/fibonacci.py +++ b/fibonacci.py @@ -12,7 +12,7 @@ def getFibonacciIterative(n: int) -> int: a = 0 b = 1 - for i in range(n): + for _ in range(n): a, b = b, a + b return a