473,385 Members | 2,003 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.

Supporting read() with fileinput?

Hi everyone,

I'm was wondering: would it be a good idea to have the FileInput class
support a read() method? I found myself running into a small problem
while using xml.sax.parse in combination with fileinput. Here's a
snippet that demonstrates the problem:

###
import xml.sax
import fileinput

xml.sax.parse(fileinput.input(), xml.sax.ContentHandler())
###

This doesn't work, because fileinput.input() isn't a file-like object:
it doesn't have read(), so it can't be converted into a nice InputSource.
But I thought it might be nice to have one, so I cooked up a quick
class to make it work:

###
class ByteStream:
def __init__(self, f):
self._buffer = []
self._f = f

def read(self, bytes):
if not self._buffer: self._prime()
next_block = self._buffer[:bytes]
del self._buffer[:bytes]
return ''.join(next_block)

def _prime(self):
self._buffer.extend(list(self._f.readline()))
###

And now I can get:

###
xml.sax.parse(ByteStream(fileinput.input()), xml.sax.ContentHandler())
###

to work. But I'd rather make this solution a bit more permanent.
*grin* Would it be a good idea to add a read() method to FileInput to
make it more file-like?
Thanks!
Jul 18 '05 #1
0 1368

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

Similar topics

10
by: wo_shi_big_stomach | last post by:
Newbie to python writing a script to recurse a directory tree and delete the first line of a file if it contains a given string. I get the same error on a Mac running OS X 10.4.8 and FreeBSD 6.1. ...
4
by: Adam Funk | last post by:
I'm using this sort of standard thing: for line in fileinput.input(): do_stuff(line) and wondering whether it reads until it hits an EOF and then passes lines (one at a time) into the...
11
by: jo3c | last post by:
hi everybody im a newbie in python i need to read line 4 from a header file using linecache will crash my computer due to memory loading, because i am working on 2000 files each is 8mb ...
3
by: Robert | last post by:
I would like to count lines in a file using the fileinput module and I am getting an unusual output. ------------------------------------------------------------------------------...
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
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...
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
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.