Re: [Tutor] working with new classes

2005-03-09 Thread Sean Perry
C Smith wrote:
###
class Ring(list):
def __init__(self, l):
self[:] = l
self._zero = 0
   
def turn(self, incr=1):
self._zero+=incr

def __getitem__(self, i):
return self[(i-self._zero)%len(self)]
l=Ring(range(10))
print l
l.turn(5)
print l#same as original
print l[0] #crashes python
###
This is a classic mistake. l[0] --> l.__getitem__(l, 0), which you 
understand. However in __getitem__ you then call self[] which will just 
call __getitem__ again. and again. and again. 

The way out is to explicitly call the parent's __getitem__.
def __getitem__(self, i):
# list is out parent, call its method
return list.__getitem__(self, (i - self._zero) % len(self))
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] working with new classes

2005-03-09 Thread Kent Johnson
Sean Perry wrote:
C Smith wrote:
###
class Ring(list):
def __init__(self, l):
self[:] = l
self._zero = 0
   def turn(self, incr=1):
self._zero+=incr
def __getitem__(self, i):
return self[(i-self._zero)%len(self)]
l=Ring(range(10))
print l
l.turn(5)
print l#same as original
print l[0] #crashes python
###
This is a classic mistake. l[0] --> l.__getitem__(l, 0), which you 
understand. However in __getitem__ you then call self[] which will just 
call __getitem__ again. and again. and again. 

The way out is to explicitly call the parent's __getitem__.
def __getitem__(self, i):
# list is out parent, call its method
return list.__getitem__(self, (i - self._zero) % len(self))
Two other changes:
- in __init__(), instead of modifying self, I think it is safer to call list.__init__(). This is the 
usual thing do do in a subclass.
- You can change the behaviour of print by defining __repr__() or __str__().

Here is a complete version that seems to work, though you may find other list methods you need to 
override:

class Ring(list):
def __init__(self, l):
list.__init__(self, l)
self._zero = 0
def turn(self, incr=1):
self._zero+=incr
def __getitem__(self, i):
return list.__getitem__(self, (i - self._zero) % len(self))
def __repr__(self):
return repr([self[i] for i in range(len(self))])
l=Ring(range(10))
print l
l.turn(5)
print l
print l[0]
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] getting a webpage via python

2005-03-09 Thread Kent Johnson
Kent Johnson wrote:
Paul Tremblay wrote:
So I just make a file called /etc/router_passwords  and include
something like
WRT54G username password
Then parse the file, and supply the info to the password handler? This
is easy to do, and I guess it is secure. 
The book "Foundations of Python Network Programming" has a sample program that uses a custom 
urllib2.HTTPPasswordMgr to ask the user for the username and password, maybe you would like to use that?

The examples can be downloaded from the book Web site at
http://www.apress.com/book/bookDisplay.html?bID=363
Look for the one called dump_info_auth.py
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Acessing files in Windows 2000

2005-03-09 Thread Dave S
Danny Yoo wrote:
[Windows bashing cut]
Python's support for Windows stuff is actually quite good, thanks to the
work of Mark Hammond:
   http://starship.python.net/crew/mhammond/
A lot of us here do use Windows for COM programming.  Let's get back to
talking about Python programming and let's help Dave with his problem.
The os.path module contains a function called expanduser() that returns
the home directory of a given user.
   http://www.python.org/doc/lib/module-os.path.html#l2h-1720
I believe this should reliably return a path that looks something like
'\\Documents and Settings\\[username]' on Windows systems.
Dave, what happens if you try something like this?
##
import os.path
print os.path.expanduser('~/memo.txt')
f = open(os.path.expanduser('~/memo.txt'))
##
Show us what you see, and we can work from there.  Can you also give us
the complete path where you see 'memo.txt' on your system?  Just right
click the file; it should show up somewhere on the file's property page.
Best of wishes to you!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
 

Hello again,
Wow what a lot of replies.
OK here goes. I know little about win 2000 so when I
My computer > C > My documents I assumed that was where the my documents 
folder was,
Having gone through what you guys said I also have

C:\Documents and Settings\Administrator\My Documents
So I copied memo.txt to C:\Documents and Settings\Administrator\My 
Documents, ammended my code

