Re: [Tutor] What kind of approach

2007-03-27 Thread Alan Gauld

"Utkarsh Tandon" <[EMAIL PROTECTED]> wrote 
> Alan Gauld wrote:
>> Thats a very long winded and difficult way to do a fairly simple
>> Python task. Don't try to write Python like a C programmer.
> 
> I'm really new to python. And I don't have much experience in 
> programming either 

Ok, My mistake. The code you had was very like the way 
experienced C programmers typically start using Python;
as if it was C. That's not a good way.

> what kind of approach should I have when coding programs 
> in Python ?

Python is often described as coming "with batteries included"
This is because of the 100 plus modules in the standard library 
that provide tools to do most things more easily. The Pythonic 
approach is to leverage those modules, plus the higher level 
data structures within Python to maximum advantage.

The best solution for stripping C comments is probably to 
use a re module's regular expressions. Define a regex that 
matches a C comment, then replace all occurrences of the 
regex with an empty string. Of course defining the regex 
can be a challenge in itself, but it's a different challenge! :-)


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] POP3 MailBox Access

2007-03-27 Thread James Rocks
> Rikard Bosnjakovic Wrote: 
> FWIW, your code works fine for me. I tested on my local pop3-server
> and I got all the messages on it. Perhaps it's a server issue of
> yours.


Thanks
for replying Richard ... it may well be my ISP, perhaps the way they
require us to use "accountname+mailboxname" for our user credentials. 

Kyu






___ 
To help you stay safe and secure online, we've developed the all new Yahoo! 
Security Centre. http://uk.security.yahoo.com___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] POP3 MailBox Access

2007-03-27 Thread Rikard Bosnjakovic
On 3/27/07, James Rocks <[EMAIL PROTECTED]> wrote:

[...]
> So, if anyone can offer any help I'd appreciate it :)

FWIW, your code works fine for me. I tested on my local pop3-server
and I got all the messages on it. Perhaps it's a server issue of
yours.


-- 
- Rikard - http://bos.hack.org/cv/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] POP3 MailBox Access

2007-03-27 Thread James Rocks




Hi,


 


I'm new to this list but not new to Python ... I've been on
and off coding in it for years. Still I remain just a hobbyist :)


 


Recently my interest in Python was rekindled when I started
hosting a game I used to play (VGA Planets or VGAP) and, because I was also a
player in the same game (something frowned upon or at least viewed suspiciously
in some VGAP circles) I wanted to host the game as neutrally as possible so I
set about writing an automatic hosting (AutoHost) program. VGAP is a 
multi-player,
strategic, space-based play-by-email game where the host runs, generates result
(RST) files, these get sent to the players who then create turn (TRN) files, 
effectively
a list of commands, that they then return to the host for the next run. 


 


The general idea of my program is that AutoHost runs (on a
separate machine, Windows 200 Professional) the host, sends out the RST files to
the players and waits for them to send their TRN files back. Once a specific
time has passed or all the TRN's have come back the host runs again and in
between AutoHost sends status updates and receipts for submitted TRN's. 


 


I have had some problems ... initially I wasn't able to get mail
functioning at all and was forced to rely on three DOS command line programs
which I called by means of Python's sys (os module) function. In addition I had
a number of issues with the receipt of mail (for some reason unknown to me the
command line utility which had been working after a fashion suddenly decided to
receive attached files as uuencoded text) and I was forced to abandon it as a
means of getting the TRN's and I switched to FTP instead. The program ran as a
series of separate instances fired off by means of an external scheduler. 


 


This worked but was unsatisfactory ... broadly speaking I'm
pleased with the result but I've got a bit fired up and want to improve it. 
Firstly
I wanted to internalise the scheduler and have since done so using a timer
routine I found on the internet and the program now runs permanently with
events being triggered by that timer. Secondly I wanted to send & receive mail
using Python's own routines ... I have been successful with SMTP sending but
receiving mail still eludes me probably because it is via POP3. Part of the
reason may be because my POP3 user isn't a single ID as such but a sub-mailbox
from my family one i.e. the VGAP user has a username of mailbox+vgap and
requires authentication. 


 


I have used the a number of variations on the following code
with no luck, even though I know there are messages in the box I get back
nothing.


 


] 


] import getpass, poplib


] 


] M = poplib.POP3('mail.isp.net')


] M.user(getpass.getuser())


] M.pass_(getpass.getpass())


] numMessages = len(M.list()[1])


] for i in range(numMessages):


] for j in
M.retr(i+1)[1]:


] print j


] 


 


So, if anyone can offer any help I'd appreciate it :)


 


Kyu









___ 
New Yahoo! Mail is the ultimate force in competitive emailing. Find out more at 
the Yahoo! Mail Championships. Plus: play games and win prizes. 
http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Webform

2007-03-27 Thread Kent Johnson
Øyvind wrote:
> I am trying to fill in a webform, and have tried using
> Clientform/mechanize, but cannot seem to get it right.
> 
> It just gives me feedback that the form is processed, but I can't seem to
> get the result I get if I manually enter the value with the browser.

