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

2007-07-04 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


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

2007-07-04 Thread Alan Gauld
Terry [EMAIL PROTECTED] wrote

 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

Not quite. y%400 == 1 will only be true for years
like 2001, 1601 etc!
You need to invert the test so y % 400 != 0.  (or use not)
But there is a more direct way:

Lets recap:

1) A year that is divisible by 4 is a leap year. (Y % 4) == 0
2) but: a year that is divisible by 100 is not a leap year. (Y % 100) 
!= 0 3)
3) however a year that is divisible by 400 is a leap year. (Y % 400) 
== 0

So a year that is divisible by 400 is *always* a leap year:

   if y % 400 == 0: return True

Now we need a combined test for the other 2 cases.
If its divisible by 4 and not by 100 its a leap year:

if (y % 4 == 0) and not (y %100 == 0): return True

So we can combine the definitions to give:

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

Which is shorter and clearer IMHO.
(You could combine it into one line but I personally
think that would make the rules less clear.)

This is one of the cases where using multiple returns
makes the code clearer in my view.

 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!

Also the point of a predicate function is that it returns
a boolean value so you don;t need any intermediate
variables. You can just use it in the condition, like

if isLeapYear(y):
   print y, '--leap year'

By using a predicate style name (isX) the code
becomes clearer than when you introduce an intermediate
variable where the reader then has to backtrack to see
what the test value is based on.

 1900 --leap year!

This is wrong, 1900 is divisible by 100 but not by 400...

 1904 --leap year!
 1908 --leap year!
 ...
 2000 --leap year!

Whereas 2000 is correct because it is divisible by 400.

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


[Tutor] Calculating Deflection angles

2007-07-04 Thread nitin chandra
Hello Every One,

I create a list of coordinates of 2 lines.
X11,Y11  X12,Y12 as starting and ending of one line.
X21,Y21  X22, Y22 as starting and ending of the 2nd line.

Now is there any way to calculate the deflection angle between the two
lines? Will any modules be required other than Python 2.3 (on Linux)?

and

How will it be done?

TIA,

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


[Tutor] Basic english

2007-07-04 Thread Lucio Arteaga
I just finish to right a script to  teach ESL.  I will be using as vocabulary 
Basic English . This was created in 1930 an consist of only 800 words . This 
script is very small just two kb.  plus the Basic vocabulary. 
Is anyone there to help me to compile this file into win32 so could be used in 
PC without having to install python in each PC using it. This will be possible, 
i.e to install Python in the PC used by Helping Hands, a church group  that is 
going to sponsor the ESL classess.
Thank you

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


Re: [Tutor] Basic english

2007-07-04 Thread Alan Gauld
Lucio Arteaga [EMAIL PROTECTED] wrote 

 Is anyone there to help me to compile this file into win32 
 so could be used in PC without having to install python 
 in each PC using it. 

Python is an interpreted language so you always need to 
install an interpreter, the only question is whether you do 
this explicitly, once only, or whether you hide the interpreter 
inside a pseudo exe file and thus install a version of Python 
for every program.

If you feel you must do the latter then look at py2exe which 
will create the necesary bundle for you. If you just want to 
install Python alongside your program almost any windows 
installer can do that for you.

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-04 Thread William O'Higgins Witteman
On Tue, Jul 03, 2007 at 06:04:16PM -0700, Terry Carroll 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('.'): 

This is a good thought, and the crux of the problem.  I pull the
starting directories from an XML file which is UTF-8, but by the time it
hits my program, because there are no extended characters in the
starting path, os.walk assumes ascii.  So, I recast the string as UTF-8,
and I get UTF-8 output.  The problem happens further down the line.

I get a list of paths from the results of os.walk, all in UTF-8, but not
identified as such.  If I just pass my list to other parts of the
program it seems to assume either ascii or UTF-8, based on the
individual list elements.  If I try to cast the whole list as UTF-8, I
get an exception because it is assuming ascii and receiving UTF-8 for
some list elements.

I suspect that my program will have to make sure to recast all
equivalent-to-ascii strings as UTF-8 while leaving the ones that are
already extended alone.
-- 

yours,

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


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

2007-07-04 Thread Kent Johnson
William O'Higgins Witteman wrote:
 for thing in os.walk(u'.'):

 instead of:

 for thing in os.walk('.'): 
 
 This is a good thought, and the crux of the problem.  I pull the
 starting directories from an XML file which is UTF-8, but by the time it
 hits my program, because there are no extended characters in the
 starting path, os.walk assumes ascii.  So, I recast the string as UTF-8,
 and I get UTF-8 output.  The problem happens further down the line.
 
 I get a list of paths from the results of os.walk, all in UTF-8, but not
 identified as such.  If I just pass my list to other parts of the
 program it seems to assume either ascii or UTF-8, based on the
 individual list elements.  If I try to cast the whole list as UTF-8, I
 get an exception because it is assuming ascii and receiving UTF-8 for
 some list elements.

FWIW, I'm pretty sure you are confusing Unicode strings and UTF-8
strings, they are not the same thing. A Unicode string uses 16 bits to
represent each character. It is a distinct data type from a 'regular'
string. Regular Python strings are byte strings with an implicit
encoding. One possible encoding is UTF-8 which uses one or more bytes to
represent each character.

Some good reading on Unicode and utf-8:
http://www.joelonsoftware.com/articles/Unicode.html
http://effbot.org/zone/unicode-objects.htm

If you pass a unicode string (not utf-8) to os.walk(), the resulting 
lists will also be unicode.

Again, it would be helpful to see the code that is getting the error.

 I suspect that my program will have to make sure to recast all
 equivalent-to-ascii strings as UTF-8 while leaving the ones that are
 already extended alone.

It is nonsense to talk about 'recasting' an ascii string as UTF-8; an 
ascii string is *already* UTF-8 because the representation of the 
characters is identical. OTOH it makes sense to talk about converting an 
ascii string to a unicode string.

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


Re: [Tutor] Calculating Deflection angles

2007-07-04 Thread Terry Carroll
On Wed, 4 Jul 2007, nitin chandra wrote:

 Hello Every One,
 
 I create a list of coordinates of 2 lines.
 X11,Y11  X12,Y12 as starting and ending of one line.
 X21,Y21  X22, Y22 as starting and ending of the 2nd line.
 
 Now is there any way to calculate the deflection angle between the two
 lines? Will any modules be required other than Python 2.3 (on Linux)?

I'm looking at my old book A Programmer's Geometry now.

If you first put the equations for each line in the AX+BY+C=0 form, i.e.,

 Line 1:  A1*X + B1*Y + C1 = 0
 Line 2:  A2*X + B2*Y + C2 = 0

Then the angle between them is found by:

theta = acos(
 (A1*A2 + B1*B2) 
   /
   sqrt((A1**2+B1**2)*(A2**2+B2**2))
   )

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


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

2007-07-04 Thread William O'Higgins Witteman
On Wed, Jul 04, 2007 at 11:28:53AM -0400, Kent Johnson wrote:

FWIW, I'm pretty sure you are confusing Unicode strings and UTF-8
strings, they are not the same thing. A Unicode string uses 16 bits to
represent each character. It is a distinct data type from a 'regular'
string. Regular Python strings are byte strings with an implicit
encoding. One possible encoding is UTF-8 which uses one or more bytes to
represent each character.

Some good reading on Unicode and utf-8:
http://www.joelonsoftware.com/articles/Unicode.html
http://effbot.org/zone/unicode-objects.htm

The problem is that the Windows filesystem uses UTF-8 as the encoding
for filenames, but os doesn't seem to have a UTF-8 mode, just an ascii
mode and a Unicode mode.

If you pass a unicode string (not utf-8) to os.walk(), the resulting 
lists will also be unicode.

Again, it would be helpful to see the code that is getting the error.

The code is quite complex for not-relevant-to-this-problem reasons.  The
gist is that I walk the FS, get filenames, some of which get written to
an XML file.  If I leave the output alone I get errors on reading the
XML file.  If I try to change the output so that it is all Unicode, I
get errors because my UTF-8 data sometimes looks like ascii, and I don't
see a UTF-8-to-Unicode converter in the docs.

I suspect that my program will have to make sure to recast all
equivalent-to-ascii strings as UTF-8 while leaving the ones that are
already extended alone.

It is nonsense to talk about 'recasting' an ascii string as UTF-8; an 
ascii string is *already* UTF-8 because the representation of the 
characters is identical. OTOH it makes sense to talk about converting an 
ascii string to a unicode string.

Then what does mystring.encode(UTF-8) do?
-- 

yours,

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


[Tutor] rotation within arrays

2007-07-04 Thread Andy Cheesman
Dear People,

I wondering if any of you lovely people can make a suggestion on a
problem which I have with a n dimensional array.
For example, I've a 3x3 array and I have been mapping an element from 1D
to the one directly above it. 3-12

0 1 2
3 4 5
6 7 8

 9 10 11
12 13 14
15 16 17

The problem which I have is that I now need to rotated alternative layer
of the arrays but I still need to have the original mapping i.e 3 - 12.

0 1 2
3 4 5
6 7 8

11 14 17
10 13 16
 9 12 15

Does anyone have any suggestions for how to do this?

Thanks

Andy

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


[Tutor] Help search files

2007-07-04 Thread Alejandro Decchi

Hello Someone can help me how to search file in a directory. I need to do a
form where the user write the word to search and if the file was found the
user must could download the file making click in the link
Sorry my english
thz
Alex
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2007-07-04 Thread Lloyd Kvam
On Wed, 2007-07-04 at 12:00 -0400, William O'Higgins Witteman wrote:
 On Wed, Jul 04, 2007 at 11:28:53AM -0400, Kent Johnson wrote:
 
 FWIW, I'm pretty sure you are confusing Unicode strings and UTF-8
 strings, they are not the same thing. A Unicode string uses 16 bits to
 represent each character. It is a distinct data type from a 'regular'
 string. Regular Python strings are byte strings with an implicit
 encoding. One possible encoding is UTF-8 which uses one or more bytes to
 represent each character.
 
 Some good reading on Unicode and utf-8:
 http://www.joelonsoftware.com/articles/Unicode.html
 http://effbot.org/zone/unicode-objects.htm
 
 The problem is that the Windows filesystem uses UTF-8 as the encoding
 for filenames, but os doesn't seem to have a UTF-8 mode, just an ascii
 mode and a Unicode mode.

Are you converting your utf-8 strings to unicode?

unicode_file_name = utf8_file_name.decode('UTF-8')

 If you pass a unicode string (not utf-8) to os.walk(), the resulting 
 lists will also be unicode.
 
 Again, it would be helpful to see the code that is getting the error.
 
 The code is quite complex for not-relevant-to-this-problem reasons.  The
 gist is that I walk the FS, get filenames, some of which get written to
 an XML file.  If I leave the output alone I get errors on reading the
 XML file.  If I try to change the output so that it is all Unicode, I
 get errors because my UTF-8 data sometimes looks like ascii, and I don't
 see a UTF-8-to-Unicode converter in the docs.
 

It is probably worth the effort to put together a simpler piece of code
that can illustrate the problem.

 I suspect that my program will have to make sure to recast all
 equivalent-to-ascii strings as UTF-8 while leaving the ones that are
 already extended alone.
 
 It is nonsense to talk about 'recasting' an ascii string as UTF-8; an 
 ascii string is *already* UTF-8 because the representation of the 
 characters is identical. OTOH it makes sense to talk about converting an 
 ascii string to a unicode string.
 
 Then what does mystring.encode(UTF-8) do?

It uses utf8 encoding rules to convert mystring FROM unicode to a
string.  If mystring is *NOT* unicode but simply a string, it appears to
do a round trip decode and encode of the string.  This allows you to
find encoding errors, but if there are no errors the result is the same
as what you started with.

The data in a file (streams of bytes) are encoded to represent unicode
characters.  The stream must be decoded to recover the underlying
unicode.  The unicode must be encoded when written to files.  utf-8 is
just one of many possible encoding schemes.

-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] Help search files

2007-07-04 Thread Alan Gauld

Alejandro Decchi [EMAIL PROTECTED] wrote

 form where the user write the word to search 
 and if the file was found 

Do you mean the word is the filename (use glob module)
or the word is inside the file (use os.walk)?

Amnd do you need an exact match or a wild card search.
The latter will use either glob or regular expressions(re module)

 user must could download the file making click in the link

You can use the ftlib module for that.

HTH,

Alan G

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


Re: [Tutor] rotation within arrays

2007-07-04 Thread Alan Gauld

Andy Cheesman [EMAIL PROTECTED]

 The problem which I have is that I now need to rotated alternative 
 layer
 of the arrays but I still need to have the original mapping i.e 3 - 
 12.

Is your problem how to rotate the array?
Or how to preserve the mapping?
or both?

I don't know offhand how to do either but I'm not sure
which I should be thinking about first!

Alan G 


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


Re: [Tutor] Help search files

2007-07-04 Thread Alejandro Decchi

The user put the word or the regular expresion in a textbox i need to do
when ther user press the buttom submit call the file for example search.py .
I need to do this search.py file to find the file looking for ther user. If
the file or files are found i want to the user can download the files found
making click in the link listed in ther form

On 7/4/07, Alan Gauld [EMAIL PROTECTED] wrote:



Alejandro Decchi [EMAIL PROTECTED] wrote

 form where the user write the word to search
 and if the file was found

Do you mean the word is the filename (use glob module)
or the word is inside the file (use os.walk)?

Amnd do you need an exact match or a wild card search.
The latter will use either glob or regular expressions(re module)

 user must could download the file making click in the link

You can use the ftlib module for that.

HTH,

Alan G

___
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] Help search files

2007-07-04 Thread Alan Gauld

Alejandro Decchi [EMAIL PROTECTED] wrote

 The user put the word or the regular expresion in a textbox i need 
 to do
 when ther user press the buttom submit call the file for example 
 search.py .
 I need to do this search.py file to find the file looking for ther 
 user.

Is the word the user enters the name of the file to be found?
Or is it a word that we must search for inside the files?


Alan G 


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


Re: [Tutor] Help search files

2007-07-04 Thread Alejandro Decchi

Is the word or part of the word that the user enters in the texbox .And this
word is the name of the file to be found



On 7/4/07, Alan Gauld [EMAIL PROTECTED] wrote:



Alejandro Decchi [EMAIL PROTECTED] wrote

 The user put the word or the regular expresion in a textbox i need
 to do
 when ther user press the buttom submit call the file for example
 search.py .
 I need to do this search.py file to find the file looking for ther
 user.

Is the word the user enters the name of the file to be found?
Or is it a word that we must search for inside the files?


Alan G


___
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] UTF-8 filenames encountered in os.walk

2007-07-04 Thread Terry Carroll
On Wed, 4 Jul 2007, William O'Higgins Witteman wrote:

 It is nonsense to talk about 'recasting' an ascii string as UTF-8; an 
 ascii string is *already* UTF-8 because the representation of the 
 characters is identical. OTOH it makes sense to talk about converting an 
 ascii string to a unicode string.
 
 Then what does mystring.encode(UTF-8) do?

I'm pretty iffy on this stuff myself, but as I see it, you basically have 
three kinds of things here.

First, an ascii string:

  s = 'abc'

In hex, this is 616263; 61 for 'a'; 62 for 'b', 63 for 'c'.

Second, a unicode string:

  u = u'abc' 

I can't say what this is in hex because that's not meaningful.  A 
Unicode character is a code point, which can be represented in a variety 
of ways, depending on the encoding used.  So, moving on

Finally, you can have a sequence of bytes, which are stored in a string as 
a buffer, that shows the particular encoding of a particular string:

  e8 = s.encode(UTF-8)
  e16 = s.encode(UTF-16) 

Now, e8 and e16 are each strings (of bytes), the content of which tells
you how the string of characters that was encoded is represented in that 
particular encoding.

In hex, these look like this.

  e8: 616263 (61 for 'a'; 62 for 'b', 63 for 'c')
  e16: FFFE6100 62006300
 (FFEE for the BOM, 6100 for 'a', 6200 for 'b', 6300 for 'c')

