Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > best way to share an instance of a class among modules?

Reply
Thread Tools

best way to share an instance of a class among modules?

 
 
CM
Guest
Posts: n/a
 
      02-06-2013
I have recently moved all my SQLite3 database-related functions into a
class, DatabaseAccess, that lives in a "utilities" module. When the
application loads, the namespace of the instance of the class is
populated with two different cursors for two different databases the
whole application needs to use. One of those cursors always refers to
a connection to the same database, but the other cursor can change
which database it refers to; in this regard, the namespace of the
DatabaseAccess class instance holds a "state" (which is which database
the one cursor currently refers to)

I have a number of modules that all need to use those two cursors.
However, if I import the utilities module and create a new instance of
the DatabasesAccess() in each of all the various modules, obviously
each new instance doesn't have the same namespace as the first
instance, and so doesn't have the *same* two cursors.

I can think of a few ways to pass the DatabaseAcess instance around to
the various modules, but all of them seem like repeating myself and
clunky. What's the "best" (most Pythonic, simplest) way for a number
of modules to all share the same instance of my DatabaseAccess class?

Thanks,
Che
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      02-06-2013
On 2/5/2013 11:40 PM, CM wrote:
> I have recently moved all my SQLite3 database-related functions into a
> class, DatabaseAccess, that lives in a "utilities" module. When the
> application loads, the namespace of the instance of the class is
> populated with two different cursors for two different databases the
> whole application needs to use. One of those cursors always refers to
> a connection to the same database, but the other cursor can change
> which database it refers to; in this regard, the namespace of the
> DatabaseAccess class instance holds a "state" (which is which database
> the one cursor currently refers to)
>
> I have a number of modules that all need to use those two cursors.
> However, if I import the utilities module and create a new instance of
> the DatabasesAccess() in each of all the various modules, obviously
> each new instance doesn't have the same namespace as the first
> instance, and so doesn't have the *same* two cursors.
>
> I can think of a few ways to pass the DatabaseAcess instance around to
> the various modules, but all of them seem like repeating myself and
> clunky. What's the "best" (most Pythonic, simplest) way for a number
> of modules to all share the same instance of my DatabaseAccess class?


Attach the instance of the class to the utilities module that clients
import. It does not matter if the code is in the utilities module itself
or in one of the importing modules (possibly the first).

# in module
shared_cursor =

# in importer
import utilities
utilities.shared_cursor = DatabaseAccess(args)

Module attributes are mutable and can be set from elsewhere.

--
Terry Jan Reedy

 
Reply With Quote
 
 
 
 
CM
Guest
Posts: n/a
 
      02-06-2013
On Feb 6, 12:04*am, Terry Reedy <(E-Mail Removed)> wrote:
> On 2/5/2013 11:40 PM, CM wrote:
>
>
>
>
>
>
>
>
>
> > I have recently moved all my SQLite3 database-related functions into a
> > class, DatabaseAccess, that lives in a "utilities" module. *When the
> > application loads, the namespace of the instance of the class is
> > populated with two different cursors for two different databases the
> > whole application needs to use. *One of those cursors always refers to
> > a connection to the same database, but the other cursor can change
> > which database it refers to; in this regard, the namespace of the
> > DatabaseAccess class instance holds a "state" (which is which database
> > the one cursor currently refers to)

>
> > I have a number of modules that all need to use those two cursors.
> > However, if I import the utilities module and create a new instance of
> > the DatabasesAccess() in each of all the various modules, obviously
> > each new instance doesn't have the same namespace as the first
> > instance, and so doesn't have the *same* two cursors.

>
> > I can think of a few ways to pass the DatabaseAcess instance around to
> > the various modules, but all of them seem like repeating myself and
> > clunky. *What's the "best" (most Pythonic, simplest) way for a number
> > of modules to all share the same instance of my DatabaseAccess class?

