473,402 Members | 2,064 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,402 software developers and data experts.

Bind an instance of a base to a subclass - can this be done?

I've been scanning Python in a Nutshell, but this seems to be either
undoable or so subtle that I don't know how to do it.

I want to subclass a base class that is returned from a Standard Library
function (particularly, subclass file which is returned from open). I
would add some extra functionality and keep the base functions, too.
But I am stuck.

E.g.

class myfile(file):
def myreadline():
#code here to return something read from file

Then do something like (I know this isn't right, I'm just trying to
convey the idea of what I would like)

mf=myfile()

mf=open("Afile","r")

s=mf.myreadline() # Use my added function

mf.close() # Use the original file function
Possible in some way? Thanks in advance for any clues.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.
May 24 '06 #1
8 1651
Lou Pecora schrieb:
I've been scanning Python in a Nutshell, but this seems to be either
undoable or so subtle that I don't know how to do it.

I want to subclass a base class that is returned from a Standard Library
function (particularly, subclass file which is returned from open). I
would add some extra functionality and keep the base functions, too.
But I am stuck.

E.g.

class myfile(file):
def myreadline():
#code here to return something read from file

Then do something like (I know this isn't right, I'm just trying to
convey the idea of what I would like)

mf=myfile()

mf=open("Afile","r")

s=mf.myreadline() # Use my added function

mf.close() # Use the original file function
Possible in some way? Thanks in advance for any clues.


Nope, not in that way. But you might consider writing a proxy/wrapper
for an object. That looks like this (rouch sketch from head):

class FileWrapper(object):
def __init__(self, f):
self._f = f

def __getattr__(self, name):
return getattr(self._f, name)

def myreadline(self):
....
Then you do

f = FileWrapper(open(name, mode))
Diez
May 24 '06 #2
Le Mercredi 24 Mai 2006 22:04, Diez B. Roggisch a écrit*:
Nope, not in that way. But you might consider writing a proxy/wrapper
for an object. That looks like this (rouch sketch from head):

class FileWrapper(object):
* * def __init__(self, f):
* * * *self._f = f

* * def __getattr__(self, name):
* * * *return getattr(self._f, name)

* * def myreadline(self):
* * * *....

Why use a proxy when you can just inherit from the builtin file object ?

class myFile(file) :
def myreadline(self) : print 'yo'
In [20]: myFile('frwiki-20060511-abstract.xml')
Out[20]: <open file 'frwiki-20060511-abstract.xml', mode 'r' at 0xa78cc1e4>

In [21]: myFile('frwiki-20060511-abstract.xml').myreadline()
yo

In [22]:

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
May 24 '06 #3
Lou Pecora wrote:
I want to subclass a base class that is returned from a Standard Library
function (particularly, subclass file which is returned from open). I
would add some extra functionality and keep the base functions, too.
But I am stuck.

E.g.

class myfile(file):
def myreadline():
#code here to return something read from file

Then do something like (I know this isn't right, I'm just trying to
convey the idea of what I would like)

mf=myfile()

mf=open("Afile","r")

s=mf.myreadline() # Use my added function

mf.close() # Use the original file function
Possible in some way? Thanks in advance for any clues.


This:
mf=myfile()
mf=open("Afile","r")
Is actually creating an instance of myfile, then throwing it away,
replacing it with an instance of file. There are no variable type
declarations in Python.

To accomplish what you want, simply instantiate the subclass:
mf=myfile("Afile","r")
You don't need to do anything tricky, like binding the instance of the
base class to a subclass. Python does actually support that, e.g.:
class Base(object): def f(self):
return 'base' class Subclass(Base): def f(self):
return 'subclass' b = Base()
b.__class__ <class '__main__.Base'> b.f() 'base' b.__class__ = Subclass
b.__class__ <class '__main__.Subclass'> b.f() 'subclass'

But the above won't work for the built-in file type:
f = file('foo')
f.__class__ <type 'file'> f.__class__ = Subclass

TypeError: __class__ assignment: only for heap types

Again though, just instantiate the subclass. Much cleaner.

Or if that's not an option due to the way your module will be used,
just define your custom file methods as global functions that take a
file instance as a parameter. Python doesn't force you to use OOP for
everything.

--Ben

May 24 '06 #4
Maric Michaud schrieb:
Le Mercredi 24 Mai 2006 22:04, Diez B. Roggisch a écrit :
Nope, not in that way. But you might consider writing a proxy/wrapper
for an object. That looks like this (rouch sketch from head):

class FileWrapper(object):
def __init__(self, f):
self._f = f

def __getattr__(self, name):
return getattr(self._f, name)

def myreadline(self):
....

Why use a proxy when you can just inherit from the builtin file object ?


To be honest - in my perception file is a function. Which it isn't, but
I see it that way :)

You are of course right, and have the better solution (unless the OP is
not in control of file creation).

Diez
May 25 '06 #5
Le Jeudi 25 Mai 2006 01:10, vous avez écrit :
The ratio of two durations has no meaning???

Oh, sorry, sure it has, I wanted to say "it has no meaning in timedelta
provided arithmetic".
It's a ratio (no dimension) not a duration. In that sense the expected result
should be a float, and the proposed operator will break the timedelta's
arithmetic consistence.

t, u, v <- timedeltas
t+u # valid
t / u # valid
t / u + v # invalid while all terms are valids

It's a big design flaw and I think it's the full answer to the original
question.

