473,382 Members | 1,390 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.

feature request / max size limit on StringIO

Hello. I've not been able to use cStringIO since I have the need to
ensure that the memory buffers created are bounded within a resonable
limit set by specifications. No, this code does not properly belong
in my application as the modules that use "files" should not have
to care about any resource limitations that may be imposed.

class LimitedBuffer(StringIO):
def __init__(self, buffer = None, maxsize = 5 * 1000 * 1000):
StringIO.__init__(self,buffer)
self.cursize = 0
self.maxsize = maxsize
def write(self,str):
self.cursize += len(str)
if self.cursize > self.maxsize:
raise IOError("allocated buffer space exceeded")
return StringIO.write(self,str)

Kind Regards,

Clark Evans
Oct 25 '05 #1
1 6542
On Tue, 25 Oct 2005 14:15:02 -0400, "Clark C. Evans" <cc*@clarkevans.com> wrote:
Hello. I've not been able to use cStringIO since I have the need to
ensure that the memory buffers created are bounded within a resonable
limit set by specifications. No, this code does not properly belong
in my application as the modules that use "files" should not have
to care about any resource limitations that may be imposed.

class LimitedBuffer(StringIO):
def __init__(self, buffer = None, maxsize = 5 * 1000 * 1000):
StringIO.__init__(self,buffer)
self.cursize = 0
self.maxsize = maxsize
def write(self,str):
self.cursize += len(str)
if self.cursize > self.maxsize:
raise IOError("allocated buffer space exceeded")
return StringIO.write(self,str)


You might want to use StringIO's own knowledge of its writing position (self.pos or self.tell())
and then you wouldn't need cursize, nor to worry about whether seek has been used to
reposition, maybe writing the same section of file over and over for some reason.

I'm not sure whether seek beyond the end actually causes allocation, but I don't think so.
So you should be ok just checking self.pos+len(strarg)> self.maxsize.
There's also some interesting things going on with self.buf and self.buflist attributes, which
probably is not doing as simple a job of buffering as you might think. Maybe you'll want to wrap
a byte array or an mmap instance to store your info, depending on what you are doing?

Regards,
Bengt Richter
Oct 26 '05 #2

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

Similar topics

16
by: Michele Simionato | last post by:
I have read with interest the recent thread about closures. The funny thing is that the authors are arguing one against the other but I actually agree with all of them and I have a proposal that...
25
by: | last post by:
Hi, The following code shows the FolderBrowserDialog = broken. FolderBrowserDialog folderDialog = new FolderBrowserDialog(); folderDialog.ShowNewFolderButton = false;...
0
by: Clark C. Evans | last post by:
Hello. I've not been able to use cStringIO since I have the need to ensure that the memory buffers created are bounded within a resonable limit set by specifications. No, this code does not...
8
by: Praveen Chandra | last post by:
Hi I was trying to call the webservice of MS Project server (ProjectCreate interface of PDS) to create a new project. It works fine when the input XML is small with a few task and allocations. It...
32
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of...
4
by: aldonnelley | last post by:
Hi there: a bit of a left-field question, I think. I'm writing a program that analyses image files downloaded with a basic crawler, and it's slow, mainly because I only want to analyse files...
11
by: subramanian | last post by:
Suppose int size = INT_MAX / 8; char a; are declared to use the feature of variable lenght array(VLA) in C99. With appropriate #includes, when this code is run, segmentation fault occurs in Red...
149
by: jacob navia | last post by:
As a consequence of a heavy discussion in another thread, I wrote this program: #include <stdlib.h> int main(void) { char *p=calloc(65521,65552); if (p) printf("BUG!!!!\n"); }
6
by: sebastian.noack | last post by:
Hi, is there a way to or at least a reason why I can not use tarfile to create a gzip or bunzip2 compressed archive in the memory? You might might wanna answer "use StringIO" but this isn't...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
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: 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:
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
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.