[Tutor] Uncertain about exceptions

2005-07-06 Thread Matej Cepl
Hi,

I have here two Python modules (see attached), where newsparser.py defines
(among other things) an object SoupReader and an exception
PageNotAvailable. This object (or its children) is then periodically
created in a loop in reprocess.py.

Now, my question is concerned with the use of exception in SoupReader.suck.
The idea is that when the page is not available for download, whole object
goes up in flames, the rest of SoapReader.__init__ is skipped over, and the
exception is then caught in reprocess.py cycle, which makes a note in some
errorlog, that this particular page has not been downloaded, but otherwise
the script continues for other pages. Is it what I am doing here or did I
missed the idea of exceptions at all?

Thanks for any answer,

Matej

-- 
Matej Cepl, http://www.ceplovi.cz/matej/blog/
GPG Finger: 89EF 4BC6 288A BF43 1BAB  25C3 E09F EF25 D964 84AC
 
I used to date a woman who did PR and marketing for MS, so you
can imagine we had some in-depth and sometimes heated
discussions about MS vs. Linux and Macs.
Well, one day we were going hiking, and she presented me with
a really nice backback. The only issue with it was that it had
the MS logo emblazoned all over it. Of course, she knew
I wouldn't refuse it.
Anyway, she said to me, "Isn't that nice? See? What'd RedHat
ever give you for free?"
I replied, "An operating system."
That was one long, quiet hike.
  -- disserto on Slashdot
 http://slashdot.org/comments.pl\
 ?sid=138492&tid=109&mode=thread&cid=11590292


newsparser.py
Description: application/python


reprocess.py
Description: application/python
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Uncertain about exceptions

2005-07-06 Thread Kent Johnson
Matej Cepl wrote:
> Now, my question is concerned with the use of exception in SoupReader.suck.
> The idea is that when the page is not available for download, whole object
> goes up in flames, the rest of SoapReader.__init__ is skipped over, and the
> exception is then caught in reprocess.py cycle, which makes a note in some
> errorlog, that this particular page has not been downloaded, but otherwise
> the script continues for other pages. Is it what I am doing here or did I
> missed the idea of exceptions at all?

After a quick look I think you have it right. You could define PageNotAvailable 
as
class PageNotAvailable(Exception):
  pass

because the behavior you give it is the default behaviour for Exception.

Kent

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


Re: [Tutor] Is there a way to combine a request for numbers and letters?

2005-07-06 Thread christinA

Please will you find out why the score on your mails are that high? 
My ISP is sending them directly to my spambox.
For your convenience I copied the headers.
Especially 
 0.8 MSGID_FROM_MTA_HEADER  Message-Id was added by a relay
 2.2 RCVD_IN_BL_SPAMCOP_NET Received via a relay in bl.spamcop.net
seems to ring the bell.

christinA 

Return-Path: <[EMAIL PROTECTED]>
Received: from smtp-vbr12.xs4all.nl (smtp-vbr12.xs4all.nl 
[194.109.24.32])
by mxdrop3.xs4all.nl (8.13.3/8.13.3) with ESMTP id j64Mnm3T005860
for <[EMAIL PROTECTED]>; Tue, 5 Jul 2005 00:49:48 +0200 (CEST)
(envelope-from [EMAIL PROTECTED])
Received: from bag.python.org (bag.python.org [194.109.207.14])
by smtp-vbr12.xs4all.nl (8.13.3/8.13.3) with ESMTP id j64MnlLQ079469
for <[EMAIL PROTECTED]>; Tue, 5 Jul 2005 00:49:47 +0200 (CEST)
(envelope-from [EMAIL PROTECTED])
Received: from bag.python.org (bag [127.0.0.1])
by bag.python.org (Postfix) with ESMTP id 4D96F1E4006
for <[EMAIL PROTECTED]>; Tue,  5 Jul 2005 00:49:50 +0200 (CEST)
X-Original-To: tutor@python.org
Delivered-To: [EMAIL PROTECTED]
Received: from bag.python.org (bag [127.0.0.1])
by bag.python.org (Postfix) with ESMTP id 9DB211E4004
for ; Tue,  5 Jul 2005 00:49:00 +0200 (CEST)
X-Spam-Status: OK 0.000
Received: from bag (HELO bag.python.org) (127.0.0.1)
by bag.python.org with SMTP; 05 Jul 2005 00:49:00 +0200
Received: from hotmail.com (unknown [65.54.161.89])
by bag.python.org (Postfix) with ESMTP
for ; Tue,  5 Jul 2005 00:48:59 +0200 (CEST)
Received: from mail pickup service by hotmail.com with Microsoft 
SMTPSVC;
Mon, 4 Jul 2005 15:48:56 -0700
Message-ID: <[EMAIL PROTECTED]>
Received: from 65.54.161.201 by BAY106-DAV17.phx.gbl with DAV;
Mon, 04 Jul 2005 22:48:55 +
X-Originating-IP: [65.54.161.201]
X-Originating-Email: [EMAIL PROTECTED]
X-Sender: [EMAIL PROTECTED]
From: "Nathan Pinno" <[EMAIL PROTECTED]>
To: 
References: <[EMAIL PROTECTED]>
Date: Mon, 4 Jul 2005 16:48:49 -0600
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-OriginalArrivalTime: 04 Jul 2005 22:48:56.0232 (UTC)
FILETIME=[8F26E280:01C580EA]
Subject: Re: [Tutor] Is there a way to combine a request for numbers 
and
letters?
X-BeenThere: tutor@python.org
X-Mailman-Version: 2.1.6
Precedence: list
List-Id: Discussion for learning programming with Python 

