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

python project layout

Hi,

What do you guys recommend in terms of python project layout, especially
unit tests layout ?
Zope has unit tests per packages, twisted has a big tests directory full of
tests ... and the file naming convention are also pretty differents ...
I guess there is part of "personnal choices" in those matters ... but there
is also good practice advices that can be gathered I think ...

- h

Jul 19 '05 #1
6 2525
Huron wrote:
What do you guys recommend in terms of python project layout, especially
unit tests layout ?
Zope has unit tests per packages, twisted has a big tests directory full of
tests ... and the file naming convention are also pretty differents ...
I guess there is part of "personnal choices" in those matters ... but there
is also good practice advices that can be gathered I think ...


(This is the result of a few years of evolution of our source tree
conventions.) We place all tests in subfolders (named "tests") of the
folders containing the files under test. Unit tests are named for the
unit (module) they are testing, so serial.py would have a unit test file
at tests/serial_unit.py.

"Acceptance" tests (higher level usually black-box functional tests) are
in the "tests" folder of the top level source folder, along with any
unit tests that reside there, and take their name from the number of the
requirement that defined them. Since we're an XP team, we call these
requirements "stories", so the files are named exciting things like
"story001.py" and "story197.py". The specific numbers match up with
either index cards with descriptions of the requirements, or an online
"issue tracker", depending on the project.

We have a little (quite simple) internal utility that lets us easily run
all unit tests under the current folder, or all acceptance tests in the
project, with one command. (Specifically, "uix" and "stix" for unit and
story tests, respectively.) This "test runner" utility scans subfolders
(doing an os.walk basically) and executes each test script in a new
process, to avoid most potential undesirable interactions between tests.

The benefits are straightforward. Tests are easy to find for any file,
but don't "clutter up" the same folder as the files under test. The
naming conventions makes it easy to automate things like the test runner
or other utilities. Running tests (always from a console) takes only a
few seconds so we can do it often. Multiple projects are easily
supported in other directory trees.

-Peter
Jul 19 '05 #2
Hi Peter,

Thanks for you detailed reply.
The layout that you suggest sounds wise to me (I'm about to start a
project).
I confess that, coming from the java world, I've trouble to move away from
the paradigm "one class" = "one file" ... and to gather things inside
"python modules" ...

-- huron
Jul 21 '05 #3
Hi again,

For instance, if you have several classes implementing the same interface
(say, doing the same things with different strategies), how would you
organize that in terms of files (modules) and directories (packages) ?

-- huron

Jul 21 '05 #4
Huron wrote:
Hi Peter,
Thanks for you detailed reply.
The layout that you suggest sounds wise to me (I'm about to start a
project).


I wouldn't necessarily recommend something so complex (not that it's
particular complex, but it's more than just "flat") for a newcomer,
however. One of the few small issues you have to deal with in order to
get such a layout working properly is the "python path" issue, and how
to import modules that are in another folder.

Normally (aside from the standard library and extensions) only the
current directory is in sys.path. Any subfolders that contain
__init__.py modules are "packages" and you can import from them using
the "dotted" notation (e.g. "import mypkg.mymodule" will try to load a
file from ./mypkg/mymodule.py if there is also a ./mypkg/__init__.py
(even if that file is empty!)).

To import a module in the *parent* directory, however, is another story
entirely. You need to get it added to the sys.path, and that's one of
the things done (dynamically) by our test utilities, so we can be in the
tests subfolder and type "story015.py" and have it load modules in the
parent folder.

So in short, start simple and let your layout evolve as you need it to.
Don't try to start with a more complex layout than you need or know
how to handle. And if you're using a revision control system like
Subversion (and you better be using something! :-) ) then it's an easy
matter to rename or move folders at a later time, without losing your
revision history.

-Peter
Jul 21 '05 #5
Huron wrote:
For instance, if you have several classes implementing the same interface
(say, doing the same things with different strategies), how would you
organize that in terms of files (modules) and directories (packages) ?


A concrete example would probably help. What are you envisioning?
Jul 21 '05 #6
> A concrete example would probably help. What are you envisioning?

Let say you have an interface (zope.interface) IMyProduct and two classes
implementing the interface MyProduct1 and MyProduct2 ...
But I think I found the answer. One file each.
Python is just more flexible than java in the sens that you dont NEED to do
"one class" = "one file"

-- h

Jul 21 '05 #7

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

Similar topics

7
by: Carlos Ribeiro | last post by:
I'm doing some experiments with mini declarative languages (as explained by David Mertz in http://www-106.ibm.com/developerworks/library/l-cpdec.html) in Python, with the intention to use it as...
16
by: Roman Susi | last post by:
Hi! Out of curiosity, how do I draw functions outside classes with UML? How module could be drawn in this case? More theoretical question is if I create classes on the fly, how UML can...
40
by: tleeuwenburg | last post by:
Greetings all, Some of you may have noticed the launch of the Python Journal a while back. Due to artistic differences, the journal has now been re-launched as The Python Papers. It is available...
31
by: sdoty044 | last post by:
Just wondering on what peoples opinions are of the GUIs avaiable for Python? All I am doing is prompting users for some data (listbox, radio buttons, text box, ect...). Then I will have some...
0
by: Gabriel Genellina | last post by:
QOTW: ""Given that C++ has pointers and typecasts, it's really hard to have a serious conversation about type safety with a C++ programmer and keep a straight face. It's kind of like having a guy...
19
by: Sam | last post by:
A friend of mine is picking up some Python and is frustrated by Python's indentation rules (http://greatbiggary.livejournal.com/ 260460.html?thread=1835884#t1835884). Personally, I've never had...
0
by: Gabriel Genellina | last post by:
QOTW: "GHUM: There are no big applications written in Python. GHUM: Big applications are written in JAVA or COBOL or C# or other legacy programming systems. GHUM: If you programm in...
0
by: mathieu | last post by:
Hi there, I am currently involved in managing a large python/c++ project (using swig to automagically wrap c++ code to python). So far my current experience was that python was a second class...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.