473,396 Members | 1,724 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,396 software developers and data experts.

Too many imports to use a business class library?

Hi all,

I am new to python. Sorry if this is too novice question, but I could
not find an answer yet.

While coding a business class library, I think it is preferable to have
one class per source file, rather than combining all classes into one
file, considering multiple developers developing the classes.

So, as per my study of python, a developer using the business class
library has to write so many imports, one per class. Like:

from person import Person
from contact import Contact
..
..
..

Is there not a simple solution, a single import and you are able to use
all the classes? Is there anything wrong in my approcah? Waiting for
suggestions.

Thanks in advance.
Sanjay

Jul 13 '06 #1
5 1878
Sanjay wrote:
I am new to python. Sorry if this is too novice question,
Don't worry and welcome.

While coding a business class library, I think it is preferable to have
one class per source file, rather than combining all classes into one
file, considering multiple developers developing the classes.
The use of one class = one sourcefile is often coming from Java.
Usually in Python people use something in the middle, putting related
names / functions / classes in the same module.
And you can put related modules in the same directory (with the
__init__ file).

Is there not a simple solution, a single import and you are able to use
all the classes? Is there anything wrong in my approcah? Waiting for
suggestions.
Other things that you may find useful to organize your names and
modules:
- the __all__ list to choose what to export;
- you can import a whole directory of modules;
- you can put many names in the same import line (from xx import xy,
yz, etc).
- Sometimes you can even use from XX import * but this can be a little
dangerous.

Bye,
bearophile

Jul 13 '06 #2
Sanjay schrieb:
Hi all,

I am new to python. Sorry if this is too novice question, but I could
not find an answer yet.

While coding a business class library, I think it is preferable to have
one class per source file, rather than combining all classes into one
file, considering multiple developers developing the classes.

So, as per my study of python, a developer using the business class
library has to write so many imports, one per class. Like:

from person import Person
from contact import Contact
.
.
.

Is there not a simple solution, a single import and you are able to use
all the classes? Is there anything wrong in my approcah? Waiting for
suggestions.
While there is nothing technically wrong, in python usually several
related classes (and functions!) are grouped in one module. Not one
class per file.

Additionally, you can use one module to import others into its
namespace. The result is that this allows to only have one import, but
get lots of modules imported with that.

foo/__init__.py:
import bar
from baz import *

foo/bar.py:
class Bar(object):
pass

foo/baz.py:
class Baz(object):
pass
can then be used as this:
import foo

foo.bar.Bar()
foo.bar.Baz()
Diez
Jul 13 '06 #3
Sanjay wrote:
Hi all,

I am new to python. Sorry if this is too novice question, but I could
not find an answer yet.

While coding a business class library, I think it is preferable to have
one class per source file, rather than combining all classes into one
file, considering multiple developers developing the classes.
The "multiple developpers" already implies the use of a CVS, so it's a
dubious argument here IMHO. Also, Python is not Java, and doesn't force
you into using classes when other constructs will do (functions, dicts
etc). The organization of the source should be done with the
hi-cohesion/low-coupling rule in mind, ie symbols that are very closely
related should go in a same module.
So, as per my study of python, a developer using the business class
library has to write so many imports, one per class. Like:

from person import Person
from contact import Contact
.
As you already noticed, this sucks.
Is there not a simple solution,
- use a CVS (not necessarily 'the' CVS - subversion is somewhat better,
and there are other options too) to address the multi-developper problem
- put closely related symbols in a same module
- put closely related modules in a same package
a single import and you are able to use
all the classes?
Python as packages:
http://docs.python.org/tut/node8.htm...00000000000000
Is there anything wrong in my approcah?
cf above

HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 13 '06 #4
Sanjay wrote:
While coding a business class library, I think it is preferable to have
one class per source file,
.... but I don't like the consequences :-)
So, as per my study of python, a developer using the business class
library has to write so many imports, one per class. Like:

from person import Person
from contact import Contact
.
.
.

Is there not a simple solution, a single import and you are able to use
all the classes? Is there anything wrong in my approcah? Waiting for
suggestions.
Put person.py, contact.py etc into a subdirectory (which I assume is called
business). Then create a file business/__init__.py to turn the directory
into a package

#contents of business/__init__.py
from person import Person
from contact import Contact

You can then use your library like so:

from business import Person, Contact

person = Person(...)
contact = Contact(...)

or preferably:

import business

person = business.Person(...)
contact = business.Contact(...)

Peter

Jul 13 '06 #5
Thanks a lot to all for the to-the-point info, quite vital to me...

Sanjay

Jul 13 '06 #6

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

Similar topics

9
by: Hasan O. Zavalsiz | last post by:
Hi , i am trying to figure out which approach is better to use . let me explain the scenario. i am using the "Nortwind" database . in this database i have "Customers " table .The following is the...
1
by: John | last post by:
Hello everyone! I've read many articles about asp.net, business and data layers, and none of them seem to discourage the use of HttpContext specific variables in non-HttpContext specific layers;...
5
by: Shapper | last post by:
Hello, I am working in a web site where all the code is placed in aspx.vb files. After a while I realized that many functions included in my aspx.vb files where common to all pages. I...
2
by: Carlos | last post by:
I'm using VS 2003, im trying to write a windows app that will send an emial bt when I import the Imports System.Web.Mail give me an error, it finds Imports System.Web but not the mail part any...
3
by: moondaddy | last post by:
I'm wanting to create a bindable list object made up of a list of business classes. I'm writing this in a vb.net 1.1 winforms app and am using a code example by Rocky Lhotka for reference...
6
by: Guy Thornton | last post by:
I have an application in asp.net using vb. The asp.net portion of this application is mainly the user interface. The UI has references made to our business logic layer. And the business logic...
2
by: grawsha2000 | last post by:
Greetings, I am developing this N-tier business app. The problem I'm facing is when I try to pass business objects (employees, dept..etc) from business tier to data tier,i.e., the add method in...
2
by: Chris Zopers | last post by:
Hello, I would like to know what's the best way to implement a business logic layer between my user interface and my database. I would say I'd make a dll-project for the business logic layer...
5
by: jehugaleahsa | last post by:
Hello: I am trying to find what is the very best approach to business objects in Windows Forms. Windows Forms presents an awesome opportunity to use DataTables and I would like to continue doing...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.