Skip to content

Instantly share code, notes, and snippets.

View sloanlance's full-sized avatar
Certified GitHub Pro

Mr. Lance E Sloan sloanlance

Certified GitHub Pro
View GitHub Profile
@sloanlance
sloanlance / clone-private-github-repo-in-google-colab.ipynb
Last active July 16, 2024 04:28
clone-private-github-repo-in-google-colab.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sloanlance
sloanlance / unicode-variables.py
Last active December 20, 2023 02:29
Python's handling of Unicode-character variables is surprising!
𝐀 = {'😀': 5}
for ℹ︎ in range(20):
# print(i, ℹ︎, i == ℹ︎) # NameError: name 'i' is not defined.
for _,__ in 𝐀.items():
if __ == ℹ︎:
print(_, ℹ︎)
print(𝐀) # Prints dictionary in 𝐀
print(A) # Also prints dictionary in 𝐀!
print(𝐀 is A) # True
@sloanlance
sloanlance / indexformatter1.py
Last active November 1, 2023 18:45
A Python class before and after optimization, as inspired by GitHub Copilot.
class IndexFormatter:
def __init__(self, volumeId: int):
self.itemPages = (
ItemPage.objects.filter(volume__id=volumeId)
.order_by('item__topic__name', F('page') * 1, 'item__name'))
def format(self) -> str:
"""
Format the index for printing.
"""
@sloanlance
sloanlance / pandas-read-csv-with-other-encodings.ipynb
Last active October 24, 2023 18:50
pandas-read-csv-with-other-encodings.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sloanlance
sloanlance / Say Goodbye to Loops in Python, and Welcome Vectorization.md
Last active October 3, 2023 16:03
Say Goodbye to Loops in Python, and Welcome Vectorization!

Say Goodbye to Loops in Python, and Welcome Vectorization!

Use Vectorization — a super fast alternative to loops in Python

By Anmol Tomar on CodeX

Nov 29, 2022

Introduction

@sloanlance
sloanlance / jq Notebook.md
Created September 29, 2023 15:18
Useful notes about using jq

jq Notebook

Useful notes about using jq.

  1. Print all paths to values
    • jq -c paths Prints each path as an array and doesn't show values.
    • jq --stream 'select(.[1]|scalars!=null) | "\(.[0]|join(".")): \(.[1]|tojson)"' example.json
      Nearly perfect. If each path shown began with . and array indices were enclosed in square brackets, then each one would be a valid jq query string.
@sloanlance
sloanlance / yq Notebook.md
Last active September 29, 2023 14:35
Useful notes about using yq

yq Notebook

Useful notes about using yq

  1. Display the path to each value in a YAML file
    $ cat << EOF | yq e '.. | select(. == "*") | {(path | join(".")): .} ' -
    > a:
    >   b: foo

> c: bar

@sloanlance
sloanlance / git-timestamps.sh
Created May 22, 2023 15:08
Set timestamps of files in a cloned git repository to when they were last committed.
#!/bin/sh --
# Set timestamps of files in a cloned git repository to when they were last committed.
# Default: All git-controlled files in current directory, recursively
if [ ${#} = 0 ]; then
files="$(git ls-files -z | xargs -0 echo)"
else
files="${@}"
fi
@sloanlance
sloanlance / IPA Reader Lambda (S3).py
Created April 4, 2023 16:10 — forked from katie7r/IPA Reader Lambda (S3).py
IPA Reader Lambda function (with S3 storage of saved audio files)
import boto3
import os
from contextlib import closing
from botocore.exceptions import ClientError
def object_exists(s3, bucket, key):
try:
boto3.resource('s3').Object(bucket, key).load()
return True
@sloanlance
sloanlance / bgsounds.sh
Last active March 5, 2024 20:06
bgsounds.sh: Toggle macOS Background Sounds on or off.
#!/bin/sh --
# Toggle macOS Background Sounds on or off.
#
# See the System Settings panel for Accessibility > Audio to change settings
# like the sound played, volume level, and automatic stop.
# `read` returns an integer, but…
if [ $(defaults read com.apple.ComfortSounds 'comfortSoundsEnabled') = 0 ]
then