'Returns the exact string that is sent to the database by calling the
execute() method.
This method follows the extension to the DB API 2.0 followed by Psycopg.'

def mogrify(self, query, args=None):

    conn = self._get_db()
    if PY2:
        query = self._ensure_bytes(query, encoding=conn.encoding)
    if (args is not None):
        query = (query % self._escape_args(args, conn))
    return query
