class Borg:
__shared_state = {}
def __init__(self):
self.__dict__ = self.__shared_state
OK, that's it. Have fun.
Showing posts with label snippet. Show all posts
Showing posts with label snippet. Show all posts
Monday, July 5, 2010
Still use singleton? There's another choice in python: Borg design pattern
Few months ago i implemented a db extension for my own python web framework, which supports different database backend. To prevent it from creating multiple instance of db-connection i have chosen singleton design pattern as usual. But after spent some time on google i found another interesting design pattern Borg. It's done this job as good as singleton. Let's take a look of this magical pattern:
Monday, June 28, 2010
How to generate a random date between two known dates in javascript?
function getRandomDate(from, to) {The Code above not tested yet, no guarantee it's runnable...
if (!from) {
from = new Date(1900, 0, 1).getTime();
} else {
from = from.getTime();
}
if (!to) {
to = new Date(2100, 0, 1).getTime();
} else {
to = to.getTime();
}
return new Date(from + Math.random() * (to - from));
}
Subscribe to:
Posts (Atom)
