Skip to content

Instantly share code, notes, and snippets.

View MattSandy's full-sized avatar
💭
Hungry

Matt Sandy MattSandy

💭
Hungry
View GitHub Profile
@MattSandy
MattSandy / server.R
Created September 28, 2015 01:46
Creates a websocket based JSON server
#Connect to this using websockets on port 9454
#Send in the format of {"data":[1,2,3]}
#The ppp returns the standard deviation of the sent array
library(jsonlite)
library(httpuv)
#server
app <- list(
onWSOpen = function(ws) {
ws$onMessage(function(binary, message) {
write(message, file = "log.txt",append = TRUE, sep = "\n")
@MattSandy
MattSandy / geo_location.php
Created September 21, 2015 20:58
Checks if within a defined box based on longitude and latitude
<?php
$url = 'https://api.smartystreets.com/street-address?';
$url .= 'auth-id=[auth-id]&auth-token=[auth-token]';
$url .= '&street=' . $street . '&zipcode=' . $zip;
$urlData = json_decode(file_get_contents($url),true);
//print_r($urlData);
if(isset($urlData[0]['metadata']['latitude'])) {
$latitude = $urlData[0]['metadata']['latitude'];
$longitude = $urlData[0]['metadata']['longitude'];
@MattSandy
MattSandy / dokku
Last active September 20, 2015 19:34
//server
sudo apt-get update
sudo apt-get install lxc wget bsdtar curl
sudo apt-get install linux-image-extra-$(uname -r)
sudo apt-get install gcc
wget -qO- https://raw.github.com/progrium/dokku/master/bootstrap.sh | sudo bash
//user
#Route port 3000 to 80
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
#Search for running node.js processes
ps aux | grep node
#Kill process
kill -9 [id]
#Run node.js after closing shell
@MattSandy
MattSandy / vector_break.R
Last active August 29, 2015 14:26
Breaks a vector into defined segment lengths
#Define the interval to cycle through
interval <- 30
#Replace sample_vector with your vector
sample_vector <- runif(100, 1, 10)
#Loop through each break in the sequence
for(i in seq(1,length(sample_vector),interval)) {
#Used to prevent the sequence from extending beyond the remainders
if(i+interval>=length(sample_vector)) {
#Vector access for this sequence is as follows:
print(sample_vector[i:length(sample_vector)])
@MattSandy
MattSandy / find_patterns.R
Last active August 29, 2015 14:24
Find repeating patterns in an R vector from a vector of intervals to check.
vector <- c(10,1,0,10,0,0,10,0,0,10)
matches <- find_patterns(vector,seq(2,3))
find_patterns <- function (vector, intervals) {
matches <- matrix(c(NA, NA), nrow=1, ncol=2)
for(interval in intervals) {
for(i in 1:interval) {
if(sd(vector[seq(i,length(vector),interval)])==0) {
if(is.na(matches[1,1])) {
matches[1,] <- c(vector[i],interval)
@MattSandy
MattSandy / match_inverval.R
Created July 7, 2015 02:04
Match repeated values in a vector based on a specified interval
vector <- c(10,1,0,10,0,0,10,0,0,10)
match_interval(vector,3))
match_interval <- function (vector, interval) {
matches <- c()
for(i in 1:interval) {
if(sd(vector[seq(i,length(vector),interval)])==0) {
matches[length(matches)+1] <- vector[i]
}
}
@MattSandy
MattSandy / split.bash
Created April 28, 2015 15:58
Split CSV by Column Value
awk -F"," 'NR>1 {print $0 >> ($1 ".csv"); close($1 ".csv")}' file.csv
@MattSandy
MattSandy / gist:0b43f602e69e09e86aff
Last active August 29, 2015 14:09
Return Date Range Array
<?php
function createDateRangeArray($startDate,$endDate)
{
$range[0] = $startDate;
$startDateInt = preg_replace('/[^0-9]/', '', $startDate);
$endDateInt = preg_replace('/[^0-9]/', '', $endDate);
if($endDateInt>$startDateInt)
{
while($range[count($range)-1]!=$endDate)
@MattSandy
MattSandy / quickstart.php
Last active May 26, 2020 17:57
Object Oriented Database Connection Quickstart
<?php
define('DB_NAME', 'zip');
/** MySQL database username */
define('DB_USER', '*****');
/** MySQL database password */
define('DB_PASSWORD', '*****');
/** MySQL hostname */