Skip to content

Instantly share code, notes, and snippets.

View DArmstrong87's full-sized avatar
Software Engineer: Python/Django, React, Java/SpringBoot

Daniel Armstrong DArmstrong87

Software Engineer: Python/Django, React, Java/SpringBoot
View GitHub Profile
@DArmstrong87
DArmstrong87 / validateDomain.js
Created June 9, 2025 16:39
Validate domains and subdomains
const validateDomain = (domain) => {
const domainRegex = /^[A-Za-z0-9-]{1,63}(\.[A-Za-z0-9-]{1,63})*\.[A-Za-z]{2,6}$/mig;
return domainRegex.test(domain)
}
@DArmstrong87
DArmstrong87 / migrate.py
Last active June 10, 2025 20:51
Django concurrency safety during migrations
"""
Ensures only one instance runs migrations when multiple instances are spun up concurrently.
This prevents a deadlock scenario.
"""
from django.core.management.base import CommandError
from django.db import connection
from django.core.management.commands.migrate import Command as MigrateCommand
@DArmstrong87
DArmstrong87 / binary_search_4_billion_ints.py
Created May 29, 2025 02:47
Binary Search of 4 Billion Integers
import random
from time import time
def binary_search(int_list: range, number: int) -> tuple[str | None, int]:
"""
Return index and number of iterations of a number in range
RANDOM NUMBER 940_695_391
Time to complete: 0.0069141387939453125ms
INDEX OF RANDOM NUMBER, ('940_695_391', 31)
@DArmstrong87
DArmstrong87 / validators.py
Created May 9, 2025 16:02
Password Validators
"""
# Password validation
# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators
# Your password can’t be too similar to your other personal information.
# Your password must contain at least 10 characters.
# Your password can’t be a commonly used password.
# Your password can’t be entirely numeric.
# Your password must contain at least 1 symbol: ()[]{}|\`~!@#$%^&*_-+=;:'",<>./?
# Your password must contain at least 1 number, 0-9.
@DArmstrong87
DArmstrong87 / dynamic_rate_limit.py
Created May 9, 2025 15:54
Dynamic Rate Limit Decorator
"""
To use this decorator, add it to the view function you want to rate limit.
from authentication.decorators import dynamic_rate_limit
@dynamic_rate_limit()
def login_view(request):
The values can be changed based on a Config model that stores values in json.
"""
@DArmstrong87
DArmstrong87 / test_form_selenium.py
Created May 9, 2025 15:38
Django Test with Selenium
import time
from unittest import skipIf
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.conf import settings
from django.test.client import Client
# Selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
@DArmstrong87
DArmstrong87 / FlywayMigrationConfig.java
Last active April 7, 2025 19:27
This class runs Flyway migrations upon application startup
package com.myproject.mypackage.config;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@DArmstrong87
DArmstrong87 / run_repos.applescript
Created December 8, 2023 21:10
Shell executable to run an Applescript in iTerm2 which opens repos in separate tabs, pulls latest code and runs a command for each repo
on run argv
local basePath
set basePath to item 1 of argv
tell application "iTerm2"
create window with default profile
end tell
set repos to {"repo-1", "repo-2", "repo-3"}
@DArmstrong87
DArmstrong87 / custom_filter_truncate_with_ellipses.py
Created December 8, 2023 15:23
Custom Django template filter: Confine a string to a max length, replace last three characters with ellipses
from django import template
register = template.Library()
@register.filter(name="truncate_with_ellipses")
def truncate_with_ellipses(value: str, max_length: int) -> str:
"""
Confine a string to a max length, replace last three characters with ellipses
"""
if len(value) <= max_length:
return value
@DArmstrong87
DArmstrong87 / custom_filter_user_in_group.py
Last active December 8, 2023 15:17
Custom django template filter to check if user is in allowed groups
from django import template
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
register = template.Library()
User = get_user_model()
@register.filter(name="user_in_group")
def user_is_in_group(user: User, allowed_groups: str) -> bool:
"""