import psycopg2
from psycopg2.extensions import AsIs


class LiteralValue(str):
    def __conform__(self, proto):
        return self

    def getquoted(self):
        return self


class LiteralValue2(object):
    def __init__(self, val):
        self._val = val

    def __conform__(self, proto):
        return self

    def getquoted(self):
        return str(self._val)


conn = psycopg2.connect("dbname=postgres user=postgres")
cur = conn.cursor()
print cur.mogrify('select %s', (5,))
print cur.mogrify('select %s', (LiteralValue('5'),))
print cur.mogrify('select %s', (LiteralValue2('5'),))
print cur.mogrify('select %s', (AsIs('5'),))
