Git Product home page Git Product logo

Comments (6)

laughingman7743 avatar laughingman7743 commented on May 25, 2024

The connection object seems to be returned. 🤔
https://github.com/laughingman7743/PyAthena/blob/master/pyathena/__init__.py#L54
Please show the source code when the cursor object is returned.

from pyathena.

spicy-jmr avatar spicy-jmr commented on May 25, 2024

Yes, I was also working on the assumption that Connection is returned. The following are the relevant excerpts from within the code (the get_conn method is within a Connection Manager class):

from pyathena import connect as athena_connect

def get_conn(self, database_name, creds_key):
    return athena_connect(
        aws_access_key_id=self.passwords[creds_key]['un'],
        aws_secret_access_key=self.passwords[creds_key]['pw'] if not self.decrypt_key else
        decrypt(self.decrypt_key, self.passwords[creds_key]['pw']).decode("utf-8"),
        s3_staging_dir=athena_us_east_1[database_name],
        region_name='us-east-1'
    )

and the excerpts of calling code which is throwing an error:

with connection_manager_instance.get_conn(db_name, key) as athena_conn:
    self.src_cursor = athena_conn.cursor()

error thrown is basically that Cursor object has no method cursor()

Do note, that I'm using all of this within a larger class which also utilizes another DB API 2.0 compliant package (pyhive.presto) which returns connection class when the same above format is used. I suspect the issue might be within your
connection.py __enter__()
since there you return the cursor and not the connection; thus, unexpected behaviour when used in a 'with -- as' block.

from pyathena.

laughingman7743 avatar laughingman7743 commented on May 25, 2024

Oh, the context manager will certainly return a cursor object.
https://github.com/laughingman7743/PyAthena/blob/master/pyathena/connection.py#L82

PyMySQL and mysqlclient-python return a cursor object.
https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/connections.py#L497
https://github.com/PyMySQL/mysqlclient-python/blob/master/MySQLdb/connections.py#L284

Which one of a cursor object and a connection object should be returned? It seems that the specification of the context manager is not written in PEP 249. 🤔

from pyathena.

spicy-jmr avatar spicy-jmr commented on May 25, 2024

Interesting; I've been using psycopg2 within context extensively, as it helps maintain clean connection lifecycles; however it does require that connection object, so that the commits/rollbacks can get called on the connection. True, this use-case may not be applicable with Athena connections, however.

Food for thought. Receiving a cursor instead of a connection when a call to connect() is made seems counter-intuitive. I would propose at the very least adding that usage example in the documentation, to clarify that distinction: when calling connect() in context you receive the cursor object back, but without the context, you must explicitly call Connection.cursor();

from pyathena.

laughingman7743 avatar laughingman7743 commented on May 25, 2024

I think that returning a connection object is simpler and easier to understand.
I will fix it on the weekend. 😆

Thanks,

from pyathena.

laughingman7743 avatar laughingman7743 commented on May 25, 2024

I fixed it. Please check the pull requests #43 .

from pyathena.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.