Fix some bugs

Change lint.py to print the name of file being linted for easy debugging. Also removed a bug from website\main.py
This commit is contained in:
Rohit Tanwar
2018-04-12 17:46:30 +05:30
parent 0c4e433628
commit 5b7baf43f8
5 changed files with 32 additions and 30 deletions

View File

@ -2,6 +2,7 @@ import util
import subprocess import subprocess
import sys import sys
for snippet in util.read_snippets(): for snippet in util.read_snippets():
print(snippet.name)
code = snippet.read_code() code = snippet.read_code()
check_1 = subprocess.run(['flake8', '-','--select=E901,E999,F821,F822,F823','--count','--show-source','--statistics'], input=code, encoding='utf8',stdout=subprocess.PIPE) check_1 = subprocess.run(['flake8', '-','--select=E901,E999,F821,F822,F823','--count','--show-source','--statistics'], input=code, encoding='utf8',stdout=subprocess.PIPE)
check_2 = subprocess.run(['flake8', '-','--exit-zero','--max-complexity=10','--count','--max-line-length=127','--statistics','--ignore=W292'], input=code, encoding='utf8',stdout=subprocess.PIPE) check_2 = subprocess.run(['flake8', '-','--exit-zero','--max-complexity=10','--count','--max-line-length=127','--statistics','--ignore=W292'], input=code, encoding='utf8',stdout=subprocess.PIPE)

View File

@ -6,7 +6,7 @@ Use list comprehension to only keep values not contained in `b`
```python ```python
def difference(a, b): def difference(a, b):
   return [item for item in a if item not in b] return [item for item in a if item not in b]
``` ```
``` python ``` python
difference([1, 2, 3], [1, 2, 4]) # [3] difference([1, 2, 3], [1, 2, 4]) # [3]

View File

@ -1,8 +0,0 @@
count_vowels
byte_size
capitalize
capitalize_every_word
decapitalize
palindrome
is_upper_case
is_lower_case

View File

@ -1,6 +1,6 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block content %}<div class="row" style="height:calc(100vh - 5.875rem);overflow:hidden"><input id="doc-drawer-checkbox" class="drawer" value="on" type="checkbox"><nav class="col-md-4 col-lg-3" style="border-top:0"><div class="group"><input class="search" id="searchInput" onkeyup="search(this)" type="text"><label id="search-label">Search for snippet...</label></div><label for="doc-drawer-checkbox" class="button drawer-close"></label><h3 style = "">Math</h3><a class="sublink-1" tags="math" href="#average" style="">average</a><a class="sublink-1" tags="math" href="#factorial" style="">factorial</a><a class="sublink-1" tags="math" href="#gcd" style="">gcd</a><a class="sublink-1" tags="math" href="#lcm" style="">lcm</a><a class="sublink-1" tags="math" href="#max_n" style="">max_n</a><a class="sublink-1" tags="math" href="#min_n" style="">min_n</a><h3 style = "">List</h3><a class="sublink-1" tags="list" href="#chunk" style="">chunk</a><a class="sublink-1" tags="list" href="#compact" style="">compact</a><a class="sublink-1" tags="list" href="#count_occurences" style="">count_occurences</a><a class="sublink-1" tags="list" href="#deep_flatten" style="">deep_flatten</a><a class="sublink-1" tags="list" href="#difference" style="">difference</a><a class="sublink-1" tags="list" href="#shuffle" style="">shuffle</a><a class="sublink-1" tags="list" href="#spread" style="">spread</a><a class="sublink-1" tags="list" href="#zip" style="">zip</a><a class="sublink-1" tags="list" href="#count_by" style="">count_by</a><a class="sublink-1" tags="list" href="#difference_by" style="">difference_by</a><a class="sublink-1" tags="list" href="#insertion_sort" style="">insertion_sort</a><h3 style = "">String</h3><a class="sublink-1" tags="string" href="#count_vowels" style="">count_vowels</a><a class="sublink-1" tags="string" href="#byte_size" style="">byte_size</a><a class="sublink-1" tags="string" href="#capitalize" style="">capitalize</a><a class="sublink-1" tags="string" href="#capitalize_every_word" style="">capitalize_every_word</a><a class="sublink-1" tags="string" href="#decapitalize" style="">decapitalize</a><a class="sublink-1" tags="string" href="#palindrome" style="">palindrome</a><a class="sublink-1" tags="string" href="#is_upper_case" style="">is_upper_case</a><a class="sublink-1" tags="string" href="#is_lower_case" style="">is_lower_case</a></nav><main class="col-sm-12 col-md-8 col-lg-9" style="height:100%;overflow-y:auto;background:#eceef2;padding:0"><a id="top"></a><h2 style="text-align:center">Math</h2><div class="card fluid"><h3 id="average" class="section double-padded">average</h3><!--<form action="" method="post"><button type="submit" value="average" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> {% block content %}<div class="row" style="height:calc(100vh - 5.875rem);overflow:hidden"><input id="doc-drawer-checkbox" class="drawer" value="on" type="checkbox"><nav class="col-md-4 col-lg-3" style="border-top:0"><div class="group"><input class="search" id="searchInput" onkeyup="search(this)" type="text"><label id="search-label">Search for snippet...</label></div><label for="doc-drawer-checkbox" class="button drawer-close"></label><h3 style = "">Math</h3><a class="sublink-1" tags="math" href="#average" style="">average</a><a class="sublink-1" tags="math" href="#factorial" style="">factorial</a><a class="sublink-1" tags="math" href="#gcd" style="">gcd</a><a class="sublink-1" tags="math" href="#lcm" style="">lcm</a><a class="sublink-1" tags="math" href="#max_n" style="">max_n</a><a class="sublink-1" tags="math" href="#min_n" style="">min_n</a><h3 style = "">List</h3><a class="sublink-1" tags="list" href="#chunk" style="">chunk</a><a class="sublink-1" tags="list" href="#compact" style="">compact</a><a class="sublink-1" tags="list" href="#count_occurences" style="">count_occurences</a><a class="sublink-1" tags="list" href="#deep_flatten" style="">deep_flatten</a><a class="sublink-1" tags="list" href="#difference" style="">difference</a><a class="sublink-1" tags="list" href="#shuffle" style="">shuffle</a><a class="sublink-1" tags="list" href="#spread" style="">spread</a><a class="sublink-1" tags="list" href="#zip" style="">zip</a><a class="sublink-1" tags="list" href="#count_by" style="">count_by</a><a class="sublink-1" tags="list" href="#difference_by" style="">difference_by</a><a class="sublink-1" tags="list" href="#insertion_sort" style="">insertion_sort</a><h3 style = "">String</h3><a class="sublink-1" tags="string" href="#count_vowels" style="">count_vowels</a><a class="sublink-1" tags="string" href="#byte_size" style="">byte_size</a><a class="sublink-1" tags="string" href="#capitalize" style="">capitalize</a><a class="sublink-1" tags="string" href="#capitalize_every_word" style="">capitalize_every_word</a><a class="sublink-1" tags="string" href="#decapitalize" style="">decapitalize</a><a class="sublink-1" tags="string" href="#palindrome" style="">palindrome</a><a class="sublink-1" tags="string" href="#is_upper_case" style="">is_upper_case</a><a class="sublink-1" tags="string" href="#is_lower_case" style="">is_lower_case</a><h3 style = ""> list</h3><a class="sublink-1" tags=" list" href="#bubble_sort" style="">bubble_sort</a></nav><main class="col-sm-12 col-md-8 col-lg-9" style="height:100%;overflow-y:auto;background:#eceef2;padding:0"><a id="top"></a><h2 style="text-align:center">Math</h2><div class="card fluid"><h3 id="average" class="section double-padded">average</h3><!--<form action="" method="post"><button type="submit" value="average" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p><emoji></emoji> Already implemented via <code>statistics.mean</code>. <code>statistics.mean</code> takes an array as an argument whereas this function takes variadic arguments.</p> <p><emoji></emoji> Already implemented via <code>statistics.mean</code>. <code>statistics.mean</code> takes an array as an argument whereas this function takes variadic arguments.</p>
<p>Returns the average of two or more numbers.</p> <p>Returns the average of two or more numbers.</p>
<p>Takes the sum of all the <code>args</code> and divides it by <code>len(args)</code>. The second argument <code>0.0</code> in sum is to handle floating point division in <code>python2</code>.</p> <p>Takes the sum of all the <code>args</code> and divides it by <code>len(args)</code>. The second argument <code>0.0</code> in sum is to handle floating point division in <code>python2</code>.</p>
@ -99,8 +99,7 @@ lcm([1, 3, 4], 5) # 60</pre>
def max_n(arr, n=1): def max_n(arr, n=1):
numbers = deepcopy(arr) numbers = deepcopy(arr)
numbers.sort() numbers.sort(reverse=True)
numbers.reverse()
return numbers[:n]</pre> return numbers[:n]</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">max_n([1, 2, 3]) # [3] <pre class="language-python">max_n([1, 2, 3]) # [3]
@ -143,7 +142,7 @@ def chunk(arr, size):
<p>Use <code>filter()</code> to filter out falsey values (False, None, 0, and "").</p> <p>Use <code>filter()</code> to filter out falsey values (False, None, 0, and "").</p>
<pre class="language-python">def compact(arr): <pre class="language-python">def compact(arr):
return list(filter(lambda x: bool(x), arr))</pre> return list(filter(bool, arr))</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]</pre> <pre class="language-python">compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
@ -151,15 +150,10 @@ def chunk(arr, size):
</div><div class="card fluid"><h3 id="count_occurences" class="section double-padded">count_occurences</h3><!--<form action="" method="post"><button type="submit" value="count_occurences" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><div class="card fluid"><h3 id="count_occurences" class="section double-padded">count_occurences</h3><!--<form action="" method="post"><button type="submit" value="count_occurences" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p><emoji></emoji> Already implemented via <code>list.count()</code>.</p> <p><emoji></emoji> Already implemented via <code>list.count()</code>.</p>
<p>Counts the occurrences of a value in an list.</p> <p>Counts the occurrences of a value in an list.</p>
<p>Uses the <code>reduce</code> functin from built-in module <code>functools</code> to increment a counter each time you encounter the specific value inside the list.</p> <p>Uses the list comprehension to increment a counter each time you encounter the specific value inside the list.</p>
<pre class="language-python">from functools import reduce <pre class="language-python">def count_occurrences(lst, val):
return len([x for x in lst if x == val and type(x) == type(val)])</pre>
def count_occurences(arr, val):
return reduce(
(lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
arr)</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3</pre> <pre class="language-python">count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
@ -188,11 +182,10 @@ def deep_flatten(arr):
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="difference" class="section double-padded">difference</h3><!--<form action="" method="post"><button type="submit" value="difference" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><div class="card fluid"><h3 id="difference" class="section double-padded">difference</h3><!--<form action="" method="post"><button type="submit" value="difference" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Returns the difference between two arrays.</p> <p>Returns the difference between two iterables.</p>
<p>Create a <code>set</code> from <code>b</code>, then use list comprehension to only keep values not contained in <code>b</code></p> <p>Use list comprehension to only keep values not contained in <code>b</code></p>
<pre class="language-python">def difference(a, b): <pre class="language-python">def difference(a, b):
b = set(b)
return [item for item in a if item not in b]</pre> return [item for item in a if item not in b]</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">difference([1, 2, 3], [1, 2, 4]) # [3]</pre> <pre class="language-python">difference([1, 2, 3], [1, 2, 4]) # [3]</pre>
@ -323,7 +316,7 @@ count_vowels('gym') # 0</pre>
<pre class="language-python">def byte_size(string): <pre class="language-python">def byte_size(string):
return(len(string.encode('utf-8')))</pre> return(len(string.encode('utf-8')))</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">byte_size('😀') # 4 <pre class="language-python">byte_size('😀') # 4
byte_size('Hello World') # 11</pre> byte_size('Hello World') # 11</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
@ -375,8 +368,8 @@ decapitalize('FooBar', True) # 'fOOBAR'</pre>
<p>Checks if a string is upper case.</p> <p>Checks if a string is upper case.</p>
<p>Convert the given string to upper case, using <code>str.upper()</code> method and compare it to the original.</p> <p>Convert the given string to upper case, using <code>str.upper()</code> method and compare it to the original.</p>
<pre class="language-python">def is_upper_case(str): <pre class="language-python">def is_upper_case(string):
return str == str.upper()</pre> return string == string.upper()</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">is_upper_case('ABC') # True <pre class="language-python">is_upper_case('ABC') # True
is_upper_case('a3@$') # True is_upper_case('a3@$') # True
@ -387,14 +380,30 @@ is_upper_case('aB4') # False</pre>
<p>Checks if a string is lower case.</p> <p>Checks if a string is lower case.</p>
<p>Convert the given string to lower case, using <code>str.lower()</code> method and compare it to the original.</p> <p>Convert the given string to lower case, using <code>str.lower()</code> method and compare it to the original.</p>
<pre class="language-python">def is_lower_case(str): <pre class="language-python">def is_lower_case(string):
return str == str.lower()</pre> return string == string.lower()</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">is_lower_case('abc') # True <pre class="language-python">is_lower_case('abc') # True
is_lower_case('a3@$') # True is_lower_case('a3@$') # True
is_lower_case('Ab4') # False</pre> is_lower_case('Ab4') # False</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><h2 style="text-align:center"> list</h2><div class="card fluid"><h3 id="bubble_sort" class="section double-padded">bubble_sort</h3><!--<form action="" method="post"><button type="submit" value="bubble_sort" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Bubble_sort uses the technique of comparing and swapping</p>
<pre class="language-python">def bubble_sort(arr):
for passnum in range(len(arr) - 1, 0, -1):
for i in range(passnum):
if arr[i] &gt; arr[i + 1]:
temp = arr[i]
arr[i] = arr[i + 1]
arr[i + 1] = temp</pre>
<label class="collapse">Show examples</label>
<pre class="language-python">arr = [54,26,93,17,77,31,44,55,20]
bubble_sort(arr)
print("sorted %s" %arr) # [17,20,26,31,44,54,55,77,91]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><button class="scroll-to-top"></button> </div><button class="scroll-to-top"></button>
<footer><p style="display:inline-block"><strong>30 seconds of python code</strong> is licensed under the <a href="https://github.com/kriadmin/30-seconds-of-python-code/blob/master/LICENSE">GPL-3.0</a> license.<br>Icons made by <a href="https://www.flaticon.com/authors/smashicons">Smashicons</a> from <a href="https://www.flaticon.com/">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/">CC 3.0 BY</a>.<br>Ribbon made by <a href="https://github.com/tholman/github-corners">Tim Holman</a> is licensed by <a href="https://opensource.org/licenses/MIT">The MIT License</a><br>Built with the <a href="https://minicss.org">mini.css framework</a>.</p></footer> <footer><p style="display:inline-block"><strong>30 seconds of python code</strong> is licensed under the <a href="https://github.com/kriadmin/30-seconds-of-python-code/blob/master/LICENSE">GPL-3.0</a> license.<br>Icons made by <a href="https://www.flaticon.com/authors/smashicons">Smashicons</a> from <a href="https://www.flaticon.com/">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/">CC 3.0 BY</a>.<br>Ribbon made by <a href="https://github.com/tholman/github-corners">Tim Holman</a> is licensed by <a href="https://opensource.org/licenses/MIT">The MIT License</a><br>Built with the <a href="https://minicss.org">mini.css framework</a>.</p></footer>
</main></div>{% endblock %} </main></div>{% endblock %}

View File

@ -8,7 +8,7 @@ codeRe = "```\s*python([\s\S]*?)```"
def tagger(): def tagger():
tag_data = open('tag_database').read() tag_data = open('tag_database').read()
tag_dict = {} tag_dict = {}
tag_list = tag_data.split('\n') tag_list = filter(lambda x:x.strip() != '',tag_data.split('\n'))
for tag in tag_list: for tag in tag_list:
category = tag.split(':')[1] category = tag.split(':')[1]
snippet = tag.split(':')[0] snippet = tag.split(':')[0]