diff --git a/README.md b/README.md index ce990ce..8891847 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,7 @@ pip install pykusto ### Usage ```python from datetime import timedelta -from pykusto.client import PyKustoClient -from pykusto.query import Query +from pykusto import PyKustoClient, Query # Connect to cluster with AAD device authentication # Databases, tables, and columns are auto-retrieved diff --git a/pykusto/__init__.py b/pykusto/__init__.py index 06b23dc..a9a7224 100644 --- a/pykusto/__init__.py +++ b/pykusto/__init__.py @@ -3,11 +3,11 @@ # Also allows for a convenient list of all externally-facing classes as the autocomplete of "import pykusto." # "import *" does not import names which start with an underscore -from pykusto.client import * -from pykusto.enums import * -from pykusto.expressions import * -from pykusto.functions import * -from pykusto.query import * +from ._src.client import * +from ._src.enums import * +from ._src.expressions import * +from ._src.functions import * +from ._src.query import * name = "pykusto" diff --git a/pykusto/_src/__init__.py b/pykusto/_src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pykusto/client.py b/pykusto/_src/client.py similarity index 98% rename from pykusto/client.py rename to pykusto/_src/client.py index 35cdcd3..c57d92c 100644 --- a/pykusto/client.py +++ b/pykusto/_src/client.py @@ -14,11 +14,11 @@ from azure.kusto.data.response import KustoResponseDataSet # noinspection PyProtectedMember from azure.kusto.data.security import _get_azure_cli_auth_token -from pykusto.expressions import BaseColumn, _AnyTypeColumn -from pykusto.item_fetcher import _ItemFetcher -from pykusto.kql_converters import KQL -from pykusto.logger import _logger -from pykusto.type_utils import _INTERNAL_NAME_TO_TYPE, _typed_column, _DOT_NAME_TO_TYPE +from .expressions import BaseColumn, _AnyTypeColumn +from .item_fetcher import _ItemFetcher +from .kql_converters import KQL +from .logger import _logger +from .type_utils import _INTERNAL_NAME_TO_TYPE, _typed_column, _DOT_NAME_TO_TYPE class KustoResponse: diff --git a/pykusto/enums.py b/pykusto/_src/enums.py similarity index 100% rename from pykusto/enums.py rename to pykusto/_src/enums.py diff --git a/pykusto/expressions.py b/pykusto/_src/expressions.py similarity index 99% rename from pykusto/expressions.py rename to pykusto/_src/expressions.py index 2b8937a..d0ee27b 100644 --- a/pykusto/expressions.py +++ b/pykusto/_src/expressions.py @@ -2,9 +2,9 @@ from datetime import datetime, timedelta from typing import Any, List, Tuple, Mapping, Optional from typing import Union -from pykusto.keywords import _KUSTO_KEYWORDS -from pykusto.kql_converters import KQL -from pykusto.type_utils import _plain_expression, _aggregation_expression, PythonTypes, _kql_converter, _KustoType, _typed_column, _TypeRegistrar, _get_base_types, _NUMBER_TYPES +from .keywords import _KUSTO_KEYWORDS +from .kql_converters import KQL +from .type_utils import _plain_expression, _aggregation_expression, PythonTypes, _kql_converter, _KustoType, _typed_column, _TypeRegistrar, _get_base_types, _NUMBER_TYPES _ExpressionType = Union[PythonTypes, 'BaseExpression'] _StringType = Union[str, '_StringExpression'] diff --git a/pykusto/functions.py b/pykusto/_src/functions.py similarity index 99% rename from pykusto/functions.py rename to pykusto/_src/functions.py index b4c322c..0a75955 100644 --- a/pykusto/functions.py +++ b/pykusto/_src/functions.py @@ -1,21 +1,21 @@ from itertools import chain from typing import Union -from pykusto.enums import Kind -from pykusto.expressions import _AnyTypeColumn, _NumberType, _NumberExpression, _TimespanType, \ +from .enums import Kind +from .expressions import _AnyTypeColumn, _NumberType, _NumberExpression, _TimespanType, \ _DatetimeExpression, _TimespanExpression, _ArrayType, _DynamicType, _DatetimeType, BaseExpression, _BooleanType, \ _ExpressionType, _StringType, _StringExpression, _BooleanExpression, \ _NumberAggregationExpression, _MappingAggregationExpression, _ArrayAggregationExpression, _to_kql, _DynamicExpression, \ _ArrayExpression, _ColumnToType, BaseColumn, AnyExpression, _AnyAggregationExpression, _MappingExpression -from pykusto.kql_converters import KQL -from pykusto.logger import _logger -from pykusto.type_utils import _plain_expression, _KustoType +from .kql_converters import KQL +from .logger import _logger +from .type_utils import _plain_expression, _KustoType class Functions: """ Recommended import style:\n - `from pykusto.functions import Functions as f` + `from pykusto import Functions as f` """ # Scalar functions diff --git a/pykusto/item_fetcher.py b/pykusto/_src/item_fetcher.py similarity index 100% rename from pykusto/item_fetcher.py rename to pykusto/_src/item_fetcher.py diff --git a/pykusto/keywords.py b/pykusto/_src/keywords.py similarity index 100% rename from pykusto/keywords.py rename to pykusto/_src/keywords.py diff --git a/pykusto/kql_converters.py b/pykusto/_src/kql_converters.py similarity index 94% rename from pykusto/kql_converters.py rename to pykusto/_src/kql_converters.py index aff081b..0b8d954 100644 --- a/pykusto/kql_converters.py +++ b/pykusto/_src/kql_converters.py @@ -4,7 +4,7 @@ from itertools import chain from numbers import Number from typing import NewType, Union, Mapping, List, Tuple -from pykusto.type_utils import _kql_converter, _KustoType, _NUMBER_TYPES +from .type_utils import _kql_converter, _KustoType, _NUMBER_TYPES KQL = NewType('KQL', str) @@ -38,7 +38,7 @@ def _build_dynamic(d: Union[Mapping, List, Tuple]) -> KQL: return KQL(f"pack({', '.join(map(_build_dynamic, chain(*d.items())))})") if isinstance(d, (List, Tuple)): return KQL(f"pack_array({', '.join(map(_build_dynamic, d))})") - from pykusto.expressions import _to_kql + from .expressions import _to_kql return _to_kql(d) diff --git a/pykusto/logger.py b/pykusto/_src/logger.py similarity index 100% rename from pykusto/logger.py rename to pykusto/_src/logger.py diff --git a/pykusto/query.py b/pykusto/_src/query.py similarity index 98% rename from pykusto/query.py rename to pykusto/_src/query.py index 9bd4219..86ea4b0 100644 --- a/pykusto/query.py +++ b/pykusto/_src/query.py @@ -5,17 +5,17 @@ from os import linesep from types import FunctionType from typing import Tuple, List, Union, Optional -from pykusto.client import _Table, KustoResponse -from pykusto.enums import Order, Nulls, JoinKind, Distribution, BagExpansion -from pykusto.expressions import _BooleanType, _ExpressionType, AggregationExpression, _OrderedType, \ +from .client import _Table, KustoResponse +from .enums import Order, Nulls, JoinKind, Distribution, BagExpansion +from .expressions import _BooleanType, _ExpressionType, AggregationExpression, _OrderedType, \ _StringType, _AssignmentBase, _AssignmentFromAggregationToColumn, _AssignmentToSingleColumn, _AnyTypeColumn, \ BaseExpression, \ _AssignmentFromColumnToColumn, AnyExpression, _to_kql, _expression_to_type, BaseColumn, _NumberType -from pykusto.functions import Functions as f -from pykusto.kql_converters import KQL -from pykusto.logger import _logger -from pykusto.type_utils import _KustoType, _typed_column, _plain_expression -from pykusto.udf import _stringify_python_func +from .functions import Functions as f +from .kql_converters import KQL +from .logger import _logger +from .type_utils import _KustoType, _typed_column, _plain_expression +from .udf import _stringify_python_func class Query: diff --git a/pykusto/type_utils.py b/pykusto/_src/type_utils.py similarity index 100% rename from pykusto/type_utils.py rename to pykusto/_src/type_utils.py diff --git a/pykusto/udf.py b/pykusto/_src/udf.py similarity index 100% rename from pykusto/udf.py rename to pykusto/_src/udf.py diff --git a/test/test_base.py b/test/test_base.py index 8a490c1..940a022 100644 --- a/test/test_base.py +++ b/test/test_base.py @@ -12,10 +12,12 @@ from azure.kusto.data import KustoClient, ClientRequestProperties from azure.kusto.data._models import KustoResultTable, KustoResultRow from azure.kusto.data.response import KustoResponseDataSet -from pykusto.client import _Table -from pykusto.expressions import _NumberColumn, _BooleanColumn, _ArrayColumn, _MappingColumn, _StringColumn, _DatetimeColumn, _TimespanColumn, _DynamicColumn -from pykusto.logger import _logger -from pykusto.type_utils import _KustoType +# noinspection PyProtectedMember +from pykusto._src.client import _Table +# noinspection PyProtectedMember +from pykusto._src.expressions import _NumberColumn, _BooleanColumn, _ArrayColumn, _MappingColumn, _StringColumn, _DatetimeColumn, _TimespanColumn, _DynamicColumn +# noinspection PyProtectedMember +from pykusto._src.type_utils import _KustoType # Naming this variable "test_table" triggers the following bug: https://github.com/pytest-dev/pytest/issues/7378 # noinspection PyTypeChecker @@ -40,7 +42,7 @@ class TestBase(TestCase): ) def setUp(self) -> None: - _logger.info("Running test: " + self._testMethodName) + test_logger.info("Running test: " + self._testMethodName) def assertRaises(self, expected_exception: BaseException, test_callable: Callable, *args, **kwargs): """ @@ -191,3 +193,6 @@ class MockKustoClient(KustoClient): if self.record_metadata or not metadata_query: self.recorded_queries.append(recorded_query) return response + + +test_logger = logging.getLogger("pykusto_test") diff --git a/test/test_client.py b/test/test_client.py index cb0cdfd..e4a2f8b 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -3,10 +3,9 @@ from unittest.mock import patch from azure.kusto.data import KustoClient -from pykusto.client import PyKustoClient -from pykusto.expressions import column_generator as col -from pykusto.logger import _logger -from pykusto.query import Query +from pykusto import PyKustoClient, column_generator as col, Query +# noinspection PyProtectedMember +from pykusto._src.logger import _logger from test.test_base import TestBase, MockKustoClient, RecordedQuery @@ -74,7 +73,7 @@ class TestClient(TestBase): def test_default_authentication(self): mock_kusto_client = MockKustoClient() - with patch('pykusto.client.PyKustoClient._get_client_for_cluster', lambda s, cluster: mock_kusto_client): + with patch('pykusto._src.client.PyKustoClient._get_client_for_cluster', lambda s, cluster: mock_kusto_client): table = PyKustoClient('https://help.kusto.windows.net/')['test_db']['mock_table'] Query().take(5).execute(table) self.assertIs( @@ -87,7 +86,7 @@ class TestClient(TestBase): ) def test_client_instances(self): - with patch('pykusto.client.PyKustoClient._get_client_for_cluster', MockKustoClient): + with patch('pykusto._src.client.PyKustoClient._get_client_for_cluster', MockKustoClient): client_1 = PyKustoClient('https://help.kusto.windows.net/') client_2 = PyKustoClient('https://help.kusto.windows.net/') @@ -97,7 +96,7 @@ class TestClient(TestBase): ) def test_client_instances_cached(self): - with patch('pykusto.client.PyKustoClient._get_client_for_cluster', MockKustoClient): + with patch('pykusto._src.client.PyKustoClient._get_client_for_cluster', MockKustoClient): client_1 = PyKustoClient('https://help.kusto.windows.net/', use_global_cache=True) client_2 = PyKustoClient('https://help.kusto.windows.net/', use_global_cache=True) @@ -107,7 +106,7 @@ class TestClient(TestBase): ) def test_client_instances_cached_distinct(self): - with patch('pykusto.client.PyKustoClient._get_client_for_cluster', MockKustoClient): + with patch('pykusto._src.client.PyKustoClient._get_client_for_cluster', MockKustoClient): client_1 = PyKustoClient('https://help1.kusto.windows.net/', use_global_cache=True) client_2 = PyKustoClient('https://help2.kusto.windows.net/', use_global_cache=True) @@ -154,14 +153,14 @@ class TestClient(TestBase): ) def test_client_for_cluster_with_azure_cli_auth(self): - with patch('pykusto.client._get_azure_cli_auth_token', lambda: "MOCK_TOKEN"), self.assertLogs(_logger, logging.INFO) as cm: + with patch('pykusto._src.client._get_azure_cli_auth_token', lambda: "MOCK_TOKEN"), self.assertLogs(_logger, logging.INFO) as cm: client = PyKustoClient('https://help.kusto.windows.net', fetch_by_default=False) self.assertIsInstance(client._PyKustoClient__client, KustoClient) self.assertEqual('https://help.kusto.windows.net', client.get_cluster_name()) self.assertEqual([], cm.output) def test_client_for_cluster_fallback_to_aad_device_auth(self): - with patch('pykusto.client._get_azure_cli_auth_token', lambda: None), self.assertLogs(_logger, logging.INFO) as cm: + with patch('pykusto._src.client._get_azure_cli_auth_token', lambda: None), self.assertLogs(_logger, logging.INFO) as cm: client = PyKustoClient('https://help.kusto.windows.net', fetch_by_default=False) self.assertIsInstance(client._PyKustoClient__client, KustoClient) self.assertEqual('https://help.kusto.windows.net', client.get_cluster_name()) diff --git a/test/test_client_fetch.py b/test/test_client_fetch.py index 90ae0e1..0a1b459 100644 --- a/test/test_client_fetch.py +++ b/test/test_client_fetch.py @@ -1,10 +1,13 @@ from concurrent.futures import Future from threading import Thread, Lock -from pykusto.client import PyKustoClient, _Database -from pykusto.expressions import _StringColumn, _NumberColumn, _AnyTypeColumn, _BooleanColumn -from pykusto.query import Query -from pykusto.type_utils import _KustoType +from pykusto import PyKustoClient, Query +# noinspection PyProtectedMember +from pykusto._src.client import _Database +# noinspection PyProtectedMember +from pykusto._src.expressions import _StringColumn, _NumberColumn, _AnyTypeColumn, _BooleanColumn +# noinspection PyProtectedMember +from pykusto._src.type_utils import _KustoType from test.test_base import TestBase, MockKustoClient, mock_columns_response, RecordedQuery, mock_tables_response, mock_getschema_response, mock_databases_response diff --git a/test/test_expressions.py b/test/test_expressions.py index dacdbee..879bff0 100644 --- a/test/test_expressions.py +++ b/test/test_expressions.py @@ -1,8 +1,9 @@ from datetime import timedelta, datetime -from pykusto.expressions import column_generator as col, _AnyTypeColumn -from pykusto.functions import Functions as f -from pykusto.query import Query +from pykusto import Functions as f +from pykusto import column_generator as col, Query +# noinspection PyProtectedMember +from pykusto._src.expressions import _AnyTypeColumn from test.test_base import TestBase, mock_table as t diff --git a/test/test_functions.py b/test/test_functions.py index cd70eca..8e0ee80 100644 --- a/test/test_functions.py +++ b/test/test_functions.py @@ -1,11 +1,10 @@ import logging from datetime import datetime, timedelta -from pykusto.expressions import column_generator as col -from pykusto.functions import Functions as f -from pykusto.logger import _logger -from pykusto.query import Query -from test.test_base import TestBase +from pykusto import column_generator as col, Functions as f, Query +# noinspection PyProtectedMember +from pykusto._src.logger import _logger +from test.test_base import TestBase, test_logger from test.test_base import mock_table as t @@ -814,7 +813,7 @@ class TestFunction(TestBase): ) def test_iff_ambiguous_type(self): - with self.assertLogs(_logger, logging.WARN) as cm: + with self.assertLogs(test_logger, logging.WARN) as cm: self.assertEqual( " | project foo = iff(boolField, time(3.0:0:0.0), foo - bar)", Query().project(foo=f.iff(t.boolField, timedelta(3), col.foo - col.bar)).render() diff --git a/test/test_query.py b/test/test_query.py index ab2e530..1dfe5fd 100644 --- a/test/test_query.py +++ b/test/test_query.py @@ -2,12 +2,9 @@ from os import linesep import pandas as pd -from pykusto.client import PyKustoClient -from pykusto.enums import Order, Nulls, JoinKind, Distribution, BagExpansion -from pykusto.expressions import column_generator as col -from pykusto.functions import Functions as f -from pykusto.query import Query, JoinException -from pykusto.type_utils import _KustoType +from pykusto import PyKustoClient, Order, Nulls, JoinKind, Distribution, BagExpansion, column_generator as col, Functions as f, Query, JoinException +# noinspection PyProtectedMember +from pykusto._src.type_utils import _KustoType from test.test_base import TestBase, mock_databases_response, MockKustoClient, mock_response from test.test_base import mock_table as t, mock_columns_response from test.udf import func, STRINGIFIED diff --git a/test/test_utils.py b/test/test_utils.py index 49b3614..89f5da7 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,6 +1,9 @@ -from pykusto.expressions import _to_kql -from pykusto.kql_converters import KQL -from pykusto.type_utils import _TypeRegistrar, _KustoType +# noinspection PyProtectedMember +from pykusto._src.expressions import _to_kql +# noinspection PyProtectedMember +from pykusto._src.kql_converters import KQL +# noinspection PyProtectedMember +from pykusto._src.type_utils import _TypeRegistrar, _KustoType from test.test_base import TestBase