Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
24 changes: 14 additions & 10 deletions GUI/notepad.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def __init__(self, **kwargs):
# Set icon
try:
self.__root.wm_iconbitmap("Notepad.ico")
except:
pass
except Exception as e:
print(f"Warning: Could not load icon: {e}")

# Set window size (the default is 300x300)

Comment on lines 26 to 32

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Opening files without a context manager can leak file descriptors on exceptions.

The try/except for surfacing errors is useful, but manually calling open() and close() risks leaving the file handle open if an exception occurs in between. Using with open(self.__file, "r") as file: (and similarly in __saveFile) simplifies the code and guarantees the file is always closed while preserving the existing error handling around the with block.

Expand Down Expand Up @@ -126,11 +126,12 @@ def __openFile(self):
self.__root.title(os.path.basename(self.__file) + " - Notepad")
self.__thisTextArea.delete(1.0, END)

file = open(self.__file, "r")

self.__thisTextArea.insert(1.0, file.read())

file.close()
try:
file = open(self.__file, "r")
self.__thisTextArea.insert(1.0, file.read())
file.close()
except Exception as e:
showerror("Error", f"Could not open file: {e}")

def __newFile(self):
self.__root.title("Untitled - Notepad")
Expand All @@ -150,9 +151,12 @@ def __saveFile(self):
self.__file = None
else:
# Try to save the file
file = open(self.__file, "w")
file.write(self.__thisTextArea.get(1.0, END))
file.close()
try:
file = open(self.__file, "w")
file.write(self.__thisTextArea.get(1.0, END))
file.close()
except Exception as e:
showerror("Error", f"Could not save file: {e}")

# Change the window title
self.__root.title(os.path.basename(self.__file) + " - Notepad")
Expand Down
2 changes: 1 addition & 1 deletion GUI/snake_ladder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Function for generating a random number on the dice
def dice_roll():
dice_number = random.randrange(1, 7, 1)
dice_number = random.randrange(1, 7, 1) # nosec B311
return dice_number


Expand Down
2 changes: 1 addition & 1 deletion Game/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def add_new_2(mat):
if mat[i][j] == 0:
empty_cells.append((i, j))
if empty_cells:
i, j = random.choice(empty_cells)
i, j = random.choice(empty_cells) # nosec B311
mat[i][j] = 2


Expand Down
2 changes: 1 addition & 1 deletion Game/color_guessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def nextColour():
# clear the text entry box.
e.delete(0, tkinter.END)

random.shuffle(colours)
random.shuffle(colours) # nosec B311

# change the colour to type, by changing the
# text _and_ the colour to a random colour value
Expand Down
30 changes: 15 additions & 15 deletions Game/colox.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

# randomly assigns a value to variables
# ranging from lower limit to upper
c1 = random.randint(125, 255)
c2 = random.randint(0, 255)
c3 = random.randint(0, 255)
c1 = random.randint(125, 255) # nosec B311
c2 = random.randint(0, 255) # nosec B311
c3 = random.randint(0, 255) # nosec B311

screen = pygame.display.set_mode(res)
clock = pygame.time.Clock()
Expand Down Expand Up @@ -60,19 +60,19 @@

# game title
colox = smallfont.render("Colox", True, (c3, c2, c1))
x1 = random.randint(width // 2, width)
y1 = random.randint(100, height // 2)
x1 = random.randint(width // 2, width) # nosec B311
y1 = random.randint(100, height // 2) # nosec B311
x2 = 40
y2 = 40
speed = 15

# score of the player
count = 0
rgb = random.choice(color_list)
rgb = random.choice(color_list) # nosec B311

# enemy position
e_p = [width, random.randint(50, height - 50)]
e1_p = [random.randint(width, width + 100), random.randint(50, height - 100)]
e_p = [width, random.randint(50, height - 50)] # nosec B311
e1_p = [random.randint(width, width + 100), random.randint(50, height - 100)] # nosec B311


# function for game_over
Expand Down Expand Up @@ -164,12 +164,12 @@ def game(lead_y, lead_X, speed, count):
if (
e_p[1] <= 40 or e_p[1] >= height - 80
): # Prevent appearing on top and bottom color banners
e_p[1] = random.randint(40, height - 80)
e_p[1] = random.randint(40, height - 80) # nosec B311
if (
e1_p[1] <= 40 or e1_p[1] >= height - 80
): # Prevent appearing on top and bottom color banners
e1_p[1] = random.randint(40, height - 80)
e_p[1] = random.randint(enemy_size, height - enemy_size)
e1_p[1] = random.randint(40, height - 80) # nosec B311
e_p[1] = random.randint(enemy_size, height - enemy_size) # nosec B311
e_p[0] = width

if lead_x <= e_p[0] <= lead_x + 40 and lead_y >= e_p[1] >= lead_y - 40:
Expand All @@ -188,13 +188,13 @@ def game(lead_y, lead_X, speed, count):
if (
e1_p[1] <= 40 or e1_p[1] >= height - 80
): # Prevent appearing on top and bottom color banners
e1_p[1] = random.randint(40, height - 80)
e1_p[1] = random.randint(enemy_size, height - 40)
e1_p[1] = random.randint(40, height - 80) # nosec B311
e1_p[1] = random.randint(enemy_size, height - 40) # nosec B311
e1_p[0] = width + 100

if lead_x <= e1_p[0] <= lead_x + 40 and e1_p[1] <= lead_y <= e1_p[1] + 40:
e1_p[0] = width + 100
e1_p[1] = random.randint(40, height - 40)
e1_p[1] = random.randint(40, height - 40) # nosec B311
count += 1
speed += 1

Expand All @@ -203,7 +203,7 @@ def game(lead_y, lead_X, speed, count):
and lead_x <= e1_p[0] <= lead_x + 40
):
e1_p[0] = width + 100
e1_p[1] = random.randint(40, height - 40)
e1_p[1] = random.randint(40, height - 40) # nosec B311
count += 1
speed += 1

Expand Down
2 changes: 1 addition & 1 deletion Game/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def roll_dice():

print("Rolling the dice...")
time.sleep(1) # Pause for 1 second to create rolling effect
dice_value = random.randint(min_value, max_value)
dice_value = random.randint(min_value, max_value) # nosec B311
print(dice_faces[dice_value - 1]) # Display the corresponding dice face

if user_guess == dice_value:
Expand Down
2 changes: 1 addition & 1 deletion Game/hangman.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def hangman():
"grapefruit",
"blueberry",
]
word = random.choice(words)
word = random.choice(words) # nosec B311
guessed_letters = []
attempts = 5

Expand Down
2 changes: 1 addition & 1 deletion Game/master_mid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def play_game():
# Generate a random 4-digit number
num = random.randrange(1000, 10000)
num = random.randrange(1000, 10000) # nosec B311

# Initialize variables
high_score = get_high_score()
Expand Down
2 changes: 1 addition & 1 deletion Game/number_guessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

lower = int(input("Enter Lower(mininum) limit:- "))
upper = int(input("Enter Upper(maxinum) limit:- "))
x = random.randint(lower, upper)
x = random.randint(lower, upper) # nosec B311
print(
"\n\tYou've only ",
round(math.log(upper - lower + 1, 2)),
Expand Down
2 changes: 1 addition & 1 deletion Game/rock_paper_scissors.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def rock_paper_scissors():
print("Invalid choice. Please try again.")
continue

computer_choice = random.randint(0, 2)
computer_choice = random.randint(0, 2) # nosec B311

print("Player's move:", choices[player_choice])
print("Computer's move:", choices[computer_choice])
Expand Down
12 changes: 6 additions & 6 deletions Game/snake_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
snake_length = 1

# Spawn the first food
food_x = round(random.randrange(0, window_width - segment_size) / 20.0) * 20.0
food_y = round(random.randrange(0, window_height - segment_size) / 20.0) * 20.0
food_x = round(random.randrange(0, window_width - segment_size) / 20.0) * 20.0 # nosec B311
food_y = round(random.randrange(0, window_height - segment_size) / 20.0) * 20.0 # nosec B311

# Game over flag
game_over = False
Expand Down Expand Up @@ -87,8 +87,8 @@
# Play eat sound effect
eat_sound.play()
# Spawn new food
food_x = round(random.randrange(0, window_width - segment_size) / 20.0) * 20.0
food_y = round(random.randrange(0, window_height - segment_size) / 20.0) * 20.0
food_x = round(random.randrange(0, window_width - segment_size) / 20.0) * 20.0 # nosec B311
food_y = round(random.randrange(0, window_height - segment_size) / 20.0) * 20.0 # nosec B311

# Create new segment and add to snake's body
snake_segments.append((snake_head_x, snake_head_y))
Expand Down Expand Up @@ -171,11 +171,11 @@
snake_length = 1
# Spawn new food
food_x = (
round(random.randrange(0, window_width - segment_size) / 20.0)
round(random.randrange(0, window_width - segment_size) / 20.0) # nosec B311
* 20.0
)
food_y = (
round(random.randrange(0, window_height - segment_size) / 20.0)
round(random.randrange(0, window_height - segment_size) / 20.0) # nosec B311
* 20.0
)
game_over = False
Expand Down
2 changes: 1 addition & 1 deletion Game/snake_ladder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Function for generating a random number on the dice
def dice_roll():
dice_number = random.randrange(1, 7, 1)
dice_number = random.randrange(1, 7, 1) # nosec B311
return dice_number


Expand Down
6 changes: 3 additions & 3 deletions Game/tick_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def make_computer_move(board, difficulty):
empty_cells = get_empty_cells(board)
if empty_cells:
if difficulty == "easy":
row, col = random.choice(empty_cells)
row, col = random.choice(empty_cells) # nosec B311
elif difficulty == "medium":
# Check for winning moves
for cell in empty_cells:
Expand All @@ -77,11 +77,11 @@ def make_computer_move(board, difficulty):
row, col = cell
break
else:
row, col = random.choice(empty_cells)
row, col = random.choice(empty_cells) # nosec B311
else:
# Choose the best move using a more advanced algorithm (e.g., Minimax)
# Implementation of Minimax is beyond the scope of this example
row, col = random.choice(empty_cells)
row, col = random.choice(empty_cells) # nosec B311

time.sleep(1) # Add a delay of 1 second before computer's move
board[row][col] = "✓" # Use tick symbol instead of "O"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ The repository is organized into categories, each containing specific project fo
- [Integration and Differentiation](https://github.com/drik493/python_projects/blob/main/Calculator/int_diff.py)
- [BMI Calculator](https://github.com/drik493/python_projects/blob/main/Calculator/bmi.py)
- [Roman to Decimal Converter](Calculator/roman_number.py)
- [Time Calculator](https://github.com/mrayanasim09/python-projects/blob/main/Calculator/time_calulator.py)
- [Time Calculator](https://github.com/mrayanasim09/python-projects/blob/main/Calculator/time_calculator.py)
- [Special Relativity Calculator](Calculator/special_relativity_calculator.py)
- [Collatz Conjecture (GUI)](https://github.com/drik493/python_projects/blob/main/Calculator/conject.py)
- [Fibonacci Sequence](https://github.com/drik493/python_projects/blob/main/Calculator/sequence.py)
Expand Down
19 changes: 17 additions & 2 deletions Utilities/connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
import requests
import time

# Default timeout for HTTP requests (seconds)
DEFAULT_TIMEOUT = 10

def check_website_connectivity(url):

def check_website_connectivity(url, timeout=DEFAULT_TIMEOUT):
"""
Check if a website is reachable and measure response time.

Args:
url: The website URL to check
timeout: Request timeout in seconds (default: 10)
"""
try:
start_time = time.time()
response = requests.get(url)
# Always use timeout to prevent hanging requests
response = requests.get(url, timeout=timeout)
end_time = time.time()

if response.status_code == 200:
Expand All @@ -19,6 +30,10 @@ def check_website_connectivity(url):
print(
f"Error: The website {url} returned a status code {response.status_code}."
)
except requests.Timeout:
print(f"Error: Connection to {url} timed out after {timeout} seconds.")
except requests.ConnectionError:
print(f"Error: Unable to connect to the website {url}.")
except requests.RequestException as e:
print(f"Error: Unable to connect to the website {url}.")
print(f"Exception: {e}")
Expand Down
Loading
Loading