473,395 Members | 1,987 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.

Organizing Code - Packages

All,

I apologize if this is a commonly asked question, but I didn't
find anything that answered my question while searching.

So what I have right now is a few packages that contain some commonly
used functions and another package that contains all of my custom
error classes. I want these error classes available to me in all of
the other packages in my library. Currently to achieve this at the top
of every module file I have the line "from My.Library.Errors import
*", my problem with this is that it manages to import the Errors into
every scope that they are used. I'm still pretty new to Python, and my
approachs are probably very rooted in C/C++ (I've had the hardest time
getting over not being able to overload functions), but am I doing
this correctly?

Also, are there any good tutorials/examples out there of how to
organize your python code into packges?

Thanks for all the help!

Regards,
Ken

Sep 7 '07 #1
12 1853
xkenneth a écrit :
All,

I apologize if this is a commonly asked question, but I didn't
find anything that answered my question while searching.

So what I have right now is a few packages that contain some commonly
used functions and another package that contains all of my custom
error classes. I want these error classes available to me in all of
the other packages in my library. Currently to achieve this at the top
of every module file I have the line "from My.Library.Errors import
*", my problem with this is that it manages to import the Errors into
every scope that they are used.
Ain't that what you want ??? Having "these error classes available to me
in all of the other packages in my library" ?

If you're worried about perfs or whatever, don't worry, a module is only
imported once - next imports will only bind the names in the importing
namespace.
I'm still pretty new to Python, and my
approachs are probably very rooted in C/C++ (I've had the hardest time
getting over not being able to overload functions), but am I doing
this correctly?
Yes, that's the right thing to do.
Also, are there any good tutorials/examples out there of how to
organize your python code into packges?
Most of the Python-specific aspects should be covered by the tutorial
(the one in the doc). Else, it's as usual, trying to have high cohesion
and low coupling.

Ah, yes, a couple of things:
- avoid the 'one-class-per-file' syndrom. It's perfectly ok to have tens
of classes in a same module
- plain functions are ok too - no need to stick everything in classes.

HTH
Sep 7 '07 #2
Ah, yes, a couple of things:
- avoid the 'one-class-per-file' syndrom. It's perfectly ok to have tens
Yes but i find it hard to edit classes easily when I have more than
one class per file.

Regards,
Ken

Sep 7 '07 #3
xkenneth <xk******@gmail.comwrites:
>Ah, yes, a couple of things:
- avoid the 'one-class-per-file' syndrom. It's perfectly ok to have tens

Yes but i find it hard to edit classes easily when I have more than
one class per file.
Why?
Sep 7 '07 #4
Paul Rudin wrote:
xkenneth <xk******@gmail.comwrites:
>>Ah, yes, a couple of things:
- avoid the 'one-class-per-file' syndrom. It's perfectly ok to have tens
Yes but i find it hard to edit classes easily when I have more than
one class per file.

Why?
Scroll-Blindness would be a good reason.

It would however be completely rediculous to create a file for every
10-liner class you have (and I have found that Python classes tend to be
rather short).

/W
Sep 7 '07 #5
On Sep 7, 2:04 pm, Wildemar Wildenburger
<lasses_w...@klapptsowieso.netwrote:
Paul Rudin wrote:
xkenneth <xkenn...@gmail.comwrites:
>Ah, yes, a couple of things:
- avoid the 'one-class-per-file' syndrom. It's perfectly ok to have tens
Yes but i find it hard to edit classes easily when I have more than
one class per file.
Why?

Scroll-Blindness would be a good reason.

It would however be completely rediculous to create a file for every
10-liner class you have (and I have found that Python classes tend to be
rather short).

/
Yes I agree, "Scroll-Blindness" it just gets long and confusing.
Navigating isn't so bad (emacs) here. I have another question for
something I don't quite understand.

How do import statements that are declared at the top of a python
module work?

for instance....

from MyModule.Objects import *

class Class:
def function:
#here i cannot access the things that should have been
imported from the above statement
#i printed the dir() function to verify this

This happens when I'm using the Class i just defined from another
python script. I can understand if that statement at the top if not
being executed when I import the above defined class, if so, I need to
access the classes from the other modules, so where do I put the
import statement? Is it proper to put import statements inside of
class function definitions? This solves my problem but seems VERY
incorrect coming from my old C/C++ ways.

Thanks Again!

Regards,
Ken
Sep 8 '07 #6
On Sat, 08 Sep 2007 12:42:19 -0700, xkenneth wrote:
How do import statements that are declared at the top of a python
module work?
They import the module. ;-)
for instance....

from MyModule.Objects import *

class Class:
def function:
#here i cannot access the things that should have been
imported from the above statement
#i printed the dir() function to verify this
Sorry I don't believe this. This doesn't even compile because the method
`function()` lacks the arguments in the definition. Please post actual
code and actual tracebacks you get.

And `MyModule` is a bad name for a package.

Ciao,
Marc 'BlackJack' Rintsch
Sep 8 '07 #7
>
How do import statements that are declared at the top of a python
module work?

for instance....

from MyModule.Objects import *

class Class:
def function:
#here i cannot access the things that should have been
imported from the above statement
#i printed the dir() function to verify this
I have seen this myself (stuff seemingly not imported) when 2 modules
import each other, perhaps indirectly, and try to use elements from
each other (at the global/declaration step, rather than in functions.
Logic that runs later (not during the import phase) still works fine,
however)). This sounds like the problem you're having, but your code
snippet isn't complete enough for me to tell. The way to fix this is
generally to move out stuff used by both modules into a separate
module.

Another reason can be that the imported module should be but isn't
setting the "__all__" variable. (in general the "import *" syntax is
frowned upon, it reduces readability).
Sep 8 '07 #8
How do import statements that are declared at the top of a python
module work?
http://docs.python.org/tut/node8.html
Sep 8 '07 #9
On Sep 8, 3:35 pm, David <wizza...@gmail.comwrote:
How do import statements that are declared at the top of a python
module work?

http://docs.python.org/tut/node8.html
On Sat, 08 Sep 2007 12:42:19 -0700, xkenneth wrote:
How do import statements that are declared at the top of a python
module work?
They import the module. ;-)
for instance....
from MyModule.Objects import *
class Class:
def function:
#here i cannot access the things that should have been
imported from the above statement
#i printed the dir() function to verify this

Sorry I don't believe this. This doesn't even compile because the
method
`function()` lacks the arguments in the definition. Please post
actual
code and actual tracebacks you get.
And `MyModule` is a bad name for a package.

Ciao,
Marc 'BlackJack' Rintsch

Code doesn't compile in python. This is pseudo code anyways.
Can't post actual code and tracebacks because the code is proprietary.
"MyModule" is pseudo code, and i forgot the arguments, the actual code
and errors are unimportant for this question.

Sep 9 '07 #10

On Sep 8, 2007, at 8:04 PM, xkenneth wrote:
>
Code doesn't compile in python. This is pseudo code anyways.
Can't post actual code and tracebacks because the code is proprietary.
"MyModule" is pseudo code, and i forgot the arguments, the actual code
and errors are unimportant for this question.
The code and traceback is important because the behavior you claim
is not easily explained without more information. How are we suppose
to help
you make your code run, when you can't even properly explain the
problem?

Cheers
Tommy
Sep 9 '07 #11
xkenneth a écrit :
>>Ah, yes, a couple of things:
- avoid the 'one-class-per-file' syndrom. It's perfectly ok to have tens


Yes but i find it hard to edit classes easily when I have more than
one class per file.
Why so ? Could it be that your classes are growing too fat ?
Sep 9 '07 #12
xkenneth a écrit :
On Sep 7, 2:04 pm, Wildemar Wildenburger
<lasses_w...@klapptsowieso.netwrote:
>>Paul Rudin wrote:
>>>xkenneth <xkenn...@gmail.comwrites:
>>>>>Ah, yes, a couple of things:
>- avoid the 'one-class-per-file' syndrom. It's perfectly ok to have tens

Yes but i find it hard to edit classes easily when I have more than
one class per file.
>>>Why?

Scroll-Blindness would be a good reason.

It would however be completely rediculous to create a file for every
10-liner class you have (and I have found that Python classes tend to be
rather short).

/


Yes I agree, "Scroll-Blindness" it just gets long and confusing.
Navigating isn't so bad (emacs) here.
Don't you use ECB ? (Emacs Code Browser) ?

But even without it, it's a problem of file size, not a problem of
number of classes.

(snip question about import, cf other answers)
Sep 9 '07 #13

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

Similar topics

3
by: Jamie | last post by:
Hi, Thanks for the excellent answer to my last question! One more: Does anyone have a method they follow for organizing stylesheets themselves? They seem like they can get bloated and hard to...
10
by: Rada Chirkova | last post by:
Hi, at NC State University, my students and I are working on a project called "self-organizing databases," please see description below. I would like to use an open-source database system for...
2
by: Eric S. Johansson | last post by:
I have a bunch of small modules that I use within my application. Most of these modules are single file modules. Currently, I have them set up as stand-alone modules but because it's a royal pain...
1
by: Ushach | last post by:
hi, I want to know about Self Organizing maps.T ounderstand the concept ,I need a small example which explains how clustering is done through self organizing maps?If any body implemented it plz...
4
by: Brian Blais | last post by:
Hello, I am trying to organize some of my code, and am having a little trouble with the import logic. I find I often have something like: MyPackage/ Part1/ # wants to use functions in...
5
by: Sascha Mombartz | last post by:
I can't believe how there aren't any decent solutions to help organize addresses and contact information. There should be an easier way to enter (automatic recognition), organize (tags) and...
5
by: ivarnelispam | last post by:
Hello all, I'm starting work on what is going to become a fairly substantial Python project, and I'm trying to find the best way to organize everything. The project will consist of: - A few...
2
by: bukzor | last post by:
I've been finding at work that I've written a set of functions several times, sometimes with more or less features or bugs, so I've decided to take my small, useful functions and put them in some...
1
by: eliben | last post by:
Hello, At the moment, I place all the code of my project in a src/ directory, and all the tests in a sibling tests/ directory, so for instance a sample project can look like this: doc/ ......
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:
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.