Hi, Revisiting a fairly old discussion...
Conversations in the past have occurred around this sort of thing: import time from Kamaelia.Util.Backplane import Backplane, PublishTo, SubscribeTo from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.Chassis.ServerCore import ServerCore from Kamaelia.File.Append import Append from Kamaelia.Util.PureTransformer import PureTransformer def log_events (**argd): peer = str(argd.get(peer, "")) return Pipeline( PureTransformer(lambda x: peer + “|” + x), PublishTo("UI_EVENTS") ) Backplane("UI_EVENTS").activate() Pipeline( SubscribeTo("UI_EVENTS"), PureTransformer(lambda x: str(time.time()+”|”+x), Append(filename = "userlog.txt"), ).activate() ServerCore(protocol=log_events, port=1500).run() Specifically with regard to the import lines: from Kamaelia.Util.Backplane import Backplane, PublishTo, SubscribeTo from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.Chassis.ServerCore import ServerCore from Kamaelia.File.Append import Append from Kamaelia.Util.PureTransformer import PureTransformer Comments regarding this: * "It's nice and clear precisely what's being imported" * "It's clear where they've come from" * "It's daunting for a newbie" * "How would I find the correct components myself?" http://www.kamaelia.org/Components is the "correct" answer, but the point stands. * "That hierarchy makes no sense to me, X should by in A.B.C not in A.D.E" These comments become even more important with larger applications. Similarly it was mooted that this could be simplified by flattening the hierarchy completely. The problem with this is when browsing through /Components, it can actually help alot having the hierachy, simply because hierarchies can be easier to navigate than everything in one directory. (Hence /usr/bin , /opt/<thing>/bin, /usr/local/bin, etc) Since I wasn't aware of any autoload (like perl has) functionality in python, I'd decided to leave this for now, but today I got curious about trying it again. After a bit of searching I came across a post, and played around a bit and came up with this: # mymod.py def greet(greeting="Hello World"): print greeting class autoload(object): def __init__(self, __name__): super(autoload, self).__init__() self.wrapped_name = __name__ self.wrapped = sys.modules[__name__] def __getattr__(self, name): try: return getattr(self.wrapped, name) except AttributeError: def f(): greet(name+" "+self.wrapped_name) return f if __name__ != "__main__": import sys sys.modules[__name__] = autoload(__name__) This can then be used like this: >>> from mymod import Pipeline, TCPClient, use_as_component >>> Pipeline() Pipeline mymod >>> TCPClient() TCPClient mymod >>> use_as_component() use_as_component mymod >>> This clearly allows, given some work, the above example to be rewritten as follows: import time from Kamaelia import Backplane, PublishTo, SubscribeTo, \ Pipeline, ServerCore, Append, PureTransformer def log_events (**argd): peer = str(argd.get(peer, "")) return Pipeline( PureTransformer(lambda x: peer + “|” + x), PublishTo("UI_EVENTS") ) Backplane("UI_EVENTS").activate() Pipeline( SubscribeTo("UI_EVENTS"), PureTransformer(lambda x: str(time.time()+”|”+x), Append(filename = "userlog.txt"), ).activate() ServerCore(protocol=log_events, port=1500).run() Which is IMO much more friendly. So some questions arise: 1. Good idea or not? 2. Really? Could be viewed as bad mojo & messing about one step too far. 3. If OK, should it be static? ie the list of what gets imported and handles what dealt with by a table lookup in Kamaelia/__init__.py ? 4. Or should it go "OK, I was imported here, I'll rummage around in all my subdirectories, in this overall order" 5. Do 3, then 4, if the name wasn't found in 3. 6. The other way round ? 7. Do we allow extra search paths for the case of 4 ? (think sys.path for modules) 8. How about allowing extra lookup tables to be added in the case of 3? 9. lots more possibilities. For the moment, if I was to do this I'd probably do "3" initially, then change "3" to be autogenerated (from Kamaelia.Support.Data.Repository). However, I'm pausing, since, aside from anything else, I'm not sure what level of aversion other people might have to autoloading like this. Personally I think it's a good option, but open to comments. (Tempted to give a lightning talk at europython on autoloading in general to gauge opinion) Regards, Michael. -- http://yeoldeclue.com/blog http://twitter.com/kamaelian http://www.kamaelia.org/Home --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "kamaelia" group. To post to this group, send email to kamaelia@googlegroups.com To unsubscribe from this group, send email to kamaelia+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/kamaelia?hl=en -~----------~----~----~----~------~----~------~--~---