470,643 Members | 1,229 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,643 developers. It's quick & easy.

Organizing python code

Hello, I am trying to organize code that a student had written. Currently it is one really long file of different classes which makes it very tough to follow things. I would prefer to have meaningful groups of classes in different files. Can someone suggest a solution.

Sorry, a rather simple question.
Feb 16 '07 #1
6 1954
bartonc
6,596 Expert 4TB
Hello, I am trying to organize code that a student had written. Currently it is one really long file of different classes which makes it very tough to follow things. I would prefer to have meaningful groups of classes in different files. Can someone suggest a solution.

Sorry, a rather simple question.
Hello PythonNewbie. Long time no post.
Perhaps you can discover some meaningful grouping by looking at the underlieing data. All classes that deal with a particular chunk of data will go into one module. Eventually there will be (hopefully) one set of functions and classes which oversees the creation and communication between all the others.
Check out the new Python > Code Forum for some examples. Links to these fourms are in a sticky above and in the announcement. Perhaps I will be able to draw a picture as well.
Feb 16 '07 #2
Well, let me be little more precise.

This program basically takes a big input files and sends it to different servers that host different analysis program by posting on forms. Then it submits the reports in a local database. It works fairly ok but it has absolutely no comments and I need to add some functionality to it.

Right now the code is:

class 1

class 2
.
.
.
class n-1

class n

I would like to separate out the classes that deal with posting data on a website, the one that deals with local database, ones that checks if the input is ok, etc etc. in separate files. Can I just copy paste classes, say 1, 4 and 6, that deal with posting the input file to different web servers into a separate new file? Then import that file in the file that contains the "main file"? Does that make sense? I guess they refer to things like this as a package in programming lingo.

I will look at the link you suggested. Thanks!
Feb 16 '07 #3
bartonc
6,596 Expert 4TB
Well, let me be little more precise.

This program basically takes a big input files and sends it to different servers that host different analysis program by posting on forms. Then it submits the reports in a local database. It works fairly ok but it has absolutely no comments and I need to add some functionality to it.

Right now the code is:

class 1

class 2
.
.
.
class n-1

class n

I would like to separate out the classes that deal with posting data on a website, the one that deals with local database, ones that checks if the input is ok, etc etc. in separate files. Can I just copy paste classes, say 1, 4 and 6, that deal with posting the input file to different web servers into a separate new file? Then import that file in the file that contains the "main file"? Does that make sense? I guess they refer to things like this as a package in programming lingo.

I will look at the link you suggested. Thanks!
Anything in the huge file that looks like
Expand|Select|Wrap|Line Numbers
  1. class myClass(superClass):
  2.     def etc():
  3.         pass # etc
is quite simply moved to it's own module (in the lingo).
In the main module, it is then refered to as
Expand|Select|Wrap|Line Numbers
  1. import moduleName
  2. instance = moduleName.myClass()
But thing in a module that look like
Expand|Select|Wrap|Line Numbers
  1. a = 10
with no indentation are actually run at import time and you'll need to decide (or find out by trial and error) which classes make use of them. One way around that is
Expand|Select|Wrap|Line Numbers
  1. from moduleName import *
but this practice is discouraged because it defeats the purpose of your original post.
Feb 16 '07 #4
bartonc
6,596 Expert 4TB
Anything in the huge file that looks like
Expand|Select|Wrap|Line Numbers
  1. class myClass(superClass):
  2.     def etc():
  3.         pass # etc
is quite simply moved to it's own module (in the lingo).
In the main module, it is then refered to as
Expand|Select|Wrap|Line Numbers
  1. import moduleName
  2. instance = moduleName.myClass()
But thing in a module that look like
Expand|Select|Wrap|Line Numbers
  1. a = 10
with no indentation are actually run at import time and you'll need to decide (or find out by trial and error) which classes make use of them. One way around that is
Expand|Select|Wrap|Line Numbers
  1. from moduleName import *
but this practice is discouraged because it defeats the purpose of your original post.
One additional trick often used is
Expand|Select|Wrap|Line Numbers
  1. def _semiPrivate():
  2.     # your code here
Such semi private functions probable belong in the main module. They are not import with the * syntax. You then refer to them as
Expand|Select|Wrap|Line Numbers
  1. moduleName._semiPrivate()
Feb 16 '07 #5
Thanks as always! Time to clean it up - hopefully I wont break it beyond fix!
Feb 16 '07 #6
bartonc
6,596 Expert 4TB
Thanks as always! Time to clean it up - hopefully I wont break it beyond fix!
You are welcome. Good luck.
Feb 16 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Jamie | last post: by
reply views Thread by Jeff Rush | last post: by
12 posts views Thread by xkenneth | last post: by
5 posts views Thread by ivarnelispam | last post: by
2 posts views Thread by bukzor | last post: by
1 post views Thread by eliben | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.