pep8

How should I format a long url in a python comment and still be PEP8 compliant

∥☆過路亽.° 提交于 2019-11-27 11:37:59
问题 In a block comment, I want to reference a URL that is over 80 characters long. What is the preferred convention for displaying this URL? I know bit.ly is an option, but the URL itself is descriptive. Shortening it and then having a nested comment describing the shortened URL seems like a crappy solution. 回答1: Don't break the url: # A Foolish Consistency is the Hobgoblin of Little Minds [1] # [1]: http://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds

Line continuation for list comprehensions or generator expressions in python

痞子三分冷 提交于 2019-11-27 11:16:14
How are you supposed to break up a very long list comprehension? [something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are_pretty_long] I have also seen somewhere that people that dislike using '\' to break up lines, but never understood why. What is the reason behind this? [x for x in (1,2,3) ] works fine, so you can pretty much do as you please. I'd personally prefer [something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are_pretty_long] The reason why \ isn't appreciated very much is that it appears at the end of a line, where it

Python: using 4 spaces for indentation. Why? [closed]

一世执手 提交于 2019-11-27 11:10:51
While coding python I'm using only 2 spaces to indent, sure PEP-8 really recommend to have 4 spaces, but historically for me it's unusual. So, can anyone convince me to use 4 spaces instead of 2? What pros and cons? P.S. And finally, what's easy way to convert all existing codebase from 2 spaces to 4 spaces? P.P.S. PEP-8 Also srictly recommend not using tabs for indention. read here So, to summarize: Pros: Have more space to arrange when wraping string more than 80 lines long. Can copy code from snippets and it just works. Cons: With deeper level of nested statements you have less space for

Verifying PEP8 in iPython notebook code

半城伤御伤魂 提交于 2019-11-27 10:29:57
问题 Is there an easy way to check that iPython notebook code, while it's being written, is compliant with PEP8? 回答1: In case this helps anyone, I'm using: conttest "jupyter nbconvert notebook.ipynb --stdout --to script | flake8 - --ignore=W391" conttest reruns when saving changes to the notebook flake8 - tells flake8 to take input from stdin --ignore=W391 - this is because the output of jupyter nbconvert seems to always have a "blank line at end of file", so I don't want flake8 to complain about

How to write very long string that conforms with PEP8 and prevent E501

一笑奈何 提交于 2019-11-27 10:26:30
As PEP8 suggests keeping below the 80 column rule for your python program, how can I abide to that with long strings, i.e. s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten." How would I go about expanding this to the following line, i.e. s = "this is my really, really, really, really, really, really" + "really long string that I'd like to shorten." Implicit concatenation might be the cleanest solution: s = "this is my really, really, really, really, really, really," \ " really long string that I'd like to shorten." Edit On reflection I

Correct style for line breaks when chaining methods in Python

早过忘川 提交于 2019-11-27 09:58:19
问题 I have some code like this. Should the break occur before the periods or after? # before my_var = somethinglikethis.where(we=do_things).where(we=domore).where(we=everdomore) # this way my_var = somethinglikethis.where(we=do_things) \ .where(we=domore) \ .where(we=everdomore) # or this way my_var = somethinglikethis.where(we=do_things). \ where(we=domore). \ where(we=everdomore) 回答1: PEP 8 recommends using parenthesis so that you don't need \ , and gently suggests breaking before binary

How can I use Emacs Flymake mode for python with pyflakes and pylint checking code?

落爺英雄遲暮 提交于 2019-11-27 09:34:09
问题 For checking code in python mode I use flymake with pyflakes Also I want check code style (pep8) with pylint (description on the same page with pyflakes) This solutions work. But I can't configure flymake for work with pyflakes and pylint together. How can I do it? 回答1: Well, flymake is just looking for a executable command thats output lines in a predefined format. You can make a shell script for example that will call successively all the checkers you want... You must also make sure that

pep8 compliant deep dictionary access

江枫思渺然 提交于 2019-11-27 06:39:04
问题 What is the pep8 compliant way to do deep dictionary access? dct = { 'long_key_name_one': { 'long_key_name_two': { 'long_key_name_three': { 'long_key_name_four': { 'long_key_name_five': 1 } } } } } E501 line too long (118 > 80 characters) print dct['long_key_name_one']['long_key_name_two']['long_key_name_three']['long_key_name_four']['long_key_name_five'] E211 whitespace before '[' print dct['long_key_name_one']['long_key_name_two']\ ['long_key_name_three']['long_key_name_four']['long_key

How to choose proper variable names for long names in python

旧巷老猫 提交于 2019-11-27 06:14:24
问题 I need help choosing proper names for variables with long actual names. I have read pep8 docs, but I couln't find addressed such issue. Would you rename very_long_variable_name to something like vry_lng_var_nm or would you leave it as it is. I notice that pythons build in libraries have very short names and I'd like to follow the conventions if it exists for this case. I know I can name it something not very descriptive and add description, which would explain its meaning, but what do you

PyLint, PyChecker or PyFlakes? [closed]

烂漫一生 提交于 2019-11-27 05:43:28
I would like to get some feedback on these tools on : features; adaptability; ease of use and learning curve. e-satis Well, I am a bit curious, so I just tested the 3 myself right after asking the question ;-) Ok, this is not a very serious review but here is what I can say : I tried the tools with the default settings (it's important because you can pretty much choose your check rules) on the following script : #!/usr/local/bin/python # by Daniel Rosengren modified by e-satis import sys, time stdout = sys.stdout BAILOUT = 16 MAX_ITERATIONS = 1000 class Iterator(object) : def __init__(self):