代码管理 [英] Code Management

查看:56
本文介绍了代码管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的读者,


我正在用Python编写一些模块,而且我也在使用单元测试。我是/ b $ b想知道一些事情:


1)我应该将我的单元测试放在子目录中吗?子目录

是否必须是一个包?


2)主文件夹/ myproject必须是一个包吗?我应该直接在/ myproject下放置

我的模块,还是应该创建一个子文件夹,

例如/ myproject / modules


是否有人有任何最佳做法至于如何管理你的代码?


谢谢!

Dear Reader,

I''m writing some modules in Python, and I''m also using unittests. I''m
wondering about some things:

1) Should I put my unittests in a subdirectory? Does the subdirectory
have to be a package?

2) Does the main folder /myproject have to be a package? Should I put
my modules directly under /myproject, or should I create a subfolder,
for example /myproject/modules

Does anyone have any "best practices" as to how to manage your code?

Thanks!

推荐答案

让我们看看:
Let''s see:

1)我应该将我的unittests放在子目录中吗?子目录

必须是一个包吗?
1) Should I put my unittests in a subdirectory? Does the subdirectory
have to be a package?



当然,为了避免混乱,将测试放在一个名为''tests'的文件夹中'

(以及测试所需的任何输入,如模拟文件,输出文件使用

来测试输出是否正确等等,应该放在那个

文件夹中或''tests''的子文件夹中。


我喜欢这个设置:


myproject

... / package1

../../测试

... / .. / .. / mock_files

... / package2


等等


如果你想能够从python导入测试(比如,''来自

myproject.tests import test_all'' ),然后是的,你需要将

tests文件夹作为一个子包(只需删除一个空的__init__.py文件)

Sure, to avoid cluttering put the tests in a folder called ''tests''
(and any input the tests require, like mock files, output files used
to test if the output is correct, etc etc, should be put in that
folder or in a subfolder of ''tests''.

I like this setup:

myproject
.../ package1
../../ tests
.../../../ mock_files
.../package2

and so on

If you want to be able to import the tests from python (say, ''from
myproject.tests import test_all''), then yes, you need to make the
tests folder a subpackage (just drop an empty __init__.py file)


2)主文件夹/ myproject必须是一个包吗?我应该直接在/ myproject下我的模块,或者我应该创建一个子文件夹,

例如/ myproject / modules
2) Does the main folder /myproject have to be a package? Should I put
my modules directly under /myproject, or should I create a subfolder,
for example /myproject/modules



当然... make / myproject包。现在,如果你的模块有点小b $ b,你可以将它们转储到/ myproject中,但如果你正在做什么

相当复杂,最好搬家每个模块到它自己的文件夹。

Of course... make /myproject a package. Now, if your modules are kinda
small you can just dump them in /myproject, but if what you are doing
is fairly complex, it''s better to move each module to its own folder.


有没有人有任何最佳实践。至于如何管理你的代码?
Does anyone have any "best practices" as to how to manage your code?



我也想要最佳实践......任何人?


Sergio

PS:凯西,alltests.py的整洁想法。顺便说一句,我认为相对

进口可能会有所帮助:
http://www.python.org/dev/peps/pep-0...ido-s-decision


Jens< j3 **** @ gmail.comwrites:
Jens <j3****@gmail.comwrites:

11月21日,04:16,Jens< j3n。 .. @ gmail.comwrote:
On 21 Nov., 04:16, Jens <j3n...@gmail.comwrote:

11月21日,01:46,brzr ... @ gmail.com写道:

假人/

dummy_package /

__init__.py

moduleA.py

tests /

__init__.py

test.py
On 21 Nov., 01:46, brzr...@gmail.com wrote:
dummy/
dummy_package/
__init__.py
moduleA.py
tests/
__init__.py
test.py



为避免混淆,作为包的目录应命名为

,因为你想要出现进口;例如如果你想''导入

foo.module_a'',那么将包含''module_a.py''的目录命名为

''foo /''。


这通常导致::


foo /

setup.py

foo /

__init__.py

module_a.py

test /

__init__.py

test_module_a.py


也就是说,* project *目录(包含所有文件)名为

''foo /''; * package *目录(其中所有实现代码都是

找到)被命名为''foo / foo /'',单元测试可以在

目录中找到''foo / test /''。


