Unit 5 Part 1
Unit 5 Part 1
Boto has become the official AWS SDK for Python. Boto versions include Boto, Boto3 and Botocore. Boto3 is the
latest version of the SDK, providing support for Python versions 2.6.5, 2.7 and 3.3. Boto3 includes several service-
specific features to ease development. Boto supports all current AWS cloud services, including Elastic Compute
Cloud, DynamoDB, AWS Config, CloudWatch and Simple Storage Service.
Boto3 replaced Boto version 2, which lacks compatibility with the latest versions of Python, but still appeals to
software developers using older versions of the programming language. Botocore provides more rudimentary
access to AWS tools, simply making low-level client requests and getting results from APIs.
Boto derives its name from the Portuguese name given to types of dolphins native to the Amazon River.
Amazon AutoScaling – Python
Example #Python program for creating an AutoScaling group (code excerpt)
• AutoScaling Service import boto.ec2.autoscale
:
• A connection to the AutoScaling service is first established print "Connecting to Autoscaling Service"
by calling the boto.ec2.autoscale.connect_to_region conn = boto.ec2.autoscale.connect_to_region(REGION,
function. aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
• Launch Configuration print "Creating launch configuration"
• After connecting to the AutoScaling service, a new launch
configuration is created by calling lc = LaunchConfiguration(name='My-Launch-Config-2',
conn.create_launch_con f iguration. Launch configuration image_id=A
contains instructions on how to launch new instances MI_ID,
including the AMI-ID, instance type, security groups, etc. key_name=EC2_KEY_HANDLE,
adjustment_type='Cha
ngeInCapacity',
scaling_adjustment=-1, a
s
cooldown=180) _
n
a
conn.create_scaling_policy(scale_up_policy) m
e
conn.create_scaling_policy(scale_down_policy)
=
'
Book website: http://www.internet-of-things-book.com BahgaM& Madisetti, ©
Amazon AutoScaling – Python Example
#Connecting to CloudWatch
cloudwatch = boto.ec2.cloudwatch.connect_to_region(REGION,
CloudWatch Alarms aws_access_key_id=ACCESS_KEY,
• With the scaling policies defined, the next step is to aws_secret_access_key=SECRET_KE
Y)
create Amazon CloudWatch alarms that trigger these alarm_dimensions = {"AutoScalingGroupName": 'My-Group'}
policies.
#Creating scale-up alarm
• The scale up alarm is defined using the CPUUtilization scale_up_alarm =
metric with the Average statistic and threshold greater MetricAlarm( name='scale_up_on_cpu',
70% for a period of 60 sec. The scale up policy created namespace='AWS/EC2', metric='CPUUtilization',
statistic='Average', comparison='>', threshold='70',
previously is associated with this alarm. This alarm is period='60', evaluation_periods=2,
triggered when the average CPU utilization of the alarm_actions=[scale_up_policy.policy_arn],
dimensions=alarm_dimensions)
instances in the group becomes greater than 70% for cloudwatch.create_alarm(scale_up_alarm)
more than 60 seconds.
#Creating scale-down alarm
• The scale down alarm is defined in a similar manner scale_down_alarm =
with a threshold less than 50%. MetricAlarm( name='scale_down_on_cpu',
namespace='AWS/EC2', metric='CPUUtilization',
statistic='Average', comparison='<', threshold='40',
period='60', evaluation_periods=2,
alarm_actions=[scale_down_policy.policy_arn],
dimensions=alarm_dimensions)
cloudwatch.create_alarm(scale_down_alarm)
conn = boto.connect_s3(aws_access_key_id='<enter>',
aws_secret_access_key='<enter>')
• JSON
• JavaScript Object Notation (JSON) is an easy to read and write data-interchange format. JSON is used as an alternative to XML
and is easy for machines to parse and generate. JSON is built on two structures: a collection of name–value pairs (e.g., a Python
dictionary) and ordered lists of values (e.g., a Python list).
• XML
• XML (Extensible Markup Language) is a data format for structured document interchange. The Python minidom library
provides a minimal implementation of the Document Object Model interface and has an API similar to that in other languages.
• HTTPLib & URLLib
• HTTPLib2 and URLLib2 are Python libraries used in network/internet programming.
• SMTPLib
• Simple Mail Transfer Protocol (SMTP) is a protocol which handles sending email and routing email between mail servers.
The Python SMTPLib module provides an SMTP client session object that can be used to send email.
• NumPy
• NumPy is a package for scientific computing in Python. NumPy provides support for large multi-dimensional arrays
and matrices.
• Scikit-learn
• Scikit-learn is an open source machine learning library for Python that provides implementations of various machine learning
algorithms for classification, clustering, regression and dimension reduction problems.
• Model
• The model acts as a definition of some stored data and handles the interactions with the database. In a web application, the
data can be stored in a relational database, non-relational database, an XML file, etc. A Django model is a Python class
that outlines the variables and methods for a particular type of data.
• Template
• In a typical Django web application, the template is simply an HTML page with a few extra placeholders. Django’s
template language can be used to create various forms of text files (XML, email, CSS, Javascript, CSV, etc.).
• View
• The view ties the model to the template. The view is where you write the code that actually generates the web pages.
View determines what data is to be displayed, retrieves the data from the database and passes the data to the template.
.py
settings.py
urls.py
wsgi.py
settings.
py
• Defines settings used by a Django application
• Referenced by wsgi.py to bootstrap the project loading
• Techniques for managing dev vs prod settings:
• Create settings-dev.py and settings-prod.py and use symlink to link settings.py
to the correct settings
• Factor out common settings into base-settings.py and import. Use
conditionals to load correct settings based on DEBUG or other
setting.
• WSGI: Web Server Gateway Interface.
Sample
Settings…
DEBUG = True
TEMPLATE_DEBUG =
True
ALLOWED_HOSTS =
[]
# Application
definition
INSTALLED_APPS =
(
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
Django
Apps
• Reusable modules
• django-admin.py startapp <app_name>
• Creates stub layout:
<APP_ROOT>
admin.py
models.py
templates
(directory
)
tests.py
views.y
urls.py
Django
Models
• Defined in models.py
•Typically inherit from django.db.models.Model
Example Model:
from django.db import models
class TestModel(models.Model):
name = models.CharField(max_length =
20) age = models.IntegerField()
Models
(cont’d)
• Default is to set NOT NULL on all fields. Override by adding
null = True to field definition:
name = models.CharField(max_length=20, null = True)
• Relationships defined through special field types:
models.OneToOneField(model)
models.ForeignKey(model)
models.ManyToManyField(model
)
Models
(cont’)
• Need Nulls in a Boolean Field? Use models.NullBooleanField()
• Set Default value with “default”:
count = models.IntegerField(default = 0)
• Use a inner Meta class to define additional options, especially useful
for abstract classes:
class TestModel(models.Model):
class Meta:
abstract = True
Model
Methods
• model.save(self, *args, **kwargs)
• model.delete(self, *args, **kwargs)
• model.get_absolute_url(self)
• model. str (self) [Python 3]
model. unicode (self) [Python 2]
• Override with super(MODEL,
self).save(*args, **kwargs)
Activating a
Model
• Add the app to INSTALLED_APPS in
settings.py
• Run manage.py validate
• Run manage.py syncdb
• Migrations
• Write custom script or manually handle migrations
• Use South
Selecting
Objects
• Models include a default manager called objects
• Manager methods allow selecting all or some instances
Question.objects.all()
Question.objects.get(pk = 1)// (primary key)
Use try block, throws DoesNotExist exception if no
match Question.objects.filter(created_date lt = ‘2014-01-01’)
• Returns QuerySet
Introspecting Legacy
Models
• manage.py inspectdb
• Cut and paste generated code into models.py – Easy!!
• django is the best suited for developing new applications, its quite possible
to integrate it into legacy databasses.
• onceyou’ve got Django set up, you’ll follow this general process to
integrate with exixteng database.
• passing parameters to jango by editing the Databases setting and assigning
values to the following keys
• NAME
• USER
Full
Sample
from django.db import models
datetime
from datetime import
class
QuestionList(ListView):
model = Question
context_object_name =
‘questions’
Django
Templates
• Very simple syntax:
variables = {{variable_name}}
template tags = {%tag%}
• Flexible – can be used to render html, text, csv, email, you name it!
• Dot notation – template engine attempts to resolve by looking for
matching attributes, hashes and methods
Question List
Template
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>List of Questions</title>
</head>
<body>
{%if questions%}
<ul>
{%for q in questions%}
<li>{{q.question_text}}</li>
{%endfor%}
</ul>
{%else%}
<p>No questions have been defined</p>
{%endif%}
</body>
</html>
urls.p
y
• Defines routes to send urls to various views
• Can use regular expressions
• Extract parameters from a url and pass to the view as a named
parameter:
r(‘^question/(?P<question_id>\d+)/$’,’views.question_detail’)
• Extensible – urls.py can include additional url files from apps:
r(‘^question/’,include(question.urls))
Hooking up the
QuestionList
from django.conf.urls import patterns, url, include urlpatterns =
patterns(‘’,(r’^questions/$’,’views.QuestionList’))
OR:
from django.conf.urls import patterns from views import
QuestionListView
urlpatterns =
patterns(‘’,(r’^questions/$’,’views.QuestionList.as_view
()))
Forms in
Django
• django.forms provides a class to build HTML forms and validation.
• Example:
from django import forms
class EditQuestionForm(forms.Form):
question_text = forms.CharField(max_length = 200)
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
super(MyView,self).dispatch(*args,
**kwargs)
Auth
•Decorators
Live in django.contrib.auth.decorators
• login_required
@login_required
def function_view(request):
….
• user_passes_test (can be used with lambda functions for real power)
–
@user_passes_test(lambda u: u.is_staff)
def function_view(request):
…
• has_perms – test for user permissions
• (can take any no.of arguments, but can only
have one expression)
Custom Auth Backend for
theBubble
Sending
Email
• django.core.mail includes functions and classes for handling email
• Set EMAIL_HOST in settings.py to outgoing mailserver
• Import send_mail for simple mail:
send_mail(subject, message, from, to_emails)
• Use django.template.render_to_string to format a message using a
template
• Use EmailMultiAlternatives to create a text message and attach a
html version as well.
Resourc
es
• Python – http://www.python.org
• Django – http://www.djangoproject.com
• Python Packages – https://pypi.python.org
• Django Packages – https://www.djangopackages.com