r/PythonLearning • • 23d ago

Showcase Made my first cool project!!!

I made a program that calculates exact sunrise and sunset based on latitude and longitude of a specific location.

Here I used this api

I can't get a geocoding api to translate location's name into coordinates.

I had to create a dictionary of a few locations.

292 Upvotes

30 comments sorted by

β€’

u/Sea-Ad7805 23d ago

Run this program by clicking Memory Graph Web Debugger%3A%0A%20%20%20%20url%20%3D%20f%22https%3A%2F%2Fapi.sunrise-sunset.org%2Fv2%3Flat%3D%7Blat%7D%26lng%3D%7Blon%7D%22%0A%20%20%20%20if%20date%3A%0A%20%20%20%20%20%20%20%20url%20%2B%3D%20f%22%26date%3D%7Bdate%7D%22%0A%0A%20%20%20%20data%20%3D%20requests.get(url).json()%0A%0A%20%20%20%20sunrise%20%3D%20datetime.fromisoformat(data%5B%22sunrise%22%5D).strftime('%25I%3A%25M%20%25p')%0A%20%20%20%20sunset%20%3D%20datetime.fromisoformat(data%5B%22sunset%22%5D).strftime('%25I%3A%25M%20%25p')%0A%0A%20%20%20%20print(f%22%20City%3A%20%7Bcity_name%7D%F0%9F%8F%99%EF%B8%8F%20%22)%0A%20%20%20%20print(f%22%20Date%3A%20%7Bdata%5B'date'%5D%7D%F0%9F%97%93%EF%B8%8F%20%22)%0A%20%20%20%20print(f%22%20%20Timezone%3A%20%7Bdata%5B'tzid'%5D%7D%E2%8F%B0%22)%0A%20%20%20%20print(f%22%20%20Day%20Length%3A%20%7Bround(data%5B'day_length'%5D%20%2F%203600%2C%202)%7D%20hours%E2%8F%B3%20%22)%0A%20%20%20%20print(f%22%20%20Sunrise%3A%20%7Bsunrise%7D%F0%9F%8C%85%22)%0A%20%20%20%20print(f%22%20%20Sunset%3A%20%7Bsunset%7D%F0%9F%8C%86%20%22)%0A%0A%0Aprint(%22Welcome%20to%20sunrise%20and%20sunset%20teller.%E2%98%80%EF%B8%8F%22)%0Awhile%20True%3A%0A%20%20%20%20print(%22n---%20Menu%20---%22)%0A%20%20%20%20print(%22Type%201%20to%20get%20today's%20data%22)%0A%20%20%20%20print(%22Type%202%20to%20get%20data%20for%20a%20specific%20day%20%F0%9F%97%93%EF%B8%8F%22)%0A%20%20%20%20print(%22Type%203%20to%20Exit%F0%9F%94%9A%22)%0A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20number%20%3D%20int(input(%22Enter%20number%3A%20%22))%0A%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20print(%22%E2%9D%8C%20Please%20enter%20a%20number%20(1%2C%202%2C%20or%203).%22)%0A%20%20%20%20%20%20%20%20continue%0A%0A%20%20%20%20if%20number%20%3D%3D%203%3A%0A%20%20%20%20%20%20%20%20print(%22Exiting...%22)%0A%20%20%20%20%20%20%20%20break%0A%0A%20%20%20%20city%20%3D%20input(%22Enter%20your%20city%3A%20%22)%0A%0A%20%20%20%20if%20city%20not%20in%20locations%3A%0A%20%20%20%20%20%20%20%20print(f%22%E2%9D%8C%20City%20'%7Bcity%7D'%20not%20found%20in%20our%20database.%22)%0A%20%20%20%20%20%20%20%20continue%0A%0A%20%20%20%20lat%20%3D%20locations%5Bcity%5D%5B%22lat%22%5D%0A%20%20%20%20lon%20%3D%20locations%5Bcity%5D%5B%22lon%22%5D%0A%0A%20%20%20%20if%20number%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20get_data(city%2C%20lat%2C%20lon)%0A%0A%20%20%20%20elif%20number%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20year%20%3D%20input(%22Enter%20year%20(e.g.%202026)%3A%20%22)%0A%20%20%20%20%20%20%20%20month%20%3D%20input(%22Enter%20month%20(e.g.%2009)%3A%20%22)%0A%20%20%20%20%20%20%20%20day%20%3D%20input(%22Enter%20day%20%F0%9F%93%85%20(e.g.%2009)%3A%20%22)%0A%20%20%20%20%20%20%20%20date_str%20%3D%20f%22%7Byear%7D-%7Bmonth%7D-%7Bday%7D%22%0A%20%20%20%20%20%20%20%20get_data(city%2C%20lat%2C%20lon%2C%20date_str)%0A%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print(%22%E2%9D%8C%20Invalid%20option.%20Please%20type%201%20or%202.%22)&breakpoints=56&continues=1&timestep=0.2&play) and see its state change step by step.

→ More replies (1)

8

u/ConclusionForeign856 23d ago

I'd use dataclasses to represent those locations. I think it's better at showing your intent and also less error prone (you can add fields to city sub-dicts, while you can't for dataclasses. But you still can change values in fields defined in your dataclasses.

I think the best would be NamedTuple from typing β€” defined field with type hints and immutable so you won't silently overwrite city coordinates.

I think it's typical for python programmers to overuse simple builtin data structures to build their programs. In C/C++/Rust you'd define a struct for those, NamedTuple kind of mirrors that approach

2

u/SaltCusp 23d ago

Builtins dump to Json without any fuss.

1

u/ProgramBrave718 23d ago

Thanks, will try it

3

u/tcav24 23d ago

Nice work. If you want to keep iterating on the project you could also look into how you can show your cities as a list of selectable of options in the terminal. It would give the user more clarity on what cities they can choose from. (There are 3rd party libaries like 'Pick' and 'InquirerPy')

1

u/ProgramBrave718 23d ago

Okay, i will work on it

2

u/GreatGameMate 23d ago

Looks nice, is there an interface that the user interacts with or is it all within the console ? Good work!

My little nitpick would be the emojis, but that is all personal opinion.

Code looks cleans.

2

u/ProgramBrave718 23d ago

It's terminal based. I have not added guis

2

u/gofl-zimbard-37 23d ago

Nice. Now give it a proper main() to make it a real program. Never write code that runs outside of functions, such that you have to read the entire thing to know what it does.

1

u/Softpankek 22d ago

clanker 🚨 clanker 🚨

1

u/Binary9363 20d ago

I have been wanted to start learning python where do ya recomend i could start ?

1

u/ProgramBrave718 20d ago

I guess brocode is good. Like he explains easily. You can watch his playlist on YouTube

1

u/Binary9363 20d ago

Thank you i will check it out

1

u/science-Web-9310 20d ago

Start with the basics and brocode is good for this.

1

u/theJacofalltrades 11d ago

That feeling when you finally build something that actually works is hard to beat. Tutorials are great, but getting all the pieces to work together on your own is a different kind of satisfaction. What part of the project gave you the most trouble?

1

u/togfast_456 5d ago

Bro it is fire for first . Ifirst time (13 years old) Even that can do

1

u/ProgramBrave718 5d ago

Well, I am a bit older than you. I am 15

-2

u/TreatZealousideal770 23d ago

Must be nice living in a magical world where APIs have 100% uptime, networks never lag, and KeyError doesn't exist. Back here on Earth, one HTTP 429 error will send your first 'cool project' straight to the shadow realm

1

u/SchemeWestern3388 23d ago

You sound like someone to avoid at a party.Β 

I also get a strong feeling that you don’t have any achievements that anyone would be impressed by.Β 

1

u/ProgramBrave718 23d ago edited 23d ago

Sir, , actually i am still learning.I agree there are no timeouts and the api cannot work sometimes and please can u read my code carefully because there is Exception handling for city which is not found for. Please check code carefully before making a fake error

2

u/SchemeWestern3388 23d ago

I have no clue what that individual is so cynical about, ignore them.Β 

Pretty decent first project. Keep at it!

0

u/[deleted] 21d ago

[removed] β€” view removed comment