Re: A few beginning questions

Discussion in 'Python' started by Michele Simionato, Jul 14, 2003.

  1. "richardc" <> wrote in message news:<3f129a8a$>...
    > Ive just started playing with Python and have a couple of questions.
    >
    > Im looking for an agument parsing library (from the command line), Ive come
    > across optik and argtools. What other ones are there are any of them any
    > good, is there a 'standard' lib for doing this.
    >


    optparse (a.k.a. optik) is great, whereas I never made my mind up to
    getopt. To "cure" getopt, I needed to define my own routines for
    argument parsing; after I started using optparse I had the satisfaction
    of erasing all of them ;)

    I vote optparse as the best addition to Python 2.3!

    (okay, well, there are other niceties, but maybe optparse is the
    most useful thing; who does agreee with me ?)



    Michele
     
    Michele Simionato, Jul 14, 2003
    #1
    1. Advertising

  2. (Michele Simionato) writes:

    > "richardc" <> wrote in message news:<3f129a8a$>...
    >> Ive just started playing with Python and have a couple of questions.
    >>
    >> Im looking for an agument parsing library (from the command line), Ive come
    >> across optik and argtools. What other ones are there are any of them any
    >> good, is there a 'standard' lib for doing this.
    >>

    >
    > optparse (a.k.a. optik) is great, whereas I never made my mind up to
    > getopt. To "cure" getopt, I needed to define my own routines for
    > argument parsing; after I started using optparse I had the satisfaction
    > of erasing all of them ;)


    I have not yet tried optparse. Is it able to parse Windows' style
    command lines (use '/' instead of '-', ignore case of command line
    options, use long options with a single '-' or '/')?

    Thomas
     
    Thomas Heller, Jul 14, 2003
    #2
    1. Advertising

  3. Michele Simionato

    Karl Scalet Guest

    Thomas Heller schrieb:
    > (Michele Simionato) writes:
    >
    >
    > I have not yet tried optparse. Is it able to parse Windows' style
    > command lines (use '/' instead of '-', ignore case of command line
    > options, use long options with a single '-' or '/')?


    Just tried it, does not handle '/' the same way as '-',
    and does not ignore case sensitivity.
    Yes, it handles short and long forms. You add options to the
    OptionParser and each option can have a short and a long form, i.e:

    option_parser.add_option('-v', '--verbose', action='store_true')

    After:
    (cfg, args_) = parser.parse_args(args)

    cfg automatically has verbose as an attribute, nice!

    Karl
     
    Karl Scalet, Jul 15, 2003
    #3
  4. Michele Simionato

    richardc Guest

    "Duncan Booth" <> wrote in message
    SNIP
    > If the file isn't too large you can call the readlines() method and simply
    > reverse the result as above. You may find this incovenient for very large
    > files though.


    It may well be

    > I can't think of many cases where I would want to read a
    > whole file in reverse, if all you want is the last few lines of a massive
    > file then just seek near the end before reading the remainder and reverse
    > the resulting list of lines.


    This is exactly what Im doing, what Im actually doing is seeking to 1024
    bytes from the end, then looking for the string
    'startref\r\n<number>\r\n%%EOF' where <number> is an integer in decimal
    format

    I wanted to scan the lines backwards until I found %%EOF (as its likley to
    be the last line) and do it that way. I might do that or might use a regex
    and just let that find it.

    Cheers

    Rich
     
    richardc, Jul 15, 2003
    #4
  5. Michele Simionato

    Duncan Booth Guest

    "richardc" <> wrote in
    news:3f13f674$:

    > I wanted to scan the lines backwards until I found %%EOF (as its
    > likley to be the last line) and do it that way. I might do that or
    > might use a regex and just let that find it.


    Ok, try this. (On Windows systems the file has to be opened in binary mode
    for this to work.)

    ----- cut here -----
    from __future__ import generators

    def readbackwards(aFile, blocksize=1024):
    def blocks(length, blocksize):
    for pos in range(length, 0, -blocksize):
    yield pos, 1024
    yield 0, pos

    aFile.seek(0, 2) # Seek to end
    eof = aFile.tell()
    residue = None
    for pos, size in blocks(eof, blocksize):
    aFile.seek(pos, 0)
    block = aFile.read(blocksize)
    if residue is not None:
    block += residue
    lines = block.split('\n')
    lines.reverse()

    residue = lines.pop()
    for line in lines:
    if line.endswith('\r'):
    line = line[:-1]
    yield line

    if residue is not None:
    yield residue

    if __name__=='__main__':
    input = file('test.txt', 'rb')
    for line in readbackwards(input, blocksize=8192):
    if line.startswith("%%EOF"):
    print line
    break
    input.close()

    ----- end cut ------

    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
     
    Duncan Booth, Jul 15, 2003
    #5
  6. Michele Simionato

    Robin Munn Guest

    richardc <> wrote:
    > Im guessing from the lack of responce that there arnt many python editors
    > which run on OSX.


    I didn't see your original post, so I missed that question. One editor
    I'd suggest you try out is Vim (http://www.vim.org/). Its name stands
    for "Vi IMproved"; it's basically the good old Unix editor vi with lots
    of extra features useful for programming, like syntax highlighting and
    folding. (For Python, use "set foldmethod=indent" -- it works like a
    charm!)

    Cons: It's vi. That means a pretty steep learning curve until you've
    gotten used to modal editing, where you go into "insert mode" to type
    text and go back into "command mode" (by hitting ESC) to move the
    cursor, copy and paste text, do searches, move text around, etc. Many
    people try vi and give up in disgust after five minutes.

    Pros: It's vi. That means a whole lot of power at your fingertips once
    you've gotten used to this editing style. I find that personally, I'm
    about twice as productive with vi as with another editor. YMMV, of
    course, but that's been my experience. And because vi has been a
    standard Unix editor for so long, *any* Unix system you use *will* have
    your favorite editor on it. Plus, Vim is available for all those
    non-Unix OS'es as well, so you'll never be forced to re-train your
    fingers on Yet Another Editor when you switch operating systems.

    If you don't like the vi style of modal editing, there's always the
    other grand old standard Unix editor, emacs. I'm sure there are several
    versions of Emacs that will run on OSX; but since I don't use Emacs
    myself, I can't tell you where to find them. One warning about Emacs,
    though this is mostly my personal preferences showing: its nickname is
    "Escape-Meta-Alt-Control-Shift". If you're used to, for example, hitting
    Command-Shift-S for "Save As..." then you might prefer Emacs' key style
    over vi's. Myself, I find that my touch-typing-trained fingers prefer
    hitting several keys in sequence (the way vi tends to do things) than
    several keys at once (the way Emacs tends to do things). But again, Your
    Mileage May Vary.

    Hope this helps.

    --
    Robin Munn <> | http://www.rmunn.com/ | PGP key 0x6AFB6838
    -----------------------------+-----------------------+----------------------
    "Remember, when it comes to commercial TV, the program is not the product.
    YOU are the product, and the advertiser is the customer." - Mark W. Schumann
     
    Robin Munn, Jul 15, 2003
    #6
  7. Robin Munn wrote:

    > I'd suggest you try out is Vim (http://www.vim.org/). Its name stands


    [...]

    > Cons: It's vi. That means a pretty steep learning curve until you've
    > gotten used to modal editing, where you go into "insert mode" to type
    > text and go back into "command mode" (by hitting ESC) to move the
    > cursor, copy and paste text, do searches, move text around, etc. Many
    > people try vi and give up in disgust after five minutes.


    Actually, moving the cursor around, erasing characters (del, bs) and
    perhaps some other frequent actions *don't* require you to switch to
    command mode in Vim...
    So perhaps it is a lot less painful to start using Vim (compared
    to a 'true' vi).

    --Irmen
     
    Irmen de Jong, Jul 15, 2003
    #7
  8. Michele Simionato

    Duncan Booth Guest

    (Mel Wilson) wrote in news:AhCF/ks/KjWP089yn@the-
    wire.com:

    > In article <Xns93B968108D2DCduncanrcpcouk@127.0.0.1>,
    > Duncan Booth <> wrote:
    >>The usual way is simply to reverse the list before iterating over it.
    >>
    >> l = ['some', 'text', 'in', 'a', 'list' ]
    >> l_reversed = l.reverse()
    >> for item in l_reversed:
    >> do something to item

    >
    > Trouble. l.reverse() reverses l in place. So just
    >
    > l.reverse()
    > for item in l:


    Duh.

    >>> for i in range(100):

    print "I must test code before posting it to Usenet"

    Thanks for correcting that stupid blunder.

    Yes, if it is safe to reverse the original list, just reverse and iterate
    over it as corrected above. If not, copy and reverse it:

    >>> l = ['some', 'text', 'in', 'a', 'list' ]
    >>> l_reversed = list(l)
    >>> l_reversed.reverse()
    >>> for item in l_reversed:

    print item


    list
    a
    in
    text
    some
    >>>


    (Both code snippets tested this time.)

    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
     
    Duncan Booth, Jul 16, 2003
    #8
  9. Michele Simionato

    Robin Munn Guest

    Thomas Heller <> wrote:
    > (Michele Simionato) writes:
    >
    >> "richardc" <> wrote in message news:<3f129a8a$>...
    >>> Ive just started playing with Python and have a couple of questions.
    >>>
    >>> Im looking for an agument parsing library (from the command line), Ive come
    >>> across optik and argtools. What other ones are there are any of them any
    >>> good, is there a 'standard' lib for doing this.
    >>>

    >>
    >> optparse (a.k.a. optik) is great, whereas I never made my mind up to
    >> getopt. To "cure" getopt, I needed to define my own routines for
    >> argument parsing; after I started using optparse I had the satisfaction
    >> of erasing all of them ;)

    >
    > I have not yet tried optparse. Is it able to parse Windows' style
    > command lines (use '/' instead of '-', ignore case of command line
    > options, use long options with a single '-' or '/')?
    >
    > Thomas


    No. From the optparse documentation:

    Some other option syntaxes that the world has seen include:

    + a hyphen followed by a few letters, e.g. -pf (this is not the same
    as multiple options merged into a single argument.)
    + a hyphen followed by a whole word, e.g. -file (this is technically
    equivalent to the previous syntax, but they aren't usually seen in
    the same program.)
    + a plus sign followed by a single letter, or a few letters, or a
    word, e.g. +f, +rgb.
    + a slash followed by a letter, or a few letters, or a word, e.g.
    /f, /file.

    optparse does not support these option syntaxes, and it never will.
    (If you really want to use one of those option syntaxes, you'll have
    to subclass OptionParser and override all the difficult bits. But
    please don't! optparse does things the traditional Unix/GNU way
    deliberately; the first three are non-standard anywhere, and the
    last one makes sense only if you're exclusively targeting
    MS-DOS/Windows and/or VMS.)

    (This is from http://www.python.org/doc/2.3b2/lib/optparse-terminology.html)

    Is Windows-style arguments (with slashes) *that* important to you?
    Because there's absolutely nothing wrong with following the standard
    that's been established for over twenty years... :)

    --
    Robin Munn <> | http://www.rmunn.com/ | PGP key 0x6AFB6838
    -----------------------------+-----------------------+----------------------
    "Remember, when it comes to commercial TV, the program is not the product.
    YOU are the product, and the advertiser is the customer." - Mark W. Schumann
     
    Robin Munn, Jul 18, 2003
    #9
  10. >>> optparse (a.k.a. optik) is great, whereas I never made my mind up to
    >>> getopt. To "cure" getopt, I needed to define my own routines for
    >>> argument parsing; after I started using optparse I had the satisfaction
    >>> of erasing all of them ;)

    >>
    >> I have not yet tried optparse. Is it able to parse Windows' style
    >> command lines (use '/' instead of '-', ignore case of command line
    >> options, use long options with a single '-' or '/')?
    >>
    >> Thomas

    >
    > No. From the optparse documentation:

    [snip]

    > Is Windows-style arguments (with slashes) *that* important to you?
    > Because there's absolutely nothing wrong with following the standard
    > that's been established for over twenty years... :)


    You (or me, at least) have to follow the standard that is used on the
    system where you work on. Well, sometimes at least.

    Example: a localserver COM object is started with an /Automate command
    line flag. And MS requires that the COM object registers itself when the
    /RegServer or -regserver (case insensitive) options are used.

    Anyway, I have my own w_getopt for windows now ;-)

    Thomas
     
    Thomas Heller, Jul 18, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Parker
    Replies:
    3
    Views:
    566
    Hans Kesting
    Jun 25, 2004
  2. Replies:
    10
    Views:
    565
  3. Murali
    Replies:
    2
    Views:
    593
    Jerry Coffin
    Mar 9, 2006
  4. Jesse B.
    Replies:
    9
    Views:
    243
    Jesse B.
    Mar 27, 2010
  5. Owatch
    Replies:
    2
    Views:
    226
    Owatch
    Mar 9, 2013
Loading...

Share This Page