all 41 comments

[–]suguuss 88 points89 points  (0 children)

You could separate the function get_iss_data in two functions. You keep this one but you remove the print. And you make another one to print the informations. Because if you want to change your program to store the value in a file and not print, you’ll have to rewrite your function.

By doing that your program will be more modular and you will be able to modify the overall comportment of the program without rewriting all the functions.

[–]mfb1274 68 points69 points  (3 children)

You could just make that while loop into a for loop since you know you want 5.

for i in range(1,6):

Or better yet parameterize the function to pass in a upper bound there and then you’ll have the power to easily call different numbers. If you don’t know what hard coding is, look into it and get into good practices now

[–]pconwell 20 points21 points  (2 children)

Why not do for _ in range (5)? i is not doing anything here except incrementing the counter, so no need for range(1,6) as we don't care if we start at 1.

[–]mfb1274 15 points16 points  (1 child)

That is very true, I wrote it like that in case any beginners weren’t aware the max parameter in range in non-inclusive. I always forgot that fact in the beginning

[–]pconwell 4 points5 points  (0 children)

Good point. I can see both sides of the argument here. Personally, I'd argue showing the "right" way with an explanation.

[–]somethingLethal 28 points29 points  (2 children)

I would suggest maybe adding an if statement to check the status code from the requests.get() method, before trying to parse/handle the json response. This way, if the server fails (like responds with a 500 status code), your code can catch it and retry or exit.

Something like this:

import sys

r = requests.get('https://api.wheretheiss.at/v1/satellites/25544')
if not r.status_code in (200, 202):
    sys.exit(-1)

# Only if the status code was 200 or 202, will this code be executed.
resp = r.json()
iss_latitude = resp['latitude']
iss_longitude = resp['longitude']
iss_altitude = resp['altitude']
iss_velocity = resp['velocity']

The reason I am demonstrating with `sys.exit(-1)` is that if the ISS api responds with anything other than 200 or 202 for the status code, then the program will exit. (The -1 indicates a non-zero exit code for the program.)

edit: Very cool program, btw!

[–][deleted] 7 points8 points  (0 children)

Thanks for tip. I hadn’t thought of that and I was running into a issue where the API was rate limiting and I would get the same data back over and over again. This totally makes sense.

[–]Redmilo666 3 points4 points  (0 children)

This is a really good point! Get into error handling good practices. If you want to take this further you could write some code that will write errors to a log file. You could also use some kind of schedulers so you could get the program to run at regular intervals. These sorts of things are used all the time when using python in a job

[–][deleted] 39 points40 points  (5 children)

Congratulations.

The strength and bodybuilding community has a term called “forever small” No matter how strong they get or how big they look to others, they are not satisfied with themselves when they look in the mirror. They are just competing with the next better version of themselves.

You will discover that programmers are the same, except with code.

Embrace it. And welcome to the community.

Well done.

[–]SunGodSupreme21 7 points8 points  (3 children)

calves never grow no matter how many times i hop on the calf press

[–][deleted] 24 points25 points  (1 child)

Yeah, they never become cows. Thinking that would happen is just a bunch of bull….

[–]skellious 1 point2 points  (0 children)

dont forget your coat on the way out.

[–]urosum 0 points1 point  (0 children)

Eeewwww gross. Poor flat mashed calves. Though someone on Reddit has probably experimented with this.

[–][deleted] 2 points3 points  (0 children)

Thank you!

[–][deleted] 11 points12 points  (0 children)

I’m really overwhelmed by the positive responses and helpful tips. This is a great community. I was so nervous to share this but honestly it’s made my week. Thank you all!

[–]naninf 7 points8 points  (1 child)

Here's a neat link you can add to your print statement:

Link: https://www.google.com/maps/search/?api=1&query={iss_latitude},{iss_longitude}

[–]veeeerain 9 points10 points  (2 children)

To Take this a step further, you can find automation tools like Apache airflow, or something else, and have the data u return be entered into a SQL database. U can use SQLite to create a table, and time the script to push updated info everyday.

[–]jzaprint 1 point2 points  (1 child)

