A suggestion for python 2.1.

Raja S. raja at cs.indiana.edu
Thu Oct 19 20:10:36 EDT 2000


olczyk at interaccess.com (Thaddeus L. Olczyk) writes:

>On 19 Oct 2000 14:01:03 GMT, raja at cs.indiana.edu (Raja S.) wrote:
> > since you seem to be running your scripts under Unix, why don't you just
> > use the conventional shell trick ?
> >
>
> Why do people always jump on stupid and rash assumptions.
> At no point do I say that I am using UNIX. There are no statements
> that rule out all other operating systems.
...

My my ... :-)
Whoa. :-) Relax.  You asked a question.  Made a suggestion.  Don't like it?
No sweat.  No one was critizing your proposal.  Does sound interesting.
But till that happens was just suggesting an alternative.

[Incidentally you didn't mention any platform.  But you did use / as your
dir seperator and you also mention emacs --- something used more under Unix
than Windows (though I personally use ntemacs on my laptop when running
windows).  I also run cygwin so have the 'bash' shell on all my platforms.] 

In any event, at the risk of raising your ire again :-), how about the
following:

1. Create an alias or put the following in your bin dir, say call it 'pscript'

   python -c "script='$1'; execfile('/u/raja/scripts/Script.py')"

2. Place Script.py where ever you want (altering the above path accordingly)

#-------------------------
import sys, string, os, os.path

try:
    psp = os.environ['PYTHONSCRIPTPATH']
except KeyError:
    print "PYTHONSCRIPTPATH not set"
    sys.exit()

def findFile (paths, f):
    for p in paths:
        fp = os.path.join(p, f)
        if os.path.isfile(fp):
            return fp
    else:  # file isn't in the list of paths
        return None

paths = string.split(psp, ":")  
f = findFile(paths, script)

if f:
    #print "execfile: ", f
    execfile(f)
else:
    print "Python script not found: %s" % script
#-------------------------

[One could of course use Perl to search PYTHONSCRIPTPATH before invoking
python but lets keep it within Python]

3.  pscript foo.py  will use your PYTHONSCRIPTPATH to run foo.py

Granted not as simple as having Python itself use PYTHONSCRIPTPATH but
better than having to type:

> python directoryWhereCleanupIsStored/cleanup.py

Seems portable across platforms but for some preliminary installation work.

> The fact is that I am A REAL PROGRAMMER who writes REAL PROGRAMS
> that are used in REAL CIRCUMSTANCES, not some student who tools around
> on one computer. 
...
> Maybe when you get out in the real world
> and do some real world programming you will understand this.

Amusing :-).  You may be surprised what some students do (and have been
doing)  apart from "tooling around on one computer" ... ;-)
Talking about "assumptions", just because a post comes from a .edu domain
doesn't necessarily mean its a student ...

Anyway, just kidding with you :-)

Hope you solve your problem to your satifaction.

Best wishes,
Raja




More information about the Python-list mailing list