这是正常的,但如果它让你感到困惑,你可能想要重命名

项目目录。我经常在多个

版本控制分支上进行开发,因此每个项目目录都以它包含的

分支命名;在每个内容中,使用相同的''foo /''名称

作为包目录。

To avoid confusion, the directory that is the package should be named
as you want the imports to appear; e.g. if you want to ''import
foo.module_a'', then name the directory containing ''module_a.py'' as
''foo/''.

This often results in::

foo/
setup.py
foo/
__init__.py
module_a.py
test/
__init__.py
test_module_a.py

That is, the *project* directory (containing all the files) is named
''foo/''; the *package* directory (where all the implementation code is
found) is named ''foo/foo/'', and the unit tests are found in the
directory ''foo/test/''.

That''s normal, though if it confuses you you might want to rename the
project directory. I''m often doing development on multiple
version-control branches, so each project directory is named for the
branch it contains; within each of those, the same ''foo/'' name is used
for the package directory.


我正在使用Python 2.5.1。当我试图在''测试'中调用函数
''moduleA'时,除非我制作''dummy',否则它不会工作。

文件夹也是一个包。那太奇怪了。

''dummy_package''必须在我的pythonpath中吗?我如何从测试中获得
参考模块A?
I''m using Python 2.5.1. When I''m trying to call a function in
''moduleA'' from ''test'' it won''t work unless I make the ''dummy''
folder a package as well. That''s pretty weird. Does
''dummy_package'' have to be in my pythonpath or something? How do I
reference moduleA from test?



您应该将软件包安装到Python的导入路径

将找到它的位置。阅读使用标准库''distutils''

机制;它涉及写一个''setup.py''文件来定义

安装包的参数。

You should install the package into a place where Python''s import path
will find it. Read up on using the standard library ''distutils''
mechanism for this; it involves writing a ''setup.py'' file to define
the parameters for installation of your package.


我想避免在包中加入''dummy''。
I would like to avoid making ''dummy'' into a package as well.



是的。顶级目录用于包含许多文件,

包括''setup.py'',它不应该是已安装的

包的一部分。

Yes. The top-level directory is used for containing a number of files,
including ''setup.py'', that should not be part of the installed
package.


问题解决了。我在PYTHONPATH添加了''dummy''。 (对于我创建的每个项目,我真的有

吗?)无论如何,它的工作方式就像我现在要的那样。
Problem solved. I added ''dummy'' to the PYTHONPATH. (Do I really have
to do that for every project I create?) Anyway, it works the way I''d
like it to now.



我希望上面的内容更清楚我对这种情况的看法。


-

\真正的伟大是通过你给予多少自由度来衡量的。

` \其他人,而不是你能用多少强迫别人去做你所做的事情。

_o__)想要。 --Larry Wall |

Ben Finney

I hope the above makes it clearer what I prefer for this situation.

--
\ "True greatness is measured by how much freedom you give to |
`\ others, not by how much you can coerce others to do what you |
_o__) want." --Larry Wall |
Ben Finney


作为旁注,我发现丢弃PTH文件比搞乱更容易
带有pythonpath的
。如果您不熟悉PTH文件,我的工作是

这个


1)转到C:\Program Files \Python25 \\ \\Lib\site-包"或者在你的情况下是什么

适当的。

2)创建一个文本文件,命名为MyProjects.PTH。 (注意

扩展名!)

3)在文件中,只需写入包含所有

项目的文件夹的路径(在我的情况,C:/ docs / python)


我们的想法是将主要的python安装与你目前正在开发的

模块分开。你的python安装转到

" program files / python"或者bin / python,你的个人项目在其他地方(通常在你的''用户''文件夹内)上去了
。这可以平滑很多

的东西,比如使用不同版本的软件包开发。


2007年11月20日下午11:34,Ben Finney< bi **************** @ benfinney.id.auwrote:
As a side note, I find much easier to drop a PTH file than messing
with pythonpath. If you are not familiar with PTH files, what I do is
this

1) Go to "C:\Program Files\Python25\Lib\site-packages" or whatever is
appropiate in your case.
2) Create a text file, name it something like "MyProjects.PTH" (note
the extension!)
3) in the file, just write the path of the folder that contains all
your projects (in my case, C:/docs/python)

