namespaces

P

Peter Otten

Paolino said:
Why descriptor mechanism doesn't apply to modules?

Because modules are instances of the module class and the descriptor has to
be defined in the class in order to work with the instance. E. g.:
.... def __get__(*args): print "__get__%r" % (args,)
........ descr = Descr()
....
[/QUOTE]

Peter
 
P

Paolino

Why descriptor mechanism doesn't apply to modules?

I suspect another anomaly in the namespaces implementations.
class Mosse(object):
def __get__(self,*_):
print 'HERE'

m=Mosse()
m # doesn't work

import new
mod=new.module('hopeless')
mod.m=Mosse()
assert 'm' in mod.__dict__ # this is said to be the key for descriptors
# to be called
mod.m # doesn't work



Thanks Paolino


___________________________________
Yahoo! Messenger: chiamate gratuite in tutto il mondo
http://it.beta.messenger.yahoo.com
 
P

Paolino

Peter said:
Paolino wrote:




Because modules are instances of the module class and the descriptor has to
be defined in the class in order to work with the instance. E. g.:
Got it,thanks.
Then there is no way of having descriptors at module level,as 'module'
class is not updatable.





___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it
 
S

Scott David Daniels

Paolino said:
Then there is no way of having descriptors at module level,as 'module'
class is not updatable.

Well, an entry in the dictionary "sys.modules" is what is returned by
imports, so you could either pre-replace or post-replace a module by
an object which uses the descriptor technique to get to some (but not
necessarily all) of the attributes. Think of this technique as a
hack to get to a goal, rather than a good technique to use; good for
debugging, not so nice for production work. If you still don't know
how to do this from this admittedly sketchy description, I'd suggest
you avoid trying it altogether.

--Scott David Daniels
 
B

Bengt Richter

Well, an entry in the dictionary "sys.modules" is what is returned by
imports, so you could either pre-replace or post-replace a module by
an object which uses the descriptor technique to get to some (but not
necessarily all) of the attributes. Think of this technique as a
hack to get to a goal, rather than a good technique to use; good for
debugging, not so nice for production work. If you still don't know
how to do this from this admittedly sketchy description, I'd suggest
you avoid trying it altogether.
I had the thought a while ago that it might be interesting to have
an import variant that could produce a subclassed module, where you
indicate the import name as usual (for __import__) and also supply
the subclass source text as an argument (string or file).
E.g., something like

subclass_source = """\
class MyModule(math):
twopi = property(lambda: math.pi*2.0')
"""

module_with_property = subclassing_import('math', subclass=subclass_source)

Another thought/bf would be a way to extend the attribute namespace of an arbitrary object
by chaining the attribute name spaces of a sequence of objects (this would be a language mod)
e.g., (sort of a dynamic instance attribute mixin)

obj ..= a, b, c # a sequence of objects

then

obj.x # looks for obj.x, then a.x then b.x then c.x before giving up with attribute error
obj ..=() # clears chained attribute name space ?

Then you could add a property to the namespace of a module by adding an object whose class defines
the property, like

mod ..= objhavingproperty # same tuple ambiguity as with 'somestr' % x

something analogous to += on immutables would have to be done for builtin objects I suppose.

Just adding more mulch/worms to the idea garden ;-)

Regards,
Bengt Richter
 
S

Simon Burton

I've been needing a module level __getattr__ for some c library
wrapping. This has solved the problem:

# mymod.py:
import sys
from new import module
class ModuleProxy(module):
def __init__( self, name, master ):
module.__init__( self, name )
self._master = master
self.__dict__["__all__"] = dir(master)
def __getattr__(self, name):
attr = getattr( self._master, name )
return attr

# ... end of file:
sys.modules["mymod"] = ModuleProxy("mymod",sys.modules["mymod"])


--Simon Burton
 
P

Paolino

Scott said:
Well, an entry in the dictionary "sys.modules" is what is returned by
imports, so you could either pre-replace or post-replace a module by
an object which uses the descriptor technique to get to some (but not
necessarily all) of the attributes. Think of this technique as a
hack to get to a goal, rather than a good technique to use; good for
debugging, not so nice for production work. If you still don't know
how to do this from this admittedly sketchy description, I'd suggest
you avoid trying it altogether.
Thanks.Is don't know if this is *the* way to wrap the module ?

import new,sys
class Free(new.module):
def __init__(self,moduleName):
new.module.__init__(self,moduleName)
assert moduleName in sys.modules
self.__dict__.update(sys.modules[moduleName].__dict__)
sys.modules[moduleName]=self
def desc(self):
pass



Free(__name__) #'no errors'


but 'desc' is not defined in this namespace.

Paolino





___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it
 
P

Paolino

Bengt Richter wrote:

Another thought/bf would be a way to extend the attribute namespace of an arbitrary object
by chaining the attribute name spaces of a sequence of objects (this would be a language mod)
e.g., (sort of a dynamic instance attribute mixin)

obj ..= a, b, c # a sequence of objects

then

obj.x # looks for obj.x, then a.x then b.x then c.x before giving up with attribute error
obj ..=() # clears chained attribute name space ?

Something like a __lookup__ attribute for all instances which are
namespaces.
Are all objects namespaces or have all objects a namespace?

Also syntax:
lookup(obj).append(x)

I suppose bound methods will not be found,but in that case some tolerant
unbound methods could do,naturally if the PEP on eliminating them will
be accepted.That check they do on the first parameter they push in the
function (aka 'self') is really contrasting dinamycal Python IMO.
Then you could add a property to the namespace of a module by adding an object whose class defines
the property, like

mod ..= objhavingproperty # same tuple ambiguity as with 'somestr' % x

something analogous to += on immutables would have to be done for builtin objects I suppose.
This is a little hard for me.Has it something to do with extensions also?

Regards Paolino





___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top