2

这里我们有一块:我不确定,如果我做得正确,“+”之后的一行被拆分,下一行被“()”人为地支撑以便能够拆分它。

代码本身背后的想法是 py.test 检查文件夹是否已正确准备好并且文件是否也存在......以确保测试本身可以运行......

(一个子问题是:“很高兴看到你是如何做这些事情的......”)

class TestFilewise():
    def setup(self):
        import os
        self.fixture_dir = ( os.path.abspath(os.path.dirname(__file__)) + 
              "/fixtures/" )
        assert os.access( self.fixture_dir, os.F_OK ), (
              "Oops! the fixture dir should be here " + self.fixture_dir )
        assert os.access( self.fixture_dir+"profiles-source1.csv", os.F_OK )

那么如何进行换行并保持最易读呢?

如何在 Python 中打破一行链式方法?仍然不确定...

4

5 回答 5

1
import os

class TestFilewise():
    def setup(self):
        self.fixture_dir = '%s/fixtures/' % \
            os.path.abspath(os.path.dirname(__file__))
        assert os.access(self.fixture_dir, os.F_OK), \
            'Oops! the fixture dir should be here: %s' % self.fixture_dir
        assert os.access(
            '%sprofiles-source1.csv/' % self.fixture_dir, os.F_OK
        )
于 2013-06-12T14:00:54.263 回答
1
import os

class TestFilewise():
    def setup(self):
        self.fixture_dir = (os.path.abspath(os.path.dirname(__file__)) +
                            "/fixtures/")
        assert os.access(self.fixture_dir, os.F_OK), \
               "Oops! the fixture dir should be here " + self.fixture_dir
        assert os.access( self.fixture_dir+"profiles-source1.csv", os.F_OK)
于 2013-06-12T13:35:26.203 回答
1

在这种情况下,我会缩短 os.path 的名称——尤其是因为它的命名空间无论如何都仅限于函数:

import os.path as op

另外,通常当我做断言时,如果我想提供一个自定义字符串,我会明确提出它:...

class TestFilewise():
    def setup(self):
        import os.path as op
        self.fixture_dir = op.join(op.abspath(op.dirname(__file__)),"fixtures")
        if not os.access( self.fixture_dir, os.F_OK ):
            raise AssertionError("Oops! "
                      "the fixture dir should be here " + self.fixture_dir )

        csvfile = op.join(self.fixture_dir,"profiles-source1.csv")
        assert os.access(csvfile, os.F_OK )

当然,这只是我的偏好(并且该assert声明有一些优点。如果您认为它使您的代码更易于阅读,您可以自由地做任何您想做的事情(包括违反 PEP 8)。毕竟,这是第一个该文件中的几行表明知道何时违反规则很重要。

最后,有时使代码更易于阅读的最佳方法是使用适当命名的临时变量。

于 2013-06-12T13:58:10.063 回答
1

一些长行可以通过使用更简单的名称来缩短:

class TestFilewise():
    def setup(self):
        from os.path import abspath, dirname
        from os import access, F_OK
        fixture_dir = abspath(dirname(__file__)) + "/fixtures/"
        self.fixture_dir = fixture_dir
        exists = access(fixture_dir,F_OK)
        assert exists, "Oops! the fixture dir should be here " + fixture_dir
        assert access( fixture_dir+"profiles-source1.csv", F_OK )

这是否更容易阅读取决于读者。

于 2013-06-12T13:59:47.090 回答
1

恕我直言,你根本不需要括号。

class TestFilewise():
    def setup(self):
        import os
        self.fixture_dir = os.path.abspath(os.path.dirname(__file__)) \
              + "/fixtures/"
        assert os.access( self.fixture_dir, os.F_OK ), \
            "Oops! the fixture dir should be here " + self.fixture_dir
        assert os.access( self.fixture_dir+"profiles-source1.csv", os.F_OK )

但说实话,我会使用以下方法制作更便携的代码os.path.join

class TestFilewise():
    def setup(self):
        import os
        self.fixture_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                "/fixtures/")
        assert os.access( self.fixture_dir, os.F_OK ), \
            "Oops! the fixture dir should be here: '%s'" % self.fixture_dir
        assert os.access(os.path.join(self.fixture_dir, 
                    "profiles-source1.csv"), os.F_OK )
于 2013-06-12T13:36:45.037 回答