python-advanced



python-advanced

0 0


python-advanced

Python not quite so basic course

On Github catalyst-training / python-advanced

Catalyst

Advanced Python

Administrivia

Callable objects

Unit testing

Code

import unittest

import YourModule

class RunTests(unittest.TestCase):

    def test_something(self):
        # Given
        o = YourModule.YourObject()

        # When
        result = o.thisShouldDoSomething(100)

        # Then
        self.assertTrue(result)
- any method which begins with the text 'test_' is a test - you can also write 'setUp', 'tearDown' methods which will be called appropriately - there is also a rich selection of assertion functions - assertTrue, assertFalse, - assertEqual, assertAlmostEqual - assertRaises - etc

Running it

python -m unittest discover test/
.........................................................
----------------------------------------------------------------------
Ran 62 tests in 4.473s

OK
- This command will find all files in the 'test/' directory, that begin with 'test_' - and run them all....

Code coverage

WSGI

WSGI is a specification, where a web application is implemented as a function or callable object.

It tightly defines the input - which is passed to the function as arguments, and the output - which is returned from the function call.

This allow for writing a web application without any need for dealing with HTTP.

Once that has been done, the application can be tested without the need for a webserver, and can be served up by any webserver that can talk WSGI.

- This make web applications - portable - testable

Django

Django is a Python web application framework.

It really is easy to create a website using Django, and it's a good starting point for a real application.

It doesn't come bundled with python, so you need to install it yourself, depending on your OS, that could be as easy as:

$ sudo apt-get install python-django

Create a new project

$ django-admin startproject mysite
$ cd mysite
$ python manage.py migrate
$ python manage.py runserver

Then, go to http://127.0.0.1:8000

- we have chosen not to configure anything, and that means: - We have a SQL lite database - We don't have a webserver - we are serving pages on an odd port - etc - normally we would sort those things out by editing settings.py

Create an app

$ python manage.py startapp myapp
# mysite/settings.py
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
)

Make it do something

# mysite/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'myapp.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
)
# myapp/views.py
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def home(request):
    return HttpResponse("Here's the text of the Web page.")

Packages

  • A package is a directory full of Python modules.
    • Or sub-packages.
    • Or data files.
  • It must* have a file called __init__.py in it.
  • __init__.py doesn't have to have any code in it.

The messy past

  • distutils is deprecated
  • distribute is deprecated
  • setuptools < 0.6 is deprecated
  • buildout is... unconventional
  • eggs are deprecated
  • The documentation/HOWTOs are all LIES!

So what are you supposed to use?

So what are you supposed to use?

This page has all you need:

https://packaging.python.org/en/latest/current.html

What's a "setup.py"?

Cheat sheet

open source technologists

Catalyst Advanced Python