"""Util functions for the package.""" import boto3 def cut_list(seq, size): """Cut flat iterable object. Cuts flat list into a list with lists of passed size. Args: seq (sequence): Iterable object. size (int): Size of the groups in the result. Returns: generator: Cut iterable object. """ return (seq[i::size] for i in range(size)) def keys_in_s3_location(bucket_name, path): """List keys in S3 location. Args: bucket_name (str): S3 bucket name. path (str): S3 path. Returns: list(str): List of S3 keys in the location. """ s3_resource = boto3.resource('s3') bucket = s3_resource.Bucket(bucket_name) return [ 's3://{}/{}'.format(bucket_name, key.key) for key in bucket.objects.filter(Prefix=path)] def bucket_and_path_from_s3_list(s3_link): """Get S3 bucket and the rest of the link. Args: s3_link (str): S3 link in the following format: s3://bucket-name/target/path/ Returns: tuple(str, str): S3 bucket name and S3 path. """ parts = s3_link.split('/') return parts[2], '/'.join(parts[3:]) def delete_s3_key(s3_path): """Delete S3 key. Args: s3_path (str): S3 link in the following format: s3://bucket-name/target/path/ """ bucket, path = bucket_and_path_from_s3_list(s3_path) s3_resource = boto3.resource('s3') obj = s3_resource.Object(bucket, path) obj.delete() def find_exception(seq): """Find first Exception object in the sequence. Args: seq (sequence): Iterable object. Returns: Exception: Returns Exception object if any found, otherwise None. """ return next((item for item in seq if isinstance(item, Exception)), None)