Skip to content

Instantly share code, notes, and snippets.

View theScottyJam's full-sized avatar

Scotty Jamison theScottyJam

View GitHub Profile
@theScottyJam
theScottyJam / new.py
Last active September 27, 2021 05:13
This adds a "new file" menu item to the nautilus right-click menu for when you right click on a file. Place this file inside `~/.local/share/nautilus/scripts` and name it `new`. This script is useful for when you are using nautilus in list view, because nautilus currently doesn't show this menu item when right clicking on files.
#!/usr/bin/env python3
# https://superuser.com/a/1251013/57284
# https://gist.github.com/theScottyJam/ae19299c8d71a21c75f2abd96daea9f8
from pathlib import Path
import tkinter as tk
from tkinter import ttk
import os
import shutil
@theScottyJam
theScottyJam / exceptions.js
Last active October 13, 2021 23:08
Light version of the explicit-exceptions API. Designed for those would rather maintain a simplified version of the library than add it as a dependency. This gist is licensed under the MIT license.
'use strict'
// This module provides a light version of the explicit-exceptions API.
// You can find the original package this module was modeled after
// at this URL: https://www.npmjs.com/package/explicit-exceptions
/**
* An exception that can be thrown (or rethrown) within a function decorated with wrap().
*/
class Exception extends Error {
@theScottyJam
theScottyJam / publicHelpers.js
Created October 25, 2021 13:58
Article: Composition Alone Can't Replace Inheritance - public helpers
// Public helpers
export const monsterBehaviors = {
kill(monster) {
userInventory.add(monster.dropLoot())
removeFromUi(this)
}
}
// private helpers
const monsterHelpers = {
@theScottyJam
theScottyJam / runtimeHierarchyCheck.js
Created October 25, 2021 13:59
Article: Composition Alone Can't Replace Inheritance - runtime hierarchy check
const monsterSentinel = {}
export const isMonster = obj => obj.typeSentinel === monsterSentinel
export class Slime {
typeSentinel = monsterSentinel
...
}
export class Skeleton {
@theScottyJam
theScottyJam / originalInheritanceExample.js
Last active October 25, 2021 14:02
Article: Composition Alone Can't Replace Inheritance - original inheritance example
export class Monster {
constructor() {
if (this.constructor === Monster) {
throw new Error('This is an abstract class. Do not instantiate it directly.');
}
}
attack() {
playHurtAnimation()
}
@theScottyJam
theScottyJam / classicComposition.js
Last active November 10, 2021 15:14
Article: Composition Alone Can't Replace Inheritance - classic composition
class MonsterBehaviors {
#dropLoot
constructor({ dropLoot }) {
this.#dropLoot = dropLoot
}
attack() {
playHurtAnimation()
}
@theScottyJam
theScottyJam / restrictedInheritance.js
Last active November 10, 2021 15:16
Article: Composition Alone Can't Replace Inheritance - restricted inheritance
const monsterHelpers = {
attack() {
playHurtAnimation()
}
}
class MonsterBehaviors {
#id = Math.random()
#dropLoot
@theScottyJam
theScottyJam / strategyPattern.js
Last active November 10, 2021 15:40
Article: Composition Alone Can't Replace Inheritance - strategy pattern
class Monster {
#onAttack
constructor({ dropLoot, onAttack, customBehaviors = {} }) {
this.dropLoot = dropLoot
this.#onAttack = onAttack ?? (() => {})
this.behaviors = customBehaviors
}
attack() {
playHurtAnimation()
@theScottyJam
theScottyJam / helperFunctions.js
Last active July 19, 2022 14:44
Article: Composition Alone Can't Replace Inheritance - helper functions
const monsterBehaviors = {
attack() {
playHurtAnimation()
},
die({ monsterId, dropLoot }) {
userInventory.add(dropLoot())
removeFromUi(monsterId)
},
}
@theScottyJam
theScottyJam / exampleData.ts
Created January 31, 2023 02:00
Example of using the return-your-exceptions pattern
import type { User } from './userManager';
export const users: Map<string, User> = new Map(Object.entries({
'1': {
id: '1',
username: 'admin',
permissions: ['VIEW', 'UPDATE'],
},
'2': {
id: '2',