List-Unsubscribe: ,

List-Archive: 
List-Post: 
List-Help: 
List-Subscribe: ,

Content-Type: multipart/mixed; boundary="===1425094626=="
Sender: [EMAIL PROTECTED]
Errors-To: [EMAIL PROTECTED]
X-Virus-Scanned: by XS4ALL Virus Scanner
X-XS4ALL-Spam-Score: 5.904 (*) 
FORGED_MUA_OUTLOOK,FROM_ENDS_IN_NUMS,HTML_30_40,HTML_MESSAGE,MAILTO_TO
_SPAM_ADDR,MSGID_FROM_MTA_HEADER,RCVD_IN_BL_SPAMCOP_NET
X-XS4ALL-Spam: YES
Envelope-To: [EMAIL PROTECTED]

This is a multi-part message in MIME format.

--===1425094626==
Content-Type: multipart/alternative;
boundary="=_NextPart_000_0038_01C580B8.4099DB90"

This is a multi-part message in MIME format.

--=_NextPart_000_0038_01C580B8.4099DB90
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

  Sorry,

  Just remembered that strings can include both letters and numbers. 
=
Case closed.

  Nathan Pinno
  Crew, McDonalds Restaurant, Camrose, AB Canada
  http://www.npinnowebsite.ca/
- Original Message -=20
From: Nathan Pinno=20
To: [EMAIL PROTECTED]
Sent: Monday, July 04, 2005 3:55 PM
Subject: [Tutor] Is there a way to combine a request for numbers 
and =
letters?


Hi all,

Is there a way to combine a request for numbers and letters (e.g. 
=
user id and passwords)?

Thanks,
Nathan Pinno
http://www.npinnowebsite.ca/



--
---=
---


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


--=_NextPart_000_0038_01C580B8.4099DB90
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable









  Sorry,
   
  Just remembered that strings can include both letters and =
numbers. Case=20
  closed.
   
  Nathan PinnoCrew, McDonalds Restaurant, Camrose, AB =
Canadahttp://www.npinnowebsite.ca/";>http://www.npinnowebsite.ca/

  
- Original Message - 

From:=20
mailto:[EMAIL PROTECTED]">Nathan=20
Pinno 
To: mailto:tutor@python.org";>tutor@python.org 
Sent: Monday, July 04, 
2005 =
3:55=20

[Tutor] (no subject)

2005-07-06 Thread Michael Huster
In python under windows, how can I create and access a file on the Desktop? The 
following do NOT work:
fp = open(r'Desktop\myFile','r')
fp = open(r'\Desktop\myFile','r')
fp = open(r'c:\Desktop\myFile','r')
fp = open(r'c:\Windows\Desktop\myFile','r')

I don't want to drill into c:\Documents and Setting\... because the user name 
is often obscure AND not all desktop files appear in the same subdir. of that. 
For example some are in 'All Users" or 'Default User'.

Michael Huster,
Simpson University
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Finding Desktop Files

2005-07-06 Thread Michael Huster
Repost - I forgot a subject
 
In python under windows, how can I create and access a file on the Desktop? The 
following do NOT work:
fp = open(r'Desktop\myFile','r')
fp = open(r'\Desktop\myFile','r')
fp = open(r'c:\Desktop\myFile','r')
fp = open(r'c:\Windows\Desktop\myFile','r')

I don't want to drill into c:\Documents and Setting\... because the user name 
is often obscure AND not all desktop files appear in the same subdir. of that. 
For example some are in 'All Users" or 'Default User'.

Michael Huster,
Simpson University
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2005-07-06 Thread Light
You could use this:

import os
fp = open(os.path.expanduser('~')+r'\Desktop\myFile', 'r')

Light


> In python under windows, how can I create and access a file on the
> Desktop? The following do NOT work: fp = open(r'Desktop\myFile','r')
> fp = open(r'\Desktop\myFile','r')
> fp = open(r'c:\Desktop\myFile','r')
> fp = open(r'c:\Windows\Desktop\myFile','r')
>
> I don't want to drill into c:\Documents and Setting\... because the
> user name is often obscure AND not all desktop files appear in the
> same subdir. of that. For example some are in 'All Users" or 'Default
> User'.
>
> Michael Huster,
> Simpson University
> ___
> 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] Case ? (fwd)

2005-07-06 Thread Danny Yoo


-- Forwarded message --
Date: Wed, 6 Jul 2005 00:01:52 -0700 (PDT)
From: Mike Cheponis <[EMAIL PROTECTED]>
To: Danny Yoo <[EMAIL PROTECTED]>
Subject: Re: [Tutor] Case ?

On Tue, 5 Jul 2005, Danny Yoo wrote:

> On Tue, 5 Jul 2005, Mike Cheponis wrote:
>
>> Why does Python not have a "case" statement, like C?



> Hi Mike,
>
> It's a proposed enhancement:
>
>http://www.python.org/peps/pep-0275.html

Thanks, comments below.

Since this didn't come out in Python 2.4, is it "automatically" re-considered 
for 2.5 ?


> That being said, a dispatch-table approach, using a dictionary, works well
> in Python because it's not hard to use functions as values --- most people
> haven't really missed case/switch statements in Python because dispatch
> tables can be very effective.
>
>
> For example, something like this:
>
> ### C ###
> switch(state) {
>case STATE_1: doStateOneStuff();
>  break;
>case STATE_2: doStateTwoStuff();
>  break;
>case STATE_3: doStateThreeStuff();
>  break;
>default:  doDefaultAction();
> ##
>
> has a natural translation into Python as:
>
> ### Python ###
> dispatchTable = { STATE_1: doStateOneStuff,
>  STATE_2: doStateTwoStuff,
>  STATE_3: doStateThreeStuff }
> command = dispatchTable.get(state, doDefaultAction)
> command()
> ##
>
> where we're essentially mimicking the jump table that a case/switch
> statement produces underneath the surface.

Sure, this is nice.

It does require one to "functionalize" each case, tho, rather than "doing the 
work" in the body of the case.


> One other consideration about C's case/switch statement is its
> bug-proneness: it's all too easy to programmers to accidently forget to
> put 'break' in appropriate places in there.


Years ago, I did this:

#define is :{
#define esac break;}


So above becomes:

switch(state) {
case STATE_1 is
 doStateOneStuff();
esac

case STATE_2 is
 doStateTwoStuff();
esac

case STATE_3 is
doStateThreeStuff();
esac

default is  doDefaultAction(); esac
}


I worked with one C programmer who said that's "not C" and refused to use this 
simple technique to reduce bugginess.  He was from Russia, and seemed pretty 
convinced about a lot of things ;-) but I don't work with him anymore.


> Hope this helps!

yes, greatly!  Thanks -Mike

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


Re: [Tutor] (no subject)

2005-07-06 Thread Mark Thomas
On 7/6/05, Michael Huster <[EMAIL PROTECTED]> wrote:
> In python under windows, how can I create and access a file on the Desktop? 
> The following do NOT work:
 
How about...

>>> userp = os.getenv('USERPROFILE') + '\\Desktop\\MyFile.txt'
>>> fo = file(userp, 'w')
-- 
 _
( ) Mark Thomas ASCII ribbon campaign
 X www.theswamp.org   - against HTML email
/ \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Adding attributes to imported class

2005-07-06 Thread Michael Lange
Hello list,

I'm trying to write a python wrapper for the tkDnD Tk extension to add drag and 
drop support
to Tkinter. Looking for a way to give all widgets access to the newly defined 
dnd methods
I came to create a subclass of Tkinter.Tk() and pass the dnd methods to 
Tkinter.BaseWidget() 
(all Tkinter widgets except Tk() inherit from BaseWidget()), like this:

## file TkinterDnD.py  #

import Tkinter

class Tk(Tkinter.Tk):
def __init__(self, *args, **kw):
Tkinter.Tk.__init__(self, *args, **kw)
self.tk.eval('package require tkdnd')

def dnd_clearsource(self):
'''Unregister widget as drag source.'''
return self.tk.call('dnd', 'clearsource', self)
Tkinter.BaseWidget.dnd_clearsource = dnd_clearsource

< etc. >

###

This is nice, because now I can simply do:

from Tkinter import *
import TkinterDnD

root = TkinterDnD.Tk()

to magically add dnd methods to all Tkinter widgets.