memo_file = 'C:\Documents and Settings\Administrator\My Documents\memo.txt'
csv_file = 'c:\data.csv'
def palm_conv():
# The money sheet
map1 = [[0]*5 for n in xrange(42)]
map1_count = 6
# The time sheet
map2 = [['']*7 for n in xrange(75)]
map2_count = 0
day_map = {'sun':0, 'mon':1, 'tue':2, 'wed':3, 'thu':4, 'fri':5, 'sat':6}
sh_offset = 0
ot_offset = 0
ca_offset = 0
pa_offset = 0
su_offset = 0
time_slot = 0
text_slot = 0
memo = open(memo_file, 'r')
count = 0
raw_line = ' '
...
And got the following again ...
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\Desktop\palm.py", line 
268, in ?
palm_conv()
File "C:\Documents and Settings\Administrator\Desktop\palm.py", line 54, 
in palm_conv
memo = open(memo_file, 'r')
IOError: [Errno 2] No such file or directory: 'C:\\Documents and 
Settings\\Administrator\\My Documents\\memo.txt'
>>>

So I tried your test code & got ...

>>>
>>>
>>> import os.path
>>> print os.path.expanduser('~/memo.txt')
C:\Documents and Settings\Administrator/memo.txt
>>> f = open(os.path.expanduser('~/memo.txt'))
Traceback (most recent call last):
File "", line 1, in ?
f = open(os.path.expanduser('~/memo.txt'))
IOError: [Errno 2] No such file or directory: 'C:\\Documents and 
Settings\\Administrator/memo.txt'
>>>

Now starting to doubt my sanity I again re-checked C:\Documents and 
Settings\Administrator\My Documents
and yes I do have a memo.txt there.

In C:\Documents and Settings I have the folders administrator, all users 
and compy, I put a copy of memo.txt in
compy as well & changed my code, same problem

Any ideas
Dave
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Acessing files in Windows 2000

2005-03-09 Thread Bill Mill
Dave,

>  >>>
>  >>>
>  >>> import os.path
>  >>> print os.path.expanduser('~/memo.txt')
> C:\Documents and Settings\Administrator/memo.txt
>  >>> f = open(os.path.expanduser('~/memo.txt'))
> Traceback (most recent call last):
> File "", line 1, in ?
> f = open(os.path.expanduser('~/memo.txt'))
> IOError: [Errno 2] No such file or directory: 'C:\\Documents and
> Settings\\Administrator/memo.txt'
>  >>>
> 
> Now starting to doubt my sanity I again re-checked C:\Documents and
> Settings\Administrator\My Documents
> and yes I do have a memo.txt there.
> 

