pep8

How to break a line in a function definition in Python according to pep8?

限于喜欢 提交于 2019-12-10 19:55:13
问题 I have the following line of code that is just over the limit of 79 characters: def ReportResults(self, intTestID, startTime, stopTime, version, serverType): How do I break the line in the correct way according to pep8? 回答1: According to PEP8: The Python standard library is conservative and requires limiting lines to 79 characters (and docstrings/comments to 72). This is in my opinion the main rule to observe. Besides this rule, PEP8 recommends aligning brackets, so I would do something like:

Indentation of closing parenthesis

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 18:29:36
问题 I believe the PEP8 style guide says that both some_kind_of_list = [ 1, 2, 3, 4, 5, 6 ] def function_that_takes_long_arguments( long_argument_1, long_argument_2 ): return long_argument_1 and some_kind_of_list = [ 1, 2, 3, 4, 5, 6 ] def function_that_takes_long_arguments( long_argument_1, long_argument_2 ): return long_argument_1 are acceptable, but does it make sense to use one or the other, e.g., if I move onto C++ later on in my life? EDIT Apologies, for a function, the PEP8 style guide

Python style for line length and format when unpacking many return values

亡梦爱人 提交于 2019-12-10 10:01:40
问题 Suppose that function some_descriptively_named_function returns a 4- tuple of 4 return parameters. I want to call some_descriptively_named_function , adhere to the 80-character line length limit, and unpack all 4 outputs each into a descriptively-named variable: some_desc_name1, some_desc_name2, some_desc_name3, some_desc_name4 = some_descriptively_named_function() One option is: some_desc_name1, some_desc_name2, some_desc_name3, some_desc_name4 = ( some_descriptively_named_function() ) With

How to break long string lines for PEP8 compliance? [duplicate]

强颜欢笑 提交于 2019-12-10 00:45:50
问题 This question already has answers here : How to write very long string that conforms with PEP8 and prevent E501 (10 answers) Closed 2 years ago . I have many long lines like this in the project and don't know how to break it to keep PEP8 happy. PEP8 shows warning from .format(me['id']) pic_url = "http://graph.facebook.com/{0}/picture?width=100&height=100".format(me['id']) How can I break the line to get rid of PEP8 warning and yet don't break the code? 回答1: Using string literal concatenation:

Python PEP8 printing wrapped strings without indent

一世执手 提交于 2019-12-09 14:59:58
问题 There is probably an easy answer for this, just not sure how to tease it out of my searches. I adhere to PEP8 in my python code, and I'm currently using OptionParser for a script I'm writing. To prevent lines from going beyond a with of 80, I use the backslash where needed. For example: if __name__=='__main__': usage = '%prog [options]\nWithout any options, will display 10 random \ users of each type.' parser = OptionParser(usage) That indent after the backslash results in: ~$ ./er_usersearch

How to prevent “too broad exception” in this case?

被刻印的时光 ゝ 提交于 2019-12-09 07:37:44
问题 I got a list of functions that may fail, and if one fail, I don't want the script to stop, but to continue with next function. I am executing it with something like this : list_of_functions = [f_a,f_b,f_c] for current_function in list_of_functions: try: current_function() except Exception: print(traceback.format_exc()) It's working fine, but it is not PEP8 compliant: When catching exceptions, mention specific exceptions whenever possible instead of using a bare except: clause. For example,

How to format a python assert statement that complies with PEP8?

杀马特。学长 韩版系。学妹 提交于 2019-12-09 07:28:33
问题 How does one format a long assert statement that complies with PEP8? Please ignore the contrived nature of my example. def afunc(some_param_name): assert isinstance(some_param_name, SomeClassName), 'some_param_name must be an instance of SomeClassName, silly goose!' One cannot wrap it in parenthesis, because that changes the behavior of the assert statement since it is a keyword, not a builtin function. 回答1: It's important to remember that PEP8 is only a guideline and even states that there

autopep8 doesn't seem to be finding config file?

隐身守侯 提交于 2019-12-09 03:44:24
According to autopep8's documentation (here: https://github.com/hhatto/autopep8#configuration ), if I place a file called "setup.cfg" in the root of my git repo, with something like [pycodestyle] ignore = D203,E501,E201,E202,E203,E211,E261,E265,W503 exclude = .git,__pycache__,docs/source/conf.py,old,build,dist,__init__.py,*_gui.py max-complexity = 25 max-line-length = 160 statistics = True then it should pick up that config. I'm using autopep8 via the pre-commit hook, here: https://github.com/pre-commit/mirrors-autopep8 Best I can tell, it's not finding the setup.cfg. I also have a .flake8

Python, PEP-8, E122 continuation line missing indentation or outdented

孤者浪人 提交于 2019-12-08 19:39:27
问题 I get this error but however I choose to indent it, I still get it, do you know why? if len(argmaxcomp) == 1: print "The complex with the greatest mean abundance is: {0}"\ .format(argmaxcomp[0]) 回答1: Generally pep8 suggests you prefer parenthesis over continuation lines. The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These

Python variables naming convention

末鹿安然 提交于 2019-12-08 15:56:24
问题 So I am trying to switch to PEP8 notation (from a rather personal CamelCase notation) and I was wondering how you guys are tackling the cases where existing functions/variables would be overwritten? e.g. having something like: open, high, low, close, sum = row would already overwrite the "open" and "sum" functions. First, if I wouldn't use a good IDE, I wouldn't even notice that I've just overwritten important basic functions. Second, how would you name the variables instead? In this example