diff --git a/README.md b/README.md index 975ccf5..ef56aba 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# Python-examples \ No newline at end of file +# Python-examples +Python code diff --git a/city_weather.py b/city_weather.py index 54f68d3..23ccc9c 100644 --- a/city_weather.py +++ b/city_weather.py @@ -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 @@ -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 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'] + cloudy = weather_data['clouds']['all'] print(f"Sunrise at {sunrise_time} and Sunset at {sunset_time}") print(f"Cloud: {cloudy}%") print(f"Info: {description}") diff --git a/get_time_from_utc.py b/get_time_from_utc.py index c0568ad..e315b13 100644 --- a/get_time_from_utc.py +++ b/get_time_from_utc.py @@ -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)) + + +# Thanks to stackoverflow for this snippet +def time_from_utc_with_timezone(utc): + utc_time = datetime.utcfromtimestamp(utc) + return utc_time.time() diff --git a/prime_range.py b/prime_range.py index c167890..09deedc 100644 --- a/prime_range.py +++ b/prime_range.py @@ -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) + try: lower = int(input('Enter start range: ')) upper = int(input('Enter end range: ')) except: 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 ): + system.exit("Ranges must be positive numbers") 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) + diff --git a/two_sum_index.py b/two_sum_index.py deleted file mode 100644 index 0f9c34c..0000000 --- a/two_sum_index.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/python -# Making change to change code hunk, was blank line -# There is an array full of integers and there is a target value t, again an integer -# You need to find which of the 2 integers sums equal to target and print those 2 integers index number -# You can safely assume that there is only such pair which would result in sum as target - -num_list = [2, 1, 3, 5, 6, 11, 2, 13, 4, 15] -target = 12 - -def twoSum(arr, t): - index_dict = {} - length = len(arr) - index = 0 - - while index < length: - if (t - arr[index]) in index_dict: - return index_dict[t - arr[index]], index - index_dict[arr[index]] = index - index += 1 - -print(twoSum(num_list, target)) -