473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1461
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.