Skip to content

with Batch() as batch: clause throws AttributeError regarding Connection object #865

@charlesbclifford

Description

@charlesbclifford

Just installed gcloud-python this past Tuesday, and this is my 1st time using it. Shown below is the code that is throwing an AttributeError regarding the Connection object. The code is about a method which builds a batch of new entities which are intended to loaded into Datastore.

After the last batch.put(_ENTITY) statement executes successfully, and the with datastore.batch.Batch(connection=connection) as batch: clause completes, this error is thrown

Load_Datastore() Unexpected error: <type 'exceptions.AttributeError'>
AttributeError: 'Connection' object has no attribute 'commit'

I'm not sure why the reference to the commit attribute is made relative to the Connection object, and not to the Batch object.

Here's the abbreviated method call that throws the AttributeError regarding the Connection object:

from gcloud import storage
storage.set_default_bucket('')
storage.set_default_project('aaaaaaa-bbbbbb-33333')
from gcloud import datastore
datastore.set_default_dataset_id('xxx_xxx')
from gcloud.datastore.batch import Batch

def Load_Datastore():
    try:
        Get_Bucket_Metadata()
        #set configuration variables       
        connection = storage.get_connection()
        storage.set_default_connection(connection)
        datastore.set_default_connection(connection)

                    if not _RECORDS:
                        print "_RECORDS is empty"
                    else:
                        print "there are %s records to write into Datastore"%(str(len(_RECORDS)))
                        #start transaction with Datastore 
                        with datastore.batch.Batch(connection=connection) as batch:
                            _ENTITIES=[]                                           
                            for _RECORD in _RECORDS:
                                _PROPERTIES=[]

                                if _RECORD:
                                    for _KEY_VALUES in _RECORD:
                                        _KEY_VALUE_PAIR=_KEY_VALUES.split('|')
                                        if str(_KEY_VALUE_PAIR[0])=='ROW_KEY':
                                            _ENTITY_KEY=str(_KEY_VALUE_PAIR[1])
                                        else:
                                            _PROPERTIES.append("%s|%s"%(str(_KEY_VALUE_PAIR[0]),str(_KEY_VALUE_PAIR[1])))

                                #an empty list is False
                                if not _PROPERTIES:   
                                    print "_PROPERTIES is empty"
                                else:   
                                    try:
                                        key = datastore.Key('CI_ADJ_TYPE',_ENTITY_KEY,namespace='xxx',dataset_id='xxx_yyy')  
                                        try:
                                           DS_ENTITY = datastore.Entity(key)
                                            for _PROPERTY in _PROPERTIES:
                                                _NAME_VALUE=_PROPERTY.split('|')
                                                _COLUMN_NAME=str(_NAME_VALUE[0])
                                                _COLUMN_VALUE=str(_NAME_VALUE[1])
                                                DS_ENTITY.update({_COLUMN_NAME:_COLUMN_VALUE})
                                            _ENTITIES.append(DS_ENTITY)                                 
                                        except Exception, inst:
                                            print "datastore.Entity(%s) Unexpected error: %s"%(str(_ENTITY_KEY),str(sys.exc_info()[0]))
                                            if isinstance(inst,ValueError):
                                                print "ValueError: %s"%(inst.message)
                                            if isinstance(inst,AttributeError):
                                                print "AttributeError: %s"%(inst.message)
                                    except Exception, inst:
                                        print 'datastore.Key(CI_ADJ_TYPE,key_index,namespace=xxx,dataset_id=xxx_yyy) Unexpected error: ' + str(sys.exc_info()[0])
                                        if isinstance(inst,ValueError):
                                            print "ValueError: %s"%(inst.message)
                                        if isinstance(inst,AttributeError):
                                            print "AttributeError: %s"%(inst.message)
                                        #print 'datastore.Key() Exception instance type: ' + str(type(inst))                      
                            for _ENTITY in _ENTITIES:
                                batch.put(_ENTITY)
                                #print 'batch.put(_ENTITY)'

                        #query = datastore.Query(kind='CI_ADJ_TYPE')
                        #for result in query.fetch():
                         #   print result   

    except Exception, inst:
        print '\nLoad_Datastore() Unexpected error: ' + str(sys.exc_info()[0])
        if isinstance(inst,ValueError):
            print "ValueError: %s\n"%(inst.message)
        if isinstance(inst,AttributeError):
            print "AttributeError: %s\n"%(inst.message)
        if isinstance(inst,TypeError):
            print "TypeError: %s\n"%(inst.message)                            
        raise

Any suggestions to get past this bug are appreciated.

Metadata

Metadata

Assignees

Labels

api: datastoreIssues related to the Datastore API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions