Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: A few beginning questions (http://www.velocityreviews.com/forums/t319661-re-a-few-beginning-questions.html)

Michele Simionato 07-14-2003 05:30 PM

Re: A few beginning questions
 
"richardc" <richardc@hmgcc.gov.uk> wrote in message news:<3f129a8a$1@mail.hmgcc.gov.uk>...
> 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

Thomas Heller 07-14-2003 06:05 PM

Re: A few beginning questions
 
mis6@pitt.edu (Michele Simionato) writes:

> "richardc" <richardc@hmgcc.gov.uk> wrote in message news:<3f129a8a$1@mail.hmgcc.gov.uk>...
>> 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

Karl Scalet 07-15-2003 07:32 AM

Re: A few beginning questions
 
Thomas Heller schrieb:
> mis6@pitt.edu (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


richardc 07-15-2003 12:41 PM

Re: A few beginning questions
 

"Duncan Booth" <duncan@NOSPAMrcp.co.uk> 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



Duncan Booth 07-15-2003 02:11 PM

Re: A few beginning questions
 
"richardc" <richardc@hmgcc.gov.uk> wrote in
news:3f13f674$1@mail.hmgcc.gov.uk:

> 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 duncan@rcp.co.uk
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?

Robin Munn 07-15-2003 05:21 PM

Re: A few beginning questions
 
richardc <richardc@hmgcc.gov.uk> 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 <rmunn@pobox.com> | 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

Irmen de Jong 07-15-2003 06:15 PM

Re: A few beginning questions
 
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


Duncan Booth 07-16-2003 09:04 AM

Re: A few beginning questions
 
mwilson@the-wire.com (Mel Wilson) wrote in news:AhCF/ks/KjWP089yn@the-
wire.com:

> In article <Xns93B968108D2DCduncanrcpcouk@127.0.0.1>,
> Duncan Booth <duncan@NOSPAMrcp.co.uk> 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 duncan@rcp.co.uk
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?

Robin Munn 07-18-2003 02:30 PM

Re: A few beginning questions
 
Thomas Heller <theller@python.net> wrote:
> mis6@pitt.edu (Michele Simionato) writes:
>
>> "richardc" <richardc@hmgcc.gov.uk> wrote in message news:<3f129a8a$1@mail.hmgcc.gov.uk>...
>>> 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/...rminology.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 <rmunn@pobox.com> | 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

Thomas Heller 07-18-2003 02:56 PM

Re: A few beginning questions
 
>>> 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


All times are GMT. The time now is 11:42 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.