Hye everyone,
I'm would like to do something a bit tricky.
I would like when I do something like to create a __init__ package's
(here calle my_package) file witch make an action when we try to
import something in this package...
Quiet like __getattribute__ work for a class, I would like this kind
of behaviour for a whole module or a package
be a bit clearer:
I've got a normal package/module/class (whathever) where I do
something like:
from my_package import my_module
(but in the my_package I've got only:
my_package / __init__.py
And in the __init__ of my my_package something like __getattribute__
(but for a module) that would return something for my_module
I know, that I can do something like charging everything "before" in
__init__ like:
my_module = the_thing_I_want
But I need it to be lazy (because I may have many module witch could
be very awful...).
Any idea ?
Is it possible ? 6 2175
En Tue, 16 Oct 2007 10:16:50 -0300, Alexandre Badez
<al*************@gmail.comescribió:
I would like when I do something like to create a __init__ package's
(here calle my_package) file witch make an action when we try to
import something in this package...
Quiet like __getattribute__ work for a class, I would like this kind
of behaviour for a whole module or a package
be a bit clearer:
I've got a normal package/module/class (whathever) where I do
something like:
from my_package import my_module
(but in the my_package I've got only:
my_package / __init__.py
And in the __init__ of my my_package something like __getattribute__
(but for a module) that would return something for my_module
I know, that I can do something like charging everything "before" in
__init__ like:
my_module = the_thing_I_want
But I need it to be lazy (because I may have many module witch could
be very awful...).
A module doesn't *have* to be a module... You can replace
sys.modules['your_module_name'] with a suitable object. See this classical
recipe from Alex Martelli:
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65207>
--
Gabriel Genellina
On Tue, 16 Oct 2007 13:16:50 +0000, Alexandre Badez wrote:
Hye everyone,
I'm would like to do something a bit tricky.
I would like when I do something like to create a __init__ package's
(here calle my_package) file witch make an action when we try to import
something in this package...
Quiet like __getattribute__ work for a class, I would like this kind of
behaviour for a whole module or a package
be a bit clearer:
I've got a normal package/module/class (whathever) where I do something
like:
from my_package import my_module
(but in the my_package I've got only: my_package / __init__.py
And in the __init__ of my my_package something like __getattribute__
(but for a module) that would return something for my_module
I know, that I can do something like charging everything "before" in
__init__ like:
my_module = the_thing_I_want
But I need it to be lazy (because I may have many module witch could be
very awful...).
Any idea ?
Is it possible ?
Depending on the exact complexity of your needs and exact use case just
inserting some code on top of every module could suffice. To reduce
boilerplate, if the task you're trying to realize is pretty generic for
every module, you could insert another module hiding this code and used
like this::
from .modregister import init
init(__name__)
If you really have to _create_ modules dynamically, follow the advice
given earlier in this thread.
HTH,
Stargaming
Thanks for all your advices, but it's not really what I would like to
do.
I'm going to be more clearer for what I really want to do.
Here we have got many library for different applications. All those
library have a version and between a version and an other, there isn't
always a very good backward compatibility (I know, it's very ugly, but
it's like that...).
Moreover some library use the version 1.1 and some the version 1.2 of
lib A and version 2.1 and version 2.0 of lib B ... you know what: it's
very ugly.
My idea was to be able to use lib quiet like that.
import A (<- If I want to use the very last version)
# or
import A.1_1 (<- If I want to use the version 1.1 of the A lib)
Something else ?
Yes :)
I do not want to add all those path in PYTHONPATH (would be too ugly,
and "complicated").
I want it lazy (do not import every version of every lib every time)
I want it scalable: if a user or the admin add a new lib or a version
of lib it would be very very great if he had nothing else (than copy
his directory) to do.
So what I wanted to do, was to be able to control what the user really
wanted to import, and act he excepted and put the "intelligence" in a
__init__ script
Alexandre Badez wrote:
Thanks for all your advices, but it's not really what I would like to
do.
I'm going to be more clearer for what I really want to do.
Here we have got many library for different applications. All those
library have a version and between a version and an other, there isn't
always a very good backward compatibility (I know, it's very ugly, but
it's like that...).
Moreover some library use the version 1.1 and some the version 1.2 of
lib A and version 2.1 and version 2.0 of lib B ... you know what: it's
very ugly.
My idea was to be able to use lib quiet like that.
import A (<- If I want to use the very last version)
# or
import A.1_1 (<- If I want to use the version 1.1 of the A lib)
Something else ?
Yes :)
I do not want to add all those path in PYTHONPATH (would be too ugly,
and "complicated").
I want it lazy (do not import every version of every lib every time)
I want it scalable: if a user or the admin add a new lib or a version
of lib it would be very very great if he had nothing else (than copy
his directory) to do.
So what I wanted to do, was to be able to control what the user really
wanted to import, and act he excepted and put the "intelligence" in a
__init__ script
Use setuptools + pkg_resources to install sereval versions of your libraries
together and then you can require a certain version of your lib.
HOWEVER: this WON'T work for several versions of a library in ONE running
python process!!!! Because the import will then either fail silently (after
all, "import foo" is ignored if foo is already present) or pkg_resources is
so clever that it keeps tracks of requirements and if they are conflicting
will puke on you.
Diez
On Oct 17, 3:56 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Alexandre Badez wrote:
Thanks for all your advices, but it's not really what I would like to
do.
I'm going to be more clearer for what I really want to do.
Here we have got many library for different applications. All those
library have a version and between a version and an other, there isn't
always a very good backward compatibility (I know, it's very ugly, but
it's like that...).
Moreover some library use the version 1.1 and some the version 1.2 of
lib A and version 2.1 and version 2.0 of lib B ... you know what: it's
very ugly.
My idea was to be able to use lib quiet like that.
import A (<- If I want to use the very last version)
# or
import A.1_1 (<- If I want to use the version 1.1 of the A lib)
Something else ?
Yes :)
I do not want to add all those path in PYTHONPATH (would be too ugly,
and "complicated").
I want it lazy (do not import every version of every lib every time)
I want it scalable: if a user or the admin add a new lib or a version
of lib it would be very very great if he had nothing else (than copy
his directory) to do.
So what I wanted to do, was to be able to control what the user really
wanted to import, and act he excepted and put the "intelligence" in a
__init__ script
Use setuptools + pkg_resources to install sereval versions of your libraries
together and then you can require a certain version of your lib.
HOWEVER: this WON'T work for several versions of a library in ONE running
python process!!!! Because the import will then either fail silently (after
all, "import foo" is ignored if foo is already present) or pkg_resources is
so clever that it keeps tracks of requirements and if they are conflicting
will puke on you.
Diez
Well, I would like to be able to use "setuptools", but the problem is
that I can't.
Cause the administrator do not want us to be able to add lib in python
dir.
So we have to create our own library directory...
Moreover, I haven't seen in distutils how it manage different version
of the same library; as far as I know, It just replace the old one by
the newest one... and that's not really what I want.
>Diez
>
Well, I would like to be able to use "setuptools", but the problem is
that I can't.
Cause the administrator do not want us to be able to add lib in python
dir.
So we have to create our own library directory...
That doesn't matter, setuptools is capable of installing anywhere - you just
have to setup _one_ PYTHONPATH (e.g. ~/.python24_packages) and then specify
that when installing the eggs.
Moreover, I haven't seen in distutils how it manage different version
of the same library; as far as I know, It just replace the old one by
the newest one... and that's not really what I want.
Well, you might consider my statement in the last post as hint that you
didn't look properly. It CAN and will install sereval versions in parallel,
and allow for previous selection. GIYF.
Diez This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Vinay Aggarwal |
last post by:
I have been thinking about the lazy initialization and double checked
locking problem. This problem is explain in detail here
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
|
by: Vio |
last post by:
I have a basic dynamic lib "noddy.so" which I want to
1- embed inside my executable
2- import by embedded python interpreter using an API call.
I have two questions:
1- what would be the...
|
by: bry |
last post by:
Hi,
I'm trying to do a dynamic import of a file that has no problems with it, that
is to say I can import it normally, my sys.path is set to the right folder etc.
but my dynamic import code is not...
|
by: Steven Bethard |
last post by:
So I end up writing code like this a fair bit:
map = {}
for key, value in sequence:
map.setdefault(key, ).append(value)
This code basically constructs a one-to-many mapping -- each value that...
|
by: Bill Davy |
last post by:
I am working with MSVC6 on Windows XP.
I have created an MSVC project called SHIP
I have a file SHIP.i with "%module SHIP" as the first line (file is below).
I run SHIP.i through SWIG 1.3.24...
|
by: Sandeep Chikkerur |
last post by:
Hi,
If the entire heap memory for dynamic allocation is not available,
does the compiler always return NULL ?
eg:
char *s;
s = (char *)malloc(...);
|
by: David Christian |
last post by:
Hello all,
I've been working on a lazy import module to be found here:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473888
The basic idea is that a module proxy for foo is returned...
|
by: Steve |
last post by:
Hi All,
I have an Excel file being delivered to a shared drive where I only
have read access on a daily basis.
The files arrive in the following format 'Filename+timestamp.xls'
Example:...
|
by: bambam |
last post by:
import works in the main section of the module, but does
not work as I hoped when run inside a function.
That is, the modules import correctly, but are not visible to
the enclosing (global)...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| |