Re: [Tutor] MemoryError

2004-12-08 Thread Michael Janssen
[forgot to cc to the list]


-- Forwarded message --
From: Michael Janssen <[EMAIL PROTECTED]>
Date: Wed, 8 Dec 2004 14:40:46 +0100
Subject: Re: [Tutor] MemoryError
To: Liam Clarke <[EMAIL PROTECTED]>


On Wed, 8 Dec 2004 23:29:38 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote:

> I'm playing with a file, and attempting to replace a section with a
> string, and using the following command -
>
> seg=codeSt[element:endInd+len(endStr]
> codeSt=codeSt.replace(seg, hrefString)
>
> At the replace, I get a MemoryError. It's part of a for loop, but it
> gives the error the first time around.

Hello Liam,

You might try to narrow the code in where the error happen. Read the
file and replace a dummy-seq-string and see what happens. Does this
work:

"test string".replace("st st", "?")

When it works, what is bad with your file? Consider asking
comp.lang.python for a greater audience.

BTW: some time past the base64 module (base64 is a mail attachment
encoding) has a bug leading to a MemoryError when undecoding an empty
base64 string. Any free GB of ram did not help.

You can allways work arround the replace:

new = old[:start] + relace_string + old[end:]

which is ugly but perhaps makes your script running.

regards
Michael
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] hi

2005-01-11 Thread Michael Janssen
On Tue, 11 Jan 2005 11:39:36 +0530, Gopinath V, ASDC Chennai
<[EMAIL PROTECTED]> wrote:

>   Can  any1 please tell me how do i open an editor in python running in
> linux os 

can be as easy as: os.system("editor-cmd")

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reinitializing namespace

2005-01-13 Thread Michael Janssen
On Thu, 13 Jan 2005 13:20:11 +0100, Dimitri D'Or
<[EMAIL PROTECTED]> wrote:

> For some purpose, I would like to reinitialize the main namespace, i.e. I want
> to delete all the variables I have created through the use of functions or
> keyboard entries.

Hello Dimiti,

sound like you're talking about an interactive session. Otherwise
(within a script) it would be a really bad idea to try this (better
put your stuff into functions, that don't create global variables).

Even in an interactive session it sounds like a not that brilliant
idea, especially since I can't think of a way other than using exec
"del %s" % key for appropriate keys from globals(). Finding
"appropiate" keys is one tricky thing.

Why not end your ugly python session and start a new one? You can
define all your imports in the python startfile (on Linux, consult
python manpage. On Windows, I don't know). You can also define useful
functions or variables in your python startfile. This way, you're
really shure that all ugly variables are away without del'iting
anything important.

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reinitializing namespace

2005-01-14 Thread Michael Janssen
On Fri, 14 Jan 2005 09:30:46 +0100, Dimitri D'Or
<[EMAIL PROTECTED]> wrote:

> Thank you for your answer. Actually, my question is not specific to
> interactive sessions. I've written a script that loads some modules, create
> variables and show figures. What I would like to find, is a command for
> clearing all the created variables and close all figures before beginning
> the execution of the script. This command will be placed at the beginning of
> the script and automatically reinitialize the namespace. The utility of this
> command is to guarantee that all the variables that are available in the
> namespace after the execution of the script were created by it and are not
> remainders from older commands or scripts.

In Python you do this by mentally tracking when and by which code
variables get created and by careful programm design. Most the time
it's as easy as:

var1 = func1()
var2 = func2()

It's obviously easy to track which variable gets created by which
function ;-) It's harder when func1 for example looks like:

def func1():
  global var3
  var3 = "something"
  return "somthing"

this way func1 has the *sideeffect* of setting var3. Sideeffects are
sometimes really bad and it's a good idea to a) document them and b)
use them seldom.

Furthermore, reusing old variables can happen and results in hard to
debug bugs. Look at this for-loop:

meaning_full_variable = None
for thing in list_of_things:
if strange_condition():
meaning_full_variable = check(thing)
print thing, meaning_full_variable

Of course, you will reuse the meaning_full_variable whenever
strange_condition() is not met. The print statment will print out the
actual "thing" but possibly the "meaning_full_variable" from an
earlier run.

You solve this issue by setting meaning_full_variable in every
iteration of this loop:

for thing in list_of_things:
  meaning_full_variable = None
  if strange_condition():
  meaning_full_variable = check(thing)
  print thing, meaning_full_variable 

which is a slightly saner version of the code ;-)

Since such problems are a little hard to debug, you should learn to
get wary when you see code like the above example (feel the problems
before running into them).


The main tactic to minimize namespace problems is to use functions and
classes which comes all with their own namespaces. Perhaps you should
post code, you find problematic, and we might find strategies to
restructure it, so that namespace problems are claryfied.


regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How does import work?

2005-02-01 Thread Michael Janssen
On Fri, 28 Jan 2005 18:40:53 +0100, Johan Nilsson
<[EMAIL PROTECTED]> wrote:

>  >>> from scipy.signal.signaltools import *
> 
> /Traceback (most recent call last):
>   File "", line 1, in -toplevel-
> from scipy.signal.signaltools import *
> ImportError: No module named signaltools/
> 
> So I try the methodic way and this works, giving me access to the
> functions I need
> 
>  >>> from scipy import *
>  >>> from scipy.signal import *
>  >>> from scipy.signal.signaltools import *

seems like overkill (and I don't understand why it works better than
the above, but that's more an issue about my understanding and not
about python ;-). Try

from scipy.signal import signaltools # don't import everything from signal

and go on using functions from signaltools like this: signaltools.function

> Now what confuses me is that when I put the above three lines in a file
> (of course without the >>>) and execute them I get a long error message.

perhaps different python versions?

> / Traceback (most recent call last):
>   File "/home/johan/pyton/import_test.py", line 5, in -toplevel-
> from scipy.signal import *

note that the error occours while importing everything from
scipy.signal . The chance are well that this error goes away when
using a reduced import statement.

[snip long traceback talking about scipy and Numerics imports]

> import lapack_lite
> ImportError:
> /usr/local/lib/python2.3/site-packages/Numeric/lapack_lite.so: undefined
> symbol: dgesdd_/

given the fact that I'm no c-programmer this seems to me like a broken
c module. I would try to reinstall Numeric/ scipy (After hunting the
problem down to a specific import).

The most obvious reason why the import in idle works is that idle uses
another version of python (ask sys.version)

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] carriage return on windows

2005-02-01 Thread Michael Janssen
On Mon, 31 Jan 2005 18:01:59 -0600, Victor Rex <[EMAIL PROTECTED]> wrote:

> I played around with this output issue and I love the way it works.
> Now, how do you do this in *nix? I tried the same approach and I get a
> blank line for 5 seconds (or whatever number of cycles you have on your
> example) and the a final line with the last value of the iterable.
> 
> Do you happen to know how this in done?

you might want to flush stdout after printing to it. "print" will
cares for this only when not using that trailing comma. "flush" means
to write immedatly instead to wait for a fair amount of data.

import sys,time
for i in range(8):
sys.stdout.write( "step: %s\r" % i) 
# or: print "step: %s\r" % i,
sys.stdout.flush()
time.sleep(.5)


There's still another possibilty using ansi control sequences. The
dirty way is to print (without trailing comma) and go back to previous
line:

import time
for i in range(8):
print "step: %s\033[A" % i
# print subsystem has done stdout.flush
time.sleep(.5)

It's dirty cause the next line was already entered and thus is written
(as an empty line) and content of older "steps" will stay an screen,
when subsequents lines aren't long enough to overwrite them. Which
also applies to \r:

import sys,time
for i in range(8,0,-1):
# printing 8**8, ..., 0**0 on line. Forget to overwrite
sys.stdout.write( "step: %s\r" % (i**i))
sys.stdout.flush()
time.sleep(.5)

Fixes are to add whitespace to the line. Stepping back with \033[D
fixes the empty newline issue (which is most often not worth the
effort):

import sys,time
for i in range(8,0,-1):
# fixing output length to 30
output = "%-30s" % "step: %s" % (i**i)
length = len(output)
sys.stdout.write(output)
# "print output," would be different, because of implizit spaces
sys.stdout.write("\033[D"* (length))
sys.stdout.flush()
time.sleep(.5)


regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] i have a question???

