Re: [Tutor] Automating Windows (Maintenance)

2007-07-03 Thread Alan Gauld

"Tiger12506" <[EMAIL PROTECTED]> wrote

> The cleanup temp files is more difficult. It depends on how much you 
> want to
> do. Usually it is sufficient to delete the contents of the 
> windows/temp
> folder. This will delete cookies, temporary files, internet temp 
> files that
> are commonly cleaned up by sys admin. So you can add  os.system('del 
> /s /f
> /s /q C:\windows\temp\*')

For deleting files you get more control doing it from within Python.
os.remove() will  delete files.

> For checking processes, you can search for a tool on the internet 
> that lists
> currently running processes.

Again this kind of thing can be done with the _winreg module for
reading the registry. (or using WSH)

> If you are really interested in a program that does this, I could be
> encouraged to write one for you (in C) - Although, it would most 
> certainly
> be better to find api s that allow you to do this in python. Google.

All the windows APIs are available to Python, no C required.
See the recent threads on mouse events for more on that topic.


-- 
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


[Tutor] how long?

2007-07-03 Thread Ben Waldin
How long will it take until I successfully create my own working program that 
is useful? I have crated the address book ones in the tutors and just want to 
know how long it takes before I start to create my own thought up programs that 
will be useful. Thanks Ben   
_
Discover the new Windows Vista
http://search.msn.com/results.aspx?q=windows+vista&mkt=en-US&form=QBRE___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] n.isalnum() is failing

2007-07-03 Thread Terry
Hi!

I am running Python 2.5, and I have an IF statement in the below program
that does not seem 
to be doing it's job. The program simply acquires a range of years from
the user and prints out 
all the leap years between the two dates. I am having trouble in
trapping for possible user 
errors. I am using x.isalnum() to check that I have a number for each
year entered, and it 
doesn't look to me like it is functioning properly.

When I purposely enter bad data, such as '' for one of the a year
entries, the IF statement:

if start.isalnum() == 1 and end.isalnum() == 1:

-Fails to detect that '' is not a number and lets the program then
die tragically a horrible 
sudden awkward death on the very next line:

start = int(start); end = int(end)

Traceback (most recent call last):
  File "/home/lucky/Documents/Python_Programs/leap_years.py", line 119,
in 
start = int(start); end = int(end)
ValueError: invalid literal for int() with base 10: ''

If I say on the commandline:

>>> n = ''
>>> n.isalnum()
True >> 


My program starts here:

def leap():
answer = 0
t1 =  / 4
if t1 == int(t1):
t2 =  / 100
t3 =  / 400
if t2 <> int(t2) or t3 == int(t3):
answer = "-- leap year!"
return answer

print "This program finds all leap years between two dates.";print;print

start = raw_input("Enter  for beginning year : ")
end = raw_input("Enter  for ending year : ")

if len(start) == 4 and len(end) == 4:
if start.isalnum() == 1 and end.isalnum() == 1:
#<--fails to detect '' as not a number
start = int(start); end = int(end)
#<--commits suicide here
if start > 0 and end > start:
print; print
for i in range(start, end + 1):
answer = leap(i)
if answer != 0:
print i, answer
print "Done!"
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] n.isalnum() is failing

2007-07-03 Thread Wolfram Kraus
Use isdigit instead of isalnum.

HTH,
Wolfram

On 03.07.2007 09:51, Terry wrote:
> Hi!
> 
> I am running Python 2.5, and I have an IF statement in the below program
> that does not seem
> to be doing it's job. The program simply acquires a range of years from
> the user and prints out
> all the leap years between the two dates. I am having trouble in
> trapping for possible user
> errors. I am using x.isalnum() to check that I have a number for each
> year entered, and it
> doesn't look to me like it is functioning properly.
> 
> When I purposely enter bad data, such as '' for one of the a year
> entries, the IF statement:
> 
> if start.isalnum() == 1 and end.isalnum() == 1:
> 
> -Fails to detect that '' is not a number and lets the program then
> die tragically a horrible
> sudden awkward death on the very next line:
> 
> start = int(start); end = int(end)
> 
> Traceback (most recent call last):
>   File "/home/lucky/Documents/Python_Programs/leap_years.py", line 119,
> in 
> start = int(start); end = int(end)
> ValueError: invalid literal for int() with base 10: ''
> 
> If I say on the commandline:
> 
 n = ''
 n.isalnum()
> True  False!!!

> 
> 
> My program starts here:
> 
> def leap():
> answer = 0
> t1 =  / 4
> if t1 == int(t1):
> t2 =  / 100
> t3 =  / 400
> if t2 <> int(t2) or t3 == int(t3):
> answer = "-- leap year!"
> return answer
> 
> print "This program finds all leap years between two dates.";print;print
> 
> start = raw_input("Enter  for beginning year : ")
> end = raw_input("Enter  for ending year : ")
> 
> if len(start) == 4 and len(end) == 4:
> if start.isalnum() == 1 and end.isalnum() == 1:  
> #<--fails to detect '' as not a number
> start = int(start); end =
> int(end)   #<--commits
> suicide here
> if start > 0 and end > start:
> print; print
> for i in range(start, end + 1):
> answer = leap(i)
> if answer != 0:
> print i, answer
> print "Done!"
> 
> 
> 
> 
> ___
> 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] n.isalnum() is failing

