How Do You Troubleshoot The Message “no module named pandas”?

Problem scenario
From a Python command prompt or when you run a Python program, you get the error message “no module named pandas”. You have used pip and pip3 to install pandas. What should you do?

Solution
Use “python3” instead of “python” to ensure you are using Python 3.

You may also want to see this posting.

How Do You Write a Python Program to Parse a Log File and Return Lines with the Word “Failed”?

Problem scenario
You want to retrieve lines from a log file with the word “failed”. How do you do this with Python?

Solution

Assuming the log file is named “auth.log”, this will work:

log_reader = open(‘auth.log’, ‘r’)
for line in log_reader:
lower_line = line.lower()
if lower_line.find(“failed”) != -1:
print(line) …

How Do You Get a Table in HTML/Javascript to Display Only The First n (or 10) Rows?

Problem scenario
You have a web page with a table. It uses HTML and Javascript. You want only a sub-portion of the rows to be displayed and allow the user from the front-end to click a drop down menu to see more results. What should you do?

Possible Solution #1
Have two blocks in the .html file. Have the top one define a function that accepts a parameter and filters the table results if/when called.

What is a Recommended Practice for Handling Asynchronous Requests with Long-Running Processes with Flask?

Problem scenario
You are designing a Flask application with Python running some long-running processes. You want to handle the requests asynchronously. The individual requests will have a duration of more than one hour. What is the recommended way of handling this?

Solution
Use a task queue like Celery or RQ (Redis Queue).

Sources:
https://stackoverflow.com/questions/42790362/how-to-handle-long-sql-query-in-flask

Flask API app for long running process?

How Do You Troubleshoot “ImportError: No module named ‘pandas'”?

Problem scenario
From a Python command line, you get this message:

Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named ‘pandas’

What should you do?

Solution
Prerequisite
You need pip to be installed. If you need assistance, see this posting.

Procedures
Run one of these commands:

pip install pandas

pip3 install pandas …

In Python How Do You Avoid the Inelegant Dictionary Key-Check Logic?

Problem scenario
You try this prog1.py:

book = {}
book[”apple”] = 5
var1 = book[”orange”]

It returns this:

Traceback (most recent call last):
File “prog1.py”, line 3, in
var1 = book[“orange”]
KeyError: ‘orange’

You often see a couple lines of code check if a key exists in a dictionary before a specific action. You want to avoid this and write Python in a more elegant fashion.

Why Would You Use the enumerate Keyword with a dictionary in Python?

Problem scenario
You see a dictionary used with the enumerate reserved word in Python.

Here is an example:


good_dict = {}
good_dict[”size”] = “medium”
good_dict[”quantity”] = “80”
good_dict[”location”] = “New_York”
good_dict[”phone”] = “555-555-5555”

for idx1, good_key in enumerate(good_dict):

Solution
A counter (a unique integer) can be assigned to each key. For logic-building purposes, it is beneficial. The above snippet would prepare a for loop for logic with 1) integers uniquely being assigned to the keys of the dictionary and 2) the keys of the dictionary.

Why Would You Assign or Copy a list in Python to be a set?

Problem scenario
You are trying to understand some code. You see a variable in Python that represents a list being cast to a set.

You see something like this in a Python program:

example = [1, 1, 2, 2, 3, 3]
new_example = set(example)

You are curious why this would be done.

From the IDE you do (and see) the following:

example = [1, …