Python program structure

Andrei Maksimov

Andrei Maksimov

0
(0)

In this section of the course, we’ll describe what major components every Python program consists of and cover best practices for proper structuring and formatting your Python programs.

Program example

Here’s an example of a simple Python program, which copies files from one S3 bucket to another.

1. Python program structure - Example program

There’s no need to understand how this code works right now.

We’ll use this example to illustrate a typical Python program structure with function:

#!/usr/bin/env python3

"""
This program copies objects from source to destination S3 bucket
"""

import boto3

REGION = 'us-east-2'
SRC_BUCKET = 'hands-on-cloud-python-course-src-bucket'
DST_BUCKET = 'hands-on-cloud-python-course-dst-bucket'

S3_RESOURCE = boto3.resource('s3', region_name=REGION)


def copy_objects():
    src_bucket = S3_RESOURCE.Bucket(SRC_BUCKET)
    dst_bucket = S3_RESOURCE.Bucket(DST_BUCKET)

    print(f'Source S3 bucket: {SRC_BUCKET}')
    print(f'Destination S3 bucket: {DST_BUCKET}')
    
    for s3_object in src_bucket.objects.all():
        print(f'Copying object: {s3_object.key}')

        source_file_data = {
            'Bucket': SRC_BUCKET,
            'Key': s3_object.key
        }

        dst_bucket.copy(source_file_data, s3_object.key)


if __name__ == '__main__':
    copy_objects()

Below we provided an explanation of all parts in mode detail.

Shebang

#!/usr/bin/env python3

Line 1 of our program contains a shebang – is a special syntax, which allows Bash to understand which interpreter to use to run your program.

In our case, we’re telling Bash that our Python program needs to be executed using python3 interpreter (command).

It is a good practice to include shebang in your Python program code and provide program users the ability to launch it without calling a Python interpreter first.

Program description

"""
This program copies objects from source to destination S3 bucket
"""

Lines 3-5 represent a multi-line comment used to describe the program’s purpose.

It is a good practice to provide a program description and leave comments in your program code to give other developers the ability to easily understand the purpose and most important parts of your code.

Reused code (modules)

import boto3

Line 7 imports a Python module called boto3.

This module contains a common Python code provided by Amazon Web Services, which allows Python developers to write software that uses AWS services.

In the Python world, modules are used to provide an ability to reuse the same code over and over again.

If you have a common code in your program, you can create a module and put your common code there.

Importing a module in your Python program allows you to access the code defined in the module.

Global variables

REGION = 'us-east-2'
SRC_BUCKET = 'hands-on-cloud-python-course-src-bucket'
DST_BUCKET = 'hands-on-cloud-python-course-dst-bucket'

S3_RESOURCE = boto3.resource('s3', region_name=REGION)

Lines 9-13 contain common global variables, which are used in the entire Python program.

Those variables store information about AWS Region, source and destination S3 bucket names, and boto3 resource, which allows you to send API calls to the S3 service.

Function definitions

def copy_objects():
    src_bucket = S3_RESOURCE.Bucket(SRC_BUCKET)
    dst_bucket = S3_RESOURCE.Bucket(DST_BUCKET)

    print(f'Source S3 bucket: {SRC_BUCKET}')
    print(f'Destination S3 bucket: {DST_BUCKET}')
    
    for s3_object in src_bucket.objects.all():
        print(f'Copying object: {s3_object.key}')

        source_file_data = {
            'Bucket': SRC_BUCKET,
            'Key': s3_object.key
        }

        dst_bucket.copy(source_file_data, s3_object.key)

Lines 16-31 contain a Python function definition.

In Python, functions are used to define reusable pieces of code, which can be used in programs many times.

This function is responsible for copying all S3 objects from source to destination bucket.

Program entry point (main)

if __name__ == '__main__':
    copy_objects()

Lines 34-35 allow us to define a Python program entry point.

Python interpreter reads the code of your program starting from the first line and tries to execute it immediately line by line.

In comparison with C, C++, or Java, for example, there is no main() function in Python, so we have to use this special syntax to tell Python that it should start our program from running copy_objects() function “first”.

Strictly speaking, this is not the first function, which Python will execute in our program, but for simplicity of explanation, we’ll stick with this logic.

How useful was this post?

Click on a star to rate it!

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Top rated Udemy Courses to improve you career

Subscribe to our updates

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Want to be an author of another post?

We’re looking for skilled technical authors for our blog!

Leave a comment

If you’d like to ask a question about the code or piece of configuration, feel free to use https://codeshare.io/ or a similar tool as Facebook comments are breaking code formatting.