<commit_before>"""
Support for example tables wrapping data stored on a PostgreSQL server.
"""
import functools
import re
import threading
from contextlib import contextmanager
from urllib import parse

import numpy as np
import sys

import Orange.misc
psycopg2 = Orange.misc.import_late_warning("psycopg2")
psycopg2.pool = Orange.misc.import_late_warning("psycopg2.pool")

from .. import domain, variable, value, table, instance, filter,\
    DiscreteVariable, ContinuousVariable, StringVariable
from Orange.data.sql import filter as sql_filter
from Orange.data.sql.filter import CustomFilterSql
from Orange.data.sql.parser import SqlParser

LARGE_TABLE = 100000
DEFAULT_SAMPLE_TIME = 1

class SqlTable(table.Table):
    connection_pool = None
    table_name = None
    domain = None
    row_filters = ()

    def __new__(cls, *args, **kwargs):
        # We do not (yet) need the magic of the Table.__new__, so we call it
        # with no parameters.
        return super().__new__(cls)

    def __init__(
            self, uri=None,
            host=None, database=None, user=None, password=<PASSWORD>, schema=None,
            table=None, type_hints=None, guess_values=False, **kwargs):
        """
        Create a new proxy for sql table.

        Database connection parameters can be specified either as a string:

            table = SqlTable("user:password@host:port/database/table")

        or using a set of keyword arguments:

            table = SqlTable(database="test", table="iris")

        All but the database and table parameters are optional. Any additional
        parameters will be forwarded to the psycopg2 backend.

        If type_hints (an Orange domain) contain a column name, then
        the variable type from type_hints will be used. If it does not,
        the variable type is selected based on the column type (double
        -> ContinuousVariable, everything else -> StringVariable).
        If guess_values is True, database columns with less that 20
        different strings will become DiscreteVariables.

        Class vars and metas can be specified as a list of column names in
        __class_vars__ and __metas__ keys in type_hints dict.
        """
        assert uri is not None or database is not None

        connection_args = dict(
            host=host,
            user=user,
            password=password,
            database=database,
            schema=schema
        )
        if uri is not None:
            parameters = self.parse_uri(uri)
            table = parameters.pop("table", None)
            connection_args.update(parameters)
        connection_args.update(kwargs)

        if self.connection_pool is None:
            self.connection_pool = psycopg2.pool.ThreadedConnectionPool(
                1, 16, **connection_args)<commit_msg>Use parameter value when table is not specified in uri.<commit_after>"""
Support for example tables wrapping data stored on a PostgreSQL server.
"""
import functools
import re
import threading
from contextlib import contextmanager
from urllib import parse

import numpy as np
import sys

import Orange.misc
psycopg2 = Orange.misc.import_late_warning("psycopg2")
psycopg2.pool = Orange.misc.import_late_warning("psycopg2.pool")

from .. import domain, variable, value, table, instance, filter,\
    DiscreteVariable, ContinuousVariable, StringVariable
from Orange.data.sql import filter as sql_filter
from Orange.data.sql.filter import CustomFilterSql
from Orange.data.sql.parser import SqlParser

LARGE_TABLE = 100000
DEFAULT_SAMPLE_TIME = 1

class SqlTable(table.Table):
    connection_pool = None
    table_name = None
    domain = None
    row_filters = ()

    def __new__(cls, *args, **kwargs):
        # We do not (yet) need the magic of the Table.__new__, so we call it
        # with no parameters.
        return super().__new__(cls)

    def __init__(
            self, uri=None,
            host=None, database=None, user=None, password=<PASSWORD>, schema=None,
            table=None, type_hints=None, guess_values=False, **kwargs):
        """
        Create a new proxy for sql table.

        Database connection parameters can be specified either as a string:

            table = SqlTable("user:password@host:port/database/table")

        or using a set of keyword arguments:

            table = SqlTable(database="test", table="iris")

        All but the database and table parameters are optional. Any additional
        parameters will be forwarded to the psycopg2 backend.

        If type_hints (an Orange domain) contain a column name, then
        the variable type from type_hints will be used. If it does not,
        the variable type is selected based on the column type (double
        -> ContinuousVariable, everything else -> StringVariable).
        If guess_values is True, database columns with less that 20
        different strings will become DiscreteVariables.

        Class vars and metas can be specified as a list of column names in
        __class_vars__ and __metas__ keys in type_hints dict.
        """
        assert uri is not None or database is not None

        connection_args = dict(
            host=host,
            user=user,
            password=password,
            database=database,
            schema=schema
        )
        if uri is not None:
            parameters = self.parse_uri(uri)
            table = parameters.pop("table", table)
            connection_args.update(parameters)
        connection_args.update(kwargs)

        if self.connection_pool is None:
            self.connection_pool = psycopg2.pool.ThreadedConnectionPool(
                1, 16, **connection_args)
{'fraction_non_alphanumeric': 0.06841081845501149, 'fraction_numerical': 0.006363797065582464, 'mean_word_length': 2.715036112934997, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '2097679', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1446, 'n_words': 550}