#! /usr/bin/env python

CopyrightInfo = '''
Copyright 2010 Nathan Wilcox

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import sys, pprint, __builtin__


def main(args = sys.argv[1:]):
    expr, strs = args[0], args[1:]
    scope = MagicScope(args = strs)
    for (i, arg) in enumerate(strs):
        scope['a%d' % i] = arg
    result = eval(expr, {}, scope)
    display(result)


def display(obj):
    if obj is None:
        return

    it = [obj]
    if type(obj) not in (str, unicode):
        try:
            it = iter(obj)
        except TypeError:
            pass

    for elem in it:
        print str(elem)



class AutoImporter (object):
    def __init__(self, name, parent = None):
        self._ai_name = name
        self._ai_parent = parent
        mod = __import__(self._ai_fullname)
        for name in self._ai_fullname.split('.')[1:]:
            mod = getattr(mod, name)
        self._ai_mod = mod

    @property
    def _ai_fullname(self):
        if self._ai_parent:
            return '%s.%s' % (self._ai_parent._ai_fullname, self._ai_name)
        else:
            return self._ai_name

    def __repr__(self):
        return repr(self._ai_mod)

    def __getattr__(self, name):
        try:
            return getattr(self._ai_mod, name)
        except AttributeError:
            return AutoImporter(name, self)



class MagicScope (dict):
    def __init__(self, **kw):
        dict.__init__(self, vars(__builtin__))
        self.update(kw)

    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            try:
                method = getattr(self, 'magic_' + key)
            except AttributeError:
                return AutoImporter(key)

            return method()

    def magic_pf(self):
        return pprint.pformat

    def magic_ri(self):
        self['ri'] = ri = sys.stdin.read()
        return ri

    def magic_i(self):
        return self['ri'].strip()


if __name__ == '__main__':
    main()