>
> Attach the instance of the class to the utilities module that clients
> import. It does not matter if the code is in the utilities module itself
> or in one of the importing modules (possibly the first).
>
> # in module
> shared_cursor =
>
> # in importer
> import utilities
> utilities.shared_cursor = DatabaseAccess(args)
>
> Module attributes are mutable and can be set from elsewhere.
>
> --
> Terry Jan Reedy


Thank you. But, I'm sorry, I'm not following this enough to get it to
work. Shouldn't it be a little more like this:

# in utilities module
shared_cursor = DatabaseAccess_instance #but how? see my question
below...

# in importer
import utilities
self.shared_cursor = utilities.shared_cursor ("self" is here to make
cursor available to all functions in importer

My only problem, then, is I create the shared_cursor from within a
function within the instance of DatabaseAccess(). How then do I pass
it from within the function's namespace to the module's namespace so
that I can do that first line?

 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      02-07-2013
On 02/06/2013 03:41 PM, CM wrote:
> Thank you. But, I'm sorry, I'm not following this enough to get it to
> work. Shouldn't it be a little more like this:


No, not exactly.

>
> # in utilities module
> shared_cursor = DatabaseAccess_instance #but how? see my question
> below...


How what?

> # in importer
> import utilities
> self.shared_cursor = utilities.shared_cursor ("self" is here to make
> cursor available to all functions in importer


Umm no. For one you're using self incorrectly. For two, it already is
visible to all functions in the module. You just have to refer to it as
"utilities.shared_cursor."

> My only problem, then, is I create the shared_cursor from within a
> function within the instance of DatabaseAccess(). How then do I pass
> it from within the function's namespace to the module's namespace so
> that I can do that first line?


Every function in a module has access to the module's global namespace.
And your shared_cursor is there, inside of the utilities reference,
since utilities was imported into your module, "importer."
 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      02-07-2013
On 02/06/2013 05:03 PM, Michael Torrie wrote:
> Every function in a module has access to the module's global namespace.
> And your shared_cursor is there, inside of the utilities reference,
> since utilities was imported into your module, "importer."


Just to be clear:

mod1.py:
a=5

------------------------
mod2.py:
import mod1

def testfunc1():
print mod1.a

def testfunc2():
mod1.a = 6

-------------------------
main.py:
import mod1
import mod2

mod2.testfunc1()
mod2.testfunc2()
mod2.testfunc1()

-----------------------

The main.py program will print out "5" and "6."

If I understand your original question, this example will demonstrate
it. But I could have understood wrong!



 
Reply With Quote
 
c
Guest
Posts: n/a
 
      02-07-2013
On Feb 6, 7:03*pm, Michael Torrie <(E-Mail Removed)> wrote:
> On 02/06/2013 03:41 PM, CM wrote:
>
> > Thank you. *But, I'm sorry, I'm not following this enough to get it to
> > work. *Shouldn't it be a little more like this:

>
> No, not exactly.
>
>
>
> > # in utilities module
> > shared_cursor = *DatabaseAccess_instance *#but how? see my question
> > below...

>
> How what?
>
> > # in importer
> > import utilities
> > self.shared_cursor = utilities.shared_cursor *("self" is here to make
> > cursor available to all functions in importer

>
> Umm no. *For one you're using self incorrectly. *For two, it already is
> visible to all functions in the module. *You just have to refer to it as
> "utilities.shared_cursor."


I was using self correctly, I think; but I should have said that the
code in the importing module would be within a class, so self there
refers to that class. But that's a side point.

I agree that utilities.shared_cursor is visible within the importing
module. But the problem below remains for me...

> > My only problem, then, is I create the shared_cursor from within a
> > function within the instance of DatabaseAccess(). *How then do I pass
> > it from within the function's namespace to the module's namespace so
> > that I can do that first line?

>
> Every function in a module has access to the module's global namespace.
> *And your shared_cursor is there, inside of the utilities reference,
> since utilities was imported into your module, "importer."


But the function in the module is also within a *class* so I don't
think the function does have access to the module's global namespace.
Here's the hierarchy:

-- Module namespace....
---- class namespace (DatabaseAccess is the name of the class)
---- function namespace
This is where the cursor is created. How do I get it
into the module namespace?

Che
 
Reply With Quote
 
CM
Guest
Posts: n/a
 
      02-07-2013
I was using self correctly, I think; but I should have said that the
code in the importing module would be within a class, so self there
refers to that class. But that's a side point.

I agree that utilities.shared_cursor is visible within the importing
module. But the problem below remains for me...

> > My only problem, then, is I create the shared_cursor from within a
> > function within the instance of DatabaseAccess(). How then do I pass
> > it from within the function's namespace to the module's namespace so
> > that I can do that first line?


> Every function in a module has access to the module's global namespace.
> And your shared_cursor is there, inside of the utilities reference,
> since utilities was imported into your module, "importer."


But the function in the module is also within a *class* so I don't
think the function does have access to the module's global namespace.
Here's the hierarchy:

-- Module namespace....
---- class namespace (DatabaseAccess is the name of the class)
---- function namespace
This is where the cursor is created. How do I get it
into the module namespace?

 
Reply With Quote
 
Ethan Furman
Guest
Posts: n/a
 
      02-07-2013
On 02/06/2013 04:43 PM, c wrote:
> But the function in the module is also within a *class* so I don't
> think the function does have access to the module's global namespace.
> Here's the hierarchy:
>
> -- Module namespace....
> ---- class namespace (DatabaseAccess is the name of the class)
> ---- function namespace
> This is where the cursor is created. How do I get it
> into the module namespace?


In the class namespace you have something like:

self.shared_cursor = ...

to get that into the module namespace instead, just remove the 'self':

shared_cursor = ...

--
~Ethan~

 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      02-07-2013
On 02/06/2013 05:45 PM, CM wrote:
> But the function in the module is also within a *class* so I don't
> think the function does have access to the module's global namespace.
> Here's the hierarchy:
>
> -- Module namespace....
> ---- class namespace (DatabaseAccess is the name of the class)
> ---- function namespace
> This is where the cursor is created. How do I get it
> into the module namespace?


Oh I see. You're trying to set a variable in the current module's
global namespace. There's two ways to do this, the first one is cleaner
and preferred:

1. Have a function in DatabaseAccess return the cursor then then set it
to a name in code that runs at the module level.

2. use the "global" keyword in the method:

def mymethod(self):
global blah

blah = something


Then your module has has blah in it's global method now. If you want to
stick a variable in another method's global namespace, as long as you've
imported it you just go:

module.newvariable = something

 
Reply With Quote
 
Ethan Furman
Guest
Posts: n/a
 
      02-07-2013
On 02/06/2013 04:57 PM, Ethan Furman wrote:
> On 02/06/2013 04:43 PM, c wrote:
>> But the function in the module is also within a *class* so I don't
>> think the function does have access to the module's global namespace.
>> Here's the hierarchy:
>>
>> -- Module namespace....
>> ---- class namespace (DatabaseAccess is the name of the class)
>> ---- function namespace
>> This is where the cursor is created. How do I get it
>> into the module namespace?

>
> In the class namespace you have something like:
>
> self.shared_cursor = ...
>
> to get that into the module namespace instead, just remove the 'self':
>
> shared_cursor = ...


Correction:

As Michael Torrie pointed out, the 'global' keyword is needed:

global shared_cursor
shared_cursor = ...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
How to share common code among pages in asp.net 2.0 just_life ASP .Net 0 08-01-2007 05:31 PM
Share common HTML code among different HTML files? MK HTML 6 05-15-2005 11:32 PM
Share SQL DB Among Dozens of Sites Guadala Harry ASP .Net 1 02-21-2004 06:50 PM
Re: How to share vector among different processes? red floyd C++ 1 09-06-2003 11:20 AM



Advertisments