473,545 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
+ 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 1465
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__.Modul e'>)


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.module s" 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.module s" 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_pro perty = subclassing_imp ort('math', subclass=subcla ss_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 ..= objhavingproper ty # 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************ *************** ************@py thon.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**@pythoncra ft.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(mod ule):
def __init__( self, name, master ):
module.__init__ ( self, name )
self._master = master
self.__dict__["__all__"] = dir(master)
def __getattr__(sel f, name):
attr = getattr( self._master, name )
return attr

# ... end of file:
sys.modules["mymod"] = ModuleProxy("my mod",sys.module s["mymod"])
--Simon Burton

Aug 10 '05 #7
Scott David Daniels wrote:
Well, an entry in the dictionary "sys.module s" 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,m oduleName):
new.module.__in it__(self,modul eName)
assert moduleName in sys.modules
self.__dict__.u pdate(sys.modul es[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).app end(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 ..= objhavingproper ty # 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
3020
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 a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less...
24
3478
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
1581
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 What's the difference here? Are there right and wrong namespaces? Is there a global list of the ones to use? Does the namespace even need to point...
3
2313
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. NameSpaces and Java Packages are similar in nature? In the dot net world, you are not able to pick out (import) a particulare "class" within the...
17
2057
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 write a signature for the developer and after getting a good start developing a 2.0 application I thought I should go through the code and start marking...
11
1742
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 understand that you declare your intention to use a namespace within a page through the "Inherits" attribute. I know that using "Inherits" isn't...
0
7459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7411
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7749
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4942
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3444
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
695
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.