conforming lines to 80 characters limits

80 views
Skip to first unread message

likage

unread,
Feb 26, 2017, 7:56:34 PM2/26/17
to Python Programming for Autodesk Maya
Hi all, 

I am trying to conform my code to fit into length width of 80 characters, pep8 style


Suppose if I have a code like this:
class something():
   
def __init__(self):
       
...
       
self.uiSetupNormalSmooth = cmds.radioButton(label='Geometry normal',
                                                    align
='right',
                                                   
onCommand=lambda*args:self.setupCallback("uiSetupNormalCol", True))


My question here is, for the onCommand line, the limit of 80 characters (from left to right) stops ar .`..self.s`, my question here would be how or what is the best way to conform this line?

Will it be like this:
        self.uiSetupNormalSmooth = cmds.radioButton(label='Geometry normal',
                                                    align
='right',
                                                    onCommand
=(lambda*args:
                                                                   
self.setupCallback("uiSetupNormalCol", True)))

Justin Israel

unread,
Feb 26, 2017, 8:36:38 PM2/26/17
to python_in...@googlegroups.com
I would recommend not trying to do so much in a single line:

cbk = lambda *args: self.setupCallback("uiSetupNormalCol", True)
self.uiSetupNormalSmooth = cmds.radioButton(label='Geometry normal',
                                            align='right',
                                            onCommand=cbk)

Justin

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/bf0a3f06-3327-4b67-a948-a637b487fff7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alok Gandhi

unread,
Feb 26, 2017, 10:57:51 PM2/26/17
to python_in...@googlegroups.com
I agree with Justin. Personally, I would slightly change this to (which is PEP8 compliant):
self.uiSetupNormalSmooth = cmds.radioButton(
    label='Geometry normal',
    align='right',
    onCommand=lambda*args: self.setupCallback(
        "uiSetupNormalCol",
        True,
    ),
)

likage

unread,
Feb 26, 2017, 11:36:14 PM2/26/17
to Python Programming for Autodesk Maya
Noted, thank you all!

Alok Gandhi

unread,
Feb 27, 2017, 12:22:50 AM2/27/17
to python_in...@googlegroups.com
@Justin: Just a side note to your example (though not a big deal):
cbk = lambda *args: self.setupCallback("uiSetupNormalCol", True)

It is not advised to assign lambda expressions to variables. It beats the whole purpose using the lambda in the first place.

Justin Israel

unread,
Feb 27, 2017, 2:15:57 AM2/27/17
to python_in...@googlegroups.com
Thanks for the info. 


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

likage

unread,
Feb 27, 2017, 2:43:55 PM2/27/17
to Python Programming for Autodesk Maya
I have another question - what about the function line?

Eg.
Class Something(object):
def __init__(self, rotate=((0, 0), (0, 0), (0, 0)), scale=((1, 1), (1, 1), (1, 1)), u_jitter=(0,0), v_jitter=(0,0)):

The character limit stops at the last open bracket of the scale parameter : `..., scale=((1, 1), (1, 1), (` 
Should/ Can it be break into two?

Justin Israel

unread,
Feb 27, 2017, 3:48:22 PM2/27/17
to python_in...@googlegroups.com
You are probably best suited to look for a pep8 formatter for your IDE of choice.
Personally, I don't always format to pep8. Rather just to a consistent style. Others will probably disagree.

You could either format this to have all your parameters on newlines:

  1. class Something(object):
  1. def __init__(self,
  2. rotate=((0, 0), (0, 0), (0, 0)),
  3. scale=((1, 1), (1, 1), (1, 1)),
  4. u_jitter=(0,0),
  5. v_jitter=(0,0)):
  1. pass
 
Or you could shorter the default arguments to None and handle them in the body of your constructor:

  1. class Something(object):
  2. def __init__(self, rotate=None, scale=None, u_jitter=None, v_jitter=None):
  3. rotate = rotate or ((0, 0), (0, 0), (0, 0))
  4. scale = scale or ((1, 1), (1, 1), (1, 1))
  5. u_jitter = u_jitter or (0,0)
  6. v_jitter = v_jitter or (0,0)

Or you could accept **kwargs and properly document your parameters.

Justin

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Alok Gandhi

unread,
Feb 27, 2017, 9:01:42 PM2/27/17
to python_in...@googlegroups.com
PEP8 stresses on increasing readability. When a line of code is long, it tends to bring in noise, thereby increasing cognitive burden on the reader. The reader has to invest more concentration into reading and comprehension. To reduce this burden, it is advised to keep the line shorter than 80 characters. PEP8 is just a style guide and has to be taken in that spirit only. It is not necessary to follow it to the word. As long as you write code that can be read without effort, it is fine. Personally, I follow it in most cases. I keep PEP8 on in my IDE/Editors, warning me whenever I am violating some PEP8 standard.

Another benefit of 80 line limit is that your code would fit nicely on github.com, consoles, portrait aligned monitors etc.

Personally, I would do the following:

class Something(object):
    def __init__(
        self,
        rotate=((0, 0), (0, 0), (0, 0)),
        scale=((1, 1), (1, 1), (1, 1)),
        u_jitter=(0, 0),
        v_jitter=(0, 0),
    ):
        pass

Reply all
Reply to author
Forward
0 new messages