Skip to content

Instantly share code, notes, and snippets.

@copy
Last active October 9, 2015 13:29
Show Gist options
  • Save copy/3b35ac14461d77dea456 to your computer and use it in GitHub Desktop.
Save copy/3b35ac14461d77dea456 to your computer and use it in GitHub Desktop.
Wolfram Alpha ipython client
"""
A Wolfram Alpha ipython client.
Installation:
1. Put this script into ~/.ipython/profile_default/startup/
2. pip install wolframalpha colorama
3. Get an API key from https://developer.wolframalpha.com/portal/myapps/ and put it into ~/.wolfram_key
Example:
In [1]: wa jacobisymbol[31,37]
Input: (31/37)
Result: -1
In [2]: wa ocelot
Input interpretation: ocelot (animal)
Scientific name: Leopardus pardalis
[...]
"""
from IPython.core import magic
@magic.register_line_magic
def wa(query):
return wa_client(query)
del magic
del wa
class WolframAlphaClient(object):
APPID_FILE = "~/.wolfram_key"
def __init__(self):
self.client = None
self.colorama = None
def lazy_init(self):
if self.client is not None:
return
import wolframalpha
import os
appid = open(os.path.expanduser(self.APPID_FILE)).read().strip()
self.client = wolframalpha.Client(appid)
import colorama
self.colorama = colorama
self.colorama.init()
def __call__(self, query):
self.lazy_init()
result = self.client.query(query)
for pod in result.pods:
color = self.colorama.Fore.GREEN + self.colorama.Style.BRIGHT
color_end = self.colorama.Style.RESET_ALL
print("%s%s%s: %s" % (color, pod.title, color_end, pod.text))
print()
return result
wa_client = WolframAlphaClient()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment