[GRASS-dev] grass and python

David Finlayson david.p.finlayson at gmail.com
Mon Aug 14 00:47:16 EDT 2006


The Python interpreter is interactive, but it is not a general purpose
shell. You would need to wrap external programs in an os.system or
os.popencall. One attractive way of getting around this is to pre-wrap
each program
in a python object. For example:

class Display:

    def rast(self, rastername):
        os.system("d.rast %s" % rastername)

Then the syntax in the Python interpreter would be:

> import Display
> d = Display.display()
> d.rast(rastername)

Which isn't so bad. This is what things would look like in PyShell (or
PyCrust) which is a wxWindows-compatible python interpreter that you can use
in wxWindows GUI's.

The most advanced Python interpreter is IPython. This has been tweaked for
interactive use by the Scipy (numeric programming) folks so that some of the
Python decorations can be dropped in the above example:

> import Display
> d = Display.display()
> d.rast rastername

No more parenthesis needed. Also, IPython has "magic" operators that allow
you to "shell-out" to the underlying shell. Simply prepend a command with a
% and it will bypass the python interpreter and send it to the shell:

> %ls

or

>%dir

The IPython interpreter is designed for interactive use. It is the Scipy
answer to Matlab's interactive prompt. It has a lot of nice shell-like
features and can be extended for use by GRASS (I think). Unfortunately, I
don't think IPython is as well integrated with Windows as Unix. At least on
my casual observations.

David

On 8/13/06, David Finlayson <david.p.finlayson at gmail.com> wrote:
>
> The Python interpreter is interactive, but it is not a general purpose
> shell. You would need to wrap external programs in an os.system or
> os.popen call. One attractive way of getting around this is to pre-wrap
> each program in a python object. For example:
>
> class Display:
>
>     def rast(self, rastername):
>
>
>
>
> On 8/12/06, Glynn Clements <glynn at gclements.plus.com > wrote:
> >
> >
> > Trevor Wiens wrote:
> >
> > > > If anyone can find a real language which is "good enough" on all of
> > > > Unix, Windows (Cygwin and native) and OSX, I'll quite happily
> > rewrite
> > > > all of GRASS' shell scripts.
> > >
> > > Python is easy to install on Windows. So with an offer like that, I
> > > can't help asking if any of the interactive Python shells are to your
> > > knowledge good enough. Or perhaps what in you mind defines an
> > > interactive shell environment that is good enough to replace bash?
> >
> > The main issue is the ability to have scripts which can be run as e.g .
> > "d.save", and not having to use "d.save.py". This needs to work both
> > from a shell and from system/exec/popen/spawn/etc functions provided
> > by various languages.
> >
> > --
> > Glynn Clements <glynn at gclements.plus.com>
> >
>
>
>
> --
> David Finlayson
>



-- 
David Finlayson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/grass-dev/attachments/20060813/b2890efa/attachment.html


More information about the grass-dev mailing list