Open LordPatate opened 1 year ago
Could also add:
# Non compliant
for element in iterable:
if element is x:
return True
return False
# Compliant
x in iterable
a = [1,2,3,4,5]
b = [2,3,4,5,6]
overlaps = []
for x in a:
for y in b:
if x==y:
overlaps.append(x)
print(overlaps)
This will print the list [2, 3, 4, 5]. The number of comparisons here will get very large, very quickly.
Another approach would be:
a = [1,2,3,4,5]
b = [2,3,4,5,6]
overlaps = set(a) & set(b)
print(overlaps)
This will print the dictionary {2, 3, 4, 5}. You’re leaning on the built-in functions and getting a big speed and memory bump as a result.
# Non-compliant
for i in range(len(iterable)):
element = iterable[i]
...
# Non-compliant
i = 0
for element in iterable:
...
i += 1
# Compliant
for i, element in enumerate(iterable):
...
fruits = ["apple", "orange", "peach"]
sizes = ["small", "medium", "big"]
# Non-compliant
for fruit in fruits:
for size in sizes:
print(fruit, size)
# Better but not optimal
product = (
(fruit, size)
for fruit in fruits
for size in sizes
)
for fruit, size in product:
print(fruit, size)
# Compliant
import itertools
for fruit, size in itertools.product(fruits, sizes):
print(fruit, size)
\newpage
OS | OS version | Langage |
---|---|---|
- | - | Python |
ID | Title | Category | Sub-category |
---|---|---|---|
{id} | Favor use of Python builtins | Environment | Optimized API |
Severity | Remediation Cost |
---|---|
Minor | Easy |
Because built-in functions are typically implemented in C or C++, they are faster and more efficient than equivalent Python code. They should always be preferred over writing Python code that accomplishes the same.
<p>Because built-in functions are typically implemented in C or C++, they are faster and more efficient than equivalent Python code.</p>
<p>They should always be preferred over writing Python code that accomplishes the same.</p>
<h2 id="abs">Abs</h2>
<h3 id="non-compliant-1">Non compliant #1</h3>
<pre><code class="lang-python">x <span class="hljs-keyword">if</span> x > <span class="hljs-number">0</span> <span class="hljs-keyword">else</span> -x
</code></pre>
<h3 id="non-compliant-2">Non compliant #2</h3>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> x > <span class="hljs-number">0</span>:
<span class="hljs-keyword">return</span> x
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">return</span> -x
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-title">abs</span><span class="hljs-params">(x)</span></span>
</code></pre>
<h2 id="bool">Bool</h2>
<h3 id="non-compliant-1">Non compliant #1</h3>
<pre><code class="lang-python"><span class="hljs-literal">True</span> <span class="hljs-keyword">if</span> x <span class="hljs-keyword">else</span> <span class="hljs-literal">False</span>
</code></pre>
<h3 id="non-compliant-2">Non compliant #2</h3>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> x:
<span class="hljs-keyword">return</span> <span class="hljs-literal">True</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">return</span> <span class="hljs-literal">False</span>
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-title">bool</span><span class="hljs-params">(x)</span></span>
</code></pre>
<h2 id="all">All</h2>
<h3 id="non-compliant">Non compliant</h3>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> iterable:
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> element:
<span class="hljs-keyword">return</span> <span class="hljs-literal">False</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">True</span>
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-title">all</span><span class="hljs-params">(iterable)</span></span>
</code></pre>
<h2 id="any">Any</h2>
<h3 id="non-compliant">Non compliant</h3>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> iterable:
<span class="hljs-keyword">if</span> element:
<span class="hljs-keyword">return</span> <span class="hljs-literal">True</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">False</span>
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-title">any</span><span class="hljs-params">(iterable)</span></span>
</code></pre>
<h2 id="filter">Filter</h2>
<h3 id="non-compliant">Non compliant</h3>
<pre><code class="lang-python">my_list = []
<span class="hljs-keyword">for</span> <span class="hljs-keyword">element</span> <span class="hljs-keyword">in</span> iterable:
<span class="hljs-keyword">if</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-title">element</span>):</span>
my_list.append(<span class="hljs-keyword">element</span>)
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python"><span class="hljs-built_in">filter</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>, <span class="hljs-title">iterable</span>)</span>
</code></pre>
<h2 id="len">Len</h2>
<h3 id="non-compliant">Non compliant</h3>
<pre><code class="lang-python"><span class="hljs-built_in">count</span> = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> iterable:
<span class="hljs-built_in">count</span> += <span class="hljs-number">1</span>
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-title">len</span><span class="hljs-params">(iterable)</span></span>
</code></pre>
<h2 id="map">Map</h2>
<h3 id="non-compliant-1">Non compliant #1</h3>
<pre><code class="lang-python">my_list = []
<span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> iterable:<span class="hljs-type"></span>
<span class="hljs-keyword">new</span><span class="hljs-type">_item</span> = <span class="hljs-function"><span class="hljs-keyword">function</span></span>(element)
my_list.append(<span class="hljs-keyword">new</span><span class="hljs-type">_item</span>)
</code></pre>
<h3 id="non-compliant-2">Non compliant #2</h3>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> <span class="hljs-keyword">element</span> <span class="hljs-keyword">in</span> iterable:
<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-title">element</span>)</span>
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-title">map</span><span class="hljs-params">(function, iterable)</span></span>
</code></pre>
<h2 id="max">Max</h2>
<h3 id="non-compliant">Non compliant</h3>
<pre><code class="lang-python">max<span class="hljs-number">_</span><span class="hljs-keyword">value</span> = None
<span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> iterable:
<span class="hljs-keyword">if</span> max<span class="hljs-number">_</span><span class="hljs-keyword">value</span> <span class="hljs-keyword">is</span> None or element > max<span class="hljs-number">_</span><span class="hljs-keyword">value</span>:
max<span class="hljs-number">_</span><span class="hljs-keyword">value</span> = element
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-title">max</span><span class="hljs-params">(iterable)</span></span>
</code></pre>
<h2 id="min">Min</h2>
<h3 id="non-compliant">Non compliant</h3>
<pre><code class="lang-python">min<span class="hljs-number">_</span><span class="hljs-keyword">value</span> = None
<span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> iterable:
<span class="hljs-keyword">if</span> min<span class="hljs-number">_</span><span class="hljs-keyword">value</span> <span class="hljs-keyword">is</span> None or element < min<span class="hljs-number">_</span><span class="hljs-keyword">value</span>:
min<span class="hljs-number">_</span><span class="hljs-keyword">value</span> = element
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-title">min</span><span class="hljs-params">(iterable)</span></span>
</code></pre>
<h2 id="sum">Sum</h2>
<h3 id="non-compliant">Non compliant</h3>
<pre><code class="lang-python"><span class="hljs-built_in">count</span> = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> iterable:
<span class="hljs-built_in">count</span> += element
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-title">sum</span><span class="hljs-params">(iterable)</span></span>
</code></pre>
<h2 id="index">Index</h2>
<h3 id="non-compliant">Non compliant</h3>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> <span class="hljs-selector-tag">i</span>, element <span class="hljs-keyword">in</span> enumerate(iterable):
<span class="hljs-keyword">if</span> element is searched_element:
return <span class="hljs-selector-tag">i</span>
raise ValueError
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python">iterable.<span class="hljs-keyword">index</span>(searched_element)
</code></pre>
<h2 id="count">Count</h2>
<h3 id="non-compliant">Non compliant</h3>
<pre><code class="lang-python"><span class="hljs-built_in">count</span> = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> iterable:
<span class="hljs-keyword">if</span> element <span class="hljs-keyword">is</span> searched_element:
<span class="hljs-built_in">count</span> += <span class="hljs-number">1</span>
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python">iterable.<span class="hljs-built_in">count</span>(searched_element)
</code></pre>
<h2 id="iterable-operators">Iterable operators</h2>
<h3 id="non-compliant">Non compliant</h3>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> iterable:
<span class="hljs-keyword">if</span> element <span class="hljs-keyword">is</span> x:
<span class="hljs-keyword">return</span> <span class="hljs-literal">True</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">False</span>
</code></pre>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python">x <span class="hljs-keyword">in</span> iterable
</code></pre>
<h2 id="set-operators">Set operators</h2>
<h3 id="compliant">Compliant</h3>
<pre><code class="lang-python">overlaps = []
<span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-selector-tag">a</span>:
<span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> <span class="hljs-selector-tag">b</span>:
<span class="hljs-keyword">if</span> x==y:
overlaps.append(x)
</code></pre>
<h3 id="non-compliant">Non compliant</h3>
<pre><code class="lang-python">set(<span class="hljs-name">a</span>) & set(<span class="hljs-name">b</span>)
</code></pre>
<h2 id="recognizable-sorting-algorithm">Recognizable sorting algorithm</h2>
<p>Most developers learned the most known sorting algorithms (selection, bubble, insertion, heap, merge, quick...) either for practice or for job interviews.
They should not re-implement those but use the <code>sorted()</code> or <code>list.sort()</code> methods instead.</p>
for
loops with just one function calllist.append()
in a loopif
inside a for
return True
or False
inside a if
in a loopif ... return True else return False
We should have an issue for each "pattern". Each pattern rule needs to be measured in my point of view. I think it can be done with tools like RAPL. I am not sure of what to do with this issue as it is today.
Detects patterns that mimic the behavior of builtins
Simple if / else blocks
Abs
Bool
Functions taking an iterable as argument
All
Any
Filter
Len
Map
Max
Min
Sum
Index
Count
Recognizable sorting algorithm
Most developers learned the most known sorting algorithms (selection, bubble, insertion, heap, merge, quick...) either for practice or for job interviews. They should not re-implement those but use the
sorted()
orlist.sort()
methods instead.