473,412 Members | 2,067 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to have application-wide global objects

Probably a newcomer question, but I could not find a solution.

I am trying to have some singleton global objects like "database
connection" or "session" shared application wide.

Trying hard, I am not even being able to figure out how to create an
object in one module and refer the same in another one. "import"
created a new object, as I tried.

Badly need guidences.

Thanks a lot
Sanjay

Jul 13 '06 #1
11 3025
Sanjay wrote:
Probably a newcomer question, but I could not find a solution.

I am trying to have some singleton global objects like "database
connection" or "session" shared application wide.
Whenever possible, dont. If you really have no other way out, create the
'singleton' in it's module (at the module's to level), then import the
module.
Trying hard, I am not even being able to figure out how to create an
object in one module and refer the same in another one. "import"
created a new object, as I tried.
I'd like to know what you actually tried.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 13 '06 #2
"Sanjay" wrote:
Trying hard, I am not even being able to figure out how to create an
object in one module and refer the same in another one. "import"
created a new object, as I tried.
"import" doesn't create new objects, so that's not very likely. can you post
some code so we don't have to guess what you've tried and not ?

</F>

Jul 13 '06 #3
Hi Bruno,

Thanks a lot for the reply. In order to post here, I wrote a very
simple program now, and it seems working! I can diagnose the original
problem now. There might be some other problem.

Pardon me if I am too novice but I could not make out the meaning of
this phrase from your reply:

"(at the module's to level)"

Thanks
Sanjay
------------------------------------------------------------------
Bruno Desthuilliers wrote:
Sanjay wrote:
Probably a newcomer question, but I could not find a solution.

I am trying to have some singleton global objects like "database
connection" or "session" shared application wide.

Whenever possible, dont. If you really have no other way out, create the
'singleton' in it's module (at the module's to level), then import the
module.
Trying hard, I am not even being able to figure out how to create an
object in one module and refer the same in another one. "import"
created a new object, as I tried.

I'd like to know what you actually tried.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 13 '06 #4
Sanjay wrote:
Probably a newcomer question, but I could not find a solution.

I am trying to have some singleton global objects like "database
connection" or "session" shared application wide.

Trying hard, I am not even being able to figure out how to create an
object in one module and refer the same in another one. "import"
created a new object, as I tried.
Try the "borg" pattern:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/66531
It's very simple and does what you need. Don't be put off by comments
that "it's not a *real* singleton".

Or, use a module-level object, e.g.:

----db.py----
class db_conn:
def __init__(self, dbname, dbhost):
self.conn = whatever(dbname, dbhost)

conn = db_conn('mydb", "myhost")

----app.py----
import db
....
cur = db.conn.cursor()
cur.execute("select lkjlkj")

Repeated imports of db by various modules in an application
do *not* rerun the code in db.py .

-- George

Jul 13 '06 #5
Sanjay wrote:
Hi Bruno,

Thanks a lot for the reply. In order to post here, I wrote a very
simple program now, and it seems working! I can diagnose the original
problem now.
Fine.
There might be some other problem.
This, we can't tell, since you didn't post the code !-)
Pardon me if I am too novice but I could not make out the meaning of
this phrase from your reply:

"(at the module's to level)"
Pardon me if I am too bad typist to avoid a typo on such a simple word
as 'top' !-)

And, <OTplease, don't top-post</OT>

(snip)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 13 '06 #6
Fredrik Lundh wrote:
"Sanjay" wrote:
>Trying hard, I am not even being able to figure out how to create an
object in one module and refer the same in another one. "import"
created a new object, as I tried.

"import" doesn't create new objects, so that's not very likely. can you
post some code so we don't have to guess what you've tried and not ?
It does if you mess around with sys.path between doing two imports of the
same thing (at least I found out the hard way on Python 2.4). I'm not sure
this is considered a bug or a "don't do that then" problem.

--
Jeremy Sanders
http://www.jeremysanders.net/
Jul 13 '06 #7
Jeremy Sanders wrote:
>"import" doesn't create new objects, so that's not very likely. can you
post some code so we don't have to guess what you've tried and not ?

It does if you mess around with sys.path between doing two imports of the
same thing (at least I found out the hard way on Python 2.4). I'm not sure
this is considered a bug or a "don't do that then" problem.
it's not a problem at all, because "doing two imports of the same thing"
will fetch the second thing from the module cache, no matter what you do
to the path.

$ more module.py
# simulate creating a new object
print "CREATE NEW OBJECT"

$ python
>>import sys
import module
CREATE NEW OBJECT
>>sys.path.insert(0, "foobar")
import module
sys.path = None
import module
if you got some other result, you didn't just import the same thing twice...

