Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
package extension problem
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Fabrizio Pollastri  
View profile  
 More options Feb 12, 1:46 pm
Newsgroups: comp.lang.python
From: Fabrizio Pollastri <f.pollas...@inrim.it>
Date: Sun, 12 Feb 2012 19:46:04 +0100
Local: Sun, Feb 12 2012 1:46 pm
Subject: package extension problem
Hello,

I wish to extend the functionality of an existing python package by creating
a new package that redefines the relevant classes of the old package. Each
new class inherits the equivalent old class and adds new methods.

In the new package there is something like the following.

import old_package as op

class A(op.A):
     ...
     add new methods
     ...

class B(op.B):
     ...
     add new methods
     ...

Some classes of the old package works as a dictionary of other classes
of the
same old package. Example: if class A and class B are classes of the old
package,
B[some_hash] returns an instance of A.

When a program imports the new package and create instances of the new
class B,
B[some_hash] still returns an instance of the old class A, while I want
an instance of the new class A.

There is a way to solve this problem without redefining in the new
package all the
methods of the old package that return old classes?

Thanks in advance for any suggestion,
Fabrizio


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Miki Tebeka  
View profile  
 More options Feb 13, 12:18 pm
Newsgroups: comp.lang.python
From: Miki Tebeka <miki.teb...@gmail.com>
Date: Mon, 13 Feb 2012 09:18:16 -0800 (PST)
Local: Mon, Feb 13 2012 12:18 pm
Subject: Re: package extension problem
> B[some_hash] still returns an instance of the old class A, while I want

an instance of the new class A.
I don't understand this sentence. How does B[some_hash] related to A?

I've tried the below and it seems to work. Can you paste some code to help us understand more?

-- old.py --
class A:
    pass

-- new.py __
import old
class A(old.A):
    pass

-- main.py --
import new
a = new.A()
a.__class__ # Shows new.A


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Miki Tebeka  
View profile  
 More options Feb 13, 12:18 pm
Newsgroups: comp.lang.python
From: Miki Tebeka <miki.teb...@gmail.com>
Date: Mon, 13 Feb 2012 09:18:16 -0800 (PST)
Local: Mon, Feb 13 2012 12:18 pm
Subject: Re: package extension problem
> B[some_hash] still returns an instance of the old class A, while I want

an instance of the new class A.
I don't understand this sentence. How does B[some_hash] related to A?

I've tried the below and it seems to work. Can you paste some code to help us understand more?

-- old.py --
class A:
    pass

-- new.py __
import old
class A(old.A):
    pass

-- main.py --
import new
a = new.A()
a.__class__ # Shows new.A


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fabrizio Pollastri  
View profile  
 More options Feb 13, 12:53 pm
Newsgroups: comp.lang.python
From: Fabrizio Pollastri <f.pollas...@inrim.it>
Date: Mon, 13 Feb 2012 18:53:27 +0100
Local: Mon, Feb 13 2012 12:53 pm
Subject: Re: package extension problem
Ok. To be more clear, consider the real python package Pandas.

This package defines a Series class and a DataFrame class.
The DataFrame is a matrix that can have columns of
different type.

If I write

import pandas as pd
df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})

a data frame with two cols named A and B is created.

If I write

col_A = df['A']

the returned col_A is an instance of Series.

Now , let suppose that I want to extend some functionality of pandas
by adding new methods to both Series and DataFrame classes.

One way to do this is to redefine this classes in a new package
(new_pandas) as follow

import pandas as pd

class Series(pd.Series):
      ...
      add new methods
      ...

class DataFrame(pd.DataFrame):
      ...
      add new methods
      ...

When I use the new package as a pandas substitute and write

import new_pandas as np
df = np.DataFrame({'A':[1,2,3],'B':[4,5,6]})
col_A = df['A']

col_A is an instance of the original pandas and not of the new pandas,
losing all the added functionality.

Fabrizio

Now, how can I add new methods to extend the functionality of pandas
classes Series and DataFrame


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Miki Tebeka  
View profile  
 More options Feb 13, 12:58 pm
Newsgroups: comp.lang.python
From: Miki Tebeka <miki.teb...@gmail.com>
Date: Mon, 13 Feb 2012 09:58:02 -0800 (PST)
Local: Mon, Feb 13 2012 12:58 pm
Subject: Re: package extension problem
> import new_pandas as np
> df = np.DataFrame({'A':[1,2,3],'B':[4,5,6]})
> col_A = df['A']

I'm not familiar with pandas, but my *guess* will be that you'll need to override __getitem__ in the new DataFrame.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Miki Tebeka  
View profile  
 More options Feb 13, 12:58 pm
Newsgroups: comp.lang.python
From: Miki Tebeka <miki.teb...@gmail.com>
Date: Mon, 13 Feb 2012 09:58:02 -0800 (PST)
Local: Mon, Feb 13 2012 12:58 pm
Subject: Re: package extension problem
> import new_pandas as np
> df = np.DataFrame({'A':[1,2,3],'B':[4,5,6]})
> col_A = df['A']

I'm not familiar with pandas, but my *guess* will be that you'll need to override __getitem__ in the new DataFrame.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Feb 13, 1:28 pm
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Mon, 13 Feb 2012 19:28:05 +0100
Local: Mon, Feb 13 2012 1:28 pm
Subject: Re: package extension problem

A quick look into the pandas source reveals that the following might work:

# untested
class DataFrame(pd.DataFrame):
    @property
    def _constructor(self):
        return DataFrame # your class
    # your new methods


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Feb 13, 2:12 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Mon, 13 Feb 2012 14:12:44 -0500
Local: Mon, Feb 13 2012 2:12 pm
Subject: Re: package extension problem
On 2/13/2012 12:58 PM, Miki Tebeka wrote:

>> import new_pandas as np df =
>> np.DataFrame({'A':[1,2,3],'B':[4,5,6]}) col_A = df['A']
> I'm not familiar with pandas, but my *guess* will be that you'll need
> to override __getitem__ in the new DataFrame.

This is essentially the same problem that if you, for instance, subclass
int as myint, you need to override (wrap) *every* method to get them to
return myints instead of ints.

class myint(int):
     ...
     def __add__(self, other): return myint(self+other)
     ....

In the OP's case, if the original class is in python, one might be able
to just change the __class__ attribute. But I would make sure to have a
good set of tests in any case.

--
Terry Jan Reedy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »