Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Python formatting #3962

Merged
merged 29 commits into from
Jun 13, 2022

Conversation

klecki
Copy link
Contributor

@klecki klecki commented Jun 6, 2022

Category: Other

Description:

Conformance to pep8 was achieved by running
yapf and adjusting by hand the rest of issues
reported by flake8.

Additional information:

Affected modules and functionalities:

Key points relevant for the review:

Tests:

  • Existing tests apply
  • New tests added
    • Python tests
    • GTests
    • Benchmark
    • Other
  • N/A

Checklist

Documentation

  • Existing documentation applies
  • Documentation updated
    • Docstring
    • Doxygen
    • RST
    • Jupyter
    • Other
  • N/A

DALI team only

Requirements

  • Implements new requirements
  • Affects existing requirements
  • N/A

REQ IDs: N/A

JIRA TASK: N/A

@klecki
Copy link
Contributor Author

klecki commented Jun 6, 2022

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [5025731]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [5025731]: BUILD FAILED

Copy link
Member

@szalpal szalpal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider wrapping the function parameters differently. I've posted handful of suggestions, at some point I stopped posting them though ;)
What I'm referring to is such example:

class PreemphasisPythonPipeline(Pipeline):
    def __init__(self,
                 device,
                 batch_size,
                 iterator,
                 border='clamp',
                 preemph_coeff=0.97,
                 per_sample_coeff=False,
                 num_threads=4,
                 device_id=0):
        super(PreemphasisPythonPipeline, self).__init__(batch_size,
                                                        num_threads,
                                                        device_id,
                                                        seed=SEED,
                                                        exec_async=False,
                                                        exec_pipelined=False)

->

class PreemphasisPythonPipeline(Pipeline):
    def __init__(self, device, batch_size, iterator, border='clamp', preemph_coeff=0.97,
                 per_sample_coeff=False, num_threads=4, device_id=0):
        super().__init__(batch_size, num_threads, device_id, seed=SEED, exec_async=False,
                         exec_pipelined=False)

or

def check_transform_shear_op(shear=None,
                             angles=None,
                             center=None,
                             has_input=False,
                             reverse_order=False,
                             batch_size=1,
                             num_threads=4,
                             device_id=0):

->

def check_transform_shear_op(shear=None, angles=None, center=None, has_input=False,
                             reverse_order=False, batch_size=1, num_threads=4, device_id=0):

Anyway, that's your call. I don't see any blockers. I've put some suggestions, feel free to reject them, if you feel these aren't necessary.

