473,467 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Organizing python code

18 New Member
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 2174
bartonc
6,596 Recognized Expert Expert
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
PythonNewbie
18 New Member
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 Recognized Expert Expert
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 Recognized Expert Expert
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
PythonNewbie
18 New Member
Thanks as always! Time to clean it up - hopefully I wont break it beyond fix!
Feb 16 '07 #6
bartonc
6,596 Recognized Expert Expert
Thanks as always! Time to clean it up - hopefully I wont break it beyond fix!
You are welcome. Good luck.
Feb 16 '07 #7

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

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...
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...
0
by: Jeff Rush | last post by:
OSCON 2007 in Portland, Oregon from July 23-27 is fast approaching. This is a professional conference that can give Python a lot of visibility in the IT world and draws a different crowd from our...
12
by: xkenneth | last post by:
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...
6
by: xkenneth | last post by:
All, Just a quick question. I want to be able to have a data structure that organizes data (timestamps I'm working with) sequentially, so that i can easily retrieve the first x amount of...
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/ ......
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
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,...
1
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.