Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newbie namespace question

Reply
Thread Tools

Newbie namespace question

 
 
Peter Hansen
Guest
Posts: n/a
 
      12-22-2004
Brandon wrote:
> Thanks, that worked to get me past the "problem". Did you see my post
> regarding my issue? I just know that there's a "Python way" to resolve
> my issue, so if anyone has a better way, I'm really interested.
> Not only does it feel like a hack, it looks like one too! Even worse!


If you mean the one you describe in your post with the Jython
code, I can't help, mainly because I can't parse it. For
one thing I think you are using TABs instead of spaces, and
many newsreaders will eliminate leading tabs so it makes the
code pretty hard to decipher. I could probably figure it out
anyway, except that I think you have a bug as well:

#jdbc.py
class DataSource:
def __init__(self, servername):
self.servername = servername

def create(name, connectionInfo, etc):
#Call the IBM supplied WebSphere config object
AdminConfig.create('DataSource')


This is how it looked to me. The presumed bug is in the "create"
method definition, where you are not supplying a "self" parameter.
At least, I assume that is a method of DataSource and not
a module-level function, because in the previous code you were
trying to call ds.create() where ds was a DataSource object.

I'd probably have some ideas if you reposted with leading
spaces and explained or fixed the supposed bug.

-Peter
 
Reply With Quote
 
 
 
 
Brandon
Guest
Posts: n/a
 
      12-22-2004
Peter,

You're correct about the bug. I did need a 'self' parm... I was just
winging the example because the actual code is pretty large. I'm using
google groups for my posting and it didn't carry spaces through (I did
use spaces and not tabs).

The "fix" or workaround was to import __builtin__ and add the
AdminConfig reference there in configure_server_foo.py as follows:

import __builtin__
__builtin__.AdminConfig = AdminConfig

As for the indentations, substitute ~ with a space.

Hopefully, a bug free and "indented" version.

#jdbc.py
class DataSource:
~~~def __init__(self, servername):
~~~~~~self.servername = servername

~~~def create(self, name, connectionInfo, etc):
~~~~~~#Call the IBM supplied WebSphere config object
~~~~~~AdminConfig.create('DataSource')

 
Reply With Quote
 
 
 
 
Nick Coghlan
Guest
Posts: n/a
 
      12-23-2004
Brandon wrote:
> Peter,
>
> You're correct about the bug. I did need a 'self' parm... I was just
> winging the example because the actual code is pretty large. I'm using
> google groups for my posting and it didn't carry spaces through (I did
> use spaces and not tabs).
>
> The "fix" or workaround was to import __builtin__ and add the
> AdminConfig reference there in configure_server_foo.py as follows:
>
> import __builtin__
> __builtin__.AdminConfig = AdminConfig
>
> As for the indentations, substitute ~ with a space.
>
> Hopefully, a bug free and "indented" version.
>
> #jdbc.py
> class DataSource:
> ~~~def __init__(self, servername):
> ~~~~~~self.servername = servername
>
> ~~~def create(self, name, connectionInfo, etc):
> ~~~~~~#Call the IBM supplied WebSphere config object
> ~~~~~~AdminConfig.create('DataSource')
>


Is there any particular reason DataSource can't be modified to accept a
reference to the AdminConfig as a constructor argument? Or as a class attribute?

Alternatively, here's another trick to 'nicely' set a module global from outside
a module:

#jdbc.py
def setAdminConfig(adm_cfg):
global AdminConfig
AdminConfig = adm_cfg

However, if you would prefer not to alter jdbc.py, consider trying:

import jdbc
jdbc.AdminConfig = AdminConfig


Global variables are bad karma to start with, and monkeying with __builtin__ is
even worse

Cheers,
Nick.

--
Nick Coghlan | http://www.velocityreviews.com/forums/(E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
Brandon
Guest
Posts: n/a
 
      12-23-2004
I did the constructor thing and just didn't like it, it didn't feel
clean (I know, I know and monkeying with __builtin__ is?)

As for global, that will just make it modlue-level global (I think?)
and I have this reference in multiple modules. I think I tried it
already, but I can't remember for sure... Anyway, all of this got me
brainstorming and I think I've got a solution that I think is clean and
will be effective, I'll bust it out and post it here for comments.
Thanks for your input!

 
Reply With Quote
 
Nick Coghlan
Guest
Posts: n/a
 
      12-23-2004
Brandon wrote:
> I did the constructor thing and just didn't like it, it didn't feel
> clean (I know, I know and monkeying with __builtin__ is?)
>
> As for global, that will just make it modlue-level global (I think?)
> and I have this reference in multiple modules. I think I tried it
> already, but I can't remember for sure... Anyway, all of this got me
> brainstorming and I think I've got a solution that I think is clean and
> will be effective, I'll bust it out and post it here for comments.
> Thanks for your input!
>

Another option for you to consider:

#__main__ ( I forget the script's name)
import app_config
app_config.setConfiguration(AdminConfig)

from jdbc import DataStore
. . .etc


#jdbc.py
from app_config import AdminConfig
.. . .etc


#app_config.py
def setConfiguration(adm_cfg):
global AdminConfig
AdminConfig = adm_cfg


The reason the import solution was breaking for you earlier was that you were
setting up circular imports by using your main script as the holder for the
global configuration.

By creating a special 'configuration data' module, you can use that to store the
data, and every module that needs it can import it without worrying about
circular imports.

Any extra global configuration properties can be added by updating the
'setConfiguration' function.

Cheers,
Nick.

--
Nick Coghlan | (E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      12-24-2004
Peter Hansen <(E-Mail Removed)> wrote:

> I'm not sure what you think that does, but I don't
> think it does it.


QOTW +1 !!!


Alex
 
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
ERROR CS0234: The type or namespace name 'DataAccessHelper' does not exist in the namespace 'BCC' (are you missing an assembly reference?) li.eddie@gmail.com ASP .Net 0 01-06-2006 11:31 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Reaching into the default namespace when using another namespace. Jason Heyes C++ 1 11-19-2004 02:36 AM
Namespace: Is it a scope or a namespace? Anonymous C++ 3 08-18-2003 01:31 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. ASP .Net 1 07-29-2003 04:31 PM



Advertisments