<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sqlalchemy
from sqlalchemy import __version__
from sqlalchemy import create_engine
# from core import config
from py_privatekonomi.utilities import common
from py_privatekonomi.utilities.common import (singleton, is_dict, is_Struct, as_obj)

@singleton
class DB(object):
    def connect(self, db_config):
        if is_dict(db_config):
            db_config = as_obj(db_config)
        else:
            if not is_Struct(db_config):
                raise Exception("db_config must be either dict or common.Struct: %s" % (repr(db_config)))
        self.__engine = sqlalchemy.create_engine("%(engine)s://%(username)s:%(password)s@%(host)s:%(port)s/%(database)s" % {
            'engine' : db_config.engine,
            'username' : db_config.username,
            'password' : <PASSWORD>,
            'host' : db_config.host,
            'port' : db_config.port,
            'database' : db_config.database
        })
        self.__connection = self.__engine.connect()
        self.__config = db_config
        self.__connected = True

    def getEngine(self):
        return self.__engine

    def getConnection(self):
        return self.__connection

    def getConfig(self, config = None):
        if config is not None:
            return getattr(self.__config, config)
        else:
            return self.__config

    def hasConfig(self, config_name):
        return hasattr(self.__config, config_name)

    def isConnected(self):
        try:
            is_connected = self.__connected
        except AttributeError:<commit_msg>force utf8 charset/encoding on engine<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sqlalchemy
from sqlalchemy import __version__
from sqlalchemy import create_engine
import sqlalchemy.engine.url as url
# from core import config
from py_privatekonomi.utilities import common
from py_privatekonomi.utilities.common import (singleton, is_dict, is_Struct, as_obj)

@singleton
class DB(object):
    def connect(self, db_config):
        if is_dict(db_config):
            db_config = as_obj(db_config)
        else:
            if not is_Struct(db_config):
                raise Exception("db_config must be either dict or common.Struct: %s" % (repr(db_config)))
        engine_url = url.URL(
            drivername=db_config.engine,
            host=db_config.host,
            port=db_config.port,
            username=db_config.username,
            password=<PASSWORD>,
            database=db_config.database,
            query={ 'charset' : 'utf8' }
        )
        self.__engine = create_engine(engine_url, encoding='utf-8')
        self.__connection = self.__engine.connect()
        self.__config = db_config
        self.__connected = True

    def getEngine(self):
        return self.__engine

    def getConnection(self):
        return self.__connection

    def getConfig(self, config = None):
        if config is not None:
            return getattr(self.__config, config)
        else:
            return self.__config

    def hasConfig(self, config_name):
        return hasattr(self.__config, config_name)

    def isConnected(self):
        try:
            is_connected = self.__connected
        except AttributeError:
{'fraction_non_alphanumeric': 0.09491733006736068, 'fraction_numerical': 0.0015309246785058174, 'mean_word_length': 2.5166846071044136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3715464', 'n_tokens_mistral': 950, 'n_tokens_neox': 924, 'n_words': 225}