Skip to content

Instantly share code, notes, and snippets.

@SavvyGuard
SavvyGuard / simpleprogressbar.py
Created July 29, 2013 18:38
Simple Progress Bar for outputting to console
import sys
def _progressBar(state):
progress_bar = '=' * state
sys.stdout.write('\r'+progress_bar)
sys.stdout.flush()
@SavvyGuard
SavvyGuard / launchec2.py
Created July 29, 2013 22:23
Launch an ec2 instance and then wait until they're ready
import sys
import boto
import boto.ec2
def _launchAWSInstances():
connection = boto.ec2.connect_to_region(AWS_REGION, aws_access_key_id = AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_ACCESS_KEY_SECRET)
global reservation
reservation = connection.run_instances(AWS_AMI,
key_name=AWS_SSH_KEY_NAME,
instance_type=AWS_INSTANCE_SIZE,
@SavvyGuard
SavvyGuard / sshready.py
Created July 30, 2013 00:31
Wait for SSH Connection to become ready with progress bar when trying to connect to Amazon EC2 server
import subprocess
import boto.ec2
import sys
def _progressBar(state):
progress_bar = '=' * state
sys.stdout.write('\r'+progress_bar)
sys.stdout.flush()
# recursive function to wait until the ssh connection is ready
@SavvyGuard
SavvyGuard / botos3upload.py
Last active February 8, 2023 14:56
Use boto to upload directory into s3
import boto
import boto.s3
import os.path
import sys
# Fill these in - you get them when you sign up for S3
AWS_ACCESS_KEY_ID = ''
AWS_ACCESS_KEY_SECRET = ''
# Fill in info on data to upload
@SavvyGuard
SavvyGuard / assigns3files.py
Last active December 20, 2015 15:59
Create a file that evenly assigns (by files) data from s3 across all tagged EC2 instances. Useful for a distributed database.
#!/usr/bin/env python
#boto libraries
import boto.ec2
import boto.utils
import boto.s3.connection
#python libraries
import json
@SavvyGuard
SavvyGuard / getEC2InstancePublicHostName.py
Created August 14, 2013 00:01
get the current EC2 instance's public host name. Easily changed to get private hostname and other info.
import boto.utils
print boto.utils.get_instance_metadata()['network']['interfaces']['macs'].values()[0]['public-hostname']
@SavvyGuard
SavvyGuard / splitfile.py
Created August 19, 2013 18:03
split a file into multiple parts by bytes
def split_file(file, prefix, max_size, buffer=1024):
"""
filename: the input filename
max_size: maximum size of each created file in bytes
buffer: buffer size in bytes
Returns the number of parts created.
"""
with open(file, 'r+b') as src:
suffix = 0
@SavvyGuard
SavvyGuard / assignValuesToKeysEvenly.py
Last active December 21, 2015 13:39
Evenly assign a list of values to another list of keys so that the each key has almost same total values assigned to it
# values are highest first
def assignValuesToKeysEvenly(keys, values):
""" keys are a list containing key names
values are a list of values
"""
keynames = {}
# make keynames a dict of keynames containing a list of assigned values
for key in keys:
keynames[key] = []
@SavvyGuard
SavvyGuard / ExDict.py
Created September 11, 2013 20:19
Python dict as a class
class ExDict(dict):
"""
Extended Dictionary
"""
def __init__(self,*arg,**kw):
super( ExDict , self).__init__(*arg, **kw)
def __setattr__(self, k, v):
if k in self.keys():
self[k] = v
@SavvyGuard
SavvyGuard / ty_vs_fy_mortgage.py
Last active August 29, 2015 14:02
mortgage 15 vs 30 year
def total_saved(monthly_sum, total_years, ror):
year = 0
total_sum = 0
year_sum = monthly_sum*12
while year <= total_years:
year += 1
total_sum += year_sum
total_sum *= ror
return total_sum