473,382 Members | 1,639 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,382 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 4019
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.