-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Throw exception if load() is called on instance rather than the class #889
Changes from 6 commits
25fc004
58976d8
cf34e2d
773a75e
96d65ec
d91b99d
c156c79
60d42a3
cc7e3e1
a0ea2b4
2d85ef5
ac0b0f3
770a277
772cdcd
4c4ec8c
7173993
bb7b9e9
23461e3
fb5bd0b
6ef3599
075d925
bb53ccf
e39b8d6
cec0a1b
c09e4d8
ea89bd5
5d743f1
d045bd2
07b3700
40f2a50
43c7b08
b50d60a
eba33bc
6ffacdf
f0137b8
3bf25fc
7519519
2d28b03
19d2035
e68bb13
37c9149
d9b45c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,7 @@ | |
from six import iteritems, itervalues, string_types | ||
from six.moves import xrange | ||
from types import GeneratorType | ||
from types import MethodType | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -452,6 +453,9 @@ def __init__( | |
self.total_train_time = 0 | ||
self.sorted_vocab = sorted_vocab | ||
self.batch_words = batch_words | ||
|
||
self.load = methodize(load, self) | ||
self.load_word2vec_format = methodize(load, self) | ||
|
||
if sentences is not None: | ||
if isinstance(sentences, GeneratorType): | ||
|
@@ -1869,3 +1873,15 @@ def __iter__(self): | |
model.accuracy(args.accuracy) | ||
|
||
logger.info("finished running %s", program) | ||
|
||
def methodize(func, instance): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These methods are defined outside of the class. Maybe that is why you see no difference in calling these methods and class methods? Can you link to a gist of the code you use to test the first parameter? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This was done on purpose. Please see here for reference.
So the argument There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My latest build is failing on Python 2 for some reason, I'm working on that right now. Once that's done, I'll write the tests and merge it back with develop. |
||
return MethodType(func, instance.__class__) | ||
|
||
|
||
def load(self, *args, **kwargs): | ||
raise TypeError("Call to load must be made using class") | ||
|
||
|
||
def load_word2vec_format(self, fname, fvocab=None, binary=False, encoding='utf8', unicode_errors='strict', | ||
limit=None, datatype=REAL): | ||
raise TypeError("Call to load must be made using class") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean
self.load = methodize(self.load, self)
?it is very confusing to assign
self.load
and then definedef load
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was done on purpose. Please see here for reference.
MethodType
binds the method to the calling instance. What I'm doing here is something like overloadingload
based on whether it is called on instance or class.So the argument
load
tomethodize
is the name of the function that is to be bound.