Permalink
Browse files

Add style check test

  • Loading branch information...
hombit committed Apr 17, 2018
1 parent 3190b56 commit 352a9755c4b0757daad2ca0430ed824707a721da
Showing with 61 additions and 4 deletions.
  1. +1 −0 .travis.yml
  2. +55 −0 bin/sci_py_test_style
  3. +1 −0 requirements.txt
  4. +3 −3 scientific_python/a_intro/sequences.py
  5. +1 −1 setup.py
@@ -11,3 +11,4 @@ install:
script:
- sci_py_import_all
- python setup.py test
- sci_py_test_style .
@@ -0,0 +1,55 @@
#!/usr/bin/env python

import glob
import os
from itertools import chain
from sys import argv

import pycodestyle


EXTS = ('.py', '.pyx')
SKIP_ERRORS = {'a_intro/sequences.py': ('E402', 'E711', 'E712')}
SCRIPTS = ('sci_py_example', 'sci_py_import_all', 'sci_py_test_style')


def check_style(fpath):
for exception_path, errors in SKIP_ERRORS.items():
if fpath.endswith(exception_path):
style = pycodestyle.StyleGuide(ignore=errors)
break
else:
style = pycodestyle.StyleGuide()
return style.check_files([fpath])


if __name__ == '__main__':
dir_name = argv[1]
script_dir = os.path.join(dir_name, 'bin')
package_dir = os.path.join(dir_name, 'scientific_python')

fpaths = [os.path.join(script_dir, script) for script in SCRIPTS]
fpaths += list(chain.from_iterable(
(glob.glob(os.path.join(package_dir, '**/*'+ext)) for ext in EXTS)
))

results = dict(filter(lambda x: x[1].total_errors != 0,
((fpath, check_style(fpath)) for fpath in fpaths)))

if len(results) > 0:
errors_per_file = (
(fpath, '\n'.join(r.get_statistics()))
for fpath, r in results.items()
)
errors = '\n'.join(
(' {}:\n{}'.format(fpath, errors)
for fpath, errors in errors_per_file)
)
msg = 'Style checker has found error{s}:\n{errors}'.format(
s='s'*bool(len(results) != 1),
errors=errors
)
print(msg)
RuntimeError(msg)
else:
print("Style check hasn't found any errors")
@@ -2,3 +2,4 @@ numpy>=1.13
scipy>=1.0.0
cython>=0.26
numba>=0.35
pycodestyle>=2.0
@@ -20,7 +20,7 @@

# list is a collection of various elements that could be and could be not
# objects of the same type. List could be initialized using "[]" operator:
a = [1, 2.3, 4.3, -1+3.5j]
a = [1, 2.3, 4.3, -1 + 3.5j]
print(a)
# [1, 2.3, 4.3, -1+3.5j]

@@ -283,7 +283,7 @@
t = ()
t = tuple()

t = (1, 2, 10+3j, 1, 'hello')
t = (1, 2, 10 + 3j, 1, 'hello')
print(t.count(1))
# 2
print(t.index(1))
@@ -314,7 +314,7 @@
# Tuples are immutable objects. It means that you cannot change their size,
# replace or add elements. NB that even operator "+=" will produce new object,
# not update current:
t = (1, 2, 10+3j, 1, 'hello')
t = (1, 2, 10 + 3j, 1, 'hello')
tt = t
t += (-10, 'world')
print(t)
@@ -42,7 +42,7 @@
packages=find_packages(),
# If you have only one Python module use `py_modules` instead of `packages`:
# py_modules=['mymodule'],
scripts=['bin/sci_py_example', 'bin/sci_py_import_all'],
scripts=['bin/sci_py_example', 'bin/sci_py_import_all', 'bin/sci_py_test_style'],
ext_modules=ext_modules, # for Cython
data_files=[
('doc', ['doc/course_abstract.docx']),

0 comments on commit 352a975

Please sign in to comment.