Created
June 7, 2014 18:07
-
-
Save Shahn-Auronas/4a9d5119af84c455e576 to your computer and use it in GitHub Desktop.
Django/Python Notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. django-admin.py startproject <mysite> | |
creates folder <mysite> | |
with: | |
manage.py: A command-line utility that lets you interact with this Django project in various ways. | |
mysite/: actual python package for the project | |
__init__.py: An empty file that tells Python that this directory should be considered a Python package. | |
settings.py: Settings/configuration for this Django project | |
urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. | |
wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. | |
2. test server from cd <mysite> | |
$ python manage.py runserver | |
create app: | |
$ python manage.py startapp <app name> | |
tell project that app is installed : add <app name> to INSTALLED_APPS in settings.py | |
run to see CREATE TABLE OUTPUT (does not actually run database) | |
$ python manage.py sql polls | |
check for errors in the model | |
$ pythong manage.py validate | |
output any custom (if applicable) SQL statements such as table modifications | |
or constraints define for the application | |
* create the model tables in the database | |
@ python manage.py syncdb | |
* invoke the shell for python | |
$ python manage.py shell | |
start the development server | |
$ python manage.py runserver | |
Views | |
open the <app_name>/views.py and write: | |
def index(request): | |
return HttpResponse("") | |
to call the view, map it to a URL using URLconf | |
import conventions: | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
ex: np.arange | |
do not (from <above_libs> import *) #because these are too large of packages | |
munge/munging/wrangling: manipulating unstructured data into clean data | |
import json | |
path = "" | |
#expression is called list comprehension. applying the json.loads operation to collection | |
records = [json.loads(line) for line in open(path)] | |
#extracting all the timezone fields, checking to see if it exist first | |
time_zones = [rec['tz'] for rec in records if 'tz' in rec] | |
#counting the time_zones using std lib in Py | |
def get_counts(sequence): | |
counts = {} | |
for x in sequence: | |
if x in counts: | |
counts[x] += 1 | |
else: | |
counts[x] = 1 | |
return counts | |
#using pandas data structure; DataFrame | |
from pandas import DataFrame, Series | |
import pandas as pd; import numpy as np | |
frame = DataFrame(records) | |
#call it to get the summary view | |
frame | |
#return the Series | |
frame['tz'][:10] | |
#use the Series object method: value_counts | |
tz_counts = frame['tz'].value_counts() | |
tz_counts[:10] | |
#the fillna function can replace missing values | |
clean_tz = frame['tz'].fillna('Missing') | |
clean_tz[clean_tz == ''] = 'Unknown' | |
tz_counts = clean_tz.value_counts() | |
tz_counts[:10] | |
#use the matplotlib to plot | |
tz_counts[:10].plot(kind='barh' rot=0) | |
#parsing the Series results to get agent | |
#ex: | |
frame['a'][50] | |
u'Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2' | |
results = Series([x.split()[0] for x in frame.a.dropna()]) | |
results[:5] | |
#prints just agent/browser and version | |
#close but not equal to one for floating point sums | |
np.allclose(<object>.groupby(['<>','<>']).prop.sum(), 1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment