472,328 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Borg vs. Module

I am making a web app, made up of many modules
that all need access to some important data, like
the current session data, cookies, navigation history,
post/get variables, etc.

I decided to go with the 'Borg' idea, by assigning the
__dict__ of an object to a class variable so that each
instantiation of the Borg() would share the same data.
That way I can either pass the Borg around, or just
Borg() it if I need it in some obscure place.

Then I read an argument that it usually makes more sense
to just have a module with the data and functions that
I need, which all the other modules can simply import.
Since I only need one instance, I could just stuff data
and call 'methods' on this module, probably without even
changing existing syntax.

Are there any arguments as to which method is better?

Thanks,

Toby

--
Posted via a free Usenet account from http://www.teranews.com

Jul 31 '06 #1
2 1637

Jordan R McCoy wrote:
For example, the Twisted framework uses this technique to allow global
access to the installed reactor via import syntax, which works well
within the framework. It did, however, throw me a bit when I first
encountered it, and drove me to pour over the source to find out what
was happening.
It sounds to me like this was a case of poor code commenting in Twisted
(Iv'e not looked at it myself). It seems to me this is a useful trick,
but like any unusual or clever bit of coding it's use should be
carefully commented to make sure it's clear to anyone coming across it.

Simon Hibbs

Aug 1 '06 #2
tobiah wrote:
I am making a web app, made up of many modules
that all need access to some important data, like
the current session data, cookies, navigation history,
post/get variables, etc.

I decided to go with the 'Borg' idea, by assigning the
__dict__ of an object to a class variable so that each
instantiation of the Borg() would share the same data.
That way I can either pass the Borg around, or just
Borg() it if I need it in some obscure place.

Then I read an argument that it usually makes more sense
to just have a module with the data and functions that
I need, which all the other modules can simply import.
Since I only need one instance, I could just stuff data
and call 'methods' on this module, probably without even
changing existing syntax.

Are there any arguments as to which method is better?
I would recommend a module over a Borg class simply because it's less
of a hack.

One thing that I never liked about using modules as classes was the
constant need to use the global statement when rebinding any module
level variables. For that reason, if there was a lot of rebinding, I
would rewrite the module as a singleton class. That had its own
problems; most significantly, it was a thorn in my side for
initializing things in the right order. (My current project used lots
of circular imports which made things a lot worse.)

I finally lost my patience and decided to pull that thorn out by
converting the singletons back to modules. That's when I came up with
this little decorator that obviates the need to use global statements,
plus it lets the module look and mostly act like a class.

def modmethod(func):
class modproxy(object):
__getattribute__ = func.func_globals.__getitem__
__setattr__ = func.func_globals.__setitem__
self = modproxy()
def call_with_method(*args,**kwargs):
return func(self,*args,**kwargs)
call_with_method.func_name = func.func_name
return call_with_method

This decorator means the function will get called with the module
(actually a proxy) as the first argument. That way, you can access
module-level variable exactly like you would access instance variables
in a class, i.e., via self.

I was able to convert all my singletons to modules simply by dedenting
one level, and prefixing all the methods with @modmethod. This worked
so sweetly it this is now my slam dunk recommendation for implementing
a "global singleton" class.
Carl Banks

Aug 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rajarshi Guha | last post by:
Hi, I'm having a little problem with understanding the working of a singleton and borg class. Basically I nedd an class whose state will be shared...
10
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
In my application, I make use of the Borg idiom, invented by Alex Martelli. class Borg(object): '''Borg Idiom, from the Python Cookbook, 2nd...
2
by: Tobiah | last post by:
I have a class that I call Borg that starts like this: class Borg(dict): static_state = {} def __init__(self): self.__dict__ =...
1
by: seanacais | last post by:
I want to create a class derived from a Borg class that can instantiated as part of a script or be contained in other classes. When methods from...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.