Skip to content

Instantly share code, notes, and snippets.

@89465127
89465127 / argparse_files.py
Last active August 27, 2022 14:24
Get a list of filenames using argparse (updated to use default formatter)
import argparse
import os
import glob
def main():
#Does not currently have support to read files from folders recursively
parser = argparse.ArgumentParser(description='Read in a file or set of files, and return the result.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('path', nargs='+', help='Path of a file or a folder of files.')
parser.add_argument('-e', '--extension', default='', help='File extension to filter by.')
args = parser.parse_args()
@89465127
89465127 / filter_dict.py
Created June 13, 2013 20:05
python filter a dictionary by keys or values
d = {1:11, 2:22, 3:33}
# filter by key
d2 = {k : v for k,v in filter(lambda t: t[0] in [1, 3], d.iteritems())}
# filter by value
d3 = {k : v for k,v in d.iteritems() if k in [2,3]}
@89465127
89465127 / cli_progress.py
Last active January 9, 2019 10:43
Simple example of how to get a python progress bar in your program.
import time
from progress.bar import Bar # sudo pip install progress
bar = Bar('Processing', max=20, suffix='%(index)d/%(max)d - %(percent).1f%% - %(eta)ds')
for i in range(20):
time.sleep(.05) # Do some work
bar.next()
bar.finish()
@89465127
89465127 / HelloJson.scala
Created October 5, 2013 15:52
Simple json deserialization in Scala
package com.example.json
import net.liftweb.json._
object HelloJson extends App {
case class Address(city: String, state: String)
case class Person(name: String, addresses: List[Address])
implicit val formats = DefaultFormats
@89465127
89465127 / longest_palandrome.py
Created September 19, 2013 16:50
Find the longest palandrom in a given string, using functional programming and recursion in python.
def sliding(iterable, n):
return [iterable[i:i+n] for i in range(len(iterable) - n + 1)]
def xsliding(iterable, n):
for i in range(len(iterable) - n + 1):
yield iterable[i:i+n]
def reversed_iter(iterable):
return iterable[::-1]
@89465127
89465127 / Palandrome.scala
Created September 18, 2013 23:02
Longest Palandrome in Scala
object Palandrome {
def main(args: Array[String]) {
val s = "meow woow ooooooo doggies aaadddfffdddaaa wow"
println("finding the longest palandrome of: " + s)
println(longestPal(s))
}
def longestPal(s: String) = {
longestPalN(s, s.length)
@89465127
89465127 / README.md
Last active December 19, 2015 03:08 — forked from rduplain/README.md

Goal: Connect to MSSQL using FreeTDS / ODBC in Python.

Host: Linux Mint 15 x86_64

Install:

sudo apt-get install python-dev python-pip freetds-dev freetds-bin unixodbc-dev tdsodbc
pip install pyodbc

In /etc/odbcinst.ini:

@89465127
89465127 / aws_instances.py
Created June 25, 2013 20:09
Give a summary of currently running/pending AWS instances, and how many are being used out of the limit.
# Setup:
# 1) sudo pip install awscli
# 2) the following environment variables must be set
# - $ export AWS_ACCESS_KEY_ID=<access_key>
# - $ export AWS_SECRET_ACCESS_KEY=<secret_key>
import collections
import datetime
import json
import subprocess
@89465127
89465127 / First singleton character in string.ipynb
Last active December 18, 2015 10:28
Interview Question: find the first character that occurs only once in a string.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@89465127
89465127 / open_hadoop.py
Created June 6, 2013 18:10
Read in flat hadoop files into python. Uses a generator, so it is memory efficient.
import glob
import os
def filelist(path, _filter="part-*"):
basepath = os.path.abspath(os.path.expanduser(path))
return [filename for filename in glob.glob(basepath + '/' + _filter)]
def hfile(path, _filter="part-*"):
for filename in filelist(path, _filter):
with open(filename) as f: