__getattr__ functionality for modules? 
Author Message
 __getattr__ functionality for modules?

Is there a way to achieve for modules what overriding __getattr__ does
for classes?

My problem: I want an infinite number of identifiers appear as if they
were defined in the global namespace.

The identifiers have structured names (say, id2m5, id2m6, id3m2, ...),
and I'd like to generate the respective values on the fly and cache
them for subsequent lookups.

If they were attributes of an object it would be trivial, but how do I
achieve this for name lookups in the __main__ (or any other) module?

Stefan



Fri, 04 Nov 2005 05:40:46 GMT  
 __getattr__ functionality for modules?

Quote:

> Is there a way to achieve for modules what overriding __getattr__ does
> for classes?

> My problem: I want an infinite number of identifiers appear as if they
> were defined in the global namespace.

> The identifiers have structured names (say, id2m5, id2m6, id3m2, ...),
> and I'd like to generate the respective values on the fly and cache
> them for subsequent lookups.

> If they were attributes of an object it would be trivial, but how do I
> achieve this for name lookups in the __main__ (or any other) module?

> Stefan

My experience is that getattr works on modules other than __main__.  For
__main__, I suppose you could use the globals() dictionary.

Alex

Python 2.2.2 (#1, May 11 2003, 11:57:38)
[GCC 3.2.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Quote:
>>> import os
>>> getattr(os, 'path')

<module 'posixpath' from '/usr/lib/python2.2/posixpath.pyc'>
Quote:

>>> class A:

...   pass
...
Quote:
>>> globals()['A']

<class __main__.A at 0x8125084>
Quote:
>>> globals()['B']='this is a test'
>>> B
'this is a test'



Fri, 04 Nov 2005 06:11:25 GMT  
 __getattr__ functionality for modules?

Quote:


>> Is there a way to achieve for modules what overriding __getattr__ does
>> for classes?

>> My problem: I want an infinite number of identifiers appear as if they
>> were defined in the global namespace.

>> The identifiers have structured names (say, id2m5, id2m6, id3m2, ...),
>> and I'd like to generate the respective values on the fly and cache
>> them for subsequent lookups.

>> If they were attributes of an object it would be trivial, but how do I
>> achieve this for name lookups in the __main__ (or any other) module?

>> Stefan

> My experience is that getattr works on modules other than __main__.  For
> __main__, I suppose you could use the globals() dictionary.

> Alex

> python 2.2.2 (#1, May 11 2003, 11:57:38)
> [GCC 3.2.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import os
>>>> getattr(os, 'path')
> <module 'posixpath' from '/usr/lib/python2.2/posixpath.pyc'>

>>>> class A:
> ...   pass
> ...
>>>> globals()['A']
> <class __main__.A at 0x8125084>
>>>> globals()['B']='this is a test'
>>>> B
> 'this is a test'

PS: I have to ask, why do you want these not encapsulate the identifiers in
a class?


Fri, 04 Nov 2005 06:18:39 GMT  
 __getattr__ functionality for modules?

Quote:



>>> Is there a way to achieve for modules what overriding __getattr__ does
>>> for classes?

>>> My problem: I want an infinite number of identifiers appear as if they
>>> were defined in the global namespace.

>>> The identifiers have structured names (say, id2m5, id2m6, id3m2, ...),
>>> and I'd like to generate the respective values on the fly and cache
>>> them for subsequent lookups.

>>> If they were attributes of an object it would be trivial, but how do I
>>> achieve this for name lookups in the __main__ (or any other) module?

>>> Stefan

>> My experience is that getattr works on modules other than __main__.  For
>> __main__, I suppose you could use the globals() dictionary.

>> Alex

>> Python 2.2.2 (#1, May 11 2003, 11:57:38)
>> [GCC 3.2.2] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> import os
>>>>> getattr(os, 'path')
>> <module 'posixpath' from '/usr/lib/python2.2/posixpath.pyc'>

>>>>> class A:
>> ...   pass
>> ...
>>>>> globals()['A']
>> <class __main__.A at 0x8125084>
>>>>> globals()['B']='this is a test'
>>>>> B
>> 'this is a test'

> PS: I have to ask, why do you want these not encapsulate the identifiers
> in a class?

That last one accidental got sent of before it finished:  It should of read:
I have to ask, why not encapsulate the identifiers in a class?


Fri, 04 Nov 2005 06:21:49 GMT  
 __getattr__ functionality for modules?


Quote:

>Is there a way to achieve for modules what overriding __getattr__ does
>for classes?

Nope, and Guido is {*filter*}ly opposed to making this possible.  Why not
just use a class?

(To Alex, Stefan isn't about using getattr(), he's asking about
<module>.__getattr__())
--

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles:  boring syntax, unsurprising semantics,
few automatic coercions, etc etc.  But that's one of the things I like
about it."  --Tim Peters on Python, 16 Sep 93



Fri, 04 Nov 2005 06:55:20 GMT  
 __getattr__ functionality for modules?

...

Quote:
>Nope, and Guido is {*filter*}ly opposed to making this possible.  Why not
>just use a class?

Because a namespace object behaves different when it comes to setting
attributes: There are no nested scopes.

What I'm trying to realize is a small domain specific configuration
language: A subset of Python that consists of nothing else but

- a couple of functions,

- the ability to define your own functions with 'def' with correct scope nesting

- Python's numeric expressions

- and an (unlimited) number of builtin constants. That's what I need
  the module getattr hook for

This is absolutely trivial to learn for the users in my context. Using
objects and attributes here adds an unnecessary (and unwanted)
level of complexity.

Stefan



Fri, 04 Nov 2005 07:34:30 GMT  
 __getattr__ functionality for modules?


Quote:

>>Nope, and Guido is {*filter*}ly opposed to making this possible.  Why not
>>just use a class?

>Because a namespace object behaves different when it comes to setting
>attributes: There are no nested scopes.

>What I'm trying to realize is a small domain specific configuration
>language: A subset of Python that consists of nothing else but

>- a couple of functions,

>- the ability to define your own functions with 'def' with correct scope nesting

>- Python's numeric expressions

>- and an (unlimited) number of builtin constants. That's what I need
>  the module getattr hook for

>This is absolutely trivial to learn for the users in my context. Using
>objects and attributes here adds an unnecessary (and unwanted)
>level of complexity.

But you can import their module and attach the functions to a class
object.  They don't need to know that.  ;-)
--

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles:  boring syntax, unsurprising semantics,
few automatic coercions, etc etc.  But that's one of the things I like
about it."  --Tim Peters on Python, 16 Sep 93



Fri, 04 Nov 2005 08:58:48 GMT  
 __getattr__ functionality for modules?

Quote:

> (To Alex, Stefan isn't about using getattr(), he's asking about
> <module>.__getattr__())

DOH!

Yes, my mistake.  Thanks...



Fri, 04 Nov 2005 11:38:18 GMT  
 __getattr__ functionality for modules?

Quote:


> ...
>>Nope, and Guido is {*filter*}ly opposed to making this possible.  Why not
>>just use a class?

> Because a namespace object behaves different when it comes to setting
> attributes: There are no nested scopes.

> What I'm trying to realize is a small domain specific configuration
> language: A subset of Python that consists of nothing else but

> - a couple of functions,

> - the ability to define your own functions with 'def' with correct scope nesting

> - Python's numeric expressions

> - and an (unlimited) number of builtin constants. That's what I need
>   the module getattr hook for

> This is absolutely trivial to learn for the users in my context. Using
> objects and attributes here adds an unnecessary (and unwanted)
> level of complexity.

This is a wild shot, but maybe what you're looking for is the so-called
sandbox functionality?  There used to be a (buggy) module for it called
rexec, but for various reasons it is no longer part of the official
standard lib of v2.3.

A small demonstration:

Python 2.3b1 (#1, Apr 27 2003, 22:07:38)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Quote:
>>> sandbox = {'__builtins__':{}}
>>> sandbox['a'] = 42
>>> exec 'print a' in sandbox
42
>>> exec 'print len("foo")' in sandbox

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 1, in ?
NameError: name 'len' is not defined

The latter fails because the __builtins__ dict in the sandbox doesn't
contain the name 'len'... we can add Python builtins thus:

Quote:
>>> import __builtin__
>>> sandbox['__builtins__']['len'] = __builtin__.len
>>> exec 'print len("foo")' in sandbox

3

The eval() and execfile() builtins can operate on a custom namespace dict,
too.  

Hope this helps,

Troels Therkelsen



Fri, 04 Nov 2005 19:07:52 GMT  
 __getattr__ functionality for modules?

Quote:
>This is a wild shot, but maybe what you're looking for is the so-called
>sandbox functionality?  There used to be a (buggy) module for it called
>rexec, but for various reasons it is no longer part of the official
>standard lib of v2.3.

[...]

Hmm, apart from the fact that the module is deprecated: How would I
enable this functionality on the command line. That would require
a Python shell within Python, wouldn't it?

Being able to replace __builtin__ with a dict-like object for instance
would much better appeal to my taste..

/s



Sat, 05 Nov 2005 01:22:44 GMT  
 __getattr__ functionality for modules?

Quote:
> What I'm trying to realize is a small domain specific configuration
> language: A subset of Python that consists of nothing else but

> - a couple of functions,

> - the ability to define your own functions with 'def' with correct scope
nesting

> - Python's numeric expressions

> - and an (unlimited) number of builtin constants. That's what I need
>   the module getattr hook for

> This is absolutely trivial to learn for the users in my context. Using
> objects and attributes here adds an unnecessary (and unwanted)
> level of complexity.

> Stefan

this sounds exactly what lua was designed for and might be a bit easier to
achieve what you want.
http://www.lua.org/

bryan



Sat, 12 Nov 2005 04:10:05 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. ANN:modeler __getattr__ for modules

2. Checking for attribute in the current module with getattr( )

3. Is there a module __getattr__ function?

4. modeler: __getattr__ for modules

5. Modules with __getattr__ (Re: PEP 317: Eliminate Implicit Exc eption Instantiation)

6. Can I use the getattr functionality in a module?

7. Functionality of C headers / module interfaces

8. expect module and/or functionality? Or SSH wrapper module?

9. Inefficiency of __getattr__

10. getattr on objects

11. missing methods or __getattr__ problem

12. Question about __getattribute__/__getattr__

 

 
Powered by phpBB® Forum Software