How Do You Install GraphQL for Python?

Problem scenario
You want to install GraphQL for Python in Linux. What should you do?

Solution

  1. Install pip3 and venv. (If you need assistance with pip3, see this posting. If you need assistance with venv, see this posting.)
  2. Run these commands:

python3 -m venv to_test
cd to_test
source bin/activate
pip3 install graphql-python
pip3 install flask ariadne flask-sqlalchemy flask-cors …

How Do You Troubleshoot String Manipulation with List Slicing Not Working as You Expect?

Problem scenario
You are taking a slice of a Python string. You know that syntax like var_x[-2:] signifies the penultimate character and the last character of a string (called var_x). But you are only seeing one character — not two. You expect to see two (or a different number of characters than what you are seeing). Why is the snippet printing out fewer characters than you expect?

Possible Solution #1
There could be an invisible “\n” attached to the string (e.g.,

How Do You Get a Multi-line Variable in Python to Be Treated as Lines and Not Characters?

Problem scenario
When a number of lines of text are read from a file, the Python program sees each line as such. When a number of lines of text are in the program itself, that is you use a multi-line variable assignment in a .py program, Python sees the characters individually and does not distinguish between the different lines. The len() function reflects these differences. You want the Python program to read the text in the program as if it were read from the “with open” and “readlines()” function line-by-line — not character-by-character.

What Is The Time Complexity when There Are Four Variables Used Arithmetically in a Program?

Question
You have a program with four variables that are used in arithmetic, but no other operations happen. (There are no while or for loops at all.) What is the time complexity of such an algorithm?

Answer
O(1). An algorithm, as long as the number of operations is fixed or constant, can be considered to have O(1) time complexity. The source is page 39 of Elements of Programming Interviews in Python.

What is Cyclomatic Complexity?

Question
What is cyclomatic complexity or McCabe’s complexity?

Answer
It refers to the number of times a line of source code is entered in its execution.

Number of executionsLevel of complexity1 to 10 Not complex11 to 20Moderately complex21 to 50Really complexMore than 50Too complex

It is advisable to endeavor to keep the cyclomatic complexity as low as possible (according to this external site);

How Do You Write a Python Program without the import Statement That Palindromically Tests a String in Four Lines of Code?

Problem scenario
You want to avoid importing any modules in Python. You want to have a way to test if a string is a palindrome is four lines of code. What do you do?

Solution

def is_palindrome_pythonic(s):
return all(a == b for a, b, in zip(
map(str.lower, filter(str.isalnum, s)),
map(str.lower, filter(str.isalnum, reversed(s)))))

# The above four lines of code were taken from page 78 of Elements of Programming Interviews in Python. …

How Do You Use Python to Retrieve Information about Bitcoin Transactions?

Problem scenario
You want to use Python to get data from BTC transactions. You do not want to use “import requests”. What do you do?

Solution
Prerequisite

This assumes you have installed the Bitcoin pypi package: pip install bitcoin
If you need assistance installing pip, see this posting for Ubuntu or this posting for a Red Hat derivative.