Using all forward slashes works fine for me. Here's a cut-n-paste from
the command line (touch creates a blank file, ls lists dirs, in case
you didn't know):

C:\Documents and Settings\WMill>touch test.txt

C:\Documents and Settings\WMill>ls test.txt
test.txt

C:\Documents and Settings\WMill>C:\Python24\python.exe
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('c:/Documents and Settings/WMill/test.txt')
>>>

I'm really pretty convinced that the file you're talking about doesn't
exist, or you don't have the security permissions to open it.

Peace
Bill Mill
bill.mill at gmail.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regular expression question

2005-03-09 Thread Mike Hall
I'm having some strange results using the "or" operator.  In every test 
I do I'm matching both sides of the "|" metacharacter, not one or the 
other as all documentation says it should be (the parser supposedly 
scans left to right, using the first match it finds and ignoring the 
rest). It should only go beyond the "|" if there was no match found 
before it, no?

Correct me if I'm wrong, but your regex is saying "match dog, unless 
it's followed by cat. if it is followed by cat there is no match on 
this side of the "|" at which point we advance past it and look at the 
alternative expression which says to match in front of cat."

However, if I run a .sub using your regex on a string contain both dog 
and cat, both will be replaced.

A simple example will show what I mean:
>>> import re
>>> x = re.compile(r"(A) | (B)")
>>> s = "X R A Y B E"
>>> r = x.sub("13", s)
>>> print r
X R 13Y13 E
...so unless I'm understanding it wrong, "B" is supposed to be ignored 
if "A" is matched, yet I get both matched.  I get the same result if I 
put "A" and "B" within the same group.

On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote:

Regular expressions are a little evil at times; here's what I think 
you're
thinking of:

###
import re
pattern = re.compile(r"""dog(?!cat)
...| (?<=dogcat)""", re.VERBOSE)
pattern.match('dogman').start()
0
pattern.search('dogcatcher').start()

Hi Mike,
Gaaah, bad copy-and-paste.  The example with 'dogcatcher' actually does
come up with a result:
###
pattern.search('dogcatcher').start()
6
###
Sorry about that!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regular expression question

2005-03-09 Thread Liam Clarke
Hi Mike, 

Do you get the same results for a search pattern of 'A|B'?


On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall
<[EMAIL PROTECTED]> wrote:
> I'm having some strange results using the "or" operator.  In every test
> I do I'm matching both sides of the "|" metacharacter, not one or the
> other as all documentation says it should be (the parser supposedly
> scans left to right, using the first match it finds and ignoring the
> rest). It should only go beyond the "|" if there was no match found
> before it, no?
> 
> Correct me if I'm wrong, but your regex is saying "match dog, unless
> it's followed by cat. if it is followed by cat there is no match on
> this side of the "|" at which point we advance past it and look at the
> alternative expression which says to match in front of cat."
> 
> However, if I run a .sub using your regex on a string contain both dog
> and cat, both will be replaced.
> 
> A simple example will show what I mean:
> 
>  >>> import re
>  >>> x = re.compile(r"(A) | (B)")
>  >>> s = "X R A Y B E"
>  >>> r = x.sub("13", s)
>  >>> print r
> X R 13Y13 E
> 
> ...so unless I'm understanding it wrong, "B" is supposed to be ignored
> if "A" is matched, yet I get both matched.  I get the same result if I
> put "A" and "B" within the same group.
> 
> 
> On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote:
> 
> >
> >
> >>
> >> Regular expressions are a little evil at times; here's what I think
> >> you're
> >> thinking of:
> >>
> >> ###
> > import re
> > pattern = re.compile(r"""dog(?!cat)
> >> ...| (?<=dogcat)""", re.VERBOSE)
> > pattern.match('dogman').start()
> >> 0
> > pattern.search('dogcatcher').start()
> >
> >
> >
> > Hi Mike,
> >
> > Gaaah, bad copy-and-paste.  The example with 'dogcatcher' actually does
> > come up with a result:
> >
> > ###
>  pattern.search('dogcatcher').start()
> > 6
> > ###
> >
> > Sorry about that!
> >
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regular expression question

2005-03-09 Thread Mike Hall
Indeed I do:
>>> import re
>>> x = re.compile('A|B')
>>> s = " Q A R B C"
>>> r = x.sub("13", s)
>>> print r
 Q 13 R 13 C

On Mar 9, 2005, at 12:09 PM, Liam Clarke wrote:
Hi Mike,
Do you get the same results for a search pattern of 'A|B'?
On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall
<[EMAIL PROTECTED]> wrote:
I'm having some strange results using the "or" operator.  In every 
test
I do I'm matching both sides of the "|" metacharacter, not one or the
other as all documentation says it should be (the parser supposedly
scans left to right, using the first match it finds and ignoring the
rest). It should only go beyond the "|" if there was no match found
before it, no?

Correct me if I'm wrong, but your regex is saying "match dog, unless
it's followed by cat. if it is followed by cat there is no match on
this side of the "|" at which point we advance past it and look at the
alternative expression which says to match in front of cat."
However, if I run a .sub using your regex on a string contain both dog
and cat, both will be replaced.
A simple example will show what I mean:
import re
x = re.compile(r"(A) | (B)")
s = "X R A Y B E"
r = x.sub("13", s)
print r
X R 13Y13 E
...so unless I'm understanding it wrong, "B" is supposed to be ignored
if "A" is matched, yet I get both matched.  I get the same result if I
put "A" and "B" within the same group.
On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote:

Regular expressions are a little evil at times; here's what I think
you're
thinking of:
###
import re
pattern = re.compile(r"""dog(?!cat)
...| (?<=dogcat)""", re.VERBOSE)
pattern.match('dogman').start()
0
pattern.search('dogcatcher').start()

Hi Mike,
Gaaah, bad copy-and-paste.  The example with 'dogcatcher' actually 
does
come up with a result:

###
pattern.search('dogcatcher').start()
6
###
Sorry about that!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

--
'There is only one basic human right, and that is to do as you damn 
well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] regular expression question

2005-03-09 Thread Mike Hall
But I only want to ignore "B" if "A" is a match. If "A" is not a match, 
I'd like it to advance on to "B".

On Mar 9, 2005, at 12:07 PM, Marcos Mendonça wrote:
Hi
Not and regexp expert. But it seems to me that if you want to ignora
"B" then it should be
(A) | (^B)
Hope it helps!
On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall
<[EMAIL PROTECTED]> wrote:
I'm having some strange results using the "or" operator.  In every 
test
I do I'm matching both sides of the "|" metacharacter, not one or the
other as all documentation says it should be (the parser supposedly
scans left to right, using the first match it finds and ignoring the
rest). It should only go beyond the "|" if there was no match found
before it, no?

Correct me if I'm wrong, but your regex is saying "match dog, unless
it's followed by cat. if it is followed by cat there is no match on
this side of the "|" at which point we advance past it and look at the
alternative expression which says to match in front of cat."
However, if I run a .sub using your regex on a string contain both dog
and cat, both will be replaced.
A simple example will show what I mean:
import re
x = re.compile(r"(A) | (B)")
s = "X R A Y B E"
r = x.sub("13", s)
print r
X R 13Y13 E
...so unless I'm understanding it wrong, "B" is supposed to be ignored
if "A" is matched, yet I get both matched.  I get the same result if I
put "A" and "B" within the same group.
On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote:

Regular expressions are a little evil at times; here's what I think
you're
thinking of:
###
import re
pattern = re.compile(r"""dog(?!cat)
...| (?<=dogcat)""", re.VERBOSE)
pattern.match('dogman').start()
0
pattern.search('dogcatcher').start()

Hi Mike,
Gaaah, bad copy-and-paste.  The example with 'dogcatcher' actually 
does
come up with a result:

###
pattern.search('dogcatcher').start()
6
###
Sorry about that!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] regular expression question

2005-03-09 Thread Liam Clarke
Actually, you should get that anyway...

"""
|
Alternation, or the ``or'' operator. If A and B are regular
expressions, A|B will match any string that matches either "A" or "B".
| has very low precedence in order to make it work reasonably when
you're alternating multi-character strings. Crow|Servo will match
either "Crow" or "Servo", not "Cro", a "w" or an "S", and "ervo".
"""

So, for each letter in that string, it's checking to see if any letter
matches 'A' or 'B' ...
the engine steps through one character at a time.
sorta like - 

for letter in s:
 if letter == 'A':
#Do some string stuff
 elif letter == 'B':
#do some string stuff


i.e. 

k = ['A','B', 'C', 'B']

for i in range(len(k)):
if k[i] == 'A' or k[i]=='B':
   k[i]==13

print k

[13, 13, 'C', 13]

You can limit substitutions using an optional argument, but yeah, it
seems you're expecting it to examine the string as a whole.


Check out the example here - 
http://www.amk.ca/python/howto/regex/regex.html#SECTION00032

Also

http://www.regular-expressions.info/alternation.html

Regards, 

Liam Clarke


On Thu, 10 Mar 2005 09:09:13 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote:
> Hi Mike,
> 
> Do you get the same results for a search pattern of 'A|B'?
> 
> 
> On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall
> <[EMAIL PROTECTED]> wrote:
> > I'm having some strange results using the "or" operator.  In every test
> > I do I'm matching both sides of the "|" metacharacter, not one or the
> > other as all documentation says it should be (the parser supposedly
> > scans left to right, using the first match it finds and ignoring the
> > rest). It should only go beyond the "|" if there was no match found
> > before it, no?
> >
> > Correct me if I'm wrong, but your regex is saying "match dog, unless
> > it's followed by cat. if it is followed by cat there is no match on
> > this side of the "|" at which point we advance past it and look at the
> > alternative expression which says to match in front of cat."
> >
> > However, if I run a .sub using your regex on a string contain both dog
> > and cat, both will be replaced.
> >
> > A simple example will show what I mean:
> >
> >  >>> import re
> >  >>> x = re.compile(r"(A) | (B)")
> >  >>> s = "X R A Y B E"
> >  >>> r = x.sub("13", s)
> >  >>> print r
> > X R 13Y13 E
> >
> > ...so unless I'm understanding it wrong, "B" is supposed to be ignored
> > if "A" is matched, yet I get both matched.  I get the same result if I
> > put "A" and "B" within the same group.
> >
> >
> > On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote:
> >
> > >
> > >
> > >>
> > >> Regular expressions are a little evil at times; here's what I think
> > >> you're
> > >> thinking of:
> > >>
> > >> ###
> > > import re
> > > pattern = re.compile(r"""dog(?!cat)
> > >> ...| (?<=dogcat)""", re.VERBOSE)
> > > pattern.match('dogman').start()
> > >> 0
> > > pattern.search('dogcatcher').start()
> > >
> > >
> > >
> > > Hi Mike,
> > >
> > > Gaaah, bad copy-and-paste.  The example with 'dogcatcher' actually does
> > > come up with a result:
> > >
> > > ###
> >  pattern.search('dogcatcher').start()
> > > 6
> > > ###
> > >
> > > Sorry about that!
> > >
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
> --
> 'There is only one basic human right, and that is to do as you damn well 
> please.
> And with it comes the only basic human duty, to take the consequences.
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regular expression question

