Python app serverless deployment

Overviews of best solution for serverless deployment: Amazon Web Services and Slam
25 August 2017   456

What is Slam?

Slam is a serverless deployment tool that allows you to deploy your Python functions or web applications to AWS Lambda, API Gateway and DynamoDB. It does so by generating a Cloudformation template that describes the deployment in its entirety. If you are deploying a web application, the Lambda function is deployed using the techniques described above to adapt API Gateway invocations to the WSGI format.

The package is called Slam, and can be installed like this:

$ pip install slam

Also, check small video tutorial:

Learn more at GitHub.

What is AWS?

Amazon Web Services (AWS) is a subsidiary of Amazon.com that provides on-demand cloud computing platforms to individuals, companies and governments, on a paid subscription basis with a free-tier option available for 12 months.

In AWS, Lambda is the function as a service offering. With this service, you can upload your Python, Node.js, Java or C# code and Lambda will store it and run it for you. To work with the Lambda service you upload your project packaged as a zip file, containing your own code plus any dependencies that are needed. You have to designate a function in your code as the entry point, and this function will be called by AWS when a client "invokes" your Lambda function.

The API Gateway service allows you to construct API endpoints, and configure what actions these endpoints trigger when a client sends requests to them. The service takes care of scaling, rate limiting, and even authenticating your clients if you wish to go that far. An API Gateway endpoint can be configured to invoke a Lambda function, so you can create endpoints in API Gateway that match the endpoints in your API, and connect each of them to a Lambda function, and with this combination of API Gateway and Lambda your clients can work with your API in the normal way, by sending HTTP requests.

The serverless platform for AWS is often described as having three main services. DynamoDB is a simple NoSQL database service. Why is a NoSQL database so popular in the serverless world? Well, serverless applications tend to be built as a collection of small and independent pieces (the so called microservices), so while you can use a relational database, that is sometimes overkill. DynamoDB tables do not need a schema, are very easy to setup and use, and follow the "only pay for what you use" model present in many AWS services.

What is YAPF?

A formatter for Python files, developed by Google team
30 October 2017   451

What is YAPF?

Most of the current formatters for Python --- e.g., autopep8, and pep8ify --- are made to remove lint errors from code. This has some obvious limitations. For instance, code that conforms to the PEP 8 guidelines may not be reformatted. But it doesn't mean that the code looks good.

YAPF takes a different approach. It's based off of 'clang-format', developed by Daniel Jasper. In essence, the algorithm takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn't violate the style guide. The idea is also similar to the 'gofmt' tool for the Go programming language: end all holy wars about formatting - if the whole codebase of a project is simply piped through YAPF whenever modifications are made, the style remains consistent throughout the project and there's no point arguing about style in every code review.

The ultimate goal is that the code YAPF produces is as good as the code that a programmer would write if they were following the style guide. It takes away some of the drudgery of maintaining your code.

Code examples

YAPF takes this code:

x = {  'a':37,'b':42,

'c':927}

y = 'hello ''world'
z = 'hello '+'world'
a = 'hello {}'.format('world')
class foo  (     object  ):
  def f    (self   ):
    return       37*-+2
  def g(self, x,y=42):
      return y
def f  (   a ) :
  return      37+-+a[42-x :  y**3]

and reformat it into:

x = {'a': 37, 'b': 42, 'c': 927}

y = 'hello ' 'world'
z = 'hello ' + 'world'
a = 'hello {}'.format('world')


class foo(object):
    def f(self):
        return 37 * -+2

    def g(self, x, y=42):
        return y


def f(a):
    return 37 + -+a[42 - x:y**3]

See GitHub for more information.