However, I can't help that this trick somehow reminds me of a do-it-yourself 
lobotomy,
so now my question, is this a common way to add new attributes to existing 
classes
or is it possible that this causes problems that I don't see at the moment?

Thanks in advance

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


[Tutor] Problem with data storage

2005-07-06 Thread festus freeborn
plz I need infor About  "Problem with data storage"
Thanks__Do You Yahoo!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2005-07-06 Thread Danny Yoo


On Wed, 6 Jul 2005, Michael Huster wrote:

> In python under windows, how can I create and access a file on the
> Desktop? The following do NOT work: fp = open(r'Desktop\myFile','r') fp
> = open(r'\Desktop\myFile','r') fp = open(r'c:\Desktop\myFile','r') fp =
> open(r'c:\Windows\Desktop\myFile','r')
>
> I don't want to drill into c:\Documents and Setting\... because the user
> name is often obscure AND not all desktop files appear in the same
> subdir. of that. For example some are in 'All Users" or 'Default User'.


Hi Michael,

One issue that might be relevant is that it appears you're asking Python
to open a file in read mode ('r').  Are you sure that you don't meant to
open a file in write mode ('w') instead?



> The following do NOT work: fp = open(r'Desktop\myFile','r') fp =
> open(r'\Desktop\myFile','r') fp = open(r'c:\Desktop\myFile','r') fp =
> open(r'c:\Windows\Desktop\myFile','r')

For the problem that you're running into now, it does seem like you need
to open the file in write mode to get it to create new files.  Next time,
it might be helpful to show how they don't work.  What kind of error
message do you see when you execute those statements?

The reason we ask this is because, in some cases, an error has less to do
with what you're doing, and more with an external situation outside of
Python.  One general principle for error reporting is to make sure to
provide enough information that someone else can try it, and enough output
that we know that the person will recognize that they're seeing the same
error that you're seeing.

It also helps because if we see an error message, we can also help explain
what the error message means.  That way, next time you see a similar error
message, you have a better chance to understand what the silly system is
really trying to say.  *grin*


Best of wishes to you!

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


[Tutor] file io: reading an int32 at the end of a file?

2005-07-06 Thread Marcus Goldfish
Hi,

I have a file format that ends in a 4-byte (int32) number.  I would
like to read this value in python on a WinXP machine with something
like:

fname = 'somefile'
f = open(fname, 'rb')
f.seek(-4,2)
offset = f.read()

... but this doesn't seem to work.  The value that Python returns is:

   '@\x19\x01\x00'

but I know from similar code in Matlab that the correct sequence is:

   64 25 1 0

Can someone point out my error?

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


Re: [Tutor] Case ? (fwd)

2005-07-06 Thread Danny Yoo


> > It's a proposed enhancement:
> >
> >http://www.python.org/peps/pep-0275.html
>
> Since this didn't come out in Python 2.4, is it "automatically"
> re-considered for 2.5 ?

Hello!

It's still in the "open" set of peps:

http://www.python.org/peps/

so there's no guarantee that PEP 275 go in. I think there's still quite a
bit of revision and discussion ahead before it's ready for the community.
The PEP system is described here:

http://www.python.org/peps/pep-0001.html




> > in Python because it's not hard to use functions as values --- most
> > people haven't really missed case/switch statements in Python because
> > dispatch tables can be very effective.

[code cut]

> > where we're essentially mimicking the jump table that a case/switch
> > statement produces underneath the surface.
>
> Sure, this is nice.
>
> It does require one to "functionalize" each case, tho, rather than
> "doing the work" in the body of the case.


Yes --- although that restriction might be a good thing, to force
programmers to break their code into smaller functions.  *grin*

Certain syntactic constraints in Python do put a cap on how complicated or
long our programs can be; I personally rationalize this as an attempt to
keep Python programs readable.



> > One other consideration about C's case/switch statement is its
> > bug-proneness: it's all too easy to programmers to accidently forget
> > to put 'break' in appropriate places in there.
>
> Years ago, I did this:
>
> #define is :{
> #define esac break;}

[code example cut]

Cute.  *grin*

Aside: I'm not against the idea of that style of case analysis; I'm
starting to get infatuated with the more general idea of "pattern
matching" that other programming languages --- for example, PLT Scheme and
OCaml --- provide.  But, unfortunately, I don't see anything like pattern
matching hitting Python anytime soon.

I just don't like how C does it, because it's so close to how the
underlying hardware works that it seems designed to be easy for the C
compiler writer to implement, rather than for the casual programmer to
use.

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


Re: [Tutor] (no subject)

2005-07-06 Thread Rudy Schockaert
This will work, but only on an English version of the OS. E.g. in Dutch the desktop is called 'Bureaublad'.On 7/6/05, Mark Thomas <
[EMAIL PROTECTED]> wrote:On 7/6/05, Michael Huster <
[EMAIL PROTECTED]> wrote:> In python under windows, how can I create and access a file on the Desktop? The following do NOT work:How about...>>> userp = os.getenv('USERPROFILE') + '\\Desktop\\MyFile.txt'
>>> fo = file(userp, 'w')-- _( ) Mark Thomas ASCII ribbon campaign X www.theswamp.org   - against HTML email/ \___
Tutor maillist  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
-- "A little nonsense now and then, is cherished by the wisest men."  -- Roald Dahl
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] file io: reading an int32 at the end of a file?

2005-07-06 Thread Danny Yoo


On Wed, 6 Jul 2005, Marcus Goldfish wrote:

> I have a file format that ends in a 4-byte (int32) number.  I would like
> to read this value in python on a WinXP machine with something like:
>
> fname = 'somefile'
> f = open(fname, 'rb')
> f.seek(-4,2)
> offset = f.read()
>
> ... but this doesn't seem to work.  The value that Python returns is:
>
>'@\x19\x01\x00'


Hi Marcus,

You are very close.  It looks like you're getting the right bytes back as
a single string, so you may need to manually destructure the bytes back
into numbers.  Let's try something:

##
>>> text = '@\x19\x01\x00'
>>> ord(text[0])
64
>>> ord(text[1])
25
>>> ord(text[2])
1
>>> ord(text[3])
0
##

So you are getting the right values: you just need to do a little bit more
work to unpack them.  Using ord() will do the trick, although there is a
better way to do it, which we'll talk about below:


> but I know from similar code in Matlab that the correct sequence is:
>
>64 25 1 0

In that case, it sounds like Matlab is taking those four bytes and
treating each bytes as a 'byte' integer.  Python has a nice facility for
doing this with the 'struct' module:

http://www.python.org/doc/lib/module-struct.html

##
>>> import struct
>>> struct.unpack('', '@\x19\x01\x00')
(64, 25, 1, 0)
##


I'm not sure if Matlab is treating each byte as an unsigned or signed
char, so I'm guessing a signed interpretation at the moment.


If you have more questions, please feel free to ask.

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


Re: [Tutor] Case ? (fwd)

2005-07-06 Thread Smith, Jeff
If you like the switch statement (which I do) and believe Python should
have one (which I do) then you might take a look at this which someone
posted when I asked this same question a few months ago:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410692

Jeff

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Danny Yoo
Sent: Wednesday, July 06, 2005 4:29 PM
To: Tutor
Cc: [EMAIL PROTECTED]
Subject: Re: [Tutor] Case ? (fwd)




> > It's a proposed enhancement:
> >
> >http://www.python.org/peps/pep-0275.html
>
> Since this didn't come out in Python 2.4, is it "automatically" 
> re-considered for 2.5 ?

Hello!

It's still in the "open" set of peps:

http://www.python.org/peps/

so there's no guarantee that PEP 275 go in. I think there's still quite
a bit of revision and discussion ahead before it's ready for the
community. The PEP system is described here:

http://www.python.org/peps/pep-0001.html




> > in Python because it's not hard to use functions as values --- most 
> > people haven't really missed case/switch statements in Python 
> > because dispatch tables can be very effective.

[code cut]

> > where we're essentially mimicking the jump table that a case/switch 
> > statement produces underneath the surface.
>
> Sure, this is nice.
>
> It does require one to "functionalize" each case, tho, rather than 
> "doing the work" in the body of the case.


Yes --- although that restriction might be a good thing, to force
programmers to break their code into smaller functions.  *grin*

Certain syntactic constraints in Python do put a cap on how complicated
or long our programs can be; I personally rationalize this as an attempt
to keep Python programs readable.



> > One other consideration about C's case/switch statement is its
> > bug-proneness: it's all too easy to programmers to accidently forget

> > to put 'break' in appropriate places in there.
>
> Years ago, I did this:
>
> #define is :{
> #define esac break;}

[code example cut]

Cute.  *grin*

Aside: I'm not against the idea of that style of case analysis; I'm
starting to get infatuated with the more general idea of "pattern
matching" that other programming languages --- for example, PLT Scheme
and OCaml --- provide.  But, unfortunately, I don't see anything like
pattern matching hitting Python anytime soon.

I just don't like how C does it, because it's so close to how the
underlying hardware works that it seems designed to be easy for the C
compiler writer to implement, rather than for the casual programmer to
use.

___
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] Problem with data storage

2005-07-06 Thread Bob Gailer