2005-02-03 Thread Michael Janssen
On Thu, 3 Feb 2005 05:53:31 -0800 (PST), alieks laouhe
<[EMAIL PROTECTED]> wrote:
> This is from a tutorial
> 
> "EXERCISE 3.9
> Use the math library to write a program to print out
> the sin and cos of numbers from 0 to 2pi in intervals
> of pi/6. You will need to use the range() function."
> 
>  Range won't let me use pi/6 as an incremator

correct. The range is for integers only. You _can_ do the exercise
with the range function but it would be easier without (eg. with a
while-loop).

Hint-no-solution: You will need a certain number of intervals, which
(the number of intervalls) is plain integer. Then translate from
current-intervall-number to pi'ish (sorry for that, my english ;-)
value.

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] i have a question???

2005-02-03 Thread Michael Janssen
[the problem provided by alieks]
> >>"EXERCISE 3.9
> >>Use the math library to write a program to print out
> >>the sin and cos of numbers from 0 to 2pi in intervals
> >>of pi/6. You will need to use the range() function."

[Michael]
> > You _can_ do the exercise
> > with the range function but it would be easier without (eg. with a
> > while-loop).

[Pierre]
> I disagree ... :P And your next remark makes it clear range (or even
> better : xrange) is exactly what's needed for that problem !

[Michael's next remark]
> > Hint-no-solution: You will need a certain number of intervals, which
> > (the number of intervalls) is plain integer. Then translate from
> > current-intervall-number to pi'ish (sorry for that, my english ;-)
> > value.

You're right, I've described the range-solution. But what makes you
think, that the range-solution is actually the best/ simplest/
easiest? Within a while-loop you would increment your variable each
time by pi/6 and test if still lower-equal than 2*pi (need care for
float comparision) . With range you need to compute the "certain
number of intervals", then for-loop through the range-generated list
and compute the "pi'ish" value.

There's a loop one way or another ;-) Unless one want to add some
extra exercises like list comprehension and the map function. With the
while-loop, you don't need to know certainly which number of intervals
are needed, you just check if the upper boundary is reached.

Don't know, perhaps I should post examples but alieks might still be
working on the exercise and I don't want to spoil anybodies joy for my
own one ;-) So, please alieks, post your results to us!

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] UselessPython 2.0

2005-04-11 Thread Michael Janssen
On Apr 10, 2005 7:18 AM, Dick Moores <[EMAIL PROTECTED]> wrote:

> I'm a newbie, but would this qualify as a contribution to UselessPython 2.0?

Hello Dick,

don't be shy, or do you suspect it might be too usefull? ;-) I found
it funny, so it must be good enough.

here my remarks:

> def makeStringAllLowercaseAlpha(s):
> """
> Take any string, convert all uppercase alphabetic characters to
> lower case,
> then strip all non-alphabetic characters

[what's bad about non-alphabetic characters?]

> """
> s1 = string.lower(userstring)

oops: this function gets an argument s, here you're using the global
variable userstring. I know, it works, but there will be functions
were mistakes like this weren't just ugly but buggy.

> s2 = ""
> for index in range(len(s1)):
> if s1[index] in string.ascii_lowercase:
> s2 += s1[index]

or easier (much more readable, helps understanding the programm):

for char in s1:
 if char in string.ascii_lowercase:
   s2 += char

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] UselessPython 2.0

2005-04-11 Thread Michael Janssen
On Apr 11, 2005 7:30 PM, Michael Janssen <[EMAIL PROTECTED]> wrote:

> [what's bad about non-alphabetic characters?]

I found it out for myself. To quote from Dick's post:
"A man, a plan, a canal, Panama!" 

Seems like palindromes are allowed not to reflect whitespace and
punctuation (an how could this be?).

cheers
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] redirecting output to logfile and logrotate

2005-07-24 Thread Michael Janssen
On 7/23/05, Winfried Tilanus <[EMAIL PROTECTED]> wrote:

> Somebody else suggested (in private) to make logrotate send a -HUP