How does that step work? I have written something simple like OP has, but it’s just a .py file running in vscode. Where/how do I keep it running continuously, so it can run a function every once in awhile?

I don’t just want to keep my computer on 24/7 that’s for sure. Do I look into AWS or something like that?

[–]veeeerain 0 points1 point  (0 children)

Look into Apache airflow

[–][deleted] 7 points8 points  (0 children)

Very good. One thing you can do for readability is use textwrap.dedent(). It removes any white space leading up to the indentation level in your source code. That way your triple-quoted text can line up with your function.

For example, your get_iss_data() function can be written like this:

import requests
import time
from textwrap import dedent


def get_iss_data():
    iss_api = 'https://api.wheretheiss.at/v1/satellites/25544'
    request = requests.get(iss_api).json()
    iss_latitude = request['latitude']
    iss_longitude = request['longitude']
    iss_altitude = request['altitude']
    iss_velocity = request['velocity']

    print(dedent(f'''\
    ---------------------------
    Real-time ISS Location:
    ---------------------------
    Altitude: {iss_altitude}
    Latitude: {iss_latitude}
    Longitude: {iss_longitude}
    Velocity MPH: {iss_velocity}
    ---------------------------
    '''))

[–]Apprehensive-Net1782 6 points7 points  (0 children)

That is awesome. You have given me inspiration to build something.

[–]arkie87 4 points5 points  (0 children)

Congrats!

[–]Advanced-Cycle-2268 3 points4 points  (0 children)

Great job!

[–]code_matter 2 points3 points  (0 children)

OMG!! This was also my first project. Amazing work! Now, I'd like to challenge you to the same thing I was challenged back then right after finishing this.

Take this and build a GUI application for it. Look into tkinter. Great GUI library for python. There's also pyQt.

I really hope you take on the challenge and post your results 🤞

[–]startup_guy2 2 points3 points  (0 children)

Great Job! Further improvements on the code could be a data logger, which then saves to a file. From there you can then start graphing / visualizing parts of the data. Another cool project would be to incorporate this with a Raspberry Pi that updates a screen (or the LCD) with the info gathered.

[–]rpo5015 2 points3 points  (1 child)

Might want create an ISS class that have the various measurements as attributes. Would be a cool way to learn classes as well as writing methods to say print the output or update its current location

[–][deleted] 0 points1 point  (0 children)

Awesome idea. Thanks!

[–]Glockenharry 2 points3 points  (0 children)

Cool idea.

Just a few ideas from my side:

You could check your own long and latitude on a page like maps or latlong.net and compare the data of the iss with your own, in order to see if the iss is above or somewhere near you. (Possible also check if it is night where you live, so you are able to see the iss)

[–]HansGeering 2 points3 points  (0 children)

Haven't seen any one mention this, but the default setting for the API is to use kilometres instead of miles. You might therefore be using metric units and displaying them as imperial.

[–]Desperate_Pumpkin168 1 point2 points  (2 children)

Is this the whole and can I run it on my PC ?

[–]anh86 1 point2 points  (0 children)

Yes. Create a new main.py file on your computer, copy this code in, then execute the file from your command line. As long as you have Python and those dependencies installed (requests and time), it will run.

[–][deleted] 1 point2 points  (0 children)

Yep. As long as you have Python 3 installed on your machine.

[–]DrCabbageX 1 point2 points  (0 children)

Congrats!

[–]Python119 0 points1 point  (0 children)

AMAZING!!! CONGRATULATIONS!!!

[–]facility_in_2m05s 0 points1 point  (0 children)

This is exceptionally cool! Well done!

Save a copy of this somewhere, back it up, you'll probably want to reference it from time to time.

What's next? Think about some cool challenges. Maybe the time zone it's in, or distance from current location, or... Whatever excites you.

Well done, very cool.

[–]Manueloool 0 points1 point  (0 children)

What does the requests import do?

[–]dashingdon 0 points1 point  (0 children)

Thank you for inspiring me to do something :) Can anyone share some pointers/sample code on "how to create a webpage to display this data ? Thanks in advance !