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!")

Why Are Python List Operations/Methods Unlike Python String Operations/Methods?

Problem scenario
List operations such as .sort() will change previous copies of the list.

String operations such as .replace("old_pattern", "new_pattern", count), will not change the variable or its copies; these operations will return a string after the replace operation has run on the original string.

Why is this behavior happening?

Solution
In-place changes and mutability are factors. Strings are generally immutable whereas copies of lists are usually shallow.

program1.py shows how lists are mutable with inplace changes happening to a copy; program2.py shows how strings are immutable and have no inplace changes to them when the .replace method is used.

# Filename: program1.py

one_list = ['dog', 'cat', 'horse', 'cow']
print(one_list)
print("Above is one_list before modifications have happened.")
copy_of_list = one_list
one_list.append('hamster')
one_list.sort()
print("The one_list was modified earlier in the execution of this program.")
print(one_list)
print("The one_list is above and the copy_list is below")
print(copy_of_list)
print("The .append and .sort methods change copies of the string in addition to the string itself.")
print("You may want to use deep copies instead of shallow copies if you don't want copies to be modified with the original.")

# Filename: program2.py:

first_string = "aaaaaaaaaaaaaaaaaabcdefgh"
second_string = first_string.replace('a', 'z', -1)
print("The replace method was invoked earlier in the execution of this program.")
print(" ")
print(first_string)
print("first_string is above; second_string is below")
print(second_string)

print("Now you can see how the .replace method only emphemerally returns a string after operating on it.")
print("The .replace method does not change the string it operates on.  The operations can be saved to a new variable if you want to use .replace.")

The replace method of a string does not modify the variable it operates on. It returns the modified string. With list manipulations, the copy of the list can be changed (unless you copy like this new_list = old_list[:]).

How Do You Troubleshoot a Kitchen Command?

Problem scenario
You are using Chef and the Kitchen command line tool. If a kitchen command fails, how can you find out the root cause?

Solution
With the command that fails, re-run it with this at the end: --log-level=debug

To learn more about the verbosity of kitchen commands, see this: https://docs.chef.io/workstation/ctl_kitchen/#options

How Do You Troubleshoot “SyntaxError: invalid syntax” in Python?

Problem scenario
You are running a Python script. You keep getting "SyntaxError: invalid syntax" and a line number. You can find nothing wrong with the syntax on the specified line number. What do you do?

Possible solution #1
Look at the line with code, as opposed to blank line(s), above it. Is there a missing quote, brace, bracket or parentheses?

Possible solution #2
Are you trying to use an "if" statement on one line? Make sure you have an "else" statement. Without an "else" clause, you can get "SyntaxError: invalid syntax". Here are four lines of code that are minimally correctly written:

f = 5
a = "unique"
x = f
print("scissors" if a == "unique" else "rocks")

# You could try to eliminate the 'else "rocks"' portion and run the program to see the error.

How Do You Close Your Apple ID Account?

Problem scenario
You have an iCloud, iTunes, iMessage, FaceTime, or some other Apple account online. You want to cancel your account and destroy or delete the data. What do you do?

Solution
1. Log into your Apple.com account (e.g., https://appleid.apple.com/#!&page=signin).
2. Go here: https://privacy.apple.com/
3. Go to "Request to delete your account"
4. Follow the prompts.


To browse Mac products, see this link.

How Do You Pass Two or More Subnets to an “aws eks” Command?

Problem scenario
You want to pass more than one subnet to an "aws eks" command.

You tried to delimit the list with commas (or separate two subnet IDs with commas). You received this error message:

An error occurred (InvalidParameterException) when calling the CreateNodegroup operation: The subnet ID 'subnet-0abcd1234,subnet-zyxw9876' does not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidSubnetID.NotFound; Request ID: Proxy: null)

What should you do?

Solution
Separate the subnet IDs with a space. Here is an example of the correct syntax:

aws eks create-nodegroup --cluster-name coolname --nodegroup-name "variousmachines" --subnets subnet-0abcd1234 subnet-zyxw9876 --node-role arn:aws:iam::<removed>:role/nameofroll --remote-access ec2SshKey=pairname,sourceSecurityGroups=sg-0securitygroup1,sg-0securitygroup2

# It is worth noting that the security groups are passed with commas and no spaces.
# But subnets are passed with spaces and no commas
# If you want to see what security groups you can choose from, run this command:
# aws eks describe-cluster --name contint --query 'cluster.resourcesVpcConfig.securityGroupIds'
# If you want to see what subnets you can choose from, run this command:
# aws eks describe-cluster --name foo --query 'cluster.resourcesVpcConfig.subnetIds'
# If you can remember a pattern in the name of the relevant role (i.e., "foobar"), try a command like this to find the exact IAM role:
# aws iam list-roles | grep -i foobar