At 08:36 AM 7/6/2005, festus freeborn wrote:
plz I need infor About
"Problem with data storage"
We need more information. How does this question relate to Python? What
is the problem you are having? Why do you need to know?

Bob Gailer
mailto:[EMAIL PROTECTED]
510 558 3275 home
720 938 2625 cell 

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


Re: [Tutor] file io: reading an int32 at the end of a file?

2005-07-06 Thread Bob Gailer


At 01:12 PM 7/6/2005, Marcus Goldfish wrote:
Hi,
I have a file format that ends in a 4-byte (int32) number.  I
would
like to read this value in python on a WinXP machine with something
like:
fname = 'somefile'
f = open(fname, 'rb')
f.seek(-4,2)
offset = f.read()
... but this doesn't seem to work.  The value that Python returns
is:
   '@\x19\x01\x00'
but I know from similar code in Matlab that the correct sequence
is:
   64 25 1 0
Can someone point out my error?
A file is a sequence of characters. That is what Python is showing you.
Since 3 of them are not "graphic characters" Python displays
their hexadecimal equivalents.. x19 translated to base 10 is 25. To ask
Python to (in essence) cast a character to an integer you use the ord
function. Ord('\x19') will give you integer 25.

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


Bob Gailer
mailto:[EMAIL PROTECTED]
510 558 3275 home
720 938 2625 cell 

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


Re: [Tutor] Adding attributes to imported class

2005-07-06 Thread jfouhy
Quoting Michael Lange <[EMAIL PROTECTED]>:

> I'm trying to write a python wrapper for the tkDnD Tk extension to add
> drag and drop support

Hi Michael,

Just a side issue --- tkDnD seems to be broken in python 2.4: see bug 1164742,
http://sourceforge.net/tracker/index.php?func=detail&aid=1164742&group_id=5470&atid=105470

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


Re: [Tutor] Case ? (fwd)

2005-07-06 Thread Alan G
The reason why Python doresn't have a case statement is simply 
because Guido didn't put one in. Why he didn't I don't know, 
you'd need to ask him. But I guess that since case stateents 
are simply syntactic sugar (well nearly), that providing one 
might have seemed like going against Python's ethos of explicit 
being better than implicit.

The reason why it doesn't have C style case statements is more 
likely to be due to the fact that C case statements are 
notoriously bug prone. Pascal or VB style case statements 
might have been acceptable...

But ultimately they are only a minor performance tweak over 
an if/elif chain saving the cost of one boolean comparison
and possibly an assembler jump per case. 

Long case constructs are better done as dispatch tables - and
yes you have to wrap them in functions, but thats definitely a 
good thing and should be done in well written case statements 
too IMHO.

Short case statements are usually fine in if/elif chains.

And type based case statements should be eliminated by using 
objects and polymorphism.

But see a long thread in the archives between Jeff Smith 
(advocating cases) and myself playing devils advocate for the 
status quo (about a year ago?) for much more discussion :-)

HTH,

Alan G.

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


Re: [Tutor] Problem with data storage

2005-07-06 Thread Alan G
From: "festus freeborn" <[EMAIL PROTECTED]>
> plz I need infor About  "Problem with data storage"

You might need to give us a clue here.
What kind of problem? What kind of data? What kind of storage?
 - for how long? how quickly retrieved?

And if its a homework exercise, which it looks like it might be, 
how far have you got yourself? What have you tried to do to find 
the answer - actually thats a good thing to tell us even if its
not homework!

Personally for long lived data I tend to use a filing cabinet...

Alan G.

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


Re: [Tutor] file io: reading an int32 at the end of a file?

2005-07-06 Thread Alan G
--
... but this doesn't seem to work.  The value that Python returns is:

   '@\x19\x01\x00'

but I know from similar code in Matlab that the correct sequence is:

   64 25 1 0

Can someone point out my error?
--

That looks like the same thing to me. 
The ascii code for '@' is 64...
hex 19 = decimal 25

Why do you think there is a problem?

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Doctype declaration

2005-07-06 Thread gordnjen



Thanks to everyone who helped me with the 
problem I had with my "age" program. I now have it working properly 
(yay).
 
So, now I need to know how to insert a 
doctype declaration into my python script so that a) the python script runs 
properly and b)the page validates as valid XHTML.
 
Here is my code below. It runs fine as a 
python script, but won't validate:
 
import timeprint "Content-type: 
text/html"printprint ""print 
"Current Time"print 
""print "Current 
Time"print "The current time is"print 
"", time.asctime(), ""print 
""
 
Any help would be greatly 
appreciated!
 
Thanks!
 
Regards,
 
Jennine
 
 

