Skip to content

Instantly share code, notes, and snippets.

View pford68's full-sized avatar

Philip Ford pford68

  • ex-IBM Watson
  • New York City and DC
View GitHub Profile
@pford68
pford68 / snippet.md
Created March 6, 2026 02:55
How to merge two dictionaries in Python #python
  • As of Python 3.9, you can use the | symbol to merge them into a new dictionary. dict3 = dict1 | dict2
  • Or you can use |= to copy the right operand into the left operand. dict1 |= dict2

The older alternative is to use the unpack operator (**) which works (like the spread operator in JavaScript. Unpack of the dictionaries into a new dictionary, as shown below.

dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "d": 4}
merged_dict = {**dict1, **dict2}
@pford68
pford68 / snippet.md
Created March 6, 2026 02:45
Reversing a string in Python #python

This example identifies palindromes. It reverses the string, copying the reversed string toa new string, then compares the two strings. It uses the step-slice to reverse the string: [::-1].

  1. a blank start and stop to make a slice of the entire string.
  2. a negative step to reverse the chararcters.
text = "madam"
text2 = text[::-1]
if text == text2:
    print("Palindrome")
else:
@pford68
pford68 / snippet.md
Created March 6, 2026 00:00
Printing a tuple in reverse order #python
a = (1, 2, 3, 4, 5)
for i in range(len(a) - 1, -1, -1):
    print(a[i])
@pford68
pford68 / snippet.md
Last active March 5, 2026 23:42
Sorting a dictionary by value in Python #python #sorting

Below we sort a dictionary, then find the key with the highest value.

d = {'a': 10, 'b': 20, 'c': 30}
sorted_items = sorted(d.items(), key=lambda item: item[1])
print(sorted_items[-1][0])
  1. Get items() from the dictionary.
  2. Pass the to sorted and pass a lambda to in key parameter. This tells the sorted() function whhat to sort on.
  3. Get the last entry in the sorted list of entries.
@pford68
pford68 / article.md
Last active August 26, 2025 18:48
How to create a password-encrypted ZIP file on a Mac #zip #utilities

Summary

To password-protect a ZIP file on a Mac, use the Terminal to run the command zip -er archive.zip folder/, replacing archive.zip with your desired file name and folder/ with the folder or files you want to compress. After entering the command, you'll be prompted to type and verify your password, which will then protect the ZIP archive.

Step-by-step Guide

  1. Open Terminal: Search for and open the Terminal application on your Mac (found in Applications > Utilities).
  2. Navigate to your files: Use the cd command to change the directory to where your files or folder are located. For example, to go to your desktop, type cd Desktop and press Enter.
  3. Enter the command: Type the following command into Terminal, pressing Enter after each part:
@pford68
pford68 / snippet.md
Last active May 15, 2025 04:32
How to delete containers #docker

To delete a Docker container, you first need to stop it, then remove it using the docker rm command. You can use the container ID or name to identify the container you want to remove. For example, if you want to remove the container with the ID "abcdef123", you can use the command docker rm abcdef123.

  1. Stop the container If the container is running, you need to stop it first using the docker stop command.
  • For example: docker stop abcdef123.
  1. Remove the container Once the container is stopped, you can remove it using the docker rm command.
@pford68
pford68 / snippet.md
Created September 5, 2024 01:48
Logging JPA queries in Spring Boot #springboot

In order to inspect queries in the console, add the following property in the application.properties file:

spring.jpa.show-sql=true
@pford68
pford68 / snippet.md
Last active August 18, 2024 16:52
How to use FormData to loop through form values #javascript

Below I am using FormData within a React component to print all element names:

    useEffect(() => {
        const el = formRef.current;
        if (el == null) return;
        // @ts-expect-error: keys() is not recognized.
        const iter =  new FormData(el).keys();
        let result = iter.next();
 while (!result.done) {
@pford68
pford68 / snippet.md
Created August 3, 2023 03:01
Add changes to your last commit #git

We have all been in a situation where we have forgotten one little change, for which we have to make a new commit. When this change is not so huge, we can add it into our last commit using the -—amend flag.

git add .
git commit --amend --no-edit

Using the —-no-edit flag allows us to apply changes to our last commit without modifying the commit message.

@pford68
pford68 / snippet.md
Created August 3, 2023 02:59
Remove file from a commit #git

If we would like to remove a certain file that has been committed to a branch, we can use git reset command.

git reset --soft HEAD^

This will bring the committed files to the staging area and then we can specify exactly which file to remove.

git reset HEAD