Singleton-like pattern

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pedro Werneck

    Singleton-like pattern

    I need to implement an object that behaves just like imutable objects,
    like int, str and tuple: if you create two objects with the same value
    at the same time, you get the same object, like a singleton, but
    instead of storing a unique object, store a set of objects, with no
    duplicates. I don't know if this is a common pattern (and if there's a
    name for it), but I remember reading something about it some time
    ago... the ASPN cookbook has some similar patterns, but none works for
    me...

    I know there are several ways to implement this, but I think this
    metaclass approach is more elegant than using __new__ and
    inheritance... at least it solves my problem (I'm working on a card
    game) but I am sure that there are better ways to do it... I could not
    find any other value suitable as a key and using str(args) + str(kwds)
    is ugly and easy to break...


    class MultiSingleton( type):
    def __call__(cls, *args, **kwds):
    cache = cls.__dict__.ge t('__cache__')
    if cache is None:
    cls.__cache__ = cache = {}
    tag = str(args) + str(kwds)
    if tag in cache:
    return cache[tag]
    obj = object.__new__( cls)
    obj.__init__(*a rgs, **kwds)
    cache[tag] = obj
    return obj


    Any help will be much appreciated... thanks in advance

    Pedro Werneck
  • Michele Simionato

    #2
    Re: Singleton-like pattern

    pedro.werneck@b ol.com.br (Pedro Werneck) wrote in message news:<ef72fb2b. 0307312003.6717 cc9e@posting.go ogle.com>...[color=blue]
    > I need to implement an object that behaves just like imutable objects,
    > like int, str and tuple: if you create two objects with the same value
    > at the same time, you get the same object, like a singleton, but
    > instead of storing a unique object, store a set of objects, with no
    > duplicates. I don't know if this is a common pattern (and if there's a
    > name for it), but I remember reading something about it some time
    > ago... the ASPN cookbook has some similar patterns, but none works for
    > me...
    >
    > I know there are several ways to implement this, but I think this
    > metaclass approach is more elegant than using __new__ and
    > inheritance... at least it solves my problem (I'm working on a card
    > game) but I am sure that there are better ways to do it... I could not
    > find any other value suitable as a key and using str(args) + str(kwds)
    > is ugly and easy to break...
    >
    >
    > class MultiSingleton( type):
    > def __call__(cls, *args, **kwds):
    > cache = cls.__dict__.ge t('__cache__')
    > if cache is None:
    > cls.__cache__ = cache = {}
    > tag = str(args) + str(kwds)
    > if tag in cache:
    > return cache[tag]
    > obj = object.__new__( cls)
    > obj.__init__(*a rgs, **kwds)
    > cache[tag] = obj
    > return obj
    >
    >
    > Any help will be much appreciated... thanks in advance
    >
    > Pedro Werneck[/color]

    "memoize" is a possible name for this. Notice that the metaclass is a
    bit of overkill, you may well use a simple function for this job.
    About the issue of finding a suitable key, in the same situation I have
    used the tuple (args,kw) as key. But me too I would like to ask if this is a
    good idea. What's the custom solution for getting a good key from
    a dictionary ?


    Michele

    Comment

    • Bengt Richter

      #3
      Re: Singleton-like pattern

      On 1 Aug 2003 09:15:34 -0700, mis6@pitt.edu (Michele Simionato) wrote:
      [color=blue]
      >pedro.werneck@ bol.com.br (Pedro Werneck) wrote in message news:<ef72fb2b. 0307312003.6717 cc9e@posting.go ogle.com>...[color=green]
      >> I need to implement an object that behaves just like imutable objects,
      >> like int, str and tuple: if you create two objects with the same value
      >> at the same time, you get the same object, like a singleton, but
      >> instead of storing a unique object, store a set of objects, with no
      >> duplicates. I don't know if this is a common pattern (and if there's a
      >> name for it), but I remember reading something about it some time
      >> ago... the ASPN cookbook has some similar patterns, but none works for
      >> me...
      >>
      >> I know there are several ways to implement this, but I think this
      >> metaclass approach is more elegant than using __new__ and
      >> inheritance... at least it solves my problem (I'm working on a card
      >> game) but I am sure that there are better ways to do it... I could not
      >> find any other value suitable as a key and using str(args) + str(kwds)
      >> is ugly and easy to break...
      >>
      >>
      >> class MultiSingleton( type):[/color][/color]
      class MultiSingleton( object):[color=blue][color=green]
      >> def __call__(cls, *args, **kwds):[/color][/color]
      def __new__(cls, *args, **kwds):[color=blue][color=green]
      >> cache = cls.__dict__.ge t('__cache__')
      >> if cache is None:
      >> cls.__cache__ = cache = {}
      >> tag = str(args) + str(kwds)[/color][/color]
      tag = '%r%r'% (args, kwds) # might be an alternative[color=blue][color=green]
      >> if tag in cache:
      >> return cache[tag]
      >> obj = object.__new__( cls)
      >> obj.__init__(*a rgs, **kwds)
      >> cache[tag] = obj
      >> return obj
      >>
      >>
      >> Any help will be much appreciated... thanks in advance
      >>
      >> Pedro Werneck[/color]
      >
      >"memoize" is a possible name for this. Notice that the metaclass is a
      >bit of overkill, you may well use a simple function for this job.[/color]
      Why not just change as above, and sublass to inherit the behavior
      (and create subclass-specific caches) ?
      [color=blue]
      >About the issue of finding a suitable key, in the same situation I have
      >used the tuple (args,kw) as key. But me too I would like to ask if this is a
      >good idea. What's the custom solution for getting a good key from
      >a dictionary ?[/color]
      Does (args,kw) work in general? IWT you could easily get something unhashable?
      IWT using a tuple of actual args may also be a bad idea since it would prevent callers'
      temp args from being garbage collected. I use repr to avoid that, maybe mistakenly?

      Regards,
      Bengt Richter

      Comment

      • Carl Banks

        #4
        Re: Singleton-like pattern

        Michele Simionato wrote:[color=blue]
        > About the issue of finding a suitable key, in the same situation I have
        > used the tuple (args,kw) as key.[/color]

        Surely not? Perhaps you meant tuple(args,tupl e(kw.items())) ? It gets
        rid of the unhashable kw dict.

        [color=blue]
        > But me too I would like to ask if this is a
        > good idea. What's the custom solution for getting a good key from
        > a dictionary ?[/color]

        Howabout cPickle.dumps(( args,kwargs),1) ?


        --
        CARL BANKS

        Comment

        • Pedro Werneck

          #5
          Re: Singleton-like pattern

          On 1 Aug 2003, Bengt Richter wrote:
          [color=blue][color=green][color=darkred]
          > >> class MultiSingleton( type):[/color][/color]
          > class MultiSingleton( object):[color=green][color=darkred]
          > >> def __call__(cls, *args, **kwds):[/color][/color]
          > def __new__(cls, *args, **kwds):[color=green][color=darkred]
          > >> cache = cls.__dict__.ge t('__cache__')
          > >> if cache is None:
          > >> cls.__cache__ = cache = {}
          > >> tag = str(args) + str(kwds)[/color][/color]
          > tag = '%r%r'% (args, kwds) # might be an alternative[color=green][color=darkred]
          > >> if tag in cache:
          > >> return cache[tag]
          > >> obj = object.__new__( cls)
          > >> obj.__init__(*a rgs, **kwds)
          > >> cache[tag] = obj
          > >> return obj[/color][/color][/color]

          This is exactly what I did at first... I only changed it to a metaclass
          because I was adding it to a module that already contain some other
          metaclasses I use; I think it's more elegant as I said before... a
          'quick and dirty' benchmark with the timeit module returned the
          folowing results, and in this project I will be dealing with a database
          of 7000+ items, this 20% bit may help a little:

          __metaclass__:
          [39.744094967842 102, 40.455733060836 792, 42.027853965759 277]

          __new__:
          [47.914013981819 153, 48.721022009849 548, 49.430392026901 245]


          On 1 Aug 2003, Michele Simionato wrote:
          [color=blue]
          > "memoize" is a possible name for this. Notice that the metaclass is a
          > bit of overkill, you may well use a simple function for this job.[/color]

          Hey... you wrote such a good article on python metaclasses and now don't
          want people to use them ? :)
          [color=blue]
          > Does (args,kw) work in general? IWT you could easily get something unhashable?
          > IWT using a tuple of actual args may also be a bad idea since it would prevent callers'
          > temp args from being garbage collected. I use repr to avoid that, maybe mistakenly?[/color]
          [color=blue]
          > About the issue of finding a suitable key, in the same situation I have
          > used the tuple (args,kw) as key. But me too I would like to ask if this is a
          > good idea. What's the custom solution for getting a good key from
          > a dictionary ?[/color]

          Actually, (args, kw) doesn't work at all, even if you only get hashable
          arguments... the 'kw' dict invalidate it as a key... besides, there's the
          garbage collection problem, as Bengt Richter mentioned...

          The "%r%r"%(arg s, kwds) works, as well as str((args, kwds))), but it
          breaks if you get as argument an object with the default __repr__, as
          the object id may be different, even if they are equal. Overriding __repr__
          and returning a real representation avoids this problem.

          Thanks for your time.

          Pedro Werneck

          Comment

          • Michele Simionato

            #6
            Re: Singleton-like pattern

            pedro.werneck@b ol.com.br (Pedro Werneck) wrote in message news:<ef72fb2b. 0308011825.6359 9ff2@posting.go ogle.com>...[color=blue]
            > On 1 Aug 2003, Bengt Richter wrote:
            >[color=green][color=darkred]
            > > >> class MultiSingleton( type):[/color][/color]
            > class MultiSingleton( object):[color=green][color=darkred]
            > > >> def __call__(cls, *args, **kwds):[/color][/color]
            > def __new__(cls, *args, **kwds):[color=green][color=darkred]
            > > >> cache = cls.__dict__.ge t('__cache__')
            > > >> if cache is None:
            > > >> cls.__cache__ = cache = {}
            > > >> tag = str(args) + str(kwds)[/color][/color]
            > tag = '%r%r'% (args, kwds) # might be an alternative[color=green][color=darkred]
            > > >> if tag in cache:
            > > >> return cache[tag]
            > > >> obj = object.__new__( cls)
            > > >> obj.__init__(*a rgs, **kwds)
            > > >> cache[tag] = obj
            > > >> return obj[/color][/color]
            >
            > This is exactly what I did at first... I only changed it to a metaclass
            > because I was adding it to a module that already contain some other
            > metaclasses I use; I think it's more elegant as I said before... a
            > 'quick and dirty' benchmark with the timeit module returned the
            > folowing results, and in this project I will be dealing with a database
            > of 7000+ items, this 20% bit may help a little:
            >
            > __metaclass__:
            > [39.744094967842 102, 40.455733060836 792, 42.027853965759 277]
            >
            > __new__:
            > [47.914013981819 153, 48.721022009849 548, 49.430392026901 245]
            >
            >
            > On 1 Aug 2003, Michele Simionato wrote:
            >[color=green]
            > > "memoize" is a possible name for this. Notice that the metaclass is a
            > > bit of overkill, you may well use a simple function for this job.[/color]
            >
            > Hey... you wrote such a good article on python metaclasses and now don't
            > want people to use them ? :)[/color]

            Maybe it is because I know them that I don't want to abuse them ;)
            Consider also that not everybody knows about metaclasses, and
            I want my code to be readable to others; finally, there is
            Occam's rasor argument (i.e. do not use metaclasses without necessity).
            [color=blue][color=green]
            > > Does (args,kw) work in general? IWT you could easily get something unhashable?
            > > IWT using a tuple of actual args may also be a bad idea since it would prevent callers'
            > > temp args from being garbage collected. I use repr to avoid that, maybe mistakenly?[/color]
            >[color=green]
            > > About the issue of finding a suitable key, in the same situation I have
            > > used the tuple (args,kw) as key. But me too I would like to ask if this is a
            > > good idea. What's the custom solution for getting a good key from
            > > a dictionary ?[/color]
            >
            > Actually, (args, kw) doesn't work at all, even if you only get hashable
            > arguments... the 'kw' dict invalidate it as a key... besides, there's the
            > garbage collection problem, as Bengt Richter mentioned...[/color]

            Yes, as I replied to Carl Banks, actually I have got problems with
            (args,kw) and I think at the end I used args+tuple(kw.i teritems()),
            but I had forgotten at the time of the posting.
            [color=blue]
            > The "%r%r"%(arg s, kwds) works, as well as str((args, kwds))), but it
            > breaks if you get as argument an object with the default __repr__, as
            > the object id may be different, even if they are equal. Overriding __repr__
            > and returning a real representation avoids this problem.
            >
            > Thanks for your time.
            >
            > Pedro Werneck[/color]


            Michele

            Comment

            • Steven Taschuk

              #7
              Re: Singleton-like pattern

              Quoth Michele Simionato:
              [...][color=blue]
              > Actually you are right, I remember now that I got problems with
              > (args,kw) and at the end I used args,tuple(kw.i tems()). But I was
              > ensure if this was a good solution.[/color]

              I'd worry about the order of kw.items().

              --
              Steven Taschuk Aral: "Confusion to the enemy, boy."
              staschuk@telusp lanet.net Mark: "Turn-about is fair play, sir."
              -- _Mirror Dance_, Lois McMaster Bujold

              Comment

              • Michele Simionato

                #8
                Re: Singleton-like pattern

                Steven Taschuk <staschuk@telus planet.net> wrote in message news:<mailman.1 059863764.14138 .python-list@python.org >...[color=blue]
                > Quoth Michele Simionato:
                > [...][color=green]
                > > Actually you are right, I remember now that I got problems with
                > > (args,kw) and at the end I used args,tuple(kw.i tems()). But I was
                > > ensure if this was a good solution.[/color]
                >
                > I'd worry about the order of kw.items().[/color]

                Yes, indeed. Also, the args tuple can contain nested dictionaries and
                become unhashable; in such a case I should recursively flatten all
                the dictionaries to tuples, taking in account the ordering.
                To much work for me ...I have really to look at the cPickle solution.
                How does it solve the ordering issue?

                Michele

                Comment

                • Carl Banks

                  #9
                  Re: Singleton-like pattern

                  Michele Simionato wrote:[color=blue]
                  > Steven Taschuk <staschuk@telus planet.net> wrote in message news:<mailman.1 059863764.14138 .python-list@python.org >...[color=green]
                  >> Quoth Michele Simionato:
                  >> [...][color=darkred]
                  >> > Actually you are right, I remember now that I got problems with
                  >> > (args,kw) and at the end I used args,tuple(kw.i tems()). But I was
                  >> > ensure if this was a good solution.[/color]
                  >>
                  >> I'd worry about the order of kw.items().[/color]
                  >
                  > Yes, indeed. Also, the args tuple can contain nested dictionaries and
                  > become unhashable; in such a case I should recursively flatten all
                  > the dictionaries to tuples, taking in account the ordering.
                  > To much work for me ...I have really to look at the cPickle solution.
                  > How does it solve the ordering issue?[/color]


                  Unfortunately, not well.
                  [color=blue][color=green][color=darkred]
                  >>> b = { 1: 1, 9: 1 }
                  >>> b[/color][/color][/color]
                  {1: 1, 9: 1}[color=blue][color=green][color=darkred]
                  >>> c = { 9: 1, 1: 1 }
                  >>> c[/color][/color][/color]
                  {9: 1, 1: 1}[color=blue][color=green][color=darkred]
                  >>> from cPickle import dumps
                  >>> dumps(b)[/color][/color][/color]
                  '(dp1\nI1\nI1\n sI9\nI1\ns.'[color=blue][color=green][color=darkred]
                  >>> dumps(c)[/color][/color][/color]
                  '(dp1\nI9\nI1\n sI1\nI1\ns.'



                  --
                  CARL BANKS
                  "You don't run Microsoft Windows. Microsoft Windows runs you."

                  Comment

                  Working...