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

concatenate file-like objects -> file-like object

kgk

I would like to concatenate several file-like objects
to create a single file-like object. I've looked at fileinput,
however
this returns a fileinput object that is not very file-like.

something like
# a has 50 bytes, and b has 100 bytes
f = FileList (open('a'), open('b'))
f.read (100) # read 50 bytes from a and 50 from b

My interest is in passing several files to an incremental parser
as if they came from a single file. I would rather not load them
in memory using StringIO and the parser reads only from file-like
objects.

Any pointers appreciated
Kris

Jul 11 '07 #1
1 2058
On Tue, 10 Jul 2007 17:55:52 -0700, kgk wrote:
I would like to concatenate several file-like objects
to create a single file-like object. I've looked at fileinput,
however
this returns a fileinput object that is not very file-like.

something like
# a has 50 bytes, and b has 100 bytes
f = FileList (open('a'), open('b'))
f.read (100) # read 50 bytes from a and 50 from b

My interest is in passing several files to an incremental parser
as if they came from a single file. I would rather not load them
in memory using StringIO and the parser reads only from file-like
objects.
Then program a file like object yourself. Something like this (untestet):

class FileList(object):
def __init__(self, files):
self.files = reversed(files)
self.current_file = self.files.pop()

def read(size):
result = ''
while self.files:
data = self.current_file.read(size)
result += data
if len(data) != size:
self.current_file = self.files.pop()
size = size - len(data)
return result

Ciao,
Marc 'BlackJack' Rintsch
Jul 11 '07 #2

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

Similar topics

1
by: aurora | last post by:
Sometimes I find it useful to create an instance of some dummy class so that I can bind abitrary attributes to it. I sometimes prefer this to mapping object. >>> class A: pass .... >>> a =...
7
by: Mike Hnatt | last post by:
I thought it would be something like an "eval" but it is not. I want to refer to an object called "myobject". I can normally do this: document.myobject.innerText= "43" But since I don't...
0
by: Wolfgang Schwanke | last post by:
Dear usenet, I'm having the following small problem. I've been ask to add some Quicktime panoramas to a website. The author of the panoramas has made two versions of each: One in MOV format,...
2
by: puzzlecracker | last post by:
I am just curious: if you declare an object constant - can it call NOT constant member functions of the class or only constant ones? are there generic rules for such objects? THANKs
3
by: MrShovel | last post by:
I'm new to this ASP.NET caper and have the following questions. I have a TestObject that contains about 50 fields of data and 3 member procedures. Below is a simplified explanation of what I do....
4
by: msnews.microsoft.com | last post by:
hello every one i am using media player in internet explorer but i cannot access it directly in code behind, so do you know a way to access it? <object id="video123"...
4
by: =?Utf-8?B?QWw=?= | last post by:
Hello, I'm writing a service using VS2005 and C#. I've found that only the objects used as parameters in WebMethods get exported to proxy dlls and the Service Description. I'd like my service...
5
by: malini | last post by:
Hi I have to concatenate an integer to an object created. something like this: for(int i - 0; i< ac.size();i++) { if ( ac->jack = A) {
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
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:
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...
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
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.