Skip to content

Instantly share code, notes, and snippets.

View isaaguilar's full-sized avatar

Isa Aguilar isaaguilar

View GitHub Profile
@isaaguilar
isaaguilar / tail-and-quit.rb
Last active December 7, 2016 06:57
"tail -f" for ruby
require 'open3'
def tail_and_quit(filename, regex_to_match)
cmd = "tail -n0 -f #{filename}" # bash tail -f command
Open3.popen2e('bash', '-c', cmd) do |i,oe,t| # input, output/error, process
oe.each do |line|
puts line
break if line.match(/#{regex_to_match}/)
end
Process.kill('INT', t.pid) # kill the tail process after matching string
end
@isaaguilar
isaaguilar / tail-and-quit.py
Created December 15, 2016 21:01
tail -f for python
import subprocess, re
def tail_and_quit(filename, regex_to_match):
try:
f = subprocess.Popen(['tail', '-F', filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
line = f.stdout.readline()
match = re.search(regex_to_match, line)
if match: break
finally: f.kill()
from functools import wraps
def parse_request_data(f):
@wraps(f)
def parse(*args, **kwargs):
json_type = False
for header in request.headers:
if header[0] == 'Content-Type':
if "application/json" in header[1].lower():
json_type = True
data = {}
@isaaguilar
isaaguilar / flask-boolean-params-converter.py
Last active January 17, 2017 10:40
Convert "true"/"false" url args or data params as a native Python Boolean
def parse_true_or_false(some_array):
if type(some_array) == list:
array = enumerate(some_array)
else:
array = some_array.iteritems()
for key, value in array:
if type(value) in (dict, list):
parse_true_or_false(some_array[key])
else:
try:
@isaaguilar
isaaguilar / tomcat7.sh
Created March 4, 2017 02:18
init.d script for tomcat7 - something new but mostly borrowed
#!/bin/bash
#
# tomcat
#
# chkconfig: 345 96 30
# description: Start up the Tomcat servlet engine.
#
# processname: java
# pidfile: /var/run/tomcat.pid
#
@isaaguilar
isaaguilar / uniquePush.js
Last active May 16, 2017 07:27
"Uniques only" push to array in JavaScript ES6 (slow)
const uniquePush = (arr, item) => {
if ( arr.indexOf(item) === -1) {
arr.push(item)
}
}
@isaaguilar
isaaguilar / trendline.py
Created June 13, 2017 09:17
linear trendline equation
def trendline(x, y):
# y = mx + b, where m = slope and b = offset
return "f(x) = {0}x + {1}".format(slope(x, y), offset(x, y))
def sum_xy(x, y):
s = float(0)
for i in range(len(x)):
s += x[i] * y[i]
return s
@isaaguilar
isaaguilar / LinearGraph.jsx
Last active April 3, 2024 05:06
Draw a basic scatter plot graph with react and d3. Draw the trend-line between your points. Example graph in comments below.
import React from "react"
import ScatterPlot from "./ScatterPlot-with-trendline"
data={[[0, 3],[5, 13],[10, 22],[15, 36],[20, 48],[25, 59],[30, 77],[35, 85],[40, 95],[45, 105],[50, 120],[55, 150],[60, 147],[65, 168],[70, 176],[75, 188],[80, 199],[85, 213],[90, 222],[95, 236],[100, 249]]}
export default class LinearGraph extends React.Component {
render() {
return <ScatterPlot data={data} />
}
}
@isaaguilar
isaaguilar / ps_ax.py
Last active July 5, 2017 07:37
a version of `ps ax` for python
import re
from subprocess import Popen, PIPE
# Equivilent to `ps ax|grep search_string`
p2 = Popen(["grep", search_string], stdin=PIPE, stdout=PIPE) # search_string
p1 = Popen(["ps", "ax"], stdout=p2.stdin)
# Break up results into invidual lines
results = p2.communicate()[0].split("\n")
# Now iterate thru the list, filter out grep, and print pids
@isaaguilar
isaaguilar / overlay.html
Created July 8, 2017 04:31
Overlay on parent element css, js, html
<html>
<head>
<style>
.overlay {
position: absolute;
top: 0;
left: 0px;
bottom: 0px;
right: 0px;
z-index: 2;