Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #179 : alias for upsert calls #180

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions astrapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,30 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import importlib.metadata
import toml
import os

from typing import Any


def get_version() -> Any:
# Get the path to the pyproject.toml file
dir_path = os.path.dirname(os.path.realpath(__file__))
pyproject_path = os.path.join(dir_path, "..", "pyproject.toml")

# Read the pyproject.toml file and get the version from the poetry section
try:
# Poetry will create a __version__ attribute in the package's __init__.py file
return importlib.metadata.version(__package__)

# If the package is not installed, we can still get the version from the pyproject.toml file
except importlib.metadata.PackageNotFoundError:
# Get the path to the pyproject.toml file
dir_path = os.path.dirname(os.path.realpath(__file__))
pyproject_path = os.path.join(dir_path, "..", "pyproject.toml")

# Read the pyproject.toml file and get the version from the poetry section
try:
with open(pyproject_path, encoding="utf-8") as pyproject:
# Load the pyproject.toml file as a dictionary
file_contents = pyproject.read()
pyproject_data = toml.loads(file_contents)

# Return the version from the poetry section
return pyproject_data["tool"]["poetry"]["version"]

# If the pyproject.toml file does not exist or the version is not found, return unknown
except (FileNotFoundError, KeyError):
return "unknown"
with open(pyproject_path, encoding="utf-8") as pyproject:
# Load the pyproject.toml file as a dictionary
file_contents = pyproject.read()
pyproject_data = toml.loads(file_contents)

# Return the version from the poetry section
return pyproject_data["tool"]["poetry"]["version"]

# If the pyproject.toml file does not exist or the version is not found, return unknown
except (FileNotFoundError, KeyError):
return "unknown"


__version__ = get_version()
64 changes: 40 additions & 24 deletions astrapy/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
from __future__ import annotations

import asyncio
import deprecation
import httpx
import logging
import json
import threading
from warnings import warn


from concurrent.futures import ThreadPoolExecutor
from functools import partial
Expand All @@ -39,6 +38,7 @@
AsyncGenerator,
)

from astrapy import __version__
from astrapy.api import AsyncAPIRequestHandler, APIRequestHandler
from astrapy.defaults import (
DEFAULT_AUTH_HEADER,
Expand Down Expand Up @@ -798,12 +798,13 @@ def replace(self, path: str, document: API_DOC) -> API_RESPONSE:
"""
return self._put(path=path, document=document)

@deprecation.deprecated( # type: ignore
deprecated_in="0.7.0",
removed_in="1.0.0",
current_version=__version__,
details="Use the 'delete_one' method instead",
)
def delete(self, id: str) -> API_RESPONSE:
DEPRECATION_MESSAGE = (
"Method 'delete' of AstraDBCollection is deprecated. Please "
"switch to method 'delete_one'."
)
warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)
return self.delete_one(id)

def delete_one(self, id: str) -> API_RESPONSE:
Expand Down Expand Up @@ -883,7 +884,16 @@ def delete_subdocument(self, id: str, subdoc: str) -> API_RESPONSE:

return response

@deprecation.deprecated( # type: ignore
deprecated_in="0.7.0",
removed_in="1.0.0",
current_version=__version__,
details="Use the 'upsert_one' method instead",
)
def upsert(self, document: API_DOC) -> str:
return self.upsert_one(document)

def upsert_one(self, document: API_DOC) -> str:
"""
Emulate an upsert operation for a single document in the collection.

Expand Down Expand Up @@ -948,7 +958,7 @@ def upsert_many(
if concurrency == 1:
for document in documents:
try:
results.append(self.upsert(document))
results.append(self.upsert_one(document))
except Exception as e:
results.append(e)
return results
Expand Down Expand Up @@ -1752,7 +1762,16 @@ async def delete_subdocument(self, id: str, subdoc: str) -> API_RESPONSE:

return response

@deprecation.deprecated( # type: ignore
deprecated_in="0.7.0",
removed_in="1.0.0",
current_version=__version__,
details="Use the 'upsert_one' method instead",
)
async def upsert(self, document: API_DOC) -> str:
return await self.upsert_one(document)

async def upsert_one(self, document: API_DOC) -> str:
"""
Emulate an upsert operation for a single document in the collection.

Expand Down Expand Up @@ -1813,7 +1832,7 @@ async def upsert_many(

async def concurrent_upsert(doc: API_DOC) -> str:
async with sem:
return await self.upsert(document=doc)
return await self.upsert_one(document=doc)

tasks = [asyncio.create_task(concurrent_upsert(doc)) for doc in documents]
results = await asyncio.gather(
Expand Down Expand Up @@ -2021,6 +2040,12 @@ def delete_collection(self, collection_name: str) -> API_RESPONSE:

return response

@deprecation.deprecated( # type: ignore
deprecated_in="0.7.0",
removed_in="1.0.0",
current_version=__version__,
details="Use the 'AstraDBCollection.clear()' method instead",
)
def truncate_collection(self, collection_name: str) -> AstraDBCollection:
"""
Clear a collection in the database, deleting all stored documents.
Expand All @@ -2029,14 +2054,6 @@ def truncate_collection(self, collection_name: str) -> AstraDBCollection:
Returns:
collection: an AstraDBCollection instance
"""
DEPRECATION_MESSAGE = (
"Method 'truncate_collection' of AstraDB is deprecated. Please "
"switch to method 'clear' of the AstraDBCollection object, e.g. "
"'astra_db.collection(\"my_collection\").clear()'."
" Note the returned object is different."
)
warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)

collection = AstraDBCollection(
collection_name=collection_name,
astra_db=self,
Expand Down Expand Up @@ -2259,6 +2276,12 @@ async def delete_collection(self, collection_name: str) -> API_RESPONSE:

return response

@deprecation.deprecated( # type: ignore
deprecated_in="0.7.0",
removed_in="1.0.0",
current_version=__version__,
details="Use the 'AsyncAstraDBCollection.clear()' method instead",
)
async def truncate_collection(self, collection_name: str) -> AsyncAstraDBCollection:
"""
Clear a collection in the database, deleting all stored documents.
Expand All @@ -2267,13 +2290,6 @@ async def truncate_collection(self, collection_name: str) -> AsyncAstraDBCollect
Returns:
collection: an AsyncAstraDBCollection instance
"""
DEPRECATION_MESSAGE = (
"Method 'truncate_collection' of AsyncAstraDB is deprecated. Please "
"switch to method 'clear' of the AsyncAstraDBCollection object, e.g. "
"'async_astra_db.collection(\"my_collection\").clear()'"
" Note the returned object is different."
)
warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)

collection = AsyncAstraDBCollection(
collection_name=collection_name,
Expand Down
Loading
Loading