473,545 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2536
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.p y" and "story197.p y". 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.p y" 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
1567
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 the mechanism to define data entry forms. My final goal is to have a simple approach to automatic generation of visual interfaces. The complete...
16
5328
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 reflect that? (I know that Python code itself is best at communication design ideas, but there are some people which prefer to talk UML.)
40
2182
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 under a Creative Commons License, something we felt was appropriate given its nature. Many here commented that this was important to them, and it...
31
2452
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 text output, maybe a scrolling text message as things are happening. I have some minor things I need to do, for example, if Checkbutton X is...
0
1074
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 who juggles chainsaws wearing body armor arguing with a guy who juggles rubber chickens wearing a T-shirt about who's in more danger." - Roy Smith,...
19
1717
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 any issues with Python's ways of indentation, but that conversation got me thinking about the issue. Consider the following indentation rules: 1....
0
1108
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 Python, your applications become quite small. Only frameworks in Python are big. JMC: So the fact that there are no big applications written in Python...
0
1044
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 citizen and extremely little python code was written and everything seemed to work. Now this is the contrary, large portion of code are written...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7752
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.