How Do You Write Python Code to Test if a Dictionary Exists?

Problem scenario
You want to test if a dictionary exists or not. You know its name if/when it exists. What do you do?

Solution

# Suggested usage: 1) run it is as it is.
# 2) uncomment out the dictionary_name1 definition stanza.
# Then run this program again.

#dictionary_name1 = “good_dict”

if ‘dictionary_name1’ in locals():
print(“IT IS IN LOCALS”)
else:
print(“IT IS NOT IN LOCALS”)

if ‘dictionary_name1’ in globals():
print(“IT IS IN GLOBALS”)
else:
print(“IT IS NOT IN GLOBALS”) …

How Do You Troubleshoot a Python MapReduce Job That Returns “Error: java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed with code 1”?

Problem scenario
Your Python MR (mapreduce) job is failing. You do not know why.

You may or may not get an error like this: “Error: java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed with code 1”

Other symptoms you may experience is the MapReduce job takes a great deal of time. It seems to hang. What should you do?

Solution

  1. Determine which map .py file you are using and which reduce .py file that you are using.

Using Python How Do You Convert Each Line to Be an Item in a List?

Problem scenario
You have a multi-line variable in Python. You want to convert each line to be its own item in a list. How do you make a multi-line string variable to become a list with the same contents?

Solution
Use the .splitlines() method to manipulate the string in this way.

Here is an example:

list_of_input=”””a line here
another line there. …

How Do You Troubleshoot the Cargo Problem “error[E0658]: use of unstable library feature ‘str_strip’: newly added”?

Problem scenario
You run a cargo command. You get this error:

error[E0658]: use of unstable library feature ‘str_strip’: newly added
–> /home/jdoe/.cargo/registry/src/github.com-1ecc6299db9ec823/ethbloom-0.10.0/src/lib.rs:69:1
|
69 | / construct_fixed_hash! {
70 | | /// Bloom hash type with 256 bytes (2048 bits) size.
71 | | pub struct Bloom(BLOOM_SIZE);
72 | | }
| |_^
|
= note: see issue #67302 https://github.com/rust-lang/rust/issues/67302 for more information
= note: this error originates in a macro (in Nightly builds,

How Do You Solve “Could not find foobar in any of the sources” after a bundle Command?

Problem scenario
You run a bundle command (e.g., bundle exec foobar create). It fails with an error such as “Could not find foobar in any of the sources”.

What should you do?

Solution
Remove the relevant Gemfile.lock file (e.g., in the directory you are running bundle) if you are allowed to. Are you using a variety of bundle commands?

What Does the “^” Operator in Python Do?

Problem scenario
In Python you try operations like these two:

5 ^ 6
9 ^ 10

Both return “3”. Why is this?

Solution
The exclusive or (aka XOR) symbol “^” “copies the bit if it is set in one operand but not both.” (Taken from https://www.tutorialspoint.com/python/python_basic_operators.htm)

Here are some integers with their binary representation one space away:

3 11

5 101
6 110

9 1001
10 1010

5 is represented as 101.

How Do You Connect over Port 5986 on a Windows Server?

Problem scenario
Port 5986 is blocked from your Linux server to your Windows server. You have used the nmap command and see that it is filtered. You believe there are no intermediate firewalls or OS firewalls blocking this port. What should you do?

Solution
This assumes you have no firewall blocking port 5986 for incoming connections to the Windows server. nmap will report 5986 is filtered despite there being nothing blocking this port if wsman’s listener has not been properly configured.*

  1. Run this script on the Windows server:

$hostName = $env:COMPUTERNAME
$serverCert = New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -DnsName $hostName
Export-Certificate -Cert $serverCert -FilePath c:\vagrant\PsRemoting-Cert1.cer
Get-ChildItem c:\vagrant\PsRemoting-Cert1.cer
Enable-PSRemoting -Force
New-Item -Path WSMan:\localhost\Listener\ -Transport HTTPS -Address * -CertificateThumbPrint $serverCert.Thumbprint -Force

  1. That is it.

Where Can You Find the build_rust.sh Script?

Problem scenario
You want to use Rust and Incubed (the Blockchains in3 crate). You search for “build_rust.sh”, but you find irrelevant build-rust.sh files. How do you find the one with the underscore (or underbar) and not the hyphen/dash?

Solution
Here is the content:

#!/bin/sh
cd ..
mkdir -p rust/in3-sys/in3-core
mkdir -p rust/in3-sys/in3-core/c
cp -r c/CMakeLists.txt c/macro.cmake c/compiler.cmake c/docs c/src c/include rust/in3-sys/in3-core/c/
cp CMakeLists.txt rust/in3-sys/in3-core/
export UPDATE_IN3_BINDINGS=1
cd rust && …

How Do You Troubleshoot the Python Problem “UnicodeEncodeError: ‘ascii’ codec can’t encode character”?

Problem scenario
You are trying to print out a variable in Python. But you get an error like this: “UnicodeEncodeError: ‘ascii’ codec can’t encode character ‘\xa0’ in position 70567: ordinal not in range(128)”

What should you do?

Solution
Root cause: A variable needs to be encoded as UTF instead of the default ASCII encoding.

Procedures
Assuming that var1 is a variable holding the results of a requests.get(url),

How Do You Troubleshoot the C Error “No such file or directory…compilation terminated”?

Problem scenario
You try to compile a C program, but it fails with “compilation terminated.” The error message says “foobar/foobar.h No such file or directory.” What should you do?

Solution
This solution does not address where should header files for c go. But it addresses the problem.
Find where foobar.h is: sudo find / -name foobar.h

Run the gcc command again,