this was me. Sorry for that, I'm still unfamilar with gmail. 

Danny's suggestion using RotatingFileHandler is better than mine,
cause it uses a standard module. I suggest to consider using
SysLogHandler for fairly sized server apps which get distributed. This
way the user has full control over the logfiles (where to store, when
to rotate, how to compress and when to delete) by normal configuring
via syslogd and logrotate (Well this means it's normal to configure
syslogd and logrotate for a sysadmin user...).

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] surprising len() results ???

2005-08-09 Thread Michael Janssen
On 8/9/05, Tom Cloyd <[EMAIL PROTECTED]> wrote:

> print len('()ÄäÀÁàáÇçÈÉèéÌÍìíÑñÒÓòóÙÚúù')
> print len('--AaAAaaCcEEeeIIiiNnOOooUUuu')
> 
> the result:
> 
> 54
> 28
> 
> I'm completely mystified by this. All of it. None of it makes sense. This
> program was working fine. Now it doesn't. And those two parameter string
> are plainly the same length, but Python doesn't think so.

on my computer (python 2.3):
>>> s = 'ä' # a-umlaut
>>> len(s)
1
>>> len(s.decode('latin-1')) # prepare for utf-8 encoding
1
>>> len(s.decode('latin-1').encode('utf-8'))
2
>>> len('a'.decode('latin-1').encode('utf-8'))
1

seems like len returns the number of bytes and some encodings uses
more than one byte for certain chars. You can proberbly decode your
strings from utf-8 (or whatever encoding you use (and perhaps encode
it back into a one-char-one-byte encoding [on my system the decoded
(unicode) string is just fine]).

regards 
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Customized endswith/startswith version

2005-08-10 Thread Michael Janssen
On 8/10/05, Negroup - <[EMAIL PROTECTED]> wrote:

> >>> f = open('codes.txt')
> >>> # valid codes starts with 'abc' or '123' or 'ff5'
> >>> [valid for valid in f.readlines() if valid.startswith('abc') or
> valid.startswith('123') or valid.startswith('ff5')]
> 
> This is just an example, in my application I don't need to check
> strings against a huge number of cases. If however it would happen,
> how to modify startswith in order to make it accept a list instead of
> a simple string?
> 
> [valid for valid in f.readlines() if valid.startswith(['abc', '123', 'ff5'])]

the easy way is not to use the string method startwith but write your
own function. So it looks like:

[valid for valid in f.readlines() if startswith(valid, ['abc', '123', 'ff5'])]

(I suppose you have your fun, writing a startswith-function for yourself?)

The hard way is to subclass from str and ovewrite the default
startswith function (or add your own function). Whilst subclassing
isn't hard, here it can be, because strings are immutable types, which
means that a new string is created ervery time we "mutate" one. Which
in turn means that you have to know how to subclass from immutable
types (using __new__ instead of __init__ and things like that I can't
remember and allways have a hard time to figure it out again). And
then you might want to subclass your file-object so that it spit out
strings of your own class?

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] where to insert?

2005-09-23 Thread Michael Janssen
On 9/22/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

> I would like to insert string.join(msgList,"") into the following program 
> where do I insert it?

do you want to build a temporary list and join its elements
afterwards? This can look like:

 # Loop through each substring and build ASCII message
 msgList = []
 for numStr in string.split(inString):
 asciiNum = int(numStr)   # convert digits to a number
 msgList.append(chr(asciiNum))
 # after completing the msgList, join it
 message = "".join(msgList)

Few notes: I have changed eval(numStr) into int(numStr) because int()
does everything you need here. eval() can do more but than just
converting digits into integers but this came at the cost of possible
unpredictable results. For example eval('1.23') is perfectly valid but
allowing floating point numbers wasn't your intention. int('1.23')
will fail with an exception wich is a good thing, because you can
catch the error and report the user back how to make better usage of
your programm. Other examples for unpredictable results with eval
invole eval('os.system("evilcommand")') or eval('range(2147483647)').
The latter would build a very large temporay list with numbers from 0
through 2147483646 which is likly to consume all of your machines
memory!

