Skip to content

Instantly share code, notes, and snippets.

View samredai's full-sized avatar

Samuel Redai samredai

View GitHub Profile
@samredai
samredai / parallel.sh
Created December 12, 2018 22:28
Linux: Run Shell commands in parallel
#!/bin/bash
for cmd in "$@"; do {
echo "Process \"$cmd\" started...";
$cmd & pid=$!
PID_LIST+=" $pid";
} done
trap "kill $PID_LIST" SIGINT
@samredai
samredai / create_user_settings.sql
Created December 13, 2018 05:57
MySQL: Create a table to store user settings
CREATE TABLE `user_settings` (
`userid` int(11) unsigned NOT NULL,
`name` varchar(50) NOT NULL,
`type` BOOL NOT NULL,
`value_int` int(10) DEFAULT NULL,
`value_str` varchar(100) DEFAULT NULL,
PRIMARY KEY (`userid`, `name`)
);
#`userid`: The user that this setting applies to
@samredai
samredai / createPost_postResponse_structs.swift
Last active December 14, 2018 04:10
Swift: Performing a POST request to a JSON Rest-API
//Create an ENCODABLE struct which will be used to create a POST body
struct createPost: Encodable {
let public_id: String
let action: String
let type: String
let value: String
}
//Let's assume the POST to the API will return a JSON object that looks like this:
//{ "action" : "create", "type": "bulletin", "result" : "Bulletin created successfully!" }
@samredai
samredai / create_decodable_struct_w_nesting.swift
Last active February 24, 2019 17:29
Swift: Create a Decodable structure for a JSON response that includes nesting
//We'll use a NASA API response as an example
//https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?earth_date=2015-6-3&api_key=DEMO_KEY
//I like to start in the inner-most part of the nesting and work my way out
//"cameras" is the inner-most nest and it contains an array
//Each array has two key:value pairs, "name" and "full_name"
//Create a Decodable struct for a single object in the array
struct roverCamera: Decodable {
let name: String
@samredai
samredai / find_string_in_list_w_wildcards.py
Last active December 15, 2018 15:58
Python: Find and Remove a String from a List Using fnmatch (Including Using Wildcards)
import fnmatch
l = ["imagine", "if", "this", "was", "a", "super", "duper", "long", "list"]
thisMatches = fnmatch.filter(l, "this")
print(thisMatches)
#['this']
#Using ? as a single-character wildcard
uperMatches = fnmatch.filter(l, "?uper")
@samredai
samredai / json_rest_to_pandas_df.py
Created December 15, 2018 22:03
Python: Convert a flat JSON response from a REST-API into a pandas DataFrame
import pandas as pd
improt requests
#Use requests to hit the API
response = requests.get('https://official-joke-api.herokuapp.com/random_ten')
#Convert the JSON response to a list of dictionaries using json()
json = response.json()
#Create a pandas DataFrame from the list of dictionaries
@samredai
samredai / today_date_function.sh
Created December 16, 2018 02:39
Linux: Get Today's Date in YYYY-MM-DD format
date +%Y-%m-%d
#2018-12-16
#Set the date to a variable
rundate=$(date +%Y-%m-%d)
echo $rundate
#2018-12-16
@samredai
samredai / git_merge_then_delete_branch.sh
Created December 17, 2018 15:47
Git: Merge a dev branch to master then delete that branch both locally and remotely
#Checkout the master branch
git checkout master
#Merge the dev branch into the master branch
git merge dev
#Push the new merged master branch
git push
#Delete the dev branch locally
@samredai
samredai / fileMakerTemplate.sh
Last active December 19, 2018 22:41
Linux: Create a file from a template
#!/bin/sh
PNAME=$1
ENDPOINT=$2
GITREPO=$3
#template
cat << EOF
Project name: $PNAME
API Endpoint: $ENDPOINT
@samredai
samredai / printRangeOfLines.sh
Created January 10, 2019 21:12
Linux: Print a specified range of lines in a file
# print lines 10 to 20 in the file called file.txt
sed -n '10,20p' file.txt