473,224 Members | 1,922 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,224 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 3725
En Tue, 24 Jul 2007 19:51:30 -0300, Boris Duek <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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
6
by: Amil Hanish | last post by:
I have two classes that I have implemented ICloneable. From my top-level class, how to I clone the base class values: See "how do I clone the base class values" below. Since Clone returns an...
8
by: Asfand Yar Qazi | last post by:
Hi, I have the following header file in my 'everything useful I think of in one place' library: ============= BEGIN CODE SNIPPET =========== /** All classes that derive from this obtain a...
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
11
by: Aflj | last post by:
This code won't compile (two compilers tried, gcc and VC++, both of recent versions, but I don't remember them exactly): class C1 { public: void M1(int i) {} }; class C2: public C1
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
11
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by...
0
by: brboLikus | last post by:
Hello everybody! My problem is somewhat strange since I can't think of any normal situation where one would like to do what I need, but here it goes. I need to somehow cast a base class to a...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.