background preloader

Charming Python: Decorators make magic easy

Charming Python: Decorators make magic easy
Doing a lot by doing very little Decorators have something in common with previous metaprogramming abstractions introduced to Python: they do not actually do anything you could not do without them. As Michele Simionato and I pointed out in earlier Charming Python installments, it was possible even in Python 1.5 to manipulate Python class creation without the "metaclass" hook. Decorators are similar in their ultimate banality. All a decorator does is modify the function or method that is defined immediately after the decorator. Listing 1. class C: def foo(cls, y): print "classmethod", cls, y foo = classmethod(foo) Though classmethod() is a built-in, there is nothing unique about it; you could also have "rolled your own" method transforming function. Listing 2. def enhanced(meth): def new(self, y): print "I am enhanced" return meth(self, y) return new class C: def bar(self, x): print "some method says:", x bar = enhanced(bar) Listing 3. Listing 4. Listing 5. Listing 6. Listing 7. Back to top Related:  Py:PatternsAdvanced TopicsPython

Python Descriptors | What Are Descriptors? Mark Summerfield shows how to use Python descriptors for powerful attribute control, including how to create attribute-validating class decorators by using new Python 2.6/3.0 features. Python descriptors have been around a long time—they were introduced way back in Python 2.2. But they're still not widely understood or used. This article shows how to create descriptors and presents three examples of use. A descriptor is a class that implements one or more of the special methods, __get__(), __set__(), and __delete__(). class MyClass: a = MyDescriptor("a") b = MyDescriptor("b") (In Python 2.x versions, we would write class MyClass(object): to make the class a new-style class.) Now we'll look at three examples that use descriptors for three completely different purposes so that you can start to see what can be achieved with descriptors.

Charming Python: Functional programming in Python, Part 1 Charming Python Making more out of your favorite scripting language David MertzPublished on March 01, 2001 We'd better start with the hardest question: "What is functional programming (FP), anyway?" Personally, I would roughly characterize functional programming as having at least several of the following characteristics. Functions are first class (objects). Advocates of functional programming argue that all these characteristic make for more rapidly developed, shorter, and less bug-prone code. Inherent Python functional capabilities Python has had most of the characteristics of FP listed above since Python 1.0. The basic elements of FP in Python are the functions map(), reduce(), and filter(), and the operator lambda. Eliminating flow control statements The first thing to think about in our elimination exercise is the fact that Python "short circuits" evaluation of Boolean expressions. Listing 1. Listing 2. Functions as first class objects Functional looping in Python Listing 3. Listing 4.

Charming Python: Distributing computing with RPyC Back in 2002, I wrote a series of articles on "distributing computing" (see Resources). At that time, RPyC did not exist, but I covered the Python tool Pyro and the library xmlrpclib. Surprisingly little has changed in the last seven years. The space RPyC steps into is very much the one already occupied by Pyro and XML-RPC. RPyC 3.0+, in a nutshell, has two modes to it: a "classic mode," which was already available prior to its version 3, and a "services mode," which was introduced in version 3. Background on distributing computing In the paradigm of stand-alone personal computing, a user's workstation contains a number of resources that are used to run an application: disk storage for programs and data; a CPU; volatile memory; a video display monitor; a keyboard and pointing device; perhaps peripheral I/O devices such as printers, scanners, sound systems, modems, game inputs, and so on. In the end, what is shared between distributed computers are sets of responsibilities. Back to top

A guide to Python Namespaces « The ByteBaker This post is part of the Powerful Python series where I talk about features of the Python language that make the programmer’s job easier. The Powerful Python page contains links to more articles as well as a list of future articles. Namespaces are a fundamental idea in Python and can be very helpful in structuring and organizing your code (especially if you have a large enough project). However, namespaces might be a somewhat difficult concept to grasp and get used to if you’re new to programming or even coming from another programming language (in my case, Java). What’s in a name? Before starting off with namespaces, you have to understand what Python means by a name. But you can also give names to things like functions: Now whenever you want to use func(), you can use f() instead. If you accessed the name var in between assignments, you’d get a number, a string and a list at different times. Modules and Namespaces go hand in hand So much for names. Importing pitfalls 1. import SomeModule

Decorators I: Introduction to Python Decorators Computing ThoughtsDecorators I: Introduction to Python Decoratorsby Bruce EckelOctober 18, 2008 Summary This amazing feature appeared in the language almost apologetically and with concern that it might not be that useful. I predict that in time it will be seen as one of the more powerful features in the language. The problem is that all the introductions to decorators that I have seen have been rather confusing, so I will try to rectify that here. (This series of articles will be incorporated into the open-source book Python 3 Patterns & Idioms). First, you need to understand that the word "decorator" was used with some trepidation, because there was concern that it would be completely confused with the Decorator pattern from the Design Patterns book. Indeed, you can use Python decorators to implement the Decorator pattern, but that's an extremely limited use of it. The macro has a long history, but most people will probably have had experience with C preprocessor macros. The output is:

