GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — with Batch() as batch: clause throws AttributeError regarding Connection object · Issue #865 · googleapis/google-cloud-python

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

New issue
Copy link
New issue
Copy link
Closed
Closed
with Batch() as batch: clause throws AttributeError regarding Connection object#865
Copy link
Assignees
dhermes
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.

Description

@charlesbclifford
charlesbclifford
opened on May 8, 2015
Issue body actions

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.

Activity

  1. jgeewax
    added
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    type: questionRequest for information or clarification. Not an issue.
    api: datastoreIssues related to the Datastore API.
    on May 8, 2015
  2. jgeewax
    added this to the This milestone has been deleted milestone on May 8, 2015
  3. dhermes commented on May 8, 2015

    @dhermes
    dhermes
    on May 8, 2015
    Contributor
    More actions

    You are calling connection = storage.get_connection() and using that connection with the datastore package.

    You need a datastore connection for interacting with the datastore

    from gcloud import datastore
    datastore_cnxn = datastore.get_connection()

    You can instead just let the implicit behavior take over and just execute

    with datastore.Batch() as batch:
        ...

    Sorry I didn't try to dig deeper into the code snippet, it is a bit of a mouthful.

  4. dhermes commented on May 8, 2015

    @dhermes
    dhermes
    on May 8, 2015
    Contributor
    More actions

    @charlesbclifford I'm going to close out. The issue is expected since Connection.commit will in fact be an attribute error for the Connection defined in storage, but won't be for the one defined in datastore.

    Please re-open if you are still having issues or have remaining questions.

  5. dhermes
    closed this as completedon May 8, 2015
  6. charlesbclifford commented on May 8, 2015

    @charlesbclifford
    charlesbclifford
    on May 8, 2015
    Author
    More actions

    Thanks for the fix, which I implemented.

    Now, using the correct datastore connection, this error is thrown:
    gcloud.exceptions.ServiceUnavailable

    There's got to be some other object property I've corrupted.

    On Friday, May 8, 2015, Danny Hermes [email protected] wrote:

    @charlesbclifford https://github.com/charlesbclifford I'm going to
    close out. The issue is expected since Connection.commit will in fact be
    an attribute error for the Connection defined in storage, but won't be
    for the one defined in datastore.

    Please re-open if you are still having issues or have remaining questions.

    —
    Reply to this email directly or view it on GitHub
    #865 (comment)
    .

    Charles B. Clifford
    (720) 842-7251

  7. dhermes commented on May 8, 2015

    @dhermes
    dhermes
    on May 8, 2015
    Contributor
    More actions

    Can you try making a datastore request with a short snippet and report back the stack trace?

    For example

    from gcloud import datastore
    
    # Make sure proper env. vars. are set for config
    q = datastore.Query('Foo')
    print list(q.fetch())

    I'm going to guess you are getting the 503 error because you haven't turned on the Cloud Datastore service yet in the APIs console. Can you make sure it is turned on?

  8. charlesbclifford commented on May 8, 2015

    @charlesbclifford
    charlesbclifford
    on May 8, 2015
    Author
    More actions

    In the Google Developers Console, of a project for which I am an
    owner, under APIs and auth, I can see that the GoogleCloud Datastore API is
    enabled.

    When I import datastore, set the Datastore default data set id, and the
    Datastore default connection, and submit the suggested commands, the
    ServiceUnavailable error is returned.

    Are there more than the above 2 defaults which must be set? My code must be
    missing some configuration setting.

    On Friday, May 8, 2015, Danny Hermes [email protected] wrote:

    Can you try making a datastore request with a short snippet and report
    back the stack trace?

    For example

    from gcloud import datastore

    Make sure proper env. vars. are set for config

    q = datastore.Query('Foo')print list(q.fetch())


    I'm going to guess you are getting the 503 error because you haven't
    turned on the Cloud Datastore service yet in the APIs console. Can you make
    sure it is turned on?

    —
    Reply to this email directly or view it on GitHub
    #865 (comment)
    .

    Charles B. Clifford
    (720) 842-7251

  9. dhermes commented on May 8, 2015

    @dhermes
    dhermes
    on May 8, 2015
    Contributor
    More actions

    Can you post a stack trace? What OS are you using? How are you setting your environment variables (or your defaults in code?) What form of auth are you using?

  10. charlesbclifford commented on May 9, 2015

    @charlesbclifford
    charlesbclifford
    on May 9, 2015
    Author
    More actions

    I am gathering together all of the requested info and will post the info
    shortly.

    Thanks.

    On Friday, May 8, 2015, Danny Hermes [email protected] wrote:

    Can you post a stack trace? What OS are you using? How are you setting
    your environment variables (or your defaults in code?) What form of auth
    are you using?

    —
    Reply to this email directly or view it on GitHub
    #865 (comment)
    .

    Charles B. Clifford
    (720) 842-7251

  11. dhermes commented on May 9, 2015

    @dhermes
    dhermes
    on May 9, 2015
    Contributor
    More actions

    Did you intend to open a new issue?

  12. jgeewax commented on May 9, 2015

    @jgeewax
    jgeewax
    on May 9, 2015
    Contributor
    More actions

    Also, what happens when you look at the Datastore UI for your project (https://console.developers.google.com/project/_/datastore/stats , then choose your project from the list)

  13. JustinBeckwith
    assigned
    dhermes
    on Feb 1, 2021
  14. parthea
    added a commit that references this issue on Aug 21, 2025

    fix(deps): update proto-plus to >=1.25 for Python 3.13 compatibility (#…

    3e8694b
  15. parthea
    added a commit that references this issue on Sep 16, 2025

    fix(deps): update proto-plus to >=1.25 for Python 3.13 compatibility (#…

    34991b2
  16. parthea
    added 2 commits that reference this issue on Nov 24, 2025

    chore(deps): update dependency protobuf to v3.16.0 (#865)

    78bdd21

    fix: flaky tests due to imprecision in floating point calculation and…

    6f47c1d
  17. parthea
    added a commit that references this issue on Feb 13, 2026

    chore: librarian release pull request: 20260108T134327Z (#883)

    b6f1ec6
  18. parthea
    added 3 commits that reference this issue on Mar 6, 2026

    fix: make unicode characters working well in load_table_from_json (#865)

    bf615e8

    chore: release 2.24.0 (#868)

    b479f51

    feat!: Support Django 4.2 (#865)

    77fddcb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @dhermes
    dhermes

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

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.