Get Secret from AWS Secrets Manager in Python

You can store secrets in AWS Secret Manager and reference their ARN in AWS Systems Secret Manager. The below snippet allows you to specify the associated parameter to get the secret value. import boto3 ssm = boto3.client('ssm') secretsmanager = boto3.client('secretsmanager') parameter = ssm.get_parameter(Name="/your/parameter/name", WithDecryption=True) secret = secretsmanager.get_secret_value(SecretId=parameter['Parameter']['Value']) secret = json.loads(secret['SecretString']) print(secret)

July 8, 2021 · 1 min · 51 words · Andrew

How to do Binary Addition in Python

The challenge Implement a function that adds two numbers together and returns their sum in binary. The conversion can be done before or after the addition. The binary number returned should be a string. Examples: add_binary(1, 1) == "10" (1 + 1 = 2 in decimal or 10 in binary) add_binary(5, 9) == "1110" (5 + 9 = 14 in decimal or 1110 in binary) The solution in Python code There are multiple ways to solve an int to binary string problem in Python....

July 7, 2021 · 1 min · 132 words · Andrew

How to Auto-Adjust Excel column widths with pandas.ExcelWriter

You have successfully written your pandas Dataframe to an Excel file, but when you open it, all the columns are squashed up against each other. There is an easy fix to auto-adjusting your column widths. Auto Adjusting Column Widths in Pandas writer = pd.ExcelWriter('file.xlsx') df.to_excel(writer, sheet_name='sheetName', index=False, na_rep='NaN') for column in df: column_length = max(df[column].astype(str).map(len).max(), len(column)) col_idx = df.columns.get_loc(column) writer.sheets['sheetName'].set_column(col_idx, col_idx, column_length) writer.save()

July 6, 2021 · 1 min · 63 words · Andrew

How to Sort the Odd in Python

The challenge You will be given an array of numbers. You have to sort the odd numbers in ascending order while leaving the even numbers at their original positions. Examples [7, 1] => [1, 7] [5, 8, 6, 3, 4] => [3, 8, 6, 5, 4] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] => [1, 8, 3, 6, 5, 4, 7, 2, 9, 0] The solution in Python code Option 1:...

July 5, 2021 · 1 min · 192 words · Andrew

Calculate the Sum of the two lowest positive integers in Python

The challenge Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 positive integers. No floats or non-positive integers will be passed. For example, when an array is passed like [19, 5, 42, 2, 77], the output should be 7. [10, 343445353, 3453445, 3453545353453] should return 3453455. The solution in Python code Option 1: def sum_two_smallest_numbers(numbers): return sum(sorted(numbers)[:2]) Option 2: def sum_two_smallest_numbers(num_list): num_list....

July 4, 2021 · 1 min · 129 words · Andrew

Unique In Order in Python

The challenge Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements. For example: unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B'] unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D'] unique_in_order([1,2,2,3,3]) == [1,2,3] The solution in Python code Option 1: def unique_in_order(iterable): result = [] prev = None for char in iterable[0:]: if char !...

July 3, 2021 · 1 min · 178 words · Andrew

How to Change the Hostname on Linux

On Linux, you can change the hostname by using the hostnamectl command. How to Set the Hostname on Linux sudo hostnamectl set-hostname <new-hostname> For example: sudo hostnamectl set-hostname server1 At this point, you will need to logout and back in to see the changes have taken effect.

July 2, 2021 · 1 min · 47 words · Andrew

How to Deploy a Docker Container to Kubernetes

Once you have a Docker container, it is really simple to deploy it to Kubernetes. Generate a Deployment YAML kubectl provides a fantastic way to help generate deployment yamls. kubectl create deployment <app_name> --image=<some/image> --dry-run=client -o yaml > <deployment_file_name>.yaml Perform the Deployment Now that we have a deployment yaml, we can simply run: kubectl apply -f <deployment_file_name>.yaml What next? Next you might want to add a Service so that the deployment is available to the world....

July 1, 2021 · 1 min · 165 words · Andrew

How to Replace Characters with Alphabet Positions in Python

The challenge Given a string, replace every letter with its position in the alphabet. If anything in the text isn’t a letter, ignore it and don’t return it. "a" = 1, "b" = 2, etc. Example alphabet_position("The sunset sets at twelve o'clock.") Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11" (as a string)...

June 30, 2021 · 2 min · 241 words · Andrew

Returning Highest and Lowest in Python

The challenge You are given a string of space-separated numbers and have to return the highest and lowest number. Example: high_and_low("1 2 3 4 5") # return "5 1" high_and_low("1 2 -3 4 5") # return "5 -3" high_and_low("1 9 3 4 -5") # return "9 -5" Notes: All numbers are valid Int32, no need to validate them. There will always be at least one number in the input string....

June 29, 2021 · 1 min · 201 words · Andrew