(Python) Decorators | coding tiger Decorators in Python are bit confusing concept to understand. It is not a standard feature generally explained in many of the Beginners’ books on Python. But, understanding the basic idea of decorators will help us in making a very good use of it. As the rule always goes, there are certain situations where decorators are highly useful but not always. Decorators is a way of transforming functions and methods at the point where the code is declared. The point of transformation is placed just before the method’s own declaration. To give a more clear idea, we generally put some code within a function, make use of it in other functions by calling it, performing some calculations or processing within the function’s body, and finally return some result or print some output. Think of a situation where the attributes of the function get modified when the function is actually executed. Example 1: Output: Before Calling Inside Function After Calling Explanation: Example 2: Ok, what else we can do ?

Charming Python: Easy Web data collection with mechanize and Beautiful Soup Writing scripts to interact with Web sites is possible with the basic Python modules, but you don't want to if you don't have to. The modules urllib and urllib2 in Python 2.x, along with the unified urllib.* subpackages in Python 3.0, do a passable job of fetching resources at the ends of URLs. However, when you want to do any sort of moderately sophisticated interaction with the contents you find at a Web page, you really need the mechanize library (see Resources for a download link). One of the big difficulties with automating Web scraping or other simulations of user interaction with Web sites is server use of cookies to track session progress. Python's mechanize is inspired by Perl's WWW:Mechanize, which has a similar range of capabilities. A close friend of mechanize is the equally excellent library Beautiful Soup (see Resources for a download link). A real-life example I have used mechanize in several programming projects. Back to top Tools to start with The search result scraper

python: import a module from a folder faif/python-patterns Python Tutorial: decorators (functions and classes) - 2013 Decorators "Decoration is a way to specify management code for functions and classes." ... "A decorator itself is a callable that returns a callable." - Mark Lutz Function Decorators Let's start with a function in Python. To understand decorators, we need to know the full scope of capabilities of Python functions. def func(): print "func()" funcObj = func funcObj() Functions can be passed around in the same way other types of object such as strings, integers, lists, etc. def myFunction(in_function): def out_function(): psss return out_function The myFunction is indeed a decorator because by definition a decorator is a function that takes a function object as its argument, and returns a function object. If we elaborate a little bit more on the function we just defined: def myFunction(in_function): def out_function(): print "Entry: ", in_function. Then, how we invoke our decorator? Let's look at the example below. def simple_function(): pass enhanced_function = myFunction(simple_function) f(123)

Understanding Python's "with" statement Fredrik Lundh | October 2006 | Originally posted to Judging from comp.lang.python and other forums, Python 2.5’s new with statement (dead link) seems to be a bit confusing even for experienced Python programmers. As most other things in Python, the with statement is actually very simple, once you understand the problem it’s trying to solve. Consider this piece of code: set things up try: do something finally: tear things down Here, “set things up” could be opening a file, or acquiring some sort of external resource, and “tear things down” would then be closing the file, or releasing or removing the resource. If you do this a lot, it would be quite convenient if you could put the “set things up” and “tear things down” code in a library function, to make it easy to reuse. def controlled_execution(callback): set things up try: callback(thing) finally: tear things down def my_function(thing): do something controlled_execution(my_function) This wasn’t very difficult, was it?

Python Decorators In August 2009, I wrote a post titled Introduction to Python Decorators. It was an attempt to explain Python decorators in a way that I (and I hoped, others) could grok. Recently I had occasion to re-read that post. It wasn’t a pleasant experience — it was pretty clear to me that the attempt had failed. That failure — and two other things — have prompted me to try again. Matt Harrison has published an excellent e-book Guide to: Learning Python Decorators.I now have a theory about why most explanations of decorators (mine included) fail, and some ideas about how better to structure an introduction to decorators. There is an old saying to the effect that “Every stick has two ends, one by which it may be picked up, and one by which it may not.” In this post I will show you what the wrong end of the stick looks like, and point out why I think it is wrong. Most explanations of Python decorators start with an example of a function to be decorated, like this: So how should we explain decorators?

PEP 3101 -- Advanced String Formatting PEP Index> PEP 3101 -- Advanced String Formatting Abstract This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing '%' string formatting operator. Rationale Python currently provides two methods of string interpolation: - The '%' operator for strings. [1] - The string.Template module. [2] The primary scope of this PEP concerns proposals for built-in string formatting operations (in other words, methods of the built-in string type). Specification String Methods The built-in string class (and also the unicode class in 2.6) will gain a new method, 'format', which takes an arbitrary number of positional and keyword arguments: "The story of {0}, {1}, and {c}".format(a, b, c=d) Within a format string, each positional argument is identified with a number, starting from zero, so in the above example, 'a' is argument 0 and 'b' is argument 1. Format Strings Simple and Compound Field Names Format Specifiers Standard Format Specifiers

Python Imaging Library (PIL) The Python Imaging Library (PIL) adds image processing capabilities to your Python interpreter. This library supports many file formats, and provides powerful image processing and graphics capabilities. Status The current free version is PIL 1.1.7. Support Free Support: If you don't have a support contract, please send your question to the Python Image SIG mailing list. You can join the Image SIG via's subscription page, or by sending a mail to You can also ask on the Python mailing list,, or the newsgroup comp.lang.python. Downloads The following downloads are currently available: Additional downloads may be found here. For a full list of changes in this release, see this page. If the Windows installer cannot find a Python interpreter, you may have to register your interpreter. For a full list of changes in this release, see this page.