Conversation
timarmstrong
left a comment
There was a problem hiding this comment.
There's already a way to do something similar to this by setting dictify=true in the cursor constructor. I can see that this is still useful if you have an existing cursor and want to convert it.
That feature is missing from the README, so I think we should document it at the very least. I'm open to having this function too, but it'd be good to have a basic test so that nobody breaks it in the future.
| coerce_float=coerce_float) | ||
|
|
||
|
|
||
| def as_dict(cursor): |
There was a problem hiding this comment.
I think as_dicts() would be a clearer name (it sounds like it returns a single dictionary).
There was a problem hiding this comment.
You are right! Another option is dictfetchall. This name refers to the fetchall operation that occurs on the function.
There was a problem hiding this comment.
Either is fine by me. It looks like the dictfetchall is used by django so there's some precedent.
| coerce_float=coerce_float) | ||
|
|
||
|
|
||
| def as_dict(cursor): |
There was a problem hiding this comment.
Can you add a test for this method. E.g. see impala/tests/test_hive_dict_cursor.py
|
Hi timarmstrong, What you think about both class HiveServer2Cursor(Cursor):
def as_dicts(self):
pass
def as_pandas(self):
passI think this provides a concise interface. with conn.cursor() as cursor:
cursor.execute('SELECT * FROM mytable LIMIT 100')
data = cursor.as_dict()
with conn.cursor() as cursor:
cursor.execute('SELECT * FROM mytable LIMIT 100')
df = cursor.as_pandas() And of course the class HiveServer2DictCursor could be removed. |
|
I think those are both useful methods. My ask is to not remove any existing functionality that people may depend on, but adding new convenience methods seems like a good idea. |
|
@rafaelreuber @timarmstrong -- Does adding an |
|
We already have an as_pandas method, but it only imports pandas inside the method. So I guess it's only a dependency if you want to use that method. |
Add the util function as_dict. This is very useful to create APIs.