[Tutor] Dumb question on emacs and python ...

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sat Dec 27 21:06:47 EST 2003


> I have another problem if you are willing to humor me: tell me about
> getopt and how this can be used to see lower or mixed case matches.  I
> am confused by the documentation I found through the python.org site.
> It seems to imply that the gnu getopt is unlike the one in UNIX and may
> not work in the same manner.

Hi Herschel,

'getopt' is actually a little dated --- if you're trying to read option
flags from the command line, you may want to use the 'optparse' module
instead:

    http://www.python.org/doc/lib/module-optparse.html


> Moreover, I cannot see how any of the options are of any use to me for
> this problem.

Hmmm... What kind of problem are you trying to solve?


>From your previous question, it sounded like you were trying to write a
'grep' string searching program.  If so, you may want to look at the
regular expression library ('re'):

    http://www.python.org/doc/lib/module-re.html

A.M. Kuckling has written a small "howto" tutorial on regular expressions
that you may find useful:

    http://www.amk.ca/python/howto/regex/


The option parsing libraries are meant to be used when we want to define
what kind of flags the user can set for us at the command line.  In our
last email, I asked if you could do this at the command line:

   ls -l some_filename

If we were to write our own version of th 'ls' command, then an option
parsing library would be responsible for looking at that '-l' section and
tell us that the user's requesting a "long" output.

So optparse/getopt is a command-line user interface thing.  Are you trying
to parse command line arguments?  Tell us more about the problem you're
trying to solve.



> I was right about the lack of color coding of the code was significant,
> though I have no idea how the simple cp command so altered the file.

>From reading the thread, my best guess at the moment was that your file
had a literal asterisk at the end of the filename.  The file extension of
your program, then, was perhaps literally something like:

    .py*

There's a section in your .emacs configuration that tells Emacs the file
extensions it should use to detect Python source files.  That extension is
'.py', not '.py*', so renaming the file to have no asterisk fixed the
problem, and allowed Emacs to recognize the file as Python source code.

(In any case, having an asterisk in there is probably a bad idea in Unix,
because the shell treats unescaped asterisks as 'globbing' metacharacters
that can stand for anything.)


Anyway, I hope this helps!  Please feel free to ask more questions.




More information about the Tutor mailing list