How Do You Use Python to Create a Tuple with Extracted and Nested Values from JSON Files?

Problem scenario
You have some JSON files like this:

$cat first.json
{“bird”: {“name”: “singy”, “species”: “sparrow”, “amount”: “45”}}

$ cat second.json
{“bird”: {“name”: “flighty”, “species”: “seagull”, “amount”: “21”}}

Within the “bird” key there are other keys. How do you create a tuple with the species values?

Solution
Place first.json, second.json and the Python program below (called reader.py) in the same directory.

How Do You Display the VPC Peering Connections with Boto3?

Problem scenario
You want to list all the VPC Peering Connections for a specific region using Python.

You want the equivalent of aws ec2 describe-vpc-peering-connections

How do you show (retrieve or fetch) the VPC peering connections using Boto3?

Solution

import boto3
contint = boto3.client(‘ec2’)
var1 = contint.describe_vpc_peering_connections()
print(var1) …

Can You Use the MVC (Model-View-Controller) on Linux?

Question
Whenever you read about the MVC, it always involves Microsoft (e.g., and ASP or .NET). Is there a way to use the MVC on purely Linux?

Answer #1
Yes. The ASP.NET MVC framework of the model-view-controller is a Microsoft implementation of it. The MVC generically can be used as an architectural pattern using programming languages on a Linux server. To read more about it,

In Python, How Do You Call a Bound Function as a New Thread with the threading Module?

Problem scenario
You want to write a program to call a bound function in a new thread. How do you do this?

Solution
Run this program (e.g., python foobar.py):

from threading import Thread

class mighty:
def good():
print(“Good”)

def contint():
print(“Hello!”)

if __name__ == “__main__”:
thread = Thread(mighty.good())
thread.start()
thread.join()
print(“thread finished…exiting”) …

How Do You Assign a Variable in JavaScript from User Input?

Problem scenario
You want to read input and assign it as a variable in JavaScript. What should you do?

Solution
Create a .html file, saved directly on your desktop or in a /var/www/html/ directory on a web server, with the follow content to illustrate how to do this (then open it in a web browser):

<!DOCTYPE html<html<body<h2Use JavaScript to Change Text</h2<pThis example writes “Hello in JavaScript from Continualintegration.com!” into an HTML element with id=”demo”:</p<p id=”demo”</p<scriptvar foo = prompt(“Please enter input and press enter”); …

In Python How Is the List Function “remove” Different from “pop”?

Problem scenario
You want to know how remove is different from pop when manipulating a list in Python. What should you do?

Solution
Here are four facts which should help elucidate the differences.

  1. The remove keyword searches for a matching item in the list — not by its indexed, integer value; then the first matching item is removed.

Why Does a PHP Program Call to a Python Program Work from the Back-End but Not from the Front-End?

Problem scenario
In your PHP program, the Python binary is executing regardless of how the PHP program is run (e.g., from a command line with the php command or when downloaded from a web browser). The PHP program will launch Python programs if the PHP program is invoked from the back-end with PHP. But Python programs are not being interpreted by Python when a web browser loads the PHP file.

How Do You Troubleshoot the Python Problem “NameError: global name ‘var2’ is not defined”?

Problem scenario
You have a Python program that works, but you get an error when you run it. You get a message like this: “NameError: global name ‘var2’ is not defined”

Here is your source code:

def fun1(var1):
print(“this is how it works”)
var1 = var2
return var2
b = fun1(“check it out”)
print(b)

What should you do about this error?

Solution
A variable inside a function must be defined.

How Do You Troubleshoot a pom.xml File?

Problem scenario
You are trying to run your Java program from a .jar file. You get an error like this:

Error: Could not find or load main class target.contint-0.1.0.jar
Caused by: java.lang.ClassNotFoundException: target.contint-0.1.0.jar

What should you do?

Solution
Examine the mainClass stanza. The word to the left of the period should be the name of the package.