Now, superficially, s and e8 are equal, because for plain old ascii 
characters (which is all I've used in this example), UTF-8 is equivalent 
to ascii.  And they compare the same:

 s == e8
True

But that's not true of the UTF-16:

 s == e16
False
 e8 == e16
False

So (and I'm open to correction on this), I think of the encode() method as 
returning a string of bytes that represents the particular encoding of a 
string value -- and it can't be used as the string value itself.

But you can get that string value back (assuming all the characters map 
to ascii):

 s8 = e8.decode(UTF-8)
 s16 = e16.decode(UTF-16)
 s == s8 == s16
True



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


Re: [Tutor] Help search files

2007-07-04 Thread Alan Gauld

Alejandro Decchi [EMAIL PROTECTED] wrote

 Is the word or part of the word that the user enters in the texbox .
 And this word is the name of the file to be found

Ok, In that case use the glob function in the glob module.
It returns a list of all files that match a pattern:

 import glob
 files = glob.glob(*.txt)
 print files

For more powerful searches you can use os.walk()

See the OS topic in my tutorial for more examples of
using glob and os.walk

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] UTF-8 filenames encountered in os.walk

2007-07-04 Thread Kent Johnson
William O'Higgins Witteman wrote:

 The problem is that the Windows filesystem uses UTF-8 as the encoding
 for filenames,

That's not what I get. For example, I made a file called Tést.txt and 
looked at what os.listdir() gives me. (os.listdir() is what os.walk() 
uses to get the file and directory names.) If I pass a byte string as 
the directory name, I get byte strings back, not in utf-8, but 
apparently in cp1252 (or latin-1, but this is Windows so it's probably 
cp1252):
  os.listdir('C:\Documents and Settings')
['Administrator', 'All Users', 'Default User', 'LocalService', 
'NetworkService', 'T\xe9st.txt']

Note the \xe9 which is the cp1252 representation of é.

If I give the directory as a unicode string, the results are all unicode 
strings as well:
  os.listdir(u'C:\Documents and Settings')
[u'Administrator', u'All Users', u'Default User', u'LocalService', 
u'NetworkService', u'T\xe9st.txt']

In neither case does it give me utf-8.

  but os doesn't seem to have a UTF-8 mode, just an ascii
  mode and a Unicode mode.

It has a unicode string mode and a byte string mode.

 The code is quite complex for not-relevant-to-this-problem reasons.  The
 gist is that I walk the FS, get filenames, some of which get written to
 an XML file.  If I leave the output alone I get errors on reading the
 XML file.  

What kind of errors? Be specific! Show the code that generates the error.

I'll hazard a guess that you are writing the cp1252 characters to the 
XML file but not specifying the charset of the file, or specifying it as 
utf-8, and the reader croaks on the cp1252.

  If I try to change the output so that it is all Unicode, I
  get errors because my UTF-8 data sometimes looks like ascii,

How do you change the output? What do you mean, the utf-8 data looks 
like ascii? Ascii data *is* utf-8, they should look the same.

  I don't
  see a UTF-8-to-Unicode converter in the docs.

If s is a byte string containing utf-8, then s.decode('utf-8') is the 
equivalent unicode string.

 I suspect that my program will have to make sure to recast all
 equivalent-to-ascii strings as UTF-8 while leaving the ones that are
 already extended alone.
 It is nonsense to talk about 'recasting' an ascii string as UTF-8; an 
 ascii string is *already* UTF-8 because the representation of the 
 characters is identical. OTOH it makes sense to talk about converting an 
 ascii string to a unicode string.
 
 Then what does mystring.encode(UTF-8) do?

It depends on what mystring is. If it is a unicode string, it converts 
it to a plain (byte) string containing the utf-8 representation of 
mystring. For example,
In [8]: s=u'\xe9'  # Note the leading u - this is a unicode string
In [9]: s.encode('utf-8')
Out[9]: '\xc3\xa9'


If mystring is a string, it is converted to a unicode string using the 
default encoding (ascii unless you have changed it), then that string is 
converted to utf-8. This can work out two ways:
- if mystring originally contained only ascii characters, the result is 
identical to the original:
In [1]: s='abc'
In [2]: s.encode('utf-8')
Out[2]: 'abc'
In [4]: s.encode('utf-8') == s
Out[4]: True

- if mystring contains non-ascii characters, then the implicit *decode* 
using the ascii codec will fail with an exception:
In [5]: s = '\303\251'
In [6]: s.encode('utf-8')

Traceback (most recent call last):
   File ipython console, line 1, in module
type 'exceptions.UnicodeDecodeError': 'ascii' codec can't decode byte 
0xc3 in position 0: ordinal not in range(128)

Note this is exactly the same error you would get if you explicitly 
tried to convert to unicode using the ascii codec, because that is what 
is happening under the hood:

In [11]: s.decode('ascii')

Traceback (most recent call last):
   File ipython console, line 1, in module
type 'exceptions.UnicodeDecodeError': 'ascii' codec can't decode byte 
0xc3 in position 0: ordinal not in range(128)

Again, it would really help if you would
- show some code
- show some data
- learn more about unicode, utf-8, character encodings and python strings.

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


Re: [Tutor] Help search files

2007-07-04 Thread Alejandro Decchi

perfect but can you give me a link to find a file in a directory.Because i
do not the function to search files in some directory. Can you give an
example :

On 7/4/07, Alan Gauld [EMAIL PROTECTED] wrote:



Alejandro Decchi [EMAIL PROTECTED] wrote

 Is the word or part of the word that the user enters in the texbox .
 And this word is the name of the file to be found

Ok, In that case use the glob function in the glob module.
It returns a list of all files that match a pattern:

 import glob
 files = glob.glob(*.txt)
 print files

For more powerful searches you can use os.walk()

See the OS topic in my tutorial for more examples of
using glob and os.walk

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

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


Re: [Tutor] Help search files

2007-07-04 Thread Alan Gauld
Just set the current directory to the one you want to search using

os.chdir(myPath)

or pass a full path to glob:

glob.glob(/some/path/to/search/*.txt)

HTH,

Alan G.

Alejandro Decchi [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 perfect but can you give me a link to find a file in a 
 directory.Because i
 do not the function to search files in some directory. Can you give 
 an
 example :

 On 7/4/07, Alan Gauld [EMAIL PROTECTED] wrote:


 Alejandro Decchi [EMAIL PROTECTED] wrote

  Is the word or part of the word that the user enters in the 
  texbox .
  And this word is the name of the file to be found

 Ok, In that case use the glob function in the glob module.
 It returns a list of all files that match a pattern:

  import glob
  files = glob.glob(*.txt)
  print files

 For more powerful searches you can use os.walk()

 See the OS topic in my tutorial for more examples of
 using glob and os.walk

 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







 ___
 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] Calculating Deflection angles

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

Well, first that sounds somehow like a homework assignment, so only some
hints:

a) work out the underlying math. Consider all special edge cases.
b) I'm not completely sure what you need, but a feeling in the stomach
tells me that you might need the atan function, it's provided in Python
in the math module.

Andreas

nitin chandra wrote:
 Hello Every One,
 
 I create a list of coordinates of 2 lines.
 X11,Y11  X12,Y12 as starting and ending of one line.
 X21,Y21  X22, Y22 as starting and ending of the 2nd line.
 
 Now is there any way to calculate the deflection angle between the two
 lines? Will any modules be required other than Python 2.3 (on Linux)?
 
 and
 
 How will it be done?
 
 TIA,
 
 Nitin
 ___
 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

iD8DBQFGi+h4HJdudm4KnO0RAgaTAKDV9dRcoFRuFpU0l0uNRrVmmUGZvACgm2B8
ute28hDtZfeMQGg+0QoF7Mo=
=kBWC
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how long?

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

You forgot the uncertainty of 1000% :)

Actually, Python is relativly easy to learn, but I've known people that
were trained by the Arbeitsamt (government employment office), which
never, even after months could predict the output of programs like:

for x in xrange(3):
for y in xrange(3):
print x, y, x * y

Guess one needs a certain level of abstract thinking to be a programmer ;)

Andreas

Thorsten Kampe wrote:
 * 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
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGi+lcHJdudm4KnO0RAhiHAKCgEGezriWG5kigHBut8FnuEB9F7QCdE+X/
0AEw/jigdaEtbXNYVSMH7OI=
=UBkm
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] rotation within arrays

2007-07-04 Thread Bob Gailer
Andy Cheesman wrote:
 Dear People,

 I wondering if any of you lovely people can make a suggestion on a
 problem which I have with a n dimensional array.
 For example, I've a 3x3 array 
What mechanism (module?) are you using to store the array? Or are you 
asking us for a recommendation?
 and I have been mapping an element from 1D
 to the one directly above it. 3-12

 0 1 2
 3 4 5
 6 7 8

  9 10 11
 12 13 14
 15 16 17

 The problem which I have is that I now need to rotated alternative layer
 of the arrays 
arrays? I only see the 2nd as rotated. And do you mean transpose? 
That is what the result looks like. What prevents you from keeping the 
original for the mapping and also having a transposed copy?
 but I still need to have the original mapping i.e 3 - 12.

 0 1 2
 3 4 5
 6 7 8

 11 14 17
 10 13 16
  9 12 15
   
The mapping can be defined in another array (I'm using origin 1 indexing 
here):
1,1 1,2 1,3
2,1 2,2 2,3
3,1 3,2 3,3
Then you'd transpose that when you transpose the other array.



-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC


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


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

2007-07-04 Thread Kent Johnson
Terry Carroll wrote:
 I'm pretty iffy on this stuff myself, but as I see it, you basically have 
 three kinds of things here.
 
 First, an ascii string:
 
   s = 'abc'
 
 In hex, this is 616263; 61 for 'a'; 62 for 'b', 63 for 'c'.
 
 Second, a unicode string:
 
   u = u'abc' 
 
 I can't say what this is in hex because that's not meaningful.  A 
 Unicode character is a code point, which can be represented in a variety 
 of ways, depending on the encoding used.  So, moving on
 
 Finally, you can have a sequence of bytes, which are stored in a string as 
 a buffer, that shows the particular encoding of a particular string:
 
   e8 = s.encode(UTF-8)
   e16 = s.encode(UTF-16) 
 
 Now, e8 and e16 are each strings (of bytes), the content of which tells
 you how the string of characters that was encoded is represented in that 
 particular encoding.

I would say that there are two kinds of strings, byte strings and 
unicode strings. Byte strings have an implicit encoding. If the contents 
of the byte string are all ascii characters, you can generally get away 
with ignoring that they are in an encoding, because most of the common 
8-bit character encodings include plain ascii as a subset (all the 
latin-x encodings, all the Windows cp12xx encodings, and utf-8 all have 
ascii as a subset), so an ascii string can be interpreted as any of 
those encodings without error. As soon as you get away from ascii, you 
have to be aware of the encoding of the string.

encode() really wants a unicode string not a byte string. If you call 
encode() on a byte string, the string is first converted to unicode 
using the default encoding (usually ascii), then converted with the 
given encoding.
 
 In hex, these look like this.
 
   e8: 616263 (61 for 'a'; 62 for 'b', 63 for 'c')
   e16: FFFE6100 62006300
  (FFEE for the BOM, 6100 for 'a', 6200 for 'b', 6300 for 'c')
 
 Now, superficially, s and e8 are equal, because for plain old ascii 
 characters (which is all I've used in this example), UTF-8 is equivalent 
 to ascii.  And they compare the same:
 
 s == e8
 True

They are equal in every sense, I don't know why you consider this 
superficial. And if your original string was not ascii the encode() 
would fail with a UnicodeDecodeError.
 
 But that's not true of the UTF-16:
 
 s == e16
 False
 e8 == e16
 False
 
 So (and I'm open to correction on this), I think of the encode() method as 
 returning a string of bytes that represents the particular encoding of a 
 string value -- and it can't be used as the string value itself.

The idea that there is somehow some kind of string value that doesn't 
have an encoding will bring you a world of hurt as soon as you venture 
out of the realm of pure ascii. Every string is a particular encoding of 
character values. It's not any different from the string value itself.
 
 But you can get that string value back (assuming all the characters map 
 to ascii):
 
 s8 = e8.decode(UTF-8)
 s16 = e16.decode(UTF-16)
 s == s8 == s16
 True

You can get back to the ascii-encoded representation of the string. 
Though here you are hiding something - s8 and s16 are unicode strings 
while s is a byte string.

In [13]: s = 'abc'
In [14]: e8 = s.encode(UTF-8)
In [15]: e16 = s.encode(UTF-16)
In [16]: s8 = e8.decode(UTF-8)
In [17]: s16 = e16.decode(UTF-16)
In [18]: s8
Out[18]: u'abc'
In [19]: s16
Out[19]: u'abc'
In [20]: s
Out[20]: 'abc'
In [21]: type(s8) == type(s)
Out[21]: False

The way I think of it is, unicode is the pure representation of the 
string. (This is nonsense, I know, but I find it a convenient mnemonic.) 
encode() converts from the pure representation to an encoded 
representation. The encoding can be ascii, latin-1, utf-8... decode() 
converts from the coded representation back to the pure one.

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


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

2007-07-04 Thread William O'Higgins Witteman
On Wed, Jul 04, 2007 at 02:47:45PM -0400, Kent Johnson wrote:

encode() really wants a unicode string not a byte string. If you call 
encode() on a byte string, the string is first converted to unicode 
using the default encoding (usually ascii), then converted with the 
given encoding.

Aha!  That helps.  Something else that helps is that my Python code is
generating output that is received by several other tools.  Interesting
facts:

Not all .NET XML parsers (nor IE6) accept valid UTF-8 XML.
I am indeed seeing filenames in cp1252, even though the Microsoft docs
say that filenames are in UTF-8.

Filenames in Arabic are in UTF-8.

What I have to do is to check the encoding of the filename as received
by os.walk (and thus os.listdir) and convert them to Unicode, continue
to process them, and then encode them as UTF-8 for output to XML.

In trying to work around bad 3rd party tools and inconsistent data I
introduced errors in my Python code.  The problem was in treating all
filenames the same way, when they were not being created the same way by
the filesystem.

Thanks for all the help and suggestions.
-- 

yours,

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


Re: [Tutor] Help search files

2007-07-04 Thread Alejandro Decchi

Ok  but i have this form:
head
meta http-equiv=Content-Type content=text/html; charset=iso-8859-1 /
titleDocumento sin tiacute;tulo/title
/head

body
form id=form1 name=form1 method=post action=/cgi-bin/search.py
 labelstrong search/strong
 input type=text name=textfield align=center /
 /label
 pnbsp;/p
 labelstrongfind it/strong
 input type=submit name=Submit value=Enviar /
 /label
 pnbsp;/p
/form
/body
/html



And i do not how to find the file looking for the user and list the file to
can download it.Can you give me an example 

Sorry to be a newbie !!!



On 7/4/07, Alan Gauld [EMAIL PROTECTED] wrote:


Just set the current directory to the one you want to search using

os.chdir(myPath)

or pass a full path to glob:

glob.glob(/some/path/to/search/*.txt)

HTH,

Alan G.

Alejandro Decchi [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 perfect but can you give me a link to find a file in a
 directory.Because i
 do not the function to search files in some directory. Can you give
 an
 example :

 On 7/4/07, Alan Gauld [EMAIL PROTECTED] wrote:


 Alejandro Decchi [EMAIL PROTECTED] wrote

  Is the word or part of the word that the user enters in the
  texbox .
  And this word is the name of the file to be found

 Ok, In that case use the glob function in the glob module.
 It returns a list of all files that match a pattern:

  import glob
  files = glob.glob(*.txt)
  print files

 For more powerful searches you can use os.walk()

 See the OS topic in my tutorial for more examples of
 using glob and os.walk

 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








 ___
 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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2007-07-04 Thread Terry
Thanks Alan,

I managed to get totally confused by the different definitions of
calculating a leap year. The book (Core Python) 
described the exercise thus:

Modulus.  Determine whether a given year is a leap year, using the
following formula: a leap year is one that is divisible by four, but
not 
by one hundred, unless it is also divisible by four hundred.

That sounded to me like:  y % 4 ==0 and (y % 100==1 or y % 400==0)
Then, after reading it ten more time I became uncertain and decided to
find another definition and compare them.
I found U.S. Naval Observatory:

According to the Gregorian calendar, which is the civil calendar in use
today, years evenly divisible by 4 are leap years, with the exception of
centurial years that are not evenly divisible by 400.
 
Comparing the two, confused me a bit more, and I ended up ignoring the
first definition, saying to myself, all 4th years are leap years, unless
they are y % 100 ==0 AND y % 400==1. That sounded clearerbut,
evidently, not correct. Ha Ha Ha  

I liked the way you directly formed it. It was more like my old IF-THEN
statements.to the point and we're out of here.

def isLeapYear(y):
if y % 4 == 0: return True
if (y % 4 == 0) and not (y %100 == 0): return True
else: return False

I am checking out your python tutor web site at
http://www.freenetpages.co.uk
It looks like it surpasses by far other's I have checked out. Great job!

Thanks for the correction and the methodology!

Terry

 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

Not quite. y%400 == 1 will only be true for years
like 2001, 1601 etc!
You need to invert the test so y % 400 != 0.  (or use not)
But there is a more direct way:

Lets recap:

1) A year that is divisible by 4 is a leap year. (Y % 4) == 0
2) but: a year that is divisible by 100 is not a leap year. (Y % 100) 
!= 0 3)
3) however a year that is divisible by 400 is a leap year. (Y % 400) 
== 0

So a year that is divisible by 400 is *always* a leap year:

   if y % 400 == 0: return True

Now we need a combined test for the other 2 cases.
If its divisible by 4 and not by 100 its a leap year:

if (y % 4 == 0) and not (y %100 == 0): return True

So we can combine the definitions to give:

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

Which is shorter and clearer IMHO.
(You could combine it into one line but I personally
think that would make the rules less clear.)

This is one of the cases where using multiple returns
makes the code clearer in my view.

 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!

Also the point of a predicate function is that it returns
a boolean value so you don;t need any intermediate
variables. You can just use it in the condition, like

if isLeapYear(y):
   print y, '--leap year'

By using a predicate style name (isX) the code
becomes clearer than when you introduce an intermediate
variable where the reader then has to backtrack to see
what the test value is based on.

 1900 --leap year!

This is wrong, 1900 is divisible by 100 but not by 400...

 1904 --leap year!
 1908 --leap year!
 ...
 2000 --leap year!

Whereas 2000 is correct because it is divisible by 400.

HTH,

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


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

2007-07-04 Thread Jon Crump

Terry, thanks.

Sadly, I'm still missing something.

I've tried all the aliases in locale.py, most return
locale.Error: unsupported locale setting

one that doesn't is:


locale.setlocale(locale.LC_ALL, ('fr_fr'))

'fr_fr'

but if I set it thus it returns:

Angoul?äMe, Angoumois.

I'm running python 2.5 on a Mac iBook G4 osX 10.4.10, and this encoding 
stuff is terra incognita for me



On Tue, 3 Jul 2007, Terry Carroll wrote:

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.
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] UTF-8 filenames encountered in os.walk

2007-07-04 Thread Kent Johnson
William O'Higgins Witteman wrote:
 On Wed, Jul 04, 2007 at 02:47:45PM -0400, Kent Johnson wrote:
 
 encode() really wants a unicode string not a byte string. If you call 
 encode() on a byte string, the string is first converted to unicode 
 using the default encoding (usually ascii), then converted with the 
 given encoding.
 
 Aha!  That helps.  Something else that helps is that my Python code is
 generating output that is received by several other tools.  Interesting
 facts:
 
 Not all .NET XML parsers (nor IE6) accept valid UTF-8 XML.

Yikes! Are you sure it isn't a problem with your XML?

 I am indeed seeing filenames in cp1252, even though the Microsoft docs
 say that filenames are in UTF-8.
 
 Filenames in Arabic are in UTF-8.

Not on my computer (Win XP) in os.listdir(). With filenames of Tést.txt 
and ق.txt (that's \u0642, an Arabic character), os.listdir() gives me
  os.listdir('.')
['Administrator', 'All Users', 'Default User', 'LocalService', 
'NetworkService', 'T\xe9st.txt', '?.txt']
  os.listdir(u'.')
[u'Administrator', u'All Users', u'Default User', u'LocalService', 
u'NetworkService', u'T\xe9st.txt', u'\u0642.txt']

So with a byte string directory it fails, with a unicode directory it 
gives unicode, not utf-8.

 What I have to do is to check the encoding of the filename as received
 by os.walk (and thus os.listdir) and convert them to Unicode, continue
 to process them, and then encode them as UTF-8 for output to XML.

How do you do that? AFAIK there is no completely reliable way to 
determine the encoding of a byte string by looking at it; the most 
common approach is to try to find one that successfully decodes the 
string; more sophisticated variations look at the distribution of 
character codes.

Anyway if you use the Unicode file names you shouldn't have to worry 
about this.

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


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

2007-07-04 Thread Kent Johnson
Jon Crump wrote:
 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?

First, don't confuse unicode and utf-8.

Second, convert the string to unicode and then title-case it, then 
convert back to utf-8 if you need to:
In [3]: s='ANGOUL\303\212ME, Angoumois'
In [5]: s
Out[5]: 'ANGOUL\xc3\x8aME, Angoumois'
In [4]: s.title()
Out[4]: 'Angoul\xc3\x8aMe, Angoumois'
In [10]: print s.title()
AngoulÊMe, Angoumois
In [6]: u=s.decode('utf-8')
In [7]: u.title()
Out[7]: u'Angoul\xeame, Angoumois'
In [8]: print u.title()

Traceback (most recent call last):
   File ipython console, line 1, in module
type 'exceptions.UnicodeEncodeError': 'ascii' codec can't encode 
character u'\xea' in position 6: ordinal not in range(128)

Oops, print is trying to convert to a byte string with the default 
encoding, have to give it some help...

In [9]: print u.title().encode('utf-8')
Angoulême, Angoumois

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


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

2007-07-04 Thread Kent Johnson
Terry Carroll wrote:
 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.

I think your file is cp1252 not utf-8 as the OP specified...

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


[Tutor] hash

2007-07-04 Thread linda.s
what is the use of def __hash__(self)?
I can not understand the document.
any example? thanks,
Linda
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] cyclical redundancy check

2007-07-04 Thread shawn bright

Hello there all,

Does anyone know where i can find a function that does an 8 bit Cyclical
Redundancy Check.
I need it to verify data, and i need to be able to create one given an 12
byte message. Does anyone know much about doing this in python ?

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


Re: [Tutor] cyclical redundancy check

2007-07-04 Thread shawn bright

wait, sorry, thats 16 bits total, a low byte and a high byte.
If that makes more sense

thanks

On 7/4/07, shawn bright [EMAIL PROTECTED] wrote:


Hello there all,

Does anyone know where i can find a function that does an 8 bit Cyclical
Redundancy Check.
I need it to verify data, and i need to be able to create one given an 12
byte message. Does anyone know much about doing this in python ?

thanks

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


Re: [Tutor] hash

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

Simple, defining __hash__ on a class allows you to supply a hash value
for instances of your class.

[EMAIL PROTECTED]:~ cat /tmp/hash.py
class X:
def __hash__(self):
print HASH CALLED
return 123

print hash(X())
[EMAIL PROTECTED]:~ python /tmp/hash.py
HASH CALLED
123

Andreas

linda.s wrote:
 what is the use of def __hash__(self)?
 I can not understand the document.
 any example? thanks,
 Linda
 ___
 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

iD8DBQFGjB6IHJdudm4KnO0RAskJAKC4WZHo1Kv6MrHOzSWULOgMa+pDsgCgznRy
Thwd2n1BgWjW8OEDUIILHXY=
=y2W0
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help search files

2007-07-04 Thread Alan Gauld
Alejandro Decchi [EMAIL PROTECTED] wrote

 Ok  but i have this form:
 body
 form id=form1 name=form1 method=post 
 action=/cgi-bin/search.py

OK, You didn't make it clear that you meant a CGI program,
I was assuming you meant a GUI program. That adds a whole
heap of extra complexity. A lot depends on what web
mechanism/framework you are using. If we assume the
standard cgi module then you need to get the submited
data out of the request with the field_storage dictionary.
Then you need to call your search function and finally
format the results as HTML. Either offer to downoad a
particular file and have a second request start the download
or just send the file back, but that could be an unexpected result.
I'd offer the file as a link and let the user click on it to fetch it
from the server.

But web programming is a whole different ball game.
You should look up some simple examples first.

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-04 Thread Terry Carroll
On Wed, 4 Jul 2007, Kent Johnson wrote:

 Terry Carroll wrote:
  Now, superficially, s and e8 are equal, because for plain old ascii 
  characters (which is all I've used in this example), UTF-8 is equivalent 
  to ascii.  And they compare the same:
  
  s == e8
  True
 
 They are equal in every sense, I don't know why you consider this 
 superficial. And if your original string was not ascii the encode() 
 would fail with a UnicodeDecodeError.

Superficial in the sense that I was using only characters in the ascii
character set, so that the same byte encoding in UTF-8.

so: 

 'abc'.decode(UTF-8)
u'abc'

works

But UTF-8 can hold other characters, too; for example

 '\xe4\xba\xba'.decode(UTF-8)
u'\u4eba'

(Chinese character for person)

I'm just saying that UTF-8 encodes ascii characters to themselves; but 
UTF-8 is not the same as ascii.

I think we're ultimately saying the same thing; to merge both our ways of
putting it, I think, is that ascii will map to UTF-8 identically; but
UTF-8 may map back or it will raise UnicodeDecodeError.

I just didn't want to leave the impression Yeah, UTF-8  ascii, they're
the same thing.

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


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

2007-07-04 Thread Kent Johnson
Terry Carroll wrote:
 I'm just saying that UTF-8 encodes ascii characters to themselves; but 
 UTF-8 is not the same as ascii.
 
 I think we're ultimately saying the same thing; to merge both our ways of
 putting it, I think, is that ascii will map to UTF-8 identically; but
 UTF-8 may map back or it will raise UnicodeDecodeError.
 
 I just didn't want to leave the impression Yeah, UTF-8  ascii, they're
 the same thing.

I hope neither of us gave that impression! I think you are right, we 
just have different ways of thinking about it. Any ascii string is also 
a valid utf-8 string (and latin-1, and many other encodings), but the 
opposite is not true.

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


Re: [Tutor] Help search files

2007-07-04 Thread Alejandro Decchi

Yes I have a debian server runing apache. Can you give me a link ? Because i
do not how to make the search and list the files found to download

On 7/4/07, Alan Gauld [EMAIL PROTECTED] wrote:


Alejandro Decchi [EMAIL PROTECTED] wrote

 Ok  but i have this form:
 body
 form id=form1 name=form1 method=post
 action=/cgi-bin/search.py

OK, You didn't make it clear that you meant a CGI program,
I was assuming you meant a GUI program. That adds a whole
heap of extra complexity. A lot depends on what web
mechanism/framework you are using. If we assume the
standard cgi module then you need to get the submited
data out of the request with the field_storage dictionary.
Then you need to call your search function and finally
format the results as HTML. Either offer to downoad a
particular file and have a second request start the download
or just send the file back, but that could be an unexpected result.
I'd offer the file as a link and let the user click on it to fetch it
from the server.

But web programming is a whole different ball game.
You should look up some simple examples first.

Alan G.


___
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] Help! Pickle file

2007-07-04 Thread Sara Johnson
This may sound silly, but when writing a program where there is a pickle file, 
how does that get included into the entire program?  For instance;
   
  to create a new pickle file..
  
  #!/usr/bin/python
# Filename: pickling.py

import cPickle as p
#import pickle as p
   
  (snipped from python.org)
  
  
Does this fall anywhere in the program in particular?  I'm REALLY unfamiliar 
with this and I'm using Python for Dummies and Python.org. 
   
  Thanks!
  SJ

   
-
Luggage? GPS? Comic books? 
Check out fitting  gifts for grads at Yahoo! Search.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help! Pickle file

2007-07-04 Thread John Fouhy
On 05/07/07, Sara Johnson [EMAIL PROTECTED] wrote:
 This may sound silly, but when writing a program where there is a pickle
 file, how does that get included into the entire program?  For instance;

Hi Sara,

You create pickles with pickle.dump and you read them with pickle.load.

For example:

### create.py ###

import pickle

data = ['one', 'two', 'three']
outfile = open('data.pickle', 'wb')
pickle.dump(data, outfile)
outfile.close()
#

 load.py 

import pickle
infile = open('data.pickle')
data = pickle.load(infile)
print data
#

If you run the first script, it will create a pickle of the list
['one', 'two', 'three'].  You'll be able to see the file in the
directory where you ran the script; you can even look at it if you
like.  The second script will read the pickle and reconstruct the
list.

Hope this helps!

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


[Tutor] n.isalnum() is failing

2007-07-04 Thread János Juhász
Hi Terry

 According to the Gregorian calendar, which is the civil calendar in use
 today, years evenly divisible by 4 are leap years, with the exception of
 centurial years that are not evenly divisible by 400.

 def isLeapYear(y):
   if y % 4 == 0: return True
As it always return True, if y%4 == 0, there is problem with the 
exceptions
   if (y % 4 == 0) and not (y %100 == 0): return True
   else: return False


I feel that, the cleanest way to translate the definition into Boolean 
logic
is to do it backward instead of thinking on the exceptions. 

def leap_year(year):
if year%400 == 0: return True# We said these years are always leap 
year
if year%100 == 0: return False   # the exception handled already
if year%4   == 0: return True# no problem with the exceptions
return False # this it the default


ps
hungarians name format: family name, christian name
hungarian date format: year/month/day
Your logic is backward, and mine is the forward, isn't it?  ;)


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