Decorator

Category: Python - Miscellaneous

This script defines a "decorator" that, rather than decorating the function, calls it, passing it a file object that it had opened as the first argument.Of course, it makes sure to close the file upon the function's return (or nonreturn) with a try...finally block. One can use this decorator to get the effect of a Lisp-like with_open_file block in Python.In Common Lisp, there is a very convenient macro called with-open-file that opens a file, binds the file object to a block-local variable, and then executes the block. The file is automatically closed when that block is exited.Not having macros in Python, we generally do this with a try...finally statement, which works fine.But let's try to (ab)use decorators to get a with-open-file block anyways. The call_with_open_file decorator kind of gives us this effect: the decorated function acts as a code block. However, unlike try...finally, it has the limitations of nested functions, so unfortunately this recipe is not quite so useful as the Lisp macro.Nevertheless, it might stil be useful in the occasional sitation where you have to use libraries that don't let you create and finalize objects yourself, but force you to use a callback. Having no way to use a try...finally, and needing to reference (but hopefully not rebind) local variables, a decorator like the one in this recipe could help. Date: 11 January, 2012


Function Decorator - File Object - File Tool - Function - Decorator - File

Homepage: http://code.activestate.com/

Developer: code.activestate.com

License: Freeware

Operating System: All

Add a Comment

all are required fields

     
What do you think of this resource?

Select Your Rate:

Votes:0

 

Related Scripts Download

This script is a decorator for automatic code optimization.

developer Developer: code.activestate.com
license License: Freeware
operating systems Operating System: All


This function decorates a function with tail call optimization.

developer Developer: code.activestate.com
license License: Freeware
operating systems Operating System: All


This script defines a decorator that allows for an abbreviated slice syntax in function arguments: {start:end}.

developer Developer: code.activestate.com
license License: Freeware
operating systems Operating System: All


Python symbols is a toy module that shows a way to define symbols inside functions, using a decorator.

developer Developer: code.activestate.com
license License: Freeware
operating systems Operating System: All


This decorator can be used to cast argument types automatically, using a pre-specified list of types, when a function is called.

developer Developer: code.activestate.com
license License: Freeware
operating systems Operating System: All


protect_utf8 is a function decorator that can prevent naive functions from breaking UTF-8.

developer Developer: code.activestate.com
license License: Artistic License, GNU General Public License (GPL)
operating systems Operating System: Windows, Linux, Mac OS, BSD, Solaris


This script is a queue data structure, for string data only, which looks like a File object.

developer Developer: code.activestate.com
license License: Freeware
operating systems Operating System: All


This script contains a class that enables a client to securely update an existing file, including the ability to make an automated backup version.

developer Developer: code.activestate.com
license License: Freeware
operating systems Operating System: All


This code demonstrates recursive file scanning using FindFirstFile and FindNextFile and a DelTree routine for MS Windows similar to the MS DOS command.

developer Developer: planet-source-code.com
license License: Freeware
operating systems Operating System: All