Skip to content

Instantly share code, notes, and snippets.

View dtaivpp's full-sized avatar

David Tippett dtaivpp

View GitHub Profile
# Courtesy of https://stackabuse.com/sorting-algorithms-in-python/
def partition(nums, low, high):
# We select the middle element to be the pivot. Some implementations select
# the first element or the last element. Sometimes the median value becomes
# the pivot, or a random one. There are many more strategies that can be
# chosen or created.
pivot = nums[(low + high) // 2]
i = low - 1
j = high + 1
# Occurances will track each time an offensive bit of code is found
# Its format will be:
# File Path, Function, Num Occurances
occurances = []
# Methods that need to be update
nogos = [
".SetFocus(",
".IsValid(",
".Clear",
@dtaivpp
dtaivpp / iterate_the_filetree.py
Last active December 16, 2019 13:48
Iterate a directory looking for certain TypeScript and JavaScript files
import os
walk_dir = 'C:\\Directory\\ForWalking\\'
# If the file path contains these we dont want them
# eg. C:\\Directory\\ForWalking\\node_modules will be ignored
exclusions = ["node_modules", "SolutionFiles", ".bin", "Test"]
# Array to store all our file paths
file_paths = []
import random
from time import time
random.seed(time())
#### List Access Tests ####
times = {'colsAndRows': 0, 'rowsAndCols': 0}
def generateArray(size):
import random
from time import time
random.seed(time())
times = {'listComprehension': 0, 'append': 0, 'preAllocate': 0}
for i in range(0,100):
endRange = random.randint(100_000, 10_000_000)
startTime = time()
class DiscordHandler(logging.Handler):
"""
Custom handler to send certain logs to Discord
"""
def __init__(self):
logging.Handler.__init__(self)
self.discordWebhook = DiscordWebhook(url=config.DISCORD_URL)
def emit(self, record):
"""
posts = [
{
'Post': {
'title':'Other today post',
'date': 43750,
'claps': 200
}
},
{
'Post': {
@dtaivpp
dtaivpp / Memoize.py
Last active October 31, 2019 14:21
List comprehension with memoization example from https://stackoverflow.com/a/15812933/4577237
def memoize(f):
""" Memoization decorator for functions taking one or more arguments. """
class memodict(dict):
def __init__(self, f):
self.f = f
def __call__(self, *args):
return self[args]
def __missing__(self, key):
ret = self[key] = self.f(*key)
return ret
# Initialization
list = ["Lists", "Is", "Pretty", "Sweet"]
# Assigning an index a new value
list[1] = "Are"
# Extending a list
list.append("Cool")
# Removeing from the end
# Initialization
dictionary = {'string key': "string value",
('tuple', 'key'): {'dict': "value"},
123: "Value for int key"}
# Assign a key a new value
dictionary[123] = "New Value"
# Retrieve all keys from a dictionary
keys = dictionary.keys()