Skip to content

Instantly share code, notes, and snippets.

View CodyKochmann's full-sized avatar

Cody Kochmann CodyKochmann

  • Severn, MD
View GitHub Profile
@CodyKochmann
CodyKochmann / downloader.py
Last active August 29, 2015 14:20
Python downloader for url to output path if duplicate file with optional duplicate renaming.
def download_file(url, output_path="./", verbose=False, allow_duplicates=False, f_name=''):
# downloads a given url to the output_path with duplicate checking options
# by: Cody Kochmann
from urllib2 import urlopen
from os import listdir
def v_print(s):
if(verbose):
print(s)
if f_name == '':
f_name = url.split('/')[-1]
@CodyKochmann
CodyKochmann / script_path.py
Created May 7, 2015 23:10
Python snippet to get the full path of the script containing the snippet. (good for finding differently positioned workspaces)
def script_path(include_name=False):
# returns the full path of the script containing this snippet
# by: Cody Kochmann
from os import path
full_path = path.realpath(__file__)
if include_name:
return(full_path)
else:
full_path = "/".join( full_path.split("/")[0:-1] ) + "/"
return(full_path)
@CodyKochmann
CodyKochmann / ensure_dir.py
Last active August 29, 2015 14:20
python script to ensure there is a directory of that name in that path
def ensure_dir(dir_name,parent_dir,verbose=False):
# ensures there is a directory of that name in that path
# by: Cody Kochmann
# ex: trash_dir=ensure_dir("trash", script_dir)
def v_log(s):
if(verbose):
print(s)
from os import listdir, path, mkdir
dir_name=dir_name.replace("/", "")
if parent_dir[-1] != "/":
@CodyKochmann
CodyKochmann / clipboard.py
Created May 8, 2015 02:23
python functions that give clipboard access to the script
import subprocess
def getClipboardData():
p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
retcode = p.wait()
data = p.stdout.read()
return data
def setClipboardData(data):
p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
@CodyKochmann
CodyKochmann / current_dir.py
Created May 8, 2015 02:25
returns the current directory on any unix system
def current_dir():
# returns a unix current directory
# By: Cody Kochmann
from os import path
return(path.dirname(path.realpath(__file__)))
@CodyKochmann
CodyKochmann / remove_spaces.js
Created May 13, 2015 12:14
Removes all spaces from a string in javascript.
function remove_spaces(s){
// removes spaces from a string
// by: Cody Kochmann
return(s.split(" ").join(""));
}
@CodyKochmann
CodyKochmann / generate_active_width.js
Created May 13, 2015 17:02
pre-renders media specific rules for a div in order to keep it a constant width without needing to worry about margin:0 auto
var generate_active_width = function(dom_id,min_inches,flexing_percentage,max_inches){
// pre-renders media specific rules for a div in order to keep it a constant width without needing to
// worry about the stability of "margin:0 auto" in certain situations.
// by: Cody Kochmann
var inject_css = function(a) {
var b = document.createElement("style");
b.innerHTML = a;
b.type = "text/css";
document.getElementsByTagName("head")[0].appendChild(b)
}
@CodyKochmann
CodyKochmann / findall.py
Created May 14, 2015 17:10
searches a string for a substring and returns an array of indexes
def findall(search_field,search):
# searches a string for a substring and returns an array of indexes
# by: Cody Kochmann
tmp_field = search_field.split(search)
if len(tmp_field) == 1:
return(False)
output = []
tmp_string = ""
for i in range(len(tmp_field)):
tmp_string += tmp_field.pop(0)
@CodyKochmann
CodyKochmann / get_complete_string.py
Created May 15, 2015 04:42
returns a substring starting with start and ending with end
def get_complete_string(s,start,end):
# returns a substring starting with start and ending with end
# By: Cody Kochmann
unique="~~~~the~obvious~way~to~do~it~~~~"
return(start+s.replace(start, unique).replace(end, unique).split(unique)[1]+end)
@CodyKochmann
CodyKochmann / random_string.py
Created May 15, 2015 23:34
Generates a random string with selectable characters
def random_string(length=32,upper=True,lower=True,digits=True):
# Generates a random string with selectable characters
# By: Cody Kochmann
from random import choice
chars = ""
if upper:
chars+="QWERTYUIOPASDFGHJKLZXCVBNM"
if lower:
chars+="qwertyuiopasdfghjklzxcvbnm"
if digits: