pep8

Comparison with boolean numpy arrays VS PEP8 E712

情到浓时终转凉″ 提交于 2019-11-28 08:01:39
问题 PEP8 E712 requires that "comparison to True should be if cond is True: or if cond: ". But if I follow this PEP8 I get different/wrong results. Why? In [1]: from pylab import * In [2]: a = array([True, True, False]) In [3]: where(a == True) Out[3]: (array([0, 1]),) # correct results with PEP violation In [4]: where(a is True) Out[4]: (array([], dtype=int64),) # wrong results without PEP violation In [5]: where(a) Out[5]: (array([0, 1]),) # correct results without PEP violation, but not as

How to properly use python's isinstance() to check if a variable is a number?

别来无恙 提交于 2019-11-28 05:42:04
I found some old Python code that was doing something like: if type(var) is type(1): ... As expected, pep8 complains about this recommending usage of isinstance() . Now, the problem is that the numbers module was added in Python 2.6 and I need to write code that works with Python 2.5+ So if isinstance(var, Numbers.number) is not a solution. Which would be the proper solution in this case? In Python 2, you can use the types module : >>> import types >>> var = 1 >>> NumberTypes = (types.IntType, types.LongType, types.FloatType, types.ComplexType) >>> isinstance(var, NumberTypes) True Note the

Why does PEP-8 specify a maximum line length of 79 characters? [closed]

霸气de小男生 提交于 2019-11-28 02:51:52
Why in this millennium should Python PEP-8 specify a maximum line length of 79 characters? Pretty much every code editor under the sun can handle longer lines. What to do with wrapping should be the choice of the content consumer, not the responsibility of the content creator. Are there any (legitimately) good reasons for adhering to 79 characters in this age? Much of the value of PEP-8 is to stop people arguing about inconsequential formatting rules, and get on with writing good, consistently formatted code. Sure, no one really thinks that 79 is optimal, but there's no obvious gain in

What is the most pythonic way to open a file?

自古美人都是妖i 提交于 2019-11-28 02:00:59
问题 I'm trying to clean up my code a little bit, and I have trouble figuring which of these 2 ways is considered the most pythonic one import os dir = os.path.dirname(__file__) str1 = 'filename.txt' f = open(os.path.join(dir,str1),'r') Although the second seems to be cleanest one, I find the declaration of fullPath a bit too much, since it will only be used once. import os dir = os.path.dirname(__file__) str1 = 'filename.txt' fullPath = os.path.join(dir,str1) f = open(fullPath,'r') In general, is

Using pass on a non necessary else statement

五迷三道 提交于 2019-11-28 01:05:02
问题 Based on PEP8 documentation, I was not able to find any reference regarding if I should use pass for aesthetic reasons on code. Based on the example below, should I keep those else or can I erase them? Until now, the main reason I'm keeping it is based on the mantra "Explicit is better than implicit." if fields: for i in foo: if i == 'something': print "something" else: pass else: pass 回答1: Yes, you can/should remove them because they do nothing. The Python community teaches "explicit is

How to integrate pep8.py in Eclipse?

穿精又带淫゛_ 提交于 2019-11-27 17:33:49
A little background: PEP 8 is the Style Guide for Python Code . It contains the conventions all python programmers should follow. pep8.py is a (very useful) script that checks the code formating of a given python script, according to PEP 8. Eclipse is a great IDE. With the Pydev extension, it that can be used to develop Python I run pep8.py manually when I'm scripting, but with bigger projects I prefer to use Eclipse. It would be really useful to integrate pep8.py in Eclipse/Pydev, so it can be run automatically in all the files in the project, and point to the lines containing the warnings.

Tool to convert Python code to be PEP8 compliant

跟風遠走 提交于 2019-11-27 16:39:26
I know there are tools which validate whether your Python code is compliant with PEP8, for example there is both an online service and a python module . However, I cannot find a service or module which can convert my Python file to a self-contained, PEP8 valid Python file. Does anyone know if there are any? I assume it's feasible since PEP8 is all about the appearance of the code, right? Andy Hayden Unfortunately "pep8 storming" (the entire project) has several negative side-effects: lots of merge-conflicts break git blame make code review difficult As an alternative (and thanks to @y-p for

What is PEP8's E128: continuation line under-indented for visual indent?

拜拜、爱过 提交于 2019-11-27 16:38:45
Just opened a file with Sublime Text (with Sublime Linter) and noticed a PEP8 formatting error that I'd never seen before. Here's the text: urlpatterns = patterns('', url(r'^$', listing, name='investment-listing'), ) It's flagging the second argument, the line that starts url(...) I was about to disable this check in ST2 but I'd like to know what I'm doing wrong before I ignore it. You never know, if it seems important I might even change my ways :) PEP-8 recommends you indent lines to the opening parentheses if you put anything on the first line, so it should either be indenting to the

Strange PEP8 recommandation on comparing Boolean values to True or False

♀尐吖头ヾ 提交于 2019-11-27 14:42:41
At the end of python PEP8 I'm reading: Don't compare boolean values to True or False using == Yes: if greeting: No: if greeting == True: Worse: if greeting is True: I have no problem with that recommandation when the boolean is True , but it sounds strange when checking for False If I want to know if variable greeting is False why shouldn't I write: if greeting == False: If I write if not greeting: it will have a very different meaning that the above statement. What if greeting is None ? What if it is empty string ? Does this PEP8 recommandation means that variables storing boolean values

How to configure PyLint to check all things PEP8 checks?

落花浮王杯 提交于 2019-11-27 12:48:58
问题 Searching for an answer on PyLint's mailing list brings no interesting results. PyLint is known to be very customizable so I guess this should be possible... The reason I would like PyLint to check compliance with PEP8 is because PyDev has much better support for PyLint than it has for PEP8. It's easier to have one tool doing all checks than having to use two. I also asked this question on PyLint's mailing list at http://thread.gmane.org/gmane.comp.python.logilab/1039 Example of diagnostic