Skip to content

Instantly share code, notes, and snippets.

// As a function
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// One liner
await new Promise(r => setTimeout(r, 2000));
// Or
const sleep = ms => new Promise(r => setTimeout(r, ms));
@RyanNutt
RyanNutt / is_odd.py
Created November 20, 2021 20:28
Recursive is_odd. Not something to actually use, but will probably talk about this in class as an example for recursion. https://www.reddit.com/r/ProgrammerHumor/comments/qyb5ut/odd/
def is_odd(num):
if k == 1:
return True
elif k == 0:
return False
elif (num < 0):
return is_odd(num * -1)
else:
return is_odd(num - 2)
@RyanNutt
RyanNutt / permutations.php
Created July 4, 2021 14:23
Get all permutations of an array in PHP - from https://stackoverflow.com/a/24517966/1561431
<?php
function computePermutations($array) {
$result = [];
$recurse = function($array, $start_i = 0) use (&$result, &$recurse) {
if ($start_i === count($array)-1) {
array_push($result, $array);
}
for ($i = $start_i; $i < count($array); $i++) {
@RyanNutt
RyanNutt / jquery.showif.js
Created December 18, 2020 15:40
jQuery showIf / hideIf plugin
(function ($) {
$.fn.showIf = function (check) {
if (check) {
$(this).show();
}
else {
$(this).hide();
}
};
@RyanNutt
RyanNutt / test_example.py
Created September 20, 2020 14:43
Python unittest starter stub
import unittest
class TestClass(unittest.TestCase):
def test(self):
exp = 4
actual = 4
self.assertEqual(exp, actual, 'Some Message')
@RyanNutt
RyanNutt / disable-scaled-images.php
Created May 26, 2020 20:30
WordPress plugin to turn off auto creation of scaled images
<?php
/**
* Plugin Name: Disable Scaled Images
* Plugin URI: https://www.nutt.net
* Description: One liner to turn off automatic scaled image creation for large images
* Version: 0.1.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Ryan Nutt
* Author URI: https://www.nutt.net
@RyanNutt
RyanNutt / download_videos.py
Last active March 2, 2020 19:06
Script to download media submissions from Canvas assignment
import json
import urllib.request
import sys
with open('submissions.json', 'r') as read_file:
data = json.load(read_file)
for sub in data:
filename = sub['user']['name'] + '.mpg4'
print(filename)
@RyanNutt
RyanNutt / readme.md
Created February 28, 2020 14:11
Update docker clock

Update Docker Clock

I switched to Docker from Virtual Box as a local web development environment a few months ago and find it much easier to work with. Except for one issue.

What I've found is that if a Docker container is running when my computer goes to sleep the clock stops and the container falls behind by however long my computer was asleep. Since the container is supposed to be in sync with my computer there didn't seem to be an easy way to update the container's clock. Lots of work arounds, but nothing quick and easy. And I couldn't find anything that worked without stopping the container first.

Stack Overflow to the rescue. I found this one liner that does exactly what I needed.

docker run --rm --privileged alpine hwclock -s
@RyanNutt
RyanNutt / .htaccess
Created February 24, 2020 19:13
Redirect old domain to new domain root - https://stackoverflow.com/a/7578810/1561431
RewriteEngine on
RewriteRule ^(.*)$ http://www.newdomain.com/ [R=301,L]
@RyanNutt
RyanNutt / moveToTop.js
Created January 29, 2020 17:06
jQuery plugin to move elements to top or bottom of parent
/**
* Move an element to the top of the stack by appending it to the end
* of its parent.
*
* The element, and its siblings, should be positioned in parent so that
* they're stacked on top of each other.
*/
(function ($) {
$.fn.moveToBottom = function () {
return this.parent().prepend(this);