Comparing objects - is there a maximum object?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Brew

    Comparing objects - is there a maximum object?

    I've just been writing some code to merge items from
    a collection of streams, where each item has a key,
    and each stream is known to be sorted in ascending
    order of key. I read all the streams once, then
    pick the minimum element of the results, replace
    it with a new one read from the corresponding
    stream, and repeat.

    Thing is, what happens at end of file? I'd like to
    do it by making the streams that are finished
    return some object (call it endmarker) such
    that min(x,endmarker ) always returns x. That way,
    when all the streams are returning endmarker, we
    would be done. And I'd like my code to work the same
    no matter what kind of thing the streams are
    returning, so I want endmarker to be a generic
    maximum object.

    The Python documentation says:

    The operators <, >, ==, >=, <=, and != compare the values of two
    objects. The objects need not have the same type. If both are numbers,
    they are converted to a common type. Otherwise, objects of different
    types always compare unequal, and are ordered consistently but
    arbitrarily

    so (unless this has been superseded) it doesn't seem that there is
    such an object. Any suggestions? I notice that empirically max(x,None)
    seems to be always x, but again I suppose I shouldn't count on that.

    Chris
  • Martin v. Löwis

    #2
    Re: Comparing objects - is there a maximum object?

    Chris Brew wrote:
    [color=blue]
    > so (unless this has been superseded) it doesn't seem that there is
    > such an object. Any suggestions?[/color]

    The easiest way to produce a maximum object is

    class Max:
    def __cmp__(self, other):
    return 1
    maximum = Max()

    HTH,
    Martin

    Comment

    • Andrew Dalke

      #3
      Re: Comparing objects - is there a maximum object?

      Martin v. Löwis:[color=blue]
      > The easiest way to produce a maximum object is
      >
      > class Max:
      > def __cmp__(self, other):
      > return 1[/color]

      Shouldn't there also be a

      def __rcmp__(self, other):
      return -1
      ?

      Andrew
      dalke@dalkescie ntific.com


      Comment

      • Martin v. Löwis

        #4
        Re: Comparing objects - is there a maximum object?

        "Andrew Dalke" <adalke@mindspr ing.com> writes:
        [color=blue]
        > Shouldn't there also be a
        >
        > def __rcmp__(self, other):
        > return -1
        > ?[/color]

        No, this would not be used. cmp(a,b) == -cmp(b,a)

        Regards,
        Martin

        Comment

        • Peter Otten

          #5
          Re: Comparing objects - is there a maximum object?

          Chris Brew wrote:
          [color=blue]
          > Thing is, what happens at end of file? I'd like to
          > do it by making the streams that are finished
          > return some object (call it endmarker) such
          > that min(x,endmarker ) always returns x. That way,
          > when all the streams are returning endmarker, we
          > would be done. And I'd like my code to work the same
          > no matter what kind of thing the streams are
          > returning, so I want endmarker to be a generic
          > maximum object.
          >[/color]

          Why not remove "streams" that have reached the end?
          You could do something like

          def gen(*seqs):
          seqs = [iter(seq) for seq in seqs]
          done = []
          while True:
          result = []
          for it in seqs:
          try:
          result.append(i t.next())
          except StopIteration:
          done.append(it)
          if len(done):
          for d in done:
          seqs.remove(d)
          done = []
          if len(result) == 0:
          break
          yield result

          # produce sample data
          import random
          l1 = range(5)
          l2 = range(10)
          l3 = range(3, 6)
          for l in l1, l2, l3:
          random.shuffle( l)

          # demonstration
          for i, x in enumerate(gen(l 1, l2, l3)):
          print i, x, "-->", min(x)

          There may be room for improvement in the gen() function, but the general
          idea is not to compare objects made up only for that purpose and when you
          already know the outcome.

          Peter

          Comment

          • Andrew Dalke

            #6
            Re: Comparing objects - is there a maximum object?

            Martin v. Löwis:[color=blue]
            > No, this would not be used. cmp(a,b) == -cmp(b,a)[/color]

            True. Unlike + and *, cmp is communitive.

            Here's the results of some instrumentation under 2.3.
            [color=blue][color=green][color=darkred]
            >>> class Max:[/color][/color][/color]
            .... def __getattr__(sel f, name):
            .... print "Looking for", name
            .... if name == "__cmp__":
            .... return lambda other: 1
            .... raise AttributeError( name)
            ....[color=blue][color=green][color=darkred]
            >>> data = [Max(), 0, 1, 2, 3]
            >>> data.sort()[/color][/color][/color]
            Looking for __gt__
            Looking for __coerce__
            Looking for __cmp__
            Looking for __gt__
            Looking for __coerce__
            Looking for __cmp__
            Looking for __gt__
            Looking for __coerce__
            Looking for __cmp__
            Looking for __gt__
            Looking for __coerce__
            Looking for __cmp__[color=blue][color=green][color=darkred]
            >>> random.shuffle( data); data.sort(); data[/color][/color][/color]
            Looking for __lt__
            Looking for __coerce__
            Looking for __cmp__
            Looking for __repr__
            [0, 1, 2, 3, <__main__.Max instance at 0x013D8C88>]

            Not an __rcmp__ in the mix.

            Andrew
            dalke@dalkescie ntific.com


            Comment

            Working...