I’ve added a new snippet about doing watermarks with PIL.
Monthly Archive for April, 2005
FedoraNEWS has a nice article about Smart, posted by Xanax.
FedoraForum also has a thread about it, as Ian MacGregor notified me.
Python 2.4 included support for function decorators. Decorators are callable objects that may modify a given function or class method arbitrarily when they’re prepended with specific syntax to the function/method definition.
The following code implements a decorator that will automatically include the arguments accepted by a given function in its own documentation:
import inspect
def argspec(object):
spec = inspect.formatargspec(*inspect.getargspec(object))
if object.__doc__:
object.__doc__ = "%s%s\n\n%s" % (object.__name__, spec, object.__doc__)
else:
object.__doc__ = "%s%s" % (object.__name__, spec)
return object
It may be used as follows:
>>> @argspec ... def test(a,b=1,*c,**d): ... """My test function""" ... >>> print test.__doc__ test(a, b=1, *c, **d) My test function
Notice that this is interesting mostly for hacking value. Most softwares that generate documentation for Python code, like the standard pydoc module, already take care of building and showing that information.
University of Massachusetts has a very nice Problem of the Week service where they post a new math related problem every monday. At that time they report the percentage of people that correctly answered the question as well. To submit an answer, check the rules page. They’re usually very quick at answering if a given solution is correct or not.
This week’s problem is not specially challenging though. The problem name is Turn the page, and consists of the following question:
A novel has 527 pages (pages 1 – 527). How many digits will it take to number all 527 pages from 1 to 527?
How many people wouldn’t know how to solve that? Solving with eyes closed is kind of fun, but with help from a calculator or from any programming language it’s trivial. In python, that single line does the job:
sum([len(str(i)) for i in range(1,527+1)])
Let’s hope for a more interesting problem next week. There are some good problems from previous weeks available as well.
Petals Around the Rose is an interesting brain teaser. Here is a copy of the game description:
The name of the game is Petals Around the Rose. The name of the game is important. The computer will roll five dice and ask you to guess the score for the roll. The score will always be zero or an even number. Your mission is to work out how the computer calculates the score and become a Potentate of the Rose.
Simple and challenging.
Here’s a snippet to compute all combinations of N elements over K positions.
Another snippet to compute the permutations of all subsets.