Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32608

Re: Organisation of python classes and their methods

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'classes,': 0.05; 'needed,': 0.05; 'class,': 0.07; 'classes.': 0.07; 'cure': 0.07; 'problem?': 0.07; 'suppose': 0.07; 'type,': 0.07; 'python': 0.09; 'already.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'looked': 0.10; 'def': 0.10; 'subject:python': 0.11; 'file,': 0.15; 'classes:': 0.16; 'googled': 0.16; 'hurt.': 0.16; 'hurts': 0.16; 'nightmare': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'released.': 0.16; 'subclasses.': 0.16; 'subject:their': 0.16; 'martin': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'module': 0.19; 'appropriate': 0.20; 'code.': 0.20; 'import': 0.21; "i'd": 0.22; 'absolute': 0.23; "haven't": 0.23; 'pass': 0.25; 'header:User- Agent:1': 0.26; 'extend': 0.26; 'am,': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'lines': 0.28; 'this?': 0.28; 'actual': 0.28; 'trouble': 0.28; 'grouping': 0.29; 'long.': 0.29; 'methods.': 0.29; 'piece': 0.29; 'objects': 0.29; 'source': 0.29; 'class': 0.29; "i'm": 0.29; 'maybe': 0.29; 'classes': 0.30; 'that.': 0.30; 'usually': 0.30; 'normally': 0.30; 'sense': 0.31; 'code': 0.31; 'file': 0.32; 'google,': 0.32; 'operate': 0.32; 'could': 0.32; 'print': 0.32; 'skip:s 30': 0.33; 'belong': 0.33; 'instances': 0.33; 'like:': 0.33; 'handle': 0.33; 'to:addr:python- list': 0.33; 'another': 0.33; 'version': 0.34; 'project': 0.34; 'minimum': 0.34; 'list': 0.35; 'clear': 0.35; 'needed': 0.35; 'so,': 0.35; 'there': 0.35; 'add': 0.36; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'alone': 0.36; 'data.': 0.36; 'modules': 0.36; "didn't": 0.36; 'method': 0.36; 'editor': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'object': 0.38; 'some': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'list,': 0.39; 'little': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'your': 0.60; 'bring': 0.62; 'relatively': 0.62; 'repeat': 0.62; 'close': 0.63; 'different': 0.63; 'more': 0.63; 'here': 0.65; 'maintenance': 0.65; 'organisation': 0.65; 'dear': 0.66; 'analysis': 0.70; '100': 0.78; 'clearly.': 0.84; 'otten': 0.84; 'overloading': 0.84; 'seriously,': 0.91; 'many,': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Organisation of python classes and their methods
Date Fri, 02 Nov 2012 10:17:16 +0100
Organization None
References <B16BD7AC-F860-4A30-A08D-A0235CC25F20@mac.com> <k6vuis$j5b$1@ger.gmane.org> <E4CF83CA-179F-4FD5-A1CB-D25060C568A4@mac.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849249.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3190.1351847843.27098.python-list@python.org> (permalink)
Lines 105
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351847843 news.xs4all.nl 6934 [2001:888:2000:d::a6]:53637
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32608

Show key headers only | View raw


Martin Hewitson wrote:

> 
> On 2, Nov, 2012, at 09:00 AM, Peter Otten <__peter__@web.de> wrote:
> 
>> Martin Hewitson wrote:
>> 
>>> Dear list,
>>> 
>>> I'm relatively new to Python and have googled and googled but haven't
>>> found a reasonable answer to this question, so I thought I'd ask it
>>> here.
>>> 
>>> I'm beginning a large Python project which contains many packages,
>>> modules and classes. The organisation of those is clear to me.
>>> 
>>> Now, the classes can contain many methods (100s of data analysis
>>> methods) which operate on instances of the class they belong to. These
>>> methods can be long and complex. So if I put these methods all in the
>>> module file inside the class, the file will get insanely long. Reading
>>> on google, the answer is usually "refactor", but that really doesn't
>>> make sense here. It's just that the methods are many, and each method
>>> can be a long piece of code. So, is there a way to put these methods in
>>> their own files and have them 'included' in the class somehow? I read a
>>> little about mixins but all the solutions looked very hacky. Is there an
>>> official python way to do this? I don't like having source files with
>>> 100's of lines of code in, let alone 1000's.
>> 
>> You googled, found the right answer ("refactor"), didn't like it and are
>> now looking to cure the symptoms of the original problem?
>> Seriously, a good editor can deal with a long source file, but a class
>> with hundreds of methods will bring trouble to any old brain.
> 
> Well, here we disagree. Suppose I have a class which encapsulates
> time-series data. Below is a list of the absolute minimum methods one
> would have to process that data. That's close to 100 already before even
> having any specialised methods for dealing with the data.  Each of these
> methods will have maybe 20 lines of documentation. That's 2000 lines
> already. And what if someone wants to extend that class to add their own
> processing methods? 

from timeseries import TimeSeries

class MyTimeSeries(TimeSeries):
    def average():
        # specialised implementation

> It would a maintenance nightmare for them to edit the
> actual class file, which they would then have to repeat each time a new
> version of the 'official' class file is released.

Patient: Doctor, it hurts when I ... 
Doctor: Then don't do that.

> So maybe some rethinking of this design is needed to handle this
> 'limitation' of python. Perhaps grouping the processing algorithms into
> methods of processing classes, then pass the data objects to these
> methods. But somehow I don't like that. I have the feeling these methods
> would end up peppered with things like:
> 
> if this data type, do this
> else if this data type, do this
> else ....
> 
> normally this would be solved by overloading methods in different data
> subclasses.

You could ask your TimeSeries for the appropriate Statistics subclass

stats = ts.get_stats()
print stats.mean()

where get_stats() is a classmethod that returns an object that provides 
min(), max(), average() etc.
 
Another approach are mix-in classes:

class Stats:
    def min(): ...
    def average(): ...

class SpecialStats(Stats):
    def min(): return 42

class TimeSeries(BaseTimeSeries, Stats):
    pass

class SpecialTimeSeries(BaseTimeSeries, SpecialStats):
    pass

>     'abs'
[...]
>     'zeropad'

You are not perchance reimplementing numpy?

> More thinking needed, clearly.

That will never hurt. Well, almost:

http://www.theonion.com/articles/beaver-overthinking-dam,1942/

:)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Organisation of python classes and their methods Peter Otten <__peter__@web.de> - 2012-11-02 10:17 +0100

csiph-web