Le Jeudi 25 Mai 2006 02:26, Robert Kern a écrit :
what you want is :

num_weeks = time_diff.days / 7
or
num_weeks = (time_diff / 7).days


Uh, no. Besides the integer division problem in your first line, keep in
mind that the .days attribute does not give you the time interval measured
in days. It gives you the number of *whole* days in the interval. The first
method will be incorrect if time_diff is not an even multiple of 1 day.
The latter will be incorrect if time_diff is not an even multiple of 7 days.

In fact i was computing the exact number of whole weeks in the delta. In
respect of that both expression are perfectly correct, but the second one
isn't clear IMO (why this "days" attribute should give me the number of
weeks ?).

This said it's not hard to figure out the correct expression of the decimal
value of weeks in deltas (discarding the microseconds which are not
relevant) :
num_weeks = (time_diff.days * 24* 3600 + time_diff.seconds) / (7.*24*3600)

If I need to do much of these in a piece of code I would probably define some
helper functions like this :

def tomicroseconds(td) :
return td.days * 24* 3600 * 10**6 +
td.seconds * 10 ** 6 + td.microseconds

def toseconds(td) : return float(tomicroseonds(td)) / 10 ** 6
tominute, tohours, todays, toweeks, etc...

and use float and int / and % operators.
This is an easy and clean implementation IMHO.

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
May 25 '06 #6
In article <4d*************@uni-berlin.de>,
"Diez B. Roggisch" <de***@nospam.web.de> wrote:
Lou Pecora schrieb:

[cut]

Then do something like (I know this isn't right, I'm just trying to
convey the idea of what I would like)

mf=myfile()

mf=open("Afile","r")
Possible in some way? Thanks in advance for any clues.


Nope, not in that way. But you might consider writing a proxy/wrapper
for an object. That looks like this (rouch sketch from head):

class FileWrapper(object):
def __init__(self, f):
self._f = f

def __getattr__(self, name):
return getattr(self._f, name)

def myreadline(self):
....

Then you do

f = FileWrapper(open(name, mode))

Diez


Interesting. I have to think about this to understand if it is a
solution I can use. But, thank you for a very different angle on this.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.
May 25 '06 #7
In article <ma***************************************@python. org>,
Maric Michaud <ma***@aristote.info> wrote:
Le Mercredi 24 Mai 2006 22:04, Diez B. Roggisch a écrit*:
Nope, not in that way. But you might consider writing a proxy/wrapper
for an object. That looks like this (rouch sketch from head):

class FileWrapper(object):
* * def init (self, f):
* * * *self. f = f

* * def getattr (self, name):
* * * *return getattr(self. f, name)

* * def myreadline(self):
* * * *....

Why use a proxy when you can just inherit from the builtin file object ?

class myFile(file) :
def myreadline(self) : print 'yo'
In [20]: myFile('frwiki-20060511-abstract.xml')
Out[20]: <open file 'frwiki-20060511-abstract.xml', mode 'r' at 0xa78cc1e4>

In [21]: myFile('frwiki-20060511-abstract.xml').myreadline()
yo

In [22]:


BINGO! This is exactly what I want. I didn't realize that I could open
using file itself instead of open. I did find this in another section
of Python in a Nutshell thanks to your suggestion.

Thank you.

And thanks to all who answered.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.
May 25 '06 #8
I came up with this solution for subclassing the file object and making
some easy I/O functions (much thanks to Maric Michaud for pointing me in
the right direction). My goal was to make I/O of variables easy and in
a form that I could easily visually examine the file (which I often need
to do). The code also keeps it very clear as to what is being read in
or out in a single function call.

The class (inherited from file) in file ezfile.py:

# ==== File subclass from file for EZ I/O =======================

class ezfile(file):

# ---- Write items to file ------------------
# converts items list to string first using repr fcn.
def printline(_, ls):
sls=repr(ls)
_.writelines(sls)

# ---- Scan line from file & return items --------------------
# converts scanned string to list first using eval fcn.
def scanline(_,):
sls=_.readline()
return eval(sls)
An example in a Python session:
from ezfile import *
# Define some variables x=2.334
i= 7
str='Some stuff here'
# Open a file and output the variables to it ff=ezfile('junk','w')
ff.printline([x,i,str])
ff.close()
# Open the same file and read the values back in to other variables f2=ezfile('junk','r')
y,j,thestr=f2.scanline()
print y,j,thestr 2.334 7 Some stuff here f2.close()


The file content looks like this:

[2.3340000000000001, 7, 'Some stuff here']

easy to see what is saved to the file.

It works! Thanks, again. Comments welcome.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.
May 25 '06 #9

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

Similar topics

1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
3
by: Randy Fraser | last post by:
What is the proper way of declaring an override on a button click event for a base form. I am getting " Cannot bind an event handler to the 'Click' event because it is read only." listed in the...
18
by: Sandra-24 | last post by:
Can you create an instance of a subclass using an existing instance of the base class? Such things would be impossible in some languages or very difficult in others. I wonder if this can be done...
1
by: s.lipnevich | last post by:
Hi All, Is anything wrong with the following code? class Superclass(object): def __new__(cls): # Questioning the statement below return super(Superclass, cls).__new__(Subclass) class...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
5
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
0
by: Hans Koller | last post by:
Hello group, I design a class to bind it to a property grid for easy modification of some settings. My problem is now that I want to raise an event when a settings has been changed. Thats not a...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.