473,699 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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='thi s is a test'

with cStringIO.Strin gIO(teststring) as testfile:
pass
###

I get the following error message:

Traceback (most recent call last):
File "testfile.p y", line 6, in <module>
with cStringIO.Strin gIO(teststring) as testfile:
AttributeError: 'cStringIO.Stri ngI' 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 2795
peppergrower wrote:
teststring='thi s is a test'

with cStringIO.Strin gIO(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**********@g mail.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.clos ing context manager can be used
instead:

with contextlib.clos ing(obj.open_fi le(...)) 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.co mescribió:
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
35672
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 fields. Can this be done in an Access db? I am trying to learn how to do these things in Access vs Visual Dim strIIF As String Dim sstrIIF As String Dim strNewDate As Date
2
9267
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 got created. But While calling the procedure using the statement
4
5188
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
1429
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 am passing the SQL Statement as a string into a datasource property of a recordset. Can you please give me a source code on how to do it. What datatype should i used for the image file?
33
2436
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
1206
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 reading/writing. outfile = sys.stdout if filename == '2-': outfile = sys.stderr else:
3
1967
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, Vehicle, Bike, etc. each one could be null and has his own attributes and properties. For example Tax { int _period;
1
1795
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
11720
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" from schm1.tab_a ax, schm2.tab_b bx where ~~ ), tab_two ( "col_1", "col_2", "col_3", "col_4" ) as (
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.