473,387 Members | 1,705 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,387 software developers and data experts.

Need some help with 'with'

I recently found myself needing to do this a lot:

lock a record in a file
read the record into a buffer
alter the buffer
write the buffer back to the record
unlock the record

I'd love to be able to create a context for this to use with the
'with' statement, something like:

from __future__ import with_statement
from contextlib import contextmanager
from fcntl import lockf, LOCK_EX, LOCK_UN

@contextmanager
def lockedRecord(fileObj, offset, length):
lockf(fileObj, LOCK_EX, offset, length)
try:
fileObj.seek(offset)
buff = fileObj.read(length)
newBuff = (yield buff)
fileObj.seek(offset)
fileObj.write(newBuff)
finally:
lockf(fileObj, LOCK_UN, offset, length)

Which would let me write code like:

fileObj = open("myfile.dat", "r+b")

with lockedRecord(fileObj, 20, 10) as rec:
newRec = rec.replace('foo', 'bar')
# Somehow send newRec back into the context

I know you can now send data back into a generator via the send method
of the generator, however i don't seem to have access to the generator
object itself.

I figure that if instead of returning the buffer from the context
directly, I instead returned the buffer in a list, I could then change
the buffer, put it in the returned list, then I'd have access to it
back inside the context, but this seems a bit ugly.

Does anyone have insight into the right way of doing this?
Nov 28 '07 #1
1 928
In article <d7**********************************@w40g2000hsb. googlegroups.com>,
Mike Kent <mr******@cox.netwrote:
>
I figure that if instead of returning the buffer from the context
directly, I instead returned the buffer in a list, I could then change
the buffer, put it in the returned list, then I'd have access to it
back inside the context, but this seems a bit ugly.

Does anyone have insight into the right way of doing this?
You're almost at the right approach: just create a class instance that
contains a buffer attribute.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"Typing is cheap. Thinking is expensive." --Roy Smith
Nov 30 '07 #2

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

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
2
by: bryan | last post by:
I have a situation that's pretty delicate that I need some help on. I've been stumped for awhile I just need some advice on the best possible solution. The problem: I have a site I'm making...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.