473,231 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Parameters in context manager's __enter__ method?


I am working on a file locking class which I'd like to work with Python
2.5's context managers. The acquire method takes an optional timeout
argument:

class FileLock:
...
def acquire(self, timeout=None):
...

def __enter__(self):
self.acquire()
return self

Can that optional timeout be somehow accommodated by the with statement?
(I'm thinking no, which may not be a big shortcoming anyway.)

Thx,

Skip
Oct 24 '07 #1
1 3997
On Oct 24, 7:06 am, s...@pobox.com wrote:
I am working on a file locking class which I'd like to work with Python
2.5's context managers. The acquire method takes an optional timeout
argument:

class FileLock:
...
def acquire(self, timeout=None):
...

def __enter__(self):
self.acquire()
return self

Can that optional timeout be somehow accommodated by the with statement?
(I'm thinking no, which may not be a big shortcoming anyway.)

Thx,

Skip
I think a better solution might be to create a locking class, and a
seperate context manager class. That way you can pass the timeout as a
parameter to the context managers constructor:

Expand|Select|Wrap|Line Numbers
  1. fl = FileLock(...)
  2. with FileLockContext(filelock=fl, timeout=1000):
  3. # do stuff
  4.  
But another option along the same line might be to have the acquire
method return the context instance. Then you could write:

Expand|Select|Wrap|Line Numbers
  1. class FileLockContext(fl, timeout):
  2. ...
  3.  
  4. class FileLock:
  5. ...
  6. def acquire(self, timeout=None):
  7. # do acquiring
  8. return FileLockContext(self, timeout)
  9.  
  10. fl = FileLock(...)
  11. with fl.acquire(1000):
  12. #do stuff
  13.  
Or, you could go the same path you are already, using a hybrid, and
pass timeout to the FileLock constructor.

Matt

Oct 24 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SPG | last post by:
Hi, I am relatively new to TOMCAT, and am trying to deploy a web app using it. I have a domain name that I want to use to access my web app directly. By default, I deploy my WAR, and it deploys...
11
by: Andrew Thompson | last post by:
I have written a few scripts to parse the URL arguments and either list them or allow access to the value of any parameter by name. <http://www.physci.org/test/003url/index.html>...
8
by: cody | last post by:
Why doesn't C# allow default parameters for methods? An argument against I hear often is that the default parameters would have to be hardbaken into the assembly, but why? The Jit can take care of...
2
by: freddy | last post by:
I have a DbManager class that has all my connect stuff like connecting to the bd and inserting data, but for some reason when I call the insert coammand for the DbManager class I get A Method...
2
by: GlennDoten | last post by:
Upgraded to .NET 2.0 and VS.NET'05 and while compiling code that was fine under 1.1/2003 I'm getting a fatal compiler error: Conditional member 'GetTraceMode(out System.Web.TraceMode)' cannot...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
4
by: Daniel | last post by:
Hello, I need to access the code inside of a context manager, i.e. the call to with myManager(v=5) as x: a=b c=sin(x) should cause the following output (minus the first line, if that's...
0
by: Mikolai Fajer | last post by:
Is there a reason that the gzip.GzipFile class does not have __enter__ and __exit__ methods that mimic those of the file object? I expected the following to work but it doesn't: import gzip...
0
by: Gabriel Genellina | last post by:
En Wed, 19 Nov 2008 17:04:22 -0200, Mikolai Fajer <mfajer@gmail.com> escribió: Several of those were added in Python 2.6, looks like GzipFile should behave the same way. -- Gabriel...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.