2005-03-09 Thread Liam Clarke
Oops 

I mean 
 for i in range(len(k)):
   i f k[i] == 'A' or k[i]=='B':
k[i ]= 13



On Thu, 10 Mar 2005 09:28:59 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote:
> Actually, you should get that anyway...
> 
> """
> |
> Alternation, or the ``or'' operator. If A and B are regular
> expressions, A|B will match any string that matches either "A" or "B".
> | has very low precedence in order to make it work reasonably when
> you're alternating multi-character strings. Crow|Servo will match
> either "Crow" or "Servo", not "Cro", a "w" or an "S", and "ervo".
> """
> 
> So, for each letter in that string, it's checking to see if any letter
> matches 'A' or 'B' ...
> the engine steps through one character at a time.
> sorta like -
> 
> for letter in s:
>  if letter == 'A':
> #Do some string stuff
>  elif letter == 'B':
> #do some string stuff
> 
> i.e.
> 
> k = ['A','B', 'C', 'B']
> 
> for i in range(len(k)):
> if k[i] == 'A' or k[i]=='B':
>k[i]==13
> 
> print k
> 
> [13, 13, 'C', 13]
> 
> You can limit substitutions using an optional argument, but yeah, it
> seems you're expecting it to examine the string as a whole.
> 
> Check out the example here -
> http://www.amk.ca/python/howto/regex/regex.html#SECTION00032
> 
> Also
> 
> http://www.regular-expressions.info/alternation.html
> 
> Regards,
> 
> Liam Clarke
> 
> 
> On Thu, 10 Mar 2005 09:09:13 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote:
> > Hi Mike,
> >
> > Do you get the same results for a search pattern of 'A|B'?
> >
> >
> > On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall
> > <[EMAIL PROTECTED]> wrote:
> > > I'm having some strange results using the "or" operator.  In every test
> > > I do I'm matching both sides of the "|" metacharacter, not one or the
> > > other as all documentation says it should be (the parser supposedly
> > > scans left to right, using the first match it finds and ignoring the
> > > rest). It should only go beyond the "|" if there was no match found
> > > before it, no?
> > >
> > > Correct me if I'm wrong, but your regex is saying "match dog, unless
> > > it's followed by cat. if it is followed by cat there is no match on
> > > this side of the "|" at which point we advance past it and look at the
> > > alternative expression which says to match in front of cat."
> > >
> > > However, if I run a .sub using your regex on a string contain both dog
> > > and cat, both will be replaced.
> > >
> > > A simple example will show what I mean:
> > >
> > >  >>> import re
> > >  >>> x = re.compile(r"(A) | (B)")
> > >  >>> s = "X R A Y B E"
> > >  >>> r = x.sub("13", s)
> > >  >>> print r
> > > X R 13Y13 E
> > >
> > > ...so unless I'm understanding it wrong, "B" is supposed to be ignored
> > > if "A" is matched, yet I get both matched.  I get the same result if I
> > > put "A" and "B" within the same group.
> > >
> > >
> > > On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote:
> > >
> > > >
> > > >
> > > >>
> > > >> Regular expressions are a little evil at times; here's what I think
> > > >> you're
> > > >> thinking of:
> > > >>
> > > >> ###
> > > > import re
> > > > pattern = re.compile(r"""dog(?!cat)
> > > >> ...| (?<=dogcat)""", re.VERBOSE)
> > > > pattern.match('dogman').start()
> > > >> 0
> > > > pattern.search('dogcatcher').start()
> > > >
> > > >
> > > >
> > > > Hi Mike,
> > > >
> > > > Gaaah, bad copy-and-paste.  The example with 'dogcatcher' actually does
> > > > come up with a result:
> > > >
> > > > ###
> > >  pattern.search('dogcatcher').start()
> > > > 6
> > > > ###
> > > >
> > > > Sorry about that!
> > > >
> > >
> > > ___
> > > Tutor maillist  -  Tutor@python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> > >
> >
> > --
> > 'There is only one basic human right, and that is to do as you damn well 
> > please.
> > And with it comes the only basic human duty, to take the consequences.
> >
> 
> --
> 'There is only one basic human right, and that is to do as you damn well 
> please.
> And with it comes the only basic human duty, to take the consequences.
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regular expression question

