Move code to _src for better of externally-facing names

This commit is contained in:
Yonatan Most 2020-07-21 13:10:38 +03:00
Родитель a6f51bc937
Коммит b5532d5c20
21 изменённых файлов: 74 добавлений и 68 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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"

0
pykusto/_src/__init__.py Normal file
Просмотреть файл

Просмотреть файл

@ -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:

Просмотреть файл

Просмотреть файл

@ -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']

Просмотреть файл

@ -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

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -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)

Просмотреть файл

Просмотреть файл

@ -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:

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -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")

Просмотреть файл

@ -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())

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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()

Просмотреть файл

@ -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

Просмотреть файл

@ -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