Monday 16 November 2015

Python with AWS

Python documentation:
https://docs.python.org

Install:
https://www.python.org/downloads/
https://www.jetbrains.com/pycharm/download/

Learn Python:
https://learnpythonthehardway.org/book/
http://files.meetup.com/18552511/Learn%20Python%20The%20Hard%20Way%203rd%20Edition%20V413HAV.pdf
https://learnpythonthehardway.org/python3/
https://docs.python.org/3.6/tutorial/index.html



# import is used to make specialty functions available
# These are called modules
import random
import sys
import os
 
# Hello world is just one line of code
# print() outputs data to the screen
print("Hello World")
 
'''
This is a multi-line comment
'''
 
# A variable is a place to store values
# Its name is like a label for that value
name = "Derek"
print(name)
 
# A variable name can contain letters, numbers, or _
# but can't start with a number
 
# There are 5 data types Numbers, Strings, List, Tuple, Dictionary
# You can store any of them in the same variable
 
name = 15
print(name)
 
# The arithmetic operators +, -, *, /, %, **, //
# ** Exponential calculation
# // Floor Division
print("5 + 2 =", 5+2)
print("5 - 2 =", 5-2)
print("5 * 2 =", 5*2)
print("5 / 2 =", 5/2)
print("5 % 2 =", 5%2)
print("5 ** 2 =", 5**2)
print("5 // 2 =", 5//2)
 
# Order of Operation states * and / is performed before + and -
 
print("1 + 2 - 3 * 2 =", 1 + 2 - 3 * 2)
print("(1 + 2 - 3) * 2 =", (1 + 2 - 3) * 2)
 
# A string is a string of characters surrounded by " or '
# If you must use a " or ' between the same quote escape it with \
quote = "\"Always remember your unique,"
 
# A multi-line quote
multi_line_quote = ''' just
like everyone else" '''
 
print(quote + multi_line_quote)
 
# To embed a string in output use %s
print("%s %s %s" % ('I like the quote', quote, multi_line_quote))
 
# To keep from printing newlines use end=""
print("I don't like ",end="")
print("newlines")
 
# You can print a string multiple times with *
print('\n' * 5)
 
# LISTS -------------
# A list allows you to create a list of values and manipulate them
# Each value has an index with the first one starting at 0
 
grocery_list = ['Juice', 'Tomatoes', 'Potatoes', 'Bananas']
print('The first item is', grocery_list[1])
 
# You can change the value stored in a list box
grocery_list[0] = "Green Juice"
print(grocery_list)
 
# You can get a subset of the list with [min:up to but not including max]
 
print(grocery_list[1:3])
 
# You can put any data type in a a list including a list
other_events = ['Wash Car', 'Pick up Kids', 'Cash Check']
to_do_list = [other_events, grocery_list]
 
print(to_do_list)
 
# Get the second item in the second list (Boxes inside of boxes)
print(to_do_list[1][1])
 
# You add values using append
grocery_list.append('onions')
print(to_do_list)
 
# Insert item at given index
grocery_list.insert(1, "Pickle")
 
# Remove item from list
grocery_list.remove("Pickle")
 
# Sorts items in list
grocery_list.sort()
 
# Reverse sort items in list
grocery_list.reverse()
 
# del deletes an item at specified index
del grocery_list[4]
print(to_do_list)
 
# We can combine lists with a +
to_do_list = other_events + grocery_list
print(to_do_list)
 
# Get length of list
print(len(to_do_list))
 
# Get the max item in list
print(max(to_do_list))
 
# Get the minimum item in list
print(min(to_do_list))
 
# TUPLES -------------
# Values in a tuple can't change like lists
 
pi_tuple = (3, 1, 4, 1, 5, 9)
 
# Convert tuple into a list
new_tuple = list(pi_tuple)
 
# Convert a list into a tuple
# new_list = tuple(grocery_list)
 
# tuples also have len(tuple), min(tuple) and max(tuple)
 
# DICTIONARY or MAP -------------
# Made up of values with a unique key for each value
# Similar to lists, but you can't join dicts with a +
 
super_villains = {'Fiddler' : 'Isaac Bowin',
                  'Captain Cold' : 'Leonard Snart',
                  'Weather Wizard' : 'Mark Mardon',
                  'Mirror Master' : 'Sam Scudder',
                  'Pied Piper' : 'Thomas Peterson'}
 
print(super_villains['Captain Cold'])
 
# Delete an entry
del super_villains['Fiddler']
print(super_villains)
 
# Replace a value
super_villains['Pied Piper'] = 'Hartley Rathaway'
 
# Print the number of items in the dictionary
print(len(super_villains))
 
# Get the value for the passed key
print(super_villains.get("Pied Piper"))
 
# Get a list of dictionary keys
print(super_villains.keys())
 
# Get a list of dictionary values
print(super_villains.values())
 
# CONDITIONALS -------------
# The if, else and elif statements are used to perform different
# actions based off of conditions
# Comparison Operators : ==, !=, >, <, >=, <=
 
# The if statement will execute code if a condition is met
# White space is used to group blocks of code in Python
# Use the same number of proceeding spaces for blocks of code
 
age = 30
if age > 16 :
    print('You are old enough to drive')
 
# Use an if statement if you want to execute different code regardless
# of whether the condition ws met or not
 
if age > 16 :
    print('You are old enough to drive')
else :
    print('You are not old enough to drive')
 
# If you want to check for multiple conditions use elif
# If the first matches it won't check other conditions that follow
 
if age >= 21 :
    print('You are old enough to drive a tractor trailer')
elif age >= 16:
    print('You are old enough to drive a car')
else :
    print('You are not old enough to drive')
 
# You can combine conditions with logical operators
# Logical Operators : and, or, not
 
if ((age >= 1) and (age <= 18)):
    print("You get a birthday party")
elif (age == 21) or (age >= 65):
    print("You get a birthday party")
elif not(age == 30):
    print("You don't get a birthday party")
else:
    print("You get a birthday party yeah")
 
# FOR LOOPS -------------
# Allows you to perform an action a set number of times
# Range performs the action 10 times 0 - 9
for x in range(0, 10):
    print(x , ' ', end="")
 
print('\n')
 
# You can use for loops to cycle through a list
grocery_list = ['Juice', 'Tomatoes', 'Potatoes', 'Bananas']
 
for y in grocery_list:
    print(y)
 
# You can also define a list of numbers to cycle through
for x in [2,4,6,8,10]:
    print(x)
 
# You can double up for loops to cycle through lists
num_list =[[1,2,3],[10,20,30],[100,200,300]];
 
for x in range(0,3):
    for y in range(0,3):
        print(num_list[x][y])
 
# WHILE LOOPS -------------
# While loops are used when you don't know ahead of time how many
# times you'll have to loop
random_num = random.randrange(0,100)
 
while (random_num != 15):
    print(random_num)
    random_num = random.randrange(0,100)
 
# An iterator for a while loop is defined before the loop
i = 0;
while (i <= 20):
    if(i%2 == 0):
        print(i)
    elif(i == 9):
        # Forces the loop to end all together
        break
    else:
        # Shorthand for i = i + 1
        i += 1
        # Skips to the next iteration of the loop
        continue
 
    i += 1
 
# FUNCTIONS -------------
# Functions allow you to reuse and write readable code
# Type def (define), function name and parameters it receives
# return is used to return something to the caller of the function
def addNumbers(fNum, sNum):
    sumNum = fNum + sNum
    return sumNum
 
print(addNumbers(1, 4))
 
# Can't get the value of rNum because it was created in a function
# It is said to be out of scope
# print(sumNum)
 
# If you define a variable outside of the function it works every place
newNum = 0;
def subNumbers(fNum, sNum):
    newNum = fNum - sNum
    return newNum
 
print(subNumbers(1, 4))
 
# USER INPUT -------------
print('What is your name?')
 
# Stores everything typed up until ENTER
name = sys.stdin.readline()
 
print('Hello', name)
 
# STRINGS -------------
# A string is a series of characters surrounded by ' or "
long_string = "I'll catch you if you fall - The Floor"
 
# Retrieve the first 4 characters
print(long_string[0:4])
 
# Get the last 5 characters
print(long_string[-5:])
 
# Everything up to the last 5 characters
print(long_string[:-5])
 
# Concatenate part of a string to another
print(long_string[:4] + " be there")
 
# String formatting
print("%c is my %s letter and my number %d number is %.5f" % ('X', 'favorite', 1, .14))
 
# Capitalizes the first letter
print(long_string.capitalize())
 
# Returns the index of the start of the string
# case sensitive
print(long_string.find("Floor"))
 
# Returns true if all characters are letters ' isn't a letter
print(long_string.isalpha())
 
# Returns true if all characters are numbers
print(long_string.isalnum())
 
# Returns the string length
print(len(long_string))
 
# Replace the first word with the second (Add a number to replace more)
print(long_string.replace("Floor", "Ground"))
 
# Remove white space from front and end
print(long_string.strip())
 
# Split a string into a list based on the delimiter you provide
quote_list = long_string.split(" ")
print(quote_list)
 
# FILE I/O -------------
 
# Overwrite or create a file for writing
test_file = open("test.txt", "wb")
 
# Get the file mode used
print(test_file.mode)
 
# Get the files name
print(test_file.name)
 
# Write text to a file with a newline
test_file.write(bytes("Write me to the file\n", 'UTF-8'))
 
# Close the file
test_file.close()
 
# Opens a file for reading and writing
test_file = open("test.txt", "r+")
 
# Read text from the file
text_in_file = test_file.read()
 
print(text_in_file)
 
# Delete the file
os.remove("test.txt")
 
# CLASSES AND OBJECTS -------------
# The concept of OOP allows us to model real world things using code
# Every object has attributes (color, height, weight) which are object variables
# Every object has abilities (walk, talk, eat) which are object functions
 
class Animal:
    # None signifies the lack of a value
    # You can make a variable private by starting it with __
    __name = None
    __height = None
    __weight = None
    __sound = None
 
    # The constructor is called to set up or initialize an object
    # self allows an object to refer to itself inside of the class
    def __init__(self, name, height, weight, sound):
        self.__name = name
        self.__height = height
        self.__weight = weight
        self.__sound = sound
 
    def set_name(self, name):
        self.__name = name
 
    def set_height(self, height):
        self.__height = height
 
    def set_weight(self, height):
        self.__height = height
 
    def set_sound(self, sound):
        self.__sound = sound
 
    def get_name(self):
        return self.__name
 
    def get_height(self):
        return str(self.__height)
 
    def get_weight(self):
        return str(self.__weight)
 
    def get_sound(self):
        return self.__sound
 
    def get_type(self):
        print("Animal")
 
    def toString(self):
        return "{} is {} cm tall and {} kilograms and says {}".format(self.__name, self.__height, self.__weight, self.__sound)
 
# How to create a Animal object
cat = Animal('Whiskers', 33, 10, 'Meow')
 
print(cat.toString())
 
# You can't access this value directly because it is private
#print(cat.__name)
 
# INHERITANCE -------------
# You can inherit all of the variables and methods from another class
 
class Dog(Animal):
    __owner = None
 
    def __init__(self, name, height, weight, sound, owner):
        self.__owner = owner
        self.__animal_type = None
 
        # How to call the super class constructor
        super(Dog, self).__init__(name, height, weight, sound)
 
    def set_owner(self, owner):
        self.__owner = owner
 
    def get_owner(self):
        return self.__owner
 
    def get_type(self):
        print ("Dog")
 
    # We can overwrite functions in the super class
    def toString(self):
        return "{} is {} cm tall and {} kilograms and says {}. His owner is {}".format(self.get_name(), self.get_height(), self.get_weight(), self.get_sound(), self.__owner)
 
    # You don't have to require attributes to be sent
    # This allows for method overloading
    def multiple_sounds(self, how_many=None):
        if how_many is None:
            print(self.get_sound)
        else:
            print(self.get_sound() * how_many)
 
spot = Dog("Spot", 53, 27, "Ruff", "Derek")
 
print(spot.toString())
 
# Polymorphism allows use to refer to objects as their super class
# and the correct functions are called automatically
 
class AnimalTesting:
    def get_type(self, animal):
        animal.get_type()
 
test_animals = AnimalTesting()
 
test_animals.get_type(cat)
test_animals.get_type(spot)
 
spot.multiple_sounds(4)

Data Types

Strings:
s = "foo bar"
s = 'foo bar'
s = r"c:\dir\new"                     # raw (== 'c:\\dir\\new')
s = """Hello
       world"""
s.join(" baz")
n = len(s)
"Ala ma {} psy i {} koty".format(2,3)
"Square root of 2 is equal to {:.2f}".format(math.sqrt(2))
Lists:
L = [1, 2, 3, 4, 5]
L[0]                                  # single position
L[0:3]                                # the first three elements
L[-2:]                                # the last two elements
L[1:4] = [7,8]                        # substitute
del L[2]                              # remove elements
L.append(x)                           # x is a value
L.remove(x)
L.extend(L2)                          # or: L3 = L + L2
L.pop()                               # simple stack (with append)
L.sort()
x in L                                # does L contain x?
L.index(x)                            # index of the first occurrence
[x*2 for x in L if x>2]               # list comprehensions
Tuples:
x = 1,2,3
x = (1,2,3)
x[1]
a,b,c = x
Dictionaries:
D = {'f1': 10, 'f2': 20}              # dict creation
D = dict(f1=10, f2=20)

keys = ('a', 'b', 'c')
D = dict.fromkeys(keys)               # new dict with empty values

for k in D: print(k)                  # keys
for v in D.values(): print(v)         # values
for k, v in D.items():                # tuples with keys and values
list(D.keys())                        # list of keys
sorted(D.keys())                      # sorted list of keys

D = {}
D[(1,8,5)] = 100                      # 3D sparse matrix
D.get((1,8,5))
D.get((1,1,1), -1)
Sets:
S = {1,3,5}
L = [1, 3, 1, 5, 3]
S = set(L)                            # set([1, 3, 5])
if (3 in S):
S1+S2, S1-S2, S1^S2, S1|S2

Loops

for x in range(6):                    # 0, 1, 2, 3, 4, 5
for x in range(1,6):                  # 1, 2, 3, 4, 5
for x in range(1,6,2):                # 1, 3, 5

for k,v in D.items():
    print("D[{}]={}".format(k,v))     # D[f1]=10  D[f2]=20

L = [1, 3, 5]
for i,v in enumerate(L):              # (index,value)
for x,y in zip(L1,L2):                # returns tuples
for i in sorted(set(L)): print(i)     # sorted set from a list
for x in reversed(L1):

Functions

def foo(arg1, *args, **dic):
  """Example documentation string.

  This function does not do anything special.
  """
  # arg1 is a positional argument
  # args is a list
  # dic is a dictionary of named arguments

def foo(a,b,c=0):
L = [1, 2, 3]
foo(*L)                               # unpacking a list of arguments
D = {'a': 10, 'b': 20}
foo(**D)                              # unpacking a dictionary of arguments

foo.__doc__                           # the docstring

Input/output

Printing:
str(x)                                # human readable representation
repr(x)                               # interpretable representation
File access:
f = open("test.txt", "w")             # r / r+ / rb / rb+ / w / wb
f.write("Ala ma kota\n")
f.close()

for line in open("test.txt"): print(line, end="")

L = open("test.txt").readlines()      # returns a list of lines
Exclusive access:
f = os.fdopen(os.open("test.txt", os.O_WRONLY|os.O_EXCL), "w")
Input:
x = raw_input("Name: ")
for line in sys.stdin: print(line)
String buffers:
from StringIO import StringIO
buf = StringIO()
sys.stdout = buf
print("Hello")
x = buf.getvalue()
Error stream:
print("Error!", file=sys.stderr, flush=True)
Other file operations:
os.rename(from, to)                  os.remove(path)
os.chmod(file, 0700)                 os.stat(file)

Special names

__name__
name of the file being run not imported
Typical usage:
if __name__ == "__main__":
    print("Do something)

Exceptions

try:
    raise TypeError("arg")
except (RuntimeError, NameError):
    pass                              # empty instruction (NOP)
except:
    info = sys.exc_info()
    print(info[0])
    print(info[1])
    traceback.print_tb(info[2])
    raise
else:
    ...                               # no exception but before finally
finally:                              # on the way out
    ...                               # unhandled exc, release resources

Object-oriented programming

class Person:
    ID = 0                            # static variable
    def __init__(self, name, age=0):
        self.name = name
        self.age  = age
        Person.ID += 1
        self.ID = Person.ID
    def lastName(self):
        return self.name.split()[-1]
    def __str__(self):
        return "{}({},{})".format(self.__class__.__name__,
                                  self.name, self.age)

class Worker(Person):
    def __init__(self, name, position, age=0):
        super().__init__(name, age)
        self.position = position
    def __str__(self):
        return "{}({},{},{})".format(self.__class__.__name__,
                                   self.name, self.position, self.age)

bob = Worker("Bob Smith", "developer", 25)
print(bob)

Useful APIs

Queues:
Q = collections.deque([10,20,30])
Q.append(40)
Q.popleft()
Pickling:
f = open("myobj.dat", "w")
pickle.dump(x, f)
f = open("myobj.dat", "r")
x = pickle.load(f)
Databases:
conn = sqlite3.connect("data.db")
c = conn.cursor()
c.execute("SELECT * FROM employees")
for row in c:
    print(row[0])
conn.commit()
conn.close()

db = shelve.open("file")
db["x"] = y
db.close()
CGI:
form = cgi.FieldStorage()
print("Content-type: text/html\n")
print(cgi.escape(form["user"].value))
HTTP Server:
srvraddr = ("", 8080)                 # my hostname, portnumber
srvrobj  = BaseHTTPServer.HTTPServer(srvraddr,
                                     CGIHTTPServer.CGIHTTPRequestHandler)
srvrobj.serve_forever()
URLs:
conn = urllib.urlopen("http://localhost:8080")
reply = conn.read()

Environment

Encoding:
#!/usr/bin/python3
# -*- coding: latin-2 -*-
Windows – use .pyw extension to run the script (with GUI) without a console window.
Paths:
PYTHONPATH
export PYTHONSTARTUP=~/.pythonrc.py
Module sys:
sys.argv      sys.stdin       sys.stdout      sys.stderr
sys.path      sys.platform    sys.version
Processes (module subprocess):
res = subprocess.call(["hostname","-f"], stderr=subprocess.DEVNULL)
res = subprocess.call("ps axu | grep ^root", shell=True)
output = subprocess.check_output(["mycmd", "myarg"],universal_newlines=True)
Module os:
os.pathsep    os.sep          os.pardir       os.curdir       os.linesep
os.startfile("index.html")
os.popen("ps ax").readlines()
os.listdir("/usr/local")              # ['bin', 'etc', ...]
os.glob("*.txt")                      # ['test.txt', 'out.txt', ...]
Module os.path:
os.path.split("/usr/bin/go.sh")       # ('/usr/bin', 'go.sh')
os.path.join("/usr/bin", "go.sh")     # '/usr/bin/go.sh'
os.path.splitext("/usr/bin/go.sh")    # ('/usr/bin/go', '.sh')
os.path.abspath("../bin/go.sh")       # '/usr/bin/go.sh'
os.path.isfile("go.sh")
Module os.environ:
os.environ.get("PYTHONSTARTUP")
Directories:
for (dir, subdirs, files) in os.walk("/tmp"):
    for f in files: print(f)

Functional programming

f = lambda x: x+10                    # creates an anonymous function
f(5)                                  # returns 15
L = [1, 4, 7]
for x in filter(lambda i: i<5, L):    # returns [1, 4]
for x in map(lambda: x: x*2, L):      # returns [2, 8, 14

Cheat Sheets:
https://github.com/ehmatthes/pcc/releases/download/v1.0.0/beginners_python_cheat_sheet_pcc_all.pdf
https://learnxinyminutes.com/docs/python/
https://learnxinyminutes.com/docs/python3/

Python 2 to 3:
http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf

Try in browser:
https://www.pythonanywhere.com/try-ipython/

AWS:
https://aws.amazon.com/sdk-for-python/
https://boto3.readthedocs.io/en/latest/guide/quickstart.html
https://boto3.readthedocs.io/en/latest/reference/services/index.html
https://docs.python.org/3/library/json.html
http://pythonexample.com/search?q=aws+ec2+describe+instances+json
# For boto3, you should be able to install it like this from your command-line:
$ pip install boto3

# For awscli, the installation process should be the same if you’re on OS X/Linux:
$ pip install awscli
$ aws configure

# Use following code to select profile
cat setup.py
session = boto3.Session(
        region_name='eu-west-1',
        profile_name='myprofile'
        )
 
# declare ec2
ec2 = session.client('ec2')

# Example code #1
$ cat ec2.py
import boto3

ec2 = boto3.client('ec2')
response = ec2.describe_instances()
print(response)

# Example code #2
$ cat ec2-json.py
import boto3, json
ec2 = boto3.client('ec2')
res = ec2.describe_instances()
res = json.dumps(res)
print(res)

No comments:

Post a Comment