coll/mc_sim_coll_blog.py [13:66]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def check_env_var(a_var, a_var_name):
    """ Check that an expected environment variable is actually present.

    :param a_var: Variable to be checked
    :param a_var_name: Name of environment variable that should be present
    :return: None; exit program if variable is not present
    """
    if a_var is None:
        print(f"Environment variable {a_var_name} is not present!")
        sys.exit(2)
    # endif #
# enddef check_env_var() #

def upload_file(file_name, bucket, object_name=None):
    """Upload a file to an S3 bucket

    :param file_name: File to upload
    :param bucket: Bucket to upload to
    :param object_name: S3 object name. If not specified then file_name is used
    :return: True if file was uploaded, else False
    """

    # If S3 object_name was not specified, use file_name
    if object_name is None:
        object_name = os.path.basename(file_name)
    # endif #

    # Upload the file
    s3_client = boto3.client('s3')
    try:
        response = s3_client.upload_file(file_name, bucket, object_name)
    except ClientError as e:
        logging.error(e)
        return False
    # endtry #
    return True
# enddef upload_file() #

def get_input_csv(bucket_name, file_name):
    """ Download and read CSV file from an S3 bucket

    :param bucket_name: Bucket in which CSV file is located
    :param file_name: key name of the CSV file to read
    :return: DataFrame constructed from CSV file
    """
    s3 = boto3.client('s3')
    response = s3.get_object(Bucket=bucket_name, Key=file_name)
    status = response.get("ResponseMetadata", {}).get("HTTPStatusCode")
    if status == 200:
        print(f"Retrieved file {file_name} from bucket {bucket_name}")
        return pd.read_csv(response.get("Body"), index_col=0)
    else:
        print(f"Error in retrieving file {file_name} from bucket {bucket_name}; {status}")
        sys.exit(1)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



sim/mc_sim_blog.py [33:86]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def check_env_var(a_var, a_var_name):
    """ Check that an expected environment variable is actually present.

    :param a_var: Variable to be checked
    :param a_var_name: Name of environment variable that should be present
    :return: None; exit program if variable is not present
    """
    if a_var is None:
        print(f"Environment variable {a_var_name} is not present!")
        sys.exit(2)
    # endif #
# enddef check_env_var() #

def upload_file(file_name, bucket, object_name=None):
    """Upload a file to an S3 bucket

    :param file_name: File to upload
    :param bucket: Bucket to upload to
    :param object_name: S3 object name. If not specified then file_name is used
    :return: True if file was uploaded, else False
    """

    # If S3 object_name was not specified, use file_name
    if object_name is None:
        object_name = os.path.basename(file_name)
    # endif #

    # Upload the file
    s3_client = boto3.client('s3')
    try:
        response = s3_client.upload_file(file_name, bucket, object_name)
    except ClientError as e:
        logging.error(e)
        return False
    # endtry #
    return True
# enddef upload_file() #

def get_input_csv(bucket_name, file_name):
    """ Download and read CSV file from an S3 bucket

    :param bucket_name: Bucket in which CSV file is located
    :param file_name: key name of the CSV file to read
    :return: DataFrame constructed from CSV file
    """
    s3 = boto3.client('s3')
    response = s3.get_object(Bucket=bucket_name, Key=file_name)
    status = response.get("ResponseMetadata", {}).get("HTTPStatusCode")
    if status == 200:
        print(f"Retrieved file {file_name} from bucket {bucket_name}")
        return pd.read_csv(response.get("Body"), index_col=0)
    else:
        print(f"Error in retrieving file {file_name} from bucket {bucket_name}; {status}")
        sys.exit(1)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



