Skip to content

Instantly share code, notes, and snippets.

View saintsGrad15's full-sized avatar

John Carrell saintsGrad15

View GitHub Profile
@saintsGrad15
saintsGrad15 / sample_list_evenly.py
Created January 12, 2022 17:46
Return the contents of 'list_' sampled evenly accross its current order.
def sample_list_evenly(list_: List[Any], samples: int) -> List[Any]:
"""
Return the contents of 'list_' sampled evenly accross its current order.
NOTE: Doesn't work perfectly yet...
:param list_: A list of anything.
:param samples: The number of samples to return.
@saintsGrad15
saintsGrad15 / find_hex_namespace_boundaries.py
Last active October 28, 2021 20:44
Return a list of tuples representing the inclusive lower and upper boundaries of each range of hexadecimal values from a namespace with 'number_of_bytes' bytes divided into 'number_of_sets' "equal" sets.
def find_hex_namespace_boundaries(number_of_bytes:int, number_of_sets:int = 2) -> Tuple[str]:
"""
Return a list of tuples representing the inclusive lower and upper boundaries
of each range of hexadecimal values from a namespace with 'number_of_bytes' bytes
divided into 'number_of_sets' "equal" sets.
:param number_of_bytes: The length of the hexadecimal namespace in bytes.
:param number_of_sets: The number of sets to divide the namespace into.
NOTE: The sets will be of as close to equal length as possible
@saintsGrad15
saintsGrad15 / range_header_regex.py
Created January 21, 2021 16:45
A regex that validates and extracts groups from a valid HTTP "range" header
# Spec source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
import re
range_header_regex = re.compile(r"^((?P<unit>[a-zA-Z]+)=)?(?P<start>\d+)?-(?P<end>\d+)?$")
@saintsGrad15
saintsGrad15 / local_minima_finder_v2.py
Created August 18, 2020 19:34
Given an array that keeps information about Temperature readings for a city, return an array of equal length that tells you, for a given day how many days have passed since a higher temperature occurred.
# Given an array that keeps information about Temperature readings for a city,
# return an array of equal length that tells you, for a given day
# how many days have passed since a higher temperature occurred
temps = [7, 3, 4, 6, 9, 1, 5, 6, 3, 7, 4, 8, 2, 10]
expected_output = [1, 1, 2, 3, 5, 1, 2, 3, 1, 5, 1, 7, 1, 14]
def local_minima_finder_v2(temps):
complexity_counter = 0
output = [1]
@saintsGrad15
saintsGrad15 / local_minima_finder_v1.py
Last active August 18, 2020 19:33
Given an array that keeps information about Temperature readings for a city, return an array of equal length that tells you, for a given day how many days have passed since a higher temperature occurred.
# Given an array that keeps information about Temperature readings for a city,
# return an array of equal length that tells you, for a given day
# how many days have passed since a higher temperature occurred
temps = [7, 3, 4, 6, 9, 1, 5, 6, 3, 7, 4, 8, 2, 10]
expected_output = [1, 1, 2, 3, 5, 1, 2, 3, 1, 5, 1, 7, 1, 14]
def local_minima_finder_v1(temps):
complexity_counter = 0
output = [1]
@saintsGrad15
saintsGrad15 / get_dict_path.py
Created July 23, 2020 19:05
A tail-recursive function to return the value at a path within a dictionary.
def get_dict_path(dict_, path, default=None):
if len(path) < 1:
return dict_
return get_dict_path(dict_.get(path[0], default), path[1:], default)
@saintsGrad15
saintsGrad15 / gist:afbcc0c27c2c1db90fe8e8d1b6e129e6
Created January 15, 2020 18:06
Solve MacOS pycurl issue.md
Source: https://github.com/transloadit/python-sdk/issues/4
1. Uninstall pycurl
2. brew install openssl (May already be installed...)
3. export CPPFLAGS=-I/usr/local/opt/openssl/include
4. export LDFLAGS=-L/usr/local/opt/openssl/lib
5. pip install pycurl --global-option="--with-openssl"
6. Rejoice
@saintsGrad15
saintsGrad15 / tox.ini
Created November 22, 2019 15:44
Basic tox.ini setup
[tox]
envlist = py27
[testenv]
install_command=pip install --extra-index-url=http://pypi.ad.cleversafe.com/simple/ --trusted-host=pypi.ad.cleversafe.com {opts} {packages}
# Allows running e.g. `tox -e DCC`
# Otherwise include commands in [testenv]
# NOTE: -s can be run pointed at a directory and it will run all modules inside
[testenv:DCC]
@saintsGrad15
saintsGrad15 / negate_multiple_words.regex
Created November 4, 2019 17:13
Negate multiple words using PCRE
^(?!development|master).*$
@saintsGrad15
saintsGrad15 / ClassInstanceCacher.py
Created October 29, 2019 20:34
A general form of a class instance cacher
class ClassInstanceCacher(object):
# Rename the outer class at will.
# Implement Klass to suit use case
__cache = {}
def __new__(cls, _id):
if cls.__cache.get(_id) is None:
cls.__cache[_id] = cls.Klass(_id)
return cls.__cache[_id]