Altering imported modules
Hi, list. I've got a simple asyncore-based server. However, I've modified the asyncore module to allow me to watch functions as well as sockets. The modified asyncore module is in a specific location in my project and is imported as usual from my classes. Now I'd like to use the tlslite library, which includes an asyncore mixin class. However, tlslite imports "asyncore", which doesn't include my own modifications. I'd like to know if it's possible to make tlslite load *my* asyncore module without changing any of the tlslite code. Thanks, Tro -- http://mail.python.org/mailman/listinfo/python-list
Re: Book Recomendations
On Saturday 01 March 2008, Ira Solomon wrote: > I am an experienced programmer (40 years). I've done Algol (if you've > heard of that you must be old too), PL/1, VB,VBA, a little C, and a > few other odd languages (e.g. Taskmate). > I'm interested in learning Python and have downloaded a slew of books. > Too many. > I'd like a recommendation as to which books are considered to be the > cream of the crop. > I know there are tutorials on the web, but, again, I don't know the > quality. I would appreciate recommendations on those as well. The official tutorial is required reading. After that, Dive Into Python (http://diveintopython.org/). Cheers, Tro -- http://mail.python.org/mailman/listinfo/python-list
Re: Altering imported modules
On Saturday 01 March 2008, Tro wrote: > Hi, list. > > I've got a simple asyncore-based server. However, I've modified the > asyncore module to allow me to watch functions as well as sockets. The > modified asyncore module is in a specific location in my project and is > imported as usual from my classes. > > Now I'd like to use the tlslite library, which includes an asyncore mixin > class. However, tlslite imports "asyncore", which doesn't include my own > modifications. > > I'd like to know if it's possible to make tlslite load *my* asyncore module > without changing any of the tlslite code. I guess I could just copy over the relevant tlslite file that imports asyncore and change the import, but that seems clumsy. Is there no better way? Thanks, Tro -- http://mail.python.org/mailman/listinfo/python-list
Re: Altering imported modules
On Sunday 02 March 2008, Terry Reedy wrote: > "Tro" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > | Hi, list. > | > | I've got a simple asyncore-based server. However, I've modified the > > asyncore > > | module to allow me to watch functions as well as sockets. The modified > | asyncore module is in a specific location in my project and is imported > > as > > | usual from my classes. > | > | Now I'd like to use the tlslite library, which includes an asyncore mixin > | class. However, tlslite imports "asyncore", which doesn't include my own > | modifications. > | > | I'd like to know if it's possible to make tlslite load *my* asyncore > > module > > | without changing any of the tlslite code. > > If your module is also 'asyncore' and comes earlier in the search path, I > would expect the import to get yours. It's not. It has a package prefix like my.package.asyncore. I think I can either move my version of asyncore up a couple of levels or add the my.package directory to sys.path. My version of asyncore imports several functions from the built-in asyncore. Now that my version of it is imported as asyncore, how would it import the built-in version from python2.5/site-packages? Thanks, Tro -- http://mail.python.org/mailman/listinfo/python-list
Re: Altering imported modules
On Sunday 02 March 2008, Paul McGuire wrote: > On Mar 2, 3:48 pm, Tro <[EMAIL PROTECTED]> wrote: > > On Sunday 02 March 2008, Terry Reedy wrote: > > > "Tro" <[EMAIL PROTECTED]> wrote in message > > >news:[EMAIL PROTECTED] > > > > > > | Hi, list. > > > | > > > | I've got a simple asyncore-based server. However, I've modified the > > > > > > asyncore > > > > > > | module to allow me to watch functions as well as sockets. The > > > | modified asyncore module is in a specific location in my project and > > > | is imported > > > > > > as > > > > > > | usual from my classes. > > > | > > > | Now I'd like to use the tlslite library, which includes an asyncore > > > | mixin class. However, tlslite imports "asyncore", which doesn't > > > | include my own modifications. > > > | > > > | I'd like to know if it's possible to make tlslite load *my* asyncore > > > > > > module > > > > > > | without changing any of the tlslite code. > > > > > > If your module is also 'asyncore' and comes earlier in the search path, > > > I would expect the import to get yours. > > > > It's not. It has a package prefix like my.package.asyncore. I think I can > > either move my version of asyncore up a couple of levels or add the > > my.package directory to sys.path. > > > > My version of asyncore imports several functions from the built-in > > asyncore. Now that my version of it is imported as asyncore, how would it > > import the built-in version from python2.5/site-packages? > > > > Thanks, > > Tro > > > > > > What happens if you do "import my.package.asyncore as asyncore"? > > If that doesn't work (trying the simplest hack first), I know that > there are various hooks in the import mechanism that should help. In the classes that use my version of asyncore currently, that is how I do it. I import my version as "import my.package.asyncore as asyncore". In my asyncore module I do "import asyncore", because I override a few functions from the asyncore module included with python. However, if I were to add "my.package" to sys.path, then I wouldn't be able to "import asyncore" from my own asyncore module. I'd have to do some trickery with sys.path to take the "my.package" component out, import standard asyncore, readd the "my.package" component, so that other modules can "import asyncore" and get my version. Is there a way to import the standard python asyncore module in this scenario? Thanks, Tro -- http://mail.python.org/mailman/listinfo/python-list
Re: Altering imported modules
On Monday 03 March 2008, [EMAIL PROTECTED] wrote: > On Mar 3, 5:09 pm, Tro <[EMAIL PROTECTED]> wrote: > > On Sunday 02 March 2008, Paul McGuire wrote: > > > On Mar 2, 3:48 pm, Tro <[EMAIL PROTECTED]> wrote: > > > > On Sunday 02 March 2008, Terry Reedy wrote: > > > > > "Tro" <[EMAIL PROTECTED]> wrote in message > > > > >news:[EMAIL PROTECTED] > > > > > > > > > > | Hi, list. > > > > > | > > > > > | I've got a simple asyncore-based server. However, I've modified > > > > > | the > > > > > > > > > > asyncore > > > > > > > > > > | module to allow me to watch functions as well as sockets. The > > > > > | modified asyncore module is in a specific location in my project > > > > > | and is imported > > > > > > > > > > as > > > > > > > > > > | usual from my classes. > > > > > | > > > > > | Now I'd like to use the tlslite library, which includes an > > > > > | asyncore mixin class. However, tlslite imports "asyncore", which > > > > > | doesn't include my own modifications. > > > > > | > > > > > | I'd like to know if it's possible to make tlslite load *my* > > > > > | asyncore > > > > > > > > > > module > > > > > > > > > > | without changing any of the tlslite code. > > > > > > > > > > If your module is also 'asyncore' and comes earlier in the search > > > > > path, I would expect the import to get yours. > > > > > > > > It's not. It has a package prefix like my.package.asyncore. I think I > > > > can either move my version of asyncore up a couple of levels or add > > > > the my.package directory to sys.path. > > > > > > > > My version of asyncore imports several functions from the built-in > > > > asyncore. Now that my version of it is imported as asyncore, how > > > > would it import the built-in version from python2.5/site-packages? > > > > > > > > Thanks, > > > > Tro > > > > > > What happens if you do "import my.package.asyncore as asyncore"? > > > > > > If that doesn't work (trying the simplest hack first), I know that > > > there are various hooks in the import mechanism that should help. > > > > In the classes that use my version of asyncore currently, that is how I > > do it. I import my version as "import my.package.asyncore as asyncore". > > In my asyncore module I do "import asyncore", because I override a few > > functions from the asyncore module included with python. However, if I > > were to add "my.package" to sys.path, then I wouldn't be able to "import > > asyncore" from my own asyncore module. I'd have to do some trickery with > > sys.path to take the "my.package" component out, import standard > > asyncore, readd the "my.package" component, so that other modules can > > "import asyncore" and get my version. > > > > Is there a way to import the standard python asyncore module in this > > scenario? > > > > Thanks, > > Tro > > > > > > Are you trying to interfere with the default module on only your > machine? Just rename it. If something in the std. lib. imports > asyncore, they get yours too that way. No, I'd like it to be a generalized solution and only for this one project. I'm trying to get it to work on any recent python installation out of the box, so I can't rename built-in modules. What I'm trying to do is allow a 3rd party module (tlslite) to import *my* version of asyncore without me going into tlslite's code and changing its import statement explicitly, but only for the scope of this one project. Thanks, Tro -- http://mail.python.org/mailman/listinfo/python-list
Re: Altering imported modules
On Tuesday 04 March 2008, Floris Bruynooghe wrote: > On Mar 1, 11:56 pm, Tro <[EMAIL PROTECTED]> wrote: > > I'd like to know if it's possible to make tlslite load *my* asyncore > > module without changing any of the tlslite code. > > the pkgutil module might be helpful, not sure though as I've never > used it myself. > > http://blog.doughellmann.com/2008/02/pymotw-pkgutil.html is a fairly > detailed look at what pkgutil can do. Thanks, that's neat. Tro -- http://mail.python.org/mailman/listinfo/python-list
Re: Altering imported modules
On Tuesday 04 March 2008, Steve Holden wrote: > >> Are you trying to interfere with the default module on only your > >> machine? Just rename it. If something in the std. lib. imports > >> asyncore, they get yours too that way. > > > > No, I'd like it to be a generalized solution and only for this one > > project. I'm trying to get it to work on any recent python installation > > out of the box, so I can't rename built-in modules. What I'm trying to do > > is allow a 3rd party module (tlslite) to import *my* version of asyncore > > without me going into tlslite's code and changing its import statement > > explicitly, but only for the scope of this one project. > > In that case try something like > > import myasyncore as asnycore > sys.modules['asyncore'] = asyncore > import tlslite Ah. That's what I've been looking for. Thanks! Tro -- http://mail.python.org/mailman/listinfo/python-list
Re: Altering imported modules
On Wednesday 05 March 2008, Bruno Desthuilliers wrote: > Tro a écrit : > > Hi, list. > > > > I've got a simple asyncore-based server. However, I've modified the > > asyncore module to allow me to watch functions as well as sockets. The > > modified asyncore module is in a specific location in my project and is > > imported as usual from my classes. > > > > Now I'd like to use the tlslite library, which includes an asyncore mixin > > class. However, tlslite imports "asyncore", which doesn't include my own > > modifications. > > > > I'd like to know if it's possible to make tlslite load *my* asyncore > > module without changing any of the tlslite code. > > Not sure this apply to your case (depends on how asyncore is implemented > and the exact "modifications"), but monkeypatching is a possible solution. > > http://en.wikipedia.org/wiki/Monkey_patch > http://wiki.zope.org/zope2/MonkeyPatch > http://mail.python.org/pipermail/python-dev/2008-January/076194.html Ooh. I didn't know this technique had a name. But that's basically how I'm modifying the built-in asyncore in my own class. I override its poll() and poll2() methods to do something extra. However, the issue with tlslite is that it imports the regular asyncore instead of the one I wrote, which means that I'd have to somehow monkeypatch its "import" statement. I'm guessing that means simply monkeypatching both the original asyncore module AND the tlslite module's asyncore attribute with my own version. Thanks, Tro -- http://mail.python.org/mailman/listinfo/python-list