Skip to content

Commit

Permalink
added docstrings to util.py
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Nov 8, 2024
1 parent 764a403 commit df8fee1
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions samples/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Provides helper logic used across samples
"""


from google.cloud import bigtable
Expand All @@ -20,6 +23,10 @@
delete_retry = Retry(if_exception_type(exceptions.TooManyRequests, exceptions.ServiceUnavailable))

class create_table_cm:
"""
Create a new table using a context manager, to ensure that table.delete() is called to clean up
the table, even if an exception is thrown
"""
def __init__(self, *args, verbose=True, **kwargs):
self._args = args
self._kwargs = kwargs
Expand All @@ -42,6 +49,9 @@ def __exit__(self, *args):


def create_table(project, instance_id, table_id, column_families={}):
"""
Creates a new table, and blocks until it reaches a ready state
"""
client = bigtable.Client(project=project, admin=True)
instance = client.instance(instance_id)

Expand All @@ -64,7 +74,14 @@ def create_table(project, instance_id, table_id, column_families={}):
exceptions.FailedPrecondition,
exceptions.NotFound,
)
timeout=120,
)
def wait_for_table(table):
"""
raises an exception if the table does not exist or is not ready to use
Because this method is wrapped with an api_core.Retry decorator, it will
retry with backoff if the table is not ready
"""
if not table.exists():
raise exceptions.NotFound

0 comments on commit df8fee1

Please sign in to comment.