Small changes in side library

M

maxim.gavrilov

Hi, all!

May be this question have been already discussed, but I found nothing
closer :-/
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?

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).

Thanks for attention!
 
G

Gabriel Genellina

En Sat, 22 Sep 2007 22:07:27 -0300, (e-mail address removed)
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.
 
V

Vircom

En Sat, 22 Sep 2007 22:07:27 -0300, (e-mail address removed)
<[email protected]> escribi?:









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.


As you said, a bad idea.

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!
 
V

Vircom

En Sat, 22 Sep 2007 22:07:27 -0300, (e-mail address removed)
<[email protected]> escribi?:
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.
As you said, a bad idea.

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!

Sorry, I'm misunderstand your propose by first reading. Importing
sidelib after my changes are really the best idea. I'm changingmy
project this way.

Thank you!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top