The Setup 


OOP in Python, terminal, git, 

& configuring your Django dev environment



01/15/2014

Object oriented Programming


Save a script (called "walker") in the IDE with these specs:

A person (aka The Walker) walks one step at a time

 in either the left, right, forward, or back direction.  


Each step is randomly chosen, so The Walker is just as likely to step forward as they are to step left.  If The Walker takes 1000 steps, where will they land in terms of (x, y) coordinates?

What will their average (x,y) coordinates be if we conduct 10 trials, during each of which The Walker takes 1000 steps?

Can we convert this into a web app?



We may want to create a simple web dashboard 
which shows aggregate results (end coordinates and averages)


Or we could show the entire walk, step by step

How  else can we extend this?

First, let's setup our environment



What is PIP?

What is Virtualenv (and VirtualenvWrapper)?

What is Git?  Github?

Why MySQL?


Pip


Helps us install Python packages such as

Django

Requests

PIL

Virtualenv


We will have different Python projects on our computer

These projects require different libraries (dependencies)

Rather than pollute our global Python space, we can create separate, isolated environments for each of our projects.


VirtualenvWrapper is a library that builds on top of Virtualenv and provides us with useful commands 

Git


A version control system which allows us to track changes to our files over time, revert back to older versions, create new branches, and collaborate with others


Github is a service that hosts your Git backed projects

Create an account on Github

MYSQL


Powerful open source database that ties in nicely with Django
 
Postgres is very similar and would work just as well

In this case, we are going to use MySQL because of the GUI, SequelPro, which will be helpful for learning

Walker Problem Django-fied


What entities (objects) do we have to represent?

How are they related to each other?

How could we model this in Django?

what views would we present?



Aggregate results across all trials

Results within a given trial (chart of walkers)

Step by step flow for a given walker in a given trial

Mapping views to URLS


List our URLs

Given a URL, how do we actually run code 
and return an HTML page?

Crate Django Project


 django-admin.py startproject walker
mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

python manage.py runserver
pro tip: sudo chmod 777 ./manage.py

Settings.py 


Important module which defines the settings of your project

See IDE

Create Walker APP


  ./manage.py startapp walker
# Created a new directory called walker # With models.py, views.py, tests.py, admin.py

We  need to add this app to our settings file 
so that our Django project knows about it

Walker Data Models


Copy and paste the code from models.py in the IDE 
into your local models.py file inside walker

Let's update the settings file to support MySQL

Lastly, we need to create tables in database

pip install MySQL-python ./manage.py syncdb

Updating Models


What if we want to update one of our models?
With SQL, you need to update your table

South to the rescue
pip install south
# add south to your apps tuple in settings.py./manage.py syncdb ### creates table for South./manage.py schemamigration walker --initial./manage.py migrate walker --fake

Hello world View.py


Views are the code (function or class) 
that is run when a request  hits a particular URL
We map URLs to Views

from django.http import HttpResponse
def hello_world(request): return HttpResponse("Hello World")

The Setup: terminal, git, & configuring your Django dev environment

By benjaminplesser

The Setup: terminal, git, & configuring your Django dev environment

  • 1,015