<commit_before>import codecs
import hashlib
from sqlalchemy import create_engine
from sqlalchemy.exc import IntegrityError, DataError
from sqlalchemy.orm import sessionmaker

from pbnh.db import models
from pbnh.db.connect import DBConnect

class Paster():
    def __init__(self, dialect='sqlite', driver=None, username=None, password=<PASSWORD>,
                 host=None, port=None, dbname='pastedb'):
        """Grab connection information to pass to DBConnect"""
        self.dialect = dialect
        self.dbname = dbname
        self.driver = driver
        self.username = username
        self.password = password
        self.host = host
        self.port = port

    def __enter__(self):
        connection = DBConnect(dialect=self.dialect, driver=self.driver, username=self.username,
                               password=<PASSWORD>, host=self.host, port=self.port,
                               dbname=self.dbname).connect
        self.engine = create_engine(connection, pool_size=1)
        Session = sessionmaker(bind=self.engine)
        self.session = Session()
        return self

    def __exit__(self, exception_type, exception_value, traceback):
        self.session.close()
        self.engine.dispose()

    def create(self, data, ip=None, mime=None, sunset=None,
               timestamp=None):
        sha1 = hashlib.sha1(data).hexdigest()
        collision = self.query(hashid=sha1)
        if collision:
            pasteid = collision.get('id')
        else:
            paste = models.Paste(
                    hashid = sha1,
                    ip = ip,<commit_msg>fix for #27<commit_after>import codecs
import hashlib
from sqlalchemy import create_engine
from sqlalchemy.exc import IntegrityError, DataError
from sqlalchemy.orm import sessionmaker

from pbnh.db import models
from pbnh.db.connect import DBConnect

class Paster():
    def __init__(self, dialect='sqlite', driver=None, username=None, password=<PASSWORD>,
                 host=None, port=None, dbname='pastedb'):
        """Grab connection information to pass to DBConnect"""
        self.dialect = dialect
        self.dbname = dbname
        self.driver = driver
        self.username = username
        self.password = password
        self.host = host
        self.port = port

    def __enter__(self):
        connection = DBConnect(dialect=self.dialect, driver=self.driver, username=self.username,
                               password=self.password, host=self.host, port=self.port,
                               dbname=self.dbname).connect
        if self.dialect == 'postgresql':
            self.engine = create_engine(connection, pool_size=1)
        else:
            self.engine = create_engine(connection, pool_size=1)
        Session = sessionmaker(bind=self.engine)
        self.session = Session()
        return self

    def __exit__(self, exception_type, exception_value, traceback):
        self.session.close()
        self.engine.dispose()

    def create(self, data, ip=None, mime=None, sunset=None,
               timestamp=None):
        sha1 = hashlib.sha1(data).hexdigest()
        collision = self.query(hashid=sha1)
        if collision:
            pasteid = collision.get('id')
        else:
            paste = models.Paste(
                    hashid = sha1,
                    ip = ip,
{'fraction_non_alphanumeric': 0.09473684210526316, 'fraction_numerical': 0.003909774436090226, 'mean_word_length': 2.2833168805528135, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4776577', 'n_tokens_mistral': 933, 'n_tokens_neox': 906, 'n_words': 218}