2

Here we have the piece: I am not sure, if I am doing it properly, one line is broken up after "+", the next one is artificially braced by "( )" to be able to break it up.

The idea behind the code itself is py.test checking, if the folder is properly ready and if the files are also existing... to be sure that the testing itself can run...

(A subquestion would be: "interesting to see how you doing such things...")

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 )

So how to do the line-break up and stay most readable?

Seen How to break a line of chained methods in Python? and still unsure...

Community
  • 1
  • 1
tverrbjelke
  • 1,234
  • 10
  • 15

5 Answers5

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)
jamylak
  • 128,818
  • 30
  • 231
  • 230
1

imho, you don't need the parenthesis at all.

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 )

but to be true, I'd make a more portable code using 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 )
zmo
  • 24,463
  • 4
  • 54
  • 90
  • 2
    Parentheses are not required, but [PEP-8](http://www.python.org/dev/peps/pep-0008/#maximum-line-length) does say their use for implicit line continuation is preferred over explicit line continuations. – chepner Jun 12 '13 at 13:38
  • ah, didn't know about that one. Thanks for telling me about a rule I won't respect :-) furthermore, making an opening parenthesis at end of line, only for line continuation is imho way more ugly and does not help ease code reading. – zmo Jun 12 '13 at 13:42
  • @chepner: I think the idea is that it's better to use those when you're actually using parentheses in the statement. I don't construe that to indicate that explicit parentheses for line continuation are favored over explicit backslashes. Have I misread PEP-8 after all? – Platinum Azure Jun 12 '13 at 13:43
  • 1
    The example given in the section I linked to explicitly adds unnecessary parentheses for the sole purpose of invoking implicit line continuation. I think it's pretty strongly implied that added parentheses are preferred over a backslash wherever possible. – chepner Jun 12 '13 at 13:44
  • 2
    The benefit of using implicit line continuation is that if you modify the expression in a way that changes the length of individual lines and "reflow" to stay under the maximum line length, you don't need to remove/relocate the backslash characters. – chepner Jun 12 '13 at 13:47
  • 1
    well that's true about that, but one of the things I like in python is how light it is in terms of parenthesis and brackets, so when I break a line for a line width rule, I usually try to do it on a term where I can avoid the line continuation character, otherwise I prefer to make it obvious and visible "hey look! here, I break the line !" – zmo Jun 12 '13 at 16:46
  • what about getting one of these newfangled wide-screen monitors, instead of trying to make your code fit on a VT100? – rbp Feb 05 '14 at 17:01
  • I already got a 1980x1200 display, and I'm planning on buying a 4k display. But it's not because I got a gigapixel wide screen that I want to have gigapixel wide codes. Keeping the code narrow improves readability and enables to have several source codes side by side. – zmo Feb 06 '14 at 11:00
1

In this case, I would shorten the name of os.path -- especially since it's namespace is limited to the function anyway:

import os.path as op

Also, generally when I do assertions, if I want to supply a custom string, I raise it explicitly: ...

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 )

Of course, that's just my preference (and there are some advantages to the assert statement. You're free to do whatever you want (including violating PEP 8) if you think it makes your code easier to read. After all, one of the first lines in that document says that it's important to know when to break the rules.

Finally, sometimes the best thing you can do to make your code easier to read is to use an appropriately named temporary variable.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • you mean temporary variable like this... looks indeed better for me. msg = ""Oops! the fixture dir should be here " + self.fixture_dir assert os.access(self.fixture_dir, os.F_OK), msg – tverrbjelke Jun 13 '13 at 08:37
1

Some of the long lines can be shortened by using simpler names:

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 )

Whether this is easier to read or not depends on the reader.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
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
        )
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143