2007-07-03 Thread bhaaluu
Greetings,

Perhaps the first thing you should do, before checking
whether the user entered '' instead of '2000' is to
get the leap year function working properly.

Definition:
Leap years occur according to the following formula:
   a leap year is divisible by four,
   but not by one hundred,
   unless it is divisible by four hundred.

I find that definition to be a wee bit misleading.
This might help:

A leap year is divisible by four, with a REMAINDER of zero.
The remainder of a division is found in python with the "%" operator.
So you might want to check and see IF the remainders are zero or one,
TRUE or FALSE.

When I run your program, entering two four-digit years,
this is the output:

This program finds all leap years between two dates.

Enter  for beginning year : 1995
Enter  for ending year : 2000

1995 -- leap year!
1996 -- leap year!
1997 -- leap year!
1998 -- leap year!
1999 -- leap year!
2000 -- leap year!
Done!

Since we know that ALL of those years can't be leap years,
it means that your leap() function isn't doing something right?
I suggest that you get leap() working properly first, then tackle
the other problem.

Cheers!
-- 
bhaaluu at gmail dot com

On 7/3/07, Terry <[EMAIL PROTECTED]> wrote:
>
>  Hi!
>
>  I am running Python 2.5, and I have an IF statement in the below program
> that does not seem
>  to be doing it's job. The program simply acquires a range of years from the
> user and prints out
>  all the leap years between the two dates. I am having trouble in trapping
> for possible user
>  errors. I am using x.isalnum() to check that I have a number for each year
> entered, and it
>  doesn't look to me like it is functioning properly.
>
>  When I purposely enter bad data, such as '' for one of the a year
> entries, the IF statement:
>
>  if start.isalnum() == 1 and end.isalnum() == 1:
>
>  -Fails to detect that '' is not a number and lets the program then die
> tragically a horrible
>  sudden awkward death on the very next line:
>
>  start = int(start); end = int(end)
>
>  Traceback (most recent call last):
>File
> "/home/lucky/Documents/Python_Programs/leap_years.py", line
> 119, in 
>  start = int(start); end = int(end)
>  ValueError: invalid literal for int() with base 10: ''
>
>  If I say on the commandline:
>
>  >>> n = ''
>  >>> n.isalnum()
>  True  False!!!
>  >>>
>
>
>  My program starts here:
>
>  def leap():
>  answer = 0
>  t1 =  / 4
>  if t1 == int(t1):
>  t2 =  / 100
>  t3 =  / 400
>  if t2 <> int(t2) or t3 == int(t3):
>  answer = "-- leap year!"
>  return answer
>
>  print "This program finds all leap years between two dates.";print;print
>
>  start = raw_input("Enter  for beginning year : ")
>  end = raw_input("Enter  for ending year : ")
>
>  if len(start) == 4 and len(end) == 4:
>  if start.isalnum() == 1 and end.isalnum() == 1:
> #<--fails to detect '' as not a number
>  start = int(start); end = int(end)
>  #<--commits suicide here
>  if start > 0 and end > start:
>  print; print
>  for i in range(start, end + 1):
>  answer = leap(i)
>  if answer != 0:
>  print i, answer
>  print "Done!"
> ___
> 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] n.isalnum() is failing

2007-07-03 Thread زياد بن عبدالعزيز الباتلي
Terry wrote:
> Hi!
> 
Hi...

  
> I am using x.isalnum() to check that I have a number for each year
> entered,  and it doesn't look to me like it is functioning properly.
  
That's because "x.isalnum()" will return True if "x" is not empty and 
it's content are *either* an Alpha or a Number (which is not what you want)!

You need to use "x.isdigit()" for your need.

Typing "help(str)" inside the Python shell will read:
  
  |  isalnum(...)
  |  S.isalnum() -> bool
  |
  |  Return True if all characters in S are alphanumeric
  |  and there is at least one character in S, False otherwise.
  |
  |  isalpha(...)
  |  S.isalpha() -> bool
  |
  |  Return True if all characters in S are alphabetic
  |  and there is at least one character in S, False otherwise.
  |
  |  isdigit(...)
  |  S.isdigit() -> bool
  |
  |  Return True if all characters in S are digits
  |  and there is at least one character in S, False otherwise.
  


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


Re: [Tutor] how long?

2007-07-03 Thread Thorsten Kampe
* Ben Waldin (Tue, 3 Jul 2007 19:46:42 +1200)
> How long will it take until I successfully create my own working program that 
> is useful? I have crated the address book ones in the tutors and just want to 
> know how long it takes before I start to create my own thought up programs 
> that will be useful. Thanks Ben   

Approximately ten days, four hours and six minutes

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


Re: [Tutor] n.isalnum() is failing

2007-07-03 Thread Andreas Kostyrka
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Consider using something like:

try:
start, end = int(start), int(end)
except ValueError:
print "oops it was not a number."

Andreas

Terry wrote:
> Hi!
> 
> I am running Python 2.5, and I have an IF statement in the below program
> that does not seem 
> to be doing it's job. The program simply acquires a range of years from
> the user and prints out 
> all the leap years between the two dates. I am having trouble in
> trapping for possible user 
> errors. I am using x.isalnum() to check that I have a number for each
> year entered, and it 
> doesn't look to me like it is functioning properly.
> 
> When I purposely enter bad data, such as '' for one of the a year
> entries, the IF statement:
> 
> if start.isalnum() == 1 and end.isalnum() == 1:
> 
> -Fails to detect that '' is not a number and lets the program then
> die tragically a horrible 
> sudden awkward death on the very next line:
> 
> start = int(start); end = int(end)
> 
> Traceback (most recent call last):
>   File "/home/lucky/Documents/Python_Programs/leap_years.py", line 119,
> in 
> start = int(start); end = int(end)
> ValueError: invalid literal for int() with base 10: ''
> 
> If I say on the commandline:
> 
 n = ''
 n.isalnum()
> True  False!!!
> 
> 
> My program starts here:
> 
> def leap():
> answer = 0
> t1 =  / 4
> if t1 == int(t1):
> t2 =  / 100
> t3 =  / 400
> if t2 <> int(t2) or t3 == int(t3):
> answer = "-- leap year!"
> return answer
> 
> print "This program finds all leap years between two dates.";print;print
> 
> start = raw_input("Enter  for beginning year : ")
> end = raw_input("Enter  for ending year : ")
> 
> if len(start) == 4 and len(end) == 4:
> if start.isalnum() == 1 and end.isalnum() == 1:
> #<--fails to detect '' as not a number
> start = int(start); end = int(end)
> #<--commits suicide here
> if start > 0 and end > start:
> print; print
> for i in range(start, end + 1):
> answer = leap(i)
> if answer != 0:
> print i, answer
> print "Done!"
> 
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGij4jHJdudm4KnO0RAnKcAJ4lQWC3g0RklnwDwbpldapIeGFxDwCg2ayR
Mr6uMnoXINQHF3QTLO6Rl34=
=0kzm
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] MySQL -->Python-->XML for JSviz

2007-07-03 Thread Picio
Hello all, I need some advice to choose an xml generator for jsviz a
tool in javascript to create some wonderful graphs (SnowFlake or Force
directed).

Starting from a SQL table (mysql) I need to create a XML file with a
structure like this:

  
  
  
  
  
  


Where nodes attributes are the MySQL table fields (the PK will be the
attribute).
I know there are a lot of good tools to do this in pyhton (some maybe
is already shipped with pyton itself), but since I need a lot of
flexibility for the future I'd like to use an Object relational mapper
like SQLAlchemy or SQLObject todo the job. When I say flexibility, I
have in mind a solution to have multiple formats coming outside of the
ORM:
json, csv, plain text etc...
Am I wrong?
Can someone advice something?

-- 
http://picio.gotdns.com ...Il mio blog su NSLU2
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how long?

2007-07-03 Thread Mike Hansen
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Ben Waldin
> Sent: Tuesday, July 03, 2007 1:47 AM
> To: tutor@python.org
> Subject: [Tutor] how long?
> 
> How long will it take until I successfully create my own 
> working program that is useful? I have crated the address 
> book ones in the tutors and just want to know how long it 
> takes before I start to create my own thought up programs 
> that will be useful. Thanks Ben   
> 

What kind of programs do you have in mind? Maybe you can break down a
program you want into pieces and work on a piece of it. Maybe you want
some sort of GUI program. Well, first you can create a command line
version on it. Then later you can make it a GUI program. The tutor list
can help when you have questions.

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


Re: [Tutor] how long?

2007-07-03 Thread Jessica Griffin

On 7/3/07, Ben Waldin <[EMAIL PROTECTED]> wrote:


How long will it take until I successfully create my own working program
that is useful? I have crated the address book ones in the tutors and just
want to know how long it takes before I start to create my own thought up
programs that will be useful. Thanks Ben




--

This depends entirely on the following things:

1) How motivated are you?
2) How much time are you willing to put in?
3) What do you call "useful"?

There is no set "time frame" you can count on.  No matter what some
books/teachers may tell you, you *cannot* "Learn how to (do whatever) in X
number of hours"---that's an unreasonable standard to hold yourself to.  But
the more motivated you are to learn, the more you *will* learn.  And the
more time you put in, the "faster" you will acquire that knowledge.  For
more on this idea, read Peter Norvig's excellent essay "Teach Yourself
Programming in Ten Years" at 

