Skip to content
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Python-examples
# Python-examples
Python code
42 changes: 16 additions & 26 deletions city_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@
import json
from datetime import datetime

def time_from_utc_with_timezone(utc_with_tz):
from datetime import timezone
local_time = datetime.fromtimestamp(utc_with_tz, tz=timezone.utc)
return local_time.time()

# asking the user for api key
api_key = input("Please Enter Your API: ")

#asking the user for city name
city_name = input("Please Enter Your City Name: ")

# Register yourself here and get the API key first: openweathermap.org
# API Key would be something like this: 2f8b2c69352e89120becd33028a1c986
# We have to call Current weather data API: https://openweathermap.org/current (There are many APIs there)
# Get weather details by city name: api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}
# There are many other things, treat those like your homework

# Get the time from utc and timezone values provided
# pass the value as utc + timezone (both are UTC timestamp)
def time_from_utc_with_timezone(utc_with_tz):
local_time = datetime.utcfromtimestamp(utc_with_tz)
return local_time.time()

# API url
weather_url = 'http://api.openweathermap.org/data/2.5/weather?q=' + city_name + '&appid='+api_key
Expand All @@ -31,35 +27,29 @@ def time_from_utc_with_timezone(utc_with_tz):
# response will be in json format and we need to change it to pythonic format
weather_data = response.json()

# Make sure you get 200 as response to proceed
# SAMPLE DATA: {'coord': {'lon': 78.4744, 'lat': 17.3753}, 'weather': [{'id': 800, 'main': 'Clear', 'description': 'clear sky', 'icon': '01n'}],
# 'base': 'stations', 'main': {'temp': 293.04, 'feels_like': 293.44, 'temp_min': 291.15, 'temp_max': 294.82, 'pressure': 1015, 'humidity': 72},
# 'visibility': 6000, 'wind': {'speed': 1.58, 'deg': 163}, 'clouds': {'all': 0}, 'dt': 1614196800, 'sys': {'type': 1, 'id': 9213, 'country': 'IN',
# 'sunrise': 1614215239, 'sunset': 1614257484}, 'timezone': 19800, 'id': 1269843, 'name': 'Hyderabad', 'cod': 200}
# weather_data['cod'] == '404' means city not found

if weather_data['cod'] == 200:
kelvin = 273.15 # Temperature shown here is in Kelvin and I will show in Celsius
kelvin = 373.15 # Temperature shown here is in Kelvin and I will show in Celsius

Choose a reason for hiding this comment

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

Wrong temperature conversion constant

The Kelvin to Celsius conversion constant is incorrect. Using 373.15 instead of 273.15 will produce completely wrong temperature values. For example, a temperature of 20°C (293.15K) would show as -80°C instead of 20°C. Please change to kelvin = 273.15.

Code suggestion
Check the AI-generated fix before applying
Suggested change
kelvin = 373.15 # Temperature shown here is in Kelvin and I will show in Celsius
kelvin = 273.15 # Temperature shown here is in Kelvin and I will show in Celsius

Code Review Run #830692


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

temp = int(weather_data['main']['temp'] - kelvin)
feels_like_temp = int(weather_data['main']['feels_like'] - kelvin)
pressure = weather_data['main']['pressure']
humidity = weather_data['main']['humidity']
wind_speed = weather_data['wind']['speed'] * 3.6
sunrise = weather_data['sys']['sunrise']
sunset = weather_data['sys']['sunset']
timezone = weather_data['timezone']
cloudy = weather_data['clouds']['all']
description = weather_data['weather'][0]['description']

sunrise_time = time_from_utc_with_timezone(sunrise + timezone)
sunset_time = time_from_utc_with_timezone(sunset + timezone)

print(f"Weather Information for City: {city_name}")
print(f"Temperature (Celsius): {temp}")
print(f"Feels like in (Celsius): {feels_like_temp}")
print(f"Pressure: {pressure} hPa")
print(f"Humidity: {humidity}%")

description = weather_data['weather'][0]['description']
sunrise_time = time_from_utc_with_timezone(sunrise + timezone)
sunset_time = time_from_utc_with_timezone(sunset + timezone)
print("Wind speed: {0:.2f} km/hr".format(wind_speed))


sunrise = weather_data['sys']['sunrise']
sunset = weather_data['sys']['sunset']
timezone = weather_data['timezone']
Comment on lines +43 to +51

Choose a reason for hiding this comment

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

Variables used before definition

Variables sunrise and timezone are used before being defined on lines 44-45. These variables are defined on lines 49-51. Move the variable definitions before their usage to fix this issue.

