Skip to content

Instantly share code, notes, and snippets.

@kalebo
Last active August 29, 2015 14:05
Show Gist options
  • Save kalebo/dd75b0b9d7a26e78f1d4 to your computer and use it in GitHub Desktop.
Save kalebo/dd75b0b9d7a26e78f1d4 to your computer and use it in GitHub Desktop.
A quick script to query WolfamAlpha for planet diameters to see if all the planets could really fit between the earth and the moon. Yes this is ugly code and it is overdoing it. See https://gist.github.com/4052828 for my inspiration.
import xmltodict
import urllib2
PLANETS = ["mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto"]
WA_KEY = "GETYOUR-OWNAPIKEY"
def query(q, key=WA_KEY):
return urllib2.urlopen('http://api.wolframalpha.com/v2/query?appid=%s&input=%s&format=plaintext' % (key,urllib2.quote(q))).read()
def returnfloat(res):
return float(res["queryresult"]["pod"][1]["subpod"]["plaintext"].split(' ')[0]) # <-- this is very liable to break!
def planetdiam(mes="diameter", planets=PLANETS):
res = {planet: xmltodict.parse(query("%s of %s" % (mes, planet))) for planet in planets}
resint = {planet: returnfloat(i) for planet, i in res.items()}
return resint
def distbetween():
res = xmltodict.parse(query("distance between earth and moon"))
return returnfloat(res)
if __name__ == "__main__":
planetdist = {}
planetdist = planetdiam()
earthmoondist = distbetween()
totaldist = 0.0
for planet,dist in planetdist.items():
totaldist += dist
print(totaldist / earthmoondist) * 100
# returns ~98.05%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment