In Python, How Do You Call an Unbound Function as a New Thread with the thread Module?

Problem scenario
You want to write a program to call a unbound function in a new thread with the threading module. How do you do this?

Solution
Run this program (e.g., python foobar.py):

import _thread as thread
def contint():
print(“Hello!”)

#if name == “main”:
foobar = thread.start_new_thread(contint, () )
print(“thread finished exiting”) …

How Do You Instantiate a class in Python?

Problem scenario
You inherited code from a different programmer. You want to test out an object. How do you create an object of a pre-made class?

Solution
Look at how many variables the class has. Take this code for example:

class Cartesian(object):
def __init__(self,a = 0,b = 0):
self.a = a
self.b = b

def distance(self):
return (self.a**2 + self.b**2) ** 0.5 # Pythagorean theorem. …

How Do You Use Encapsulation in Python?

Problem scenario
You know about encapsulation.  You know about private methods that are only accessible from inside a class — not outside.  You want to encapsulate a method in a class in Python.  How do you write such a program?

Solution
“Programmers are most effective if shielded from, not exposed to, the innards of modules not their own.” (This was taken from page 272 of The Mythical Man-Month.)

Here is an example;

How Do You Use Python to Create a Server in AWS?

Problem scenario
You want to be able to create EC-2 instances in AWS with a Python script.  How do you do this?

Solution
Prerequisites

This assumes you have installed Boto3.  If you do not know how, see this posting.

Procedures
As a reference, you may go here to learn more about Boto’s features:  http://boto3.readthedocs.io/en/latest/guide/migrationec2.html
Use this program:

“”” Usage instructions
1. …

How Do You Troubleshoot the Message “pycurl error 22”?

Problem scenario
You are trying to install a new package or apply yum updates on a CentOS/RHEL/Fedora server.  But you get 404 errors or “pycurl error 22” as the result of your yum commands.  How do you use yum to install a new package or apply a new update?

Solution
Run this command:  sudo yum clean all

Now your yum commands should work.

How Do You Find the Instance Ids of Virtual Servers in AWS Using Python?

Problem scenario
You want to write a Python program to display the instance IDs of virtual servers in AWS.  What do you do?

Solution

Prerequisites
This assumes you have installed Boto3.  If you do not know how, see this posting.  This assumes that you are running it on a server that has access to AWS (e.g., it has access to the internet).

How Do You Eliminate the “Namespace”, Quotes and Brackets from the Variable You Are Printing with Python?

Problem scenario
You are trying to print a variable, but it prints out like this:

Namespace(string=[‘foobar’])

How do you print out just “foobar” with no quotes?

Solution

Overview
The key word here is “string”.  The “string” text in this is the attribute you will want.

Background
Your program may have something like this:

args = parser.parse.args() # Assuming this is the variable assignment of “foobar”
print (args)

 Procedures
1. 

How Do You Install pip or pip3?

Problem scenario
You think you installed pip (e.g., using this posting for Debian/Ubuntu, using this posting with Linux SUSE, using this posting for a CentOS/RHEL/Fedora server, or this posting as an alternative), but when you run pip, you get “command not found.”  How do you install pip and get it working?

Solution
pip or pip3 may actually be installed despite the fact that things may seem otherwise. 

What Is the Python Requests Syntax Equivalent of a curl “-X DELETE”?

Problem scenario
You are invoking requests.get() in a Python program (after an “import requests” statement near the top of your code).  You want to pass the equivalent of “-X DELETE” in curl in your Python program.  How do you use the DELETE option with a REST API call in Python?

Solution
Prerequisites

This assumes you have “requests” installed (e.g., sudo pip install requests).