Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Singleton pattern in python

   1  
   2  class Borg:
   3      __shared_state = {}
   4      def __init__(self):
   5          self.__dict__ = self.__shared_state

What a pythonic way to use Singleton. It uses shared-state
approach where you can actually have many instances as you want
but they all share the same state. See Alex's recipe.
   1  
   2  >>> b = Borg()
   3  >>> b.x = 1
   4  >>> c = Borg()
   5  >>> c.x
   6  1

Sine wave interference patterns

A nice visualization from Simon Wittber's recipe
   1  
   2  from appuifw import *
   3  import e32, random
   4  from math import *
   5  
   6  app.body = c = Canvas()
   7  width, height =  c.size
   8  freq = random.choice([25., 50., 100., 200., 400.])
   9  
  10  for y in range(height):
  11      for x in range(width):
  12          z1 = sin(x/freq*1.7*pi)
  13          z2 = sin((x/3+y)/freq*1.5*pi)
  14          z3 = sin(y/freq*0.1*pi)
  15  
  16          z = abs(z1+z2+z3)*255
  17          c.point((x,y), (z,z/4,z*4))
  18  c.text((5, height-12), u'Freq = %d' %freq, 0xffffff)
  19  
  20  e32.ao_sleep(5)    # wait 5 sec then quit

See a screenshot here.
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS