Re: Why and how there is only one way to do something?

2005-12-15 Thread John Hazen
* Chris Mellon [EMAIL PROTECTED] [2005-12-15 06:09]:
 (Am I dating myself? Do teenagers still put studs on their jackets?)

No.  They put studs in their lips, tongues, eyebrows, navels, and sexual
organs.

Oh, and ears.  (How quaint.)
-- 
http://mail.python.org/mailman/listinfo/python-list


regexp non-greedy matching bug?

2005-12-03 Thread John Hazen
I want to match one or two instances of a pattern in a string.

According to the docs for the 're' module 
( http://python.org/doc/current/lib/re-syntax.html ) the '?' qualifier
is greedy by default, and adding a '?' after a qualifier makes it
non-greedy.

 The *, +, and ? qualifiers are all greedy...
 Adding ? after the qualifier makes it perform the match in
 non-greedy or minimal fashion...

In the following example, though my re is intended to allow for 1 or 2
instinces of 'foo', there are 2 in the string I'm matching.  So, I would
expect group(1) and group(3) to both be populated.  (When I remove the
conditional match on the 2nd foo, the grouping is as I expect.)

$ python2.4
Python 2.4.1 (#2, Mar 31 2005, 00:05:10) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
Type help, copyright, credits or license for more information.
 import re
 foofoo = re.compile(r'^(foo)(.*?)(foo)?(.*?)$')
 foofoo.match(s).group(0)
'foobarbazfoobar'
 foofoo.match(s).group(1)
'foo'
 foofoo.match(s).group(2)
''
 foofoo.match(s).group(3)
 foofoo.match(s).group(4)
'barbazfoobar'
 foofoo = re.compile(r'^(foo)(.*?)(foo)(.*?)$')
 foofoo.match(s).group(0)
'foobarbazfoobar'
 foofoo.match(s).group(1)
'foo'
 foofoo.match(s).group(2)
'barbaz'
 foofoo.match(s).group(3)
'foo'
 foofoo.match(s).group(4)
'bar'



So, is this a bug, or just a problem with my understanding?  If it's my
brain that's broken, what's the proper way to do this with regexps?

And, if the above is expected behavior, should I submit a doc bug?  It's
clear that the ? qualifier (applied to the second foo group) is _not_
greedy in this situation.

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regexp non-greedy matching bug?

2005-12-03 Thread John Hazen
 [John Hazen]
  I want to match one or two instances of a pattern in a string.
 
   s = 'foobarbazfoobar'
   foofoo = re.compile(r'^(foo)(.*?)(foo)?(.*?)$')
   foofoo.match(s).group(1)
  'foo'
   foofoo.match(s).group(3)
   

[Tim Peters]
 Your problem isn't that
 
 (foo)?
 
 is not greedy (it is greedy), it's that your first
 
 (.*?)
 
 is not greedy.  Remember that regexps also work left to right.

Well, I had the same symptoms when that .* was greedy (it ate up the
optional foo), which is why I went to non-greedy there.

I guess my error was thinking that greedy trumped non-greedy, rather
than left trumping right.  (ie, in order for the (foo)? to be maximally
greedy, the (.*?) has to be non-maximally non-greedy :)

 Maybe what you're looking for is
 
 ^P(.*P)?.*$

Yes.  That works the way I wanted.  ( ^(foo)(.*(foo))?.*$ )

Thank you, both for the specific answer, and the general education.

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regexp non-greedy matching bug?

2005-12-03 Thread John Hazen
[Mike Meyer]
 The thing to understand is that regular expressions are *search*
 functions, that return the first parsing that matches. They search a
 space of possible matches to each term in the expression. If some term
 fails to match, the preceeding term goes on to its next match, and you
 try again. The greedy vs. non-greedy describes the order that the
 term in question tries matches. If it's greedy, it will try the
 longest possible match first. If it's non-greedy, it'll try the
 shortest possible match first.

That's a good explanation.  Thanks.

[John]
  I want to match one or two instances of a pattern in a string.
  foofoo = re.compile(r'^(foo)(.*?)(foo)?(.*?)$')

[Mike]
 First, this pattern doesn't look for one or two instances of foo in
 a string. It looks for a string that starts with foo and maybe has a
 second foo in it as well.

Right.  In simplifying the expression for public consumption, one of the
terms I dropped was r'^.*?(foo)...'.

 To do what you said you want to do, you want to use the split method:
 
 foo = re.compile('foo')
 if 2 = len(foo.split(s)) = 3:
print We had one or two 'foo's

Well, this would solve my dumbed down example, but each foo in the
original expression was a stand-in for a more complex term.  I was using
match groups to extract the parts of the match that I wanted.  Here's an
example (using Tim's correction) that actually demonstrates what I'm
doing:

 s = 'zzzfoo123barxxxfoo456baryyy'
 s2 = 'zzzfoo123barxxxfooyyy'
 foobar2 = re.compile(r'^.*?foo(\d+)bar(.*foo(\d+)bar)?.*$')
 print foobar2.match(s).group(1)
123
 print foobar2.match(s).group(3)
456
 print foobar2.match(s2).group(1)
123
 print foobar2.match(s2).group(3)
None
 


Looking at re.split, it doesn't look like it returns the actual matching
text, so I don't think that fits my need.

 As the founder of SPARE...

Hmm, not a very effective name.  A google search didn't fing any obvious
hits (even after adding the python qualifier, and removing spare time
and spare parts hits).  (I couldn't find it off your homepage,
either.)

Thanks for your help.  If you have any suggestions about a non-re way to
do the above, I'd be interested.

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Background process for ssh port forwarding

2005-10-04 Thread John Hazen
Hi Jesse-

 def hostforward():
 #This is based on the assumption that the passfile is the gnus
 #authinfo file, or has a similar format...
 f = open(PASS_FILE, r)
 f_list = f.read().split(' ')
 f.close()
 #Now, we get the entry after password (be slicker to make it a
 #dictionary, but maybe wouldn't work as well).
 pass_index = f_list.index('password') + 1
 forwardpass = f_list[pass_index]
 #now we connect
 command = 'ssh -l %s -L 2022:%s:22 %s' % \
   (login, my_server, forwarding_server)
 connection = pexpect.spawn(command)
 connection.expect('.*assword:')
 connection.sendline(forwardpass)
 
 If I end this with 'connection.interact()', I will end up logged in to the
 forwarding server. But what I really want is to go on and run rsync to
 localhost port 2022, which will forward to my_server port 22. So, how can
 I put the ssh connection I set up in hostforward() in the background?
 I need to make sure that connection is made before I can run the rsync
 command.

I think what's happening is that when you return from 'hostforward', the
connection is being closed because of garbage collection.  Python uses
(among other stuff) reference counting to tell it when to delete
objects.  After hostforward returns from execution, there are no longer
any references to 'connection', so it gets deleted, which cleans up the
connection.

You probably want to add:

return connection

at the end of hostforward, and call it like:

connection = hostforward()
my_rsync_function()
connection.close()   # or whatever the approved pexpect cleanup is


Hope that helps-

John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Struggling with this concept please help

2005-09-26 Thread John Hazen
* George [EMAIL PROTECTED] [2005-09-25 16:20]:
 Hello everyone I know many have helped but I cannot get this to work
 out correctly.
snip
 Please help I have never done python before and I can't seem to get the
 hang of it.

You posted code.  Is what's in the docstring actual output?  If so, what
isn't it doing that you want it to?

If it's not working correctly, what's it doing?  Just saying I cannot
get this to work doesn't give us much to go on.  If you want specific
help, be sure to:

1) post actual code
2) explain what you expect it to do
3) post actual output (or traceback)
4) ask a specific question about what you don't understand about item 3.

Even though we know this is schoolwork, I'm OK with offering you a hint
or answering a specific question.

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How am I doing?

2005-09-19 Thread John Hazen
* Jason [EMAIL PROTECTED] [2005-09-19 16:28]:
 I've restructured my code with the assistance of George and Mike which 
 is now as follows...
 
 import random
 
 class HiScores:
  def __init__(self,hiScores):
  self.hiScores=[(entry[:5],entry[5:]) for entry in hiScores]

With your redefined hiScores, the above is wrong.  I think it should
just be:
   self.hiScores=[entry for entry in hiScores]

Your original code used the slicing to pull the score and name out of a
single string.  Since you've split the string into its two parts, you
don't need the indexing anymore.

  def showScores(self):
  for name,score in self.hiScores:
  print %s - %s % name,score

The error you cite below is due to trying to zfill an integer, not a
string.  I'm not going to modify your code, but I would use integers all
over for the scores, and only turn to a string (and do the zfill) when
outputting it.

   def showScores(self):
   for name,score in self.hiScores:
   score = str(score).zfill(5)  #untested
   print %s - %s % name,score

 def main():
  
 hiScores=[('1','Alpha'),('07500','Beta'),('05000','Gamma'),('02500','Delta'),('0','Epsilon')]
  

This looks like an indentation error to me.  Is hiScores indented in
your version?

  a=HiScores(hiScores)
  print Original Scores\n---
  a.showScores()
 
  while 1:
  newScore=random.randint(0,1)

As I said, I would use int's throughout, but to use it as-is, change the
above line to:
   newScore=str(random.randint(0,1))

  if newScore.zfill(5)  a.lastScore():
  print Congratulations, you scored %d  % newScore
  name=raw_input(Please enter your name :)
  a.addScore(newScore,name)
  a.showScores()
 
 if __name__==__main__:
  main()

snip

 1) The most important is that when run, the program crashes with
 
 AttributeError: 'int' object has no attribute 'zfill'
 
 I've read as many websites as I can about zfill and I can't see why on 
 earth it's failing.

I think I explained this above.  You're trying to call a string method
on an integer.

 
 2) The output of the predefined hiscores is now...
 
 1 - Alpha ()
 07500 - Beta ()
 05000 - Gamma ()
 02500 - Delta ()
 0 - Epsilon ()

Are you sure it's not:

('1', 'Alpha') - ()
etc.  ?

I think this is a result of your still doing indexing to separate the
score and the name, even though you've already separated the name and
score into tuples in the predefined list:

 hiScores=[('1','Alpha'),('07500','Beta'),('05000','Gamma'),('02500','Delta'),('0','Epsilon')]
 s=hiScores[0]
 s
('1', 'Alpha')
 s[:5]
('1', 'Alpha')
 s[5:]
()
 p = (s[:5],s[5:])
 p
(('1', 'Alpha'), ())
 print %s - %s % p
('1', 'Alpha') - ()
 


 Why are there the pairing parenthesis there?  George very kindly showed 
 me another way which was to have...
 
 def showScores(self):
for entry in self.hiScores:
print entry[0:5], - ,entry[5:]
 
 But using that method output the entire list in it's full format (sorry 
 if that's not the correct terminology).  But give me a small plus mark 
 for changing code and not simply copying George :)
 
 3) The hardest thing to 'understand' is the line...
   self.hiScores=[(entry[:5],entry[5:]) for entry in hiScores]

This is now slicing into the tuple for each entry, instead of into the
string, so your results are unexpected.  slicing past the end of a tuple
returns the empty tuple (which I think is the '()' you're getting in
your output.

 TIA

HTH-

John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sockets: code works locally but fails over LAN

2005-09-01 Thread John Hazen
* n00m [EMAIL PROTECTED] [2005-08-31 05:45]:
 import socket, thread
 host, port = '192.168.0.3', 1434
 s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s2.connect((host, 1433))
 s1.bind((host, port))

I think the problem is that you're using the same host for both the send
and recieve sockets.  Wouldn't you want one host to be the localhost,
and one to be sql server?

If it doesn't work even when only the VBscript is on a different box,
are you running an OS with a firewall that you have to disable?

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: To the python-list moderator

2005-08-31 Thread John Hazen
  This is probably a fairly bad way of contacting the python-list
  admins...
 
 Perhaps.  Feel free to forward if you know a better way.

I tried sending a question to [EMAIL PROTECTED], and got
another 'don't call us, we'll maybe call you'.  Maybe
[EMAIL PROTECTED]

 But public posting allows comments by others 
 as to similar problems, like Gregory's.

And mine.  FWIW, I'm posting direct to the mailing list with
mutt/postfix.  Since I *do* have concrol over my headers, it would be
nice to see what header is 'suspicious', so I could possibly modify it.

It'll be interesting to see if this post makes it to the list...

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why is this?

2005-08-11 Thread John Hazen
  [[]]*2
  [[], []]
  [[], []] == [[]]*2
  True
 
 Same effect. But try the 'is' operator, to see if they are actually the  
 same instances of 'empty list':
 
  [[], []] is [[]]*2
  True

Just curious, did you actually cut and paste this from a real
interactive session?  (I think not.)  My interpreter (yes, I know it's
old) gives:

 $ python
 Python 2.3 (#1, Sep 13 2003, 00:49:11) 
 [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
 Type help, copyright, credits or license for more information.
  [[], []] is [[]]*2
 False

Which is, I think, the point you were trying to make.

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subscribe

2005-07-26 Thread John Hazen
* Brian Lee [EMAIL PROTECTED] [2005-07-26 06:34]:
 Subscribe

OK.  You're now subscribed to thousands of spam lists.  Congratulations!

;)

You can subscribe to the python-list here:

http://mail.python.org/mailman/listinfo/python-list

Good luck-

John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-19 Thread John Hazen
* Helge Aksdal [EMAIL PROTECTED] [2005-07-19 11:23]:
 if i then change to a console window, and telnet to this server it
 sends me to another one. That's probably why my program dies, how
 can i get my code to handle this?
 
 Trying xxx.xxx.xxx.xxx
 telnet: connect to address xxx.xxx.xxx.xxx: Connection refused
 Trying xxx.xxx.xxx.xxx
 Connected to xx.x.
 Escape character is '^]'.

I don't think the server is sending you to another one.  I think
perhaps DNS returns two IP addresses for the name, and after the first
one fails, your telnet client tries the second?

I'm just guessing, since you didn't include what command you ran to do
the telnet.  As Grant mentioned, the telnet protocol doesn't do
redirects.

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Programming Contest

2005-07-17 Thread John Hazen
* Brian Quinlan [EMAIL PROTECTED] [2005-07-15 02:08]:
 
 You can find the first problem here:
 http://www.sweetapp.com/pycontest/contest1

I have one question about the problem.  Is the cost we are to minimize
the cost of arriving in the target city at all, or the cost of arriving
at the target city at the end of the final day of the schedule?

(If you were traveling to a conference, for example, you'd have a
specific arrival time, and a cost to stay in the destination city until
that time.  But, if you were going to sight-see, then you could arrive
at any time, and begin your itinerary upon arrival.)

Say I can find a combination of flights that gets me to the target at
the end of day 3 for 390 units, and a combination that gets me there at
the end of day 4 for 400.  If you count the hostel cost from day 3 to
day 4, the first combination costs 410.  So, which is preferred?

-John

P.S.  I just realized this may be answered be the test suite, but I'm
still at the thinking stage.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: poker card game revisited (code included)

2005-06-08 Thread John Hazen
[Erik Max Francis]
   Searching for straights and flushes is much better done by masks.

Interesting.  I've been thinking about playing with this stuff too, but
hadn't considered masks.  So each card has a representation:

n bits for rank, then m bits for suit.

10 0001 = 2 clubs
01 1000 = K spades
...

[flupke]
  As for straights, if i understand correctly, you make all possible 
  straights of the cards in the hand and then see if one matches?

Yeah, I originally thought that's what you meant, too.  But if you take
the scheme above, and sort the cards before you merge them into the
hand-bits aggregate, then you can just have *one* mask for straights,
and shift it down by a bit each time you check. So the best straight
mask (A high) would be (ignoring suit bits):

10 01 001000 000100 10

Then this could be shifted right for a K-high straight:

01 001000 000100 10 01

I guess that means that an Ace has to have the following representation,
since it can be both at the high and low end of a straight:

11 0100 = A hearts

But this may mess with bit-counting shortcuts for other calculations...

This is interesting to think about.  Thanks for the pointer.  I'm also
going to look at pokersource, though I may put that off until I at least
partially re-invent the wheel for learning purposes.

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Challenge ahead [NEW] for riddle lovers

2005-05-02 Thread John Hazen
* [EMAIL PROTECTED] [EMAIL PROTECTED] [2005-05-02 05:10]:
 Any hints on level 6 ?
 There is zip in the page source, ZIp in the
 image data, but have no clue what to do with them
 - tried many things :). 

Have you found the zip file yet?

(I did, and I still can't figure it out.  I'm wondering if there are
comments embedded in the jpg that none of my viewers will show me.
Hmm, maybe there's a python module that does that...  I'll play with it
more after I sleep on it.)

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Challenge ahead [NEW] for riddle lovers

2005-05-02 Thread John Hazen
* Reinhold Birkenfeld [EMAIL PROTECTED] [2005-05-02 06:19]:
 John Hazen wrote:
  Have you found the zip file yet?
  
  (I did, and I still can't figure it out) 
 
 It's best when you use the zipfile module.

Cool.  I had just unzipped the zipfile, and was looking at the files
directly.  The comments weren't added to the files in the OS, so I was
only thinking about jpeg comments (since the photo was also a zip).

I love the way this challenge is making me poke into pieces of the
library I've never used before.  :)

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Challenge ahead [NEW] for riddle lovers

2005-05-02 Thread John Hazen
* Dan Bishop [EMAIL PROTECTED] [2005-05-02 21:09]:
 Dan Christensen wrote:
  Reinhold Birkenfeld [EMAIL PROTECTED] writes:
 
   Dan Christensen wrote:
   Roel Schroeven [EMAIL PROTECTED] writes:
  
   There's no level 12 yet though.
  
   Now there's a 12 and a 13 (at least!).
  
   Anyone solved 12? Hints?
 
  **hint below**
 
  Look at the filename for the first image you see,
 
 You mean evil1.jpg?

yup.

  and make a guess at another filename.
 
 I've made tons of guesses, but none of them worked :-(

Hmmm.  The title of the html page is dealing evil.  If you were
playing war instead of solitaire, how would you change how you deal the
cards?

-John

P.S. - I think I've found everything, but still haven't figured out what
to do with it.  No hints yet, though.  I want to think about it some
more...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dr. Dobb's Python-URL! - weekly Python news and links (Apr 11)

2005-04-14 Thread John Hazen
* Dave Brueck [EMAIL PROTECTED] [2005-04-14 07:49]:
[Roel Schroeven]
 Not that it really matters, but does anybody know why the weekly Python
 news always arrives twice? Does it only happen to me, or does it happen
 to others too?
 
 It's not that it irritates me or anything, I'm just being curious.

[Dave Brueck]
 I just thought it was for emphasis.

My theory has always been that it's posted once to python-list, and once
to usenet, and the bridge dutifully sends each message to the other
domain.

(And, yes.  (Obviously) I get two copies, as well.  I've meen meaning to
ask the same question.)

-John
-- 
http://mail.python.org/mailman/listinfo/python-list