I'm running Python 2.6.1 on Windows XP SP3. My IDE is PyCharm 1.0-Beta 2 build PY-96.1055.
我正在Windows XP SP3上运行Python 2.6.1。我的IDE是PyCharm 1.0-Beta 2 build PY-96.1055。
I'm storing my .py files in a directory named "src"; it has an __init__.py
file that's empty except for an "__author__
" attribute at the top.
我将.py文件存储在名为“src”的目录中;它有一个__init__。除了顶部的“__author__”属性外,py文件是空的。
One of them is called Matrix.py:
其中一个叫matrix。py:
#!/usr/bin/env python
"""
"Core Python Programming" chapter 6.
A simple Matrix class that allows addition and multiplication
"""
__author__ = 'Michael'
__credits__ = []
__version__ = "1.0"
__maintainer__ = "Michael"
__status__ = "Development"
class Matrix(object):
"""
exercise 6.16: MxN matrix addition and multiplication
"""
def __init__(self, rows, cols, values = []):
self.rows = rows
self.cols = cols
self.matrix = values
def show(self):
""" display matrix"""
print '['
for i in range(0, self.rows):
print '(',
for j in range(0, self.cols-1):
print self.matrix[i][j], ',',
print self.matrix[i][self.cols-1], ')'
print ']'
def get(self, row, col):
return self.matrix[row][col]
def set(self, row, col, value):
self.matrix[row][col] = value
def rows(self):
return self.rows
def cols(self):
return self.cols
def add(self, other):
result = []
for i in range(0, self.rows):
row = []
for j in range(0, self.cols):
row.append(self.matrix[i][j] + other.get(i, j))
result.append(row)
return Matrix(self.rows, self.cols, result)
def mul(self, other):
result = []
for i in range(0, self.rows):
row = []
for j in range(0, other.cols):
sum = 0
for k in range(0, self.cols):
sum += self.matrix[i][k]*other.get(k,j)
row.append(sum)
result.append(row)
return Matrix(self.rows, other.cols, result)
def __cmp__(self, other):
"""
deep equals between two matricies
first check rows, then cols, then values
"""
if self.rows != other.rows:
return self.rows.cmp(other.rows)
if self.cols != other.cols:
return self.cols.cmp(other.cols)
for i in range(0, self.rows):
for j in range(0, self.cols):
if self.matrix[i][j] != other.get(i,j):
return self.matrix[i][j] == (other.get(i,j))
return True # if you get here, it means size and values are equal
if __name__ == '__main__':
a = Matrix(3, 3, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = Matrix(3, 3, [[6, 5, 4], [1, 1, 1], [2, 1, 0]])
c = Matrix(3, 3, [[2, 0, 0], [0, 2, 0], [0, 0, 2]])
a.show()
b.show()
c.show()
a.add(b).show()
a.mul(c).show()
I've created a new directory named "test" that also has an __init__.py
file that's empty except for an "__author__
" attribute at the top. I've created a MatrixTest.py to unit my Matrix class:
我创建了一个名为“test”的新目录,它也有一个__init__。py文件是空的,除了顶部的“__author__”属性。我已经创建了一个MatrixTest。py到单位矩阵类:
#!/usr/bin/env python
"""
Unit test case for Matrix class
See http://jaynes.colorado.edu/PythonGuidelines.html#module_formatting for Python coding guidelines
"""
import unittest #use my unittestfp instead for floating point
from src import Matrix # Matrix class to be tested
__author__ = 'Michael'
__credits__ = []
__license__ = "GPL"
__version__ = "1.0"
__maintainer__ = "Michael"
__status__ = "Development"
class MatrixTest(unittest.TestCase):
"""Unit tests for Matrix class"""
def setUp(self):
self.a = Matrix.Matrix(3, 3, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
self.b = Matrix.Matrix(3, 3, [[6, 5, 4], [1, 1, 1], [2, 1, 0]])
self.c = Matrix.Matrix(3, 3, [[2, 0, 0], [0, 2, 0], [0, 0, 2]])
def testAdd(self):
expected = Matrix.Matrix(3, 3, [[7, 7, 7], [5, 6, 7], [9, 9, 9]]) # need to learn how to write equals for Matrix
self.a.add(self.b)
assert self.a == expected
if __name__ == '__main__': #run tests if called from command-line
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
unittest.TextTestRunner(verbosity=2).run(suite)
Yet when I try to run my MatrixTest I get this error:
但是当我试着运行我的矩阵时,我得到了这个错误:
C:\Tools\Python-2.6.1\python.exe "C:/Documents and Settings/Michael/My Documents/Projects/Python/learning/core/test/MatrixTest.py"
Traceback (most recent call last):
File "C:/Documents and Settings/Michael/My Documents/Projects/Python/learning/core/test/MatrixTest.py", line 8, in <module>
from src import Matrix # Matrix class to be tested
ImportError: No module named src
Process finished with exit code 1
Everything I've read tells me that having the init.py in all my directories should take care of this.
我读过的所有东西都告诉我有init。在我的所有目录中,py应该负责这个。
If someone could point out what I've missed I'd greatly appreciate it.
如果有人能指出我错过了什么,我会非常感激。
I'd also like advice on the best way to develop and maintain source and unit test classes. I'm thinking about this the way I usually do when I write Java: /src and /test directories, with identical package structures underneath. Is this "Pythonic" thinking, or should I consider another organization scheme?
我还想要关于开发和维护源代码和单元测试类的最佳方法的建议。在编写Java: /src和/test目录时,我通常是这样考虑这个问题的,下面有相同的包结构。这是“毕达哥拉斯式”的想法,还是我应该考虑另一个组织方案?
UPDATE:
更新:
Thanks to those who have answered, here's the solution that worked for me:
感谢那些回答我的人,以下是对我有效的解决办法:
- Change import to
from src import Matrix # Matrix class to be tested
- 将导入从src导入到要测试的# Matrix类
- Add
sys.path
as an environment variable to my unittest configuration, with ./src and ./test directories separated by semi-colon. - 添加系统。作为环境变量的路径到我的unittest配置,用./src和./test目录用分号分隔。
- Change declarations in MatrixTest.py as shown.
- 改变MatrixTest声明。py如图所示。
2 个解决方案
#1
14
This is a bit of a guess, but I think you need to change your PYTHONPATH environment variable to include the src and test directories.
这只是一个猜测,但是我认为您需要更改PYTHONPATH环境变量来包含src和测试目录。
Running programs in the src
directory may have been working, because Python automatically inserts the directory of the script it is currently running into sys.path
. So importing modules in src
would have worked as long as you are also executing a script that resides in src
.
在src目录中运行程序可能是有效的,因为Python会自动将当前运行的脚本的目录插入到sys.path中。因此,在src中导入模块可以工作,只要您还在执行一个驻留在src中的脚本。
But now that you are running a script from test
, the test
directory is automatically added to sys.path
, while src
is not.
但是现在您正在运行一个来自test的脚本,测试目录将自动添加到sys中。路径,而src不是。
All directories listed in PYTHONPATH get added to sys.path
, and Python searches sys.path
to find modules.
所有在PYTHONPATH中列出的目录都被添加到sys中。path和Python搜索系统。路径找到模块。
Also, if you say
另外,如果你说
from src import Matrix
then Matrix
would refer to the package, and you'd need to say Matrix.Matrix
to access the class.
然后矩阵会指向包,你需要输入矩阵。矩阵访问类。
#2
1
Regarding best practices, PycURL uses a tests
directory at the same level as the main source code. On the other hand projects like Twisted or sorl-thumbnail use a test(s)
subdirectory under the main source code.
关于最佳实践,PycURL使用与主源代码相同级别的测试目录。另一方面,像Twisted或sorl-thumbnail这样的项目使用主源代码下的test(s)子目录。
The other half of the question has been already answered by ~unutbu.
这个问题的另一半已经被~unutbu解答了。
#1
14
This is a bit of a guess, but I think you need to change your PYTHONPATH environment variable to include the src and test directories.
这只是一个猜测,但是我认为您需要更改PYTHONPATH环境变量来包含src和测试目录。
Running programs in the src
directory may have been working, because Python automatically inserts the directory of the script it is currently running into sys.path
. So importing modules in src
would have worked as long as you are also executing a script that resides in src
.
在src目录中运行程序可能是有效的,因为Python会自动将当前运行的脚本的目录插入到sys.path中。因此,在src中导入模块可以工作,只要您还在执行一个驻留在src中的脚本。
But now that you are running a script from test
, the test
directory is automatically added to sys.path
, while src
is not.
但是现在您正在运行一个来自test的脚本,测试目录将自动添加到sys中。路径,而src不是。
All directories listed in PYTHONPATH get added to sys.path
, and Python searches sys.path
to find modules.
所有在PYTHONPATH中列出的目录都被添加到sys中。path和Python搜索系统。路径找到模块。
Also, if you say
另外,如果你说
from src import Matrix
then Matrix
would refer to the package, and you'd need to say Matrix.Matrix
to access the class.
然后矩阵会指向包,你需要输入矩阵。矩阵访问类。
#2
1
Regarding best practices, PycURL uses a tests
directory at the same level as the main source code. On the other hand projects like Twisted or sorl-thumbnail use a test(s)
subdirectory under the main source code.
关于最佳实践,PycURL使用与主源代码相同级别的测试目录。另一方面,像Twisted或sorl-thumbnail这样的项目使用主源代码下的test(s)子目录。
The other half of the question has been already answered by ~unutbu.
这个问题的另一半已经被~unutbu解答了。