Skip to content

Commit 8b7046b

Browse files
authored
Merge pull request #244 from Dekken/Python_format_checks
Addition of CI python formatting checks + format python
2 parents 211ddb5 + 436ee50 commit 8b7046b

83 files changed

Lines changed: 349 additions & 284 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

sh/format-diff.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ DIRS=(lib/include lib/cpp lib/cpp-test)
99

1010
[ -z "$CLANG_FORMAT" ] && which clang-format &> /dev/null \
1111
&& CLANG_FORMAT="clang-format"
12-
[ -z "$CLANG_FORMAT" ] && echo "clang-format not on PATH, "\
13-
"add to PATH or set CLANG_FORMAT"\ && exit 1
12+
[ -z "$CLANG_FORMAT" ] && echo "clang-format not on PATH, " \
13+
"add to PATH or set CLANG_FORMAT" \
14+
&& exit 1
1415

1516
[ -z "$CLANG_FORMATDIFF" ] && which clang-format-diff.py &> /dev/null \
1617
&& CLANG_FORMATDIFF="clang-format-diff.py"
17-
[ -z "$CLANG_FORMATDIFF" ] && echo "clang-format-diff.py not on PATH, "\
18-
"add to PATH or set CLANG_FORMATDIFF" && exit 1
18+
[ -z "$CLANG_FORMATDIFF" ] && echo "clang-format-diff.py not on PATH, " \
19+
"add to PATH or set CLANG_FORMATDIFF" \
20+
&& exit 1
1921

2022
BRANCH=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
2123

sh/format_python.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6+
PY=python
7+
VER=$($PY --version 2>&1 | cut -d' ' -f 2 | cut -d'.' -f1)
8+
((VER == 2 )) && PY=python3
9+
10+
cd ..
11+
$PY -m yapf --style tools/python/yapf.conf -i tick --recursive
12+
13+
echo "Please confirm your version of yapf is up to date from pip"
14+
echo "To upgrade run 'pip3 install yapf --upgrade'"

tick/array/tests/serialize_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313

1414
class Test(object):
15-
1615
def __init__(self, *args, dtype="float64", **kwargs):
1716
unittest.TestCase.__init__(self, *args, **kwargs)
1817
self.dtype = dtype
@@ -49,8 +48,8 @@ def test_serialize_2d_array(self):
4948
def test_serialize_sparse_2d_array(self):
5049
"""...Test serialization of 2d dense array is done as expected
5150
"""
52-
array = sparse.rand(10, 10, density=0.3, format='csr').astype(
53-
self.dtype)
51+
array = sparse.rand(10, 10, density=0.3,
52+
format='csr').astype(self.dtype)
5453
serialize_array(array, self.array_file)
5554

5655
serialized_array = load_array(self.array_file, array_dim=2,

tick/array_test/tests/array_methods_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ def test_dot(self):
183183
array_types_1d, array_types_1d):
184184
test_arrays_1 = self._test_arrays(array_type_1)
185185
test_arrays_2 = self._test_arrays(array_type_2)
186-
test_func = getattr(test, "test_dot_%s_%s" % (array_type_1,
187-
array_type_2))
186+
test_func = getattr(
187+
test, "test_dot_%s_%s" % (array_type_1, array_type_2))
188188
for test_array_1, test_array_2 in itertools.product(
189189
test_arrays_1, test_arrays_2):
190190
compare_array_1 = Test.cast_1d_array_to_1d_dense(test_array_1)

tick/array_test/tests/array_typemap_test.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def setUp(self):
5151
info['number'] = 148 # info['python_type'](np.exp(5))
5252

5353
# The dense array of the corresponding type
54-
python_array = np.array([1, 2, 5, 0, 4, 1]).astype(
55-
info['python_type'])
54+
python_array = np.array([1, 2, 5, 0, 4,
55+
1]).astype(info['python_type'])
5656

5757
# The dense array 2D of the corresponding type
58-
python_array_2d = np.array([[1, 2, 5], [0, 4, 1]]).astype(
59-
info['python_type'])
58+
python_array_2d = np.array([[1, 2, 5],
59+
[0, 4, 1]]).astype(info['python_type'])
6060

