473,378 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

modules and generated code


Hi all,

Python newbie here with what I hope is a blindingly obvious question
that I simply can't find the answer for in the documentation.

So, if I have a tool that generates python code for me (in my case,
CORBA stubs/skels) in a particular package is there a way of placing my
own code under the same package hierarchy without all the code living in
the same directory structure.

Ideally I would like something like the following:

package_dir/
top_level_package/
generated_code_package/
implementation_code_package/

but have two distinct directories that hold them so that I can simply
delete the generated code and regenerate it without worrying that
anything got left behind.

So, I want something like:

generated_package_dir/
top_level_package/
generated_code_package/

implementation_package_dir/
top_level_package/
implementation_code_package/

Whilst I can create this structure, and add 'generated_package_dir' and
'implementation_package_dir' to PYTHONPATH the fact that both
directories contain 'top_level_package' seems to be causing clashes,
perhaps because there are multiple __init__.py files for
'top_level_package'?

I know that this is possible in Java, Perl and C++ so I am finding it
hard to believe I can't do the same in Python, I just think I'm too new
to know how.

I have spent most of this morning searching through all the docs I can
find, searching on USENET and the web to no avail.

Any help or pointers greatly appreciated.

Regards,

n
Nov 14 '06 #1
8 1005
Nigel Rantor wrote:
So, if I have a tool that generates python code for me (in my case,
CORBA stubs/skels) in a particular package is there a way of placing my
own code under the same package hierarchy without all the code living in
the same directory structure.
http://docs.python.org/lib/module-pkgutil.html

Peter

Nov 14 '06 #2
Peter Otten wrote:
Nigel Rantor wrote:
>So, if I have a tool that generates python code for me (in my case,
CORBA stubs/skels) in a particular package is there a way of placing my
own code under the same package hierarchy without all the code living in
the same directory structure.

http://docs.python.org/lib/module-pkgutil.html
Ooh, thanks for that.

Yep, looks like that should work, but it doesn't. :-/

Do you have any idea whether other __init__.py scripts from the same
logical module will still be run in this case?

The generated code uses its init script to pull in other code.

Off, to tinker some more with this.

n
Nov 14 '06 #3
Nigel Rantor wrote:
Peter Otten wrote:
>Nigel Rantor wrote:
>>So, if I have a tool that generates python code for me (in my case,
CORBA stubs/skels) in a particular package is there a way of placing my
own code under the same package hierarchy without all the code living in
the same directory structure.
>http://docs.python.org/lib/module-pkgutil.html
Yep, looks like that should work, but it doesn't. :-/

Do you have any idea whether other __init__.py scripts from the same
logical module will still be run in this case?
I don't think it will.
The generated code uses its init script to pull in other code.
You could invoke it explicitly via

execfile("/path/to/generated/package/__init__.py")

in the static package/__init__.py.

Peter
Nov 14 '06 #4
Peter Otten wrote:
Nigel Rantor wrote:
>Peter Otten wrote:
>>Nigel Rantor wrote:
>>>So, if I have a tool that generates python code for me (in my case,
CORBA stubs/skels) in a particular package is there a way of placing my
own code under the same package hierarchy without all the code living in
the same directory structure.
>>http://docs.python.org/lib/module-pkgutil.html
>Yep, looks like that should work, but it doesn't. :-/

Do you have any idea whether other __init__.py scripts from the same
logical module will still be run in this case?

I don't think it will.
Yeah, I am getting that impression. Gah!
>The generated code uses its init script to pull in other code.

You could invoke it explicitly via

execfile("/path/to/generated/package/__init__.py")

in the static package/__init__.py.
Hmm, yes, that works. It's not pretty though, it seems to be finding the
file relative to the current directory, I suppose writing a bit of code
that figures out where this package is located and modifying it won't be
too hard.

And, at the risk of being flamed or sounding like a troll, this seems
like something that should be easy to do...other languages manage it
quite neatly. Up until this point I was really liking my exposure to
Python :-/

I wonder if there is any more magic that I'm missing, the thing is the
pkgutil method looks exactly like what I want, except for not executing
the additional __init__.py files in the added directories.

Thanks for the help so far Peter, if anyone has a prettier solution then
I'm all ears.

Cheers,

n

Nov 14 '06 #5
Nigel Rantor wrote:
Peter Otten wrote:
>Nigel Rantor wrote:
>>Peter Otten wrote:
Nigel Rantor wrote:
>>>>So, if I have a tool that generates python code for me (in my case,
CORBA stubs/skels) in a particular package is there a way of
placing my
own code under the same package hierarchy without all the code
living in
the same directory structure.
>>>http://docs.python.org/lib/module-pkgutil.html
>>Yep, looks like that should work, but it doesn't. :-/

Do you have any idea whether other __init__.py scripts from the same
logical module will still be run in this case?

I don't think it will.

Yeah, I am getting that impression. Gah!
>>The generated code uses its init script to pull in other code.

You could invoke it explicitly via
execfile("/path/to/generated/package/__init__.py")
in the static package/__init__.py.

Hmm, yes, that works. It's not pretty though, it seems to be finding the
file relative to the current directory, I suppose writing a bit of code
that figures out where this package is located and modifying it won't be
too hard.

And, at the risk of being flamed or sounding like a troll, this seems
like something that should be easy to do...other languages manage it
quite neatly. Up until this point I was really liking my exposure to
Python :-/

I wonder if there is any more magic that I'm missing, the thing is the
pkgutil method looks exactly like what I want, except for not executing
the additional __init__.py files in the added directories.

Thanks for the help so far Peter, if anyone has a prettier solution then
I'm all ears.

Cheers,

n


Maybe I'm missing something obvious, but it sounds like you are
over-complicating the idea of inheritance. Do you just want to create a
subclass of the other class?

Cheers,
Cliff
Nov 15 '06 #6
J. Clifford Dyer wrote:
>
Maybe I'm missing something obvious, but it sounds like you are
over-complicating the idea of inheritance. Do you just want to create a
subclass of the other class?
Nope, that isn't my problem.

I have an IDL file that is used to generate a set of stub and skeleton
code that is not human-modifiable.

Eventually I would like to have my IDL in source control and have a
setup script able to generate my stubs and skels and install them for me.

At the same time I want to produce code that uses this code but in the
same package.

In Java or Perl I can easily create a couple package/module like this:

package org.mine.package;

[...class definitions...]
and then somewhere else

package org.mine.otherpackage;

[...class definitions...]

These can be compiled into separate Jar files and just work.

Since the python is the final target though I don't want to put it all
in one directory because then I need to be clever when I regenerate the
generated code, I don't want old python modules lying around that are no
longer in the IDL. Blowing the entire generated directory away is the
best way of doing this, so I don't want my implementation code in there.

Basically, I want the same top-level package to have bits of code in
different directories, but because Python requires the __init__.py file
it only picks up the first one in PYTHONPATH.

I'm not sure if that makes sense, my brain is already toast from
meetings today.

n

Nov 15 '06 #7
Nigel Rantor wrote:
Basically, I want the same top-level package to have bits of code in
different directories, but because Python requires the __init__.py file
it only picks up the first one in PYTHONPATH.
would a single __init__.py function that does from-import-* on the
various implementation modules solve your problem?

</F>

Nov 15 '06 #8
At Wednesday 15/11/2006 14:33, Nigel Rantor wrote:
>I have an IDL file that is used to generate a set of stub and skeleton
code that is not human-modifiable.

Eventually I would like to have my IDL in source control and have a
setup script able to generate my stubs and skels and install them for me.

At the same time I want to produce code that uses this code but in the
same package.

[...]
Basically, I want the same top-level package to have bits of code in
different directories, but because Python requires the __init__.py file
it only picks up the first one in PYTHONPATH.
Put both directories in the same package. But this is the obvious
thing, so maybe I'm missing something.
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ”gratis!
”Abrķ tu cuenta ya! - http://correo.yahoo.com.ar
Nov 15 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Raaijmakers, Vincent (IndSys,GE Interlogix) | last post by:
Sorry for the long header of this mail ;-) Has anyone experienced modules developed in 2.3.2 (using IDLE and PythonWin as IDE) don't work under 2.2.2.... I mean basic stuff like it can not see...
8
by: Dan Perl | last post by:
Is there a direct way to get a list of all the modules in a package, other than using an os.listdir() for the directory of the package? To be more specific, I would like to inspect every module...
3
by: Rickard Lind | last post by:
Is there any way to build the python executable statically and still be able to load modules built as shared libraries? I'm trying to run python scripts on a stripped down FreeBSD (4.9) machine...
5
by: Ben R. | last post by:
Hello, Iā€™m in the process of reading "Inside Microsft .NET IL Assembler" by Serge Lidin to gain a better understanding of my .NET code at a lower (IL) level. There is one concept that I am...
2
by: James Buchanan | last post by:
Hi group, I'm preparing Python 2.4.2 for the upcoming Minix 3.x release, and I have problems with make. configure runs fine and creates the makefile, but right at the end ends with an error...
2
by: Norton | last post by:
I understand how to create HTTP modules that can be used to add functionality to a website but there are a few things I don't understand. If I create an HTTP Module and I want it to intercept a...
8
by: Alexia | last post by:
Hi, I just wanted a hand on using modules. I have just managed to create a custom smooth progress bar control and i wanted to know if it is possible to insert the code in a module so it can be...
1
by: Mark Asbach | last post by:
Hi pythonians, I'm one of the maintainers of an open source image processing toolkit (OpenCV) and responsible for parts of the autotools setup. The package mainly consists of four shared...
173
by: Zytan | last post by:
I've read the docs on this, but one thing was left unclear. It seems as though a Module does not have to be fully qualified. Is this the case? I have source that apparently shows this. Are...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.