How Do You Troubleshoot the Flask Message “LookupError: the converter ‘str’ does not exist”?

Problem scenario
You are trying to pass a string parameter to a Flask application via a call to a URL. You get the message "LookupError: the converter 'str' does not exist." What should you do?

Solution
Use the term "string" instead of "str".

Here is an example of incorrect syntax:
@app.route("/ccc/", strict_slashes=False)

This shows the correct syntax:
@app.route("/ccc/", strict_slashes=False)

What is Writing Your Own Provisioner in Terraform?

Question
You have heard there is such a thing as customer provisioners (not providers) in Terraform. What is a custom-built provisioner in Terraform?

Answer
There are generic provisioners such as "file", "local-exec", and "remote-exec".

To learn about provisioners, see this: https://www.terraform.io/docs/language/resources/provisioners/

Custom provisioners exist; you can create your own provisioner beyond the generic three ("file", "local-exec", and "remote-exec"). To learn more about writing your own (e.g., a plugin or special word for a Terraform provisioner), you can read this GitHub.com posting.

How Do You Go to an ELB from Your Workstation?

Problem scenario
You can go to an ELB's FQDN via an EC-2 instance (with a curl command). But you cannot go to an ELB from your workstation (with a web browser). What should you do?

Possible solution #1
From the EC-2 instance, can you use nslookup FQDNofELB (where FQDNofELB is the FQDN of the ELB)? This should provide you with the IP address (the last of the IP addresses in the results). You can test it from the EC-2 instance by using curl on it or from your workstation going to it in a web browser. Run nslookup x.x.x.x where x.x.x.x is the IP address of the ELB. This should give you a different FQDN that should be accessible from the internet.

Possible solution #2
Check the security groups's rules to ensure they allow for inbound connectivity. From the EC-2 instance, can you use traceroute FQDNofELB (where FQDNofELB is the FQDN of the ELB)? This should resolve the IP address. If you use the IP address of the ELB, you can determine if there is an intermediate firewall blocker between your workstation and the ELB. The AWS Security Groups could be configured to block connectivity from one workstation.

Possible solution #3
Create a Route 53 record for the FQDN. If you need to find the IP address, use "traceroute" from the EC-2 instance. Alternatively you could use nslookup FQDNofELB where FQDNofELB is the FQDN of the load balancer.

Possible solution #4
For the clients that need access, update the local hosts file so it maps the FQDN to the IP address.

Possible solution #5
Wait. If you wait 30 minutes, public DNS servers can update on their own. We have found newly-created ELBs to work immediately. In some cases it may not be Amazon's fault, but the routing tables don't update for the ELB's DNS name that does not resemble an FQDN of a server's name; ELBs have both types of names (one that is a longer alphanumeric string and a second one that resembles the IP address but looks more like an FQDN of a server) and an external IP address.

Possible solution #6
Supposedly this works:

'Use an A record rather than a CNAME in Route53.

In the AWS Management Console, choose "A record" and then move the radio button labeled "Alias" to "Yes." Then select your ELB from the dropdown menu.' It was taken from https://serverfault.com/questions/469094/issues-with-ec2-elastic-load-balancer-dns-and-routing

Possible solution #7
Was the ELB recently deleted? If you run a kubectl command to get the ELB, but the ELB was recently deleted via the web console, it will not work. Try running commands like these to verify you copied the correct ELB:

aws elb describe-load-balancers | grep -i dnsname
aws elbv2 describe-load-balancers | grep -i dnsname

How Do You Use a Nested Dictionary in Python?

Problem scenario
You want to create a dictionary of dictionaries in Python. You want to use them. What do you do?

Solution
Here is an example/illustration:

contint_dict = {}
contint_dict["a_test"] = "123"
contint_dict["b_test"] = "456"
contint_dict["c_test"] = "789"

color_animal_dict = {}
color_animal_dict["blue"] = "dog"
color_animal_dict["orange"] = "cat"
color_animal_dict["green"] = "goat"

days_dict = {}
days_dict["Tuesday"] = "midnight"
days_dict["Wednesday"] = "noon"
days_dict["Thursday"] = "evening"

big_dictionary = {}
big_dictionary["dict_1"] = contint_dict
big_dictionary["dict_2"] = color_animal_dict
big_dictionary["dict_3"] = days_dict

print(big_dictionary["dict_1"]["b_test"])
print(big_dictionary["dict_2"]["green"])
print(big_dictionary["dict_3"]["Wednesday"])

super_dictionary = {}
super_dictionary["top_of_nested"] = big_dictionary
print(super_dictionary["top_of_nested"]["dict_3"]["Wednesday"])

You may also want to see this: https://pypi.org/project/nested_dict/

Object-Oriented Programming Quiz

1. What are the three types of design patterns?
__________________

2. As a recommended practice in OOP, which is preferred?

