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


Groups > comp.lang.python > #12138 > unrolled thread

Re: Adding modified methods from another class without subclassing

Started byJohn O'Hagan <research@johnohagan.com>
First post2011-08-24 21:01 +1000
Last post2011-08-24 21:01 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Adding modified methods from another class without subclassing John O'Hagan <research@johnohagan.com> - 2011-08-24 21:01 +1000

#12138 — Re: Adding modified methods from another class without subclassing

FromJohn O'Hagan <research@johnohagan.com>
Date2011-08-24 21:01 +1000
SubjectRe: Adding modified methods from another class without subclassing
Message-ID<mailman.384.1314183730.27778.python-list@python.org>
On Mon, 22 Aug 2011 20:38:30 +0200
Peter Otten <__peter__@web.de> wrote:

> John O'Hagan wrote:
> 
> > On Mon, 22 Aug 2011 11:32:18 +0200
> > Peter Otten <__peter__@web.de> wrote:
> > 
> >> John O'Hagan wrote:
> >> 
> >> > I have a class like this:
> >> > 
> >> > class MySeq():
> >> >     def __init__(self, *seq, c=12):
> >> >         self.__c = c
> >> >         self.__pc = sorted(set([i % __c for i in seq]))
> >> >         self.order = ([[self.__pc.index(i % __c), i // __c] for i in
> >> >         seq])
> >> >         #other calculated attributes
> >> > 
> >> >     @property
> >> >     def pitches(self):
> >> >         return [self.__pc[i[0]] + i[1] * self.__c for i in self.order]
> >> > 
> >> >     #other methods
> >> 
> >> That makes me dizzy. Are there any maxims in the Zen of Python that this
> >> piece doesn't violate?
> > 
> > "Now is better than never"?
> > 
> > I know it looks crazy to take something apart and put it back together, as
> > in this *simplified* *example*
> 
> emphasis mine ;)
> 
> > , but it does have a meaningful use: reducing a
> > series of musical notes to a unique irreducible form "__pc", ("prime form"
> > in pitch-class set theory), but keeping track of the actual current
> > manifestation of that form via the writeable "order" attribute. That's why
> > "pitches" must be called with each reference, because it may change,
> > unlike "__pc", which can only change if the instance is reinitialised.
> > 
> > I am open to more elegant suggestions, of course. :)
> > [...]
> 
> Dunno. Maybe you could inherit from collections.(Mutable)Sequence and 
> somewhat reduce the number of methods you'd have to implement. 
> Or you introduce a Pitch (Python) class that knows about its pitch class, 
> and put that into a normal list:
> 
> >>> class Pitch(int):
> ...     @property
> ...     def pitch_class(self):
> ...             return self % 12
> ...     def __repr__(self):
> ...             return "Pitch(%s, pitch_class=%s)" % (self, 
> self.pitch_class)
> ...
> >>> map(Pitch, [1,0,42])
> [Pitch(1, pitch_class=1), Pitch(0, pitch_class=0), Pitch(42, pitch_class=6)]
>

That's a good starting point for a neater solution, but there's more I have to solve. For completeness, here's the un-simplified __init__ of the class (yes, it gets worse!):

  
class MySeq():

    def __init__(self, *sequence, octave=12):

        pcset = sorted(set([i % octave for i in sequence]))
        steps = ([pcset[i] - pcset[i - 1] for i in range(1, len(pcset))]
                    + [octave - pcset[-1]]) 
        rotations = [steps[n:] + steps[:n] for n in range(len(steps))]
        prime_steps = min([i for i in rotations if i[-1] == max(steps)])
        self.__prime = [0]
        for step in prime_steps:
            self.__prime.append(step + prime[-1])
        self.__offset = pcset[rotations.index(prime_steps)]
        self.__order = Order([[prime.index((i - offset) % octave),
            (i - self.__offset) // octave] for i in sequence])
        self.__octave = octave   

The __prime attribute is the one all the internal methods refer to, and it's not so much a collection of pitch classes as an interval structure. Accordingly, I'll follow your tip and look into the collections ABCs, many of which look applicable, and go back to the drawing board.

Thanks,

John

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web