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, …

What Does MoRef Mean or Refer to?

Question
What does MoRef mean?

Answer
It is a portmanteau of “Managed Object Reference.” It is a data type that comes up in the context of alerts/alarms in vSphere.

(The answer was adapted from page 152 of PowerCLI Cookbook by Sellers.)

“MoRefs are unique identifiers assigned to objects at the time of their creation.” (Taken from page 202 of PowerCLI Cookbook by Sellers.)

Is There a Difference between Coding Languages and Programming Languages?

Question
You read that there is a difference between coding languages and programming languages. What is the difference?

Answer
The question is theoretical and subjective. Sometimes diction, semantics and job descriptions are not that important.

Coding and programming are different (but there are probably very highly-paid people who disagree). It seems that in certain contexts, rightly or wrongly,

Python Challenge of the Day (2/17/22)

Problem scenario
You have a program as follows:

class Location:
def __init__(self, country=”US”, state=”CA”, city=”Los Angeles”):
self.country = country
self.state = state
self.city = city

def printer(self):
return [self.country, self.state, self.city]

new_loc = Location(“Mexico”, “Mexico City”, “Mexico City”, “good neighborhood”)
print(new_loc.printer())

Here are your choices of what it could print out:

a. TypeError: init() takes from 1 to 4 positional arguments but 5 were given
b.

Do Ordered Dictionaries in Python Consume More Memory than Regular Dictionaries?

Question
You are considered using ordered dictionaries in Python (e.g., because of the way they handle temporal data). Will your program use more memory this way compared to using a regular dictionary?

Answer
Yes. All things being equal, ordered dictionaries use more memory than normal dictionaries.

# You will see that Python 3’s ordered dictionaries consume more memory than regular dictionaries
# This was adapted from https://lerner.co.il/2019/05/12/python-dicts-and-memory-usage/

import sys
from collections import OrderedDict

x = OrderedDict()
y = {}

for i in range(1, …