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

Refactor by pep8 #1550

Merged
merged 21 commits into from
Sep 5, 2017
Merged

Refactor by pep8 #1550

merged 21 commits into from
Sep 5, 2017

Conversation

zsef123
Copy link
Contributor

@zsef123 zsef123 commented Aug 27, 2017

Fixes #1521

First checkbox

PEP8 (without a hard 80-char constraint for long lines)

Most changes are whitespace adjustment and import order changes.

Then unused var and header are removed except in try catch, named _ var and side effects call.
Excepted case add comments 'noqa'

And test_atmodel.py, test_ldamallet_wrapper.py, test_ldamodel codes don't use fname like under code

fname = testfile()
model = self.model
model.save(testfile(), sep_limit=0)

save or load argument changed from testfile() to fname

Copy link
Owner

@piskvorky piskvorky left a comment

Choose a reason for hiding this comment

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

Welcome changes overall, thanks! Minor fixes required, see comments.

from .textcorpus import TextCorpus
from .ucicorpus import UciCorpus
from .malletcorpus import MalletCorpus
from .mmcorpus import MmCorpus # noqa:F401
Copy link
Owner

@piskvorky piskvorky Aug 27, 2017

Choose a reason for hiding this comment

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

-1: we don't want to litter the code with some tool-specific constructs.

Instead, open an issue with the tool that requires this, it's a bug on their side if they cannot handle this line.

If you find such comments in other parts of gensim, please remove them too.

Copy link
Contributor

Choose a reason for hiding this comment

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

@piskvorky it's not a tool bug, it's really "unused import" by PEP

Copy link
Owner

Choose a reason for hiding this comment

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

But it is used -- it makes the classes available directly from the package. Without it, you won't be able to from corpora import MmCorpus.

Or how can this be done in other way, that doesn't trigger F401?

Copy link
Contributor

@menshikh-iv menshikh-iv Aug 28, 2017

Choose a reason for hiding this comment

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

it makes the classes available directly from the package

You are right, but now in the code, it's unused import.

I know another way - use this imports (like your example) in gensim internally, but I don't think that this is a good idea. For my opinion, noqa suppress is OK.

def idFromDir(self, path):
assert len(path) > len(self.baseDir)
dmlczId = open(os.path.join(path, 'dspace_id')).read().strip()
pathId = path[len(self.baseDir) + 1 : ]
pathId = path[len(self.baseDir) + 1:]
Copy link
Owner

Choose a reason for hiding this comment

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

@@ -267,7 +263,7 @@ def __iter__(self):
# (unlike numpy). so, clip the end of the chunk explicitly to make
# scipy.sparse happy
chunk_end = min(self.index.shape[0], chunk_start + self.chunksize)
chunk = self.index[chunk_start : chunk_end]
chunk = self.index[chunk_start: chunk_end]
Copy link
Owner

Choose a reason for hiding this comment

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

@zsef123
Copy link
Contributor Author

zsef123 commented Aug 28, 2017

E722 | do not use bare except, specify exception instead

For example

try:
    "do something"
except:
    pass

E722 error occurs except line

In gensim code, some line add Exception class but the others not specify exception

So if not used exception class in exception scope, I just add Exception like under code

try:
    "do something"
except Exception:
    pass

@zsef123
Copy link
Contributor Author

zsef123 commented Aug 28, 2017

I miss check in corpora dir files.

I'm not touch about noqa, line slice whitespace yet.

and naming like 'l' changed to suitable name

@@ -150,7 +150,7 @@ def zeros_aligned(shape, dtype, order='C', align=128):
nbytes = np.prod(shape, dtype=np.int64) * np.dtype(dtype).itemsize
buffer = np.zeros(nbytes + align, dtype=np.uint8) # problematic on win64 ("maximum allowed dimension exceeded")
start_index = -buffer.ctypes.data % align
return buffer[start_index: start_index + nbytes].view(dtype).reshape(shape, order=order)
return buffer[start_index:start_index + nbytes].view(dtype).reshape(shape, order=order)
Copy link
Owner

Choose a reason for hiding this comment

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