-Original Message-From: gordnjen 
[mailto:[EMAIL PROTECTED] Sent: July 4, 2005 10:25 
PMTo: 'tutor@python.org'Subject: "And" 
function
I need to write a program that will do the 
following:
 
Ask the user's age.
If their age is below 1 yr old, it prints 
"you are old enought to eat baby food"
If they are over 16, it prints "You are old 
enough to drive"
If they are over 65, it prints "You are old 
enough to drive" and "You are old enough to retire"
 
If they are between the ages of 16 and 25, 
it prints "you are old enough to get a student discount".
 
So far, I have this:
 
age =int(raw_input("How old are 
you?"))if age>16:    print "You are old enough to 
drive!"    if 
age>65:    print "You are old 
enough to retire!"    if 
age<1:    
print "You are old enough to eat baby food!"
 
 
I get stuck with the old enough to get a 
student discount part. I have tried
 
if age>16 and age<25:
print "You are old enough to get a student 
discount"
 
But it doesn't seem to work.
 
Any help would be great.
 
Thanks!
 
Jennine
 
 

 


 



--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.8.9/41 - Release Date: 05/07/2005
 

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


Re: [Tutor] Doctype declaration

2005-07-06 Thread Kent Johnson
gordnjen wrote:
> So, now I need to know how to insert a doctype declaration into my 
> python script so that a) the python script runs properly and b)the page 
> validates as valid XHTML.
>  
> Here is my code below. It runs fine as a python script, but won't validate:
>  
> import time
> print "Content-type: text/html"
> print

print ''
print '''http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>'''
print 'http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">'

> print ""
> print "Current Time"
> print ""
> print "Current Time"
> print "The current time is"
> print "", time.asctime(), ""
> print ""

By the way, triple-quoted strings are very handy for this, you can just say

print '''
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">

Current Time

Current Time
The current time is'''

Kent

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


Re: [Tutor] What's wrong with this code?

2005-07-06 Thread Nathan Pinno
  Hi all,

  I meant to ask why the main part after the password is not working right. 
No one has answered that yet. When I run the code and try to load a file 
that has been saved, a TypeError appears. How do I fix the code so no more 
errors will show up. Here is the newest code so far:

  # This is the code for a password protected program to store passwords.
  password = "hello"
  print "The Password Program"
  print "Copywrite 2005. All Rights Reserved."
  print
  answer = raw_input("What is the password? ")
  while password != answer:
  print "The password is incorrect."
  answer = raw_input("What is the password? ")
  def main_menu():
  print "1) Add a login info card"
  print "2) Lookup a login info card"
  print "3) Remove a login info card"
  print "4) Print Login info list"
  print "5) Save login list"
  print "6) Open Login list"
  print "9) Exit"

  def load_login(site,filename):
  in_file = open(filename,"r")
  while 1:
  in_line = in_file.readline()
  if len(in_file) == 0:
  break
  in_line = in_line[:-1]
  [site,id,passcard] = string.split(in_line,",")
  list[site] = id and passcard
  in_file.close()

  def save_login(site,filename):
  out_file = open(filename,"w")
  for x in site.keys():
  out_file.write(x+","+sites[x]+"\n")
  out_file.close()

  menu_choice = 0
  list = {}
  print "Welcome to the second half of the program."
  print main_menu()
  while menu_choice != 9:
  menu_choice = input("Choose an option: ")
  if menu_choice == 1:
  print "Add a login info card"
  site = raw_input("Site: ")
  id = raw_input("User ID: ")
  passcard = raw_input("Password: ")
  list[site] = id and passcard
  menu_choice = input("Choose an option: ")
  elif menu_choice == 2:
  print "Lookup a login info card"
  site = raw_input("Site: ")
  if site.has_key(site):
  print "The ID is: ",id(site)
  print "The password is: ",passcard(site)
  else:
  print site," was not found."
  menu_choice = input("Choose an option: ")
  elif menu_choice == 3:
  print "Remove a login info card"
  site = raw_input("Site: ")
  if sites.has_key(site):
  del numbers[site]
  else:
  print site," was not found."
  menu_choice = input("Choose an option: ")
  elif menu_choice == 4:
  print "Login Info"
  for x in site.keys():
  print "Site: ",x," \tID: ",numbers[x]," \tPassword: 
",numbers[x]
  print
  menu_choice = input("Choose an option: ")
  elif menu_choice == 5:
  filename = raw_input("Filename to save: ")
  save_login(list,filename)
  menu_choice = input("Choose an option: ")
  elif menu_choice == 6:
  filename == raw_input("Filename to load: ")
  load_login(list,filename)
  menu_choice = input("Choose an option: ")
  print "Have a nice day!"

  Thanks for the input so far,
  Nathan Pinno

  - Original Message - 
  From: "Brian van den Broek" <[EMAIL PROTECTED]>
  Cc: 
  Sent: Tuesday, July 05, 2005 1:03 AM
  Subject: Re: [Tutor] What's wrong with this code?


  > Andre Engels said unto the world upon 05/07/2005 02:44:
  >>>From the program::
  >>
  >> answer = raw_input("What is the password? ")
  >> while password != answer:
  >> print "The password is incorrect."
  >
  > 
  >
  >> I think you intended to make it so that
  >> the program kept asking for passwords until the right one was given.
  >> This is done with:
  >> answer = raw_input("What is the password? ")
  >> while password != answer:
  >> print "The password is incorrect."
  >> answer = raw_input("What is the password? ")
  >
  >
  > A small thing, but I think that is better as:
  >
  > while True:
  >  answer = raw_input("What is the password? ")
  >  if password == answer:
  >  break
  >  print "The password is incorrect."
  >
  > It probably runs a bit slower, but who cares, as the bottleneck is in
  > the chair, not the chip. The advantage is that there is only one
  > statement of the prompt to alter if you wanted to change it later.
  >
  > But, I think this will be one where reasonable people can differ.
  > Andre's version does make the semantics of the loop somewhat more 
obvious.
  >
  > Best to all,
  >
  > Brian vdB
  >
  > 
  >
  >> Andre Engels
  >
  > 
  >
  >
  > ___
  > Tutor maillist  -  Tutor@python.org
  > http://mail.python.org/mailman/listinfo/tutor
  >
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Why is this error showing up? (Original Message: (Tutor) What's wrong with this code?) Ignore previous post.

2005-07-06 Thread Nathan Pinno



Hi all,
 
Here's one of the messages that pops up:
 
Traceback (most recent call last):  File "D:\password.py", line 
77, in ?    filename == raw_input("Filename to load: 
")NameError: name 'filename' is not defined
 
Why is it popping up whenever I try to load a file?
 
Here's the latest code:
 
# This is the code for a password protected program to store 
passwords.password = "hello"print "The Password Program"print 
"Copyright 2005 Nathan Pinno."printanswer = raw_input("What is the 
password? ")while password != answer:    print "The 
password is incorrect."    answer = raw_input("What is the 
password? ")def main_menu():    print "1) Add a login 
info card"    print "2) Lookup a login info 
card"    print "3) Remove a login info 
card"    print "4) Print Login info 
list"    print "5) Save login list"    
print "6) Open Login list"    print "9) Exit"
 
