<commit_before>import os
import datetime
import socket
import contextlib
import json

from piper import utils

from sqlalchemy import Boolean
from sqlalchemy import Column
from sqlalchemy import DateTime
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import create_engine
from sqlalchemy import update
from sqlalchemy.ext.declarative import DeclarativeMeta
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import relationship

from piper.db.core import DatabaseBase


Base = declarative_base()
Session = sessionmaker()


class SQLAlchemyManager(object):
    def __init__(self, db):
        self.db = db

    def get_or_create(self, session, model, expunge=False, keys=(), **kwargs):
        """
        Get or create an object.

        A filter is done on the model with `kwargs`. If `keys` are specified,
        only those keys will be used to do the filtering.

        If `expunge` is True, the created object is detached from the session.<commit_msg>Add logging to SQLAManager()<commit_after>import os
import datetime
import socket
import contextlib
import json
import logbook

from piper import utils

from sqlalchemy import Boolean
from sqlalchemy import Column
from sqlalchemy import DateTime
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import create_engine
from sqlalchemy import update
from sqlalchemy.ext.declarative import DeclarativeMeta
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import relationship

from piper.db.core import DatabaseBase


Base = declarative_base()
Session = sessionmaker()


class SQLAlchemyManager(object):
    def __init__(self, db):
        self.db = db
        self.log = logbook.Logger(self.__class__.__name__)

    def get_or_create(self, session, model, expunge=False, keys=(), **kwargs):
        """
        Get or create an object.

        A filter is done on the model with `kwargs`. If `keys` are specified,
        only those keys will be used to do the filtering.

        If `expunge` is True, the created object is detached from the session.
{'fraction_non_alphanumeric': 0.052585064074237735, 'fraction_numerical': 0.0, 'mean_word_length': 4.228637413394919, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4521400', 'n_tokens_mistral': 636, 'n_tokens_neox': 598, 'n_words': 254}