Skip to content

Commit

Permalink
db.col_name and db['col_name'] aliases for get_collection
Browse files Browse the repository at this point in the history
  • Loading branch information
hemidactylus committed Feb 29, 2024
1 parent 17e7c8a commit 97ad75a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
24 changes: 18 additions & 6 deletions astrapy/idiomatic/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ def __init__(
caller_version=caller_version,
)

@property
def namespace(self) -> str:
return self._astra_db.namespace
def __getattr__(self, collection_name: str) -> Collection:
return self.get_collection(name=collection_name)

def __getitem__(self, collection_name: str) -> Collection:
return self.get_collection(name=collection_name)

def __repr__(self) -> str:
return f'{self.__class__.__name__}[_astra_db={self._astra_db}"]'
Expand All @@ -126,6 +128,10 @@ def __eq__(self, other: Any) -> bool:
else:
return False

@property
def namespace(self) -> str:
return self._astra_db.namespace

def copy(self) -> Database:
return Database(
**self._constructor_params,
Expand Down Expand Up @@ -324,9 +330,11 @@ def __init__(
caller_version=caller_version,
)

@property
def namespace(self) -> str:
return self._astra_db.namespace
async def __getattr__(self, collection_name: str) -> AsyncCollection:
return await self.get_collection(name=collection_name)

async def __getitem__(self, collection_name: str) -> AsyncCollection:
return await self.get_collection(name=collection_name)

def __repr__(self) -> str:
return f'{self.__class__.__name__}[_astra_db={self._astra_db}"]'
Expand All @@ -352,6 +360,10 @@ async def __aexit__(
traceback=traceback,
)

@property
def namespace(self) -> str:
return self._astra_db.namespace

def copy(self) -> AsyncDatabase:
return AsyncDatabase(
**self._constructor_params,
Expand Down
5 changes: 5 additions & 0 deletions tests/idiomatic/unit/test_databases_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ async def test_database_get_collection_async(
collection = await async_database.get_collection(TEST_COLLECTION_INSTANCE_NAME)
assert collection == async_collection_instance

assert (
await getattr(async_database, TEST_COLLECTION_INSTANCE_NAME) == collection
)
assert await async_database[TEST_COLLECTION_INSTANCE_NAME] == collection

NAMESPACE_2 = "other_namespace"
collection_ns2 = await async_database.get_collection(
TEST_COLLECTION_INSTANCE_NAME, namespace=NAMESPACE_2
Expand Down
3 changes: 3 additions & 0 deletions tests/idiomatic/unit/test_databases_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def test_database_get_collection_sync(
collection = sync_database.get_collection(TEST_COLLECTION_INSTANCE_NAME)
assert collection == sync_collection_instance

assert getattr(sync_database, TEST_COLLECTION_INSTANCE_NAME) == collection
assert sync_database[TEST_COLLECTION_INSTANCE_NAME] == collection

NAMESPACE_2 = "other_namespace"
collection_ns2 = sync_database.get_collection(
TEST_COLLECTION_INSTANCE_NAME, namespace=NAMESPACE_2
Expand Down

0 comments on commit 97ad75a

Please sign in to comment.