def load_login(site,filename):    in_file = 
open(filename,"r")    while 
1:    in_line = 
in_file.readline()    if len(in_file) 
== 0:    
break    in_line = 
in_line[:-1]    [site,id,passcard] = 
string.split(in_line,",")    
list[site] = id and passcard    
in_file.close()    def 
save_login(site,filename):    out_file = 
open(filename,"w")    for x in 
site.keys():    
out_file.write(x+","+sites[x]+"\n")    
out_file.close()    menu_choice = "0"list = 
{}print "Welcome to the second half of the program."print 
main_menu()while menu_choice != "9":    menu_choice = 
raw_input("Choose an option: ")    if menu_choice == 
"1":    print "Add a login info 
card"    site = raw_input("Site: 
")    id = raw_input("User ID: 
")    passcard = raw_input("Password: 
")    list[site] = id and 
passcard    menu_choice = 
raw_input("Choose an option: ")    elif menu_choice == 
"2":    print "Lookup a login info 
card"    site = raw_input("Site: 
")    if 
site.has_key(site):    
print "The ID is: 
",id(site)    
print "The password is: 
",passcard(site)    
else:    
print site," was not found."    
menu_choice = raw_input("Choose an option: ")    elif 
menu_choice == "3":    print "Remove 
a login info card"    site = 
raw_input("Site: ")    if 
sites.has_key(site):    
del numbers[site]    
else:    
print site," was not found."    
menu_choice = raw_input("Choose an option: ")    elif 
menu_choice == "4":    print "Login 
Info"    for x in 
site.keys():    
print "Site: ",x," \tID: ",numbers[x]," \tPassword: 
",numbers[x]    
print    menu_choice = 
raw_input("Choose an option: ")    elif menu_choice == 
"5":    filename = 
raw_input("Filename to save: ")    
save_login(list,filename)    
menu_choice = raw_input("Choose an option: ")    elif 
menu_choice == "6":    filename == 
raw_input("Filename to load: ")    
load_login(list,filename)    
menu_choice = raw_input("Choose an option: ")print "Have a nice day!"
 
Anything else that needs addressing?
 
Thanks,
Nathan Pinnohttp://www.npinnowebsite.ca/

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