This form is doing some funny stuff with JavaScript. Take a look at the 
page source. The button you click is not the actual submit button, 
clicking it calls _doClick() with a long parameter. This parameter is 
set as the value of the hidden __Click field before submitting the form.

I don't know for sure why this is done, but probably to thwart machine 
harvesting of the data such as you are doing. You should make sure you 
aren't violating the terms of use of the website by automatically 
fetching the data.

Anyway this seems to work:
In [1]: import urllib2
In [2]: from ClientForm import ParseResponse
In [4]: response = 
urllib2.urlopen("http://www.datateam.no/boc/bocadresse.nsf/wMedlemsoek?OpenForm&Seq=1";)
In [5]: forms = ParseResponse(response, backwards_compat=False)
In [6]: form = forms[0]
In [10]: click = form.find_control('__Click')
In [11]: click.readonly=False
In [12]: form['__Click'] = 
'41256C7C00368A2B.8b5a45b319030f3ec125728a00788ddb/$Body/0.CB4'
In [13]: form["QueryField"] = """980213250"""
In [20]: print urllib2.urlopen(form.click()).read()

The value for the __Click field doesn't seem to change, but if it does 
you will have to parse it out of the onclick atttribute of the SubmitBtn 
control.

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


Re: [Tutor] guess my number game

2007-03-27 Thread Dick Moores


At 03:16 AM 3/27/2007, Alexander Kapshuk wrote:
I’m
working on a program that has the user think of a number between 1 and
100 and then tries to guess that number.
 
I’m having trouble telling the computer to keep on looking for the
correct number, each time narrowing down the search range.
 
Please see the code below.
 
import random
 
print "\tWelcome to 'Guess My Number 1.2'!"
print "\nThink of a number between 1 and 100."
print "The computer will try to guess it in as few attempts as
possible.\n"
 
# set the initial values
user_number = int(raw_input("Think of a number between 1 and 100 and
press Enter: "))
guess = random.randrange(50) + 1
answer = ""
tries = 1
 
# guessing loop
print guess
answer = raw_input("Is the above No '>', '<' or '=' as your
No?: ")
while (answer != "="):
    if (answer == ">"):
    print (guess =
random.randrange(100) + 51)
    elif (answer == "<"):
    print (guess =
random.randrange(49) + 1)
    elif (answer == "="):
    print "Correct! The
number was", user_number "And it only took you", tries
" tries!\n"
    else:
    print "Keep on
trying!"
 
    tries += 1
 
raw_input("\n\nPress the enter key to exit.")
 
 
 
Here are some hints.
You can use randrange, but maybe random.randint is easier to understand.
>From the docs:
==
randint( a, b)
Return a random integer N such that a <= N <= b. 
==
You're not employing variables such as "a" and "b"
(or "low" and "high") to "pinch down" on
the user_number. 
Also, you have errors in your print statements. Correcting these, here's
your while loop:
=
while (answer != "="):
    if (answer == ">"):
    print "guess =",
(random.randrange(100) + 51)
    elif (answer == "<"):
    print "guess =",
(random.randrange(49) + 1)
    elif (answer == "="):
    print "Correct! The
number was", user_number, "And it only took you", tries,
" tries!\n"
    else:
    print "Keep on
trying!"

    tries += 1
=
Now, the loop needs rewriting. If answer is "=", the loop isn't
entered. If "<" or ">", the loop is
endless.
Let's say the user gives the answer, ">".  Then the
loop is endless, because answer remains ">", with no
opportunity for the user to change it depending on the last guess.

I hope this helps you.
Dick Moores




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


Re: [Tutor] guess my number game

2007-03-27 Thread Alexander Kapshuk
I'm working on a program that has the user think of a number between 1
and 100 and then tries to guess that number.

 

I'm having trouble telling the computer to keep on looking for the
correct number, each time narrowing down the search range.

 

Please see the code below.

 

import random

 

print "\tWelcome to 'Guess My Number 1.2'!"

print "\nThink of a number between 1 and 100."

print "The computer will try to guess it in as few attempts as
possible.\n"

 

# set the initial values

user_number = int(raw_input("Think of a number between 1 and 100 and
press Enter: "))

guess = random.randrange(50) + 1

answer = ""

tries = 1

 

# guessing loop

print guess

answer = raw_input("Is the above No '>', '<' or '=' as your No?: ")

while (answer != "="):

if (answer == ">"):

print (guess = random.randrange(100) + 51)

elif (answer == "<"):

print (guess = random.randrange(49) + 1)

elif (answer == "="):

print "Correct! The number was", user_number "And it only took
you", tries " tries!\n"

else:

print "Keep on trying!"

 

tries += 1

 

raw_input("\n\nPress the enter key to exit.")

 

 

 

 

 

Thanking you all in advance.

 

 

Alexander Kapshuk

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