2

I have a csv file where one of the columns is written in hstore format. I would like to convert it into python dict but at the same time keep my code DRY. My codebase uses sqlalchemy, which has a _parse_hstore function defined in https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/dialects/postgresql/hstore.py#L390. I tried to import it like this:

import sqlalchemy.dialects.postgresql.hstore as hstore

But unfortunately the upstream module (sqlalchemy.dialects.postgresql) shadows the hstore module with an object: https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/dialects/postgresql/init.py#L53

Is it still possible to import the module somehow? I'd prefer not to copy function code directly into my codebase even if the function is relatively simple.

1 Answer 1

3

You can try to use function HSTORE.result_processor (from sqlalchemy.dialects.postgresql import HSTORE) which is wrapper for private _parse_hstore function.

>>> h = sqlalchemy.dialects.postgresql.HSTORE()
>>> f = h.result_processor(None, None)
>>> f('"a"=>"1"')
{'a': '1'}

Snippet credits to @lbolla.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.