Second I'm using the "".join(msgList) (join as a string method)
instead of string.join(msgList, "") (join as a function of the string
modul), because it's slightly more readable and you mustn't remember
how string.join want to get its arguments.

Last not least: joining a list of strings instead of adding those
strings directly is a pattern used to avoid the problem that each
'alteration' of a string (as in 'message = message + chr(asciiNum)) is
in fact no alteration of the string but creates a new string every
time (because strings are immutable, i.e. can't be altered in place).
OTOH this problem doesn't seem to be such a big one ... In fact for
small strings and a small numer of them, the joining-a-list approach
can take more time. This is why I prefer to add strings directly and
only switch to an temporary list when I have really many strings or
when I gain some other advantages, when I e.g. want to join the
strings with a newline.


regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help

2005-10-24 Thread Michael Janssen
On 10/24/05, Shi Mu <[EMAIL PROTECTED]> wrote:

> I got confused by the following information from the help for "FIND":
> find(s, *args)
>   find(s, sub [,start [,end]]) -> in

Hello Shi Mu,

this is the help for the function find in the string module, which is
slightly out of date. Better you use the string method find:

###
s = ""
help(s.find)

find(...)
S.find(sub [,start [,end]]) -> int
 ###

so the "in" is simply a typo. find returns an "int" not an "in".
"*args" is the arbitrary number of arguments notation:
http://docs.python.org/tut/node6.html#SECTION00673

which might be misleading and is replaced by "..." in the string
method's online help. The comma before start ist mandatory: *when* you
want to give a "start" argument, you leave out the []-brackets but you
need the comma:

s.find('something', 1)

The builtin documentation ist meant (there are such statements by
Guido van Rossum in the web) as a brief mnemonic for things that are
explained in more detail in the library documentation and the
tutorial.

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Currency conversion

2005-12-12 Thread Michael Janssen
On 12/12/05, David Holland <[EMAIL PROTECTED]> wrote:

> wouldn't it be better to change :-
> "def rupees_from_dollars(d_doll):
> return 43*(d_doll)
>
> etc to" :-
> "def conversiond_doll, x):
> return x*(d_doll)
> "

> You can then have one function for all currencies and
> would be less code and easier to maintain.

Having just one function instead of several is fine, perhaps even for
the cost of one additional function argument. Good to maintain, nice
flexibility. OTOH usability is important, too. Eg. I'm always driven
crazy by concurrency conversions, never can imaging what to take and
what to get, so I prefer a single pretty clear named function for my
use case like "rupees_from_dollars" instead of a "conversion(d_doll,
dollar_exchange_rate)" (the hard thing for me is to remember, if it
must be dollar_exchange_rate or 1/dollar_exchange_rate)

What you can often/sometimes find in code is an all purpose
function/class with full flexibility and additionally some easy-to-use
functions provided for the common case. As in the email package with
it's message_from_string and message_from_file functions. You can read
about them and their relationship to the package in:
http://docs.python.org/lib/node584.html

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dictionaries

2006-01-27 Thread Michael Janssen
On 1/26/06, John Fouhy <[EMAIL PROTECTED]> wrote:

> >>> for name in pairs:
> ...  print '%*s --> %*s' % (maxKey, name, maxValue, pairs[name])

thanks for pointing out the '*' syntax. Ignorant as I am I have had to
write things like

   print '%%%ss --> %%%ss' % (maxKey, maxValue) % (name, pairs[name])

for years. Thanks :-)
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fill in a web form

2006-03-18 Thread Michael Janssen
On 3/18/06, David Holland <[EMAIL PROTECTED]> wrote:

> Is there a way in python to automatically put values in a web page ?  For
> example I used to travel on a train (I live in the UK) that is always late.
> Is there a way to automatically to do this ?
> I can't think of  how to do it.

You don't "fill" the form fields, but rather you perform the same
action the browser would performs with a filled form.

You must look into the html-form-tag and its action- and
method-attributes. "Action" is the url to the script receiving the
filled form. Method tells the browser how to send the data: "get"
means to generate an url like "www.google.com?q=python', ie an url
with the content of the form. Method "post" means to send the data
"inline". urllib2.urlopen handles this (its data parameter for "post"
data). Perhaps urllib.urlencode is of interest for you (before
generating a get-url, urlencode the data, so that it renders a sane
url).

