Error when loading the word2vec model - word2vec

I am using the following function to load my word2vec model.
def __init__(self, filename):
print filename
try:
self.model = gensim.models.Word2Vec.load(filename)
except cPickle.UnpicklingError:
load = gensim.models.Word2Vec.load_word2vec_format
self.model = load(filename, binary=True)
However, I am getting the following error when I try to do it.
Traceback (most recent call last):
File "./explore", line 70, in <module>
api_controller.model = Model(sys.argv[1])
File "/home/volka/Documents/projects/word2vec-explorer/explorer.py", line 77, in __init__
self.model = gensim.models.Word2Vec.load(filename)
File "/usr/local/lib/python2.7/dist-packages/gensim/models/word2vec.py", line 1458, in load
model = super(Word2Vec, cls).load(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/gensim/utils.py", line 256, in load
obj = unpickle(fname)
File "/usr/local/lib/python2.7/dist-packages/gensim/utils.py", line 920, in unpickle
return _pickle.loads(f.read())
AttributeError: 'module' object has no attribute 'call_on_class_only'
The genism version I am using in both the versions are 0.12.3.
Please let me know where I am making it wrong?
This is how I tried to remove call_on_class_only.
model = word2vec.Word2Vec(text, sg=0, negative=5, hs=0)
model.save("test_project")
#load, delete and save
model_1 = word2vec.Word2Vec.load("test_project")
del model_1.call_on_class_only
model.save(model_name_2)
It gives me the following error: AttributeError: call_on_class_only
Please help me.

Related

Graphene Returning NoneType Error on Subscription

I'm trying to setup subscription on graphene-django and channels using channels_graphql_ws.
I'm getting the following error when trying to run my subscription query:
An error occurred while resolving field Subscription.onNewComment
Traceback (most recent call last):
File "/Users/noroozim/.pyenv/versions/nexus37/lib/python3.7/site-packages/graphql/execution/executor.py", line 450, in resolve_or_error
return executor.execute(resolve_fn, source, info, **args)
File "/Users/noroozim/.pyenv/versions/nexus37/lib/python3.7/site-packages/graphql/execution/executors/sync.py", line 16, in execute
return fn(*args, **kwargs)
File "/Users/noroozim/.pyenv/versions/nexus37/lib/python3.7/site-packages/channels_graphql_ws/subscription.py", line 371, in _subscribe
register_subscription = root.register_subscription
AttributeError: 'NoneType' object has no attribute 'register_subscription'
Here is what I have in my setup:
# /subscription.py/
class OnNewComment(channels_graphql_ws.Subscription):
comment = Field(types.UserCommentNode)
class Arguments:
content_type = String(required=False)
def subscribe(root, info, content_type):
return [content_type] if content_type is not None else None
def publish(self, info, content_type=None):
new_comment_content_type = self["content_type"]
new_comment = self["comment"]
return OnNewComment(
content_type=content_type, comment=new_comment
)
#classmethod
def new_comment(cls, content_type, comment):
cls.broadcast(
# group=content_type,
payload={"comment": comment},
)
I'm not sure if this is a bug or if I'm missing something.
I found out that graphne's graphiql template doesn't come with websocket support and I had to modify my graphene/graphiql.html file to incorporate websocket to get it to work.

Django Model QuerySet: RuntimeError generator raised StopIteration

I've recently updated my Django to version 2.2.4 and python to 3.7,
and I encounter this error randomly:
Traceback (most recent call last):
File "/foo/venv/lib/python3.7/site-packages/django/db/models/query.py", line 73, in __iter__
obj = model_cls.from_db(db, init_list, row[model_fields_start:model_fields_end])
File "/foo/venv/lib/python3.7/site-packages/django/db/models/base.py", line 511, in from_db
for f in cls._meta.concrete_fields
File "/foo/venv/lib/python3.7/site-packages/django/db/models/base.py", line 511, in <listcomp>
for f in cls._meta.concrete_fields
StopIteration
I tried to debug the code to find out what's happening, and it seems the from_db function in django.db.models.base.py is causing this error:
# /foo/venv/lib/python3.7/site-packages/django/db/models/base.py
...
#classmethod
def from_db(cls, db, field_names, values):
if len(values) != len(cls._meta.concrete_fields):
values_iter = iter(values)
values = [
next(values_iter) if f.attname in field_names else DEFERRED
for f in cls._meta.concrete_fields
]
new = cls(*values)
new._state.adding = False
new._state.db = db
return new
...
next(values_iter) is raising this error and it seems that Django devs should surround that with try except block to make this work in python 3.7,
but my question is, is there a way to overcome this issue as a temp fix or not?
thanks.
Update#1:
I've found out when exactly this error happens, When I call .values_list('myfield', flat=True) on a QuerySet, the query changes to SELECT myfield from ... and this breaks everything.

Why am I getting TypeError: 'vc' is an invalid keyword argument for this function error?

I don't understand what I am doing wrong here. Here is my Django model:
class VMMigrationEvent(models.Model):
created = models.DateTimeField(auto_now_add=True) # DateTime because there may be multiple events in a day.
dc = models.TextField(max_length=150), # data center
destination_host = models.TextField(max_length=150), # host migrated to.
message = models.TextField(), # The message from virtual center.
updated = models.DateTimeField(auto_now=True) # DateTime because there may be multifple events in a day.
user = models.TextField(max_length=150), # The user logged into the virtual center that execute the migration.
vc = models.TextField(max_length=150), # virtual center
vm = models.ForeignKey(VirtualMachine) # The VirtualMachine record associated with this event.
And from the python console I do this:
>>> from cm.models import *
>>> dc='DCM01N-01'
>>> destination_host='auroravm2-1.example.com'
>>> message='my hovercraft is full of eels.'
>>> user='mister_gumby'
>>> vc='vca-001-s.example.com'
>>> vm='ads-108'
>>> vm_db_obj = VirtualMachine.objects.filter(name=vm).latest('create_date')
>>> vmme = VMMigrationEvent.objects.create(dc=dc,
... destination_host=destination_host,
... message=message, user=user, vc=vc, vm=vm_db_obj)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/apps/man/man/env/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/apps/man/man/env/lib/python2.7/site-packages/django/db/models/query.py", line 392, in create
obj = self.model(**kwargs)
File "/apps/man/man/env/lib/python2.7/site-packages/django/db/models/base.py", line 573, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'vc' is an invalid keyword argument for this function
Why is vc invalid? It is a field in my model?
You have commas after several of your field definitions: vc, but also dc, destination_host and user. This turns them into tuples, which can't be used as fields on the model.
Delete those commas
(Also, you probably meant CharField rather than TextField.)

Exception with Django 1.6.5 and factory_boy

I decided to use factory_boy in my simple django application for test purposes.
But I had a problem with simple example. Here is the code of my simple test.
from django.utils import unittest
from ..models import Server, ServerAddress, L2TPServer, serialize_open_vpn_server_json
from factory import django as django_factory
class SshOpenVpnServerFactory(django_factory.DjangoModelFactory):
class Meta:
model = L2TPServer
django_get_or_create = ('name', 'address')
name = 'Hello'
address = 'Nono'
class ServersTestCase(unittest.TestCase):
def test_serialize_server_info(self):
print Server.objects.all()
SshOpenVpnServerFactory.build()
When test-runner executes this test, I get an error:
Traceback (most recent call last):
File "/Users/green/Development/Wasel/experimental/wasel_services/packages/waselcore/backend/tests/test_models.py", line 20, in test_serialize_server_info
SshOpenVpnServerFactory.build()
File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/base.py", line 504, in build
attrs = cls.attributes(create=False, extra=kwargs)
File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/base.py", line 365, in attributes
force_sequence=force_sequence,
File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/containers.py", line 265, in build
sequence = self.factory._generate_next_sequence()
File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/base.py", line 338, in _generate_next_sequence
cls._setup_counter()
File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/base.py", line 318, in _setup_counter
first_seq = cls._setup_next_sequence()
File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/django.py", line 83, in _setup_next_sequence
manager = cls._get_manager(model)
File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/django.py", line 76, in _get_manager
return target_class.objects
AttributeError: 'NoneType' object has no attribute 'objects'
Where am I wrong? Does factory_boy support django 1.6.5?
The proposed syntax using the Meta class will only be released as part of the (unreleased) Factory Boy 2.4. Use SshOpenVpnServerFactory.FACTORY_FOR instead. Related issue: https://github.com/rbarrois/factory_boy/issues/143

django get_profile error

I want to get my userprofile id in the views i have already included below in settings.py
AUTH_PROFILE_MODULE = 'users.UserProfile'
views code:
def savecontent(request):
try:
logging.debug("=========================== in save content")
logging.debug(dir(request.user))
my_id = request.user.get_profile().id
logging.debug(my_id)
Error is:
2012-07-17 13:00:00,564 ERROR Error while saing content
Traceback (most recent call last):
File "/opt/labs/labs_site/content/views.py", line 21, in savecontent
my_id = request.user.get_profile().id
File "/opt/labs/django/django/utils/functional.py", line 185, in inner
return func(self._wrapped, *args)
AttributeError: 'AnonymousUser' object has no attribute 'get_profile'
AttributeError: 'AnonymousUser' object has no attribute 'get_profile'
You must have an authenticated user to retrieve a profile, that's your problem
#login_required
def foo(request):
profile_id = request.user.get_profile().id