Comment on lines 72 to 86
def __init__(self,
device,
batch_size,
iterator,
border='clamp',
preemph_coeff=0.97,
per_sample_coeff=False,
num_threads=4,
device_id=0):
super(PreemphasisPythonPipeline, self).__init__(batch_size,
num_threads,
device_id,
seed=SEED,
exec_async=False,
exec_pipelined=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about?

class PreemphasisPythonPipeline(Pipeline):
    def __init__(self, device, batch_size, iterator, border='clamp', preemph_coeff=0.97,
                 per_sample_coeff=False, num_threads=4, device_id=0):
        super().__init__(batch_size, num_threads, device_id, seed=SEED, exec_async=False,
                         exec_pipelined=False)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turned a knob in yapf so the formatting is more condensed. I also reverted the changes where not needed as apparently we don't want to have that.

Comment on lines +52 to +56

def shape_gen_fn():
return random_shape(in_shape_min, in_shape_max, ndim)

def data_gen_f():
return batch_gen(max_batch_size, shape_gen_fn)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the named lambdas case (we have handful of that) I like what @jantonguirao proposed:

        def shape_gen_fn(): return random_shape(in_shape_min, in_shape_max, ndim)
        def data_gen_f(): return batch_gen(max_batch_size, shape_gen_fn)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be in favor of keeping regular function line breaks. We are just defining them, not passing something inline

dali/test/python/test_operator_rotate.py Show resolved Hide resolved
dali/test/python/test_operator_rotate.py Show resolved Hide resolved
Comment on lines 146 to 148
yield raises(
RuntimeError,
glob=error_msg)(check_fail_sequence_rearrange), 2, shape, new_order, per_sample, dev
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving aside, that yield raises(...)(...) looks really peculiar, how about this kind of formatting?

        yield raises(RuntimeError, glob=error_msg)(check_fail_sequence_rearrange), \
            2, shape, new_order, per_sample, dev

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it consistent with the one below that is so long, that it had to be broken similar way.

Comment on lines 640 to 642
np.testing.assert_array_equal(out,
op(l_np, r_np),
err_msg="{} op\n{} =\n{}".format(l_np, r_np, out))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using modern f notation? This way this can be squashed into one line:

Suggested change
np.testing.assert_array_equal(out,
op(l_np, r_np),
err_msg="{} op\n{} =\n{}".format(l_np, r_np, out))
np.testing.assert_array_equal(out, op(l_np, r_np), err_msg=f"{l_np} op\n{r_np} =\n{out}")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 725 to 730
pipe = ExprOpPipeline(kinds,
types,
iterator, (lambda x, y: x // y),
batch_size=batch_size,
num_threads=2,
device_id=0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this looks odd. I'm OK with putting every argument in a separate line, but if so, I'd vote for not putting two args in one line by exception, i.e.

    pipe = ExprOpPipeline(kinds,
                          types,
                          iterator, (lambda x, y: x // y),   <--- here
                          batch_size=batch_size,
                          num_threads=2,
                          device_id=0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I will try some knobs to see why it happened.

def check_raises_te(kinds, types, op, shape, _):
check_raises(kinds, types, op, shape)


# Arithmetic operations between booleans that are not allowed
bool_disallowed = [((lambda x, y: x + y), "+"), ((lambda x, y: x - y), "-"),
((lambda x, y: x / y), "/"), ((lambda x, y: x / y), "//"),
((lambda x, y: x ** y), "**")]
((lambda x, y: x**y), "**")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL that pow operator formatting in Python w.r.t. whitespace is inconsistent with other ops :D

dali/test/python/test_operator_multipaste.py Show resolved Hide resolved
import os
from glob import glob
import math
import dali
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import dali
import nvidia.dali as dali

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines +53 to +55
assert val == expected_val, (
f"Unexpected value in batch: got {val}, expected {expected_val}, "
f"for batch {batch_idx}, sample {idx_in_batch}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this parenthesis required for anything?

Suggested change
assert val == expected_val, (
f"Unexpected value in batch: got {val}, expected {expected_val}, "
f"for batch {batch_idx}, sample {idx_in_batch}")
assert val == expected_val, \
f"Unexpected value in batch: got {val}, expected {expected_val}, " \
f"for batch {batch_idx}, sample {idx_in_batch}"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To not use the \ I'd say it's preferred.

Copy link
Contributor

@mzient mzient Jun 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 says so, I guess, but it still looks no less weird than a backslash (and perhaps more so).

@klecki klecki assigned mzient and unassigned banasraf Jun 7, 2022
Comment on lines +64 to +66
integer_types = [
np.bool_, np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and the nice grouping went to Hell... - I think the original formatting was actually correct under PEP8?

Comment on lines 453 to +454
pipe = ExprOpPipeline(kind, type, iterator, op, batch_size=batch_size, num_threads=2,
device_id=0)
device_id=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pipe = ExprOpPipeline(kind, type, iterator, op, batch_size=batch_size, num_threads=2,
device_id=0)
device_id=0)
pipe = ExprOpPipeline(kind, type, iterator, op,
batch_size=batch_size, num_threads=2, device_id=0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like this file compatible with full auto-format and this doesn't work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pipe = ExprOpPipeline(kind, type, iterator, op, batch_size=batch_size, num_threads=2,
device_id=0)
device_id=0)
pipe = ExprOpPipeline(
kind, type, iterator, op, batch_size=batch_size, num_threads=2, device_id=0)

Out of curiosity: would this work?

@klecki
Copy link
Contributor Author

klecki commented Jun 7, 2022

!build

Comment on lines 568 to 570
for types_in in [(np.int32, np.int32, np.int32), (np.float32, np.int32, np.int32),
(np.uint8, np.float32, np.float32), (np.int32, np.float32, np.float32)]:
(np.uint8, np.float32, np.float32),
(np.int32, np.float32, np.float32)]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for types_in in [(np.int32, np.int32, np.int32), (np.float32, np.int32, np.int32),
(np.uint8, np.float32, np.float32), (np.int32, np.float32, np.float32)]:
(np.uint8, np.float32, np.float32),
(np.int32, np.float32, np.float32)]:
for types_in in [(np.int32, np.int32, np.int32),
(np.float32, np.int32, np.int32),
(np.uint8, np.float32, np.float32),
(np.int32, np.float32, np.float32)]:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 606 to +611
pipe = ExprOpPipeline(kinds, types, iterator, op, batch_size=batch_size, num_threads=2,
device_id=0)
device_id=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pipe = ExprOpPipeline(kinds, types, iterator, op, batch_size=batch_size, num_threads=2,
device_id=0)
device_id=0)
pipe = ExprOpPipeline(kinds, types, iterator, op,
batch_size=batch_size, num_threads=2, device_id=0)

Comment on lines +645 to +649
pipe = ExprOpPipeline(kinds, types, iterator, dali_op, batch_size=batch_size, num_threads=2,
device_id=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pipe = ExprOpPipeline(kinds, types, iterator, dali_op, batch_size=batch_size, num_threads=2,
device_id=0)
pipe = ExprOpPipeline(kinds, types, iterator, dali_op,
batch_size=batch_size, num_threads=2, device_id=0)

Comment on lines +688 to +692
pipe = ExprOpPipeline(kinds, types, iterator, (lambda x, y: x // y), batch_size=batch_size,
num_threads=2, device_id=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pipe = ExprOpPipeline(kinds, types, iterator, (lambda x, y: x // y), batch_size=batch_size,
num_threads=2, device_id=0)
pipe = ExprOpPipeline(kinds, types, iterator, (lambda x, y: x // y),
batch_size=batch_size, num_threads=2, device_id=0)

Comment on lines 769 to 770
yield check_raises_re, kinds, (np.bool_, np.bool_,
np.bool_), op, shape_small, op_desc, error_msg
Copy link
Contributor

@mzient mzient Jun 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please.... this is just a coincidence - don't abuse it.

Suggested change
yield check_raises_re, kinds, (np.bool_, np.bool_,
np.bool_), op, shape_small, op_desc, error_msg
yield check_raises_re, kinds, (np.bool_, np.bool_, np.bool_), \
op, shape_small, op_desc, error_msg

or

Suggested change
yield check_raises_re, kinds, (np.bool_, np.bool_,
np.bool_), op, shape_small, op_desc, error_msg
yield (check_raises_re, kinds, (np.bool_, np.bool_, np.bool_),
op, shape_small, op_desc, error_msg)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [5034722]: BUILD STARTED

),
)
)
lambda s: int(s[s.rfind("/") + 1:s.rfind(".")]) < 2500,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is less readable; the colon disappeared.

Suggested change
lambda s: int(s[s.rfind("/") + 1:s.rfind(".")]) < 2500,
lambda s: int(s[s.rfind("/") + 1 : s.rfind(".")]) < 2500,

According to PEP8, the colon in array slice can have spaces on both sides or none.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to my flake8 it's not allowed, just tried it, reverting.

Copy link
Contributor

@mzient mzient Jun 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a known bug in flake8.... known since 2015.... to make it even "funnier", black adds those spaces and doesn't have an option not to 🙃
I think we should disable E203 entirely in flake8 - can we do it?

Related issues:
PyCQA/flake8#257
PyCQA/pycodestyle#373

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dunno. Sadly, it's not possible to use black with our codebase, so this is what we get with flake8 and other formatters, which is still pep8 compliant (as it has uniform spacing).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then:

Suggested change
lambda s: int(s[s.rfind("/") + 1:s.rfind(".")]) < 2500,
lambda s: int(s[s.rfind("/") + 1 : s.rfind(".")]) < 2500, # noqa: 203

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if that's much better with the noqa, but done.

klecki and others added 21 commits June 8, 2022 19:23
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Co-authored-by: Michał Zientkiewicz <mzient@gmail.com>
Co-authored-by: Michał Zientkiewicz <mzient@gmail.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
@klecki klecki force-pushed the python-formatting-with-yapf-and-autopep8 branch from 11633e6 to cf9dd80 Compare June 8, 2022 17:23
@klecki
Copy link
Contributor Author

klecki commented Jun 8, 2022

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [5045456]: BUILD FAILED

@klecki
Copy link
Contributor Author

klecki commented Jun 8, 2022

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [5045665]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [5045665]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [5045665]: BUILD PASSED

@klecki klecki merged commit 5b000e7 into NVIDIA:main Jun 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants