Re: Singleton

2007-10-10 Thread OnCallSupport321
Spring Python (http://springpython.python-hosting.com) offers a
singleton solution. You can mark up function calls to only be called
the first time, i.e. as singletons. The results are cached inside the
Spring Python IoC container, so the next time the function is called,
it returns the cached copy. Prototypes bypass the caching, and instead
allow the function to be called everytime. This also support the idea
of prototype functions contain references to singleton objects.

from springpython.context import *

class MyContainer(DecoratorBasedApplicationContext):
   def __init__(self):
  DecoratorBasedAppicationContext.__init__(self)

   @component(scope.SINGLETON) # You can leave off the argument, since
scope.SINGLETON is the default)
   def singletonComponent(self):
  return ["There can be only one!"]

   @component(scope.PROTOTYPE):
   def prototypeComponent(self):
  return ["I think I'm a clone now. There's another one of me
always hangin' around!"]

if __name__ == "__main__":
   appContext = MyContainer()

   obj1 = appContext.singletonComponent()
   obj2 = appContext.singletonComponent()
   if obj1 == obj2:
  print "These are the same object"
   else:
  print "Something is wrong!"

   obj3 = appContext.prototypeComponent()
   obj4 = appContext.prototypeComponent()
   if obj1 != obj2:
  print "These are different instances of the function call."
   else:
  print "These shouldn't be the same!"

See also http://springpython.python-hosting.com/wiki/InversionOfControl
and 
http://springpython.python-hosting.com/wiki/DecoratorBasedApplicationContext.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Jython import coventions

2007-10-10 Thread OnCallSupport321
On Oct 9, 10:42 pm, Benjamin <[EMAIL PROTECTED]> wrote:
> When importing Java packages in Jython, what is the convention to
> simplify imports? For example:
> import java.util as util
> Is this correct? Or should I use from * import *?

I have preferred the style...

import java
import javax

...and then using complete references to java classes, in your case,
java.util.whatever. It clearly shows me where I'm using Python classes
vs. java classes when I read my own code. For this situation, more
path structure seems less confusing to me.

-- 
http://mail.python.org/mailman/listinfo/python-list