a. Object composition
b. Class inheritance
c. Programming to an implementation
d. none of the above

3. Polymorphism happens at which time?

a. Compile time
b. Run time
c. Both of the above
d. None of the above

The sources for C are these:
https://www.geeksforgeeks.org/difference-between-compile-time-and-run-time-polymorphism-in-java/
https://www.nerd.vision/post/polymorphism-encapsulation-data-abstraction-and-inheritance-in-object-oriented-programming

4. Inheritance happens at which time?

a. Compile time
b. Run time
c. Both of the above
d. None of the above

5. Composition in OOP happens at which time?

a. Compile time
b. Run time
c. Both of the above
d. None of the above

6. With encapsulation, what is exposed?

a. Methods
b. Data
c. Both of the above
d. None of the above.

7. When can objects (as in OOP) exist?

a. Objects can exist at compile time
b. Objects can exist at run time
c. Both of the above
d. None of the above

8. What is another term for parent class?

a. Ancestor class
b. Base class
c. Superclass
d. All of the above
e. None of the above

9. Which of the following programming languages allow for multiple inheritance of classes? Choose all that apply.

a. Python
b. Java
c. C++
d. C#
e. None of the above


To see the answers, click here.

Object-Oriented Programming Quiz & Answers

1. What are the three types of design patterns?
__________________

Answer: Creational, behavioral, structural. Source: Page XV of Design Patterns: Elements of Reusable Object-Oriented Software

2. As a recommended practice in OOP, which is preferred?

a. Object composition
b. Class inheritance
c. Programming to an implementation
d. none of the above

Answer: A. Source: Page 20 of Design Patterns: Elements of Reusable Object-Oriented Software

3. Polymorphism happens at which time?

a. Compile time
b. Run time
c. Both of the above
d. None of the above

Answer: C, but B could be an acceptable answer. For B, the source is page 361 of Design Patterns: Elements of Reusable Object-Oriented Software; apparently with C++, it is just at run-time.

The sources for C are these:
https://www.geeksforgeeks.org/difference-between-compile-time-and-run-time-polymorphism-in-java/
https://www.nerd.vision/post/polymorphism-encapsulation-data-abstraction-and-inheritance-in-object-oriented-programming

4. Inheritance happens at which time?

a. Compile time
b. Run time
c. Both of the above
d. None of the above

Answer: A. Source: Page 19 of Design Patterns: Elements of Reusable Object-Oriented Software and https://www.quora.com/What-is-meant-by-%E2%80%9Cinheritance-is-compile-time-and-composition-is-runtime%E2%80%9D

5. Composition in OOP happens at which time?

a. Compile time
b. Run time
c. Both of the above
d. None of the above

Answer: B Page 19 of Design Patterns: Elements of Reusable Object-Oriented Software. Source: https://www.quora.com/What-is-meant-by-%E2%80%9Cinheritance-is-compile-time-and-composition-is-runtime%E2%80%9D

6. With encapsulation, what is exposed?

a. Methods
b. Data
c. Both of the above
d. None of the above.

Answer: A. Source: https://www.nerd.vision/post/polymorphism-encapsulation-data-abstraction-and-inheritance-in-object-oriented-programming

7. When can objects (as in OOP) exist?

a. Objects can exist at compile time
b. Objects can exist at run time
c. Both of the above
d. None of the above

Answer: B. Page 361 of Design Patterns: Elements of Reusable Object-Oriented Software and
https://www.quora.com/When-an-object-is-created-is-it-during-compile-time-or-during-run-time.

8. What is another term for parent class?

a. Ancestor class
b. Base class
c. Superclass
d. All of the above
e. None of the above

Answer: D. Source: Page 361 of Design Patterns: Elements of Reusable Object-Oriented Software.

9. Which of the following programming languages allow for multiple inheritance of classes? Choose all that apply.

a. Python
b. Java
c. C++
d. C#
e. None of the above

Answer: A and C. Sources: Page 188 of Programming Interviews Exposed by Mongan, Kindler and Giguere (for why C is correct and B and D are incorrect). Page 118 of Learning Python for why A is correct.

How Do You Troubleshoot “timed out waiting for the condition” after Running “kubeadm init”?

Problem scenario
You run "sudo kubeadm init", and you get this message:

[kubelet-check] It seems like the kubelet isn't running or healthy.
[kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp 127.0.0.1:10248: connect: connection refused.

[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[kubelet-check] Initial timeout of 40s passed.

        Unfortunately, an error has occurred:
                timed out waiting for the condition

        This error is likely caused by:
                - The kubelet is not running
                - The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)

        If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
                - 'systemctl status kubelet'
                - 'journalctl -xeu kubelet'

        Additionally, a control plane component may have crashed or exited when started by the container runtime.
        To troubleshoot, list all containers using your preferred container runtimes CLI.

        Here is one example how you may list all Kubernetes containers running in docker:
                - 'docker ps -a | grep kube | grep -v pause'
                Once you have found the failing container, you can inspect its logs with:
                - 'docker logs CONTAINERID'

error execution phase wait-control-plane: couldn't initialize a Kubernetes cluster

What should you do?

Solution
If this is for production or something with sensitive data, we would be concerned and have no real solution for you. However if there is no real SLA, if you are doing development (e.g., for a test or proof-of-concept), just continue. Ignore the above error, as it will likely not affect you or block you. You may have "ready" nodes with the above error. Run "kubectl get nodes". You may just need to install flannel with these commands and continue:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml

kubectl -n kube-system apply -f https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml

You may need to log into the worker nodes and create /run/flannel/subnet.env (create the path if necessary). Here is the suggested content of this subnet.env file:

FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true

We were surprised that the above message could be ignored without it affecting Kubernetes' functionality. However for something important you may want to look into the error.

How Does sorted(list_arg1, key=arg2) Work in Python?

Question
You see that Python's sorted reserved word has an option to use a key (rather than the default mechanism). How does the key parameter/flag in Python's sorted function work?

Short Answer:
The word "key" designates a special function that you create. It accepts two parameters and returns positive or negative numbers. These help give you the ability to sort the list in a non-standard way.

Detailed Answer:
"The value (or values) that determines the sorting order is referred to as the key." (This quote was taken from page 144 of Programming Interviews Exposed.)

The function is invoked as a key with syntax like this:

sorted(contint_list, key=functools.cmp_to_key(cool_comp))

This function "cool_comp" is the arbitrary name and "cmp_to_key" is part of the "functools" library. (The above would need "import functools" in the Python program to work properly; it would also need "cool_comp" or whatever function name you choose to be defined). This "cool_comp" can return different values. However if it returns a True or False, the regular "sorted" feature will happen with default functionality (as if no key= was designated).

If the function in the key ("cool_comp" in this example) of sorted returns negative numbers (integers or decimals), the function will change the position of the two values in comparison.

In the sorted() keyword, if the function returns a negative value, the two values in the list will be swapped. If the function returns a positive value, the two values in the list will not be swapped.

The function via "key=functools.cmp_to_key(foobar)" syntax will be called with sending to parameters from the list. It will start with the leftmost two elements and iterate toward the right (taking two elements in the list at a time). The number of comparisons varies based on what is in the list and their current order. Some lists are almost already in order while others are in reverse order or random order.

The function "foobar" (or "cool_comp" in the example above) as an optional "key" function for the "sorted" feature should be defined to accept two parameters exactly.

Often sorting involves alphabetizing or ordering numbers in a sequence from small to large. However some sorting for complex use-cases may not be this trivial. Thus keys are involved for handling a composite number of smaller components/inputs for sophisticated sorting beyond ordering numbers or letters in default fashions. Incorporating a second component, such as a sequence of integers to the items in the array, can facilitate tracking whether the sorting algorithm is "stable." A sorting algorithm is said to be "stable" if it preserves the original order in the case of two equivalent keys. Some keys are not unique and their corresponding elements are how a person knows if the original order is preserved after the sorting of the array.

In some cases the key is very complex -- handling multiple data points with a formula that is based on a computation involving these different factors. With these types of keys, swapping the position of two elements in a list is a faster and less resource intensive operation than comparing the two elements. In these cases, selection sort may be an optimal/performant solution (per page 145 of Programming Interviews Exposed). We believe that implementing a sorting algorithm beyond the sorted reserved word and including a key, such as coding selection sort, would be rare in a business environment but possible in an interview.

Advertisement
If you want to purchase a book on Python, see this posting.

How are Provisioners in Terraform Different from User Data?

Question
How are Terraform provisioners different from Terraform User Data? Both usually involve scripts to customize a server.

Answer
Provisioners have built-in support for CM tools and can have scripts longer than 16 KB; these things are not true with User Data. (This was taken from page 213 of Terraform: Up & Running, 2nd Edition by Yevgeniy Brikman (O'Reily), Copyright 2019, 978-1-492-04690-5.) User Data is recommended unless you have more than 16 KB of scripts or if you want to deploy/leverage configuration management tools.

How Do You Sleep when It Is Light Out?

Problem scenario
You live in a northern latitude and it stays light until late at night and gets light out early in the morning. How do you sleep under these conditions?

Solution
Background information: Sleep is important. Research says you should try to get more than eight hours of sleep.

Buy a high quality sleep mask (as opposed to a very affordable one). There is a difference in the usefulness and comfort; if you have only used inexpensive ones, you may want to try a new one.

If you travel regularly, you may want to buy a travel pillow or an inflatable pillow.

If you want tips on how to deal with noise, see this article.