Skip to content

Instantly share code, notes, and snippets.

@drincruz
drincruz / csvreader.py
Created December 8, 2011 16:27
I needed a quick and easy way to print specific csv columns for my life on the command line...
#!/usr/bin/env python
import csv
import sys
if 3 > len(sys.argv):
sys.exit("Usage: %s <csv_file> <column_to_print>" % sys.argv[0])
fileparse = csv.reader(open(sys.argv[1]))
for f in fileparse:
@drincruz
drincruz / hex_to_ascii.py
Created July 25, 2013 13:58
Reads stdin hex and outputs ascii. Youtube posted on google+, so I wrote this quickly. https://plus.google.com/u/0/+youtube/posts/TWvnTtEgz8L
#!/usr/bin/env python
import sys
def main():
for line in sys.stdin:
print("%s\n" % bytes.fromhex(line.replace(' ', '').strip()).decode('utf-8'))
if '__main__' == __name__:
main()
@drincruz
drincruz / SecretSanta.java
Last active December 25, 2015 07:49
An input String array of names and returns a two dimensional array to see who has who in secret Santa. So basically, {"user1":"user2"}, meaning "user1" needs to get "user2" a gift.
import static java.util.Arrays.deepToString;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Queue;
class SecretSanta {
public String[][] go(String[] s) {
@drincruz
drincruz / .mailcap
Last active August 29, 2015 14:04
.mailcap and .muttrc entries
text/html; w3m -I %{charset} -T text/html; copiousoutput;
@drincruz
drincruz / anagram.py
Created August 17, 2014 15:53
Simple example to find out if two strings are anagrams of each other
#!/usr/bin/env python
"""
Example of how you can test if strings are anagrams
"""
def is_anagram(str1, str2):
"""
Check if str1 is an anagram of str2
@drincruz
drincruz / db_row_generator.py
Created August 22, 2014 16:17
Simple example of using a Python generator to fetch a row from a MySQL database
#!/usr/bin/env python
"""
Here is a simple example of how you can
use a generator to fetch a row from a database
"""
import MySQLdb
@drincruz
drincruz / string_appending.py
Created August 22, 2014 16:31
Appending to a Python string
"""
There are multiple ways to append to a string in Python
"""
str0 = "This is my string"
# Use the + operator
@drincruz
drincruz / python2_round
Created September 14, 2014 03:00
Python 2 round() vs. Python 3 round()
>>> round(0.5)
1.0
>>> round(2.5)
3.0
>>> round(0.4)
0.0
>>> round(2.6)
3.0
@drincruz
drincruz / data_on_cli001.sh
Last active December 22, 2015 17:08
data mining on the command line with bash and python
# This is the main meat and potatoes of what I really want
grep -E DEBUG 2015-10-09.err | grep -E '(anonymous_id|high_scores:)'| sed -e 's/anonymous_id//g' |cut -d ' ' -f 2-| python -c "import fileinput; l0=[int(x.split(' ')[-1].strip()) for x in fileinput.input()];print('searches:{}\thigh_scoring:{}\tratio:{}'.format(l0[0],l0[1],l0[1]/float(l0[0])))"
# This is just a bonus to loop through a date range of my log files
for d in {0..15}; do today=`date -d "2015-10-09 + $d days" +'%Y-%m-%d'`; echo -n "${today} "; grep -E DEBUG ${today}.err|grep -E '(anonymous_id|high_scores:)'| sed -e 's/anonymous_id//g' |cut -d ' ' -f 2-| python -c "import fileinput; l0=[int(x.split(' ')[-1].strip()) for x in fileinput.input()];print('searches:{}\thigh_scoring:{}\tratio:{}'.format(l0[0],l0[1],l0[1]/float(l0[0])))"; done
@drincruz
drincruz / hacker_news_standard_deviation.sql
Last active April 29, 2016 10:12
Getting variance and standard deviation in BigQuery
SELECT
variance,
standard_deviation,
input
FROM (standardDeviation(
SELECT
'[' + GROUP_CONCAT_UNQUOTED(STRING(score)) + ']' AS json_array_input
FROM (
SELECT
score