Be careful with exec and eval in Python written on Tuesday, February 1, 2011 One of the perceived features of a dynamic programming language like Python is the ability to execute code from a string. In fact many people are under the impression that this is the main difference between something like Python and C#. That might have been true when the people compared Python to things like C. Wait what. This post was inspired by a discussion on reddit about the use of the execfile function in the web2py web framework but also applies to other projects. Disclaimer beforehand: the numbers for this post are taken from Python 2.7 on OS X. Behind the Scenes of Imports Let's start with everybody's favourite topic: Performance. it locates the module (surprise). Now first of all, none of the above steps ever passed a string to the exec keyword or function. >>> code = compile('a = 1 + 2', '<string>', 'exec')>>> exec code>>> print a3 As you can see, exec happily executes bytecode too. Why should you do that? Performance Characteristics
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.
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
Command-line syntax: some basic concepts I’ve been reading about parsers for command-line arguments lately, for example Plac. And, as Michele Simionato says: There is no want of command line arguments parsers in the Python world. The standard library alone contains three different modules: getopt (from the stone age), optparse (from Python 2.3) and argparse (from Python 2.7). My reading has made me realize that there is an immense range of possible syntaxes for command-line arguments, and far less consensus and standardization than I thought. It also has made me wonder if anyone had ever written an overview of, or introduction to, the basic concepts involved in command line arguments. Of course, there may be something out there and I just missed it. What is a command line argument? When you invoke an application from a command line, it is often useful to be able to send one or more pieces of information from the command line to the application. superedit a_filename.txt It is possible to supply more than one command line argument
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 ?
python: import a module from a folder Speed/PerformanceTips This page is devoted to various tips and tricks that help improve the performance of your Python programs. Wherever the information comes from someone else, I've tried to identify the source. Python has changed in some significant ways since I first wrote my "fast python" page in about 1996, which means that some of the orderings will have changed. I migrated it to the Python wiki in hopes others will help maintain it. You should always test these tips with your application and the specific version of the Python implementation you intend to use and not just blindly accept that one method is faster than another. Also new since this was originally written are packages like Cython, Pyrex, Psyco, Weave, Shed Skin and PyInline, which can dramatically improve your application's performance by making it easier to push performance-critical code into C or machine language. Other Versions Russian: Overview: Optimize what needs optimizing Sorting use
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?
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 online.effbot.org 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?
How To Use Linux epoll with Python Contents Introduction As of version 2.6, Python includes an API for accessing the Linux epoll library. This article uses Python 3 examples to briefly demonstrate the API. Blocking Socket Programming Examples Example 1 is a simple Python server that listens on port 8080 for an HTTP request message, prints it to the console, and sends an HTTP response message back to the client. Line 9: Create the server socket.Line 10: Permits the bind() in line 11 even if another program was recently listening on the same port. The official HOWTO has a more detailed description of socket programming with Python. Example 1 (All examples use Python 3) 1 import socket 2 3 EOL1 = b'\n\n' 4 EOL2 = b'\n\r\n' 5 response = b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n' 6 response += b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n' 7 response += b'Hello, world!' The finally statement block in lines 23-24 ensures that the listening server socket is always closed, even if an exception occurs.
3 decorator examples & other awesome things about Python - Peter Coles A decorator is a function that takes another function and returns a newer, prettier version of that function.* Here are three examples of Python decorators for people who know how to use them, but forget how to write them. If you make it past the examples, there is further discussion of some of the awesome features of Python that are utilized in the code below. Also, these specific examples are for Django views, but you can generalize it to any function by pulling out the request argument. Basic decorator: from functools import wraps def my_decorator(view_func): def _decorator(request, *args, **kwargs): # maybe do something before the view_func call response = view_func(request, *args, **kwargs) # maybe do something after the view_func call return response return wraps(view_func)(_decorator) # how to use it...def foo(request): return HttpResponse('...')foo = my_decorator(foo) # or... (the wraps function is explained below) Parameterized decorator: A class decorator: Callable objects: