Skip to content

Instantly share code, notes, and snippets.

View pedramamini's full-sized avatar
👋

Pedram Amini pedramamini

👋
View GitHub Profile
@pedramamini
pedramamini / object_method_instance_attach.py
Created February 15, 2012 06:43
python: how to attach a function as a method instance to an arbitrary object.
# how to attach a function as a method instance to an arbitrary object.
class person:
def __init__ (self):
self.name = ""
def beast (self):
if self.name == "pedram":
return True
else:
return False
@pedramamini
pedramamini / ruby_style_method_missing.py
Last active September 30, 2015 17:18
Python: Ruby style metthodmissing hack
class proxy:
"""
Hack to get Ruby methodmissing functionality.
"""
####################################################################################################################
def __getattr__ (self, method_name):
'''
This routine is called by default when a requested attribute (or method) is accessed that has no definition.
Unfortunately __getattr__ only passes the requested method name and not the arguments. So we extend the
@pedramamini
pedramamini / misc_python_utilities.py
Last active September 30, 2015 23:08
Collection of type conversion, output formatting, and other miscellaneous Python utilities I've written and used across a variety of projects.
import re
########################################################################################################################
def blend_colors (first, second, scale=.5):
'''
Takes 2 RGB color hex values and blend them together.
'''
rold = (first & 0xFF0000) >> 16
gold = (first & 0x00FF00) >> 8
@pedramamini
pedramamini / speech_stopper.py
Created March 2, 2012 03:19
Speech Echo Proof-of-Concept
"""
Read the following in an interesting article today:
" Psychologists have known for some years that it is almost impossible to speak when your words are replayed to you
with a delay of a fraction of a second. "
Source: http://www.technologyreview.com/blog/arxiv/27620/
I found it interesting and wanted to hack together a proof-of-concept for myself. Two children are spawned, one for
listening and the other for repeating what was heard. Data is shared between processes via shared memory queue. The
# Joe Sandbox API wrapper.
# REQUIRES: python-requests http://docs.python-requests.org/en/latest/
import sys
import time
import random
import getpass
import requests
try:
import random
def random_fact ():
return facts[random.randint(0, len(facts)-1)]
facts = \
[
"Chuck Norris' tears cure cancer. Too bad he has never cried. Ever.",
"Chuck Norris does not sleep. He waits.",
"Chuck Norris is currently suing NBC, claiming Law and Order are trademarked names for his left and right legs.",
@pedramamini
pedramamini / ped_missile.py
Created January 10, 2013 22:13
USB Missile Launcher Python Interface written by Pedram Amini <pamini@tippingpoint.com> http://dvlabs.tippingpoint.com/blog/2009/02/12/python-interfacing-a-usb-missile-launcher
"""
USB Missile Launcher Python Interface
written by Pedram Amini <pamini@tippingpoint.com>
http://dvlabs.tippingpoint.com/blog/2009/02/12/python-interfacing-a-usb-missile-launcher
"""
import ctypes
import struct
import time
@pedramamini
pedramamini / pedram-sublimetext-setup.md
Last active January 1, 2016 06:29
My SublimeText setup.

I'm running SublimeText 3 beta on MacOS, but this should apply everywhere. My user prefs:

{
	"always_show_minimap_viewport": true,
	"bold_folder_labels": true,
	"drag_text": false,
	"draw_minimap_border": true,
	"ensure_newline_at_eof_on_save": true,
	"highlight_line": true,
@pedramamini
pedramamini / bash_prompt.sh
Last active August 29, 2015 14:06
Git aware prompt that shows dirty state and unsynced commit state.
function parse_git_dirty() {
[[ $(git status 2> /dev/null | tail -n1) != *"working directory clean"* ]] && echo "*"
}
function parse_git_branch() {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(parse_git_dirty)/"
}
# requires the following in .gitconfig under [alias] section:
# unpushed = !GIT_CURRENT_BRANCH=$(git name-rev --name-only HEAD) && git log origin/$GIT_CURRENT_BRANCH..$GIT_CURRENT_BRANCH --oneline
@pedramamini
pedramamini / levenshtein_distance.py
Created September 22, 2014 23:12
Provides the Levenshtein distance between two strings. ie: The number of transformations required to transform one string to the other
########################################################################################################################
def levenshtein_distance (first, second):
"""
Provides the Levenshtein distance between two strings. ie: The number of transformations required to transform
one string to the other.
@type first: String
@param first: First string.
@type second: String
@param second: Second string.