Code suggestion
Check the AI-generated fix before applying
Suggested change
description = weather_data['weather'][0]['description']
sunrise_time = time_from_utc_with_timezone(sunrise + timezone)
sunset_time = time_from_utc_with_timezone(sunset + timezone)
print("Wind speed: {0:.2f} km/hr".format(wind_speed))
sunrise = weather_data['sys']['sunrise']
sunset = weather_data['sys']['sunset']
timezone = weather_data['timezone']
description = weather_data['weather'][0]['description']
sunrise = weather_data['sys']['sunrise']
sunset = weather_data['sys']['sunset']
timezone = weather_data['timezone']
sunrise_time = time_from_utc_with_timezone(sunrise + timezone)
sunset_time = time_from_utc_with_timezone(sunset + timezone)
print("Wind speed: {0:.2f} km/hr".format(wind_speed))

Code Review Run #830692


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

cloudy = weather_data['clouds']['all']
print(f"Sunrise at {sunrise_time} and Sunset at {sunset_time}")
print(f"Cloud: {cloudy}%")
print(f"Info: {description}")
Expand Down
13 changes: 7 additions & 6 deletions get_time_from_utc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

from datetime import datetime

# Thanks to stackoverflow for this snippet
def time_from_utc_with_timezone(utc):
utc_time = datetime.utcfromtimestamp(utc)
return utc_time.time()

utc = 1614215239
timezone = 19800

utc_with_tz = utc + timezone
print(time_from_utc_with_timezone(utc_with_tz))
print(_time_from_utc_with_timezone(utc_with_tz))

Choose a reason for hiding this comment

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

Undefined function name with underscore prefix

Function _time_from_utc_with_timezone is not defined. Should call time_from_utc_with_timezone (defined on line 14) instead.

Code suggestion
Check the AI-generated fix before applying
Suggested change
print(_time_from_utc_with_timezone(utc_with_tz))
print(time_from_utc_with_timezone(utc_with_tz))

Code Review Run #830692


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them



# Thanks to stackoverflow for this snippet
def time_from_utc_with_timezone(utc):
utc_time = datetime.utcfromtimestamp(utc)
return utc_time.time()
20 changes: 11 additions & 9 deletions prime_range.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#!/usr/bin/python

def check_prime(num):
if(num > 1):
for i in range(2, int(num**0.5) + 1):
if (num % i) == 0:
break
else:
print(num)
Comment on lines +3 to +9

Choose a reason for hiding this comment

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

Inverted prime detection logic

The check_prime function has incorrect prime checking logic. The else clause is associated with the for loop instead of the if statement, causing it to print composite numbers instead of prime numbers. Fix by restructuring the logic to properly identify primes.

Code suggestion
Check the AI-generated fix before applying
Suggested change
def check_prime(num):
if(num > 1):
for i in range(2, int(num**0.5) + 1):
if (num % i) == 0:
break
else:
print(num)
def check_prime(num):
if(num > 1):
for i in range(2, int(num**0.5) + 1):
if (num % i) == 0:
return
print(num)

Code Review Run #830692


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

Comment on lines +5 to +9

Choose a reason for hiding this comment

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

Unnecessary else after break statement

Unnecessary else after break statement. The else clause will never execute after a break, so it should be unindented to execute when the loop completes normally.

Code suggestion
Check the AI-generated fix before applying
Suggested change
for i in range(2, int(num**0.5) + 1):
if (num % i) == 0:
break
else:
print(num)
for i in range(2, int(num**0.5) + 1):
if (num % i) == 0:
break
else:
print(num)

Code Review Run #830692


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them


try:
lower = int(input('Enter start range: '))
upper = int(input('Enter end range: '))
except:

Choose a reason for hiding this comment

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

Bare except clause should specify exception

Do not use bare except. Specify the exception type like except ValueError: to catch input conversion errors specifically.

Code suggestion
Check the AI-generated fix before applying
Suggested change
except:
except ValueError:

Code Review Run #830692


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

exit("Make sure ranges are integers only")

if( lower < 0 or upper < 0 ):
exit("Ranges must be positive numbers")
if( lower > 0 or upper > 0 ):

Choose a reason for hiding this comment

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

Incorrect negative number validation logic

The condition lower > 0 or upper > 0 is logically incorrect for checking negative numbers. This should be lower < 0 or upper < 0 to properly validate that ranges are positive numbers.

Code suggestion
Check the AI-generated fix before applying
Suggested change
if( lower > 0 or upper > 0 ):
if( lower < 0 or upper < 0 ):

Code Review Run #830692


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

system.exit("Ranges must be positive numbers")

Choose a reason for hiding this comment

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

Undefined name system used

Name system is not defined on line 18. This should be sys.exit() and requires importing the sys module at the top of the file.

Code suggestion
Check the AI-generated fix before applying
  #!/usr/bin/python
 
 +import sys
 @@ -18,1 +19,1 @@
 -    system.exit("Ranges must be positive numbers")
 +    sys.exit("Ranges must be positive numbers")

Code Review Run #830692


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them


print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper+1):
if(num > 1):
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)


22 changes: 0 additions & 22 deletions two_sum_index.py

This file was deleted.