2005-03-09 Thread Mike Hall
but yeah, it
seems you're expecting it to examine the string as a whole.
I guess I was, good point.

On Mar 9, 2005, at 12:28 PM, Liam Clarke wrote:
Actually, you should get that anyway...
"""
|
Alternation, or the ``or'' operator. If A and B are regular
expressions, A|B will match any string that matches either "A" or "B".
| has very low precedence in order to make it work reasonably when
you're alternating multi-character strings. Crow|Servo will match
either "Crow" or "Servo", not "Cro", a "w" or an "S", and "ervo".
"""
So, for each letter in that string, it's checking to see if any letter
matches 'A' or 'B' ...
the engine steps through one character at a time.
sorta like -
for letter in s:
 if letter == 'A':
#Do some string stuff
 elif letter == 'B':
#do some string stuff
i.e.
k = ['A','B', 'C', 'B']
for i in range(len(k)):
if k[i] == 'A' or k[i]=='B':
   k[i]==13
print k
[13, 13, 'C', 13]
You can limit substitutions using an optional argument, but yeah, it
seems you're expecting it to examine the string as a whole.
Check out the example here -
http://www.amk.ca/python/howto/regex/ 
regex.html#SECTION00032

Also
http://www.regular-expressions.info/alternation.html
Regards,
Liam Clarke
On Thu, 10 Mar 2005 09:09:13 +1300, Liam Clarke <[EMAIL PROTECTED]>  
wrote:
Hi Mike,
Do you get the same results for a search pattern of 'A|B'?
On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall
<[EMAIL PROTECTED]> wrote:
I'm having some strange results using the "or" operator.  In every  
test
I do I'm matching both sides of the "|" metacharacter, not one or the
other as all documentation says it should be (the parser supposedly
scans left to right, using the first match it finds and ignoring the
rest). It should only go beyond the "|" if there was no match found
before it, no?

Correct me if I'm wrong, but your regex is saying "match dog, unless
it's followed by cat. if it is followed by cat there is no match on
this side of the "|" at which point we advance past it and look at  
the
alternative expression which says to match in front of cat."

However, if I run a .sub using your regex on a string contain both  
dog
and cat, both will be replaced.

A simple example will show what I mean:
import re
x = re.compile(r"(A) | (B)")
s = "X R A Y B E"
r = x.sub("13", s)
print r
X R 13Y13 E
...so unless I'm understanding it wrong, "B" is supposed to be  
ignored
if "A" is matched, yet I get both matched.  I get the same result if  
I
put "A" and "B" within the same group.

On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote:

Regular expressions are a little evil at times; here's what I think
you're
thinking of:
###
import re
pattern = re.compile(r"""dog(?!cat)
...| (?<=dogcat)""", re.VERBOSE)
pattern.match('dogman').start()
0
pattern.search('dogcatcher').start()

Hi Mike,
Gaaah, bad copy-and-paste.  The example with 'dogcatcher' actually  
does
come up with a result:

###
pattern.search('dogcatcher').start()
6
###
Sorry about that!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
--
'There is only one basic human right, and that is to do as you damn  
well please.
And with it comes the only basic human duty, to take the consequences.


--
'There is only one basic human right, and that is to do as you damn  
well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] Acessing files in Windows 2000