The idea is to keep the main python installation separated from the
modules you are currently developing. Your python installation goes to
"program files/python" or "bin/python", and your personal projects go
somewhere else (usually inside your ''user'' folder). This smooths many
things, like working with different versions of a package you are
developing.

On Nov 20, 2007 11:34 PM, Ben Finney <bi****************@benfinney.id.auwrote:

Jens < j3 **** @ gmail.comwrites:
Jens <j3****@gmail.comwrites:

11月21日,04:16,Jens< j3n ... @ gmail.comwrote:
On 21 Nov., 04:16, Jens <j3n...@gmail.comwrote:

11月21日,01:46,brzr ... @ gmail.com写道:

dummy /

dummy_package /

__init__.py

moduleA.py

tests /

__init __。py

test.py
On 21 Nov., 01:46, brzr...@gmail.com wrote:
dummy/
dummy_package/
__init__.py
moduleA.py
tests/
__init__.py
test.py



为了避免混淆,作为包的目录应命名为

as你想要出口进口;例如如果你想''导入

foo.module_a'',那么将包含''module_a.py''的目录命名为

''foo /''。


这通常导致::


foo /

setup.py

foo /

__init__.py

module_a.py

test /

__init__.py

test_module_a.py


也就是说,* project *目录(包含所有文件)名为

''foo /''; * package *目录(其中所有实现代码都是

找到)被命名为''foo / foo /'',单元测试可以在

目录中找到''foo / test /''。


这是正常的,但如果它让你感到困惑,你可能想要重命名

项目目录。我经常在多个

版本控制分支上进行开发,因此每个项目目录都以它包含的

分支命名;在每个内容中,使用相同的''foo /''名称

作为包目录。


To avoid confusion, the directory that is the package should be named
as you want the imports to appear; e.g. if you want to ''import
foo.module_a'', then name the directory containing ''module_a.py'' as
''foo/''.

This often results in::

foo/
setup.py
foo/
__init__.py
module_a.py
test/
__init__.py
test_module_a.py

That is, the *project* directory (containing all the files) is named
''foo/''; the *package* directory (where all the implementation code is
found) is named ''foo/foo/'', and the unit tests are found in the
directory ''foo/test/''.

That''s normal, though if it confuses you you might want to rename the
project directory. I''m often doing development on multiple
version-control branches, so each project directory is named for the
branch it contains; within each of those, the same ''foo/'' name is used
for the package directory.


我正在使用Python 2.5.1。当我试图在''测试'中调用函数
''moduleA'时,除非我制作''dummy',否则它不会工作。

文件夹也是一个包。那太奇怪了。

''dummy_package''必须在我的pythonpath中吗?我如何从测试中获得
参考模块A?
I''m using Python 2.5.1. When I''m trying to call a function in
''moduleA'' from ''test'' it won''t work unless I make the ''dummy''
folder a package as well. That''s pretty weird. Does
''dummy_package'' have to be in my pythonpath or something? How do I
reference moduleA from test?



您应该将软件包安装到Python的导入路径

将找到它的地方。阅读使用标准库''distutils''

机制;它涉及写一个''setup.py''文件来定义

安装包的参数。


You should install the package into a place where Python''s import path
will find it. Read up on using the standard library ''distutils''
mechanism for this; it involves writing a ''setup.py'' file to define
the parameters for installation of your package.


我想避免在包中加入''dummy''。
I would like to avoid making ''dummy'' into a package as well.



是的。顶级目录用于包含许多文件,

包括''setup.py'',它不应该是已安装的

包的一部分。


Yes. The top-level directory is used for containing a number of files,
including ''setup.py'', that should not be part of the installed
package.


问题解决了。我在PYTHONPATH添加了''dummy''。 (对于我创建的每个项目,我真的有

吗?)无论如何,它的工作方式就像我现在要的那样。
Problem solved. I added ''dummy'' to the PYTHONPATH. (Do I really have
to do that for every project I create?) Anyway, it works the way I''d
like it to now.



我希望上面的内容让我更清楚我对这种情况的看法。


-

\真正的伟大是通过你给予多少自由度来衡量的。

` \其他人,而不是你能用多少强迫别人去做你所做的事情。

_o__)想要。 --Larry Wall |

Ben Finney


-
http://mail.python.org/mailman/listinfo/python-list


这篇关于代码管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