471,354 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Base class for file-like objects? (a.k.a "Stream" in Java)

Hello,

(sorry to begin with Java in a Python list ;-)
in Java, when I want to pass input to a function, I pass
"InputStream", which is a base class of any input stream.

In Python, I found that "file" objects exist. While specifying
argument types in Python is not possible as in Java, it is possible to
check whether an object is an instance of some class and that's what I
need - I need to check if an argument is a "file"-like object, and if
yes, behave accordingly, if not, treat the argument as string with
URL.

But I am afraid there is no such a base class - I tried the following:
>>import urllib

f = open("test.txt", "r")
g = urllib.urlopen("http://www.google.com/")

isinstance(f, file)
True
>>isinstance(f, file)
False
....

Is there some base class to "file"-like (or "stream"-like) objects in
Python? And if not, is it at least planned for Python 3.0?

Thanks for any suggestions,
Boris Dušek

P.S.: The code should finally look in esence something like this:

if isinstance(f, file):
pass
elif isinstance(f, string):
f = urllib.urlopen(f)
else:
raise "..."
process_stream(f)

Jul 24 '07 #1
4 3559
En Tue, 24 Jul 2007 19:51:30 -0300, Boris Du¨ek <bo*********@gmail.com>
escribió:
in Java, when I want to pass input to a function, I pass
"InputStream", which is a base class of any input stream.

In Python, I found that "file" objects exist. While specifying
argument types in Python is not possible as in Java, it is possible to
check whether an object is an instance of some class and that's what I
need - I need to check if an argument is a "file"-like object, and if
yes, behave accordingly, if not, treat the argument as string with
URL.
No, it's not what you need, it's what you *think* you need :)
P.S.: The code should finally look in esence something like this:
Posting this is much better that saying what you think you need.
if isinstance(f, file):
pass
elif isinstance(f, string):
f = urllib.urlopen(f)
else:
raise "..."
process_stream(f)
I can imagine that process_stream is something like this:

def process_stream(f):
...
data = f.read()
...

or similar. Then, you dont need a file object: you need something with a
read() method. So, this is what you should check in your code above.

if hasattr(f, "read"):
pass
elif isinstance(f, basestring):
f = urllib.urlopen(f)
else:
raise TypeError, "Expecting either a readable file-like object or an URL"
process_stream(f)

Or perhaps:

if isinstance(f, basestring):
f = urllib.urlopen(f)
process_stream(f)

and just let the exception happen below at f.read - which explains itself
rather well.

--
Gabriel Genellina

Jul 25 '07 #2
On Jul 25, 8:51 am, Boris Dušek <boris.du...@gmail.comwrote:
In Python, I found that "file" objects exist. While specifying
argument types in Python is not possible as in Java, it is possible to
check whether an object is an instance of some class and that's what I
need - I need to check if an argument is a "file"-like object, and if
yes, behave accordingly, if not, treat the argument as string with
URL.

But I am afraid there is no such a base class - I tried the following:
Yup you are right. Look.
>>type(g).mro()
[<type 'instance'>, <type 'object'>]
>>type(f).mro()
[<type 'file'>, <type 'object'>]

"File-like object" refers the the behaviour of the object, not it's
inheritance. Python is an equal opportunity employer, we don't ask
and object what race it is, we simply ask "can you do the job?" This
is known colloquially as "duck-typing." In other words Python has
'polymorphism by interface,' in contradistinction to Java's
'polymorphism by implementation.' Until you understand this you will
be trying to write Java in Python, and you will not have much joy.
P.S.: The code should finally look in esence something like this:

if isinstance(f, file):
pass
elif isinstance(f, string):
f = urllib.urlopen(f)
else:
raise "..."
process_stream(f)
Because of duck-typing, you should almost never use the isintance()
(or type(), hasattr(), or any others that don't immediately come to
mind ...) in actual code. It (usually) breaks Python's polymorphism!
If you find these methods popping up in your code it strongly
indicates that you should be using a try/except statement, if not a
complete change of your code's logic. Search for LBYL (look before
you leap) vs EAFP (easier to ask forgiveness than permission) for a
full explanation.

I'm not sure why you would ever be sending a file object to urlopen
(or is the test isinstance(f, file) supposed to test for an already
opened url?), but you final code should actually look more like
something along these lines:

try :
f = urlopen(f)
except AttributeError :
pass

This is not as elegant as it could be, as it will pass not only on
open files (or urls), but on any type that lacks the .strip method
(ie. it doesn't account for your else condition). You'd probably have
to catch an exception later when you try to use what should be an open
url, or rewrite your code not to get in this position in the first
place. But however you refactor your code, it is advisable to
concentrate on what an object can do (and catch an exception where it
fails to perform), rather on the type of the object.

Jul 25 '07 #3
On Jul 25, 12:28 pm, Asun Friere <afri...@yahoo.co.ukwrote:
On Jul 25, 8:51 am, Boris Dušek <boris.du...@gmail.comwrote:
In Python, I found that "file" objects exist. While specifying
argument types in Python is not possible as in Java, it is possible to
check whether an object is an instance of some class and that's what I
need - I need to check if an argument is a "file"-like object, and if
yes, behave accordingly, if not, treat the argument as string with
URL.
But I am afraid there is no such a base class - I tried the following:

Yup you are right. Look.>>type(g).mro()

[<type 'instance'>, <type 'object'>]>>type(f).mro()

[<type 'file'>, <type 'object'>]

"File-like object" refers the the behaviour of the object, not it's
inheritance. Python is an equal opportunity employer, we don't ask
and object what race it is, we simply ask "can you do the job?" This
is known colloquially as "duck-typing." In other words Python has
'polymorphism by interface,' in contradistinction to Java's
'polymorphism by implementation.' Until you understand this you will
be trying to write Java in Python, and you will not have much joy.
P.S.: The code should finally look in esence something like this:
if isinstance(f, file):
pass
elif isinstance(f, string):
f = urllib.urlopen(f)
else:
raise "..."
process_stream(f)

Because of duck-typing, you should almost never use the isintance()
(or type(), hasattr(), or any others that don't immediately come to
mind ...) in actual code. It (usually) breaks Python's polymorphism!
If you find these methods popping up in your code it strongly
indicates that you should be using a try/except statement, if not a
complete change of your code's logic. Search for LBYL (look before
you leap) vs EAFP (easier to ask forgiveness than permission) for a
full explanation.

I'm not sure why you would ever be sending a file object to urlopen
(or is the test isinstance(f, file) supposed to test for an already
opened url?), but you final code should actually look more like
something along these lines:

try :
f = urlopen(f)
except AttributeError :
pass

This is not as elegant as it could be, as it will pass not only on
open files (or urls), but on any type that lacks the .strip method
(ie. it doesn't account for your else condition). You'd probably have
to catch an exception later when you try to use what should be an open
url, or rewrite your code not to get in this position in the first
place. But however you refactor your code, it is advisable to
concentrate on what an object can do (and catch an exception where it
fails to perform), rather on the type of the object.
I agree that using try statements is often, perhaps usually, a good
idea. That said...

One good reason to avoid using a try: except: methodology is if the
actions taken in the try block could potentially modify data
structures or the underlying data during processing, or if the action
to be tried is expensive.

Being able to check before taking an action that it will be safe can
be important.

In these cases, you can use isinstance. Python 3k, I believe, is
thinking of being able to implement Abstract Base Classes for this
very reason.

As things stand, however, you will not be able to catch all cases with
a simple isinstance call. Try except should work in almost all cases.
If you want to do your own duck-typing, you can use dir(object) to get
its methods and check whether what you need is in there.

Anyway, good luck with it.

Jul 25 '07 #4
On Jul 24, 6:51 pm, Boris Dušek <boris.du...@gmail.comwrote:
Hello,

(sorry to begin with Java in a Python list ;-)
in Java, when I want to pass input to a function, I pass
"InputStream", which is a base class of any input stream.

In Python, I found that "file" objects exist. While specifying
argument types in Python is not possible as in Java, it is possible to
check whether an object is an instance of some class and that's what I
need - I need to check if an argument is a "file"-like object, and if
yes, behave accordingly, if not, treat the argument as string with
URL.

But I am afraid there is no such a base class - I tried the following:
>import urllib
>f = open("test.txt", "r")
g = urllib.urlopen("http://www.google.com/")
>isinstance(f, file)
True
>isinstance(f, file)

False
...

Is there some base class to "file"-like (or "stream"-like) objects in
Python? And if not, is it at least planned for Python 3.0?

Thanks for any suggestions,
Boris Dušek

P.S.: The code should finally look in esence something like this:

if isinstance(f, file):
pass
elif isinstance(f, string):
f = urllib.urlopen(f)
else:
raise "..."
process_stream(f)

Other replies show you how to tackle this in Python 2.x. Python 3K
will come closer to Java by formalizing the concept of abstract base
classes [1] and will most likely include a fine-grained hierarchy of
stream-like base classes [2].

George
[1] http://www.python.org/dev/peps/pep-3119/
[2] http://www.python.org/dev/peps/pep-3116/

Jul 25 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Amil Hanish | last post: by
8 posts views Thread by Asfand Yar Qazi | last post: by
11 posts views Thread by anongroupaccount | last post: by

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.