473,395 Members | 2,006 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,395 software developers and data experts.

Question on making names visible in packages

This will have an easy answer, no doubt, unfortunately I can't
think of it...

I'm writing a package which will have a number of classes. I'd
like to split them amongst different files within the package.
However, I'd also like the package user to be able to say:

import Package

and then to use the classes as

Package.Class1, Package.Class2, etc.

However, I'm only able to get this behavior when I leave the
class definitions in the __init__.py file. When in other files,
they of course have to be referenced as :

Package.filename.Class1

Is there some way of making the names visible at the top level
of the module, to mask the fact that they are split amongst
different files within the package? The split reflects a
logical organization of the code from the point of view of
the designer of the package, but not from the user, and
so I don't want the user to necessarily be aware of the
file organization. (If they want to be, hey, that's fine...)
Thanks,
Ken McDonald
Jul 18 '05 #1
3 1224
"Kenneth McDonald" <km********@wisc.edu> wrote in message
news:sl***********************@g4.gateway.2wire.ne t...
[snip]

import Package

and then to use the classes as

Package.Class1, Package.Class2, etc.

However, I'm only able to get this behavior when I leave the
class definitions in the __init__.py file. When in other files,
they of course have to be referenced as :

Package.filename.Class1

Is there some way of making the names visible at the top level
of the module, to mask the fact that they are split amongst
different files within the package?

[snip]

In your __init__.py you can add :

from filename import Class1

to make the names visible at the top-level.

HTH
Sean


Jul 18 '05 #2
> However, I'm only able to get this behavior when I leave the
class definitions in the __init__.py file. When in other files,
they of course have to be referenced as :


Make your init.py be like this:

from filename1 import *
from filename2 import *
from filename3 import *

If you don't want lots of namespace pollution, you can import
only the symbols you want to export in the package namespace.

Roger
Jul 18 '05 #3
Kenneth McDonald wrote:
I'm writing a package which will have a number of classes. I'd
like to split them amongst different files within the package.
However, I'd also like the package user to be able to say:

import Package

and then to use the classes as

Package.Class1, Package.Class2, etc.


Example:

D:\BUFFER\Package
bar.py
foo.py
__init__.py

bar defines class Bar; foo defines class Foo.

__init__.py looks like this:

from bar import Bar
from foo import Foo

Now, I can:

import Package
f = Package.Foo()

If the contents of Package are volatile, you could even use os, glob,
and __import__ to make the from x import y entirely dynamic.

// m
Jul 18 '05 #4

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

Similar topics

6
by: Ralph A. Gable | last post by:
I am opening MSIE6 with this code: ie=Dispatch('InternetExplorer.Application.1') ie.Navigate(url) while ie.Busy: time.sleep(0.1) ied=ie.Document while ied.ReadyState != 'complete':...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
55
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.