Skip to content

Instantly share code, notes, and snippets.

🗃
Archiving all the things!

Nick Sweeting pirate

🗃
Archiving all the things!
Block or report user

Report or block pirate

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@pirate
pirate / alfred-clipboard.sh
Last active Jul 17, 2019
Script to manage searching, backing up, and collecting infinite clipboard history from the Alfred Clipboard History on macOS.
View alfred-clipboard.sh
#!/usr/bin/env bash
# This is a script that provides infinite history to get around Alfred's 3-month limit.
# It works by regularly backing up and appending the items in the alfred db to a
# sqlite database in the user's home folder. It also provides search functionality.
# Example Usage:
# alfred-clipboard.sh backup
# alfred-clipboard.sh status
# alfred-clipboard.sh shell
# alfred-clipboard.sh dump > ~/Desktop/clipboard_db.sqlite3
View chronosync_heart.py
def get_master_from_obj_id(org, obj_type: str, obj_id: str):
from sync.models import Master, Version
try:
version = Version.objects.filter(
source__organization_id=org.id,
obj_type=obj_type,
obj_id=obj_id,
mapping__isnull=False,
mapping__master_id__isnull=False,
@pirate
pirate / get_python_function_name.py
Created Jul 9, 2019
Efficiently return the full module path and function name of the calling function in Python e.g. "app.module.class.method"
View get_python_function_name.py
import sys
def current_function_name(above=1):
"""returns the calling function's __module__.__name__"""
# https://gist.github.com/JettJones/c236494013f22723c1822126df944b12#gistcomment-2962311
frame = sys._getframe()
for frame_idx in range(0, above):
frame = frame.f_back
@pirate
pirate / strip_bad_filename_characters.sh
Last active Jul 8, 2019
Bash script to remove accents and special characters from filenames recursively.
View strip_bad_filename_characters.sh
#!/usr/bin/env bash
# Recursively remove all special characters from filenames by renaming them to their ASCII normalized forms.
#
# By default it does a dry run, to actually move the files uncomment the `mv -vi ...` line.
#
# This is useful for cleaning up network shares that will be shared via SMB/NFS between Unix/macOS/Windows
# where non-ASCII filenames can sometimes cause "file does not exist" errors when trying to access the files.
#
# This script removes leading/trailing whitespace in filenames and replaces accents and non-english
# characters with their ASCII equivalent, if no ASCII equivalent exists, it removes the character e.g.:
@pirate
pirate / docker-compose-backup.sh
Last active Jul 3, 2019
Backup a docker-compose project, including all images, named and unnamed volumes, container filesystems, config, logs, and databases.
View docker-compose-backup.sh
#!/usr/bin/env bash
# Fully backup a docker-compose project, including all images, named and unnamed volumes, container filesystems, config, logs, and databases.
project_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
cd "$project_dir"
project_name=$(basename "$project_dir")
backup_time=$(date +"%Y-%m-%d_%H-%M")
backup_dir="$project_dir/data/backups/$backup_time"
@pirate
pirate / django_turbo_response.py
Last active Jul 5, 2019
An extended HTTPResponse class for Django 2.2 adding support for streaming partial template responses incrementally, preload headers, HTTP2 server push, CSP headers, running post-request callbacks, and more (fully typed).
View django_turbo_response.py
"""
This is an extended HTTP response class for Django >=2.0 that adds lots of fancy features.
It's most useful when needing to accellerate slow view functions that return templates,
but it can be used anywhere where you need to return an HTTPResponse() or render(...).
It works by subclassing the builtin Django HttpResponse and StreamingHttpResponse,
and adding improvements in many areas, including functionality, speed, and security.
The most up-to-date version of this code can be found here:
View enforce_types.py
# This is a decorator that checks the function parameters as runtime against their type hints.
#
# @enforce_types
# def example(a: str, b: int):
# return 'Success!'
#
# if __name__ == '__main__':
# example('abc', 123)
# example(123, 'abc') # this throws a TypeError
View socket.py
NoneType = type(None)
JsonValue = Union[str, int, float, bool, NoneType]
SocketMessage = Dict[str, JsonValue]
socket_routes = {
'/table/<table_id>/': [PresenceManager, TimeSync, PokerTable],
'/tournament/<table_id>/': [PresenceManager, Tournament, PokerTable],
'/speedtest/': [SpeedTest],
}
View after_response.py
"""Run some code after the response is returned to the user in django
Great for analytics or other slow tasks that you don't want to block the response
to the user for.
"""
class HttpResponseWithCallback(HttpResponse):
def __init__(self, *args, **kwargs):
self.callback = kwargs.pop('callback')
super().__init__(*args, **kwargs)
You can’t perform that action at this time.