2005-03-09 Thread jfouhy
> > >>>
> > >>>
> > >>> import os.path
> > >>> print os.path.expanduser('~/memo.txt')
> > C:\Documents and Settings\Administrator/memo.txt
> > >>> f = open(os.path.expanduser('~/memo.txt'))
> > Traceback (most recent call last):
> > File "", line 1, in ?
> > f = open(os.path.expanduser('~/memo.txt'))
> > IOError: [Errno 2] No such file or directory: 'C:\\Documents and
> > Settings\\Administrator/memo.txt'
> > >>>
> > 
> > Now starting to doubt my sanity I again re-checked C:\Documents and
> > Settings\Administrator\My Documents
> > and yes I do have a memo.txt there.

Um ---

So you have a file 'C:\Documents and Settings\Administrator\My
Documents\memo.txt'...

But you are attempting to open the file 'C:\Documents and
Settings\Administrator\memo.txt'.

There is a difference there!

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


Re: [Tutor] getting a webpage via python

2005-03-09 Thread Paul Tremblay
On Wed, Mar 09, 2005 at 08:29:46AM -0500, Kent Johnson wrote:
> 
> Kent Johnson wrote:
> >Paul Tremblay wrote:
> >
> >>So I just make a file called /etc/router_passwords  and include
> >>something like
> >>WRT54G username password
> >>
> >>Then parse the file, and supply the info to the password handler? This
> >>is easy to do, and I guess it is secure. 
> 
> The book "Foundations of Python Network Programming" has a sample program 
> that uses a custom urllib2.HTTPPasswordMgr to ask the user for the username 
> and password, maybe you would like to use that?
> 
> The examples can be downloaded from the book Web site at
> http://www.apress.com/book/bookDisplay.html?bID=363
> 
> Look for the one called dump_info_auth.py
> 

Thanks. There are lots of good examples in the free download examples.

Paul

-- 


*Paul Tremblay *
[EMAIL PROTECTED]*

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


Re: [Tutor] regular expression question

2005-03-09 Thread Kent Johnson
Mike Hall wrote:
A simple example will show what I mean:
 >>> import re
 >>> x = re.compile(r"(A) | (B)")
 >>> s = "X R A Y B E"
 >>> r = x.sub("13", s)
 >>> print r
X R 13Y13 E
...so unless I'm understanding it wrong, "B" is supposed to be ignored 
if "A" is matched, yet I get both matched.  I get the same result if I 
put "A" and "B" within the same group.
The problem is with your use of sub(), not with |.
By default, re.sub() substitutes *all* matches. If you just want to substitute the first match, 
include  the optional count parameter:

 >>> import re
 >>> s = "X R A Y B E"
 >>> re.sub(r"(A) | (B)", '13', s)
'X R 13Y13 E'
 >>> re.sub(r"(A) | (B)", '13', s, 1)
'X R 13Y B E'
BTW, there is a very handy interactive regex tester that comes with Python. On Windows, it is 
installed at
C:\Python23\Tools\Scripts\redemo.py

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


Re: [Tutor] Acessing files in Windows 2000

2005-03-09 Thread Dave S
[EMAIL PROTECTED] wrote:
import os.path
print os.path.expanduser('~/memo.txt')
   

C:\Documents and Settings\Administrator/memo.txt
 

f = open(os.path.expanduser('~/memo.txt'))
   

Traceback (most recent call last):
File "", line 1, in ?
f = open(os.path.expanduser('~/memo.txt'))
IOError: [Errno 2] No such file or directory: 'C:\\Documents and
Settings\\Administrator/memo.txt'
 

Now starting to doubt my sanity I again re-checked C:\Documents and
Settings\Administrator\My Documents
and yes I do have a memo.txt there.
 

Um ---
So you have a file 'C:\Documents and Settings\Administrator\My
Documents\memo.txt'...
But you are attempting to open the file 'C:\Documents and
Settings\Administrator\memo.txt'.
There is a difference there!
 

mmm ... I kind of see what you mean.
Does anyone have like a realy large shovel so I can dig a hole and hide ?
Thanks
Dave
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] working with new classes

2005-03-09 Thread C Smith
Thanks to Sean and Kent for replies.  I found a site that provided some  
good examples, too, at  
http://www.cafepy.com/articles/python_attributes_and_methods/ 
ch03s02.html

Here's a blurb from the title page:
 wep page excerpt
