Code Monkey home page Code Monkey logo

pyhstore's Introduction

This is pyhstore, a Python extension module that allows bidirectional
transformation between PostgreSQL hstore and Python dictionary objects
in PL/Python, the PostgreSQL embedded Python programming language.

To build it you will need the Python and PostgreSQL development
headers, as well as the header file for the hstore module.

It has been tested on PostgreSQL 9.0 and Python 2.6, but should work
on earlier versions. It will not work for Python 3.

The easiest way to build and install pyhstore is:
$ python setup.py build_py
$ python setup.py build_ext -I $(pg_config --includedir-server):/usr/src/postgresql-x.x.x/contrib/hstore
$ sudo python setup.py install

After these steps check if you have correctly installed pyhstore, by
connecting to your database and issuing as a superuser:

=# do 'import pyhstore' language plpythonu;

If the second command completed successfully, you're done with the
installation. If not make sure that your server's PYTHONPATH includes
the path where extension modules are installed.

Using pyhstore
==============

You use pyhstore inside your PL/Python functions to serialise and
unserialise hstores from dictionaries. The serialisation functions
actually accept and output text, but in the format that can then by
interpreted by PostgreSQL as hstores. The module itself is a stopgap
solution to enable easier hstore usage until PL/Python gets native
hstore handling abilities.

And example of using pyhstore in PL/Python would be:

CREATE FUNCTION hstore_xform(h hstore) RETURNS hstore AS
$$
import pyhstore

d = pyhstore.parse_hstore(h)
total = 0

for key, val in d.items():
    v = int(val)
    total += v
    d[key] = str(2 * v)

d["total"] = str(total)

return pyhstore.serialize_hstore(d)
$$ LANGUAGE plpythonu;

select * from hstore_xform('John => 10, Mary => 30, Tom => 100');

The output will be a hstore with the value
'"Tom"=>"200", "John"=>"20", "Mary"=>"60", "total"=>"140"'.

The module is distributed under the MIT License.
You can contact the author, Jan Urbanski, at [email protected].

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.