gdata AttributeError: 'ContactEntry' object has no attribute 'name' - python

Using GData Python libraries, version 2.0.18
Attempting to retrieve contact list using the Service approach (not Client like the sample app). It appears that the return is mapped to a ContactEntry (good), but it gives error when I try to access the name attribute:
AttributeError: 'ContactEntry' object has no attribute 'name'
from gdata.contacts.service import ContactsService
(...)
self.client = ContactsService(source='appname', additional_headers=additional_headers )
feed = self.client.GetContactsFeed(uri=query.ToUri())
self.client is a gdata.contacts.service
GetContactsFeed uses
def GetContactsFeed(self, uri=None):
uri = uri or self.GetFeedUri()
return self.Get(uri, converter=gdata.contacts.ContactsFeedFromString)
The sample code uses desired_class=gdata.contacts.data.ContactsFeed
Seems like there should be a name attribute.
Is my syntax wrong?

Ok, here is the issue for the python contacts sample vs my implementation:
in gdata/sample/contacts/contacts_example.py it uses gdata.contacts.Client which (long chain to it) calls for the atom classes to use desired_class=gdata.contacts.data.ContactsFeed.
The service, as pointed out in the question, uses converter=gdata.contacts.ContactsFeedFromString.
This converter comes from the init file, src/gdata/contacts/init.py, as do the class definitions. Obviously at this point, you know what's coming -- the classes for the xml in the initializer do not match the ones in the data file.
I added these missing, incorrect ones in to initializer and things worked as expected. Alternatively, changing to use desired_class would do it too (at some point you'd have to map to converter...isn't supported directly in service.py), or adding converter to data.ContactsFeed, etc.
Hope this helps someone.

Related

Azure blob storage model access for gensim in python

I am trying to load my model files using below code
import gensim
import os
from azure.storage.blob import BlobServiceClient
from smart_open import open
azure_storage_connection_string = "DefaultEndpointsProtocol=https;AccountName=lnipcfdevlanding;AccountKey=xxxxxxxxx"
client = BlobServiceClient.from_connection_string(azure_storage_connection_string)
file_prefix="azure://landing/TechnologyCluster/VectorCreation/embeddings/"
fin = open(file_prefix+"word2vec.Tobacco.fasttext.model", transport_params=dict(client=client))
clustering.embedding = gensim.models.Word2Vec.load(fin)
But it is failing with below error
AttributeError: '_io.TextIOWrapper' object has no attribute 'endswith'
I assume the way I am passing file to gensim.models.Word2Vec.load is not the right way. I could not find any good example that how to pass the filename which is on Azure blob storage, if I give complete uri it does not work, what is the right way to achieve this ?
Please check :
AttributeError usually occurs when save() or load() function is called on an object instance instead of class as that is a class method..
Please note that the information in file can be incomplete ,check if the binary tree is missing.In this case, the query may be obtained ,but you may not be able to continue with a model loaded with this way.
Check if the file path must be saved in word2vec-format file
binary is bool, if True, indicates data is in binary word2vec format.
Note from: https://radimrehurek.com/gensim/models/keyedvectors.html.
Check if this way can be worked around by importing required models and by checking version compliance : -
gensim.models.Word2Vec.load_word2vec_format('model', binary=True)
or withKeyedVectors.load in place of .Word2Vec.load_... according to what supports fasttext.
Also check whether the model is correctly supports the
function.
References:
python - AttributeError: 'Word2Vec' object has no attribute
'endswith' - Stack Overflow
models.keyedvectors – Store and query word vectors — gensim
(radimrehurek.com)

VSCode Python autocompletion not working on some classes only

When using autocompletion in VSCode in my python project, it works very well except for one class and I cannot figure out why.
The class is coming from a module database which contains 3 classes total: DatabaseClient, Database, Collection
DatabaseClient has a get_database() method which returns a Database object.
Database has a get_collection() method which returns a Collection object.
Collection has several methods.
The autocompletion works as intended on the DatabaseClient & Database objects, but not at all on the Collection object.
The code below works and give the expected results (line 15 and 20 of the screenshot results match), but not the autocompletion while editing as stated above.
import lib.database as database
print( database.DatabaseClient().get_database('business').get_collection('reviews').find() )
client = database.DatabaseClient()
db = client.get_database(client.list_database_names()[0])
coll = db.get_collection(db.list_collection_names()[0])
print(coll.find())
Tried to reload VSCode, unload and reload the python extensions.
Tried to uninstall and re-install the python extensions.
Tried to change the name of my module.
Tried to change the name of the class.
Tried to change the name of the method of the Collection class.
Tried using single line command line 3 and noticed it worked this way.
Found out the solution.
The problem comes from not using typing in the method headers.
Once I added proper typing, the autocompletion worked everywhere as expected.
def get_database(self, database_name: str) -> Database:
def get_collection(self, collection_name: str) -> Collection:

register an external exporter to pyqtgraph

I would like to add a format to export plots in a pyqtgraph application. I've dived in the code looking for a way to do that, but from outside the library source code didn't work. Or at least I couldn't make it work.
Looking how the exporters in the library work, I thought that replicating, for example what the CSVExporter does, would work. In short, a sample code like:
from pyqtgraph.exporters.Exporter import Exporter
class DataFormatExporter(Exporter):
Name = "DataFormat from plot data"
# (...) pure copy of the rest
DataFormatExporter.register()
The only exported that doesn't do this last call to register is the PrintExporter but all the others. This method 'register' is a #classmethod in the Exporter superclass.
But if I test the code, the exception says that the newer class doesn't have the register defined:
AttributeError: type object 'DataFormatExporter' has no attribute 'regiter'
The closest reference to what I'm looking is a post in a pyqtgraph google group and its steps 1 and 2 are just what I've thought should be and what I've tried.
Any hint?

Python. Adding custom methods to existing site-packages

I'm using openpyxl with python 2.7 to manipulate some workbooks. I've implemented my own methods for class Worksheet except they're not being found when I try to access them. I''m on windows and I've adding the following code to site-packages/openpyxl/worksheet/worksheet.py. Source code here.
class Worksheet(_WorkbookChild):
"""
Library defined methods here.
"""
#My Method.
def hello_world(self):
print "Hello from worksheet."
When I open up a workbook and try and call my function it raises an error for 'no attribute' even though it's been defined (properly?) under the scope of the class Worksheet.
import openpyxl
wb = openpyxl.load_workbook('helloworld.xlsx')
sheet = wb.get_active_sheet()
sheet.hello_world()
AttributeError: 'Worksheet' object has no attribute 'hello_world'
Do I need to do anything differently to update the existing library? I've deleted the .pyc, recompiled and am still getting the same error.
For anyone else who runs into this problem. The library was using 4 spaces for each indent while I was using a regular tab. The variation in indents caused the method to not be defined even though it appeared to be.

pickling and unpickling user-defined class

I have a user-defined class 'myclass' that I store on file with the pickle module, but I am having problem unpickling it. I have about 20 distinct instances of the same structure, that I save in distinct files. When I read each file, the code works on some files and not on others, when I get the error:
'module' object has no attribute 'myclass'
I have generated some files today and some other yesterday, and my code only works on the files generated today (I have NOT changed class definition between yesterday and today).
I was wondering if maybe my method is not robust, if I am not doing things as I should do, for example maybe I cannot pickled user-defined class, and if this is introducing some randomness in the process.
Another issue could be that the files that I generated yesterday were generated on a different machine --- because I work on an academic cluster, I have some login nodes and some computing nodes, that differs by architecture. So I generated yesterday files on the computing nodes, and today files on the login nodes, and I am reading everything on the login nodes.
As suggested in some of the comments, I have installed dill and loaded it with import dill as pickle. Now I can read the files from computing nodes to login nodes of the same cluster. But if I try to read the files generated on the computing node of one cluster, on the login node of another cluster I cannot. I get KeyError: 'ClassType' in _load_type(name) in dill.py
Can it be because the python version is different? I have generated the files with python2.7 and I read them with python3.3.
EDIT:
I can read the pickled files, if I use everywhere python 2.7. Sadly, part of my code, written in python 3, is not automatically compatible with python 2.7 :(
Can you from mymodule import myclass? Pickling does not pickle the class, just a reference to it. To load a pickled object python must be able to find the class that was to be used to create the object.
eg.
import pickle
class A(object):
pass
obj = A()
pickled = pickle.dumps(obj)
_A = A; del A # hide class
try:
pickle.loads(pickled)
except AttributeError as e:
print(e)
A = _A # unhide class
print(pickle.loads(pickled))

Categories

Resources