Small changes in side library

Vircom maxim.gavrilov at gmail.com
Mon Sep 24 14:26:04 EDT 2007


On Sep 24, 1:16 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Sat, 22 Sep 2007 22:07:27 -0300, maxim.gavri... at gmail.com
> <maxim.gavri... at gmail.com> escribi?:
>
>
>
> > I have the side library which provides wide set of different
> > functions, but I'm going to replace some of them with mine and
> > provided such 'modified' library thought my project.
>
> > The following way works well for my purpose:
>
> > ------- mylib.py -------
> > import sidelib
>
> > import os, sys, ....
>
> > def func():
> >     .....
> > sidelib.func = func
> > ......
> > ?!?!?!?!
> > --------------------------
>
> > But this cause to write mylib.sidelib.func() to function call, is it
> > any way to 'map' definitions from sidelib to mylib (possible at point
> > marked ?!?!?!?!) such that constructions like mylib.func() will be
> > provided and client code don't see difference between changed and
> > original library in syntax way?
>
> Your code already works as you like:
> import sidelib
> sidelib.func()
> and you get the modified function.
>
> You don't have to say mylib.sidelib.func - in fact, mylib.sidelib is the
> same module object as sidelib
> But you have to ensure that the replacing code (mylib.py) runs *before*
> anyone tries to import something from sidelib.
>
> > One my idea was to do from sidelib import * and then modify globals()
> > dictionary, but this isn't good too because mylib imports many other
> > modules and they all mapped into it's namespace (like mylib.os,
> > mylib.sys).
>
> As you said, a bad idea.
>
> --
> Gabriel Genellina

Thank you for reply!
I make a mistake in my problem description, because I'm going not only
use my own functions (it'll be simple import library question), but
also using side library many functions (with only a few one replaced
by me). For now I'm stay at the following solution:
----- mylib.py -----
import sidelib
from sidelib import *

_sidelib_set = set(dir(sidelib))
_mylib_set = set(['replacedfunc1', 'replacedfunc2', ....]) # all
exports
__all__ = list(_sidelib_set.union(_mylib_set))

....implementations of _mylib_set functions....
--------------------

Seems it's pretty good for my task, mylib seems fully the same as the
sidelib one.

Thanks for you attention, anyway!




More information about the Python-list mailing list