That pretty much answers 1 and 2, but to answer 3, you have to think about
what you mean by a "useful" program.  That is entirely subjective, and I
think you're backing yourself into a bad corner by thinking of a *program*
as useful/nonuseful.  The beauty of a language like Python is that it is
easy to build different "modules," if you will, that you can use over and
over again in new and creative ways.  So perhaps instead of thinking in
terms of "how useful is this *program*" you should be thinking "how useful
is this *skill*."  The answer, for each skill you learn, will be different,
but if you think of it that way, you'll be a lot more satisfied with your
daily progress.  For example:  If you are learning how to print text to the
screen, such as the basic "Hello World" that almost everyone learns first,
that *is* useful, because *without* that, you can't really do anything
else.  See what I'm getting at here?

Once you build a basic toolbox of skills, then you can begin to look for
challenges that need to be solved.  And in order to do that, you are only
limited by the skills you have, and by your own imagination.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] UTF-8 filenames encountered in os.walk

2007-07-03 Thread William O'Higgins Witteman
I have several programs which traverse a Windows filesystem with French
characters in the filenames.

I have having trouble dealing with these filenames when outputting these
paths to an XML file - I get UnicodeDecodeError: 'ascii' codec can't
decode byte 0xe9 ... etc.  That happens when I try to convert to UTF-8.

I know what os will give me UFT-8 if I give it UTF-8, and I am trying to
do that, but somewhere down the line it seems like it reverts to ASCII,
and then I get these errors.

Has anyone found a silver bullet for ensuring that all the filenames
encountered by os.walk are treated as UTF-8?  Thanks.
-- 

yours,

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


[Tutor] Help with plugins

2007-07-03 Thread Ark

Hi!
First, my english may be bad, so I apoligize.
This is the first time i mail the list and it's about something i've been
investigating, but i haven't found enough information.
I want to learn how to do extensible aplications using plugins.  I've found
some how tos an tutorials, but they're not deep enough for my needs, and i
need your help with documentation about the topic, or with everything you
consider helpful.

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


Re: [Tutor] how long?

2007-07-03 Thread Carroll, Barry
> -Original Message-
> Message: 7
> Date: Tue, 3 Jul 2007 19:46:42 +1200
> From: Ben Walden <[EMAIL PROTECTED]>
> Subject: [Tutor] how long?
> To: 
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> How long will it take until I successfully create my own working
program
> that is useful? I have crated the address book ones in the tutors and
just
> want to know how long it takes before I start to create my own thought
up
> programs that will be useful. Thanks Ben
> _
Hi, Ben.

The quick answer to your question is another question: what do you want
to do?  If you have done the tutorial examples successfully, you
probably have the basics of Python down well enough to begin a project
of your own right now.  So, pick an idea that interests you or a
function that you need and start in!  When you run into trouble ask for
help here.  Plenty of people are happy to help out.  

If you have trouble coming up with an idea for a project,  check
python.org for existing projects in need of help (anybody know the exact
URL?).  There are lots of ongoing projects looking for programmers to
assist, with tasks ranging from novice to guru skill levels.  You should
be able to find something that will challenge you without overwhelming
you.  

Good luck! 

Regards,
 
Barry
[EMAIL PROTECTED]
541-302-1107

We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed


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


Re: [Tutor] Help with plugins

2007-07-03 Thread David Rock
* Ark <[EMAIL PROTECTED]> [2007-07-03 11:50]:
> Hi!
> First, my english may be bad, so I apoligize.
> This is the first time i mail the list and it's about something i've been
> investigating, but i haven't found enough information.
> I want to learn how to do extensible aplications using plugins.  I've found
> some how tos an tutorials, but they're not deep enough for my needs, and i
> need your help with documentation about the topic, or with everything you
> consider helpful.

I think we need some more information explaining what you are trying to
accomplish.  Can you supply an example of an "extensible application"
and also a longer description of what you mean when you say "plugin."

Perhaps some links to what you have looked at so far would also help to
clarify?

-- 
David Rock
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with plugins

2007-07-03 Thread Kent Johnson
Ark wrote:
> Hi!
> First, my english may be bad, so I apoligize.
> This is the first time i mail the list and it's about something i've 
> been investigating, but i haven't found enough information.
> I want to learn how to do extensible aplications using plugins.  I've 
> found some how tos an tutorials, but they're not deep enough for my 
> needs, and i need your help with documentation about the topic, or with 
> everything you consider helpful.

Googling 'python plugin' finds quite a few examples including this one 
which is very simple:
http://pytute.blogspot.com/2007/04/python-plugin-system.html

and this which seems pretty detailed:
http://lucumr.pocoo.org/blogarchive/python-plugin-system

and this:
http://peak.telecommunity.com/DevCenter/setuptools#dynamic-discovery-of-services-and-plugins

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


[Tutor] n.isalnum() is failing

2007-07-03 Thread Terry
Ha Ha Ha  It appears I was having a very blond day.

For some reason, I was mentally verbalizing to myself, each time I
looked at x.isalnum(), "X  IS-ALL-NUMBERS", 
instead of "X  IS-Alpha-Numeric".  I remember thinking..if one can
call it that., "I wonder why x.isdigit() 
is singular for only one digit...why couldn't they have made it
plural and singular? (It might help to realize the 
example I was looking at for x.digit() showed it only operating on one
digit. But then, one must remember that 
blond is sometimes really blond.once it starts.

And you are sooo right, I need to fix the leap() portion of the program.

Thanks!  It will be nice to get back to the program without stubbornly
trying to coerce or trick poor isalnum() into 
doing what it can't do.

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


[Tutor] [Fwd: [Fwd: Re: n.isalnum() is failing]]

2007-07-03 Thread Terry
Hi Bhaaluu,

I have misled you. I am working on a section ABOVE the program I showed,
that I am thinking to 
have my most used elements in, left in a state of being # remarks. In
other words, I am thinking 
of creating a set of most used CONVENTIONS and leave them as a blank
file. Thus, when I need 
to write a new program, I just grab the blank CONVENTIONS, and rename it
to my newest program 
name. I have no idea if this is a good idea or not, it is just how I
used to start a program in BASIC 
25 years ago.or so.

What you didn't see, was the following line which I had enable in the
Conventions area and 
neglected to conscientiously reveal:

from __future__ import division

With that line enabled, my divisions are floating point, rather than
integer, and using the same 
years you did, my program yields the following results:

>>> 
This program finds all leap years between two dates.


Enter  for beginning year : 1995
Enter  for ending year : 2000


1996 -- leap year!
2000 -- leap year!
Done!
>>> 

I admit, I have never seen the "%" operator before. I should probably
learn to 
use it.but I had felt this overpowering need wash over me to get it
done and 
see it happen without spending more time researching. It was one of
those 
primal things where you have this need to do more than you can. So,
instead,
I was using the  "t1 == int(t1)" type compare, that I learned in BASIC
about 
25 years ago, to discover a remainder or no. My blond brain is currently
thinking 
that worksbut the day is young. And maybe "%" is faster?

Have a great day!

 Forwarded Message 
From: bhaaluu <[EMAIL PROTECTED]>
To: Terry <[EMAIL PROTECTED]>, Tutor@python.org
Subject: Re: [Tutor] n.isalnum() is failing
Date: Tue, 3 Jul 2007 05:06:20 -0400


Greetings,

Perhaps the first thing you should do, before checking
whether the user entered '' instead of '2000' is to
get the leap year function working properly.

Definition:
Leap years occur according to the following formula:
   a leap year is divisible by four,
   but not by one hundred,
   unless it is divisible by four hundred.

I find that definition to be a wee bit misleading.
This might help:

A leap year is divisible by four, with a REMAINDER of zero.
The remainder of a division is found in python with the "%" operator.
So you might want to check and see IF the remainders are zero or one,
TRUE or FALSE.

When I run your program, entering two four-digit years,
this is the output:

This program finds all leap years between two dates.

Enter  for beginning year : 1995
Enter  for ending year : 2000

1995 -- leap year!
1996 -- leap year!
1997 -- leap year!
1998 -- leap year!
1999 -- leap year!
2000 -- leap year!
Done!

Since we know that ALL of those years can't be leap years,
it means that your leap() function isn't doing something right?
I suggest that you get leap() working properly first, then tackle
the other problem.

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


Re: [Tutor] how long?

2007-07-03 Thread Alan Gauld
"Carroll, Barry" <[EMAIL PROTECTED]> wrote 

> If you have trouble coming up with an idea for a project,  check
> python.org for existing projects in need of help 

Or even try sourceforge.net.

You can search for OpenSource projects that need help and 
filter by language.

Or try UselessPython for a list of programming challenges - some 
of which have not been done yet.

Or try taking the Python Challenge adventure game - each 
step involves solving a problem in Python and the answer
gives you the url for the next challenge.

But best of all is simply to find a task you do often and 
automate it using Python. Maybe its working out tax returns 
or expenses or planning an itinery or a shopping list.
Or maybe you fancy creating a game? Or what about 
synchronising your web site with your PC?

Lots of things, just use your imagination and remember that 
if you can do it manually then Python can probably go at 
least part way to automate it

-- 
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] n.isalnum() is failing

2007-07-03 Thread Alan Gauld
"Terry" <[EMAIL PROTECTED]> wrote

> trapping for possible user  errors.
> I am using x.isalnum() to check that I have a number

First, alnum() checks for alpha-numeric characters so
will allow both alphabetic characters and numerics.

BUT in python its better to ask forgiveness that permission
so don't bother doing the check but catch the errors when
they occur:

try:start = int(start); end = int(end)
except: ValueError: start = None  # or whatever you prefer

> def leap():
>answer = 0
>t1 =  / 4
>if t1 == int(t1):

For integer division this will always be true.

>t2 =  / 100
>t3 =  / 400
>if t2 <> int(t2) or t3 == int(t3):

and similarly the second test will always be true.

>answer = "-- leap year!"

thus this will always be true.

> start = raw_input("Enter  for beginning year : ")
> end = raw_input("Enter  for ending year : ")

The easiest way to solve your conversion problems
is to do it at source so wrap raw_input in a call to int()
and wrap both prompts in a try/except as shown above.

try:
   start = int(raw_input)
   end = int(raw_input)
except ValueError: pass # do something with error

> if len(start) == 4 and len(end) == 4:

that way you don't need these lines but can instead
test if the numeric value is greater than some lower
limit

>if start.isalnum() == 1 and end.isalnum() == 1:
> #<--fails to detect '' as not a number
>start = int(start); end = int(end)
> #<--commits suicide here
>if start > 0 and end > start:

As you are doing it start can never be less than 1000
(unless the user enters 0001 I suppose...)

Also in Python you can use the neater style of

if 0 < start < end:

to test whether start lies between 0 and end.

>for i in range(start, end + 1):
>answer = leap(i)
>if answer != 0:

Its best not to mix return types in a function. If it is a
leap year you return a string. If it's not, you return a
number - 0. It would probably be better for the function
to return a boolean: True if it is a leap year and False
if not. That style of function is often called a predicate
and is usually named with an 'is' in front so it would
become isLeapYear() Then this test becomes the more
readable

for y in range(start, end+1):  # use y for year instead of meaningless 
i
  if isLeapYear(y):
 print i, 'is a leap year'

HTH,


-- 
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] MySQL -->Python-->XML for JSviz

2007-07-03 Thread Alan Gauld

"Picio" <[EMAIL PROTECTED]> wrote

> I know there are a lot of good tools to do this in pyhton

Yep, you could use minidom and I think ElementTree can write
XML as well as read it.

> flexibility for the future I'd like to use an Object relational 
> mapper
> like SQLAlchemy or SQLObject todo the job.

ORMs are good at translkating between objects in memory
and SQL they are not generally good at XML. You will probably
be better combining an ORM with a templating system like Kid
or Cheetah. They are designed to generate XML and fill in the
blanks with data.

> have in mind a solution to have multiple formats coming outside of 
> the
> ORM: json, csv, plain text etc...

Kid can definitely do XML, JSON. The ORM and plain Python is
probably best for plain text and similarly the csv module will handle
that format.

HTH

Alan G. 


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


Re: [Tutor] UTF-8 filenames encountered in os.walk

2007-07-03 Thread Alan Gauld

"William O'Higgins Witteman" <[EMAIL PROTECTED]> wrote

>I have several programs which traverse a Windows filesystem with 
>French
> characters in the filenames.

I suspect you need to set the Locale at the top of your file.

Do a search for locale in this lists archive where we had a
thread on this a few months ago.

HTH,

Alan G 


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


Re: [Tutor] [Fwd: Re: n.isalnum() is failing]]

2007-07-03 Thread Alan Gauld
"Terry" <[EMAIL PROTECTED]> wrote 

> I was using the  "t1 == int(t1)" type compare, that I 
> learned in BASIC about  25 years ago, to discover 
> a remainder or no.

What a pity. Even the most elementary BASIC has 
always had the MOD operator for finding remainders.
and a \ operator for integer division. Thus in GW BASIC 
(from around 1981)

PRINT 10\3 -> 3
PRINT 10 MOD 3 -> 1

> that worksbut the day is young. And maybe "%" is faster?

Probably, but not by enough that you'll notice unless you 
are testing a few centuries worth of dates!

-- 
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] MySQL -->Python-->XML for JSviz

2007-07-03 Thread Kent Johnson
Picio wrote:
> Hello all, I need some advice to choose an xml generator for jsviz a
> tool in javascript to create some wonderful graphs (SnowFlake or Force
> directed).
> 
> Starting from a SQL table (mysql) I need to create a XML file with a
> structure like this:
> 
>   
>   
>   
>   
>   
>   
> 
> 
> Where nodes attributes are the MySQL table fields (the PK will be the
> attribute).
> I know there are a lot of good tools to do this in pyhton (some maybe
> is already shipped with pyton itself), but since I need a lot of
> flexibility for the future I'd like to use an Object relational mapper
> like SQLAlchemy or SQLObject todo the job. When I say flexibility, I
> have in mind a solution to have multiple formats coming outside of the
> ORM:
> json, csv, plain text etc...

I'm not sure why you need an ORM here. ISTM you just need a database 
connection, the name of a table containing the data and a list of the 
fields you want to pull out of it. Then a simple SELECT gets you the 
data in a list of lists; pass the same list of field names and the list 
of data to whatever output formatter you want. I think the whole program 
would only be 20-30 lines of code.

ElementTree is a good XML package, it is part of Python 2.5 and 
available as an add-on for older Python versions.

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


Re: [Tutor] UTF-8 filenames encountered in os.walk

2007-07-03 Thread Kent Johnson
Alan Gauld wrote:
> "William O'Higgins Witteman" <[EMAIL PROTECTED]> wrote
> 
>> I have several programs which traverse a Windows filesystem with 
>> French
>> characters in the filenames.
> 
> I suspect you need to set the Locale at the top of your file.

Do you mean the
# -*- coding:  -*-
comment? That only affects the encoding of the source file itself.

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


Re: [Tutor] UTF-8 filenames encountered in os.walk

2007-07-03 Thread Kent Johnson
William O'Higgins Witteman wrote:
> I have several programs which traverse a Windows filesystem with French
> characters in the filenames.
> 
> I have having trouble dealing with these filenames when outputting these
> paths to an XML file - I get UnicodeDecodeError: 'ascii' codec can't
> decode byte 0xe9 ... etc.  That happens when I try to convert to UTF-8.
> 
> I know what os will give me UFT-8 if I give it UTF-8, and I am trying to
> do that, but somewhere down the line it seems like it reverts to ASCII,
> and then I get these errors.
> 
> Has anyone found a silver bullet for ensuring that all the filenames
> encountered by os.walk are treated as UTF-8?  Thanks.

Some code would help here, there are so many ways people get confused by 
UTF-8 and stumble over the subtleties of Python's use of Unicode. 
Particularly the code that gives you the error. The error you quote is a 
decode error, whereas converting to UTF-8 is encoding.

Also it would be helpful to figure out for sure what you are getting 
from os.walk() - is it UTF-8 or Unicode? The best way to find out is to
   print repr(filename)
and see what you get on output.

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


Re: [Tutor] n.isalnum() is failing

2007-07-03 Thread Terry
Alan,

Wow! Those changes radically change the program! 

Is there a way to get the try-exception to cause the end user questions 
to be repeated until acceptable answers are given? Like a kind of
"return" 
or "repeat" command that could be added to the except side of things?

try:
start = int(raw_input("Enter  for beginning year : "))
end = int(raw_input("Enter  for ending year : "))
except ValueError: print "Must be a integer number as YEAR."


I also noticed that I had to make the statement "start = end = 0" prior 
to the 'try'. To my eye, it looks to me that 'start' and 'end' are
being 
declared inside the try-exception, but evidently python does not agree.

Thanks,
Terry












On Tue, 2007-07-03 at 21:42 +0100, Alan Gauld wrote:

> "Terry" <[EMAIL PROTECTED]> wrote
> 
> > trapping for possible user  errors.
> > I am using x.isalnum() to check that I have a number
> 
> First, alnum() checks for alpha-numeric characters so
> will allow both alphabetic characters and numerics.
> 
> BUT in python its better to ask forgiveness that permission
> so don't bother doing the check but catch the errors when
> they occur:
> 
> try:start = int(start); end = int(end)
> except: ValueError: start = None  # or whatever you prefer
> 
> > def leap():
> >answer = 0
> >t1 =  / 4
> >if t1 == int(t1):
> 
> For integer division this will always be true.
> 
> >t2 =  / 100
> >t3 =  / 400
> >if t2 <> int(t2) or t3 == int(t3):
> 
> and similarly the second test will always be true.
> 
> >answer = "-- leap year!"
> 
> thus this will always be true.
> 
> > start = raw_input("Enter  for beginning year : ")
> > end = raw_input("Enter  for ending year : ")
> 
> The easiest way to solve your conversion problems
> is to do it at source so wrap raw_input in a call to int()
> and wrap both prompts in a try/except as shown above.
> 
> try:
>start = int(raw_input)
>end = int(raw_input)
> except ValueError: pass # do something with error
> 
> > if len(start) == 4 and len(end) == 4:
> 
> that way you don't need these lines but can instead
> test if the numeric value is greater than some lower
> limit
> 
> >if start.isalnum() == 1 and end.isalnum() == 1:
> > #<--fails to detect '' as not a number
> >start = int(start); end = int(end)
> > #<--commits suicide here
> >if start > 0 and end > start:
> 
> As you are doing it start can never be less than 1000
> (unless the user enters 0001 I suppose...)
> 
> Also in Python you can use the neater style of
> 
> if 0 < start < end:
> 
> to test whether start lies between 0 and end.
> 
> >for i in range(start, end + 1):
> >answer = leap(i)
> >if answer != 0:
> 
> Its best not to mix return types in a function. If it is a
> leap year you return a string. If it's not, you return a
> number - 0. It would probably be better for the function
> to return a boolean: True if it is a leap year and False
> if not. That style of function is often called a predicate
> and is usually named with an 'is' in front so it would
> become isLeapYear() Then this test becomes the more
> readable
> 
> for y in range(start, end+1):  # use y for year instead of meaningless 
> i
>   if isLeapYear(y):
>  print i, 'is a leap year'
> 
> HTH,
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] n.isalnum() is failing

2007-07-03 Thread Alan Gauld

"Terry" <[EMAIL PROTECTED]> wrote 

> Wow! Those changes radically change the program! 
> 
> Is there a way to get the try-exception to cause the 
> end user questions to be repeated until acceptable 
> answers are given? 

Put the whole blocxk inside a while loop:

start = end = None
while start == None:
   try:
 start = int(raw_input("Enter  for beginning year : "))
 end = int(raw_input("Enter  for ending year : "))
   except ValueError: 
  print "Must be a integer number as YEAR."
  start = end = None

> I also noticed that I had to make the statement "start = end = 0" 

You shouldn't need to but its better to do so.
For one thing if you don't (before we introduce the 
loop which needs start to be there) and the 
error occurs then start and end won't exist

HTH,

Alan G.

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


Re: [Tutor] UTF-8 filenames encountered in os.walk

2007-07-03 Thread Alan Gauld

"Kent Johnson" <[EMAIL PROTECTED]> wrote

>> I suspect you need to set the Locale at the top of your file.
> 
> Do you mean the
> # -*- coding:  -*-
> comment? That only affects the encoding of the source file itself.

No, I meant the Locale but I got it mixed up with the encoding
in how it is set. Oops!

Alan G.

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


[Tutor] UTF-8 title() string method

2007-07-03 Thread Jon Crump

Dear All,

I have some utf-8 unicode text with lines like this:

ANVERS-LE-HOMONT, Maine.
ANGOULÊME, Angoumois.
ANDELY (le Petit), Normandie.

which I'm using as-is in this line of code:

place.append(line.strip())

What I would prefer would be something like this:

place.append(line.title().strip())

which works for most lines, giving me, for example:

Anvers-Le-Homont, Maine.
and
Andely (Le Petit), Normandie.

but where there are diacritics involved, title() gives me:

AngoulÊMe, Angoumois.

Can anyone give the clueless a clue on how to manage such unicode strings 
more effectively?


Many thanks,
Jon
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] UTF-8 filenames encountered in os.walk

2007-07-03 Thread Terry Carroll
On Tue, 3 Jul 2007, William O'Higgins Witteman wrote:

> Has anyone found a silver bullet for ensuring that all the filenames
> encountered by os.walk are treated as UTF-8?  Thanks.

What happens if you specify the starting directory as a Unicode string, 
rather than an ascii string, e.g., if you're walking the current 
directory:
 
 for thing in os.walk(u'.'):

instead of:

 for thing in os.walk('.'): 

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


Re: [Tutor] UTF-8 title() string method

2007-07-03 Thread Terry Carroll
On Tue, 3 Jul 2007, Jon Crump wrote:

> but where there are diacritics involved, title() gives me:
>
> AngoulMe, Angoumois.
>
> Can anyone give the clueless a clue on how to manage such unicode strings 
> more effectively?

I think setting the locale is the trick:

>>> s1 = open("text.txt").readline()
>>> print s1
ANGOUL.ME, Angoumois.
>>> print s1.title()
Angoul.Me, Angoumois.
>>> import locale
>>> locale.setlocale(locale.LC_ALL,('french'))
'French_France.1252'
>>> print s1.title()
Angoul.me, Angoumois.



(sorry about the '.' for the characters that my term program won't accept)

You might have to hunt around and experiment for the right locale that 
will work in all your cases.


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


Re: [Tutor] n.isalnum() is failing

2007-07-03 Thread Terry
For anyone who was following this, here is the finished, non floating
point, program, after all the kind advice received has been incorporated
into it (hopefully I have the leap year logic right...):

# *** (7) MAIN BODY -- The Buck Starts Here! ***

def isLeapYear(y):
if y % 4 == 0:
if y % 100 == 0 and y % 400 == 1:
answer = False
return answer
answer = True
return answer
answer = False
return answer

print "This program finds all leap years between any two
dates.";print;print

start = end = None 
while start == None:
try:
start = int(raw_input("Enter  for beginning year : "))
end = int(raw_input("Enter  for ending year : "))

except ValueError:
print;print "YEAR must be a integer number -- TRY AGAIN!"
start = end = None

if 1 <= start < end:
print; print
for y in range(start, end + 1):
answer = isLeapYear(y)
if answer == True:
print y, "--leap year!"

print "Done!"


*** EXECUTION ***
>>> 
This program finds all leap years between any two dates.


Enter  for beginning year : 1900
Enter  for ending year : 2000


1900 --leap year!
1904 --leap year!
1908 --leap year!
1912 --leap year!
1916 --leap year!
1920 --leap year!
1924 --leap year!
1928 --leap year!
1932 --leap year!
1936 --leap year!
1940 --leap year!
1944 --leap year!
1948 --leap year!
1952 --leap year!
1956 --leap year!
1960 --leap year!
1964 --leap year!
1968 --leap year!
1972 --leap year!
1976 --leap year!
1980 --leap year!
1984 --leap year!
1988 --leap year!
1992 --leap year!
1996 --leap year!
2000 --leap year!
Done!
>>> 

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