Skip to content

Instantly share code, notes, and snippets.

@nborwankar
Created March 20, 2013 22:04
Show Gist options
  • Save nborwankar/5208953 to your computer and use it in GitHub Desktop.
Save nborwankar/5208953 to your computer and use it in GitHub Desktop.
two files needed for %pg magic in IPython - pgdb.py and pgmagic.py, this is pgmagic.py
__author__ = 'nitin'
# -*- coding: utf-8 -*-
"""
===========
pgmagic
===========
Magics for interacting with Postgres database server via psycopg2.
.. note::
The ``psycopg2`` module needs to be installed separately and
can be obtained using ``easy_install`` or ``pip``.
Usage
=====
``%pg_qry``
{PG_QRY_DOC}
``%pg_test``
{PG_TEST_DOC}
``%pg_close``
{PG_CLOSE_DOC}
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2012 Nitin Borwankar
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
import pgdb
#from IPython.core.displaypub import publish_display_data
from IPython.core.magic import (Magics, magics_class, line_magic, line_cell_magic, needs_local_scope)
from IPython.testing.skipdoctest import skip_doctest
from IPython.core.magic_arguments import (argument, magic_arguments, parse_argstring)
from IPython.utils.py3compat import unicode_to_str
@magics_class
class PGMagics(Magics):
"""A set of magics useful for interactive work with Postgres server via psycopg2.
"""
def __init__(self, shell, data):
super(PGMagics, self).__init__(shell)
self._pg = pgdb
self._cs = self._pg.connect_string(self._pg.db_data)
self._data = data
# prints to console to let developer know alls well - debug use only
print "PGMagics.__init__ got data: %s" % (data)
@skip_doctest
@line_magic
def pg_test(self, line):
return self._pg.test()
@skip_doctest
@line_cell_magic
def pg_qry(self, line, cell=None, local_ns=None):
print "line = %s" % (line)
print "cell = %s" % (cell)
#args = parse_argstring(self.pg_qry, line)
#print "args: %s" % (args)
# arguments 'code' in line are prepended to
# the cell lines
if cell is None:
code = line
return_output = True
line_mode = True
else:
code = line + ' ' + cell
return_output = False
line_mode = False
#code = ' '.join(args.code) + code
print "line_mode = %s" % (line_mode)
print "code = %s" % (code)
text_output = ''
if line_mode:
for line in code.split(';'):
status, results = self._pg.exec_qry(line)
text_output += str(results)
else:
status, results = self._pg.exec_qry(code)
text_output += str(results)
if not text_output:
text_output = 'fail: ' + str(status)
return text_output
@skip_doctest
@line_magic
def pg_close(self, line):
self._pg.close()
def load_ipython_extension(ip):
"""Load the extension in IPython."""
magics = PGMagics(ip, {"x": 1, "y": 2}) # dummy data dict passed for testing
ip.register_magics(magics)
"""
def unload_ipython_extension(ip):
pass
ip = get_ipython()
magics = PGMagics(ip, {"x": 1, "y": 2})
ip.register_magics(magics)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment