[FAQTS] Python Knowledge Base Update -- July 11th, 2000
Fiona Czuczman
fiona at sitegnome.com
Tue Jul 11 07:54:16 EDT 2000
More information about the Python-list mailing list
Tue Jul 11 07:54:16 EDT 2000
- Previous message (by thread): Installing and running JPython on the AS/400
- Next message (by thread): a show stopper with pygimp-0.5
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hello All, Another instalment of edited, or newly entered, entries into http://python.faqts.com regards, Fiona Czuczman including: - module for continued fractions, useful for finding optimal fractional approximation to real numbers - How do I use Windows API in Python? With an example please. - I'm new to Python and would appreciate advice on what book(s) I should be looking at? - Is there a utility to convert module docstrings to HTML for nice documentation? - What is lambda? ## New Entries ################################################# ------------------------------------------------------------- module for continued fractions, useful for finding optimal fractional approximation to real numbers http://www.faqts.com/knowledge-base/view.phtml/aid/4649 ------------------------------------------------------------- Fiona Czuczman Huaiyu Zhu #!/usr/bin/env python # (C) 2000 Huaiyu Zhu <hzhu at users.sourceforge.net>. Licence: GPL # $Id: ContFrac.py,v 1.3 2000/07/10 05:09:11 hzhu Exp $ """ ContFrac.py - Continued fraction approximation utilities Convert between float, continued fraction and fractions. Useful for finding optimal rational approximation at any precision. Usage examples: python ContFrac.py Representation: Continued fraction: list of integers. Ordinary fraction: pair of long integers. Optional variables in the functions: nterm: max number of terms in continued fraction. prec: precition (inverse relative error) - maybe too conservative? """ def cf2real(cf): "Convert to float from continued fraction" x = 0. for i in range(1,len(cf)+1): x = 1./(cf[-i] + x) return 1./x def real2cf(x, prec=1e16, nterms=40): "Convert to continued fraction from float" cf = []; p = 1.; a = 1 for i in range(nterms): a, a0 = int(x), long(a) cf.append(a) p = p * (abs(a*a0) + 1) if p > prec or abs(x-a)<1e-12: break x = 1./(x-a) "Merge last term if it is one, so the expression is unique" n = len(cf)-1 if cf[n] == 1: cf[n-1]=cf[n-1]+1; del cf[n] return cf def cf2frc(cf, prec=1e16): "Convert to fraction from continued fraction" "Estimates term n that achieves precision - need better algorithm" p = 1.; n = 0 for n in range(1, len(cf)): p = p * (cf[n-1]*(cf[n]+.5)+1) if p >= prec: break "Conversion using first n terms" x, y = long(cf[n]), 1L for i in range(n): x, y = x*cf[n-1-i] + y, x return x, y def frc2cf(frc): "Convert to continued fraction from fraction" cf = [] x, y = frc while 1: if y == 0: break a, b = x/y, x%y cf.append(a) x, y = y, b return cf #------------------------------------------------------------------ def test(x, prec, nterms): print "Convert %.16f to continued fraction and back to float" % x cf = real2cf(x, nterms=nterms) print cf, cf2real(cf) print "Convert to fraction and back to float, limit precision: %g" %prec print "num. of terms fraction float error" for n in range(1, nterms+1): f = cf2frc(cf[:n], prec) r = f[0]/float(f[1]) print "%2d %33s % .16f %- 8.4g" % (n, f, r, r/x-1) if __name__ == "__main__": from math import pi, exp, sqrt print "------------ Effect of too much precision and terms ------------" test(-0.6667, prec=1e19, nterms=6) print "------------ Effect of limited number of terms ----------------" test(pi, prec=1e16, nterms=6) print "------------ Effect of limited of precision --------------------" test(exp(1), prec=1e4, nterms=9) # Interesting pattern in continued fraction of e. print "------------ Conversion between fraction and continued fraction" cf = real2cf(1/sqrt(2), nterms=17); print cf f = cf2frc(cf); print f cf1 = frc2cf(f); print cf1 if cf == cf1: print "Idential" else: print "Something wrong" ------------------------------------------------------------- How do I use Windows API in Python? With an example please. http://www.faqts.com/knowledge-base/view.phtml/aid/4651 ------------------------------------------------------------- Fiona Czuczman Neil Hodgson You should grab Mark Hammond's Win32 extensions from http://starship.python.net/crew/mhammond/ which includes the Pythonwin development environment. For an example, start Pythonwin, and you should an Interactive Window. Into that window type (just the bit after ">>>"): >>> import win32api this loads definitions of the core Windows API. Then call an API with >>> win32api.Beep(3000,300) As you press the "." a list of the available calls should appear. You can continue typing or select an item and press Tab to enter that item. Pressing enter after this typing will execute the code and produce a noise - hope you have a sound card. The online help lists all of the APIs wrapped by the Win32 extensions which includes a good proportion of the entire Win32 API. For complete descriptions of each API, you will want access to MSDN, either online at http://msdn.microsoft.com or by downloading the Platform SDK from the same site. There is a book by Mark and Andy called "Python Programming on Win32" which contains a lot of pages and a decent cover illustration. ## Edited Entries ############################################## ------------------------------------------------------------- I'm new to Python and would appreciate advice on what book(s) I should be looking at? http://www.faqts.com/knowledge-base/view.phtml/aid/2795 ------------------------------------------------------------- Fiona Czuczman Chris Lada, Snoopy :-)), Paul Winkler, Dana Booth, Frank V. Castellucci,richard_chamberlain,Alessandro Bottoni There a lots, Go to http://www.python.org/psa/bookstore/ for a list. The manning and oreilly books have sample chapters online you could look at. A range of books that should be useful when starting out with Python: - "Quick Python" by Harms, and McDonald. - If you already program, David Beasly's "Python Essential Reference" is very good. Another plus is that the book is high quality. It covers 1.5.2, is tight fast text with no little fluffy animal or snakes writhing through the text, just down to the bone reading. Snoopy :-)) wrote: If you are New to Programming, then In addition to the Tutorials found on Python's Home Page, you should get as the 1st. choice "Teach Yourself Python in 24hrs". BTY: "Learning Python" is also a very good book, but as a Newbie I found it quite overwhelming, to the point that I almost stopped learning Python. On the other hand I am very happy that I bought the "Teach Yourself Python in 24hrs". I find it considerable easier to comprehend the concepts, etc. You can read the book online at the following: http://www.pauahtun.org/TYPython/ Paul Winkler wrote: OTOH, I got "Learning Python" and I've found that after reading just the first half of the book and doing maybe 1/4 of the exercises, I had a very good grasp of the language, including classes and exceptions (both new ideas to me). From there the next step is to skim the second half looking for useful bits, or poke around in the library reference manual. But then, it did take more than 24 hours. :) [Alessandro Bottoni] I LOVE this book: "Learning Python" by Mark Lutz and David Ascher Published by O'Reilly ISBN 1565924649 It is concise, exhaustive, clean and well-organized. > How about a book about Python and XML? [Alessandro Bottoni] There is just one, and it is quite good: "XML processing with Python" By Sean McGrath Published by Prentice-Hall ISBN 013211192 > How about a book about Python for web site development? [Alessandro Bottoni] Unfortunately, the only existing book on this topic is out of print: "Internet Programming with Python" By Aaron Watters and James Ahlstrom Published by M&T Books ISBN 1558514848 If you are working on Windows, buy this book: "Python Programming on Win32" By Mark Hammond and Andy Robinson Published by O'Reilly ISBN 1565926218 If you have to develop GUIs, buy this book: "Python and Tkinter programming" By John Grayson Published by Manning ISBN 1884777813 ------------------------------------------------------------- Is there a utility to convert module docstrings to HTML for nice documentation? http://www.faqts.com/knowledge-base/view.phtml/aid/4610 ------------------------------------------------------------- Stu D, Fiona Czuczman Richard Jones,David Goodger >From http://starship.python.net/crew/danilo/download.html: Gendoc generates documentation from Python source code in different formats. Currently it can generate HTML, MIF, MML and plain old ascii (MIF and MML are FrameMaker formats). If you want to generate HTML files, I strongly suggest you grab a copy of Robin Friedrich's HTMLgen package, with which gendoc generates quite nice pages, optionally with frames. In addition to gendoc/HTMLgen, there are several others I know of: pythondoc (the successor to gendoc): http://starship.python.net/crew/danilo/pythondoc/ manpy: http://www.lfw.org/python/ crystal: http://www.btinternet.com/~tratt/comp/python/crystal/index.html structured text (part of Zope; used by several of these projects): http://www.zope.org/Members/millejoh/structuredText ------------------------------------------------------------- What is lambda? http://www.faqts.com/knowledge-base/view.phtml/aid/4136 ------------------------------------------------------------- Fiona Czuczman Warren Postma, david_ullrich 1) Meta-Answer: Read the FAQ and Manual sections. 2) Instant Gratification Answer Here's a standard function definition: def func(x): return x *2 Here's a way to write the same thing, in effect, but using Lambda: func = lambda (x): x * 2 While that example isn't useful, it gives you the idea. Think of lambda as a way to generate a function and return that function as an object, without having to give it a name. I primarily use it to pass an algorithm or expression as a parameter to a function, or other places where I want to pass code in a variable instead of passing a reference to a method containing that code. ------------ What it does is allow you to construct "anonymous functions". Oops, not my own words. The syntax lambda x: [expression in x] is itself an expression - the _value_ of the expression "lambda x: [expression in x]" is a function which returns [expression in x] when passed x. For example "lambda x: x+x" is a function that returns x+x; saying f = lambda x: x+x has the same effect as saying def f(x): return x + x But you don't usually use it that way - people use lambda when they want to pass a function to some routine without having to make up a name for the function. For example map takes a function as a parameter; saying print map(lambda x: x+x, [1,2,3]) is the same as saying def f(x): return x + x print map(f, [1,2,3]]) You should note there's varying opinions on whether lambda is a good thing - if you're a beginner at programming as well as with Python it's not the first thing you should worry about.
- Previous message (by thread): Installing and running JPython on the AS/400
- Next message (by thread): a show stopper with pygimp-0.5
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list