Skip to content

Instantly share code, notes, and snippets.

View wcDogg's full-sized avatar

wcDogg wcDogg

  • Philadelphia, PA
View GitHub Profile
@wcDogg
wcDogg / .md
Last active August 22, 2019 23:31

WordPress: Goggle Analytics Opt-Out

I have a modest blog. One day I might care about Google Anaylitcs. For now, my basic needs are:

  • Add a GA global tag to the site head.
  • Present users with a way to opt-out of GA for my site.
  • Display this option on the Privacy Policy page - Settings > Privacy.

For this, I'm using:

Python: Logging to Files

How to set up Python logging and write log files to an OS-appropriate directory using dictconfig and appdirs.

pip

python -m pip install appdirs
@wcDogg
wcDogg / python-task-timer.md
Last active September 18, 2022 23:22
A Python utility class to capture a task's start, stop and elapsed datetimes.

Python Task Timer

A Python utility class to capture a task's start, stop and elapsed datetimes.

TaskTimer.py

from datetime import timedelta
from timeit import default_timer as timer
from time import strftime, localtime, sleep
@wcDogg
wcDogg / FizzBuzz-Python.md
Last active January 21, 2023 06:59
FizzBuzz in Python

FizzBuzz in Python

Like 3M other people inspired by Tom Scott's FizzBuzz: One Simple Interview Question video, here is my 20 minute stab at FizzBuzz. I imagine a programmer would nail this in under 10 :P

I did it this way because I was curious about the different patterns variable inputs would create. The results remind me of The Rythm of Primes :)

class FizzBuzz:
  '''Classic children's game in Python.
  '''
@wcDogg
wcDogg / bash-prompt-create-password.md
Last active September 7, 2022 04:18
Bash: Create Password Prompt

Bash: Create Password Prompt

  • Prompt user to create and confirm a new password.
  • Cannot be blank.
  • Hide user's entry.
while :     
do
 read -rs -p "${QUEST} Admin PW: " NETWORK_PW && echo

Python: Simple Logging

# module.py
import logging
logging.basicConfig(level="DEBUG", format='%(asctime)s : %(levelname)s : %(name)s : %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
log = logging.getLogger(__name__)

Python: Catch Warnings with Try Except

Today I needed to handle a Pillow warning. Many posts demonstrate how to treat all warnings as exceptions so they can be caught with try and exept. I quickly discovered that catching all warnings can be problematic. Here's how to catch specific warnings:

import warnings
warnings.filterwarnings("error", category=Image.DecompressionBombWarning)

def process_images():
 try:
@wcDogg
wcDogg / python-appdirs.md
Last active August 30, 2023 16:26
How to locate OS-appropriate directories for storing app data on a user's computer in Python.

Python: Locate OS Paths for Common App Directories

appdirs helps you locate OS-appropriate directories for storing app data on a user's computer - user, site, caxhe, log. I use appdirs to write log files.

python -m pip install appdirs

appdirs Paths

@wcDogg
wcDogg / python-list-files-in-directory.md
Last active January 21, 2023 06:28
An OS-agnostic way to list files in a directory using Python.

Python: List Files in a Directory

from os import listdir
from pathlib import Path

dir_start: Path = "D:\Media\Movies"

for f in listdir(dir_start):
 print(f)