mnowotka
mnowotka

Reputation: 17258

parsing hstore in python3 using sqlalchemy

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.

Upvotes: 2

Views: 794

Answers (1)

maciek
maciek

Reputation: 3374

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.

Upvotes: 3

Related Questions