Skip to content

Instantly share code, notes, and snippets.

@wehappyfew
wehappyfew / class_example.py
Created September 23, 2013 16:41
codecademy python class exmaple
class Fruit(object):
"""A class that makes various tasty fruits."""
def __init__(self, name, color, flavor, poisonous):
self.name = name
self.color = color
self.flavor = flavor
self.poisonous = poisonous
def description(self):
print "I'm a %s %s and I taste %s." % (self.color, self.name, self.flavor)
#Step 1
#install the PiFm library
cd ~
mkdir pifm
cd pifm
wget http://www.icrobotics.co.uk/wiki/images/c/c3/Pifm.tar.gz
tar -xvf Pifm.tar.gz
#Step 2
@wehappyfew
wehappyfew / movement.py
Last active August 29, 2015 14:07
A script that reads input from a serial port (eg Arduino on COM3) using the pyserial library and sends an email (using the GMAIL SMTP settings) when a condition is met.
# wehappyfew
# Distributed over IDC(I Don't Care) license
__author__ = 'wehappyfew'
import time
import serial
import smtplib
def delete_instances(instance_tag="your_tag" , instance_state="stopped"):
"""
The function
1. filters all the instances by tag and instance-state
2. terminates/deletes the filtered instances (irreversible action!!!)
"""
reserves = conn.get_all_instances( filters={"tag-value":instance_tag , "instance-state-name":instance_state} )
for r in reserves:
for instance in r.instances:
instance.terminate()
import boto, boto.ec2, time
# creds for AWS user
aws_access_key_id = "sdvsdvsdvsdvsdvsdv"
aws_secret_access_key = "sdvsdvsdvsdvsdvsdvsdv"
Windows_Server_2003_R2_Base_64bit_imageID = "ami-b9f2d789"
Ubuntu_Server_14_04_LTS_imageID = "ami-23ebb513"
AMIs = {
@wehappyfew
wehappyfew / tag_AWS_instances.py
Last active August 29, 2015 14:17
A function that tags all the newly created instances based on their 'pending' state.Of course it can be used for tagging any other state just by providing different argument.
def tag_instances(connection,tag , instance_state="pending"):
"""
-The function must be run immediately after the spawn of an instance!!!-
1. The function fetches all the instances
2. Filters them by instance-state-name:pending
(for a short period the newly created instances are 'pending' before becoming 'running')
3. Tags them with the user defined tag.
4. Returns the just tagged instances (ids) for future reference
"""
def stop_instances(region, instance_tag="my_instance" , instance_state="running"):
"""
The function
1. filters all the instances by tag and instance-state
2. stops the filtered instances (does not terminate/delete them)
3. returns the just stopped instances (ids) for future reference
"""
c = boto.ec2.connect_to_region(region_name = region,
aws_access_key_id = aws_access_key_id,
aws_secret_access_key = aws_secret_access_key,
def delete_instances(region, instance_tag="my_instance" , instance_state="stopped"):
"""
The function
1. filters all the instances by tag and instance-state
2. terminates/deletes the filtered instances (irreversible action!!!)
"""
c = boto.ec2.connect_to_region(region_name = region,
aws_access_key_id = aws_access_key_id,
aws_secret_access_key = aws_secret_access_key,
)
def list_all_instances(region):
# make the connection to AWS with my creds
conn = boto.ec2.connect_to_region(region_name = region,
aws_access_key_id = aws_access_key_id,
aws_secret_access_key = aws_secret_access_key,
)
reservations = conn.get_all_instances()
for r in reservations:
for instance in r.instances:
@wehappyfew
wehappyfew / fetch_excel_values.py
Last active August 29, 2015 14:19
Parse an excel file and grab the values in the columns and save them in dictionaries
def fetch_excel_values(filepath, sheet_index=0):
from xlrd import open_workbook
# open the excel file
book = open_workbook(filename=filepath)
# open the specific sheet based on index
sheet = book.sheet_by_index(0)
# read headers
keys = [sheet.cell(int(sheet_index),col_index).value for col_index in range(sheet.ncols)]