This answere is a bit short and might leaves you with a lot of open
questions. If so, ask :-)

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regex help

2006-08-28 Thread Michael Janssen
On 8/26/06, William O'Higgins Witteman <[EMAIL PROTECTED]> wrote:

> I want a case-insensitive, verbose pattern.  I have a long-ish list of
> match criteria (about a dozen distinct cases), which should be all "or",
> so I won't need to be clever with precedence.

BTW I find it easier not to use re.VERBOSE which ignores any
whitespace. Instead I use the silent-string-continuation feature to
put my comments in the right place:

regexp_str = ('one' # matches one
'|two' # matches two
'|three' # matches three
)

there must be no commas: I don't want a tuple but rather the strings
to be concatenated within the brackets. But then:

regexp_tuple = ('one', 'two', 'three')
regexp_str = '|'.join(regexp_tuple)

This way you do not need to manually sets the "|" specialchars.

The python modul /usr/lib/python2.4/tokenize.py comes with lots of
examples, especially with OR'ed pattterns.

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting a list in an add order

2006-09-06 Thread Michael Janssen
On 9/6/06, Matt Williams <[EMAIL PROTECTED]> wrote:

> The input is done by specifying a directory, and using glob to find the
> ".tex" filenames.
>
> However, I want to process them so that they are arranged in the correct
> order, which means I need to sort the list of files. Of course, because
> they aren't named in any (obvious) order, I'm a bit stuck.

a clean way to do it would be to rename the files so that they can be
sorted. OTOH this involves not much python and is entirely boring.
OTOOH renaming the files might help you later on, when you need them
ordered for other purposes.

> I thought about using a dictionary to map names and order: so {"OAF":1,
> "Valuation":2...etc}, but I don't don't know how to take it on from
> here. I was thinking of looking up the filename in the dictionary (using
> .startswith() to get some basic rough-matching capacity) and then using
> that to return the order that the files should be handled in.

you can specify your own comparing funtion for aList.sort(). Here's a
comparing function, that does the same as the default:

def mysort(a, b):
return cmp(a, b)

aList.sort(mysort)

Now you can simply lookup other values for filenames a and b and
compare that via python builtin cmp function.

You can also use a list of filenames and sort by comparing the indecees.

The bad thing is, that you have to specify all those filenames within
the script - which defeats many of the advantages to read them in
dynamically via glob ;-) Is there any chance to determine the
sortorder from the files' content?

rough-matching is another topic which might bring up some nice algorithms.

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regex newbie question

2008-05-09 Thread Michael Janssen
On Fri, May 9, 2008 at 3:16 AM, Dick Moores <[EMAIL PROTECTED]> wrote:
>
> At 04:32 PM 5/8/2008, Steve Willoughby wrote:
>>
>> That would be r'^\d\d(\d\d)*$'
>
> I bought RegexBuddy () today, which is a big
> help. However, it has a comment about your regex

The comment on http://www.rcblue.com/Regex/all_even_number_of_digits.htm
is telling us, that the group only "captures" the last repetition.
That's fine for the problem given, since we're not interessted in
whatever the groups "captures". The group is solely used to enforce
repetitions of two-digits-together.

Since we're not interested in the group's submatch, we can ignore it beforehand:

>>> mt = re.search(r'^\d\d(\d\d)*$', '1234') # capture (\d\d)
>>> mt.group() # the full match
'1234'
>>> mt.group(1) # here is the captured submatch of group one
'34'
>>> mt = re.search(r'^\d\d(?:\d\d)*$', '1234') # non-grouping version, simply 
>>> enforce two-digits, do not capture
>>> mt.group()
'1234'
>>> mt.group(1)
Traceback (most recent call last):
  File "", line 1, in ?
IndexError: no such group

the benefit of such a non-group is, that it's clear for the reader,
that you're not intending to use the submatch later on and it keeps
mt.group(*) clean. When I first saw the grouping versions, I've
actually asked myself, hey, what you're gonna make with the group's
match...

regards
Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor