How Do You Use the Python self Keyword?

Problem scenario
You have seen Python functions defined using the self keyword. You want to test it out. How do you do this?

Solution
First of all, self is NOT a keyword in Python. Yes, you should probably use the word “self” when it is invoked. This is a well-accepted convention. It may be an unwritten rule. But you do not need to use the word “self”.

How Do You Write a Python Program to Test if a Word is a Substring of Another Word?

Problem scenario
You want to write a program that will test if a pattern is in another word. You are looking for the SQL equivalent of “contains” in Python. How do you test if a string is a substring of another word?

Solution
Use this program:

# Change “micro” and “microsoft” to the substring and string to be searched respectively:

a = “micro”
b = “microsoft”
if a in b:
print(a + ” is in ” + b)
else:
print(a + ” is NOT in ” + b) …

Using Python How Do You Write a Palindrome Tester with Special Requirements?

Problem scenario
You want to write a Python that will test if a string is a palindrome with two other requirements. One requirement is to not use a “::” (a colon pair) in the code. The second requirement is to search for a substring that is a palindrome if the full string is not a palindrome. This substring is not any substring however: it must include the leftmost character.

Why Does Your Python Function Return None?

Problem scenario
Your Python function prints out the value of the variable immediately before it returns the variable.  When it is assigned via the “return” statement, the value is “None”.  You were expecting an integer or some other value.  What is wrong?

Solution
Does your function use recursion?  If it is using recursion rather than merely calling the function and it returns “None”,

How Do You Troubleshoot the Python Message “ImportError: No module named setuptools”?

Problem scenario
You try to run sudo python setup.py build but you encounter a problem. You get a message that says “ImportError: No module named setuptools”. What should you do?

Solution
If you are running Debian/Ubuntu Linux, run this: sudo apt-get -y install python-setuptools

If you are running CentOS/RedHat/Fedora Linux, run this: sudo yum -y install python-setuptools

How Do You Troubleshoot the Message “ModuleNotFoundError: No module named ‘awscli'”?

Problem scenario
You try to run an AWS CLI command. But you receive this error:

‘ File “/bin/aws-cli-1.16.226/bin/aws”, line 19, in
import awscli.clidriver
ModuleNotFoundError: No module named ‘awscli’

Solution
Did you run sudo python3 setup.py install ? If you only ran the build step, this could happen.

How Do You Troubleshoot “/usr/bin/env: ‘python’: No such file or directory”?

Problem scenario
You try to run an AWS CLI command but you receive this error:
/usr/bin/env: ‘python’: No such file or directory

What should you do?

Possible Solution #1
If Python 3 is not installed, install it. You may want to see these postings:
How Do You Upgrade to Python 3.x on Ubuntu 16?
How Do You Upgrade Python 2.x to Python 3.7 in Debian or Ubuntu Linux?

How Do You Install Molecule on an Ubuntu 18.x Linux Server?

Problem scenario
You want to use Molecule to test Ansible roles. How do you install Molecule on Ubuntu 18.x with Python 3?

Solution
Prerequisites
i. You must have Docker installed. If you need assistance see this posting.
ii. You must have pip3 installed on the Docker host. sudo apt-get install -y python3-pip

Procedures
1.

How Do You Create a Dockerfile That Will Use Reserved Words Such as FROM, RUN, COPY, WORKDIR, ADD, and LABEL?

Problem scenario
You want to create your own Dockerfile. You also want to invoke reserved words such as FROM, RUN, COPY, WORKDIR, ADD, and LABEL. You want to then create a Docker image from it. You want to ultimately create a working Docker container from that image. How do you do all of this?

Solution

1. In a given directory,