Skip to content
Open
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
15 changes: 8 additions & 7 deletions samples/snippets/writes/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def blacken(session: nox.sessions.Session) -> None:
# format = isort + black
#


@nox.session
def format(session: nox.sessions.Session) -> None:
"""
Expand Down Expand Up @@ -187,7 +188,9 @@ def _session_tests(
session: nox.sessions.Session, post_install: Callable = None
) -> None:
# check for presence of tests
test_list = glob.glob("**/*_test.py", recursive=True) + glob.glob("**/test_*.py", recursive=True)
test_list = glob.glob("**/*_test.py", recursive=True) + glob.glob(
"**/test_*.py", recursive=True
)
test_list.extend(glob.glob("**/tests", recursive=True))

if len(test_list) == 0:
Expand All @@ -209,9 +212,7 @@ def _session_tests(

if os.path.exists("requirements-test.txt"):
if os.path.exists("constraints-test.txt"):
session.install(
"-r", "requirements-test.txt", "-c", "constraints-test.txt"
)
session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt")
else:
session.install("-r", "requirements-test.txt")
with open("requirements-test.txt") as rtfile:
Expand All @@ -224,9 +225,9 @@ def _session_tests(
post_install(session)

if "pytest-parallel" in packages:
concurrent_args.extend(['--workers', 'auto', '--tests-per-worker', 'auto'])
concurrent_args.extend(["--workers", "auto", "--tests-per-worker", "auto"])
elif "pytest-xdist" in packages:
concurrent_args.extend(['-n', 'auto'])
concurrent_args.extend(["-n", "auto"])

session.run(
"pytest",
Expand Down Expand Up @@ -256,7 +257,7 @@ def py(session: nox.sessions.Session) -> None:


def _get_repo_root() -> Optional[str]:
""" Returns the root folder of the project. """
"""Returns the root folder of the project."""
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
p = Path(os.getcwd())
for i in range(10):
Expand Down
33 changes: 33 additions & 0 deletions samples/snippets/writes/write_increment_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# [START bigtable_write_increment_async]
from google.cloud.bigtable.data import BigtableDataClientAsync
from google.cloud.bigtable.data.mutations import AddToCell, RowMutationEntry
from google.cloud.bigtable.data.exceptions import MutationsExceptionGroup


async def write_increment_async(project_id, instance_id, table_id):
"""Increments a value in a Bigtable table using the async client."""
async with BigtableDataClientAsync(project=project_id) as client:
table = client.get_table(instance_id, table_id)
row_key = "unique_device_ids_1"
try:
async with table.mutations_batcher() as batcher:
# The AddToCell mutation increments the value of a cell.
# The value must be a positive or negative integer.
reading = AddToCell(
family="counters", qualifier="odometer", value=32304
)
await batcher.append(
RowMutationEntry(row_key.encode("utf-8"), [reading])
)
except MutationsExceptionGroup as e:
# MutationsExceptionGroup contains a FailedMutationEntryError for
# each mutation that failed.
for sub_exception in e.exceptions:
failed_entry: RowMutationEntry = sub_exception.entry
cause: Exception = sub_exception.__cause__
print(
f"Failed mutation for row {failed_entry.row_key!r} with error: {cause!r}"
)


# [END bigtable_write_increment_async]
12 changes: 11 additions & 1 deletion samples/snippets/writes/writes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import asyncio
import os
import uuid

import backoff
from google.api_core.exceptions import DeadlineExceeded
import pytest
import uuid

from .write_increment_async import write_increment_async
from .write_batch import write_batch
from .write_conditionally import write_conditional
from .write_increment import write_increment
Expand Down Expand Up @@ -71,3 +73,11 @@ def _write_batch():
_write_batch()
out, _ = capsys.readouterr()
assert "Successfully wrote 2 rows" in out

@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=60)
def _write_increment_async():
asyncio.run(write_increment_async(PROJECT, BIGTABLE_INSTANCE, table_id))

_write_increment_async()
out, _ = capsys.readouterr()
assert "Successfully incremented row" in out
Loading