Ticket #237: trac_237.patch

File trac_237.patch, 1.7 KB (added by ncohen, 8 years ago)
  • new file sage/databases/isc.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1367061599 -7200
    # Node ID 1936f74e252baff253290117bf1c1018976cf84e
    # Parent  55cd02796375ceee8c2079c725b26096d88a4dac
    [mq]: ararey
    
    diff --git a/sage/databases/isc.py b/sage/databases/isc.py
    new file mode 100644
    - +  
     1r"""
     2Interface with the Inverse Symbolic Calculator (ISC)
     3
     4ISC is an online database associating symbolic formulas to real constants. It is
     5available at `<http://isc.carma.newcastle.edu.au/index>`_.
     6
     7"""
     8
     9import re, urllib
     10
     11def ask_ISC(x):
     12    r"""
     13    Query the Inverse Symbolic Calculator with a real value.
     14
     15    INPUT:
     16
     17    - ``x`` -- the real value
     18
     19    EXAMPLE::
     20
     21        sage: from sage.databases.isc import ask_ISC
     22        sage: ask_ISC("3.14") # requires - internet
     23
     24    """
     25    global s
     26    urlparams = urllib.urlencode({'input': x});
     27
     28    try:
     29        url = "http://isc.carma.newcastle.edu.au/standardCalc"
     30        print "Asking ISC what it knows about", x
     31        f = urllib.urlopen(url+'?'+urlparams);
     32        s = f.read()
     33        reformat_and_print(s)
     34        f.close()
     35    except IOError as msg:
     36        raise IOError("%s\nError fetching the following website:\n    %s\nTry checking your internet connection."%(msg, url))
     37
     38def reformat_and_print(s):
     39    right = ""
     40    for l in s.splitlines():
     41        if "class='even'" in l:
     42            left = re.sub(".*'even'.*?>","",l)
     43            left = re.sub("<.*>","",left)
     44        elif "class='odd'" in l:
     45            r = re.sub("<.*?>","",l)
     46            if r:
     47                right = r
     48            print right, left
     49    if not right:
     50        print "No result found in ISC"