Skip to content

Instantly share code, notes, and snippets.

View andygarfield's full-sized avatar

Andy Garfield andygarfield

  • St. Petersburg, FL
View GitHub Profile
@andygarfield
andygarfield / continents.json
Created January 24, 2023 22:39 — forked from hrbrmstr/continents.json
Continents GeoJSON
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@andygarfield
andygarfield / promise_all_errors.md
Last active October 29, 2021 17:15
Dealing with errors in Promise.all

Dealing with errors in Promise.all

Promise.all stops execution if any of the Promises have an uncaught error.

async function wait(ms) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
}

Testing

My general strategy for testing is to separate out pure from non-pure functions.

All programs need a main function which should have the context needed to run the program as it's expected to run. This code can be tested using integration tests, but can't be unit tested. It is ideal to keep this function small, using external configuration if possible.

Pure functions don't need any tricks to test correctly. Just give them data, and they'll do their thing. This code should be where the business logic resides.

Non-pure functions are necessary for performing IO tasks or for doing something which varies (getting the current day of the week for instance). These functions can sometimes be tested with integration tests, other times not. The idea is to keep the code inside these functions extremely small so that it can be easily verified with a quick glance. These functions shouldn't contain branching logic, loops, or anything of the sort.

It is ideal to perform all of the needed IO with non-pure functio

@andygarfield
andygarfield / curlwebdriver.sh
Last active January 24, 2018 14:54
Curl Webdriver Example
# jq is a dependency and ChromeDriver needs to be running
# Create a headless Chrome session and save the sessionId to variable SID
SID=$(curl -X POST -d '{"desiredCapabilities":{"browserName":"chrome","chromeOptions":{"args":["--headless"]}}}' http://localhost:9515/session | jq -r '.sessionId')
echo "Session ID: $SID"
# Point session to a webpage
curl -X POST -d '{"url":"http://localhost:8080"}' "http://localhost:9515/session/$SID/url"
# Fetch an element using the xpath selector and save the elementId to ELEM
ELEM=$(curl -X POST -d '{"using": "xpath", "value": "//a[contains(.,'"'"'CSV format (.csv)'"'"')]"}' "http://localhost:9515/session/$SID/elements" | jq -r '.value[0].ELEMENT')
# Get the href attribute
@andygarfield
andygarfield / r2a.go
Created November 19, 2017 22:33
Convert Dockerfile RUN's
package main
// Dockerfiles build faster when Docker caches the result of your RUN commands. But, the
// resulting image can only be small if the the RUN commands are combined into only one
// RUN command. This script converts Dockerfiles with multiple RUN commands in a row in
// a single RUN command separated by "&& \".
// Use: Run the command while in the current directory to convert the Dockerfile there,
// or give it an optional folder path.
@andygarfield
andygarfield / ConcurrentPrimes.go
Created September 18, 2017 23:11
Prime number generator in go
package main
import (
"fmt"
)
func checkPrime(i int, c chan int) {
prime := true
for j := 3; j < i/2; j += 2 {
if i%j == 0 {
@andygarfield
andygarfield / convert_metadata.py
Last active December 9, 2016 00:20
Batch Convert Metadata to Excel
import exifread
import os
import pandas as pd
def convert_metatag_to_decimal(metatag_coordinate, direction_reference=None):
"""
Converts funky exif number format to float number
:param metatag_coordinate: Metatag coordinate (ie "[28, 120289/10000, 0]")
:param direction_reference: "N", "S", "E", or "W"
@andygarfield
andygarfield / Project_better.py
Last active June 14, 2018 17:59
ArcPy Project tool
from arcpy import CreateFeatureclass_management, os, da, ListFields, Describe
def project_better(in_dataset, out_dataset, spatial_reference):
# Script borrowed from http://joshwerts.com/blog/2015/09/10/arcpy-dot-project-in-memory-featureclass/
# Can project a dataset and create the output in an 'in_memory' workspace
path, name = os.path.split(out_dataset)
CreateFeatureclass_management(
path,
@andygarfield
andygarfield / Pythonista_GPS_log.py
Last active February 7, 2016 01:09
Pythonista GPS logger
import location
import time
import datetime
location.start_updates()
loc = location.get_location()
last_loc = '{0},{1}\n'.format(loc['latitude'], loc['longitude'])
print(last_loc)
for seconds in range(1800):
loc = location.get_location()
@andygarfield
andygarfield / line_speed.py
Last active February 15, 2016 21:25
Create a line sequence feature class from GPS data
#
# CSV file must be in the format: Latitude,Longitude,datetime
# The third field is date time info from: datetime.datetime.utcnow()
#
from arcpy import Project_management, CreateFeatureclass_management, AddField_management, SpatialReference, da, Array,\
Point, Polyline, GetParameterAsText, mapping, Describe, ListFields, MakeTableView_management, GetCount_management
import os
from datetime import datetime
from math import floor