6161
# The list of dense array of the corresponding type
6262
python_array_list_1d = [
@@ -67,8 +67,8 @@ def setUp(self):
6767

6868
python_array2d_list_1d = [
6969
np.array([[1, 2, 5], [0, 4, 1]]).astype(info['python_type']),
70-
np.array([[1, 2, 9], [1, 2, 5], [0, 4, 1]]).astype(
71-
info['python_type']),
70+
np.array([[1, 2, 9], [1, 2, 5],
71+
[0, 4, 1]]).astype(info['python_type']),
7272
np.array([[0]]).astype(info['python_type'])
7373
]
7474

@@ -82,22 +82,20 @@ def setUp(self):
8282
python_array2d_list_2d = [[
8383
np.array([[1, 2, 5], [0, 4, 1]]).astype(info['python_type']),
8484
], [
85-
np.array([[1, 2, 9], [1, 2, 5], [0, 4, 1]]).astype(
86-
info['python_type']),
85+
np.array([[1, 2, 9], [1, 2, 5],
86+
[0, 4, 1]]).astype(info['python_type']),
8787
np.array([[0]]).astype(info['python_type'])
8888
], []]
8989

9090
# The sparse array of the corresponding type
91-
python_sparse_array = csr_matrix((np.array([1.5, 2, 3, 1]),
92-
np.array([3, 5, 7, 9]),
93-
np.array([0, 4]))).astype(
94-
info['python_type'])
91+
python_sparse_array = csr_matrix(
92+
(np.array([1.5, 2, 3, 1]), np.array([3, 5, 7, 9]),
93+
np.array([0, 4]))).astype(info['python_type'])
9594

9695
# The sparse array 2D of the corresponding type
97-
python_sparse_array_2d = csr_matrix((np.array([1.5, 2, 3, 1]),
98-
np.array([3, 5, 7, 4]),
99-
np.array([0, 3, 4]))).astype(
100-
info['python_type'])
96+
python_sparse_array_2d = csr_matrix(
97+
(np.array([1.5, 2, 3, 1]), np.array([3, 5, 7, 4]),
98+
np.array([0, 3, 4]))).astype(info['python_type'])
10199

102100
python_sparse_array_list_1d = [
103101
csr_matrix((np.array([1.5, 2, 3, 1]), np.array([3, 5, 7, 9]),

tick/base/base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ def setter(self, val):
180180
if set_in_init:
181181
base_setter(self, val)
182182
else:
183-
raise AttributeError("%s is readonly in %s" %
184-
(str(attr_name), class_name))
183+
raise AttributeError(
184+
"%s is readonly in %s" % (str(attr_name), class_name))
185185

186186
def deletter(self):
187-
raise AttributeError("can't delete %s in %s" % (str(attr_name),
188-
class_name))
187+
raise AttributeError(
188+
"can't delete %s in %s" % (str(attr_name), class_name))
189189

190190
# We set doc to None otherwise it will interfere with
191191
# the docstring of the class.
@@ -529,9 +529,9 @@ def __init__(self, *args, **kwargs):
529529
])
530530

531531
# We copy property and add the doc found in docstring
532-
setattr(self.__class__, attr_name,
533-
property(prop.fget, prop.fset, prop.fdel,
534-
prop_doc))
532+
setattr(
533+
self.__class__, attr_name,
534+
property(prop.fget, prop.fset, prop.fdel, prop_doc))
535535

536536
@staticmethod
537537
def _get_now():

tick/base/learner/learner_optim.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,9 @@ def elastic_net_ratio(self, val):
389389
if self.penalty == 'elasticnet':
390390
self._prox_obj.ratio = val
391391
else:
392-
warn('Penalty "%s" has no elastic_net_ratio attribute' %
393-
self.penalty, RuntimeWarning)
392+
warn(
393+
'Penalty "%s" has no elastic_net_ratio attribute' %
394+
self.penalty, RuntimeWarning)
394395

395396
@property
396397
def blocks_start(self):
@@ -442,8 +443,9 @@ def sdca_ridge_strength(self, val):
442443
if self.solver == 'sdca':
443444
self._solver_obj.l_l2sq = val
444445
else:
445-
warn('Solver "%s" has no sdca_ridge_strength attribute' %
446-
self.solver, RuntimeWarning)
446+
warn(
447+
'Solver "%s" has no sdca_ridge_strength attribute' %
448+
self.solver, RuntimeWarning)
447449

448450
@staticmethod
449451
def _safe_array(X, dtype="float64"):

tick/base/tests/base_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,14 @@ def test_cpp_setter(self):
432432
self.assertEqual(self.a0._a0.get_cpp_int(), 15)
433433

434434
# Test we raise correct error if setter was not a cpp method
435-
with self.assertRaisesRegex(NameError,
436-
"set_cpp_unlinked_int is not a method "
437-
"of.*"):
435+
with self.assertRaisesRegex(
436+
NameError, "set_cpp_unlinked_int is not a method "
437+
"of.*"):
438438
self.a0.cpp_unlinked_int = 3
439439

440-
with self.assertRaisesRegex(NameError,
441-
"set_cpp_unlinked_int is not a method "
442-
"of.*"):
440+
with self.assertRaisesRegex(
441+
NameError, "set_cpp_unlinked_int is not a method "
442+
"of.*"):
443443
self.a0._set('cpp_unlinked_int', 3)
444444

445445
# Test we raise correct error if _cpp_obj_name was not set

tick/base/tests/exceptions_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def test_throw_system_error(self):
2626
os.strerror(errno.EACCES).lower()):
2727
throw_system_error()
2828
else:
29-
with self.assertRaisesRegex(RuntimeError, os.strerror(
30-
errno.EACCES)):
29+
with self.assertRaisesRegex(RuntimeError,
30+
os.strerror(errno.EACCES)):
3131
throw_system_error()
3232

3333
def test_throw_invalid_argument(self):

tick/base_model/model.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ def loss(self, coeffs: np.ndarray) -> float:
135135
if not self._fitted:
136136
raise ValueError("call ``fit`` before using ``loss``")
137137
if coeffs.shape[0] != self.n_coeffs:
138-
raise ValueError(("``coeffs`` has size %i while the model" +
139-
" expects %i coefficients") % (len(coeffs),
140-
self.n_coeffs))
138+
raise ValueError(
139+
("``coeffs`` has size %i while the model" +
140+
" expects %i coefficients") % (len(coeffs), self.n_coeffs))
141141
self._inc_attr(N_CALLS_LOSS)
142142
self._inc_attr(PASS_OVER_DATA, step=self.pass_per_operation[LOSS])
143143

@@ -159,10 +159,11 @@ def _get_typed_class(self, dtype_or_object_with_dtype, dtype_map):
159159
def astype(self, dtype_or_object_with_dtype):
160160
import tick.base.dtype_to_cpp_type
161161
new_model = tick.base.dtype_to_cpp_type.copy_with(
162-
self, ["_model"] # ignore _model on deepcopy
162+
self,
163+
["_model"] # ignore _model on deepcopy
163164
)
164165
new_model._set('_model',
165-
new_model._build_cpp_model(dtype_or_object_with_dtype))
166+
new_model._build_cpp_model(dtype_or_object_with_dtype))
166167
return new_model
167168

168169
def _build_cpp_model(self, dtype: str):

0 commit comments

Comments
 (0)