Does not conform to PEP8 (need spaces around : here, see #1521 (comment)).

Copy link
Contributor Author

@zsef123 zsef123 Aug 29, 2017

Choose a reason for hiding this comment

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

@piskvorky Sorry, Line slice white space was not my intention. I missed these line in this commit.

Anyway, I agree to input space around :.

but if input space like return buffer[start_index : start_index + nbytes], E203 error occurs. (pycodestyle issue)

Is the above line okay if an error occurs?

Copy link
Owner

@piskvorky piskvorky Aug 29, 2017

Choose a reason for hiding this comment

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

I don't think there's any error in the code, the version with spaces is correct. The version without spaces is incorrect. It appears to be a bug in the style-checking software. CC @menshikh-iv

@zsef123
Copy link
Contributor Author

zsef123 commented Aug 31, 2017

Issue #1551

By adding # flake8: noqa to the file (docs:ignoring-entire-file)

I have adding above comment ignore file

It may be dangerous but I think __init__.py files short and simple enough so I think safely ignore flake8 errors

Excepts models/_init.py, this file contains short class.
Still, I think it's okay to ignore the error because the file is still short, and support by PEP8.

@@ -1,17 +1,17 @@
"""
This package contains implementations of various streaming corpus I/O format.
"""

# flake8: noqa
Copy link
Owner

Choose a reason for hiding this comment

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

This seems to combine the worst of both worlds. Still extra comments, AND no style checking.

I think I prefer even the noqa:F401 version.

@zsef123
Copy link
Contributor Author

zsef123 commented Sep 1, 2017

@piskvorky I canceled commit about ignore whole file.

F403 : from module import * used; unable to detect undefined names

And just from module import * disassembled to each functions

@zsef123 zsef123 closed this Sep 5, 2017
@zsef123 zsef123 deleted the refactor-by-pep8 branch September 5, 2017 07:21
@zsef123 zsef123 restored the refactor-by-pep8 branch September 5, 2017 08:19
@zsef123
Copy link
Contributor Author

zsef123 commented Sep 5, 2017

Sorry, I clicked delete accidently.

And I think I've finished applying PEP8.

  1. white space adjustment
  2. delete unused var( except naming _ ), unused import modules
  3. Add specific exceptions( except Exception: )
  4. noqa comments about flake8 F401 error ( Issue flake8 F401 error in __init__.py #1551 )
  5. disassembled import * about flake8 F403 error

@zsef123 zsef123 reopened this Sep 5, 2017
@menshikh-iv
Copy link
Contributor

Hi @zsef123, please remove file gensim/scripts/make_wiki_lemma.py from your PR ASAP (because you broke current symlink -> we can't build package)

@menshikh-iv
Copy link
Contributor

@zsef123 I means to remove this file from your PR (not from the repo), you need to checkout this file to develop state.

@zsef123
Copy link
Contributor Author

zsef123 commented Sep 5, 2017

@menshikh-iv Sorry I check again.
But PEP8 E203 error occurs in travis

gensim/examples/dmlcz/sources.py:125:41: E203 whitespace before ':'
        intId = path[1 + path.rfind('#') :]

gensim/models/word2vec.py:1552:37: E203 whitespace before ':'
                        yield line[i : i + self.max_sentence_length]

Issue #1521

Second, line changed to
return buffer[start_index:start_index + nbytes].view(dtype).reshape(shape, order=order)
these line not occurs error in pep8 library and fine too

And

piskvorky commented 8 days ago • edited
The second option does not conform to PEP8 (see the "No:" section in https://www.python.org/dev/peps/pep-0008/#pet-peeves).Basically, the : operator has a lower priority than e.g. +, and so shouldn't have less whitespace because that implies more priority in Python, by convention.

So I input space around :. Is it problem?

@menshikh-iv
Copy link
Contributor

@zsef123 space before : is problem here.

@zsef123
Copy link
Contributor Author

zsef123 commented Sep 5, 2017

@menshikh-iv In matutils.py, I already remove space before : ( in first commits )
And I requested like under code

-    return buffer[start_index: start_index + nbytes].view(dtype).reshape(shape, order=order)
+    return buffer[start_index:start_index + nbytes].view(dtype).reshape(shape, order=order)

zsef123 7 days ago • edited Contributor
Sorry, Line slice white space was not my intention. I missed these line in this commit.
Anyway, I agree to input space around :.
but if input space like return buffer[start_index : start_index + nbytes], E203 error occurs. (pycodestyle issue)
Is the above line okay if an error occurs?

piskvorky 7 days ago • edited Owner
I don't think there's any error in the code, the version with spaces is correct. The version without spaces is incorrect. It appears to be a bug in the style-checking software. CC @menshikh-iv

So I changed to input space around :

@zsef123
Copy link
Contributor Author

zsef123 commented Sep 5, 2017

@menshikh-iv Should I remove space before :? like line[i: i + self.max_sentence_length]
This line passed Travis

@menshikh-iv
Copy link
Contributor

@zsef123 yep

@menshikh-iv
Copy link
Contributor

Very large and useful work, thank you very much @zsef123 🔥

@menshikh-iv menshikh-iv merged commit 6d6f5dc into piskvorky:develop Sep 5, 2017
zsef123 added a commit to zsef123/gensim that referenced this pull request Sep 6, 2017
…orky#1550)

* gensim dir PEP8 fixes

* corpora dir PEP8 fixes

* example dir PEP8 fixes

* model/wrapper dir PEP8 fixes

* models dir PEP8 fixes

* parsing dir PEP8 fixes

* scripts dir PEP8 fixes

* similarities dir PEP8 fixes

* summarization and topic_coherence dir PEP8 fixes

* test dir PEP8 fixes

* PEP8 E722 error fixes

* PEP8 fixes

* list slice whitespace PEP8 fixes

* disassemble import *

* Fix symlink

* fix symlink

* fix make_wiki_lemma file

* Replace relative import to absolute

* fix typo

* fix E203 error
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.

3 participants