Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo: Code review #559

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
67 changes: 67 additions & 0 deletions examples/demo/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import random
import time

# Global variable
GLOBAL_COUNTER = 0


def process_data(data):
global GLOBAL_COUNTER
result = []
for i in range(len(data)):
item = data[i]
if item % 2 == 0:
result.append(item * 2)
else:
result.append(item * 3)
GLOBAL_COUNTER += 1
return result
Comment on lines +8 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment: Inefficient list processing in process_data function

Solution: Use a list comprehension or direct iteration over the data list.

Suggested change
def process_data(data):
global GLOBAL_COUNTER
result = []
for i in range(len(data)):
item = data[i]
if item % 2 == 0:
result.append(item * 2)
else:
result.append(item * 3)
GLOBAL_COUNTER += 1
return result
def process_data(data):
global GLOBAL_COUNTER
result =[item * 2 if item % 2 == 0 else item * 3 for item in data]
GLOBAL_COUNTER += len(data)
return result



def fetch_user_data(user_id):
# Simulating a database query
time.sleep(2)
return {"id": user_id, "name": f"User{user_id}", "score": random.randint(1, 100)}


def calculate_average(numbers):
total = 0
count = 0
for num in numbers:
total += num
count += 1
return total / count if count > 0 else 0


def write_to_file(filename, content):
f = open(filename, "w")
f.write(content)
f.close()
Comment on lines +36 to +39
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment: File not closed properly in write_to_file function

Solution: Use a 'with' statement to ensure the file is properly closed.

Suggested change
def write_to_file(filename, content):
f = open(filename, "w")
f.write(content)
f.close()
def write_to_file(filename, content):
with open(filename, "w") as f:
f.write(content)



def main():
data = [1, 2, 3, 4, 5]
processed_data = process_data(data)
print("Processed data:", processed_data)

user_data = fetch_user_data(123)
print("User data:", user_data)

numbers = [10, 20, 30, 40, 50]
avg = calculate_average(numbers)
print("Average:", avg)

write_to_file("output.txt", "Hello, World!")

unused_var = 42
print("Unused variable:", unused_var)

db_password = "password123"
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment: Hardcoded database password in plain text

Solution: Use environment variables or a secure configuration management system to store sensitive information.

Suggested change
db_password = "password123"
import os
db_password = os.environ.get('DB_PASSWORD')

print("DB Password:", db_password)

squared_numbers = [num * num for num in range(100)]
print("Squared numbers:", squared_numbers)


if __name__ == "__main__":
main()
Loading