Using Brownie with Codenvy


Brownie tutorial with Codenvy IDE

What is Brownie

Brownie is a python package that provides some interesting features not available in the python standard library. Brownie provides us with those utilities and data-structures not important enough to package and maintain our own versions. Also it provides us with tools not available with the python standard library at all, or at least not in the older versions.

Two of those data-structures are OrderedDict and Counter. They appeared in the collections module in python 2.7 standard library and brownie provides a compatible interface, so users of earlier python versions can depend on Brownie to make their python applications portable between different python interpreter versions.

OrderedDict is a Python Dictionary that remembers the order which items got inserted, something not guaranteed in the normal python dictionary.

Counter is also subclass of a Python Dictionary although it is used to efficiently counting key occurrences. All of this will become more clear a bit later.

Lets start a python project

First of all create a new python project using the Codenvy IDE. Select from menu ‘Project’, New->Create Project. new_project Name your new application accordingly, select Python as the project type, and leave PaaS as None. new_project-2 Next select the ‘simple python project’ and click finish.

The sample ‘Simple python application’ is based on bottle, a small and simple python web framework. The function ‘application’ inside the file is what the deployment environment is looking for when running the application. This application should return a valid wsgi response which is initiated with ‘start_response’ function. Lets just use this application as base for our brownie experiments.

Type in this sample code to get us started:

import bottle
import os
import sys
import brownie
from brownie.datastructures import OrderedDict as od
from brownie.datastructures import Counter as cnt

def application(environ, start_response):
    data = 'Brownie Tutorial With Codenvy Cloud IDE\n\n'
    data += ordereddict_example()
    start_response("200 OK", [
            ("Content-Type", "text/plain"),
            ("Content-Length", str(len(data)))
    return iter([data])

On the top of the code you can see the two import statement for the datastructure we are going to use.

Importing Brownie in our project

Now, if you try to run this code, it will fail with some cryptic message, this is because we have not instructed the python environment responsible for our application deployment, that we are importing a non standard package. In python packages this is done usually via the requirements.txt file that is present in the simple python application. This file currently is a one liner instructing the python env to install the bottle web framework. Just add another line with the word brownie.

An OrderedDict example

Add the ordereddict_example function like this:

def ordereddict_example():
    olist = od({1:1, 2:2})
    olist[3] = 3
    olist[4] = 4
    for i in range(4,20):
    ret = "This is a serialized ordered dictionary: \n%s\n"%str(olist)
    ret += "Iterating over this dictionary guaranties that results are outputed in the order they got inserted:\n"
    for key, value in olist.iteritems():
      ret += str(key) +':'+ str(value) + '\n'
    return ret

You are ready at this point to run your first brownie application. screen run project. You should see the messages we typed above with the ordered presentation of key value pairs in two different formats.

A Counter example

Counter will accept any iterable and hashable object and will return a dictionary containing the initial values as keys and the number of occurrences as a value for the key. Lets make a function that uses Counter. Type in this code:

def counter_example():
    alist = [1,2,2,3,3,3]
    count = cnt(alist)
    ret = "\nCounting of %s results in %s\n"%(str(alist),str(count))
    return ret

And add a call to this func inside the application entry point:

def application(environ, start_response):
    data = 'Brownie Tutorial With Codenvy Cloud IDE\n\n'
    data += ordereddict_example()
    data += counter_example()
    start_response("200 OK", [
            ("Content-Type", "text/plain"),
            ("Content-Length", str(len(data)))
    return iter([data])

Try running the code and you will see the result of counting the occurenses of items inside the alist container.