Exploring the math module interactively

TrustyTony 0 Tallied Votes 217 Views Share

With this code you can explore the functions of math module interactively seeing the docstrings automatically for each function.


EDIT: The docstring was unfortunately from old code, this version copes with values like 'e' and multiple parameter functions.

""" Do single parameter math calculations with prompting
optionally converting degrees to radians"""

import math
try:
    input = raw_input
    # Python 2, input is replaced by raw_input like in Python 3
except:
    # no raw_input -> Python 3, input has correct definition -> do nothing
    pass

lookup = dict((func,math.__dict__[func])
              for func in math.__dict__
              # let's take out special __ starting functions
              if not func.startswith('__'))

while True:
    a = input("""
Enter function or
empty input to quit
? for list of functions:
""").lower()
    try:
        func = lookup[a]
    except KeyError:
        if not a:
            break
        elif a == '?':
            print('Available functions are:\n\t'+'\n\t'.join(lookup)+'\n')
        else:
            print("%r is not a legal function choice!" % a)
    else:
        if  hasattr(func, '__call__'):
            print('\n%s\n' % func.__doc__)
            try:
                args = input('Give parameter for %s: ' % a)
                if args:
                    args = args.split(',')
                    args = [float(arg) if '.' in arg else int(arg) for arg in args]
                    if 'radians' in func.__doc__:
                        args = args if input('Convert degrees to radians (Y/n)?').lower().startswith('n') else map(math.radians, args)
                    print (func(*args))
                else:
                    # function without parameters
                    print(func())
            except ValueError as e:
                print(e)
            except TypeError:
                print('Wrong number of parameters for %s' % a)
        else:
            print('Variable %r has value %s' % ( a, func))
LogicallyInsane 0 Newbie Poster

Lmao, this is the snippet of the code you posted in my Combining if statements thread xDDDD

TrustyTony 888 pyMod Team Colleague Featured Poster

It is adapted to include multiple parameters and checking of when radians conversion is apropriate from docstring, among other improvements. Little hurried it through as you can see prompting for parameter instead of 'appropriate number of parameters' and the old docstring in beginning, like I wrote.

It is good idea to finalize the code for some responses (maybe generalize little) and post them as code snippets if they could be helpful for somebody in future.

I wanted to post some new code for the holidays and to celebrate 2000 posts mark, which does not show actually in post count in my postings, but you can see in my info page. For some reason they are out of sync.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.