How Do You Troubleshoot the Boto3 Error “AttributeError: ‘ec2.ServiceResource’ object has no attribute ‘terminate_instances'”?

Problem scenario
You are trying to terminate some EC-2 instances. But you see a message like this:

"AttributeError: 'ec2.ServiceResource' object has no attribute 'terminate_instances'"
"AttributeError: 'ec2.ServiceResource' object has no attribute 'destroy_instances'"
"AttributeError: 'ec2.ServiceResource' object has no attribute 'delete_instances'"

What should you do?

Solution
Rewrite the terminate portion to look like this:

ec2.instances.filter(InstanceIds=['i-0abcd1234', 'i-0wxyz9999']).terminate()

How Do You Use Boto and CloudFormation to Delete a Stack?

Problem scenario
You want to delete a CloudFormation Stack with Boto. What do you do?

Solution
Run a program like this (but replace "continualstack" with the name of the stack you deployed):

import boto3
cloudf = boto3.client('cloudformation', region_name='us-west-1', aws_access_key_id='AKIAMNOPQRST', aws_secret_access_key='FOOBAR1234/ZYXWV987654')
json_stack=open('lampstack.json', 'r').read()
cloudf.delete_stack(StackName='continualstack')

How Do You Eliminate Old Email Settings in Mozilla Thunderbird on Windows?

Problem scenario
You keep opening Mozilla Thunderbird in Windows, and it keeps finding the old settings. You want a fresh installation. What should you do?

Solution
Overview: Totally remove Thunderbird and reinstall it.

Procedure

  1. Close Thunderbird
  2. Install CCleaner. It can be downloaded from here: https://www.ccleaner.com/ccleaner/download. (The free version will work.)
  3. Open it.
  4. Go to Tools -> Uninstall.
  5. Find Mozilla Thunderbird and highlight it.
  6. Click Uninstall.
  7. After it uninstalls, click "Custom Clean".
  8. Make sure nothing is checked in the Windows tab.
  9. Click on the "Applications" tab.
  10. Click on every option under Thunderbird.
  11. Click "Run ccleaner"
  12. Go to C:\Users\jdoe\AppData\Roaming\Thunderbird (where jdoe is the username that has the old Thunderbird settings). Delete this folder.
  13. Go to C:\Users\jdoe\AppData\Local\Thunderbird (where jdoe is the username that has the old Thunderbird settings). Delete this folder.
  14. Reboot your computer.
  15. Reinstall Thunderbird and configure it again.

How Do You Use Boto to Deploy a CloudFormation Stack?

Problem scenario
You want to use Boto to deploy a CloudFormation stack. Where can you find an example of a Python program that deploys a CloudFormation stack?

Solution
Prerequisites

This assumes you have installed Boto. If you need assistance, see this posting.

Procedures

  1. Create a .json template file. For this proof of concept, you can use this file.
  2. Run a program like this:
import boto3
cloudf = boto3.client('cloudformation', region_name='us-west-1', aws_access_key_id='AKIAabcdefgh12345', aws_secret_access_key='SOMETHING1234here/moretext')
json_stack=open('lampstack.json', 'r').read()
cloudf.create_stack(StackName='continualstack', TemplateBody=json_stack, Parameters = [ { 'ParameterKey': 'KeyName', 'ParameterValue': 'foobar'}, {'ParameterKey': 'DBPassword', 'ParameterValue': 'verycont'}, {'ParameterKey': 'DBUser', 'ParameterValue': 'contuser'}, {'ParameterKey': 'DBRootPassword', 'ParameterValue': 'contpassword'}] )

How Do You Troubleshoot the Python Errors “NameError: name ‘copy’ is not defined” or “object has no attribute ‘copy'”?

Problem scenario
You are running a Python program. But you get "NameError: name 'copy' is not defined" or "object has no attribute 'copy'". What should you do?

Solution
Use "import copy" at the top of the program.

How Do You Troubleshoot the Hadoop Error “ApplicationClientProtocolPBClientImpl.getApplicationReport”?

Problem scenario
You are running a Hadoop command, but you get this message:

java.net.ConnectException: Your endpoint configuration is wrong; For more details see: http://wiki.apache.org/hadoop/UnsetHostnameOrPort, while invoking ApplicationClientProtocolPBClientImpl.getApplicationReport over null after 3 failover attempts. Trying to failover after sleeping for 22088ms.
2020-12-20 18:54:36,679 INFO ipc.Client:

What should you do?

Possible Solution
Is Resource Manager running? Start a new terminal and run "jps" to find out. Often Resource Manager is started with a Yarn script like this: bash /usr/local/hadoop/sbin/start-yarn.sh

How Do You Determine the GitHub Enterprise URL for the API Endpoint?

Problem scenario
You want to run some curl commands to invoke a GitHub Enterprise URL. You tried using "https://api.github.foobar.com/…", but you get a message about the URL not resolving. How can you find out what the API endpoint is?

Possible Solution
Try "https://github.foobar.com/api/v3/…" instead. Although Github.com uses the "api" immediately after the "https://" constructor (and to the left of the "github..." section), your GitHub Enterprise instance may use "http(s)://[hostname]/api/v3" (with the "api" to the right of the hostname in the URL constructor).

To learn more, you may want to see this: https://developer.github.com/enterprise/2.20/v3
Or this: https://docs.github.com/en/enterprise-server@2.20/rest/overview/resources-in-the-rest-api

How Do You Write a Python Program to Find if The “Read more” Preview on a Web Page is Visible for Every Web Page Previewed?

Problem scenario
Sometimes the "Read more" or "Continue reading" hyperlink in the page preview of your website (e.g., when you search for a word), is not visible. You know that some of your web page previews on your website don't have the "Read more" hyperlink at the end. This makes the preview appear to have the entire web page. You need to identify the postings that don't have the "Read more." How do you use Python to find the pages that have previews that have no "Read more" hyperlink?

Solution

# Change the "http://www.domainname.com/miscellaneous/page/" to the URL that will have the format http://www.domainname.com/miscellaneous/page/25 
# where 25 is the number of pages to iterate through.
# Change the "25" to be the page number associated with the highest url in this format: http://www.domainname.com/miscellaneous/page/25
# If you have a different number of page previews per page of search results than 10, change the 10 to the number your website uses.


import re
import requests
listofurls = []

tempstring = "http://www.domainname.com/miscellaneous/page/"
for i in range(25): 
  url = tempstring + str(i)
  listofurls.append(url)

searchterm = "Read more"

def finder(yoururl, searchterm):
    r = requests.get(yoururl)
    found = str(len(re.findall(searchterm, r.text)))
    if found:
      if found != '10':
        print("The string/pattern '" + searchterm + "' was found " + found + " times when searching " + yoururl)


for x in listofurls:
  finder(x, searchterm)

print("The format of the URL returned in the output will show you 25 (or 10 or whatever number you have) previews.  
You will have to manually find which posting has no "Read more" hyperlink.  We have found making a back up of the posting, deleting it completely, and reposting it as a new page to be the most effective way of making the "Read more" hyperlink appear.")


# You may also want to see "How Do You Get WordPress Preview Postings to Show ">> Read more..."?"

If the page preview hyperlink is not visible and you are using WordPress, see this posting.

How Do You Write a Python Function to Determine if There Are Three or More Unique Characters in a String?

Problem scenario
You want a function to tell you if there are three unique characters in a string. For example "ababababa" would have fewer than three unique characters. But "abc" would have three or more unique characters. How do you write a function that does this that solves in linear time?

Solution


variable_to_test = "aaaaaaaaaaaabbbbbb"

def two_unique(s):
   flag = "There are only two unique characters in the string!"
   dict_a = {}
   for string_portion in s:
     if string_portion in dict_a:
         pass
     else:
       dict_a[string_portion] = 1
   key_counter = 0
   for key in dict_a.keys():
     key_counter = key_counter + 1;
     if key_counter > 2:
       flag = True
       return flag
   return False


answer = two_unique(variable_to_test)
if(answer):
  print("There are more than two unique characters!")
else:
  print("There are two or fewer unique characters in the string!")
variable_to_test = "aaaaaaaaaaaabbbbbbc"

def two_unique(s):
   flag = "There are only two unique characters in the string!"
   dict_a = {}
   for string_portion in s:
     if string_portion in dict_a:
       dict_a[string_portion] = dict_a[string_portion] + 1
     else:
       dict_a[string_portion] = 1
   key_counter = 0
   for key in dict_a.keys():
     key_counter = key_counter + 1;
     if key_counter > 2:
       flag = True
       return flag
   return False


answer = two_unique(variable_to_test)
if(answer):
  print("There are more than two unique characters!")
else:
  print("There are two or fewer unique characters in the string!")