Shalabh Chaturvedi
Copyright © 2004 Shalabh Chaturvedi
This book is part of a series:
Python Types and Objects
Python Attributes and Methods [you are here]

After playing with this for a while I get the feeling that trying to  
create an alternate access for the list is not the way to go.  I was  
able to define the following methods with success...

###
class Ring(list):
def __init__(self,l):
list.__init__(self,l)
self._zero=0
def turn(self,incr=1):
self._zero+=incr
	def __getitem__(self,i):
		if type(i)==int:
			return list.__getitem__(self,(i-self._zero)%len(self))
		else:
			return [list.__getitem__(self,(k-self._zero)%len(self)) for k in  
range(i.start,i.stop,i.step)]
			
	def __setitem__(self,i,v):
		list.__setitem__(self,(i-self._zero)%len(self),v)
	
	def __getslice__(self,i,j):
		return  
list.__getslice__(self,(i-self._zero)%len(self),(j- 
self._zero)%len(self))

	def __setslice__(self,i,j,v):
		list.__setslice__(self,(i-self._zero)%len(self),(j- 
self._zero)%len(self),v)

	def __repr__(self):
		return repr([self[i] for i in range(len(self))])
###
..but then something like pop(3) pops the original element not the  
element in the 3rd position of the turned list:

###
>>> l=Ring(range(10))
>>> l.turn(5)
>>> print l
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
>>> l.pop(3); print l
[5, 6, 7, 8, 9, 0, 1, 2, 4] #I wanted the 8 to go
###
So in the absence of being able to redefine in the class the way that  
indices should be computed (like the way that your own cmp function can  
be supplied for the sort method) it seems best to use the existing  
structure and either access the list by passing it indices which have  
been remapped:

###
>>> def ring_index(i):
return (i-ring_zero)%ring_length
>>> l = range(10)
>>> ring_length = len(l)
>>> ring_zero = 3 #does a virtual shift to the right 3 elements
>>> print l[ring_index(0)]
7
###
But this will only help you look at individual entries and slices that  
don't go across the boundaries of the list.

Alternatively, the list class can be appended with helpers like 'turn'  
and 'segment' which can actually turn the "ring" and remove a piece  
from it without worrying about the endpoint:

###
>>> class ring(list):
def turn(self, incr=1):
incr%=len(self)
self[:] = self[incr:]+self[:incr]
def segment(self, i, length, incr=1):
length=min(len(self),length)
if i+length>len(self):
return 
self[i::incr]+self[(length-i)%incr:i+length-len(self):incr]
>>> l=ring(range(20)); l.turn(3); print l
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2]
>>> >>> l.segment(17,5,3) #start at 17, length of 5, stride 3
[0, 3]
###
This is my first intoduction to the new class modifications...initially  
it seems nice to be able to wrap your methods up into a class like this  
rather than creating dangling functions in one's program.

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


Re: [Tutor] working with new classes

2005-03-09 Thread Kent Johnson
C Smith wrote:
Alternatively, the list class can be appended with helpers like 'turn'  
and 'segment' which can actually turn the "ring" and remove a piece  
from it without worrying about the endpoint:

###
 >>> class ring(list):
def turn(self, incr=1):
incr%=len(self)
self[:] = self[incr:]+self[:incr]
def segment(self, i, length, incr=1):
length=min(len(self),length)
if i+length>len(self):
return 
self[i::incr]+self[(length-i)%incr:i+length-len(self):incr]

 >>> l=ring(range(20)); l.turn(3); print l
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2]
 >>> >>> l.segment(17,5,3) #start at 17, length of 5, stride 3
[0, 3]
###
This certainly looks like the simplest solution. You might want to look at UserList (in module 
UserList in the standard library) which is a list work-alike implemented in Python. In the old days, 
when it wasn't possible to subclass list, you would subclass UserList instead. Looking at the source 
for UserList shows all the methods that have to change to make your ring class - it is a lot.

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


Re: [Tutor] Acessing files in Windows 2000

2005-03-09 Thread Bill Mill
> mmm ... I kind of see what you mean.
> 
> Does anyone have like a realy large shovel so I can dig a hole and hide ?

No worries, we've all been there before. Sometimes you just can't see
what's right in front of your face.

Don't let it stop you, or stop you from asking questions.

Peace
Bill Mill
bill.mill at gmail.com

> 
> Thanks
> 
> Dave
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor