Skip to content

Instantly share code, notes, and snippets.

View nicksspirit's full-sized avatar
❤️
Currently in a committed relationship with python

Nick Muoh nicksspirit

❤️
Currently in a committed relationship with python
View GitHub Profile
@nicksspirit
nicksspirit / poetry_tools.py
Last active June 24, 2023 04:28
Gist of python programs that can be remotely executed
"""
Command Line tool that provides compatibility between poetry and cyclonus docker scans
"""
import argparse
import sys
from contextlib import contextmanager
from itertools import chain
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
let cmp = function(a, b){
if(a < b) return -1;
if(a > b) return 1;
return 0;
};
function bsearch(arr, value, comparator) {
let low = 0,
high = arr.length - 1,
mid;
@nicksspirit
nicksspirit / is_prime.py
Created January 29, 2022 16:16
Naive Primality Test in python
import math
def smallest_div(n):
if n <= 1:
return 1
return min(i for i in range(2, n + 1) if n % i == 0)
def check_prime(n):
if n % 2 == 0:
@nicksspirit
nicksspirit / fizzbuzz.py
Created January 29, 2022 16:14
A functional programming approach to fizzbuzz in python
from typing import Callable
def is_multiple(x: int, num: int) -> bool:
return x % num == 0
def in_number(x: int, num: int) -> bool:
return str(num) in str(x)
@nicksspirit
nicksspirit / split_daterange.py
Created January 12, 2022 17:59
Split a date range into smaller ranges.
def date_delta(startdate, enddate, no_of_ranges):
start_epoch = calendar.timegm(startdate.timetuple())
end_epoch = calendar.timegm(enddate.timetuple())
date_diff = end_epoch - start_epoch
step = date_diff / no_of_ranges
return datetime.timedelta(seconds=step)
@nicksspirit
nicksspirit / run_async.py
Created October 15, 2020 13:20
Decorator to run an async function
def run_async(fn):
@wraps(fn)
def fn_wrapped(*args, **kwargs):
return asyncio.run(fn(*args, **kwargs))
return fn_wrapped
@nicksspirit
nicksspirit / domready.js
Last active January 12, 2022 17:53
Check if the document is ready
function onReady(callbackFn) {
if (document.readyState !== 'loading') {
// Document is already ready, call the callback directly
callbackFn();
} else if (document.addEventListener) {
// All modern browsers to register DOMContentLoaded
document.addEventListener('DOMContentLoaded', callbackFn);
} else {
// Old IE browsers
document.attachEvent('onreadystatechange', function() {
@nicksspirit
nicksspirit / gather_with_concurrency.py
Created June 27, 2020 20:23
Start many tasks concurrently but with a limit of how many tasks can run at a time.
import asyncio
async def gather_with_concurrency(n, *tasks):
semaphore = asyncio.Semaphore(n)
async def concurrent_task(task):
async with semaphore:
await task
await asyncio.gather(*(concurrent_task(task) for task in tasks))
@nicksspirit
nicksspirit / goFullScreen.js
Created January 24, 2020 16:31
Javascript function that uses the Full Screen API to display an element in fullscreen.
goFullScreen() {
const fullScreenPrefixes = [
'requestFullscreen',
'webkitRequestFullscreen',
'mozRequestFullScreen',
'msRequestFullscreen'
]
// HTMLElement is the DOM element you want to make full screen
const prefixedFn = fullScreenPrefixes.find(
prefixedFn => HTMLElement[prefixedFn] !== undefined
@nicksspirit
nicksspirit / hide-dots.ps1
Created January 13, 2020 15:23
Hide files with a dot infront of them on windows
function Hide-Dots {
Param(
[Parameter(Position=0)]
[ValidateScript({ Test-Path $_ })]
[string] $path="."
)
if ($path -eq ".") {
$cwd = (Get-Location).Path.Substring((Get-Location).Path.LastIndexOf("\") + 1)
echo "Searching for files in $($cwd) ..."