Re: How to get current module object

2008-02-19 Thread Gabriel Genellina
On 19 feb, 03:33, Alex <[EMAIL PROTECTED]> wrote:
> GabrielGenellinawrote:
> > En Mon, 18 Feb 2008 14:49:02 -0200, Alex <[EMAIL PROTECTED]> escribió:
> >> That's what I've been searching for, thanks. By the way, I know it might
> >> be trivial question... but function and class namespaces have __name__
> >> attribute too. Why is global one always returned?
> > I don't understand the question (even with the later correction  
> > namespaces->objects)
>
> There's no question anymore, I just failed to distinguish function local
> variables (which don't include __name__) and function object's attributes>>> 
> Why do you want to get the module object? globals() returns the module
> >>> namespace, its __dict__, perhaps its only useful attribute...
>
> >> To pass it as a parameter to a function (in another module), so it can
> >> work with several modules ("plugins" for main program) in a similar  
> >> manner.
>
> > The function could receive a namespace to work with (a dictionary). Then  
> > you just call it with globals() == the namespace of the calling module.
>
> Yes, but access to module seems more verbose:
>
>  >>> module_dict['x']()
> xxx
>
> Instead of just:
>
>  >>> module.x()
> xxx

You could write a wrapper class on the client side, but I guess it's
easier to pass the module object directly, as you said earlier.
Anyway, this class would fake attribute access for dictionary entries:

py> class Idx2Attr(object):
...   def __init__(self, d): self.__dict__[None] = d
...   def __getattr__(self, name):
... try: return self.__dict__[None][name]
... except KeyError: raise NameError, name
...   def __setattr__(self, name, value):
... self.__dict__[None][name]=value
...
py> import htmllib
py> dir(htmllib)
['AS_IS', 'HTMLParseError', 'HTMLParser', '__all__', '__buil
tins__', '__doc__', '__file__', '__name__', 'sgmllib', 'test
']
py> mod = Idx2Attr(htmllib.__dict__) # htmllib.__dict__ is what you
get if you use globals() inside htmllib
py> mod.__file__
'C:\\APPS\\PYTHON25\\lib\\htmllib.pyc'
py> mod.foo = 3
py> htmllib.foo
3

([None] is to avoid name collisions to some extent)

--
Gabriel Genellina
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get current module object

2008-02-18 Thread Alex
Gabriel Genellina wrote:
> En Mon, 18 Feb 2008 14:49:02 -0200, Alex <[EMAIL PROTECTED]> escribió:
>> That's what I've been searching for, thanks. By the way, I know it might
>> be trivial question... but function and class namespaces have __name__
>> attribute too. Why is global one always returned?
> I don't understand the question (even with the later correction  
> namespaces->objects)
There's no question anymore, I just failed to distinguish function local 
variables (which don't include __name__) and function object's attributes
>>> Why do you want to get the module object? globals() returns the module
>>> namespace, its __dict__, perhaps its only useful attribute...
>>>   
>> To pass it as a parameter to a function (in another module), so it can
>> work with several modules ("plugins" for main program) in a similar  
>> manner.
>> 
>
> The function could receive a namespace to work with (a dictionary). Then  
> you just call it with globals() == the namespace of the calling module.
Yes, but access to module seems more verbose:

 >>> module_dict['x']()
xxx

Instead of just:

 >>> module.x()
xxx
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get current module object

2008-02-18 Thread Gabriel Genellina
En Mon, 18 Feb 2008 14:49:02 -0200, Alex <[EMAIL PROTECTED]> escribió:
> Gabriel Genellina wrote:

> That's what I've been searching for, thanks. By the way, I know it might
> be trivial question... but function and class namespaces have __name__
> attribute too. Why is global one always returned?

I don't understand the question (even with the later correction  
namespaces->objects)

>> Why do you want to get the module object? globals() returns the module
>> namespace, its __dict__, perhaps its only useful attribute...
> To pass it as a parameter to a function (in another module), so it can
> work with several modules ("plugins" for main program) in a similar  
> manner.

The function could receive a namespace to work with (a dictionary). Then  
you just call it with globals() == the namespace of the calling module.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get current module object

2008-02-18 Thread Alex
Alex wrote:
> function and class namespaces have __name__ attribute too
I was wrong - these were function and class _objects'_ namespaces
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get current module object

2008-02-18 Thread Alex
Gabriel Genellina wrote:
> En Sun, 17 Feb 2008 16:25:44 -0200, Alex <[EMAIL PROTECTED]> escribi�:
>   
>> Can I get reference to module object of current module (from which the
>> code is currently executed)? I know __import__('filename') should
>> probably do that, but the call contains redundant information (filename,
>> which needs to be updated), and it'll perform unnecessary search in
>> loaded modules list.
>>
>> It shouldn't be a real problem (filename can probably be extracted from
>> the traceback anyway), but I wonder if there is more direct and less
>> verbose way.
>> 
> sys.modules[__name__]
>   
That's what I've been searching for, thanks. By the way, I know it might 
be trivial question... but function and class namespaces have __name__ 
attribute too. Why is global one always returned?
> Why do you want to get the module object? globals() returns the module  
> namespace, its __dict__, perhaps its only useful attribute...
To pass it as a parameter to a function (in another module), so it can 
work with several modules ("plugins" for main program) in a similar manner.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to get current module object

2008-02-17 Thread Gabriel Genellina
En Sun, 17 Feb 2008 16:25:44 -0200, Alex <[EMAIL PROTECTED]> escribi�:

> Can I get reference to module object of current module (from which the
> code is currently executed)? I know __import__('filename') should
> probably do that, but the call contains redundant information (filename,
> which needs to be updated), and it'll perform unnecessary search in
> loaded modules list.
>
> It shouldn't be a real problem (filename can probably be extracted from
> the traceback anyway), but I wonder if there is more direct and less
> verbose way.

sys.modules[__name__]

Why do you want to get the module object? globals() returns the module  
namespace, its __dict__, perhaps its only useful attribute...

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to get current module object

2008-02-17 Thread John Machin
On Feb 18, 5:25 am, Alex <[EMAIL PROTECTED]> wrote:
> Can I get reference to module object of current module (from which the
> code is currently executed)? I know __import__('filename') should
> probably do that, but the call contains redundant information (filename,
> which needs to be updated), and it'll perform unnecessary search in
> loaded modules list.
>
> It shouldn't be a real problem (filename can probably be extracted from
> the traceback anyway), but I wonder if there is more direct and less
> verbose way.
>

Try this:

C:\junk>type whoami.py
def showme():
import sys
modname = globals()['__name__']
print repr(modname)
module = sys.modules[modname]
print repr(module)
print dir(module)


C:\junk>python
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import whoami
>>> whoami.showme()
'whoami'

['__builtins__', '__doc__', '__file__', '__name__', 'showme']
>>>


-- 
http://mail.python.org/mailman/listinfo/python-list


How to get current module object

2008-02-17 Thread Alex
Can I get reference to module object of current module (from which the 
code is currently executed)? I know __import__('filename') should 
probably do that, but the call contains redundant information (filename, 
which needs to be updated), and it'll perform unnecessary search in 
loaded modules list.

It shouldn't be a real problem (filename can probably be extracted from 
the traceback anyway), but I wonder if there is more direct and less 
verbose way.

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list