</F>

Jul 13 '06 #8
Got crystal clear. Thanks a lot to all for the elaborated replies.

Sanjay

Jul 14 '06 #9
Fredrik Lundh wrote:
if you got some other result, you didn't just import the same thing
twice...
I think you may be incorrect, or I have misinterpreted you.

Try this:

** In test.py ********************
import sys

import foo.bar

print foo.bar.myvar
foo.bar.myvar = 42
print foo.bar.myvar

sys.path.insert(0, 'foo')
import bar

print bar.myvar

** In foo/__init__.py ************
# this is blank

** In foo/bar.py *****************
myvar = 10
If you run test.py, then you get the output
10
42
10

When I would have expected 10, 42, 42. The bar module gets imported twice,
once as foo.bar and secondly as bar. The value of 42 in myvar does not get
retained, as there are two copies of the module imported.

--
Jeremy Sanders
http://www.jeremysanders.net/
Jul 14 '06 #10
Jeremy Sanders wrote:
>if you got some other result, you didn't just import the same thing
twice...

I think you may be incorrect, or I have misinterpreted you.
you've misinterpreted what Python means by "a module".
Try this:
import foo.bar
here you import the module named "foo.bar"
print foo.bar.myvar
foo.bar.myvar = 42
print foo.bar.myvar

sys.path.insert(0, 'foo')
import bar
here you import the module named "bar".

thanks to your path munging, that happens to point to the same file, but
Python's import system doesn't give a damn about that. it identifies
modules by their *names*, not their file system location.
When I would have expected 10, 42, 42. The bar module gets imported twice,
once as foo.bar and secondly as bar. The value of 42 in myvar does not get
retained, as there are two copies of the module imported.
no, the "bar.py" *file* gets loaded twice, first as the "foo.bar"
module, and then as the "bar" module.

</F>

Jul 14 '06 #11
Fredrik Lundh wrote:
no, the "bar.py" *file* gets loaded twice, first as the "foo.bar"
module, and then as the "bar" module.
True and I agree with your email, but suppose there is bar1.py and bar2.py
in foo, then they can refer to each other by importing bar2 and bar1,
respectively. These module objects will be the same modules that test.py
would get by importing foo.bar1 and foo.bar2.

By analogy you might expect the path munging example to work, but the
details are in the nitty-gritty of how python importing works. The import
docs say something like "Details of the module searching and loading
process are implementation and platform specific". The results can be a
little suprising! It would be include a check so that you could get a
warning with debugging switched on.

--
Jeremy Sanders
http://www.jeremysanders.net/
Jul 14 '06 #12

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

Similar topics

33
by: aa | last post by:
I am migrating to PHP from ASP where there are the Application Scope variables which are accessible from any page on a website and which are used, in particular, for hit counters. Is there a similar...
9
by: J. Baute | last post by:
I'm caching data in the Application object to speed up certain pages on a website The main reason is that the retrieval of this data takes quite a while (a few seconds) and fetching the same data...
4
by: Bob Day | last post by:
Using VS 2003, VB.net... I am confused about the Application.Exit method, where the help states "This method does not force the application to exit." Aside from the naming confusion, how do I...
6
by: farmer | last post by:
Compiler Error Message: CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement And: (int)Application++; Makes no sense.
6
by: Josef Brunner | last post by:
Hi, I published my application (VS 2005) and am now trying to install it when I get this error message. It worked before...even on a different machine. Here is the detailed description: ...
1
by: =?Utf-8?B?VGFvZ2U=?= | last post by:
Hi All, When I use applcation.exit() in winForm application, the form closed, but the process is still going!! ( The debug process is still running if debug in VS IDE). Environment.Exit(0) works...
0
by: Tifer | last post by:
Hello, I am building my first .Net Application. The first couple of Publish and Installs I did went fine. But after a couple of builds, I get a modal dialogue box error every time upon trying...
0
by: coopdog | last post by:
This is a new issue as of an install to sp1 on vb express 2005. When I publish the application to my drive then I try to install it is wants to be installed from the same location as it was...
0
by: Andrus | last post by:
I created .NET 3.5 SP1 Winforms application setup by pressing publish button in VCSE 2008 SP1 Running created setup.exe in same computer causes error below "Reference in the manifest does not...
5
by: Oriane | last post by:
Hi, I'm developinf an Asp.Net 3.5 site on W2k3, and I use a Web application Visual Studio 2008 project. This Web app is configured on IIS 6 as an "application" under a web site. When I start...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.