Re: [Tutor] py in a nut

2005-10-25 Thread Frank Moore
[EMAIL PROTECTED] wrote:

> Hope this is not too off topic:
> I noticed that the Python in a Nutshell book is version 2.2 and a few 
> years old.  Does anyone know if there are plans to bring out a new 
> edition anytime soon?  I checked the O'reilly page and didnt find 
> anything, also googled it.
> If anyone knows anything let me know.
> Thanks,
> Carl
>
>
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>  
>
Carl,

I mailed O'Reilly in June of this year to ask about any Python books on 
the horizon.
I particularly wanted to know if an 'Advanced Python' book was on the 
cards.
Something that included subjects like Descriptors, Generators, 
Iterators, Metaclasses, Decorators, Design patterns, etc.
And this was the response I got from them:


We do have a couple of Python books in the works.

Twisted: A Developer's Notebook - should be released in October.

Programming Python, 3E  which should be released in December.

Python in a Nutshell, 2nd Edition - scheduled to be released
in March of next year.


So there is no Advanced Python book on the horizon, which is a shame.
But as you can see, Python in a Nutshell 2nd edition is due out next March.

Cheers,
F.

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


Re: [Tutor] testing for modules?

2005-10-25 Thread Frank Moore
Shitiz Bansal wrote:

>try:
>   import 
>except:
>   
>   import 
>
>
>--- Ed Hotchkiss <[EMAIL PROTECTED]> wrote:
>
>  
>
Explicit is always better than implicit...

try:
import 
except ImportError:# Only catch import errors
   
import 

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


Re: [Tutor] py in a nut

2005-10-25 Thread Frank Moore
Kent Johnson wrote:

>Have you looked at Python Cookbook 2E? Chapter 19 is "Iterators and 
>Generators". Chapter 20 is "Descriptors, Decorators and Metaclasses".
>  
>
I have. But I'd rather have a complete book on these subjects ;-)
I'm sure someone will write one in the end. Hopefully Alex Martelli 
will. He currently runs a course on these subjects.

The reason I wrote to O'Reilly originally was because the 2nd Edition of 
the 'Advanced Perl Programming' book had just come out and I noticed how 
perl had so many books (almost 40) written about it but Python only had 
9. An advanced Python book seemed to be the one that was so obviously 
missing.

>This one is available:
>http://www.oreilly.com/catalog/twistedadn/
>  
>
I'm still waiting for O'Reilly to publish the Table of Contents so I can 
see what's in it.
As everyone else has noted, the cover is definitely one of their best.

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


Re: [Tutor] py in a nut

2005-10-25 Thread Frank Moore
Damien Gouteux wrote:

> In french, 'Python précis & concis', from O'Reilly is now out : from 
> Mark Lutz, it covers python 2.4 (and decorators).
> I assume there is a english edition of this book.


Yes, that's the Python Pocket Reference.
It does have 1 (small) page on decorators, but nothing on metaclasses, 
descriptors etc...

The Pocket Reference is useful as a reminder on how something works 
syntactically,
but it's probably not the best place to find an in-depth explanation on 
how something works in the first place.

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


Re: [Tutor] Percentage

2005-11-07 Thread Frank Moore
Johan Geldenhuys wrote:

>Hi all,
>
>What is the syntax if I want to work out what percentage 42 is out of 250?
>  
>
Johan,

You could try:

percentage = (42 * 250)/100

This gives the answer 105.

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


Re: [Tutor] Percentage

2005-11-07 Thread Frank Moore
Johan Geldenhuys wrote:

> Wow, you gave 105% on this one. ;-)
>
You're right. I misread the question and thought that you wanted 42% of 
250.
My mistake.

Cheers,
F.

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


Re: [Tutor] Introspection (modules and functions)

2005-11-23 Thread Frank Moore
Negroup - wrote:

>Hi.
>
>My application hosts a module A that contains only a (variable) number
>of functions. In another part of this application I need to know which
>functions are defined inside module A. Initially I thought to use
>__dict__, but along with the functions of module A are listed all the
>builtins too.
>  
>
Negroup,

You might want to try this:

custom_list = [x for x in A.__dict__ if not x.startswith('_')]

This should remove the builtins.

Hope this helps,
Frank.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pretty XML

2005-11-23 Thread Frank Moore
Greg Lindstrom wrote:

> Hello-
> I am in the process of creating an XML document from information 
> stored in our database.  One of my colleagues will use the record to 
> format our information (health care claims) into all sorts of forms, 
> reports, etc.  He is partial to PHP5 but I like Python and would like 
> to know if there is something that would read in my XML file and 
> format it in a similar manner to "pretty print" so I can verify the 
> correct information is being pulled.  I have looked into the XML 
> documentation and, to be honest, I am overwhelmed with the choices; 
> SAX, DOM, XPath, 4Suite, and more.  Though I've been coding full time 
> for 25 years, I'm new to XML and could use some pointers.
>
> My immediate concern is to read in an XML stream from a file and 
> format it with indentation so that I can read and verify the data 
> against out database.  My long term concern is what tool(s) do you 
> think would give the biggest return against the effort required to 
> learn them?  The claim files I am generating will be less than a meg 
> each, if that matters.
>
Greg,

Googling found the following Python/SAX XML indenter:

http://mail.python.org/pipermail/xml-sig/1999-January/000756.html

It should do what you want or at least give you a head start.

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


[Tutor] Regex across multiple lines

2006-04-26 Thread Frank Moore
Hi,

Can anyone tell me how to do a regex substitution across multiple lines 
in HTML?
I can search for the piece of HTML I want to substitute, for instance:


This is my title


using

html_text = file('some.html', 'r').read()
search_string = '.*'
p = re.compile(search_string, re.DOTALL)

But when I try and use:

replace_string = 'Another title'
html_text = re.sub(search_string, replace_string, html_text)

it doesn't work.

However, if the HTML is all on one line:

This is my title

it works fine.

Any ideas?

Many thanks,
Frank.

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


Re: [Tutor] Regex across multiple lines

2006-04-26 Thread Frank Moore
Kent Johnson wrote:

>Use your compiled regex for the sub(), so it will have the DOTALL flag set:
>html_text = p.sub(replace_string, html_text)
>  
>
Kent,

I was trying to work out how to use the DOTALL flag with the sub method, 
but couldn't figure it out.
It's so obvious once someone points it out. ;-)

Many thanks,
Frank



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


[Tutor] Unicode Encode Error

2006-04-27 Thread Frank Moore
Hi,

I'm getting the following error when I try and write some HTML with 
German text in it.

UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in 
position 1367: ordinal not in range(128)

This was my code:

  html_text = open(inFile, 'r').read()
 # ... do some processing on html_text
  file(outFile, 'w').write(html_text)

I've now read the 'Unicode - How To' by AMKuchling and changed the code to:

  html_text = codecs.open(inFile, encoding='utf-8').read()
 # ... do some processing on html_text
  f = codecs.open(outFile, encoding='utf-8', mode='w')
  f.write(html_text)

but I'm still getting the error.
Does anybody know what I'm doing wrong?

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


Re: [Tutor] Unicode Encode Error

2006-04-27 Thread Frank Moore
Kent Johnson wrote:

>Do you explicitly close the output file? If not, the data may not be 
>actually written.
>  
>
Kent,

You're right, I realised after playing with Tim's example that the 
problem was that I wasn't calling close() on the codecs file.
Adding this after the f.write(html_text) seems to flush the buffer which 
means that the content now gets written to the file.

Thanks for your help,
Frank.




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


[Tutor] HTML encoding of character sets...

2006-05-03 Thread Frank Moore
Hi,

I need to do some encoding of text that will be used in a web page.
The text has been translated into 16 different languages.
I've managed the manual translation of some of the more regular 
languages (French, Spanish, Italian etc...) , by
replacing characters like 'á' with the numeric entity á etc...
This works when you only have a few characters like this in the text and 
they visually stand out.
However, I now have to move on to other languages like Arabic, Russian, 
Chinese, Hebrew, Japanese, Korean, Hindi and Polish.
In these languages, the sheer volume of characters that need to be 
encoded is huge.
For instance, the following text is a title on a web page (in Russian), 
asking the user to wait for the page to load:

Èäåò çàãðóçêà, ïîæàëóéñòà, ïîäîæäèòå…

It obviously looks like garbage unless you have your email reader set to 
a Russian text encoding.
But even if it appears correctly, the sheer number of characters that I 
will need to numerically encode is massive.

Does anyone know how I can automate this process?
I want to be able to read a string out of a translation file, pass it to 
a Python script and get back a list or string of numeric entities
that I can then bury in my HTML.

I had a play with a snippet of code from the Unicode chapter of 'Dive 
Into Python' (http://diveintopython.org/xml_processing/unicode.html)
but get the following error:

text = open('russian.txt', 'r').read()
converted_text = text.encode('koi8-r')
Traceback (most recent call last):
File "", line 1, in ?
File "c:\Python24\lib\encodings\koi8_r.py", line 18, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc8 in position 0: 
ordinal not in range(128)

Anybody got any ideas?

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