What Is a Decorator in I.T. or DevOps?

Problem scenario
You have read about decorators in object-oriented programming and in Python. Is there a disambiguation of what a decorator is?

Solution
In object-oriented programming, inheritance allows for an object to change from its class at compile time (according to page 201 of Programming Interviews Exposed). In OOP, the decorator pattern is a structural design pattern (according to the inside of the front cover of Design Patterns) and is a way to modify an object in OOP at run-time (according to page 201 of Programming Interviews Exposed).

How Do You Troubleshoot the pipenv error message “tomlkit.exception”?

Problem scenario
You try to run a pipenv command, but you get an error about “tomlkit.exception”.

Solution
Copy the Pipfile to a new name (to back it up). Delete the original Pipfile. Try to run the command again. If it still fails, try removing pipenv by uninstalling it. Then reinstall pipenv. Then reboot the computer.

A List of Python Books

1593279280Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to Programming by No Starch Press

1593279922Automate the Boring Stuff with Python, 2nd Edition: Practical Programming for Total Beginners by No Starch Press

1449355730Learning Python, 5th Edition by O’Reilly Media

0134692888Learn Python 3 The Hard Way (Zed Shaw’s Hard Way Series) by Addison-Wesley Professional

1491957662Python for Data Analysis: Data Wrangling with Pandas,

How Do You Write a Palindrome Tester Function in Python?

Problem scenario
You want to write a palindrome tester function in Python without the :: operator in the code (that reverses the order of the list). What do you do?

Solution

def palindrome_tester(s):
s = s.lower()
x = len(s) // 2
for i in range(x):
if s[i] == s[-(i+1)]:
pass
else:
return False
return True

var_string = “mmnMM”

if (palindrome_tester(var_string)):
print(var_string + ” is a palindrome!”)
else:
print(var_string + ” is NOT a palindrome!”) …

How Do You Escape or Break from a Method in Python?

Problem scenario
You want to quit a function, or return out of it, when a certain condition is met. How do you do this?

Solution
This program illustrates how it is done. You will want to run the program twice, once by leaving the “5” as it is, and a second time by changing the “5” to “6”.

def cool(x):
if x == 5:
return “There was equivalence”
else:
y = x
print(“This statement is printing as an illustration”)
return “The comparison was NOT equivalent”

var1 = cool(5)
print(var1) …

How Do You Troubleshoot the Python3 Error “No such file or directory: ‘/usr/local/lib/python3.6/site-packages/” on RHEL 8.x?

One or more of the following problems are happening to you:

Problem scenario #1
You run python3 setup.py install on RHEL 8.x, but you get an error message like this:

running install
error: can’t create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

[Errno 2] No such file or directory: ‘/usr/local/lib/python3.6/site-packages/test-easy-install-52191.write-test’

The installation directory you specified (via –install-dir, …

How Do You Get Python to Parse Web Pages to Find a String?

Problem scenario
You know a string is buried in a series of web pages. How do you get Python to read the web pages and find the string?

Solution
Change the URLs as you desire. Change the “searchterm” variable assignment to the word of your choice. Then run this Python 3 program:

import re
import requests
listofurls = [‘https://www.continualintegration.com/’, ‘https://www.continualintegration.com/miscellaneous-articles/page/1/’, …

How Do You Troubleshoot the Python Error “NameError: name ” is not defined”?

Problem scenario
You are trying to run a Python program with a class. You get this message:

File “foobar.py”, line 11, in
class Building(param1, param2):
NameError: name ‘param1’ is not defined

What should you do?

Solution
Use the word “object” instead of one or more parameters that you are passing in the parentheses () of the class.

How Do You Troubleshoot “ModuleNotFoundError: No module named ‘zlib'”?

Problem scenario
You are trying to install Python 3 but you get a message about zlib. This is the full message:

rm /usr/local/lib/python3.9/lib-dynload/_sysconfigdata__linux_x86_64-linux-gnu.py
rm -r /usr/local/lib/python3.9/lib-dynload/__pycache__
/bin/install -c -m 644 ./Misc/python.man \
/usr/local/share/man/man1/python3.9.1
if test “xupgrade” != “xno” ; then \
case upgrade in \
upgrade) ensurepip=”–altinstall –upgrade” ;; \
install|*) ensurepip=”–altinstall” ;; \
esac; \
./python -E -m ensurepip \
$ensurepip –root=/ ; \
fi
Traceback (most recent call last):
File “<frozen zipimport”, …