program in interactive mode

Mike Meyer mwm at mired.org
Mon Dec 27 11:00:59 EST 2004


"John Machin" <sjmachin at lexicon.net> writes:

> Mike Meyer wrote:
>>
>> I've discovered a truly elegant trick with python programs that
>> interpret other data.
> Q0. Other than what?

Other than Python code.

>> You make them ignore lines that start with # at
>> the beginning of the line,
> Q1. After the first user accidentally gets a # at the start of a real
> data line, a few hundred lines into their file, then what will you do?
> Fix your script to detect this error and re-issue your documentation,
> emphasising that this is not a general comment convention?

Depends on how you implement it. Possibly issue an error
message. Possibly treat this as data. Possibly treat this as a comment.

> Q2. Then when users 2+ steam up complaining that they have stacks of
> files containing lines like "#### Next section is frappenwanger
> readings in picoHertz ####", and the script is printing out a whole lot
> of what they regard as gobbledegook followed by
> "HashmarkAtStartOfOtherThanLineZeroError", and then stopping, what do
> you do?

You don't implement the hashmark that way, of course.

>> and accept the name of a file to be
>> interpreted as a first argument. Your users can then put
>>
>> #!/usr/bin/env mycode.py
> Q3. Will that work on 'Doze?

Probably not. I don't know if this is part of the Posix compatability
level or not.

> Q4. Doesn't that tie their file to your script, or force other scripts
> to ignore the first line?

This trick is really only applicable to data where you control the
file format. As I mentioned, I use it to treat pickled program
configuration files as executables.

>> at the top of their files, and then treat their files as normal
>> executables. mycode.py has to be on their path; if not, they need to
>> plug in the full path to mycode.py.
>
> Q5. For comparison purposes, could you please post an example of what
> you regard as a filthy ugly trick?

f = __import__(__name__)
f.__dict__['name'] = value

                   <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list