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

Using the 'with' statement with cStringIO objects

I've been experimenting with the 'with' statement (in __future__), and
so far I like it. However, I can't get it to work with a cStringIO
object. Here's a minimum working example:

###
from __future__ import with_statement
import cStringIO

teststring='this is a test'

with cStringIO.StringIO(teststring) as testfile:
pass
###

I get the following error message:

Traceback (most recent call last):
File "testfile.py", line 6, in <module>
with cStringIO.StringIO(teststring) as testfile:
AttributeError: 'cStringIO.StringI' object has no attribute '__exit__'

So, I'm guessing you can't use the 'with' statement with cStringIO
objects? Is this a bug, or do I need to use the 'with' statement
differently to get this to work?

Thanks,
peppergrower
Sep 27 '08 #1
5 2777
peppergrower wrote:
teststring='this is a test'

with cStringIO.StringIO(teststring) as testfile:
pass
umm. what exactly do you expect that code to do?

</F>

Sep 27 '08 #2
So, I'm guessing you can't use the 'with' statement with cStringIO
objects? Is this a bug, or do I need to use the 'with' statement
differently to get this to work?

Thanks,
peppergrower
Neither, just not implemented. Only classes with __enter__ and __exit__
methods(ie context manager types) can be used in with statements. And,
correct me if I'm wrong, I think it's pointless for a StringIO object to
have those.
Sep 27 '08 #3
George Boutsioukis <gb**********@gmail.comwrites:
Neither, just not implemented. Only classes with __enter__ and
__exit__ methods(ie context manager types) can be used in with
statements. And, correct me if I'm wrong, I think it's pointless for
a StringIO object to have those.
It's definitely superfluous, but it should still be provided, for the
same reason a "close" method is provided, to allow StringIO objects to
be treated like other file-like objects. For example, code that does
the following:

with obj.open_file(...) as f:
...

shouldn't have to care if obj.open_file returns a built-in file
instance, or a StringIO instance. As the above code becomes more
popular, __enter__ and __exit__ are beginning to be part of the file
interface (in the duck-typing sense) and should be implemented.

Until then, the the contextlib.closing context manager can be used
instead:

with contextlib.closing(obj.open_file(...)) as f:
...
Sep 27 '08 #4
Thanks for the help. I'm fairly new to programming (which you
probably could have guessed...). When I realized that you could use a
StringIO instance as if it were a file, I wanted to try some of the
same techniques on it as I would with a file. In this case, I wanted
to use a "for line in testfile" construction to iterate over the
StringIO instance. (I did find a better way for my particular case,
one that didn't involve StringIO at all.) When I got that particular
error, I suspected that it had something to do with the relative
newness of the 'with' statement.

If this is something that should be considered for addition in the
future, is there somewhere specific I should suggest that?
Sep 27 '08 #5
En Sat, 27 Sep 2008 19:28:49 -0300, peppergrower
<sp******************@gmail.comescribió:
When I got that particular
error, I suspected that it had something to do with the relative
newness of the 'with' statement.

If this is something that should be considered for addition in the
future, is there somewhere specific I should suggest that?
Yes: http://bugs.python.org/ (setting type="feature request", I think)

--
Gabriel Genellina

Sep 30 '08 #6

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

Similar topics

7
by: Bob | last post by:
Currently I am using this statement to translate 3 fields in my db thru Visual Basic. I import the data from one table to another then call the IFF statements and the NewDate to translate the...
2
by: bashanitwonline | last post by:
Hi All, I have DB2 8.1 on my system. I have created a procedure as follows. CREATE PROCEDURE DB2ER000.XYZ() LANGUAGE SQL BEGIN INSERT INTO DB2ER000.A ( C,D,E) VALUES (10,20,30); END It...
4
by: doodle | last post by:
access 97 can someone tell me the syntax for a with stement for multiple controls? pseudo (doesn't work) With cmbBox1,cmbBox2,cmbBox3 .Visible = False End With
0
by: Lavs | last post by:
Hi Guys... Is there anyone who could help me on how to add an image file to a database using SQL Statement... I am using a database in MS ACCESS 2003, and my application was designed in VB 6.0. I...
33
by: =?Utf-8?B?RE9UTkVUR1VZ?= | last post by:
Hello, In vb.net there is a with statement, Is there are similar constructor in c#?
2
by: Steven D'Aprano | last post by:
I have found myself writing functions rather like these: def openfile(filename): if filename == '-': # convention for shell scripts in Unix-land is to use # '-' for stdin/stdout for...
3
by: Maximiliano | last post by:
Hello, I have an asp.net project that calculates a general tax. Ok, this tax is a big object formed with another child objects (as a mather of fact 15 another child object within it), like Ship,...
1
by: =?Utf-8?B?bGlhbnF0bGl0?= | last post by:
Is using a jump statement more faster than using if statement with a jump statement example for(int i=0; i < 10; i++) { if(a == null) {
1
by: tensi4u | last post by:
Hi all, I've been in stuck on updating a table. Here is the query. with tab_one ( "col_1", "col_2", "col_3", "col_4" ) as ( select ax."col1", ax."col2", bx."col1", bx."col2" ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.