<commit_before># -*- coding: utf-8 -*-
"""
    pyrseas.dbconn
    ~~~~~~~~~~~~~~

    A `DbConnection` is a helper class representing a connection to a
    PostgreSQL database.
"""
import os
import sys
import getpass

from psycopg2 import connect
from psycopg2.extras import DictConnection


class DbConnection(object):
    """A database connection, possibly disconnected"""

    def __init__(self, dbname, user=None, pswd=False, host=None, port=None):
        """Initialize the connection information

        :param dbname: database name
        :param user: user name
        :param pswd: prompt user for password
        :param host: host name
        :param port: host port number
        """
        self.dbname = dbname
        self.user = user
        if pswd:
            self.pswd = " <PASSWORD>()
        else:
            self.pswd = ''
        if host is None or host == '127.0.0.1' or host == 'localhost':
            self.host = ''
        else:
            self.host = "host=%s " % host
        if port is None or port == 5432:
            self.port = ''
        else:
            self.port = "port=%d " % port
        self.conn = None
        self._version = 0

    def connect(self):
        """Connect to the database

        If user is None, the USER environment variable is used
        instead.
        """
        try:
            self.conn = connect("%s%sdbname=%s user=%s%s" % (
                    self.host, self.port, self.dbname,
                    self.user or os.getenv("USER"), self.pswd),
                                connection_factory=DictConnection)
        except Exception as exc:
            if exc.message[:6] == 'FATAL:':
                sys.exit("Database connection error: %s" % exc.message[8:])
            else:
                raise
        try:
            self._execute("set search_path to public, pg_catalog")
        except:
            self.conn.rollback()
            self._execute("set search_path to pg_catalog")<commit_msg>Fix dbconn.py exception handling under Python 3. Fixes #25.<commit_after># -*- coding: utf-8 -*-
"""
    pyrseas.dbconn
    ~~~~~~~~~~~~~~

    A `DbConnection` is a helper class representing a connection to a
    PostgreSQL database.
"""
import os
import sys
import getpass

from psycopg2 import connect
from psycopg2.extras import DictConnection


class DbConnection(object):
    """A database connection, possibly disconnected"""

    def __init__(self, dbname, user=None, pswd=False, host=None, port=None):
        """Initialize the connection information

        :param dbname: database name
        :param user: user name
        :param pswd: prompt user for password
        :param host: host name
        :param port: host port number
        """
        self.dbname = dbname
        self.user = user
        if pswd:
            self.pswd = " <PASSWORD>" % getpass.getpass()
        else:
            self.pswd = ''
        if host is None or host == '127.0.0.1' or host == 'localhost':
            self.host = ''
        else:
            self.host = "host=%s " % host
        if port is None or port == 5432:
            self.port = ''
        else:
            self.port = "port=%d " % port
        self.conn = None
        self._version = 0

    def connect(self):
        """Connect to the database

        If user is None, the USER environment variable is used
        instead.
        """
        try:
            self.conn = connect("%s%sdbname=%s user=%s%s" % (
                    self.host, self.port, self.dbname,
                    self.user or os.getenv("USER"), self.pswd),
                                connection_factory=DictConnection)
        except Exception as exc:
            if str(exc)[:6] == 'FATAL:':
                sys.exit("Database connection error: %s" % str(exc)[8:])
            else:
                raise
        try:
            self._execute("set search_path to public, pg_catalog")
        except:
            self.conn.rollback()
            self._execute("set search_path to pg_catalog")
{'fraction_non_alphanumeric': 0.1115527950310559, 'fraction_numerical': 0.008695652173913044, 'mean_word_length': 1.7708189951823812, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5065683', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1108, 'n_words': 344}