473,491 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

namespaces

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
Aug 9 '05 #1
8 1462
Paolino wrote:
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.:
import sys
class Descr(object): .... def __get__(*args): print "__get__%r" % (args,)
.... module = type(sys)
class Module(module): .... descr = Descr()
.... m = Module("noname")
m.descr __get__(<__main__.Descr object at 0x4029372c>, <module 'noname' (built-in)>,
<class '__main__.Module'>)


Peter

Aug 9 '05 #2
Peter Otten wrote:
Paolino wrote:

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.:

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
Aug 9 '05 #3
Paolino wrote:
Peter Otten wrote:
Paolino wrote:
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....

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
Aug 9 '05 #4
On Tue, 09 Aug 2005 08:35:38 -0700, Scott David Daniels <Sc***********@Acm.Org> wrote:
Paolino wrote:
Peter Otten wrote:
Paolino wrote:
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....

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.

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
Aug 9 '05 #5
In article <ma***************************************@python. org>,
Paolino <pa*************@tiscali.it> wrote:

Why descriptor mechanism doesn't apply to modules?


The real answer: Because Guido doesn't want it to
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

The way to build large Python applications is to componentize and
loosely-couple the hell out of everything.
Aug 9 '05 #6
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

Aug 10 '05 #7
Scott David Daniels wrote:
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
Aug 10 '05 #8
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
Aug 10 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
3007
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
24
3466
by: Marcin Vorbrodt | last post by:
Here is an example of my code: //Header file #include <vector> using std::vector; namespace Revelation { // class definitions, etc... // class members are of type std::vector }
2
1576
by: Mike Morse | last post by:
What see sample that show xs:element where the xs namespace = http://www.w3.org/2001/XMLSchema However, I see another example with xsi: where xsi = http://www.w3.org/2001/XMLSchema-instance ...
3
2303
by: Jim Heavey | last post by:
Trying to get the hang of Namespaces. I have primarly developed in VB and am transitioning to C# and the .Net Environment. I have worked a bit with Java as well in school about a year or so ago....
17
2053
by: clintonG | last post by:
Using 2.0 with Master Pages and a GlobalBaseClass for the content pages. I understand the easy part -- the hierarchical structure of a namespace naming convention -- but the 2.0 IDE does not...
11
1737
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.