What Does “__” Mean in Python?

Problem scenario
You see two underscores or two underbars before a function in Python. What does this syntax signify?

Possible Solution #1
The answer is best explained by running this program once with no modification and a second time with a modification.

class ContintClass():
def __init__(self):
self.__completelyprivate = “1111111111”
self._semiprivate = “2222222”

foo = ContintClass()
#print(foo.__completelyprivate)

print(“Above is an attempt to print a completely private data member of an object”)

print(“Below is an attempt to print a semi-private data member of an object”)
print(foo._semiprivate)

Before you run the program the second time,

 » Read more..

Why is Your Python Programming Printing Duplicate Lines?

Problem scenario
You have a Python program with print statements. When you run the program, you get twice as many print statements as you expect. What could be wrong?

Solution
Do not name the program re.py or string.py. When you name the program re.py and have an import re statement, Python may print every line it is supposed to print twice.

 » Read more..

How Do You Write a Python Program to Create a .txt File with 50 Key-Value Pairs?

Problem scenario
You want to create a .txt file that has 50 key-value pairs. You want the keys to be whole numbers. You want the values to be five-character strings. What should you do?

Solution

import random
import string
import sys

def randomword(length):
letters = string.ascii_lowercase
return ”.join(random.choice(letters) for i in range(length))

def printer(counter, webstera):
webstera[counter] = randomword(5)
if (counter <  » Read more..

How Do You Troubleshoot “AttributeError: module ‘re’ has no attribute ‘IGNORECASE'”?

Problem scenario
When you run your Python program, you get this message: “AttributeError: module ‘re’ has no attribute ‘IGNORECASE'”

What should you do to get your program to work correctly?

Solution
Change the name of the program to something different. Do not name your program “re.py”. If you use the “import string” and your program is called “re.py”, you may get this error.

 » Read more..

How Do You Install pip on RHEL?

Problem scenario
You are using a Red Hat derivative of Linux. How do you install pip (e.g., on CentOS or Fedora)?

Solution
Run this command: sudo dnf install python3-pip

 » Read more..

Where Can You Find a List of PowerShell Version 5 Commands?

Problem scenario
You want to see every PowerShell 5 command available. What should you do?

Solution
This may not show every PowerShell v. 5 command, but it will show many: Get-Command

Another was is to open Windows PowerShell then go to View -Show Command Add-On. The list will be different if you are running Windows in Google Cloud Platform or AWS.

 » Read more..

How Do You Troubleshoot Messages about a markupsafe Fatal Error, C Extension Not Being Compiled, And/Or a Syntax Error with async when You Are Trying to Build a Docker Image?

Problem scenario
You wrote a Dockerfile. You are using it to try to create a Docker image. When you run the “docker build” command you get some error messages. The errors include one or more of the following:

1) A MarkupSafe fatal error related to Python.h.
2) A C extension not being compiled.
3) An invalid syntax error related to async (e.g., Jinja2/jinja2/asyncfilters.py”).

You need to base the image off Ubuntu.

 » Read more..

When Manipulating Lists in Python, Why is pop Slower than del?

Problem scenario
You use del and pop to remove items from lists. You noticed that pop is slower than del. Why is this the case?

Solution
There are two reasons. One, pop returns the value that is removed. Two, pop acts as a function call as opposed to a primitive action. Whereas the del invokes a primitive action (a non-recursive function call),

 » Read more..

How Do You Find what Version of C++ Is Installed?

Problem scenario
You have C++ installed. You want to know what version it is. What do you do?

Solution
Possible solution #1
Run this command: /usr/bin/cpp -v

Look for the word “version”. Use CTRL-c to exit out.

Possible solution #2
Run these two commands:

. /opt/intel/bin/iccvars.sh intel64
icc -v

(Taken from https://superuser.com/questions/465949/how-to-know-what-version-of-c-compiler-is-installed-on-linux-server)

 » Read more..

How Do You Find Where the Python Interpreter Will Look for Modules?

Problem scenario
You want to use custom modules (e.g., .py files that will be called by another Python program). You are trying to figure out which location(s) the Python interpreter will look for such modules. How do you determine the directory where Python will look when it uses the “import” key word?

Solution
Run these three commands (the first is a Bash command and the other two are Python):

python
import sys
print(sys.path)  » Read more..