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

Resolving linter errors flake8 #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

thrunabilax
Copy link
Contributor

Multiple linter errors resolved, reported by flake8.

Multiple linter errors resolved, reported by flake8.
@MasterMind2k
Copy link
Member

Related to #1.

Copy link
Member

@MasterMind2k MasterMind2k left a comment

Choose a reason for hiding this comment

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

Looks very good for first try.

I suspect majority of work was done by autopep8?

pysubtools/__init__.py Outdated Show resolved Hide resolved
pysubtools/exporters/base.py Outdated Show resolved Hide resolved
pysubtools/exporters/base.py Show resolved Hide resolved
pysubtools/exporters/subrip.py Outdated Show resolved Hide resolved
pysubtools/parsers/base.py Show resolved Hide resolved
pysubtools/parsers/encodings.py Outdated Show resolved Hide resolved
"""Parser for SubRip.
"""
FORMAT = 'MicroDVD'
FORMAT_RE = re.compile(r'^\{(?P<start>\d+)\}\{(?P<end>\d+)\}(?P<header>(:?'
Copy link
Member

Choose a reason for hiding this comment

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

Try to understand the regex and break it into several components :).

pysubtools/parsers/microdvd.py Outdated Show resolved Hide resolved
try:
h = h[1:-1]
# Take pair out, and strip them
d = dict([(j.strip() for j in i.split(':'))
Copy link
Member

Choose a reason for hiding this comment

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

Be more aggressive:

d = dict([
    (
        j.strip() for j in i.split(':')
    )
    for i in h.split('}{')
])

It would be probably nicer if you unnest it.

pysubtools/subtitle.py Show resolved Hide resolved
Resolving multiple comments from Github regarding coding style and additional flake8
linter errors.
Copy link
Member

@MasterMind2k MasterMind2k left a comment

Choose a reason for hiding this comment

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

Good work.

Some minor comments left.

Don't forget to rebase to latest master to get the changes from parallel branch in.

@@ -60,7 +61,7 @@ def export(self, output, subtitle):
raise TypeError("Can export only Subtitle objects.")

try:
basestring
global basestring
Copy link
Member

Choose a reason for hiding this comment

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

I think it should be better to ignore flake8 error here.

Also, there might be a better way how to identify python 2/3 incompatibility for basestring.

Copy link
Member

Choose a reason for hiding this comment

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

@@ -52,7 +52,8 @@ def _export_unit(self, unit):
# TODO 3D positions
output.append("{} --> {}".format(
self._convert_time(unit.start),
self._convert_time(unit.end)).encode(self._encoding))
self._convert_time(unit.end)
Copy link
Member

Choose a reason for hiding this comment

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

Last line in lists, tuples etc. should end with a comma. Helps when you add or move lines up and down (they look all the same).

@@ -68,8 +67,8 @@ def __init__(self, stop_level='error'):
self._current_line = None

def _add_msg(self, level, line_number, column, line, description):
if (self._stop_level and self.LEVELS.index(level) >=
self.LEVELS.index(self._stop_level)):
if (self._stop_level and self.LEVELS.index(level)
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be more readable if breaking at and.

We have the first part of condition to safeguard so we don't use undefined _stop_level in the second part of the criteria. Other way how to solve this is, to ensure _stop_level always have a value, so you get a simpler condition.

Also, if you don't understand the condition at the beginning, try to understand it and give it a name. If you can, put it into a variable with that name. For this case, it could be named is_suppressed_message and use it as if not is_suppressed_message. Of course, you need to flip to condition for this.

Just some ideas for improving legacy code :).

encoding = encodings.pop()
if can_decode(data, encoding if not isinstance(encoding, tuple) else
encoding[0]):
encoding = encoding_to_use = encodings.pop()
Copy link
Member

Choose a reason for hiding this comment

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

Naming could be better.

For instance:

encoding_with_confidence = encodings.pop()
if isinstance(encoding_with_confidence, tuple):
  encoding = encoding_with_confidence[0]
else:
  encoding = encoding_with_confidence
  encoding_with_confidence = (encoding, None)

Copy link
Member

Choose a reason for hiding this comment

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

Just a small FYI (not to fix it, just to avoid code like this in future). I was here reusing encoding input parameter. It should be better to be named as default_encoding, it is less error prone.

t['styles']['*']['font-size'] = v.strip() + \
('px' if v.strip().isdigit() else '')
t['styles']['*']['font-size'] = v.strip()
+ ('px' if v.strip().isdigit() else '')
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure this code works? I think you need \ or wrap whole expression into ().

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.

2 participants